-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.ts
318 lines (287 loc) · 10.4 KB
/
types.ts
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
/// <reference types="@webgpu/types" />
export type _GPUShaderStageRTX = number & { readonly brand: unique symbol };
export type _GPURayTracingAccelerationContainerUsage = number & { readonly brand: unique symbol };
export type _GPURayTracingAccelerationGeometryUsage = number & { readonly brand: unique symbol };
export type _GPURayTracingAccelerationInstanceUsage = number & { readonly brand: unique symbol };
// Extends existing WebGPU interfaces
declare global {
interface GPUDevice {
/**
* The size in bytes of the shader header.
*/
readonly ShaderGroupHandleSize: number;
/**
* The required alignment in bytes for the base of the shader binding table.
*/
readonly ShaderGroupBaseAlignment: number;
/**
* The required alignment in bytes for each shader binding table entry.
*/
readonly ShaderGroupHandleAlignment: number;
/**
* The maximum allowed size for the shader record stride.
*/
readonly ShaderGroupRecordMaxStride: number;
/**
* Creates a new acceleration structure object. Current implementation does
* not support creating bottom-level acceleration structures separately,
* they must be part of the top-level acceleration structure description.
* @param descriptor - Description of the {@link GPURayTracingAccelerationContainer_top} to create.
*/
createRayTracingAccelerationContainer(descriptor: GPURayTracingAccelerationContainerDescriptor_top): GPURayTracingAccelerationContainer_top;
/**
* Builds an acceleration structure on the host.
* @param container - The {@link GPURayTracingAccelerationContainer_top} to be built.
*/
hostBuildRayTracingAccelerationContainer(container: GPURayTracingAccelerationContainer_top): void;
/**
* Creates a new ray tracing pipeline object.
* @param descriptor - Description of the {@link GPURayTracingPipeline} to create.
* @param tlas - The built {@link GPURayTracingAccelerationContainer_top}.
* Current implementation requires it to properly set states
* in the combined shader of the ray tracing pipeline.
*/
createRayTracingPipeline(descriptor: GPURayTracingPipelineDescriptor, tlas: GPURayTracingAccelerationContainer_top): Promise<GPURayTracingPipeline>;
}
interface GPUCommandEncoder {
/**
* Begins encoding a ray tracing pass.
*/
beginRayTracingPass(): GPURayTracingPassEncoder;
}
}
// Global variables
declare global {
/**
* A special handle denoting that all shaders in the hit group are not used.
*/
var WEBRTX_HIT_GROUP_ALL_SHADERS_UNUSED_HANDLE: number;
// TODO: figure out how to extend GPUBufferUsage and GPUShaderStage instead of
// introducing types and variables
var GPUBufferUsageRTX: {
/**
* VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR
*/
ACCELERATION_STRUCTURE_BUILD_INPUT_READONLY: GPUFlagsConstant,
/**
* VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR
*/
SHADER_BINDING_TABLE: GPUFlagsConstant,
};
/**
* Ray tracing extension specific shader stages.
*/
var GPUShaderStageRTX: {
/**
* Ray generation shader.
*/
RAY_GENERATION: _GPUShaderStageRTX,
/**
* Ray any-hit shader, called whenever a ray hit occurs.
*/
RAY_ANY_HIT: _GPUShaderStageRTX,
/**
* Ray closest-hit shader, called once for the closest hit along a ray.
*/
RAY_CLOSEST_HIT: _GPUShaderStageRTX,
/**
* Ray miss shader, called when a ray didn't hit anything.
*/
RAY_MISS: _GPUShaderStageRTX,
/**
* Ray intersection shader, implements ray-primitive intersections e.g. for
* procedural geometries.
*/
RAY_INTERSECTION: _GPUShaderStageRTX,
};
/**
* Usage flags for GPURayTracingAccelerationContainer.
*/
var GPURayTracingAccelerationContainerUsage: {
NONE: _GPURayTracingAccelerationContainerUsage,
// TODO: implement these
// ALLOW_UPDATE: _GPURayTracingAccelerationContainerUsage,
// ALLOW_COMPACTION: _GPURayTracingAccelerationContainerUsage,
// PREFER_FAST_TRACE: _GPURayTracingAccelerationContainerUsage,
// PREFER_FAST_BUILD: _GPURayTracingAccelerationContainerUsage,
// LOW_MEMORY: _GPURayTracingAccelerationContainerUsage,
};
/**
* Usage flags for geometries in GPURayTracingAccelerationContainer.
*/
var GPURayTracingAccelerationGeometryUsage: {
NONE: _GPURayTracingAccelerationGeometryUsage,
// TODO: implement these
// OPAQUE: _GPURayTracingAccelerationGeometryUsage,
// NO_DUPLICATE_ANY_HIT_INVOCATION: _GPURayTracingAccelerationGeometryUsage,
};
/**
* Usage flags for instances in GPURayTracingAccelerationContainer.
*/
var GPURayTracingAccelerationInstanceUsage: {
NONE: _GPURayTracingAccelerationInstanceUsage,
// TODO: implement these
// TRIANGLE_FACING_CULL_DISABLE: _GPURayTracingAccelerationInstanceUsage,
// TRIANGLE_FRONT_COUNTERCLOCKWISE: _GPURayTracingAccelerationInstanceUsage,
// FORCE_OPAQUE: _GPURayTracingAccelerationInstanceUsage,
// FORCE_NO_OPAQUE: _GPURayTracingAccelerationInstanceUsage,
};
}
// Global types
declare global {
type GPURayTracingAccelerationContainerLevel =
| 'bottom'
| 'top';
interface GPURayTracingAccelerationGeometryVertexDescriptor
extends GPUBufferBinding {
format: 'float32x3', // TODO: support GPUVertexFormat;
stride: GPUSize64;
}
interface GPURayTracingAccelerationGeometryIndexDescriptor
extends GPUBufferBinding {
format: 'uint32', // TODO: support GPUIndexFormat;
}
interface GPURayTracingAccelerationGeometryAABBDescriptor
extends GPUBufferBinding {
format: 'float32x2';
stride: GPUSize64;
}
interface GPURayTracingAccelerationGeometryDescriptor_triangles {
usage: _GPURayTracingAccelerationGeometryUsage;
type: 'triangles';
vertex: GPURayTracingAccelerationGeometryVertexDescriptor;
index?: GPURayTracingAccelerationGeometryIndexDescriptor;
// TODO: support optional transform matrix
}
interface GPURayTracingAccelerationGeometryDescriptor_aabbs {
usage: _GPURayTracingAccelerationGeometryUsage;
type: 'aabbs';
aabb: GPURayTracingAccelerationGeometryAABBDescriptor;
}
type GPURayTracingAccelerationGeometryDescriptor =
| GPURayTracingAccelerationGeometryDescriptor_triangles
| GPURayTracingAccelerationGeometryDescriptor_aabbs;
interface GPURayTracingAccelerationInstanceDescriptor {
usage: _GPURayTracingAccelerationInstanceUsage;
/**
* An 8-bit visibility mask for the geometry. Currently not used.
*/
mask: number;
/**
* A 24-bit user-specified index value accessible to ray shaders via gl_InstanceCustomIndex.
*/
instanceCustomIndex?: number;
/**
* A 24-bit offset used in calculating the hit shader binding table index.
*/
instanceSBTRecordOffset: number;
/**
* 3x4 row-major affine transform matrix.
*/
transformMatrix?: Float32Array;
// TODO: instead of specifying not-built descriptor, allow built TLAS or BLAS
blas: GPURayTracingAccelerationContainerDescriptor_bottom,
}
interface GPURayTracingAccelerationContainerDescriptor_bottom {
usage: _GPURayTracingAccelerationContainerUsage;
level: 'bottom';
// TODO: geometries should be of only single type, not both.
geometries: GPURayTracingAccelerationGeometryDescriptor[];
}
interface GPURayTracingAccelerationContainerDescriptor_top {
usage: _GPURayTracingAccelerationContainerUsage;
level: 'top';
instances: GPURayTracingAccelerationInstanceDescriptor[];
}
interface GPURayTracingShaderStageDescriptor {
stage: _GPUShaderStageRTX;
/**
* Code for the shader stage, currently only GLSL_EXT_ray_tracing is supported.
*/
glslCode: string;
entryPoint: string;
}
interface GPURayTracingShaderTrianglesHitGroupDescriptor {
type: 'triangles-hit-group';
closestHitIndex?: number;
anyHitIndex?: number;
}
interface GPURayTracingShaderProceduralHitGroupDescriptor {
type: 'procedural-hit-group';
intersectionIndex: number;
closestHitIndex?: number;
anyHitIndex?: number;
}
interface GPURayTracingShaderGeneralGroupDescriptor {
type: 'general';
generalIndex: number;
}
type GPURayTracingShaderGroupDescriptor =
| GPURayTracingShaderGeneralGroupDescriptor
| GPURayTracingShaderTrianglesHitGroupDescriptor
| GPURayTracingShaderProceduralHitGroupDescriptor;
// format
// 00 rahit rchit int
type ShaderGroupHandle = number;
interface BufferRegion {
// TODO: allow using different buffers per shader, see GPUShaderBindingTable
// buffer: GPUBuffer;
start: GPUSize32;
stride: GPUSize32;
size: GPUSize32;
}
/**
* Shader binding table consists of a set of shader function handles and
* embedded parameters for these functions.
*/
interface GPUShaderBindingTable {
buffer: GPUBuffer;
rayGen: BufferRegion,
rayMiss: BufferRegion,
rayHit: BufferRegion,
callable: BufferRegion,
}
interface GPURayTracingPipelineDescriptor {
// TODO: allow specifying layout
/**
* The set of the shader stages to be included in the ray tracing pipeline.
*/
stages: GPURayTracingShaderStageDescriptor[];
/**
* The set of the shader stages to be included in each shader group in the ray tracing pipeline.
*/
groups: GPURayTracingShaderGroupDescriptor[];
}
interface GPURayTracingPipeline {
getBindGroupLayout(index: number): GPUBindGroupLayout;
/**
* Query ray tracing pipeline shader group handles, see vkGetRayTracingShaderGroupHandlesKHR.
* @param first
* @param count
*/
getShaderGroupHandles(first: number, count: number): ShaderGroupHandle[];
}
interface GPURayTracingPassEncoder {
setPipeline(pipeline: GPURayTracingPipeline): void;
setBindGroup(index: GPUIndex32, bindGroup: GPUBindGroup): void;
/**
* Initializes a ray tracing dispatch.
* @param device
* @param sbt - The shader binding table data to be used in this pass.
* @param width - The width of the ray trace query dimensions.
* @param height - The height of the ray trace query dimensions.
* @param depth - The depth of the ray trace query dimensions.
*/
traceRays(
device: GPUDevice,
sbt: GPUShaderBindingTable,
width: GPUSize32,
height: GPUSize32,
depth?: GPUSize32,
): void;
end(): void;
}
// TODO: extends GPUBindingResource
interface GPURayTracingAccelerationContainer_top {
}
}