Skip to content

Commit 8c524e9

Browse files
authored
BatchedMesh: update example, fix depth conversion & auxiliary buffer (#27228)
* BatchedMesh: bit-cast depth f32 to u32 & fix auxiliary array Signed-off-by: Guilherme Avila <[email protected]> * webgl_mesh_batch: make custom sort self-contained Signed-off-by: Guilherme Avila <[email protected]> * webgl_mesh_batch: remove global auxiliary buffer Signed-off-by: Guilherme Avila <[email protected]> * clean-up / lint Signed-off-by: Guilherme Avila <[email protected]> --------- Signed-off-by: Guilherme Avila <[email protected]>
1 parent 525242a commit 8c524e9

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

examples/jsm/utils/SortUtils.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ const POWER = 3;
55
const BIT_MAX = 32;
66
const BIN_BITS = 1 << POWER;
77
const BIN_SIZE = 1 << BIN_BITS;
8+
const BIN_MAX = BIN_SIZE - 1;
89
const ITERATIONS = BIT_MAX / BIN_BITS;
910

1011
const bins = new Array( ITERATIONS );
11-
const caches = new Array( ITERATIONS );
12-
const bins_buffer = new ArrayBuffer( 2 * ITERATIONS * BIN_SIZE * 4 );
12+
const bins_buffer = new ArrayBuffer( ( ITERATIONS + 1 ) * BIN_SIZE * 4 );
1313

1414
let c = 0;
15-
for ( let i = 0; i < ITERATIONS; i ++ ) {
16-
15+
for ( let i = 0; i < ( ITERATIONS + 1 ); i ++ ) {
1716
bins[ i ] = new Uint32Array( bins_buffer, c, BIN_SIZE );
1817
c += BIN_SIZE * 4;
19-
caches[ i ] = new Uint32Array( bins_buffer, c, BIN_SIZE );
20-
c += BIN_SIZE * 4;
21-
2218
}
2319

2420
const defaultGet = ( el ) => el;
@@ -48,7 +44,7 @@ export const radixSort = ( arr, opt ) => {
4844
recurse = ( cache, depth, start ) => {
4945

5046
let prev = 0;
51-
for ( let j = BIN_SIZE - 1; j >= 0; j -- ) {
47+
for ( let j = BIN_MAX; j >= 0; j -- ) {
5248

5349
const cur = cache[ j ], diff = cur - prev;
5450
if ( diff != 0 ) {
@@ -136,20 +132,20 @@ export const radixSort = ( arr, opt ) => {
136132
const shift = ( 3 - depth ) << POWER;
137133
const end = start + len;
138134

139-
const bin = bins[ depth ];
140-
const cache = caches[ depth ];
135+
const cache = bins[ depth ];
136+
const bin = bins[ depth + 1 ];
141137

142138
bin.fill( 0 );
143139

144140
for ( let j = start; j < end; j ++ )
145-
bin[ ( get( a[ j ] ) >> shift ) & ( BIN_SIZE - 1 ) ] ++;
141+
bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ++;
146142

147143
accumulate( bin );
148144

149145
cache.set( bin );
150146

151147
for ( let j = end - 1; j >= start; j -- )
152-
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & ( BIN_SIZE - 1 ) ] ] = a[ j ];
148+
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ] = a[ j ];
153149

154150
if ( depth == ITERATIONS - 1 ) return;
155151

examples/webgl_mesh_batch.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,17 @@
287287
// initialize options
288288
this._options = this._options || {
289289
get: el => el.z,
290-
aux: new Array( list.length ),
290+
aux: new Array( this._maxGeometryCount )
291291
};
292292

293293
const options = this._options;
294294
options.reversed = this.material.transparent;
295295

296296
// convert depth to unsigned 32 bit range
297-
const den = camera.far;
297+
const factor = ( 2**32 - 1 ) / camera.far; // UINT32_MAX / max_depth
298298
for ( let i = 0, l = list.length; i < l; i ++ ) {
299299

300-
const el = list[ i ];
301-
el.z = ( 1 << 30 ) * ( el.z / den );
300+
list[i].z *= factor;
302301

303302
}
304303

0 commit comments

Comments
 (0)