Skip to content

Commit 2d5b225

Browse files
committed
Fix bug in CFG dependencies in p5.strands
1 parent 933a860 commit 2d5b225

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/strands/strands_for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ export class StrandsFor {
297297
const scopeEndBlock = CFG.createBasicBlock(cfg, BlockType.SCOPE_END);
298298
CFG.addEdge(cfg, updateBlock, scopeEndBlock);
299299

300-
// Loop back to break check
301-
CFG.addEdge(cfg, scopeEndBlock, breakCheckBlock);
300+
// Connect end of for loop to the merge agter the loop
301+
CFG.addEdge(cfg, scopeEndBlock, mergeBlock);
302302

303303
// Break condition exits to merge
304304
CFG.addEdge(cfg, breakCheckBlock, mergeBlock);

test/unit/webgl/p5.Shader.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,40 @@ suite('p5.Shader', function() {
11281128
const pixelColor = myp5.get(25, 25);
11291129
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
11301130
});
1131+
1132+
test('handle statements after for loop before return', () => {
1133+
myp5.createCanvas(50, 50, myp5.WEBGL);
1134+
1135+
const testShader = myp5.baseMaterialShader().modify(() => {
1136+
myp5.getPixelInputs(inputs => {
1137+
let avg = myp5.vec3(0.0);
1138+
let total = 0.0;
1139+
1140+
// Simulate blur-like accumulation in for loop
1141+
for (let i = 0; i < 3; i++) {
1142+
const sample = myp5.vec3(0.2, 0.1, 0.3);
1143+
const weight = 1.0;
1144+
avg += weight * sample;
1145+
total += weight;
1146+
}
1147+
1148+
const blended = avg / total;
1149+
1150+
inputs.color = [blended.x, blended.y, blended.z, 1.0];
1151+
return inputs;
1152+
});
1153+
}, { myp5 });
1154+
1155+
myp5.noStroke();
1156+
myp5.shader(testShader);
1157+
myp5.plane(myp5.width, myp5.height);
1158+
1159+
// Expected result: (3 * [0.2, 0.1, 0.3]) / 3 = [0.2, 0.1, 0.3]
1160+
const pixelColor = myp5.get(25, 25);
1161+
assert.approximately(pixelColor[0], 51, 5); // 0.2 * 255 ≈ 51
1162+
assert.approximately(pixelColor[1], 25, 5); // 0.1 * 255 ≈ 25
1163+
assert.approximately(pixelColor[2], 77, 5); // 0.3 * 255 ≈ 77
1164+
});
11311165
});
11321166

11331167
suite('passing data between shaders', () => {

0 commit comments

Comments
 (0)