Skip to content

Commit 6a84a80

Browse files
authored
TSL: Add bitCount functions (#31990)
1 parent 01206ac commit 6a84a80

File tree

6 files changed

+446
-25
lines changed

6 files changed

+446
-25
lines changed

examples/jsm/tsl/display/SSGINode.js

Lines changed: 3 additions & 19 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, interleavedGradientNoise } 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, countOneBits, interleavedGradientNoise } from 'three/tsl';
33

44
const _quadMesh = /*@__PURE__*/ new QuadMesh();
55
const _size = /*@__PURE__*/ new Vector2();
@@ -435,22 +435,6 @@ class SSGINode extends TempNode {
435435
]
436436
} );
437437

438-
const bitCount = Fn( ( [ value ] ) => {
439-
440-
const v = uint( value );
441-
v.assign( v.sub( v.shiftRight( uint( 1 ) ).bitAnd( uint( 0x55555555 ) ) ) );
442-
v.assign( v.bitAnd( uint( 0x33333333 ) ).add( v.shiftRight( uint( 2 ) ).bitAnd( uint( 0x33333333 ) ) ) );
443-
444-
return v.add( v.shiftRight( uint( 4 ) ) ).bitAnd( uint( 0xF0F0F0F ) ).mul( uint( 0x1010101 ) ).shiftRight( uint( 24 ) );
445-
446-
} ).setLayout( {
447-
name: 'bitCount',
448-
type: 'uint',
449-
inputs: [
450-
{ name: 'value', type: 'uint' }
451-
]
452-
} );
453-
454438
const horizonSampling = Fn( ( [ directionIsRight, RADIUS, viewPosition, slideDirTexelSize, initialRayStep, uvNode, viewDir, viewNormal, n ] ) => {
455439

456440
const STEP_COUNT = this.stepCount.toConst();
@@ -513,7 +497,7 @@ class SSGINode extends TempNode {
513497
currentOccludedBitfield = currentOccludedBitfield.bitAnd( globalOccludedBitfield.bitNot() );
514498

515499
globalOccludedBitfield.assign( globalOccludedBitfield.bitOr( currentOccludedBitfield ) );
516-
const numOccludedZones = bitCount( currentOccludedBitfield );
500+
const numOccludedZones = countOneBits( currentOccludedBitfield );
517501

518502
//
519503

@@ -597,7 +581,7 @@ class SSGINode extends TempNode {
597581
color.addAssign( horizonSampling( bool( true ), RADIUS, viewPosition, slideDirTexelSize, initialRayStep, uvNode, viewDir, viewNormal, n ) );
598582
color.addAssign( horizonSampling( bool( false ), RADIUS, viewPosition, slideDirTexelSize, initialRayStep, uvNode, viewDir, viewNormal, n ) );
599583

600-
ao.addAssign( float( bitCount( globalOccludedBitfield ) ).div( float( MAX_RAY ) ) );
584+
ao.addAssign( float( countOneBits( globalOccludedBitfield ) ).div( float( MAX_RAY ) ) );
601585

602586
} );
603587

examples/webgpu_compute_reduce.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ <h3 id="panel-title" style="flex: 0 0 auto;">Subgroup Reduction Explanation</h3>
190190
<script type="module">
191191

192192
import * as THREE from 'three/webgpu';
193-
import { instancedArray, Loop, If, vec3, dot, clamp, storage, uvec4, subgroupAdd, uniform, uv, uint, float, Fn, vec2, invocationLocalIndex, invocationSubgroupIndex, uvec2, floor, instanceIndex, workgroupId, workgroupBarrier, workgroupArray, subgroupSize, select, log2 } from 'three/tsl';
193+
import { instancedArray, Loop, If, vec3, dot, clamp, storage, uvec4, subgroupAdd, uniform, uv, uint, float, Fn, vec2, invocationLocalIndex, invocationSubgroupIndex, uvec2, floor, instanceIndex, workgroupId, workgroupBarrier, workgroupArray, subgroupSize, select, countTrailingZeros } from 'three/tsl';
194194

195195
import WebGPU from 'three/addons/capabilities/WebGPU.js';
196196

@@ -831,12 +831,12 @@ <h3 id="panel-title" style="flex: 0 0 auto;">Subgroup Reduction Explanation</h3>
831831

832832
// Multiple approaches here
833833
// log2(subgroupSize) -> TSL log2 function
834-
// countTrailingZeros/findLSB(subgroupSize) -> Currently unsupported function in TSL that counts trailing zeros in number bit representation
834+
// countTrailingZeros/findLSB(subgroupSize) -> TSL function that counts trailing zeros in number bit representation
835835
// Can technically petition GPU for subgroupSize in shader and calculate logs on CPU at cost of shader being generalizable across devices
836836
// May also break if subgroupSize changes when device is lost or if program is rerun on lower power device
837-
const subgroupSizeLog = uint( log2( float( subgroupSize ) ) ).toVar( 'subgroupSizeLog' );
837+
const subgroupSizeLog = countTrailingZeros( subgroupSize ).toVar( 'subgroupSizeLog' );
838838
const spineSize = uint( workgroupSize ).shiftRight( subgroupSizeLog );
839-
const spineSizeLog = uint( log2( float( spineSize ) ) ).toVar( 'spineSizeLog' );
839+
const spineSizeLog = countTrailingZeros( spineSize ).toVar( 'spineSizeLog' );
840840

841841

842842
// Align size to powers of subgroupSize

src/Three.TSL.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ export const context = TSL.context;
136136
export const convert = TSL.convert;
137137
export const convertColorSpace = TSL.convertColorSpace;
138138
export const convertToTexture = TSL.convertToTexture;
139+
export const countLeadingZeros = TSL.countLeadingZeros;
140+
export const countOneBits = TSL.countOneBits;
141+
export const countTrailingZeros = TSL.countTrailingZeros;
139142
export const cos = TSL.cos;
140143
export const cross = TSL.cross;
141144
export const cubeTexture = TSL.cubeTexture;

src/nodes/TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './core/MRTNode.js';
2020

2121
// math
2222
export * from './math/BitcastNode.js';
23+
export * from './math/BitcountNode.js';
2324
export * from './math/Hash.js';
2425
export * from './math/MathUtils.js';
2526
export * from './math/TriNoise3D.js';

0 commit comments

Comments
 (0)