Skip to content

Commit 6adfbe0

Browse files
committed
dependsOn prototype simplified
1 parent b89c506 commit 6adfbe0

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ function shadergenerator(p5, fn) {
297297
this.useTemp = true;
298298
}
299299

300-
assertUsedInConditional() {
300+
assertUsedInConditional(branch) {
301301
this.usedInConditional = true;
302+
this.usedIn.push(branch);
302303
this.forceTemporaryVariable();
303304
}
304305

@@ -311,7 +312,7 @@ function shadergenerator(p5, fn) {
311312
const isUsedSatisfied = () => statement.usedInSatisfied.length >= 1;
312313
const isDepsSatisfied = () => statement.dependsOn.length === statement.dependsOnSatisfied.length;
313314
if (statement.insertionPoint > -1 || !statement.usedIn.length) return;
314-
if (statement.dependsOn.includes(this) && !statement.dependsOnSatisfied.includes(this)) {
315+
if (statement.dependsOn.some(d => d.node === this) && !statement.dependsOnSatisfied.includes(this)) {
315316
statement.dependsOnSatisfied.push(this);
316317
}
317318
if (statement.usedIn.includes(this) && !statement.usedInSatisfied.includes(this)) {
@@ -330,22 +331,17 @@ function shadergenerator(p5, fn) {
330331
let oldLength = context.declarations.length;
331332
result = this.getTemporaryVariable(context);
332333
let diff = context.declarations.length - 1 - oldLength;
333-
if (Array.isArray(this.dependsOn)){
334-
this.dependsOn.forEach(d => {
335-
context.updateComponents(d, diff > 0 ? diff : undefined);
336-
});
337-
} else {
338-
// Update the incorrect components
339-
this.dependsOn.nodesArray.forEach((nodeDependency, i) => {
340-
const originalComponents = this.dependsOn[i];
341-
const currentComponents = nodeDependency.componentNames.map(name => nodeDependency[name]);
342-
if (!originalComponents) return;
343-
const dependencies = originalComponents.map((component, i) =>
344-
component === currentComponents[i]
345-
)
346-
context.updateComponents(nodeDependency, diff > 0 ? diff : undefined, dependencies);
347-
})
348-
}
334+
diff = diff > 0 ? diff : undefined;
335+
this.dependsOn.forEach(dependency => {
336+
if (dependency.isVector) {
337+
const dependencies = dependency.originalComponents.map((component, i) =>
338+
component === dependency.currentComponents[i]
339+
);
340+
context.updateComponents(dependency.node, diff, dependencies);
341+
} else {
342+
context.updateComponents(dependency.node, diff);
343+
}
344+
});
349345
} else {
350346
result = this.toGLSL(context);
351347
}
@@ -600,18 +596,6 @@ function shadergenerator(p5, fn) {
600596

601597
super(isInternal, functionSignature.returnType);
602598

603-
if (userArgs.find(arg => isVectorNode(arg))) {
604-
this.dependsOn = { nodesArray: [] };
605-
}
606-
userArgs.forEach((arg, i) => {
607-
arg.usedIn.push(this);
608-
let arr = Array.isArray(this.dependsOn) ? this.dependsOn : this.dependsOn.nodesArray;
609-
if (isVectorType(arg)) {
610-
this.dependsOn[i] = [];
611-
arg.componentNames.forEach(name => this.dependsOn[i].push(arg[name]));
612-
}
613-
arr.push(arg);
614-
});
615599
this.name = name;
616600
this.args = userArgs;
617601
this.argumentTypes = functionSignature.args;
@@ -873,7 +857,7 @@ function shadergenerator(p5, fn) {
873857

874858
toGLSL(context) {
875859
const oldLength = context.declarations.length;
876-
this.dependsOn.forEach(dep => context.updateComponents(dep));
860+
this.dependsOn.forEach(dep => context.updateComponents(dep.node));
877861
const newLength = context.declarations.length;
878862
const diff = newLength - oldLength;
879863
this.insertionPoint += diff;
@@ -939,14 +923,14 @@ function shadergenerator(p5, fn) {
939923
}
940924
node = node.parent ? node.parent : node;
941925
value = value.parent ? value.parent : value;
942-
if ([node, value].some(n => this.dependsOn.includes(n))) {
926+
if ([node, value].some(n => this.dependsOn.some(d=>d.node===n))) {
943927
return;
944928
}
945-
node.assertUsedInConditional();
946-
this.dependsOn.push(node)
929+
node.assertUsedInConditional(this);
930+
this.dependsOn.push(makeDependencyObject(node))
947931
if (value.shouldUseTemporaryVariable()) {
948-
value.assertUsedInConditional();
949-
this.dependsOn.push(value);
932+
value.assertUsedInConditional(this);
933+
this.dependsOn.push(makeDependencyObject(value));
950934
}
951935
}
952936

@@ -1071,7 +1055,7 @@ function shadergenerator(p5, fn) {
10711055
}
10721056

10731057
function isConditionalNode(node) {
1074-
return (node instanceof ConditionalNode)
1058+
return (node instanceof ConditionalNode || node instanceof BranchNode)
10751059
}
10761060

10771061
function hasTemporaryVariable(node) {
@@ -1302,6 +1286,28 @@ function shadergenerator(p5, fn) {
13021286
}
13031287

13041288
// User function helpers
1289+
function makeDependencyObject(dep) {
1290+
if (isVectorType(dep)) {
1291+
return {
1292+
node: dep,
1293+
isVector: true,
1294+
originalComponents: [...dep.componentNames.map(name => dep[name])],
1295+
get currentComponents() {
1296+
return dep.componentNames.map(name => dep[name]);
1297+
}
1298+
};
1299+
} else {
1300+
return {
1301+
node: dep,
1302+
isVector: false
1303+
};
1304+
}
1305+
}
1306+
1307+
function makeDependencyArray(dependencies) {
1308+
return dependencies.map(dep => makeDependencyObject(dep));
1309+
}
1310+
13051311
function conformVectorParameters(value, vectorDimensions) {
13061312
// Allow arguments as arrays or otherwise. The following are all equivalent:
13071313
// ([0,0,0,0]) (0,0,0,0) (0) ([0])
@@ -1423,11 +1429,19 @@ function shadergenerator(p5, fn) {
14231429
function fnNodeConstructor(name, userArgs, properties, isInternal) {
14241430
let node = new FunctionCallNode(name, userArgs, properties, isInternal);
14251431
node = dynamicAddSwizzleTrap(node);
1426-
if (node.args.some(arg => arg.isUsedInConditional())) {
1427-
GLOBAL_SHADER.context.ifs.forEach(statement => {
1428-
statement.usedIn.push(node);
1432+
node.dependsOn = makeDependencyArray(node.args);
1433+
const dependsOnConditionals = node.args.map(arg => {
1434+
const conditionals = arg.usedIn.filter(n => isConditionalNode(n)).map(c => {
1435+
if (c instanceof BranchNode) {
1436+
return c.parent;
1437+
} else {
1438+
return c;
1439+
}
14291440
});
1430-
}
1441+
return conditionals;
1442+
}).flat();
1443+
dependsOnConditionals.forEach(conditional => conditional.usedIn.push(node));
1444+
14311445
return node;
14321446
}
14331447

0 commit comments

Comments
 (0)