-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathWebGPUPathTracer.js
More file actions
441 lines (296 loc) · 10.6 KB
/
WebGPUPathTracer.js
File metadata and controls
441 lines (296 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { DataTexture, LinearFilter, Vector2, Scene, PerspectiveCamera, Color, NoToneMapping, FloatType, Timer, StorageTexture } from 'three/webgpu';
import { SkinnedMeshBVH, MeshBVH, SAH } from 'three-mesh-bvh';
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
import { RenderToScreenNodeMaterial } from './materials/RenderToScreenMaterial.js';
import { MegaKernelPathTracer } from './MegaKernelPathTracer.js';
import { WaveFrontPathTracer } from './WaveFrontPathTracer.js';
import { CubeToEquirectGenerator } from '../utils/CubeToEquirectGenerator.js';
import { PathtracerBVHComputeData } from './nodes/PathtracerBVHComputeData.js';
import { RenderTarget2DArray } from './RenderTarget2DArray.js';
import { setCommonAttributes } from '../core/utils/GeometryPreparationUtils.js';
const _resolution = new Vector2();
const _color = new Color();
export class WebGPUPathTracer {
get bounces() {
return this._pathTracer.bounces;
}
set bounces( v ) {
this._pathTracer.bounces = v;
}
get samples() {
return this._pathTracer.samples;
}
get fadeState() {
return this._fadeState;
}
useMegakernel( value ) {
this._pathTracer.dispose();
this._pathTracer = value ? new MegaKernelPathTracer( this._renderer ) : new WaveFrontPathTracer( this._renderer );
this._pathTracer.setBVHData( this._bvhData );
this._pathTracer.setTextures( this.textureArray.texture );
this.setCamera( this.camera );
this.updateEnvironment();
}
constructor( renderer ) {
// members
this._renderer = renderer;
this._pathTracer = new MegaKernelPathTracer( renderer );
this._timer = new Timer();
this._envColorTexture = new DataTexture( );
this._envColorTexture.image.data = new Uint8Array( [ 255, 255, 255, 255 ] );
this._envColorTexture.needsUpdate = true;
this._envColorTexture.minFilter = LinearFilter;
this._envColorTexture.magFilter = LinearFilter;
this._backgroundColorTexture = new DataTexture( );
this._backgroundColorTexture.image.data = new Uint8Array( [ 255, 255, 255, 255 ] );
this._backgroundColorTexture.needsUpdate = true;
this._backgroundColorTexture.minFilter = LinearFilter;
this._backgroundColorTexture.magFilter = LinearFilter;
this._resetTime = - 1;
this._fadeState = 0;
this._size = new Vector2();
this._lowResTarget = new StorageTexture( 1, 1 );
this._lowResTarget.type = FloatType;
this._blitQuad = new FullScreenQuad( new RenderToScreenNodeMaterial() );
// options
this.minSamples = 1;
this.renderDelay = 500;
this.fadeDuration = 500;
this.dynamicLowRes = true;
this.lowResScale = 0.2;
this.renderScale = 1;
this.synchronizeRenderSize = true;
this.generateMissingAttributes = true;
this.commonAttributes = [ 'normal', 'uv', 'tangent', 'color' ];
this.textureArray = new RenderTarget2DArray( 1024, 1024 );
// initialize the scene so it doesn't fail
this.setScene( new Scene(), new PerspectiveCamera() );
}
setScene( scene, camera ) {
scene.updateMatrixWorld( true );
camera.updateMatrixWorld();
// Build BVH for each mesh geometry
scene.traverse( child => {
if ( this.generateMissingAttributes && child.geometry?.isBufferGeometry ) {
setCommonAttributes( child.geometry, this.commonAttributes );
}
if ( child.isSkinnedMesh ) {
if ( ! child.boundsTree ) {
child.boundsTree = new SkinnedMeshBVH( child, { strategy: SAH, maxLeafSize: 5, indirect: true } );
} else {
child.boundsTree.refit();
}
} else if ( child.isMesh ) {
if ( ! child.geometry.boundsTree ) {
child.geometry.boundsTree = new MeshBVH( child.geometry, { strategy: SAH, maxLeafSize: 5 } );
}
}
} );
// Build TLAS and compute functions
const bvhData = new PathtracerBVHComputeData( scene );
bvhData.update();
bvhData.useTransparencyRaycastFn();
this.textureArray.setTextures( this._renderer, bvhData.textures );
this._pathTracer.setTextures( this.textureArray.texture );
this.scene = scene;
this._bvhData = bvhData;
this._pathTracer.setBVHData( bvhData );
this.setCamera( camera );
this.updateEnvironment();
}
getMaterial() {
return this._pathTracer.getMaterial();
}
setMaterial( material ) {
this._pathTracer.setMaterial( material );
}
// TODO: support async generation of ObjectBVH
setSceneAsync( ...args ) {
this.setScene( ...args );
}
setCamera( camera ) {
this.camera = camera;
this.updateCamera();
}
updateCamera() {
const camera = this.camera;
camera.updateMatrixWorld();
this._pathTracer.setCamera( camera );
this.reset();
}
updateEnvironment() {
const {
_renderer,
_pathTracer,
scene,
_envColorTexture,
_backgroundColorTexture,
} = this;
const environment = convertToTexture( _renderer, scene.environment || _color.set( 0 ), _envColorTexture );
const background = convertToTexture( _renderer, scene.background, _backgroundColorTexture );
_pathTracer.setEnvironment(
environment,
scene.environmentIntensity,
scene.environmentRotation,
background,
scene.backgroundIntensity,
scene.backgroundRotation,
scene.backgroundBlurriness,
);
}
setSize( x, y ) {
if ( this._size.x !== x || this._size.y !== y ) {
this._size.set( x, y );
this.reset();
}
}
reset() {
this._pathTracer.reset();
this._resetTime = 0;
this._fadeState = 0;
}
renderSample() {
const renderer = this._renderer;
const size = this._size;
const blitQuad = this._blitQuad;
const pathTracer = this._pathTracer;
const lowResTarget = this._lowResTarget;
const timer = this._timer;
const {
renderDelay,
dynamicLowRes,
synchronizeRenderSize,
renderScale,
lowResScale,
minSamples,
} = this;
timer.update();
if ( ! this._renderer._initialized ) {
return;
}
const delta = 1000 * timer.getDelta();
this._resetTime += delta;
const originalToneMapping = renderer.toneMapping;
const originalExposure = renderer.toneMappingExposure;
const originalTarget = renderer.getRenderTarget();
const originalAutoClear = renderer.autoClear;
renderer.toneMapping = NoToneMapping;
renderer.toneMappingExposure = 1.0;
// handle canvas-size auto synchronization
if ( synchronizeRenderSize ) {
renderer.getDrawingBufferSize( _resolution );
const w = Math.floor( renderScale * _resolution.x );
const h = Math.floor( renderScale * _resolution.y );
this.setSize( w, h );
}
// check if we should be in low res mode and calculate the target size
let { width, height } = size;
const lowResMode = this._resetTime < renderDelay;
if ( lowResMode ) {
width = Math.ceil( lowResScale * width );
height = Math.ceil( lowResScale * height );
}
// set the size if necessary
pathTracer.getSize( _resolution );
const resized = _resolution.x !== width || _resolution.y !== height;
if ( resized ) {
if ( ! lowResMode && dynamicLowRes ) {
// copy the low reset content if we're transitioning to the full
// resolution view so we can fade to it
lowResTarget.setSize( Math.ceil( lowResScale * width ), Math.ceil( lowResScale * height ) );
renderer.copyTextureToTexture( pathTracer.outputTarget, lowResTarget );
}
pathTracer.setSize( width, height );
}
if ( ! lowResMode && pathTracer.samples >= minSamples ) {
this._fadeState += delta / this.fadeDuration;
this._fadeState = Math.min( 1.0, this._fadeState );
}
// update the samples
if ( ! lowResMode || ( lowResMode && dynamicLowRes ) ) {
pathTracer.lowResMode = lowResMode;
pathTracer.update();
}
// render the content to the canvas
const opacity = ( lowResMode && dynamicLowRes ? 1.0 : this._fadeState );
renderer.autoClear = dynamicLowRes ? true : opacity === 1.0;
blitQuad.material.transition = dynamicLowRes ? opacity : 1.0;
blitQuad.material.opacity = dynamicLowRes ? 1.0 : opacity;
blitQuad.material.fromTexture = lowResTarget;
blitQuad.material.texture = pathTracer.outputTarget;
blitQuad.material.toneMapping = originalToneMapping;
blitQuad.material.toneMappingExposure = originalExposure;
blitQuad.render( renderer );
// reset the renderer
renderer.autoClear = originalAutoClear;
renderer.toneMapping = originalToneMapping;
renderer.toneMappingExposure = originalExposure;
renderer.setRenderTarget( originalTarget );
}
dispose() {
this._pathTracer.dispose();
this._blitQuad.dispose();
this._lowResTarget.dispose();
this._envColorTexture.dispose();
this._backgroundColorTexture.dispose();
}
async getDetailedSampleCount() {
const sampleCountTarget = this._pathTracer.sampleCountTarget;
const { width, height } = sampleCountTarget;
const renderer = this._renderer;
// Create a stub "render target" with just textures field to read from the storage texture
const targetStub = { textures: [ sampleCountTarget ] };
const buffer = await renderer.readRenderTargetPixelsAsync( targetStub, 0, 0, width, height );
const uintBuffer = new Uint32Array( buffer.buffer );
// copyTexture requires a multiple of 256 bytes for texelsPerRow
// Hence a multiple of 64 u32 per row
const texelsPerRow = Math.ceil( width / 64 ) * 64;
// Sum up all sample counts and divide by pixel count to get average samples per pixel
let totalSamples = 0;
let minSamples = Number.MAX_VALUE;
let maxSamples = - Number.MAX_VALUE;
for ( let i = 0, l = uintBuffer.length; i < l; i ++ ) {
// Skip padding
if ( i % texelsPerRow >= width ) {
continue;
}
// Each entry contains sample count in lower bits and active flag in high bit
// Mask out the active flag (0xF0000000) to get just the sample count
const samples = uintBuffer[ i ] & 0x0FFFFFFF;
totalSamples += samples;
minSamples = Math.min( minSamples, samples );
maxSamples = Math.max( maxSamples, samples );
}
return {
min: minSamples,
max: maxSamples,
avg: Math.floor( totalSamples / ( width * height ) ),
};
}
// Returns time since last reset in ms
getRenderTime() {
return this._resetTime;
}
}
function convertToTexture( renderer, value, colorTexture ) {
if ( ! value ) {
const clearAlpha = renderer.getClearAlpha();
renderer.getClearColor( _color );
colorTexture.image.data[ 0 ] = _color.r * 255;
colorTexture.image.data[ 1 ] = _color.g * 255;
colorTexture.image.data[ 2 ] = _color.b * 255;
colorTexture.image.data[ 3 ] = clearAlpha * 255;
colorTexture.needsUpdate = true;
value = colorTexture;
} else if ( value.isColor ) {
colorTexture.image.data[ 0 ] = value.r * 255;
colorTexture.image.data[ 1 ] = value.g * 255;
colorTexture.image.data[ 2 ] = value.b * 255;
colorTexture.image.data[ 3 ] = 255;
colorTexture.needsUpdate = true;
value = colorTexture;
} else if ( value?.isCubeTexture ) {
value = new CubeToEquirectGenerator( renderer ).generate( value );
}
return value;
}