Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/strands/strands_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
//////////////////////////////////////////////
Expand Down
11 changes: 9 additions & 2 deletions src/strands/strands_transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions test/unit/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, can we add similar tests to the webgpu module too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, adding them now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in a29df88, same three cases using the material shader pattern, since filter shader tests are still todo on WebGPU.

});

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', () => {
Expand Down
60 changes: 60 additions & 0 deletions test/unit/webgpu/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down