-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathRayIntersectionKernel.js
More file actions
145 lines (109 loc) · 4.95 KB
/
RayIntersectionKernel.js
File metadata and controls
145 lines (109 loc) · 4.95 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
import { DataTexture, Matrix3, IndirectStorageBufferAttribute, StorageTexture } from 'three/webgpu';
import { ComputeKernel } from '../ComputeKernel.js';
import { uniform, texture, sampler, storage, textureStore, globalId } from 'three/tsl';
import { pcgRand2, pcgInit } from '../../nodes/random.wgsl.js';
import { queuedRayStruct, queuedHitStruct } from './structs.js';
import { proxy } from '../../lib/nodes/NodeProxy.js';
import { sampleEnvironmentFn, weightedAlphaBlendFn } from '../../nodes/sampling.wgsl.js';
import { wgslTagFn } from '../../lib/nodes/WGSLTagFnNode.js';
export class RayIntersectionKernel 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(),
// rays
rayQueue: storage( new IndirectStorageBufferAttribute( 1, queuedRayStruct.getLength() ), queuedRayStruct ).toReadOnly(),
hitQueue: storage( new IndirectStorageBufferAttribute( 1, queuedHitStruct.getLength() ), queuedHitStruct ),
queueSizes: storage( new IndirectStorageBufferAttribute( 4, 1 ), 'u32' ).toAtomic(),
// 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 ),
globalId: globalId,
};
const raycastOutput = proxy( 'bvhData.value.fns.raycastFirstHit.outputType', params );
const raycastFirstHitFn = proxy( 'bvhData.value.fns.raycastFirstHit', params );
const fn = wgslTagFn /* wgsl */`
fn compute(
// environment
envMap: texture_2d<f32>,
envMapSampler: sampler,
envMapRotation: mat3x3f,
envMapIntensity: f32,
background: texture_2d<f32>,
backgroundSampler: sampler,
backgroundRotation: mat3x3f,
backgroundIntensity: f32,
backgroundBlurriness: f32,
globalId: vec3u
) -> void {
let rayQueue = &${ params.rayQueue };
let hitQueue = &${ params.hitQueue };
let queueSizes = &${ params.queueSizes };
let envInfo = EnvironmentInfo(
envMapRotation,
envMapIntensity,
0.0 // blur,
);
let backgroundInfo = EnvironmentInfo(
backgroundRotation,
backgroundIntensity,
backgroundBlurriness,
);
// skip any rays invocations beyond the ray count
let queueCapacity = arrayLength( rayQueue );
let rayIndex = ( globalId.x + atomicLoad( &queueSizes[ 0 ] ) );
if ( rayIndex >= atomicLoad( &queueSizes[ 1 ] ) ) {
return;
}
// get the ray info
let ACTIVE_FLAG = 0xF0000000u;
let input = rayQueue[ rayIndex % queueCapacity ];
let indexUV = input.pixel;
let seed = ( textureLoad( ${ params.sampleCountTarget }, indexUV ).r & ( ~ ACTIVE_FLAG ) ) + input.currentBounce;
${ pcgInit }( indexUV, seed );
// run intersection
let ray = Ray( input.origin, input.direction );
var hitResult: ${ raycastOutput };
if ( ${ raycastFirstHitFn }( ray, &hitResult ) ) {
// TODO: we process all of these materials immediately to push to the ray queue
let index = atomicAdd( &queueSizes[ 3 ], 1 );
hitQueue[ index ].view = - input.direction;
hitQueue[ index ].indices = hitResult.indices.xyz;
hitQueue[ index ].barycoord = hitResult.barycoord;
hitQueue[ index ].normal = hitResult.normal.xyz;
hitQueue[ index ].side = hitResult.side;
hitQueue[ index ].pixel_x = input.pixel.x;
hitQueue[ index ].pixel_y = input.pixel.y;
hitQueue[ index ].objectIndex = hitResult.objectIndex;
hitQueue[ index ].throughputColor = input.throughputColor;
hitQueue[ index ].currentBounce = input.currentBounce;
hitQueue[ index ].pcgStateS0 = input.pcgStateS0;
hitQueue[ index ].resultColor = input.resultColor;
} else {
var resultColor = input.resultColor;
if ( input.currentBounce > 0u ) {
resultColor += ${ sampleEnvironmentFn }( envMap, envMapSampler, envInfo, input.direction, ${ pcgRand2 }() ) * vec4f( input.throughputColor, 0.0 );
} else {
resultColor = ${ sampleEnvironmentFn }( background, backgroundSampler, backgroundInfo, input.direction, ${ pcgRand2 }() );
}
let sampleCount = ( textureLoad( ${ params.sampleCountTarget }, indexUV ).r & ( ~ ACTIVE_FLAG ) ) + 1;
let prevColor = textureLoad( ${ params.prevOutputTarget }, indexUV );
let blendedColor = ${ weightedAlphaBlendFn }( prevColor, resultColor, 1.0 / f32( sampleCount ) );
textureStore( ${ params.sampleCountTarget }, indexUV, vec4( sampleCount ) );
textureStore( ${ params.outputTarget }, indexUV, blendedColor );
}
}
`;
super( fn( params ) );
this.defineUniformAccessors( params );
}
}