Skip to content

Commit 94dc159

Browse files
committed
temp
1 parent fa21c5a commit 94dc159

File tree

1 file changed

+63
-32
lines changed

1 file changed

+63
-32
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,39 @@ function shadergenerator(p5, fn) {
299299
}
300300

301301
assertUsedInConditional() {
302-
this.usedInConditional = true;
303-
this.forceTemporaryVariable();
302+
this.usedInConditional = true;
303+
this.forceTemporaryVariable();
304304
}
305-
306-
checkConditionalDependencies(context) {
307-
// if (!this.usedInConditional) return;
305+
306+
isUsedInConditional() {
307+
return this.usedInConditional;
308+
}
309+
310+
dependentsUsedInConditional() {
311+
let thisDeps = Array.isArray(this.dependsOn) ? this.dependsOn : this.dependsOn.nodesArray
312+
thisDeps = thisDeps.map((d) => {
313+
return Array.isArray(d) ? d[0].parent : d;
314+
});
315+
let depsUsed = thisDeps.filter(d => d.isUsedInConditional());
316+
return depsUsed;
317+
}
318+
319+
checkConditionalDependencies(context, dependentOnConditional) {
308320
context.ifs.forEach((statement) => {
309321
if (statement.insertionPoint > -1) return;
310-
statement.dependeciesFulfilled += statement.dependsOn
311-
.filter((dependecy) => dependecy === this)
312-
.length;
313-
// console.log("CHECKING: ");
314-
// console.log("DEPENDS ON, this: ", statement.dependsOn, this);
315-
// console.log("this === any dep:", statement.dependsOn.filter(d => d == this).length)
316-
// console.log("FULFILLED / TOTAL: ", statement.dependeciesFulfilled ,'/', statement.dependsOn.length);
317-
// console.log(context.declarations.join('\n'));
318-
// console.log('\n')
322+
if (statement.dependsOn.includes(this)
323+
&& !statement.dependeciesFulfilled.includes(this)) {
324+
statement.dependeciesFulfilled.push(this);
325+
this.oldName = this.toGLSLBase(context);
326+
this.temporaryVariable = `temp_${context.getNextID()}`;
327+
context.declarations.push(
328+
` ${this.type} ${this.toGLSLBase(context)} = ${this.oldName};`
329+
);
330+
}
331+
console.log(dependentOnConditional)
332+
if (dependentOnConditional.includes(this)) {
333+
console.log(context.declarations.join('\n'))
334+
}
319335
if (statement.dependsOn.length === statement.dependeciesFulfilled) {
320336
statement.saveState(context);
321337
}
@@ -330,28 +346,36 @@ function shadergenerator(p5, fn) {
330346
result = this.getTemporaryVariable(context);
331347
let diff = context.declarations.length - 1 - oldLength;
332348
if (Array.isArray(this.dependsOn)){
333-
this.dependsOn.forEach(d => context.updateComponents(d, diff > 0 ? diff : undefined));
349+
this.dependsOn.forEach(d => {
350+
context.updateComponents(d, diff > 0 ? diff : undefined);
351+
});
334352
} else {
335-
this.dependsOn.nodesArray.forEach((node, i) => {
336-
const originalComponents = this.dependsOn[i];
337-
const currentComponents = node.componentNames.map(name => node[name]);
338-
if (!originalComponents) return;
339-
const dependencies = originalComponents.map((component, i) =>
340-
component === currentComponents[i]
341-
)
342-
context.updateComponents(node, diff > 0 ? diff : undefined, dependencies);
343-
})
353+
// Update the incorrect components
354+
this.dependsOn.nodesArray.forEach((nodeDependency, i) => {
355+
const originalComponents = this.dependsOn[i];
356+
const currentComponents = nodeDependency.componentNames.map(name => nodeDependency[name]);
357+
if (!originalComponents) return;
358+
const dependencies = originalComponents.map((component, i) =>
359+
component === currentComponents[i]
360+
)
361+
context.updateComponents(nodeDependency, diff > 0 ? diff : undefined, dependencies);
362+
})
344363
}
345364
} else {
346365
result = this.toGLSL(context);
347366
}
348-
this.checkConditionalDependencies(context);
367+
const depsUsedInConditional = this.dependentsUsedInConditional();
368+
if (this.isUsedInConditional() || depsUsedInConditional.length > 0) {
369+
this.checkConditionalDependencies(context, depsUsedInConditional)
370+
};
349371
return result;
350372
}
351373

352374
shouldUseTemporaryVariable() {
353375
if (this.componentsChanged || hasTemporaryVariable(this) || this.useTemp) { return true; }
354376
if (this.isInternal || isVariableNode(this) || isConditionalNode(this) || this.type === 'sampler2D') { return false; }
377+
378+
// return false;
355379
// Swizzles must use temporary variables as otherwise they will not be registered
356380
let score = 0;
357381
score += isFunctionCallNode(this) * 2;
@@ -504,7 +528,7 @@ function shadergenerator(p5, fn) {
504528
}
505529

506530
toGLSL(context) {
507-
if (!this.componentsChanged || !this.defined) {
531+
if ((!this.componentsChanged || !this.defined) && !this.oldName) {
508532
let glslArgs = this.componentNames.map((_name, i) => this.originalValues[i].toGLSLBase(context)).join(', ');
509533
this.defined = true;
510534
return `${this.type}(${glslArgs})`;
@@ -616,6 +640,7 @@ function shadergenerator(p5, fn) {
616640
if (isIntType(argNode) && this.argumentTypes[i] != 'float') {
617641
argNode = argNode.toFloat();
618642
}
643+
argNode.toGLSLBase(context);
619644
return argNode.toGLSLBase(context);
620645
}).join(', ');
621646
return argsString;
@@ -821,7 +846,7 @@ function shadergenerator(p5, fn) {
821846
constructor(condition, branchCallback) {
822847
this.dependsOn = [];
823848
this.if(condition, branchCallback);
824-
this.dependeciesFulfilled = 0;
849+
this.dependeciesFulfilled = [];
825850
this.insertionPoint = -1;
826851
this.elseIfs = [];
827852
this.elseBranch = null;
@@ -854,9 +879,7 @@ function shadergenerator(p5, fn) {
854879

855880
saveState(context) {
856881
if (this.insertionPoint = -1) {
857-
context.declarations.join('\n');
858882
this.insertionPoint = context.declarations.length;
859-
console.log(this.insertionPoint);
860883
}
861884
}
862885

@@ -927,9 +950,12 @@ function shadergenerator(p5, fn) {
927950
this.assignments.push({ node, value });
928951
}
929952
node = node.parent ? node.parent : node;
953+
value = value.parent ? value.parent : value;
954+
if ([node, value].some(n => this.dependsOn.includes(n))) {
955+
return;
956+
}
930957
node.assertUsedInConditional();
931958
this.dependsOn.push(node)
932-
value = value.parent ? value.parent : value;
933959
if (value.shouldUseTemporaryVariable()) {
934960
value.assertUsedInConditional();
935961
this.dependsOn.push(value);
@@ -1235,8 +1261,8 @@ function shadergenerator(p5, fn) {
12351261
this.context.varyings[node.name] = [];
12361262
}
12371263
this.context.varyings[node.name].push({ node, value });
1238-
this.output.vertexDeclarations.add(`out ${node.type} ${node.name};`);
1239-
this.output.fragmentDeclarations.add(`in ${node.type} ${node.name};`);
1264+
this.output.vertexDeclarations.add(`OUT ${node.type} ${node.name};`);
1265+
this.output.fragmentDeclarations.add(`IN ${node.type} ${node.name};`);
12401266
}
12411267

12421268
resetGLSLContext() {
@@ -1364,6 +1390,11 @@ function shadergenerator(p5, fn) {
13641390
fn.instanceID = function() {
13651391
return variableConstructor('gl_InstanceID', 'int');
13661392
}
1393+
1394+
fn.getTexture = function(...userArgs) {
1395+
const props = { args: ['sampler2D', 'vec2'], returnType: 'vec4', isp5Function: true };
1396+
return fnNodeConstructor('getTexture', userArgs, props);
1397+
}
13671398

13681399
// Generating uniformFloat, uniformVec, createFloat, etc functions
13691400
// Maps a GLSL type to the name suffix for method names

0 commit comments

Comments
 (0)