1- import { GLSLNodeParser , NodeBuilder , TextureNode , vectorComponents } from '../../../nodes/Nodes.js' ;
1+ import { CodeNode , GLSLNodeParser , NodeBuilder , TextureNode , vectorComponents } from '../../../nodes/Nodes.js' ;
22
33import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js' ;
44import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js' ;
@@ -9,13 +9,77 @@ import { NoColorSpace, ByteType, ShortType, RGBAIntegerFormat, RGBIntegerFormat,
99import { DataTexture } from '../../../textures/DataTexture.js' ;
1010import { error } from '../../../utils.js' ;
1111
12+ const uintBitCount = new CodeNode ( /* glsl */ `
13+ uint tsl_bit_count ( uint value ) {
14+
15+ uint newValue = value;
16+ newValue = ( newValue - ( ( newValue >> 1u ) & 1431655765u ) );
17+ newValue = ( ( newValue & 858993459u ) + ( ( newValue >> 2u ) & 858993459u ) );
18+
19+ return ( ( ( ( newValue + ( newValue >> 4u ) ) & 252645135u ) * 16843009u ) >> 24u );
20+
21+ }
22+ ` ) ;
23+
24+ const intBitCount = new CodeNode ( /* glsl */ `
25+ uint tsl_bit_count_int ( int value ) {
26+
27+ uint v = floatBitsToUint( intBitsToFloat( value ) );
28+ v = ( v - ( ( v >> 1u ) & 1431655765u ) );
29+ v = ( ( v & 858993459u ) + ( ( v >> 2u ) & 858993459u ) );
30+
31+ return ( ( ( ( v + ( v >> 4u ) ) & 252645135u ) * 16843009u ) >> 24u );
32+
33+ }
34+ ` ) ;
35+
36+ const uvec2BitCount = new CodeNode ( /* glsl */ 'uint tsl_bit_count_vec2 (uvec2 value) { return tsl_bit_count(value.x) + tsl_bit_count(value.y) } )' ) ;
37+ const uvec3BitCount = new CodeNode ( /* glsl */ 'uint tsl_bit_count_vec3 (uvec3 value) { return tsl_bit_count(value.x) + tsl_bit_count(value.y) + tsl_bit_count(value.z) } )' ) ;
38+ const uvec4BitCount = new CodeNode ( /* glsl */ 'uint tsl_bit_count_vec3 (uvec4 value) { return tsl_bit_count(value.x) + tsl_bit_count(value.y) + tsl_bit_count(value.z) + tsl_bit_count(value.w) }' ) ;
39+
40+ const uintFindLSB = '' ;
41+ const uintFindMSB = '' ;
42+
43+
44+ /* Remove polyfills and method mappings for findLSB, findMSB, and bitCount
45+ when OpenGL ES 3.1 bit counting functionality is implemented in WebGL.
46+ Tracking for issue found here https://github.com/KhronosGroup/WebGL/issues/3714 */
47+ const glslPolyfills = {
48+ tsl_find_lsb : {
49+ entryIndex : 0 ,
50+ codeNodes : [ uintFindLSB ] ,
51+ } ,
52+ tsl_find_msb : {
53+ entryIndex : 0 ,
54+ codeNodes : [ uintFindMSB ] ,
55+ } ,
56+ tsl_bit_count : {
57+ codeNodes : [ uintBitCount ] ,
58+ } ,
59+ tsl_bit_count_uvec2 : {
60+ codeNodes : [ uintBitCount , uvec2BitCount ]
61+ } ,
62+ tsl_bit_count_uvec3 : {
63+ codeNodes : [ uintBitCount , uvec3BitCount ]
64+ } ,
65+ tsl_bit_count_uvec4 : {
66+ codeNodes : [ uintBitCount , uvec4BitCount ]
67+ }
68+ } ;
69+
1270const glslMethods = {
1371 textureDimensions : 'textureSize' ,
1472 equals : 'equal' ,
1573 bitcast_float_int : 'floatBitsToInt' ,
1674 bitcast_int_float : 'intBitsToFloat' ,
1775 bitcast_uint_float : 'uintBitsToFloat' ,
1876 bitcast_float_uint : 'floatBitsToUint' ,
77+ findLSB : 'tsl_find_lsb' ,
78+ findMSB : 'tsl_find_msb' ,
79+ bitCount : 'tsl_bit_count' ,
80+ countTrailingZeros : 'tsl_find_lsb' ,
81+ countLeadingZeros : 'tsl_find_msb' ,
82+ countOneBits : 'tsl_bit_count'
1983} ;
2084
2185const precisionLib = {
@@ -127,6 +191,23 @@ class GLSLNodeBuilder extends NodeBuilder {
127191
128192 }
129193
194+ _include ( name ) {
195+
196+ // Collect the array of functions needed to polyfill the specified functionality
197+ const polyfill = glslPolyfills [ name ] ;
198+
199+ for ( const codeNode of polyfill . codeNodes ) {
200+
201+ // Build and include each relevant function
202+ codeNode . build ( this ) ;
203+ this . addInclude ( codeNode ) ;
204+
205+ }
206+
207+ return polyfill . entryIndex ? polyfill . codeNodes [ polyfill . entryIndex ] : polyfill . codeNodes [ 0 ] ;
208+
209+ }
210+
130211 /**
131212 * Returns the native shader method name for a given generic name.
132213 *
@@ -135,7 +216,17 @@ class GLSLNodeBuilder extends NodeBuilder {
135216 */
136217 getMethod ( method ) {
137218
138- return glslMethods [ method ] || method ;
219+ const glslMethod = glslMethods [ method ] || method ;
220+
221+ if ( glslPolyfills [ glslMethod ] !== undefined ) {
222+
223+ console . log ( 'calling glsl polyfill for ' , method ) ;
224+
225+ this . _include ( glslMethod ) ;
226+
227+ }
228+
229+ return glslMethod ;
139230
140231 }
141232
@@ -1288,6 +1379,8 @@ ${vars}
12881379 */
12891380 _getGLSLVertexCode ( shaderData ) {
12901381
1382+ console . log ( shaderData ) ;
1383+
12911384 return `#version 300 es
12921385
12931386${ this . getSignature ( ) }
@@ -1337,6 +1430,8 @@ void main() {
13371430 */
13381431 _getGLSLFragmentCode ( shaderData ) {
13391432
1433+ console . log ( shaderData ) ;
1434+
13401435 return `#version 300 es
13411436
13421437${ this . getSignature ( ) }
@@ -1446,6 +1541,8 @@ void main() {
14461541
14471542 this . vertexShader = this . _getGLSLVertexCode ( shadersData . vertex ) ;
14481543 this . fragmentShader = this . _getGLSLFragmentCode ( shadersData . fragment ) ;
1544+ console . log ( this . vertexShader ) ;
1545+ console . log ( this . fragmentShader ) ;
14491546
14501547 } else {
14511548
0 commit comments