From 5971e8cf11c8e45afe1a68f6a00cf4e49146d0d6 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Wed, 4 Nov 2026 23:50:40 +0530 Subject: [PATCH 1/3] Support ** in p5.strands and error on unsupported operators --- src/strands/strands_api.js | 11 +++++++ src/strands/strands_transpiler.js | 11 +++++-- test/unit/webgl/p5.Shader.js | 51 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/strands/strands_api.js b/src/strands/strands_api.js index 681ba4a080..6b26b54ebf 100644 --- a/src/strands/strands_api.js +++ b/src/strands/strands_api.js @@ -389,6 +389,17 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { }; } } + + // `a ** b` is transpiled to `a.pow(b)`. Build a pow() function call rather + // than a binary operator, since GLSL and WGSL have no ** operator. + StrandsNode.prototype.pow = function (exponent) { + const { id, dimension } = build.functionCallNode( + strandsContext, + 'pow', + [this, exponent] + ); + return createStrandsNode(id, dimension, strandsContext); + }; ////////////////////////////////////////////// // Unique Functions ////////////////////////////////////////////// diff --git a/src/strands/strands_transpiler.js b/src/strands/strands_transpiler.js index d4d7550368..d40d2768d0 100644 --- a/src/strands/strands_transpiler.js +++ b/src/strands/strands_transpiler.js @@ -54,8 +54,15 @@ function replaceBinaryOperator(codeSource) { return 'and'; case '||': return 'or'; - // TODO: handle ** --> pow, but make it stay pow in - // GLSL instead of turning it back into ** + case '**': + // Becomes a pow() function call (see StrandsNode.prototype.pow), + // since GLSL and WGSL have no ** operator. + return 'pow'; + default: + FES.userError( + 'unsupported operator', + `The ${codeSource} operator isn't supported in p5.strands shaders.` + ); } } function nodeIsUniform(ancestor) { diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index c5d5b8d3bf..61c925e972 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -5976,6 +5976,57 @@ suite('p5.Shader', function () { }).not.toThrow(); }); + suite('exponentiation operator (#9252)', () => { + test('** gives the same result as pow()', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseFilterShader().modify( + () => { + myp5.getColor(() => { + let v = 0.5; + return [v ** 2, 0, 0, 1]; + }); + }, + { myp5 } + ); + myp5.background(0); + myp5.filter(testShader); + const c = myp5.get(25, 25); + assert.approximately(c[0], 64, 5); // 0.5 ** 2 = 0.25 -> ~64 + }); + + test('**= works', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseFilterShader().modify( + () => { + myp5.getColor(() => { + let v = 0.5; + v **= 2; + return [v, 0, 0, 1]; + }); + }, + { myp5 } + ); + myp5.background(0); + myp5.filter(testShader); + const c = myp5.get(25, 25); + assert.approximately(c[0], 64, 5); + }); + + test('unsupported operators throw a clear error', () => { + myp5.createCanvas(5, 5, myp5.WEBGL); + expect(() => { + myp5.baseFilterShader().modify( + () => { + myp5.getColor(() => { + let v = 1; + return [v & 1, 0, 0, 1]; + }); + }, + { myp5 } + ); + }).toThrow(/unsupported operator/); + }); + }); suite('matrix uniforms (#8992)', () => { test('short aliases declare the same uniforms as the NxN names', () => { From a29df88f2d05cf6806e1d3ab52c2cf36e0e765e0 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Sat, 14 Nov 2026 23:42:47 +0530 Subject: [PATCH 2/3] Add WebGPU tests for ** and drop PR number from suite name --- test/unit/webgl/p5.Shader.js | 2 +- test/unit/webgpu/p5.Shader.js | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 61c925e972..2b9027299e 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -5976,7 +5976,7 @@ suite('p5.Shader', function () { }).not.toThrow(); }); - suite('exponentiation operator (#9252)', () => { + suite('exponentiation operator', () => { test('** gives the same result as pow()', () => { myp5.createCanvas(50, 50, myp5.WEBGL); const testShader = myp5.baseFilterShader().modify( diff --git a/test/unit/webgpu/p5.Shader.js b/test/unit/webgpu/p5.Shader.js index cd706149fa..380c10b6c8 100644 --- a/test/unit/webgpu/p5.Shader.js +++ b/test/unit/webgpu/p5.Shader.js @@ -2627,6 +2627,67 @@ suite('WebGPU p5.Shader', function () { }); }); + + suite('exponentiation operator', () => { + test('** gives the same result as pow()', async () => { + await myp5.createCanvas(50, 50, myp5.WEBGPU); + myp5.pixelDensity(1); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getFinalColor(() => { + let v = 0.5; + return [v ** 2, 0, 0, 1]; + }); + }, + { myp5 } + ); + myp5.background(0); + myp5.noStroke(); + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + await myp5.loadPixels(); + const i = (25 * myp5.width + 25) * 4; + assert.approximately(myp5.pixels[i], 64, 5); // 0.5 ** 2 = 0.25 -> ~64 + }); + + test('**= works', async () => { + await myp5.createCanvas(50, 50, myp5.WEBGPU); + myp5.pixelDensity(1); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getFinalColor(() => { + let v = 0.5; + v **= 2; + return [v, 0, 0, 1]; + }); + }, + { myp5 } + ); + myp5.background(0); + myp5.noStroke(); + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + await myp5.loadPixels(); + const i = (25 * myp5.width + 25) * 4; + assert.approximately(myp5.pixels[i], 64, 5); + }); + + test('unsupported operators throw a clear error', async () => { + await myp5.createCanvas(5, 5, myp5.WEBGPU); + expect(() => { + myp5.baseMaterialShader().modify( + () => { + myp5.getFinalColor(() => { + let v = 1; + return [v & 1, 0, 0, 1]; + }); + }, + { myp5 } + ); + }).toThrow(/unsupported operator/); + }); + }); + suite('matrix uniforms (#8992)', () => { test('short aliases declare the same uniforms as the NxN names', async () => { await myp5.createCanvas(5, 5, myp5.WEBGPU); From 2509c418a9bcb247ee7db0dc37593f9647d9f0c1 Mon Sep 17 00:00:00 2001 From: harshiltewari2004 Date: Tue, 17 Nov 2026 19:26:17 +0530 Subject: [PATCH 3/3] Fix indentation of exponentiation operator test suite --- test/unit/webgpu/p5.Shader.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/webgpu/p5.Shader.js b/test/unit/webgpu/p5.Shader.js index 380c10b6c8..a99166996b 100644 --- a/test/unit/webgpu/p5.Shader.js +++ b/test/unit/webgpu/p5.Shader.js @@ -2627,8 +2627,7 @@ suite('WebGPU p5.Shader', function () { }); }); - - suite('exponentiation operator', () => { + suite('exponentiation operator', () => { test('** gives the same result as pow()', async () => { await myp5.createCanvas(50, 50, myp5.WEBGPU); myp5.pixelDensity(1);