-
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathPathTracerMegaKernel.js
More file actions
201 lines (141 loc) · 6.34 KB
/
PathTracerMegaKernel.js
File metadata and controls
201 lines (141 loc) · 6.34 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
import { DataTexture, Matrix3, Matrix4, Vector2, StorageTexture } from 'three/webgpu';
import { ndcToCameraRay } from '../lib/wgsl/common.wgsl.js';
import { ComputeKernel } from './ComputeKernel.js';
import { texture, sampler, uniform, globalId, textureStore } from 'three/tsl';
import { pcgRand2, pcgInit } from '../nodes/random.wgsl.js';
import { getSurfaceRecordFunc, lambertBsdfFunc } from '../nodes/material.wgsl.js';
import { sampleEnvironmentFn, weightedAlphaBlendFn } from '../nodes/sampling.wgsl.js';
import { proxy, proxyFn } from '../lib/nodes/NodeProxy.js';
import { wgslTagFn } from '../lib/nodes/WGSLTagFnNode.js';
import { packCompensationFn, unpackCompensationFn } from '../nodes/f16packing.wgsl.js';
export class PathTracerMegaKernel extends ComputeKernel {
constructor() {
const params = {
bvhData: { value: null },
prevOutputTarget: textureStore( new StorageTexture( 1, 1 ) ).toReadOnly(),
outputTarget: textureStore( new StorageTexture( 1, 1 ) ).toWriteOnly(),
sampleCountTarget: textureStore( new StorageTexture( 1, 1 ) ).toReadWrite(),
compensationTarget: textureStore( new StorageTexture( 1, 1 ) ).toReadWrite(),
offset: uniform( new Vector2() ),
tileSize: uniform( new Vector2() ),
seed: uniform( 0 ),
bounces: uniform( 5 ),
// transforms
inverseProjectionMatrix: uniform( new Matrix4() ),
cameraToModelMatrix: uniform( new Matrix4() ),
// environment
envMap: texture( new DataTexture() ),
envMapSampler: sampler( new DataTexture() ),
envMapRotation: uniform( new Matrix3() ),
envMapIntensity: uniform( 1 ),
background: texture( new DataTexture() ),
backgroundSampler: sampler( new DataTexture() ),
backgroundRotation: uniform( new Matrix3() ),
backgroundIntensity: uniform( 1 ),
backgroundBlurriness: uniform( 0 ),
textures: texture( new DataTexture() ),
textureSampler: sampler( new DataTexture() ),
// compute variables
globalId: globalId,
};
const raycastFirstHitFn = proxyFn( 'bvhData.value.fns.raycastFirstHit', params );
const sampleTrianglePointFn = proxyFn( 'bvhData.value.fns.sampleTrianglePoint', params );
const shader = wgslTagFn/* wgsl */`
fn compute(
// indices and target
globalId: vec3u,
// tiles
offset: vec2u,
tileSize: vec2u,
// settings
inverseProjectionMatrix: mat4x4f,
cameraToModelMatrix: mat4x4f,
seed: u32,
bounces: u32,
// environment
envMap: texture_2d<f32>,
envMapSampler: sampler,
envMapRotation: mat3x3f,
envMapIntensity: f32,
background: texture_2d<f32>,
backgroundSampler: sampler,
backgroundRotation: mat3x3f,
backgroundIntensity: f32,
backgroundBlurriness: f32,
textures: texture_2d_array<f32>,
textureSampler: sampler
) -> void {
let transforms = &${ proxy( 'bvhData.value.storage.transforms', params ) };
let materials = &${ proxy( 'bvhData.value.storage.materials', params ) };
let envInfo = EnvironmentInfo(
envMapRotation,
envMapIntensity,
0.0 // blur,
);
let backgroundInfo = EnvironmentInfo(
backgroundRotation,
backgroundIntensity,
backgroundBlurriness,
);
// make sure we don't bleed over the edge of our tile
if ( globalId.x >= tileSize.x || globalId.y >= tileSize.y ) {
return;
}
// to screen coordinates
let indexUV = offset + globalId.xy;
let targetDimensions = textureDimensions( ${ params.outputTarget } );
if ( indexUV.x >= targetDimensions.x || indexUV.y >= targetDimensions.y ) {
return;
}
let uv = vec2f( indexUV ) / vec2f( targetDimensions );
let ndc = uv * 2.0 - vec2f( 1.0 );
${ pcgInit }( indexUV, seed );
// scene ray
var jitter = 2.0 * ${ pcgRand2 }() / vec2f( targetDimensions.xy );
var ray = ${ ndcToCameraRay }( ndc + jitter, cameraToModelMatrix * inverseProjectionMatrix );
ray.direction = normalize( ray.direction );
var resultColor = vec4f( 0, 0, 0, 1 );
var throughputColor = vec3f( 1.0 );
for ( var bounce = 0u; bounce < bounces; bounce ++ ) {
let hitResult = ${ raycastFirstHitFn }( ray );
if ( hitResult.didHit ) {
let object = transforms[ hitResult.objectIndex ];
var material = materials[ object.materialIndex ];
// apply per-object colors
material.color *= object.color.rgb;
material.opacity *= object.color.a;
var vertexData = ${ sampleTrianglePointFn }( hitResult.barycoord, hitResult.indices.xyz );
vertexData.normal = normalize( transpose( object.inverseMatrixWorld ) * vertexData.normal );
vertexData.position = object.matrixWorld * vertexData.position;
let surface = ${ getSurfaceRecordFunc }( material, vertexData, hitResult.side, hitResult.normal, textures, textureSampler );
let scatterRec = ${ lambertBsdfFunc }( - ray.direction, surface );
// white diffuse surface
throughputColor *= scatterRec.color / scatterRec.pdf;
ray.origin = vertexData.position.xyz;
ray.direction = scatterRec.direction;
} else {
if ( bounce > 0u ) {
resultColor = ${ sampleEnvironmentFn }( envMap, envMapSampler, envInfo, ray.direction, pcgRand2() ) * vec4f( throughputColor, 1.0 );
} else {
resultColor = ${ sampleEnvironmentFn }( background, backgroundSampler, backgroundInfo, ray.direction, pcgRand2() );
}
break;
}
}
// Kahan-compensated running mean: recover true mean before computing delta
let prevColor = textureLoad( ${ params.prevOutputTarget }, indexUV );
let packedComp = textureLoad( ${ params.compensationTarget }, indexUV ).r;
let compensation = ${ unpackCompensationFn }( packedComp, prevColor );
let sampleCount = textureLoad( ${ params.sampleCountTarget }, indexUV ).r + 1;
let blendedColor = ${ weightedAlphaBlendFn }( prevColor + compensation, resultColor, 1.0 / f32( sampleCount ) );
// simulate FP16 rounding via pack/unpack to compute the residual that will be lost at store
let storedColor = quantizeToF16( blendedColor );
let newPackedComp = ${ packCompensationFn }( blendedColor - storedColor, storedColor );
textureStore( ${ params.sampleCountTarget }, indexUV, vec4( sampleCount ) );
textureStore( ${ params.outputTarget }, indexUV, storedColor );
textureStore( ${ params.compensationTarget }, indexUV, vec4( newPackedComp ) );
}`;
super( shader( params ) );
this.defineUniformAccessors( params );
}
}