Skip to content

Commit 618e4c8

Browse files
authored
Merge pull request #8641 from processing/fix/shared-variable
Fix a bug where shared strands variables are detected in the wrong spot
2 parents 05394c9 + c30b018 commit 618e4c8

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

src/strands/p5.strands.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ function strands(p5, fn) {
4141
ctx.vertexDeclarations = new Set();
4242
ctx.fragmentDeclarations = new Set();
4343
ctx.hooks = [];
44-
ctx.globalAssignments = [];
4544
ctx.backend = backend;
4645
ctx.active = active;
4746
ctx.renderer = renderer;
@@ -63,7 +62,6 @@ function strands(p5, fn) {
6362
ctx.vertexDeclarations = new Set();
6463
ctx.fragmentDeclarations = new Set();
6564
ctx.hooks = [];
66-
ctx.globalAssignments = [];
6765
ctx.active = false;
6866
p5.disableFriendlyErrors = ctx.previousFES;
6967
for (const key in ctx.windowOverrides) {

src/strands/strands_api.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) {
427427
typeInfo,
428428
usedInVertex: false,
429429
usedInFragment: false,
430-
declared: false
431430
});
432431

433432
return createStrandsNode(id, dimension, strandsContext);

src/strands/strands_codegen.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ export function generateShaderCode(strandsContext) {
4747
backend.generateBlock(blockID, strandsContext, generationContext);
4848
}
4949

50-
// Process any unvisited global assignments to ensure side effects are generated
51-
for (const assignmentNodeID of strandsContext.globalAssignments) {
52-
if (!generationContext.visitedNodes.has(assignmentNodeID)) {
53-
// This assignment hasn't been visited yet, so we need to generate it
54-
backend.generateAssignment(generationContext, strandsContext.dag, assignmentNodeID);
55-
generationContext.visitedNodes.add(assignmentNodeID);
56-
}
57-
}
58-
59-
// Reset global assignments for next hook
60-
strandsContext.globalAssignments = [];
61-
6250
const firstLine = backend.hookEntry(hookType);
6351
let returnType;
6452
if (hookType.returnType.properties) {

src/strands/strands_node.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ export class StrandsNode {
6969
const assignmentID = getOrCreateNode(dag, assignmentNode);
7070
recordInBasicBlock(cfg, cfg.currentBlock, assignmentID);
7171

72-
// Track for global assignments processing
73-
this.strandsContext.globalAssignments.push(assignmentID);
74-
7572
// Simply update this node to be a variable node with the identifier
7673
// This ensures it always generates the variable name in expressions
7774
const variableNodeData = createNodeData({
@@ -135,9 +132,6 @@ export class StrandsNode {
135132
const assignmentID = getOrCreateNode(dag, assignmentNode);
136133
recordInBasicBlock(cfg, cfg.currentBlock, assignmentID);
137134

138-
// Track for global assignments processing in the current hook context
139-
this.strandsContext.globalAssignments.push(assignmentID);
140-
141135
// Simply update this node to be a variable node with the identifier
142136
// This ensures it always generates the variable name in expressions
143137
const variableNodeData = createNodeData({

test/unit/webgl/p5.Shader.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,38 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
17701770
assert.approximately(centerColor[2], 255, 5); // Blue component
17711771
});
17721772

1773+
test('handle passing a value between fragment hooks only while having a vertex hook', () => {
1774+
myp5.createCanvas(50, 50, myp5.WEBGL);
1775+
myp5.pixelDensity(1);
1776+
1777+
const testShader = myp5.baseMaterialShader().modify(() => {
1778+
let processedNormal = myp5.sharedVec3();
1779+
myp5.objectInputs.begin();
1780+
myp5.objectInputs.position += [0, 0, 0];
1781+
myp5.objectInputs.end();
1782+
1783+
myp5.pixelInputs.begin();
1784+
processedNormal = myp5.normalize(myp5.pixelInputs.normal);
1785+
myp5.pixelInputs.end();
1786+
1787+
myp5.finalColor.begin();
1788+
// Use the processed normal to create a color - should be [0, 0, 1] for plane facing camera
1789+
myp5.finalColor.set([myp5.abs(processedNormal), 1]);
1790+
myp5.finalColor.end();
1791+
}, { myp5 });
1792+
1793+
myp5.background(255, 0, 0); // Red background to distinguish from result
1794+
myp5.noStroke();
1795+
myp5.shader(testShader);
1796+
myp5.plane(myp5.width, myp5.height);
1797+
1798+
// Normal of plane facing camera should be [0, 0, 1], so color should be [0, 0, 255]
1799+
const centerColor = myp5.get(25, 25);
1800+
assert.approximately(centerColor[0], 0, 5); // Red component
1801+
assert.approximately(centerColor[1], 0, 5); // Green component
1802+
assert.approximately(centerColor[2], 255, 5); // Blue component
1803+
});
1804+
17731805
test('handle passing a value from a vertex hook to a fragment hook using shared*', () => {
17741806
myp5.createCanvas(50, 50, myp5.WEBGL);
17751807
myp5.pixelDensity(1);

test/unit/webgpu/p5.Shader.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,38 @@ suite('WebGPU p5.Shader', function() {
978978
assert.approximately(centerColor[2], 255, 5); // Blue component
979979
});
980980

981+
test('handle passing a value between fragment hooks only while having a vertex hook', async () => {
982+
await myp5.createCanvas(50, 50, myp5.WEBGPU);
983+
myp5.pixelDensity(1);
984+
985+
const testShader = myp5.baseMaterialShader().modify(() => {
986+
let processedNormal = myp5.sharedVec3();
987+
myp5.objectInputs.begin();
988+
myp5.objectInputs.position += [0, 0, 0];
989+
myp5.objectInputs.end();
990+
991+
myp5.pixelInputs.begin();
992+
processedNormal = myp5.normalize(myp5.pixelInputs.normal);
993+
myp5.pixelInputs.end();
994+
995+
myp5.finalColor.begin();
996+
// Use the processed normal to create a color - should be [0, 0, 1] for plane facing camera
997+
myp5.finalColor.set([myp5.abs(processedNormal), 1]);
998+
myp5.finalColor.end();
999+
}, { myp5 });
1000+
1001+
myp5.background(255, 0, 0); // Red background to distinguish from result
1002+
myp5.noStroke();
1003+
myp5.shader(testShader);
1004+
myp5.plane(myp5.width, myp5.height);
1005+
1006+
// Normal of plane facing camera should be [0, 0, 1], so color should be [0, 0, 255]
1007+
const centerColor = await myp5.get(25, 25);
1008+
assert.approximately(centerColor[0], 0, 5); // Red component
1009+
assert.approximately(centerColor[1], 0, 5); // Green component
1010+
assert.approximately(centerColor[2], 255, 5); // Blue component
1011+
});
1012+
9811013
test('handle passing a value from a vertex hook to a fragment hook using shared*', async () => {
9821014
await myp5.createCanvas(50, 50, myp5.WEBGPU);
9831015
myp5.pixelDensity(1);

0 commit comments

Comments
 (0)