Skip to content

Commit 3890354

Browse files
authored
GLSLNodeBuilder: Adjustments and Polyfill (#31999)
* add polyfill for GLSLNodeBuilder * unify how GLSL _include and WGSL _include work
1 parent 5be8714 commit 3890354

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents } from '../../../nodes/Nodes.js';
1+
import { GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents, CodeNode } from '../../../nodes/Nodes.js';
22

33
import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js';
44
import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js';
@@ -9,13 +9,20 @@ import { NoColorSpace, ByteType, ShortType, RGBAIntegerFormat, RGBIntegerFormat,
99
import { DataTexture } from '../../../textures/DataTexture.js';
1010
import { error } from '../../../utils.js';
1111

12+
const glslPolyfills = {
13+
bitcast_int_uint: new CodeNode( /* glsl */'uint tsl_bitcast_uint_to_int ( int x ) { return floatBitsToInt( uintBitsToFloat( x ) ); }' ),
14+
bitcast_uint_int: new CodeNode( /* glsl */'uint tsl_bitcast_int_to_uint ( int x ) { return floatBitsToUint( intBitsToFloat ( x ) ); }' )
15+
};
16+
1217
const glslMethods = {
1318
textureDimensions: 'textureSize',
1419
equals: 'equal',
1520
bitcast_float_int: 'floatBitsToInt',
1621
bitcast_int_float: 'intBitsToFloat',
1722
bitcast_uint_float: 'uintBitsToFloat',
1823
bitcast_float_uint: 'floatBitsToUint',
24+
bitcast_uint_int: 'tsl_bitcast_uint_to_int',
25+
bitcast_int_uint: 'tsl_bitcast_int_to_uint'
1926
};
2027

2128
const precisionLib = {
@@ -127,6 +134,25 @@ class GLSLNodeBuilder extends NodeBuilder {
127134

128135
}
129136

137+
/**
138+
* Includes the given method name into the current
139+
* function node.
140+
*
141+
* @private
142+
* @param {string} name - The method name to include.
143+
* @return {CodeNode} The respective code node.
144+
*/
145+
_include( name ) {
146+
147+
const codeNode = glslPolyfills[ name ];
148+
codeNode.build( this );
149+
150+
this.addInclude( codeNode );
151+
152+
return codeNode;
153+
154+
}
155+
130156
/**
131157
* Returns the native shader method name for a given generic name.
132158
*
@@ -135,6 +161,12 @@ class GLSLNodeBuilder extends NodeBuilder {
135161
*/
136162
getMethod( method ) {
137163

164+
if ( glslPolyfills[ method ] !== undefined ) {
165+
166+
this._include( method );
167+
168+
}
169+
138170
return glslMethods[ method ] || method;
139171

140172
}
@@ -148,7 +180,7 @@ class GLSLNodeBuilder extends NodeBuilder {
148180
*/
149181
getBitcastMethod( type, inputType ) {
150182

151-
return glslMethods[ `bitcast_${ inputType }_${ type }` ];
183+
return this.getMethod( `bitcast_${ inputType }_${ type }` );
152184

153185
}
154186

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,11 +2068,7 @@ ${ flowData.code }
20682068
const codeNode = wgslPolyfill[ name ];
20692069
codeNode.build( this );
20702070

2071-
if ( this.currentFunctionNode !== null ) {
2072-
2073-
this.currentFunctionNode.includes.push( codeNode );
2074-
2075-
}
2071+
this.addInclude( codeNode );
20762072

20772073
return codeNode;
20782074

0 commit comments

Comments
 (0)