Skip to content

Commit 7dea976

Browse files
committed
sketch out solve
1 parent a5413d7 commit 7dea976

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed

examples/jsm/tsl/display/SSGINode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RenderTarget, Vector2, TempNode, QuadMesh, NodeMaterial, RendererUtils, MathUtils } from 'three/webgpu';
2-
import { clamp, normalize, reference, nodeObject, Fn, NodeUpdateType, uniform, vec4, passTexture, uv, logarithmicDepthToViewZ, viewZToPerspectiveDepth, getViewPosition, screenCoordinate, float, sub, fract, dot, vec2, rand, vec3, Loop, mul, PI, cos, sin, uint, cross, acos, sign, pow, luminance, If, max, abs, Break, sqrt, HALF_PI, div, ceil, shiftRight, convertToTexture, bool, getNormalFromDepth } from 'three/tsl';
2+
import { clamp, normalize, reference, nodeObject, Fn, NodeUpdateType, uniform, vec4, passTexture, uv, logarithmicDepthToViewZ, viewZToPerspectiveDepth, getViewPosition, screenCoordinate, float, sub, fract, dot, vec2, rand, vec3, Loop, mul, PI, cos, sin, uint, cross, acos, sign, pow, luminance, If, max, abs, Break, sqrt, HALF_PI, div, ceil, shiftRight, convertToTexture, bool, getNormalFromDepth, bitCount } from 'three/tsl';
33

44
const _quadMesh = /*@__PURE__*/ new QuadMesh();
55
const _size = /*@__PURE__*/ new Vector2();
@@ -445,7 +445,7 @@ class SSGINode extends TempNode {
445445
]
446446
} );
447447

448-
const bitCount = Fn( ( [ value ] ) => {
448+
/*const bitCount = Fn( ( [ value ] ) => {
449449
450450
const v = uint( value );
451451
v.assign( v.sub( v.shiftRight( uint( 1 ) ).bitAnd( uint( 0x55555555 ) ) ) );
@@ -459,7 +459,7 @@ class SSGINode extends TempNode {
459459
inputs: [
460460
{ name: 'value', type: 'uint' }
461461
]
462-
} );
462+
} ); */
463463

464464
const horizonSampling = Fn( ( [ directionIsRight, RADIUS, viewPosition, slideDirTexelSize, initialRayStep, uvNode, viewDir, viewNormal, n ] ) => {
465465

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

Lines changed: 99 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 { CodeNode, GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents } from '../../../nodes/Nodes.js';
22

33
import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js';
44
import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js';
@@ -9,13 +9,77 @@ import { NoColorSpace, ByteType, ShortType, RGBAIntegerFormat, RGBIntegerFormat,
99
import { DataTexture } from '../../../textures/DataTexture.js';
1010
import { 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+
1270
const 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

2185
const 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

Comments
 (0)