Skip to content

Commit 36c9890

Browse files
authored
TSL: Refine pow2/3/4 implementation and remove the polyfill for pow on Windows (#31720)
* Change pow2, pow3, pow4 implementations, remove polyfill * Address non-integer powers of negative bases * E2E: Fix screenshot
1 parent 5011da1 commit 36c9890

File tree

5 files changed

+5
-21
lines changed

5 files changed

+5
-21
lines changed
18 KB
Loading

examples/webgpu_tsl_galaxy.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
sin( angle )
6969
).mul( radius );
7070

71-
const randomOffset = range( vec3( - 1 ), vec3( 1 ) ).pow( 3 ).mul( radiusRatio ).add( 0.2 );
71+
const randomOffset = range( vec3( - 1 ), vec3( 1 ) ).pow3().mul( radiusRatio ).add( 0.2 );
7272

7373
material.positionNode = position.add( randomOffset );
7474

examples/webgpu_tsl_vfx_flames.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
// main UV
137137
const mainUv = uv().toVar();
138138
mainUv.assign( spherizeUV( mainUv, 10 ).mul( 0.6 ).add( 0.2 ) ); // spherize
139-
mainUv.assign( mainUv.pow( vec2( 1, 3 ) ) ); // stretch
139+
mainUv.assign( mainUv.abs().pow( vec2( 1, 3 ) ).mul( mainUv.sign() ) ); // stretch
140140
mainUv.assign( mainUv.mul( 2, 1 ).sub( vec2( 0.5, 0 ) ) ); // scale
141141

142142
// perlin noise

src/nodes/math/MathNode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ export const pow = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW ).setPa
900900
* @param {Node | number} x - The first parameter.
901901
* @returns {Node}
902902
*/
903-
export const pow2 = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW, 2 ).setParameterLength( 1 );
903+
export const pow2 = ( x ) => mul( x, x );
904904

905905
/**
906906
* Returns the cube of the parameter.
@@ -910,7 +910,7 @@ export const pow2 = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW, 2 ).s
910910
* @param {Node | number} x - The first parameter.
911911
* @returns {Node}
912912
*/
913-
export const pow3 = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW, 3 ).setParameterLength( 1 );
913+
export const pow3 = ( x ) => mul( x, x, x );
914914

915915
/**
916916
* Returns the fourth power of the parameter.
@@ -920,7 +920,7 @@ export const pow3 = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW, 3 ).s
920920
* @param {Node | number} x - The first parameter.
921921
* @returns {Node}
922922
*/
923-
export const pow4 = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.POW, 4 ).setParameterLength( 1 );
923+
export const pow4 = ( x ) => mul( x, x, x, x );
924924

925925
/**
926926
* Transforms the direction of a vector by a matrix and then normalizes the result.

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,6 @@ const wgslMethods = {
131131
bitcast: 'bitcast<f32>'
132132
};
133133

134-
// WebGPU issue: does not support pow() with negative base on Windows
135-
136-
if ( typeof navigator !== 'undefined' && /Windows/g.test( navigator.userAgent ) ) {
137-
138-
wgslPolyfill.pow_float = new CodeNode( 'fn tsl_pow_float( a : f32, b : f32 ) -> f32 { return select( -pow( -a, b ), pow( a, b ), a > 0.0 ); }' );
139-
wgslPolyfill.pow_vec2 = new CodeNode( 'fn tsl_pow_vec2( a : vec2f, b : vec2f ) -> vec2f { return vec2f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ) ); }', [ wgslPolyfill.pow_float ] );
140-
wgslPolyfill.pow_vec3 = new CodeNode( 'fn tsl_pow_vec3( a : vec3f, b : vec3f ) -> vec3f { return vec3f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ) ); }', [ wgslPolyfill.pow_float ] );
141-
wgslPolyfill.pow_vec4 = new CodeNode( 'fn tsl_pow_vec4( a : vec4f, b : vec4f ) -> vec4f { return vec4f( tsl_pow_float( a.x, b.x ), tsl_pow_float( a.y, b.y ), tsl_pow_float( a.z, b.z ), tsl_pow_float( a.w, b.w ) ); }', [ wgslPolyfill.pow_float ] );
142-
143-
wgslMethods.pow_float = 'tsl_pow_float';
144-
wgslMethods.pow_vec2 = 'tsl_pow_vec2';
145-
wgslMethods.pow_vec3 = 'tsl_pow_vec3';
146-
wgslMethods.pow_vec4 = 'tsl_pow_vec4';
147-
148-
}
149-
150134
//
151135

152136
let diagnostics = '';

0 commit comments

Comments
 (0)