Skip to content

Commit 1782cb5

Browse files
authored
Merge pull request #7706 from processing/glsl-types
Glsl types
2 parents 3e2d17a + 209a63d commit 1782cb5

File tree

4 files changed

+100
-110
lines changed

4 files changed

+100
-110
lines changed

src/webgl/ShaderGenerator.js

+70-62
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function shadergenerator(p5, fn) {
7676
type: 'Literal',
7777
value: node.operator,
7878
}
79-
79+
8080
const standardReplacement = (node) => {
8181
node.type = 'CallExpression'
8282
node.callee = {
@@ -93,11 +93,11 @@ function shadergenerator(p5, fn) {
9393
['r', 'g', 'b', 'a'],
9494
['s', 't', 'p', 'q']
9595
];
96-
96+
9797
let isSwizzle = swizzleSets.some(set =>
9898
[...property].every(char => set.includes(char))
9999
) && node.argument.type === 'MemberExpression';
100-
100+
101101
if (isSwizzle) {
102102
node.type = 'MemberExpression';
103103
node.object = {
@@ -114,7 +114,7 @@ function shadergenerator(p5, fn) {
114114
};
115115
} else {
116116
standardReplacement(node);
117-
}
117+
}
118118
} else {
119119
standardReplacement(node);
120120
}
@@ -139,7 +139,7 @@ function shadergenerator(p5, fn) {
139139
}
140140
},
141141
Identifier(node, _state, _ancestors) {
142-
if (_state.varyings[node.name]
142+
if (_state.varyings[node.name]
143143
&& !_ancestors.some(a => a.type === 'AssignmentExpression' && a.left === node)) {
144144
node.type = 'ExpressionStatement';
145145
node.expression = {
@@ -297,7 +297,7 @@ function shadergenerator(p5, fn) {
297297
this.useTemp = true;
298298
}
299299

300-
assertUsedInConditional(branch) {
300+
assertUsedInConditional(branch) {
301301
this.usedInConditional = true;
302302
this.usedIn.push(branch);
303303
this.forceTemporaryVariable();
@@ -316,8 +316,8 @@ function shadergenerator(p5, fn) {
316316
statement.dependsOnSatisfied.push(this);
317317
}
318318
if (statement.usedIn.includes(this) && !statement.usedInSatisfied.includes(this)) {
319-
statement.usedInSatisfied.push(this);
320-
}
319+
statement.usedInSatisfied.push(this);
320+
}
321321
if (isDepsSatisfied() && isUsedSatisfied()) {
322322
statement.saveState(context, isDepsSatisfied(), isUsedSatisfied());
323323
}
@@ -334,7 +334,7 @@ function shadergenerator(p5, fn) {
334334
diff = diff > 0 ? diff : undefined;
335335
this.dependsOn.forEach(dependency => {
336336
if (dependency.isVector) {
337-
const dependencies = dependency.originalComponents.map((component, i) =>
337+
const dependencies = dependency.originalComponents.map((component, i) =>
338338
component === dependency.currentComponents[i]
339339
);
340340
context.updateComponents(dependency.node, diff, dependencies);
@@ -345,14 +345,14 @@ function shadergenerator(p5, fn) {
345345
} else {
346346
result = this.toGLSL(context);
347347
}
348-
this.checkConditionalDependencies(context)
348+
this.checkConditionalDependencies(context)
349349
return result;
350350
}
351351

352352
shouldUseTemporaryVariable() {
353353
if (this.componentsChanged || hasTemporaryVariable(this) || this.useTemp) { return true; }
354354
if (this.isInternal || isVariableNode(this) || isConditionalNode(this) || this.type === 'sampler2D') { return false; }
355-
355+
356356
// return false;
357357
// Swizzles must use temporary variables as otherwise they will not be registered
358358
let score = 0;
@@ -480,7 +480,7 @@ function shadergenerator(p5, fn) {
480480
this.originalValues = conformVectorParameters(values, parseInt(type.slice(3)));
481481
this.componentNames = ['x', 'y', 'z', 'w'].slice(0, this.originalValues.length);
482482
}
483-
483+
484484
addVectorComponents() {
485485
const values = this.originalValues;
486486
this.componentsChanged = false;
@@ -556,8 +556,8 @@ function shadergenerator(p5, fn) {
556556
const findBestOverload = function (best, current) {
557557
current = determineFunctionSignature(current);
558558
if (!current.valid) { return best; }
559-
if (!best || current.similarity > best.similarity) {
560-
best = current;
559+
if (!best || current.similarity > best.similarity) {
560+
best = current;
561561
}
562562
return best;
563563
}
@@ -568,7 +568,7 @@ function shadergenerator(p5, fn) {
568568

569569
if (!functionSignature || !functionSignature.valid) {
570570
const argsStrJoin = (args) => `(${args.map((arg) => arg).join(', ')})`;
571-
const expectedArgsString = Array.isArray(properties) ?
571+
const expectedArgsString = Array.isArray(properties) ?
572572
properties.map(prop => argsStrJoin(prop.args)).join(' or ')
573573
: argsStrJoin(properties.args);
574574
const providedArgsString = argsStrJoin(userArgs.map((a)=>getType(a)));
@@ -645,7 +645,7 @@ function shadergenerator(p5, fn) {
645645
}
646646
}
647647

648-
//
648+
//
649649
class VaryingNode extends VariableNode {
650650
constructor(name, type, isInternal = false) {
651651
super(name, type, isInternal);
@@ -771,36 +771,36 @@ function shadergenerator(p5, fn) {
771771

772772
// Conditions and logical modifiers
773773
BaseNode.prototype.equalTo = function(other) {
774-
return binaryExpressionNodeConstructor(this, this.enforceType(other), '==');
774+
return binaryExpressionNodeConstructor(this, this.enforceType(other), '==');
775775
}
776776

777777
BaseNode.prototype.greaterThan = function(other) {
778-
return binaryExpressionNodeConstructor(this, this.enforceType(other), '>');
778+
return binaryExpressionNodeConstructor(this, this.enforceType(other), '>');
779779
}
780780

781781
BaseNode.prototype.greaterThanEqualTo = function(other) {
782-
return binaryExpressionNodeConstructor(this, this.enforceType(other), '>=');
782+
return binaryExpressionNodeConstructor(this, this.enforceType(other), '>=');
783783
}
784784

785785
BaseNode.prototype.lessThan = function(other) {
786-
return binaryExpressionNodeConstructor(this, this.enforceType(other), '<');
786+
return binaryExpressionNodeConstructor(this, this.enforceType(other), '<');
787787
}
788-
788+
789789
BaseNode.prototype.lessThanEqualTo = function(other) {
790790
return binaryExpressionNodeConstructor(this, this.enforceType(other), '<='); }
791-
791+
792792
BaseNode.prototype.not = function() {
793-
return new UnaryExpressionNode(this.condition, '!', true);
793+
return new UnaryExpressionNode(this.condition, '!', true);
794794
}
795-
795+
796796
BaseNode.prototype.or = function(other) {
797-
return new binaryExpressionNodeConstructor(this, this.enforceType(other), '||', true);
797+
return new binaryExpressionNodeConstructor(this, this.enforceType(other), '||', true);
798798
}
799-
799+
800800
BaseNode.prototype.and = function(other) {
801-
return new binaryExpressionNodeConstructor(this, this.enforceType(other), '&&', true);
801+
return new binaryExpressionNodeConstructor(this, this.enforceType(other), '&&', true);
802802
}
803-
803+
804804
function branch(callback) {
805805
const branch = new BranchNode();
806806
callback();
@@ -847,8 +847,8 @@ function shadergenerator(p5, fn) {
847847
};
848848

849849
saveState(context, usedInSatisfied, dependsOnSatisfied) {
850-
this.states.push({
851-
line: context.declarations.length,
850+
this.states.push({
851+
line: context.declarations.length,
852852
usedInSatisfied,
853853
dependsOnSatisfied
854854
});
@@ -951,13 +951,13 @@ function shadergenerator(p5, fn) {
951951
} else {
952952
result = value.toGLSLBase(context);
953953
}
954-
954+
955955
if (isVariableNode(node) || hasTemporaryVariable(node)) {
956956
statement = `${node.toGLSLBase(context)} = ${result};`;
957-
}
957+
}
958958
else if (isFloatNode(node) && node.name) {
959959
statement = `${node.parent.toGLSLBase(context)}.${node.name} = ${result};`;
960-
}
960+
}
961961
else {
962962
node.temporaryVariable = `temp_${context.getNextID()}`;
963963
statement = `${node.type} ${node.toGLSLBase(context)} = ${result};`
@@ -994,11 +994,11 @@ function shadergenerator(p5, fn) {
994994
if (Array.isArray(values)) {
995995
for(let val of values) {
996996
if (isVectorType(val)) {
997-
length += parseInt(val.type.slice(3));
997+
length += parseInt(val.type.slice(3));
998998
}
999999
else length += 1;
10001000
}
1001-
}
1001+
}
10021002
else if (isVectorType(values)) {
10031003
length += parseInt(val.type.slice(3));
10041004
}
@@ -1208,7 +1208,7 @@ function shadergenerator(p5, fn) {
12081208
});
12091209
codeLines.push(...finalVaryingAssignments);
12101210
}
1211-
1211+
12121212
codeLines.push(' return finalReturnValue;', '}');
12131213
this.output[hookName] = codeLines.join('\n');
12141214
this.resetGLSLContext();
@@ -1220,7 +1220,7 @@ function shadergenerator(p5, fn) {
12201220
GLOBAL_SHADER[hookTypes.name](userOverride);
12211221
};
12221222
});
1223-
1223+
12241224

12251225
this.cleanup = () => {
12261226
for (const key in windowOverrides) {
@@ -1230,8 +1230,8 @@ function shadergenerator(p5, fn) {
12301230
}
12311231

12321232
registerVarying(node, value) {
1233-
if (!Array.isArray(this.context.varyings[node.name])) {
1234-
this.context.varyings[node.name] = [];
1233+
if (!Array.isArray(this.context.varyings[node.name])) {
1234+
this.context.varyings[node.name] = [];
12351235
}
12361236
this.context.varyings[node.name].push({ node, value });
12371237
this.output.vertexDeclarations.add(`OUT ${node.type} ${node.name};`);
@@ -1303,7 +1303,7 @@ function shadergenerator(p5, fn) {
13031303
};
13041304
}
13051305
}
1306-
1306+
13071307
function makeDependencyArray(dependencies) {
13081308
return dependencies.map(dep => makeDependencyObject(dep));
13091309
}
@@ -1336,15 +1336,15 @@ function shadergenerator(p5, fn) {
13361336
['r', 'g', 'b', 'a'],
13371337
['s', 't', 'p', 'q']
13381338
].map(s => s.slice(0, size));
1339-
return {
1339+
return {
13401340
get(target, property, receiver) {
13411341
if (property in target) {
13421342
return Reflect.get(...arguments);
13431343
} else {
13441344
for (const set of swizzleSets) {
13451345
if ([...property].every(char => set.includes(char))) {
13461346
if (property.length === 1) {
1347-
return target[swizzleSets[0][set.indexOf(property[0])]]
1347+
return target[swizzleSets[0][set.indexOf(property[0])]]
13481348
}
13491349
const components = [...property].map(char => {
13501350
const index = set.indexOf(char);
@@ -1376,7 +1376,7 @@ function shadergenerator(p5, fn) {
13761376
}
13771377
}
13781378
}
1379-
1379+
13801380
// User functions
13811381
fn.If = function (condition, branch) {
13821382
return new ConditionalNode(condition, branch);
@@ -1390,7 +1390,7 @@ function shadergenerator(p5, fn) {
13901390
const props = { args: ['sampler2D', 'vec2'], returnType: 'vec4', isp5Function: true };
13911391
return fnNodeConstructor('getTexture', userArgs, props);
13921392
}
1393-
1393+
13941394
// Generating uniformFloat, uniformVec, createFloat, etc functions
13951395
// Maps a GLSL type to the name suffix for method names
13961396
const GLSLTypesToIdentifiers = {
@@ -1407,7 +1407,7 @@ function shadergenerator(p5, fn) {
14071407
const size = _size ? _size : parseInt(node.type.slice(3));
14081408
node = new Proxy(node, swizzleTrap(size));
14091409
node.addVectorComponents();
1410-
}
1410+
}
14111411
return node;
14121412
}
14131413

@@ -1480,8 +1480,8 @@ function shadergenerator(p5, fn) {
14801480
return GLOBAL_SHADER[uniformMethodName](...args);
14811481
};
14821482

1483-
1484-
// We don't need a createTexture method.
1483+
1484+
// We don't need a texture creation method.
14851485
if (glslType === 'sampler2D') { continue; }
14861486

14871487
const varyingMethodName = `varying${typeIdentifier}`;
@@ -1493,16 +1493,24 @@ function shadergenerator(p5, fn) {
14931493
return GLOBAL_SHADER[varyingMethodName](name);
14941494
};
14951495

1496-
// Generate the create*() Methods for creating variables in shaders
1497-
const createMethodName = `create${typeIdentifier}`;
1498-
fn[createMethodName] = function (...value) {
1499-
if (glslType.startsWith('vec')) {
1500-
value = conformVectorParameters(value, parseInt(glslType.slice(3)));
1496+
// Generate the creation methods for creating variables in shaders
1497+
const originalFn = fn[glslType];
1498+
fn[glslType] = function (...value) {
1499+
if (GLOBAL_SHADER?.isGenerating) {
1500+
if (glslType.startsWith('vec')) {
1501+
value = conformVectorParameters(value, parseInt(glslType.slice(3)));
1502+
} else {
1503+
value = value[0];
1504+
}
1505+
return nodeConstructors[glslType](value);
1506+
} else if (originalFn) {
1507+
return originalFn.apply(this, value);
15011508
} else {
1502-
value = value[0];
1509+
p5._friendlyError(
1510+
`It looks like you've called ${glslType} outside of a shader's modify() function.`
1511+
);
15031512
}
1504-
return nodeConstructors[glslType](value);
1505-
}
1513+
};
15061514
}
15071515

15081516
// GLSL Built in functions
@@ -1546,16 +1554,16 @@ function shadergenerator(p5, fn) {
15461554
'log': { args: ['genType'], returnType: 'genType', isp5Function: true},
15471555
'log2': { args: ['genType'], returnType: 'genType', isp5Function: false},
15481556
'max': [
1549-
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: true},
1550-
{ args: ['genType', 'float'], returnType: 'genType', isp5Function: true},
1557+
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: true},
1558+
{ args: ['genType', 'float'], returnType: 'genType', isp5Function: true},
15511559
],
15521560
'min': [
1553-
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: true},
1554-
{ args: ['genType', 'float'], returnType: 'genType', isp5Function: true},
1561+
{ args: ['genType', 'genType'], returnType: 'genType', isp5Function: true},
1562+
{ args: ['genType', 'float'], returnType: 'genType', isp5Function: true},
15551563
],
15561564
'mix': [
1557-
{ args: ['genType', 'genType', 'genType'], returnType: 'genType', isp5Function: false},
1558-
{ args: ['genType', 'genType', 'float'], returnType: 'genType', isp5Function: false},
1565+
{ args: ['genType', 'genType', 'genType'], returnType: 'genType', isp5Function: false},
1566+
{ args: ['genType', 'genType', 'float'], returnType: 'genType', isp5Function: false},
15591567
],
15601568
// 'mod': {},
15611569
// 'modf': {},
@@ -1582,7 +1590,7 @@ function shadergenerator(p5, fn) {
15821590
// 'notEqual': {},
15831591
'reflect': { args: ['genType', 'genType'], returnType: 'genType', isp5Function: false},
15841592
'refract': { args: ['genType', 'genType', 'float'], returnType: 'genType', isp5Function: false},
1585-
1593+
15861594
////////// Texture sampling //////////
15871595
'texture': {args: ['sampler2D', 'vec2'], returnType: 'vec4', isp5Function: true},
15881596
}

0 commit comments

Comments
 (0)