Skip to content

Commit b89c506

Browse files
committed
ifStatement dependents fixed
1 parent 94dc159 commit b89c506

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function shadergenerator(p5, fn) {
3737
}
3838
const generator = new ShaderGenerator(generatorFunction, this, options.srcLocations);
3939
const generatedModifyArgument = generator.generate();
40-
console.log(generatedModifyArgument['Vertex getCameraInputs'])
4140
return oldModify.call(this, generatedModifyArgument);
4241
}
4342
else {
@@ -307,33 +306,19 @@ function shadergenerator(p5, fn) {
307306
return this.usedInConditional;
308307
}
309308

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) {
309+
checkConditionalDependencies(context) {
320310
context.ifs.forEach((statement) => {
321-
if (statement.insertionPoint > -1) return;
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-
);
311+
const isUsedSatisfied = () => statement.usedInSatisfied.length >= 1;
312+
const isDepsSatisfied = () => statement.dependsOn.length === statement.dependsOnSatisfied.length;
313+
if (statement.insertionPoint > -1 || !statement.usedIn.length) return;
314+
if (statement.dependsOn.includes(this) && !statement.dependsOnSatisfied.includes(this)) {
315+
statement.dependsOnSatisfied.push(this);
330316
}
331-
console.log(dependentOnConditional)
332-
if (dependentOnConditional.includes(this)) {
333-
console.log(context.declarations.join('\n'))
334-
}
335-
if (statement.dependsOn.length === statement.dependeciesFulfilled) {
336-
statement.saveState(context);
317+
if (statement.usedIn.includes(this) && !statement.usedInSatisfied.includes(this)) {
318+
statement.usedInSatisfied.push(this);
319+
}
320+
if (isDepsSatisfied() && isUsedSatisfied()) {
321+
statement.saveState(context, isDepsSatisfied(), isUsedSatisfied());
337322
}
338323
});
339324
}
@@ -364,10 +349,7 @@ function shadergenerator(p5, fn) {
364349
} else {
365350
result = this.toGLSL(context);
366351
}
367-
const depsUsedInConditional = this.dependentsUsedInConditional();
368-
if (this.isUsedInConditional() || depsUsedInConditional.length > 0) {
369-
this.checkConditionalDependencies(context, depsUsedInConditional)
370-
};
352+
this.checkConditionalDependencies(context)
371353
return result;
372354
}
373355

@@ -845,8 +827,11 @@ function shadergenerator(p5, fn) {
845827
class ConditionalNode {
846828
constructor(condition, branchCallback) {
847829
this.dependsOn = [];
830+
this.usedIn = [];
831+
this.dependsOnSatisfied = [];
832+
this.usedInSatisfied = [];
833+
this.states = [];
848834
this.if(condition, branchCallback);
849-
this.dependeciesFulfilled = [];
850835
this.insertionPoint = -1;
851836
this.elseIfs = [];
852837
this.elseBranch = null;
@@ -877,10 +862,13 @@ function shadergenerator(p5, fn) {
877862
return new ConditionalDiscard(this.condition);
878863
};
879864

880-
saveState(context) {
881-
if (this.insertionPoint = -1) {
882-
this.insertionPoint = context.declarations.length;
883-
}
865+
saveState(context, usedInSatisfied, dependsOnSatisfied) {
866+
this.states.push({
867+
line: context.declarations.length,
868+
usedInSatisfied,
869+
dependsOnSatisfied
870+
});
871+
this.insertionPoint = context.declarations.length - 1;
884872
}
885873

886874
toGLSL(context) {
@@ -1207,6 +1195,7 @@ function shadergenerator(p5, fn) {
12071195
}
12081196

12091197
this.context.ifs.forEach((statement) => {
1198+
if (statement.usedIn.length === 0) { return; }
12101199
const lines = statement.toGLSL(this.context);
12111200
this.context.declarations.splice(statement.insertionPoint, 0, lines);
12121201
})
@@ -1432,12 +1421,14 @@ function shadergenerator(p5, fn) {
14321421
}
14331422

14341423
function fnNodeConstructor(name, userArgs, properties, isInternal) {
1435-
const node = new FunctionCallNode(name, userArgs, properties, isInternal)
1436-
if (node.type.startsWith('vec')) {
1437-
return dynamicAddSwizzleTrap(node);
1438-
} else {
1439-
return node;
1424+
let node = new FunctionCallNode(name, userArgs, properties, isInternal);
1425+
node = dynamicAddSwizzleTrap(node);
1426+
if (node.args.some(arg => arg.isUsedInConditional())) {
1427+
GLOBAL_SHADER.context.ifs.forEach(statement => {
1428+
statement.usedIn.push(node);
1429+
});
14401430
}
1431+
return node;
14411432
}
14421433

14431434
const nodeConstructors = {

0 commit comments

Comments
 (0)