three-gpu-pathtracer 0.0.26 → 0.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/build/index.module.js +23 -5
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +22 -4
- package/build/index.umd.cjs.map +1 -1
- package/package.json +8 -8
- package/src/core/PathTracingSceneGenerator.js +1 -1
- package/src/core/utils/sceneUpdateUtils.js +2 -1
- package/src/uniforms/EquirectHdrInfoUniform.js +4 -2
- package/src/uniforms/LightsInfoUniformStruct.js +16 -1
- package/src/webgpu/API.md +167 -19
- package/src/webgpu/AtlasTexture.js +32 -12
- package/src/webgpu/EquirectHdrInfoNode.js +97 -46
- package/src/webgpu/LightsInfoNode.js +142 -4
- package/src/webgpu/MegaKernelPathTracer.js +11 -3
- package/src/webgpu/PathTracerBackend.js +174 -21
- package/src/webgpu/TurquinTexture.js +2 -0
- package/src/webgpu/WaveFrontPathTracer.js +169 -71
- package/src/webgpu/WebGPUPathTracer.js +637 -51
- package/src/webgpu/compute/ComputeKernel.js +18 -0
- package/src/webgpu/compute/PathTracerMegaKernel.js +253 -71
- package/src/webgpu/compute/TallySampleCountsKernel.js +10 -10
- package/src/webgpu/compute/UnpackAuxKernel.js +48 -0
- package/src/webgpu/compute/ZeroOutKernel.js +3 -1
- package/src/webgpu/compute/targets.js +55 -0
- package/src/webgpu/compute/wavefront/LogicKernel.js +136 -55
- package/src/webgpu/compute/wavefront/MaterialKernel.js +153 -66
- package/src/webgpu/compute/wavefront/MediumKernel.js +154 -0
- package/src/webgpu/compute/wavefront/PopulatePixelIndicesKernel.js +5 -5
- package/src/webgpu/compute/wavefront/QueueLengthToDispatchKernel.js +5 -5
- package/src/webgpu/compute/wavefront/QueueMediumWalksKernel.js +52 -0
- package/src/webgpu/compute/wavefront/ResetSlotsKernel.js +6 -6
- package/src/webgpu/compute/wavefront/TraceRayKernel.js +11 -9
- package/src/webgpu/compute/wavefront/TraceShadowRayKernel.js +18 -23
- package/src/webgpu/compute/wavefront/structs.js +63 -35
- package/src/webgpu/constants.js +2 -2
- package/src/webgpu/denoise/OIDNDenoiser.js +81 -78
- package/src/webgpu/index.d.ts +79 -6
- package/src/webgpu/materials/GltfCompliantMaterial.js +20 -6
- package/src/webgpu/materials/debug/AuxDebugMaterial.js +54 -0
- package/src/webgpu/nodes/PathtracerBVHComputeData.js +109 -17
- package/src/webgpu/nodes/aux.wgsl.js +61 -0
- package/src/webgpu/nodes/eon.wgsl.js +1 -1
- package/src/webgpu/nodes/lights.wgsl.js +21 -1
- package/src/webgpu/nodes/material.wgsl.js +2 -1
- package/src/webgpu/nodes/medium.wgsl.js +165 -0
- package/src/webgpu/nodes/random.wgsl.js +2 -0
- package/src/webgpu/nodes/sampling.wgsl.js +44 -3
- package/src/webgpu/nodes/structs.wgsl.js +8 -1
- package/src/webgpu/upscale/FSRUpscaler.js +3 -2
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { Vector2 } from 'three';
|
|
2
|
-
import { StorageBufferAttribute
|
|
2
|
+
import { StorageBufferAttribute } from 'three/webgpu';
|
|
3
3
|
import { ComputeKernel } from '../ComputeKernel.js';
|
|
4
|
+
import { createSampleCountTarget } from '../targets.js';
|
|
4
5
|
import { uniform, storage, textureStore, globalId } from 'three/tsl';
|
|
5
|
-
import {
|
|
6
|
+
import { proxyFn, contextProxy, contextProxyFn, rayStruct, wgslTagFn } from 'three-mesh-bvh/webgpu';
|
|
6
7
|
import { rngInit, rand1, rand2, RNG_INDEX_RAY_JITTER, RNG_INDEX_ALPHA_TEST, RNG_INDEX_RUSSIAN_ROULETTE, RNG_INDEX_DISPERSION_WAVELENGTH } from '../../nodes/random.wgsl.js';
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
rayDataStruct, traceQueuedRayStruct,
|
|
10
|
+
RAY_QUEUE_LENGTH, SHADOW_RAY_QUEUE_LENGTH, PIXEL_QUEUE_CURRENT, PIXEL_QUEUE_ELEMENT_COUNT, PIXEL_QUEUE_HEADER,
|
|
11
|
+
} from './structs.js';
|
|
12
|
+
import { enterMediumFn, loadMediumFn, storeMediumFn } from '../../nodes/medium.wgsl.js';
|
|
8
13
|
import { SAMPLE_ACTIVE_FLAG, SAMPLE_COUNT_MASK, SAMPLE_DISPATCHED_FLAG } from '../../constants.js';
|
|
9
14
|
import { applyDispersionFunc, dispersionColorWeightFunc, DISPERSION_MIN_WAVELENGTH, DISPERSION_MAX_WAVELENGTH, transmissionAttenuationFunc } from '../../nodes/material.wgsl.js';
|
|
10
15
|
import { isTerminatingScatterFunc, offsetRayOriginFunc } from '../../nodes/utils.wgsl.js';
|
|
11
16
|
import { LIGHT_EPSILON } from '../../nodes/lights.wgsl.js';
|
|
17
|
+
import { isAuxSurfaceFn } from '../../nodes/aux.wgsl.js';
|
|
12
18
|
|
|
13
19
|
// Pure material evaluation and ray generation: terminated slots pull a recycled pixel and emit a
|
|
14
20
|
// fresh camera ray; live slots evaluate the surface staged by LogicKernel, sample the bsdf, and
|
|
@@ -18,7 +24,6 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
18
24
|
constructor( ) {
|
|
19
25
|
|
|
20
26
|
const params = {
|
|
21
|
-
bvhData: { value: null },
|
|
22
27
|
material: { value: null },
|
|
23
28
|
|
|
24
29
|
seed: uniform( 0, 'uint' ),
|
|
@@ -29,21 +34,24 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
29
34
|
maxTransparentBounces: uniform( 5, 'uint' ),
|
|
30
35
|
maxBounces: uniform( 5, 'uint' ),
|
|
31
36
|
|
|
32
|
-
sampleCountTarget: textureStore(
|
|
37
|
+
sampleCountTarget: textureStore( createSampleCountTarget() ).toReadWrite(),
|
|
33
38
|
|
|
34
39
|
rayDataStorage: storage( new StorageBufferAttribute( 1, 1 ), rayDataStruct ),
|
|
35
|
-
rayQueue: storage( new StorageBufferAttribute( 1, 1 ),
|
|
36
|
-
shadowRayQueue: storage( new StorageBufferAttribute( 1, 1 ),
|
|
37
|
-
|
|
40
|
+
rayQueue: storage( new StorageBufferAttribute( 1, 1 ), traceQueuedRayStruct ),
|
|
41
|
+
shadowRayQueue: storage( new StorageBufferAttribute( 1, 1 ), traceQueuedRayStruct ),
|
|
42
|
+
queueCounters: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ).toAtomic(),
|
|
43
|
+
pixelQueue: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ).toAtomic(),
|
|
38
44
|
|
|
39
45
|
globalId: globalId,
|
|
40
46
|
};
|
|
41
47
|
|
|
42
|
-
const getCameraRayFn =
|
|
43
|
-
const sampleTrianglePointFn =
|
|
44
|
-
const getSurfaceRecordFn =
|
|
48
|
+
const getCameraRayFn = contextProxyFn( 'bvhData.fns.getCameraRay' );
|
|
49
|
+
const sampleTrianglePointFn = contextProxyFn( 'bvhData.fns.sampleTrianglePoint' );
|
|
50
|
+
const getSurfaceRecordFn = contextProxyFn( 'bvhData.fns.getSurfaceRecord' );
|
|
45
51
|
const bsdfSampleFn = proxyFn( 'material.value.bsdfSample', params );
|
|
46
52
|
const bsdfEvalPdfFn = proxyFn( 'material.value.bsdfEvalPdf', params );
|
|
53
|
+
const materialsStorage = contextProxy( 'bvhData.storage.materials' );
|
|
54
|
+
const transformsStorage = contextProxy( 'bvhData.storage.transforms' );
|
|
47
55
|
|
|
48
56
|
const fn = wgslTagFn/* wgsl */`
|
|
49
57
|
|
|
@@ -62,11 +70,9 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
62
70
|
let rayDataStorage = &${ params.rayDataStorage };
|
|
63
71
|
let rayQueue = &${ params.rayQueue };
|
|
64
72
|
let shadowRayQueue = &${ params.shadowRayQueue };
|
|
73
|
+
let queueCounters = &${ params.queueCounters };
|
|
65
74
|
let pixelQueue = &${ params.pixelQueue };
|
|
66
75
|
|
|
67
|
-
let materials = &${ proxy( 'bvhData.value.storage.materials', params ) };
|
|
68
|
-
let transforms = &${ proxy( 'bvhData.value.storage.transforms', params ) };
|
|
69
|
-
|
|
70
76
|
// bound by "rayCount" rather than the pool length. The dispatch rounds up to the
|
|
71
77
|
// workgroup size and those extra slots hold a zeroed pixel index
|
|
72
78
|
let index = globalId.x;
|
|
@@ -76,20 +82,21 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
76
82
|
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
|
|
85
|
+
// whole structs are read from the buffer, not the pointer alias, which Safari fails to compile
|
|
86
|
+
let input = ${ params.rayDataStorage }[ index ];
|
|
80
87
|
if ( input.objectIndex < 0 ) {
|
|
81
88
|
|
|
82
89
|
// the slot's path has terminated: recycle the pixel through the overflow queue and
|
|
83
90
|
// generate a fresh camera ray
|
|
84
91
|
var pixelIndex = input.pixelIndex;
|
|
85
|
-
let elementCount = atomicLoad( &pixelQueue
|
|
92
|
+
let elementCount = atomicLoad( &pixelQueue[ ${ PIXEL_QUEUE_ELEMENT_COUNT }u ] );
|
|
86
93
|
if ( elementCount > 0u ) {
|
|
87
94
|
|
|
88
95
|
// TODO: If we've pulled off a pixel that's already finished we currently just
|
|
89
96
|
// write a no-op ray, wasting a frame. It may be better to iterate over a few
|
|
90
97
|
// points in the queue to see if we can find one we can use.
|
|
91
|
-
let queueIndex = atomicAdd( &pixelQueue
|
|
92
|
-
pixelIndex = atomicExchange( &pixelQueue
|
|
98
|
+
let queueIndex = atomicAdd( &pixelQueue[ ${ PIXEL_QUEUE_CURRENT }u ], 1u ) % elementCount;
|
|
99
|
+
pixelIndex = atomicExchange( &pixelQueue[ ${ PIXEL_QUEUE_HEADER }u + queueIndex ], pixelIndex );
|
|
93
100
|
|
|
94
101
|
}
|
|
95
102
|
|
|
@@ -116,9 +123,16 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
116
123
|
var ray: ${ rayStruct };
|
|
117
124
|
if ( ! ${ getCameraRayFn }( jitteredUv, vec2f( targetDimensions ), &ray ) ) {
|
|
118
125
|
|
|
119
|
-
// the camera declined the pixel, so leave the slot dormant for this round
|
|
126
|
+
// the camera declined the pixel, so leave the slot dormant for this round. With a
|
|
127
|
+
// sample cap the pixel counts as finished, since it will never take a sample
|
|
120
128
|
// TODO: same as above - this work is a bit wasteful and leaves slots empty for
|
|
121
129
|
// a frame. Is it possible to quickly skip rays outside the mask or are finished?
|
|
130
|
+
if ( maxSamples != 0u ) {
|
|
131
|
+
|
|
132
|
+
textureStore( ${ params.sampleCountTarget }, indexUV, vec4( ( combinedField & ~${ SAMPLE_COUNT_MASK }u ) | maxSamples ) );
|
|
133
|
+
|
|
134
|
+
}
|
|
135
|
+
|
|
122
136
|
rayDataStorage[ index ].pixelIndex = pixelIndex;
|
|
123
137
|
rayDataStorage[ index ].rayIntersectionIndex = - 1;
|
|
124
138
|
rayDataStorage[ index ].shadowRayIntersectionIndex = - 1;
|
|
@@ -128,14 +142,13 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
128
142
|
|
|
129
143
|
ray.direction = normalize( ray.direction );
|
|
130
144
|
|
|
131
|
-
let rayIndex = atomicAdd( &
|
|
132
|
-
rayQueue
|
|
133
|
-
rayQueue
|
|
134
|
-
rayQueue
|
|
135
|
-
rayQueue
|
|
136
|
-
rayQueue
|
|
137
|
-
rayQueue
|
|
138
|
-
rayQueue.elements[ rayIndex ].maxDist = ray.maxDist;
|
|
145
|
+
let rayIndex = atomicAdd( &queueCounters[ ${ RAY_QUEUE_LENGTH }u ], 1u );
|
|
146
|
+
rayQueue[ rayIndex ].origin = ray.origin;
|
|
147
|
+
rayQueue[ rayIndex ].direction = ray.direction;
|
|
148
|
+
rayQueue[ rayIndex ].pixelIndex = pixelIndex;
|
|
149
|
+
rayQueue[ rayIndex ].rngIndex = 0u;
|
|
150
|
+
rayQueue[ rayIndex ].seed = seed + samples;
|
|
151
|
+
rayQueue[ rayIndex ].maxDist = ray.maxDist;
|
|
139
152
|
|
|
140
153
|
rayDataStorage[ index ].origin = ray.origin;
|
|
141
154
|
rayDataStorage[ index ].direction = ray.direction;
|
|
@@ -151,22 +164,33 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
151
164
|
rayDataStorage[ index ].emission = vec3f( 0.0 );
|
|
152
165
|
rayDataStorage[ index ].lightPdf = 0.0;
|
|
153
166
|
rayDataStorage[ index ].alphaDepth = 0u;
|
|
167
|
+
rayDataStorage[ index ].rayStart = 0.0;
|
|
154
168
|
rayDataStorage[ index ].maxDist = ray.maxDist;
|
|
155
169
|
rayDataStorage[ index ].rayIntersectionIndex = i32( rayIndex );
|
|
156
170
|
rayDataStorage[ index ].shadowRayIntersectionIndex = - 1;
|
|
157
171
|
rayDataStorage[ index ].dispersionWavelength = - mix( ${ DISPERSION_MIN_WAVELENGTH }.0, ${ DISPERSION_MAX_WAVELENGTH }.0, ${ rand1 }( ${ RNG_INDEX_DISPERSION_WAVELENGTH } ) );
|
|
172
|
+
rayDataStorage[ index ].auxAlbedo = 0u;
|
|
173
|
+
rayDataStorage[ index ].auxNormal = 0u;
|
|
174
|
+
rayDataStorage[ index ].subsurfaceBounces = 0u;
|
|
175
|
+
rayDataStorage[ index ].walkPending = 0u;
|
|
176
|
+
rayDataStorage[ index ].mediumScattered = 0u;
|
|
177
|
+
rayDataStorage[ index ].medium.inside = 0u;
|
|
158
178
|
|
|
159
179
|
// write the active params & dispatched flag
|
|
160
180
|
textureStore( ${ params.sampleCountTarget }, indexUV, vec4( ${ SAMPLE_ACTIVE_FLAG }u | ${ SAMPLE_DISPATCHED_FLAG }u | samples ) );
|
|
161
181
|
|
|
162
|
-
} else {
|
|
182
|
+
} else if ( input.walkPending == 0u ) {
|
|
163
183
|
|
|
164
|
-
// evaluate the surface staged by LogicKernel
|
|
184
|
+
// evaluate the surface staged by LogicKernel. Paths still walking a medium have none
|
|
165
185
|
let indexUV = vec2u( input.pixelIndex >> 16, input.pixelIndex & 0xFFFF );
|
|
166
|
-
${ rngInit }( indexUV, input.seed, input.currentBounce + input.alphaDepth );
|
|
186
|
+
${ rngInit }( indexUV, input.seed, input.currentBounce + input.alphaDepth + input.subsurfaceBounces );
|
|
167
187
|
|
|
168
|
-
|
|
169
|
-
var
|
|
188
|
+
// the medium the path is in, which the bsdf sample may enter or exit
|
|
189
|
+
var medium = ${ loadMediumFn }( input.medium );
|
|
190
|
+
let segmentInMedium = medium.inside;
|
|
191
|
+
|
|
192
|
+
let objectInfo = ${ transformsStorage }[ u32( input.objectIndex ) ];
|
|
193
|
+
var materialInfo = ${ materialsStorage }[ objectInfo.materialIndex ];
|
|
170
194
|
|
|
171
195
|
// a matte surface hit by the camera ray renders as a fully transparent
|
|
172
196
|
let isMatte = materialInfo.matte != 0 && input.currentBounce == 0u;
|
|
@@ -215,16 +239,31 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
215
239
|
|
|
216
240
|
}
|
|
217
241
|
|
|
218
|
-
let
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
242
|
+
let alphaOrigin = ${ offsetRayOriginFunc }( vertexData.position.xyz, input.direction, input.normal );
|
|
243
|
+
if ( segmentInMedium ) {
|
|
244
|
+
|
|
245
|
+
// inside a medium, MediumKernel walks on past the surface, from the surface
|
|
246
|
+
rayDataStorage[ index ].walkPending = 1u;
|
|
247
|
+
rayDataStorage[ index ].origin = alphaOrigin;
|
|
248
|
+
rayDataStorage[ index ].rayStart = 0.0;
|
|
249
|
+
|
|
250
|
+
} else {
|
|
251
|
+
|
|
252
|
+
let alphaIndex = atomicAdd( &queueCounters[ ${ RAY_QUEUE_LENGTH }u ], 1u );
|
|
253
|
+
rayQueue[ alphaIndex ].origin = alphaOrigin;
|
|
254
|
+
rayQueue[ alphaIndex ].direction = input.direction;
|
|
255
|
+
rayQueue[ alphaIndex ].pixelIndex = input.pixelIndex;
|
|
256
|
+
rayQueue[ alphaIndex ].rngIndex = input.currentBounce + input.alphaDepth + 1u + input.subsurfaceBounces;
|
|
257
|
+
rayQueue[ alphaIndex ].seed = input.seed;
|
|
258
|
+
// the origin advanced to the hit point, so the remaining budget shrinks by the
|
|
259
|
+
// distance already traced
|
|
260
|
+
rayQueue[ alphaIndex ].maxDist = max( input.maxDist - input.dist, 0.0 );
|
|
261
|
+
rayDataStorage[ index ].rayIntersectionIndex = i32( alphaIndex );
|
|
262
|
+
|
|
263
|
+
// the ray keeps its origin, and only the queued traversal starts past the surface
|
|
264
|
+
rayDataStorage[ index ].rayStart = input.rayStart + input.dist;
|
|
265
|
+
|
|
266
|
+
}
|
|
228
267
|
|
|
229
268
|
// the surface is skipped, so no scatter or emission is staged for LogicKernel.
|
|
230
269
|
// "pdf" is left alone so the previous scatter still weights the forward MIS,
|
|
@@ -233,8 +272,6 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
233
272
|
rayDataStorage[ index ].emission = vec3f( 0.0 );
|
|
234
273
|
rayDataStorage[ index ].scatterColor = vec3f( input.scatterPdf );
|
|
235
274
|
rayDataStorage[ index ].lightPdf = 0.0;
|
|
236
|
-
rayDataStorage[ index ].origin = rayQueue.elements[ alphaIndex ].origin;
|
|
237
|
-
rayDataStorage[ index ].rayIntersectionIndex = i32( alphaIndex );
|
|
238
275
|
rayDataStorage[ index ].shadowRayIntersectionIndex = - 1;
|
|
239
276
|
return;
|
|
240
277
|
|
|
@@ -259,8 +296,9 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
259
296
|
}
|
|
260
297
|
|
|
261
298
|
// attenuate the light transmitted through the volume when exiting a backface so
|
|
262
|
-
// the surface's emission and NEE resolve against the attenuated throughput
|
|
263
|
-
|
|
299
|
+
// the surface's emission and NEE resolve against the attenuated throughput. MediumKernel
|
|
300
|
+
// already applied a scattering medium's transmittance
|
|
301
|
+
if ( input.side < 0.0 && materialInfo.transmission > 0.0 && ! segmentInMedium ) {
|
|
264
302
|
|
|
265
303
|
throughputColor *= ${ transmissionAttenuationFunc }( input.dist, materialInfo.attenuationColor, materialInfo.attenuationDistance );
|
|
266
304
|
rayDataStorage[ index ].throughputColor = throughputColor;
|
|
@@ -271,6 +309,20 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
271
309
|
var scatterRec = ${ bsdfSampleFn }( view, surface );
|
|
272
310
|
let newBounce = input.currentBounce + 1u;
|
|
273
311
|
|
|
312
|
+
// The first surface the path scatters off fills the denoiser's auxiliary images. Until one is
|
|
313
|
+
// found the last mirror or transmission hit stands in, for paths that reach the environment.
|
|
314
|
+
// The albedo is tinted by the throughput of the surfaces looked past to reach it. The packed
|
|
315
|
+
// albedo's alpha marks the surface found
|
|
316
|
+
let auxFound = unpack4x8unorm( input.auxAlbedo ).a != 0.0;
|
|
317
|
+
if ( ! auxFound ) {
|
|
318
|
+
|
|
319
|
+
let isAuxSurface = ${ isAuxSurfaceFn }( surface, scatterRec );
|
|
320
|
+
let auxAlbedo = saturate( throughputColor * surface.color );
|
|
321
|
+
rayDataStorage[ index ].auxAlbedo = pack4x8unorm( vec4f( auxAlbedo, select( 0.0, 1.0, isAuxSurface ) ) );
|
|
322
|
+
rayDataStorage[ index ].auxNormal = pack4x8snorm( vec4f( surface.normal, 0.0 ) );
|
|
323
|
+
|
|
324
|
+
}
|
|
325
|
+
|
|
274
326
|
// decide termination now so finished paths skip the bounce trace entirely - a
|
|
275
327
|
// zeroed pdf reads as a terminating scatter in LogicKernel, which still resolves
|
|
276
328
|
// the surface's emission and NEE before freeing the slot
|
|
@@ -301,23 +353,59 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
301
353
|
rayDataStorage[ index ].isFullyTransmissive = input.isFullyTransmissive & select( 0u, 1u, scatterRec.isTransmissive );
|
|
302
354
|
rayDataStorage[ index ].emission = surface.emission;
|
|
303
355
|
rayDataStorage[ index ].currentBounce = newBounce;
|
|
356
|
+
rayDataStorage[ index ].mediumScattered = 0u;
|
|
357
|
+
|
|
358
|
+
// As in "KHR_materials_scatter", the surface bsdf is the medium's boundary: refracting out
|
|
359
|
+
// ends the walk, reflecting continues it
|
|
360
|
+
if ( segmentInMedium && u32( input.objectIndex ) == medium.object && input.side < 0.0 && scatterRec.isTransmissive ) {
|
|
361
|
+
|
|
362
|
+
medium.inside = false;
|
|
363
|
+
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// refracting into a front face of a scattering material starts a random walk inside it
|
|
367
|
+
if ( scatterRec.isTransmissive && ! surface.thinWall && input.side > 0.0 ) {
|
|
368
|
+
|
|
369
|
+
let entered = ${ enterMediumFn }( materialInfo, u32( input.objectIndex ) );
|
|
370
|
+
if ( entered.inside ) {
|
|
371
|
+
|
|
372
|
+
medium = entered;
|
|
373
|
+
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// keep the medium the path left or entered
|
|
379
|
+
if ( segmentInMedium || medium.inside ) {
|
|
380
|
+
|
|
381
|
+
// a whole struct is written to the buffer, not through the pointer alias, which Safari fails to compile
|
|
382
|
+
${ params.rayDataStorage }[ index ].medium = ${ storeMediumFn }( medium );
|
|
383
|
+
|
|
384
|
+
}
|
|
304
385
|
|
|
305
386
|
// the NEE shadow ray below still resolves the surface's direct light, so only
|
|
306
|
-
// the bounce segment is skipped for finished paths
|
|
307
|
-
if ( ! isTerminated ) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
387
|
+
// the bounce segment is skipped for finished paths. Inside a medium, MediumKernel walks it
|
|
388
|
+
if ( ! isTerminated && medium.inside ) {
|
|
389
|
+
|
|
390
|
+
rayDataStorage[ index ].walkPending = 1u;
|
|
391
|
+
rayDataStorage[ index ].origin = ${ offsetRayOriginFunc }( vertexData.position.xyz, scatterRec.direction, input.normal );
|
|
392
|
+
rayDataStorage[ index ].direction = scatterRec.direction;
|
|
393
|
+
rayDataStorage[ index ].rayStart = 0.0;
|
|
394
|
+
|
|
395
|
+
} else if ( ! isTerminated ) {
|
|
396
|
+
|
|
397
|
+
let rayIndex = atomicAdd( &queueCounters[ ${ RAY_QUEUE_LENGTH }u ], 1u );
|
|
398
|
+
rayQueue[ rayIndex ].origin = ${ offsetRayOriginFunc }( vertexData.position.xyz, scatterRec.direction, input.normal );
|
|
399
|
+
rayQueue[ rayIndex ].direction = scatterRec.direction;
|
|
400
|
+
rayQueue[ rayIndex ].pixelIndex = input.pixelIndex;
|
|
401
|
+
rayQueue[ rayIndex ].rngIndex = newBounce + input.alphaDepth + input.subsurfaceBounces;
|
|
402
|
+
rayQueue[ rayIndex ].seed = input.seed;
|
|
403
|
+
rayQueue[ rayIndex ].maxDist = 0.0;
|
|
317
404
|
rayDataStorage[ index ].rayIntersectionIndex = i32( rayIndex );
|
|
318
405
|
|
|
319
|
-
rayDataStorage[ index ].origin = rayQueue
|
|
406
|
+
rayDataStorage[ index ].origin = rayQueue[ rayIndex ].origin;
|
|
320
407
|
rayDataStorage[ index ].direction = scatterRec.direction;
|
|
408
|
+
rayDataStorage[ index ].rayStart = 0.0;
|
|
321
409
|
|
|
322
410
|
}
|
|
323
411
|
|
|
@@ -332,14 +420,13 @@ export class MaterialKernel extends ComputeKernel {
|
|
|
332
420
|
rayDataStorage[ index ].lightBsdf = evalRec.color;
|
|
333
421
|
rayDataStorage[ index ].lightBsdfPdf = evalRec.pdf;
|
|
334
422
|
|
|
335
|
-
let shadowIndex = atomicAdd( &
|
|
336
|
-
shadowRayQueue
|
|
337
|
-
shadowRayQueue
|
|
338
|
-
shadowRayQueue
|
|
339
|
-
shadowRayQueue
|
|
340
|
-
shadowRayQueue
|
|
341
|
-
shadowRayQueue
|
|
342
|
-
shadowRayQueue.elements[ shadowIndex ].maxDist = input.lightDist - ${ LIGHT_EPSILON };
|
|
423
|
+
let shadowIndex = atomicAdd( &queueCounters[ ${ SHADOW_RAY_QUEUE_LENGTH }u ], 1u );
|
|
424
|
+
shadowRayQueue[ shadowIndex ].origin = ${ offsetRayOriginFunc }( vertexData.position.xyz, input.lightDirection, input.normal );
|
|
425
|
+
shadowRayQueue[ shadowIndex ].direction = input.lightDirection;
|
|
426
|
+
shadowRayQueue[ shadowIndex ].pixelIndex = input.pixelIndex;
|
|
427
|
+
shadowRayQueue[ shadowIndex ].rngIndex = input.currentBounce + input.alphaDepth + input.subsurfaceBounces;
|
|
428
|
+
shadowRayQueue[ shadowIndex ].seed = input.seed;
|
|
429
|
+
shadowRayQueue[ shadowIndex ].maxDist = input.lightDist * ( 1.0 - ${ LIGHT_EPSILON } );
|
|
343
430
|
rayDataStorage[ index ].shadowRayIntersectionIndex = i32( shadowIndex );
|
|
344
431
|
|
|
345
432
|
} else {
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { StorageBufferAttribute } from 'three/webgpu';
|
|
2
|
+
import { ComputeKernel } from '../ComputeKernel.js';
|
|
3
|
+
import { storage, uniform, globalId } from 'three/tsl';
|
|
4
|
+
import { contextProxy, wgslTagFn } from 'three-mesh-bvh/webgpu';
|
|
5
|
+
import { rngInit, rngNextBounce, rand2, RNG_INDEX_MEDIUM_DISTANCE, RNG_INDEX_MEDIUM_DIRECTION } from '../../nodes/random.wgsl.js';
|
|
6
|
+
import { LIGHT_FAR_DISTANCE } from '../../nodes/lights.wgsl.js';
|
|
7
|
+
import { sampleHenyeyGreensteinFn } from '../../nodes/sampling.wgsl.js';
|
|
8
|
+
import {
|
|
9
|
+
loadMediumFn,
|
|
10
|
+
mediumChannelPdfFn, sampleMediumDistanceFn, mediumDistancePdfFn,
|
|
11
|
+
MEDIUM_THROUGHPUT_EPSILON,
|
|
12
|
+
} from '../../nodes/medium.wgsl.js';
|
|
13
|
+
import { rayDataStruct, intersectionResultStruct, MEDIUM_QUEUE_LENGTH, MEDIUM_QUEUE_HEADER } from './structs.js';
|
|
14
|
+
|
|
15
|
+
// Random walks through scattering media, as in Cycles' subsurface kernel but capped per pass. A walk that
|
|
16
|
+
// reaches a surface hands its hit to LogicKernel like a traced ray, one that doesn't resumes next pass. The
|
|
17
|
+
// walk's weight goes into the staged scatter color.
|
|
18
|
+
export class MediumKernel extends ComputeKernel {
|
|
19
|
+
|
|
20
|
+
constructor( ) {
|
|
21
|
+
|
|
22
|
+
const params = {
|
|
23
|
+
maxSubsurfaceBounces: uniform( 256, 'uint' ),
|
|
24
|
+
stepsPerPass: uniform( 16, 'uint' ),
|
|
25
|
+
rayCount: uniform( 0, 'uint' ),
|
|
26
|
+
|
|
27
|
+
rayDataStorage: storage( new StorageBufferAttribute( 1, 1 ), rayDataStruct ),
|
|
28
|
+
mediumQueue: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ),
|
|
29
|
+
rayIntersectionsStorage: storage( new StorageBufferAttribute( 1, 1 ), intersectionResultStruct ),
|
|
30
|
+
|
|
31
|
+
globalId: globalId,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const raycastOutput = contextProxy( 'bvhData.fns.raycastFirstHit.outputType' );
|
|
35
|
+
const raycastFirstHitFn = contextProxy( 'bvhData.fns.raycastFirstHit' );
|
|
36
|
+
|
|
37
|
+
const fn = wgslTagFn/* wgsl */`
|
|
38
|
+
|
|
39
|
+
fn compute( maxSubsurfaceBounces: u32, stepsPerPass: u32, rayCount: u32, globalId: vec3u ) -> void {
|
|
40
|
+
|
|
41
|
+
let rayDataStorage = &${ params.rayDataStorage };
|
|
42
|
+
let mediumQueue = &${ params.mediumQueue };
|
|
43
|
+
let rayIntersectionsStorage = &${ params.rayIntersectionsStorage };
|
|
44
|
+
|
|
45
|
+
if ( globalId.x >= mediumQueue[ ${ MEDIUM_QUEUE_LENGTH }u ] ) {
|
|
46
|
+
|
|
47
|
+
return;
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let index = mediumQueue[ ${ MEDIUM_QUEUE_HEADER }u + globalId.x ];
|
|
52
|
+
// whole structs are read from the buffer, not the pointer alias, which Safari fails to compile
|
|
53
|
+
let input = ${ params.rayDataStorage }[ index ];
|
|
54
|
+
let medium = ${ loadMediumFn }( input.medium );
|
|
55
|
+
|
|
56
|
+
let indexUV = vec2u( input.pixelIndex >> 16, input.pixelIndex & 0xFFFF );
|
|
57
|
+
${ rngInit }( indexUV, input.seed, input.currentBounce + input.alphaDepth + input.subsurfaceBounces );
|
|
58
|
+
|
|
59
|
+
var ray = Ray( input.origin, input.direction, 0.0 );
|
|
60
|
+
var weight = input.scatterColor;
|
|
61
|
+
var subsurfaceBounces = input.subsurfaceBounces;
|
|
62
|
+
var hasScattered = false;
|
|
63
|
+
var isFinished = false;
|
|
64
|
+
var isTerminated = false;
|
|
65
|
+
|
|
66
|
+
for ( var i = 0u; i < stepsPerPass; i ++ ) {
|
|
67
|
+
|
|
68
|
+
// only trace up to the sampled scatter event
|
|
69
|
+
let channelPdf = ${ mediumChannelPdfFn }( input.throughputColor * weight, medium.albedo );
|
|
70
|
+
let scatterDist = ${ sampleMediumDistanceFn }( medium, channelPdf, ${ rand2 }( ${ RNG_INDEX_MEDIUM_DISTANCE } ) );
|
|
71
|
+
ray.maxDist = scatterDist;
|
|
72
|
+
|
|
73
|
+
var hitResult: ${ raycastOutput };
|
|
74
|
+
let didHit = ${ raycastFirstHitFn }( ray, &hitResult );
|
|
75
|
+
let surfaceDist = select( ${ LIGHT_FAR_DISTANCE }, hitResult.dist, didHit );
|
|
76
|
+
|
|
77
|
+
let scattered = scatterDist < surfaceDist;
|
|
78
|
+
let segmentDist = select( surfaceDist, scatterDist, scattered );
|
|
79
|
+
let transmittance = exp( - medium.extinction * segmentDist );
|
|
80
|
+
let distancePdf = dot( channelPdf, ${ mediumDistancePdfFn }( medium, segmentDist, scattered ) );
|
|
81
|
+
if ( distancePdf <= 0.0 ) {
|
|
82
|
+
|
|
83
|
+
isTerminated = true;
|
|
84
|
+
break;
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if ( ! scattered ) {
|
|
89
|
+
|
|
90
|
+
// reaching the surface, weighted by the chance of not scattering before it
|
|
91
|
+
weight *= transmittance / distancePdf;
|
|
92
|
+
|
|
93
|
+
// The hit fills the ray results from the back, by the walk's medium queue position, while the
|
|
94
|
+
// traced rays fill them from the front. A path slot walking a medium queued no ray this round,
|
|
95
|
+
// so the two ranges never exceed the slot count or overlap
|
|
96
|
+
let resultIndex = rayCount - 1u - globalId.x;
|
|
97
|
+
rayIntersectionsStorage[ resultIndex ].barycoord = hitResult.barycoord;
|
|
98
|
+
// a walk that leaves without hitting anything is a miss, as in the trace kernel
|
|
99
|
+
rayIntersectionsStorage[ resultIndex ].objectIndex = select( - 1, i32( hitResult.objectIndex ), didHit );
|
|
100
|
+
rayIntersectionsStorage[ resultIndex ].position = ray.origin + ray.direction * hitResult.dist;
|
|
101
|
+
rayIntersectionsStorage[ resultIndex ].dist = hitResult.dist;
|
|
102
|
+
rayIntersectionsStorage[ resultIndex ].normal = hitResult.normal.xyz;
|
|
103
|
+
rayIntersectionsStorage[ resultIndex ].side = hitResult.side;
|
|
104
|
+
rayIntersectionsStorage[ resultIndex ].indices = hitResult.indices.xyz;
|
|
105
|
+
rayDataStorage[ index ].rayIntersectionIndex = i32( resultIndex );
|
|
106
|
+
|
|
107
|
+
isFinished = true;
|
|
108
|
+
break;
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
weight *= medium.albedo * medium.extinction * transmittance / distancePdf;
|
|
113
|
+
ray.origin += ray.direction * scatterDist;
|
|
114
|
+
ray.direction = ${ sampleHenyeyGreensteinFn }( ray.direction, medium.anisotropy, ${ rand2 }( ${ RNG_INDEX_MEDIUM_DIRECTION } ) );
|
|
115
|
+
hasScattered = true;
|
|
116
|
+
|
|
117
|
+
// capped separately from bounces, and ends once the throughput is negligible
|
|
118
|
+
subsurfaceBounces ++;
|
|
119
|
+
let throughput = input.throughputColor * weight / input.scatterPdf;
|
|
120
|
+
if ( subsurfaceBounces >= maxSubsurfaceBounces || max( max( throughput.r, throughput.g ), throughput.b ) < ${ MEDIUM_THROUGHPUT_EPSILON } ) {
|
|
121
|
+
|
|
122
|
+
isTerminated = true;
|
|
123
|
+
break;
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
${ rngNextBounce }();
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// a zeroed pdf terminates the path in LogicKernel
|
|
132
|
+
rayDataStorage[ index ].scatterColor = weight;
|
|
133
|
+
rayDataStorage[ index ].scatterPdf = select( input.scatterPdf, 0.0, isTerminated );
|
|
134
|
+
rayDataStorage[ index ].origin = ray.origin;
|
|
135
|
+
rayDataStorage[ index ].direction = ray.direction;
|
|
136
|
+
rayDataStorage[ index ].subsurfaceBounces = subsurfaceBounces;
|
|
137
|
+
rayDataStorage[ index ].walkPending = select( 1u, 0u, isFinished || isTerminated );
|
|
138
|
+
if ( hasScattered ) {
|
|
139
|
+
|
|
140
|
+
rayDataStorage[ index ].mediumScattered = 1u;
|
|
141
|
+
rayDataStorage[ index ].isFullyTransmissive = 0u;
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
}
|
|
146
|
+
`;
|
|
147
|
+
|
|
148
|
+
super( fn( params ) );
|
|
149
|
+
|
|
150
|
+
this.defineUniformAccessors( params );
|
|
151
|
+
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
@@ -3,7 +3,7 @@ import { StorageBufferAttribute } from 'three/webgpu';
|
|
|
3
3
|
import { uniform, storage, globalId } from 'three/tsl';
|
|
4
4
|
import { ComputeKernel } from '../ComputeKernel.js';
|
|
5
5
|
import { wgslTagFn } from 'three-mesh-bvh/webgpu';
|
|
6
|
-
import { rayDataStruct,
|
|
6
|
+
import { rayDataStruct, PIXEL_QUEUE_CURRENT, PIXEL_QUEUE_ELEMENT_COUNT, PIXEL_QUEUE_HEADER } from './structs.js';
|
|
7
7
|
|
|
8
8
|
// Runs once per reset: assigns each of the first rayData-pool-count pixels to a path slot and parks
|
|
9
9
|
// the overflow pixel indices in the pixel queue. Slots are initialized so LogicKernel skips them and
|
|
@@ -15,7 +15,7 @@ export class PopulatePixelIndicesKernel extends ComputeKernel {
|
|
|
15
15
|
|
|
16
16
|
const params = {
|
|
17
17
|
rayDataStorage: storage( new StorageBufferAttribute( 1, 1 ), rayDataStruct ),
|
|
18
|
-
pixelQueue: storage( new StorageBufferAttribute( 1, 1 ),
|
|
18
|
+
pixelQueue: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ),
|
|
19
19
|
targetDimensions: uniform( new Vector2() ),
|
|
20
20
|
frameBudget: uniform( 0, 'uint' ),
|
|
21
21
|
globalId: globalId,
|
|
@@ -37,8 +37,8 @@ export class PopulatePixelIndicesKernel extends ComputeKernel {
|
|
|
37
37
|
let pixelCount = targetDimensions.x * targetDimensions.y;
|
|
38
38
|
if ( globalId.x == 0u && globalId.y == 0u ) {
|
|
39
39
|
|
|
40
|
-
pixelQueue
|
|
41
|
-
pixelQueue
|
|
40
|
+
pixelQueue[ ${ PIXEL_QUEUE_CURRENT }u ] = 0u;
|
|
41
|
+
pixelQueue[ ${ PIXEL_QUEUE_ELEMENT_COUNT }u ] = select( 0u, pixelCount - rayCount, pixelCount >= rayCount );
|
|
42
42
|
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -56,7 +56,7 @@ export class PopulatePixelIndicesKernel extends ComputeKernel {
|
|
|
56
56
|
|
|
57
57
|
} else {
|
|
58
58
|
|
|
59
|
-
pixelQueue
|
|
59
|
+
pixelQueue[ ${ PIXEL_QUEUE_HEADER }u + pixelIndex - rayCount ] = pixelHash;
|
|
60
60
|
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -2,26 +2,26 @@ import { IndirectStorageBufferAttribute, StorageBufferAttribute } from 'three/we
|
|
|
2
2
|
import { storage } from 'three/tsl';
|
|
3
3
|
import { ComputeKernel } from '../ComputeKernel.js';
|
|
4
4
|
import { wgslTagFn } from 'three-mesh-bvh/webgpu';
|
|
5
|
-
import {
|
|
5
|
+
import { RAY_QUEUE_LENGTH } from './structs.js';
|
|
6
6
|
|
|
7
7
|
// Converts a queue's atomic length counter into indirect dispatch arguments so the trace kernels can
|
|
8
8
|
// be dispatched with exactly as many threads as there are queued rays.
|
|
9
9
|
export class QueueLengthToDispatchKernel extends ComputeKernel {
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
11
|
+
constructor( counterIndex = RAY_QUEUE_LENGTH, workgroupSize = 64 ) {
|
|
12
12
|
|
|
13
13
|
const params = {
|
|
14
|
-
|
|
14
|
+
queueCounters: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ).toAtomic(),
|
|
15
15
|
outputDispatch: storage( new IndirectStorageBufferAttribute( 3, 1 ), 'u32' ).setName( 'outputDispatch' ),
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
const fn = wgslTagFn/* wgsl */`
|
|
19
19
|
fn compute() -> void {
|
|
20
20
|
|
|
21
|
-
let
|
|
21
|
+
let queueCounters = &${ params.queueCounters };
|
|
22
22
|
let outputDispatch = &${ params.outputDispatch };
|
|
23
23
|
|
|
24
|
-
let queueLength = atomicLoad( &
|
|
24
|
+
let queueLength = atomicLoad( &queueCounters[ ${ counterIndex }u ] );
|
|
25
25
|
outputDispatch[ 0 ] = ( queueLength + ${ workgroupSize - 1 }u ) / ${ workgroupSize }u;
|
|
26
26
|
outputDispatch[ 1 ] = 1u;
|
|
27
27
|
outputDispatch[ 2 ] = 1u;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { StorageBufferAttribute } from 'three/webgpu';
|
|
2
|
+
import { uniform, storage, globalId } from 'three/tsl';
|
|
3
|
+
import { ComputeKernel } from '../ComputeKernel.js';
|
|
4
|
+
import { wgslTagFn } from 'three-mesh-bvh/webgpu';
|
|
5
|
+
import { rayDataStruct, MEDIUM_QUEUE_LENGTH, MEDIUM_QUEUE_HEADER } from './structs.js';
|
|
6
|
+
|
|
7
|
+
// Gathers the path slots walking a medium for MediumKernel's dispatch. Separate from MaterialKernel, which
|
|
8
|
+
// is at its storage buffer limit.
|
|
9
|
+
export class QueueMediumWalksKernel extends ComputeKernel {
|
|
10
|
+
|
|
11
|
+
constructor( ) {
|
|
12
|
+
|
|
13
|
+
const params = {
|
|
14
|
+
rayCount: uniform( 0, 'uint' ),
|
|
15
|
+
|
|
16
|
+
rayDataStorage: storage( new StorageBufferAttribute( 1, 1 ), rayDataStruct ).toReadOnly(),
|
|
17
|
+
mediumQueue: storage( new StorageBufferAttribute( 1, 1 ), 'uint' ).toAtomic(),
|
|
18
|
+
|
|
19
|
+
globalId: globalId,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const fn = wgslTagFn/* wgsl */`
|
|
23
|
+
|
|
24
|
+
fn compute( rayCount: u32, globalId: vec3u ) -> void {
|
|
25
|
+
|
|
26
|
+
let rayDataStorage = &${ params.rayDataStorage };
|
|
27
|
+
let mediumQueue = &${ params.mediumQueue };
|
|
28
|
+
|
|
29
|
+
let index = globalId.x;
|
|
30
|
+
if ( index >= rayCount ) {
|
|
31
|
+
|
|
32
|
+
return;
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ( rayDataStorage[ index ].walkPending != 0u && rayDataStorage[ index ].objectIndex >= 0 ) {
|
|
37
|
+
|
|
38
|
+
let queueIndex = atomicAdd( &mediumQueue[ ${ MEDIUM_QUEUE_LENGTH }u ], 1u );
|
|
39
|
+
atomicStore( &mediumQueue[ ${ MEDIUM_QUEUE_HEADER }u + queueIndex ], index );
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
super( fn( params ) );
|
|
47
|
+
|
|
48
|
+
this.defineUniformAccessors( params );
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|