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 89adeceb49..a50a7c4785 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -6141,6 +6141,57 @@ suite('p5.Shader', function () { }).not.toThrow(); }); + suite('exponentiation operator', () => { + 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', () => { diff --git a/test/unit/webgpu/p5.Shader.js b/test/unit/webgpu/p5.Shader.js index 041ae652dc..7d00898108 100644 --- a/test/unit/webgpu/p5.Shader.js +++ b/test/unit/webgpu/p5.Shader.js @@ -2801,6 +2801,66 @@ 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);