forked from webgpu/webgpu-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
430 lines (389 loc) · 11.6 KB
/
main.ts
File metadata and controls
430 lines (389 loc) · 11.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
import { mat4, vec2, vec3 } from 'wgpu-matrix';
import { GUI } from 'dat.gui';
import { mesh } from '../../meshes/teapot';
import computePickPrimitive from './computePickPrimitive.wgsl';
import vertexForwardRendering from './vertexForwardRendering.wgsl';
import fragmentForwardRendering from './fragmentForwardRendering.wgsl';
import vertexTextureQuad from './vertexTextureQuad.wgsl';
import fragmentPrimitivesDebugView from './fragmentPrimitivesDebugView.wgsl';
import {
quitIfWebGPUNotAvailableOrMissingFeatures,
quitIfFeaturesNotAvailable,
} from '../util';
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
const requiredFeatures: GPUFeatureName[] = ['primitive-index'];
quitIfFeaturesNotAvailable(adapter, requiredFeatures);
const device = await adapter.requestDevice({
requiredFeatures,
});
quitIfWebGPUNotAvailableOrMissingFeatures(adapter, device);
const context = canvas.getContext('webgpu') as GPUCanvasContext;
const devicePixelRatio = window.devicePixelRatio;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const aspect = canvas.width / canvas.height;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat,
});
// Create the model vertex buffer.
const kVertexStride = 6;
const vertexBuffer = device.createBuffer({
// position: vec3, normal: vec3
size: mesh.positions.length * kVertexStride * Float32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.VERTEX,
mappedAtCreation: true,
});
{
const mapping = new Float32Array(vertexBuffer.getMappedRange());
for (let i = 0; i < mesh.positions.length; ++i) {
mapping.set(mesh.positions[i], kVertexStride * i);
mapping.set(mesh.normals[i], kVertexStride * i + 3);
}
vertexBuffer.unmap();
}
// Create the model index buffer.
const indexCount = mesh.triangles.length * 3;
const indexBuffer = device.createBuffer({
size: indexCount * Uint16Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.INDEX,
mappedAtCreation: true,
});
{
const mapping = new Uint16Array(indexBuffer.getMappedRange());
for (let i = 0; i < mesh.triangles.length; ++i) {
mapping.set(mesh.triangles[i], 3 * i);
}
indexBuffer.unmap();
}
// Render targets
// The primitive index for each triangle will be written out to this texture.
// Using a r32uint texture ensures we can store the full range of primitive indices.
const primitiveIndexTexture = device.createTexture({
size: [canvas.width, canvas.height],
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
format: 'r32uint',
});
const depthTexture = device.createTexture({
size: [canvas.width, canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
});
const vertexBuffers: Iterable<GPUVertexBufferLayout> = [
{
arrayStride: Float32Array.BYTES_PER_ELEMENT * kVertexStride,
attributes: [
{
// position
shaderLocation: 0,
offset: 0,
format: 'float32x3',
},
{
// normal
shaderLocation: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 3,
format: 'float32x3',
},
],
},
];
const primitive: GPUPrimitiveState = {
topology: 'triangle-list',
// Using `none` because the teapot has gaps that you can see the backfaces through.
cullMode: 'none',
};
const forwardRenderingPipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
module: device.createShaderModule({
code: vertexForwardRendering,
}),
buffers: vertexBuffers,
},
fragment: {
module: device.createShaderModule({
code: fragmentForwardRendering,
}),
targets: [
// color
{ format: presentationFormat },
// primitive-id
{ format: 'r32uint' },
],
},
depthStencil: {
depthWriteEnabled: true,
depthCompare: 'less',
format: 'depth24plus',
},
primitive,
});
const primitiveTextureBindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
texture: {
sampleType: 'uint',
},
},
],
});
const primitivesDebugViewPipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [primitiveTextureBindGroupLayout],
}),
vertex: {
module: device.createShaderModule({
code: vertexTextureQuad,
}),
},
fragment: {
module: device.createShaderModule({
code: fragmentPrimitivesDebugView,
}),
targets: [
{
format: presentationFormat,
},
],
},
primitive,
});
const pickBindGroupLayout = device.createBindGroupLayout({
entries: [
{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: { type: 'storage' },
},
{
binding: 1,
visibility: GPUShaderStage.COMPUTE,
texture: {
sampleType: 'uint',
},
},
],
});
const pickPipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [pickBindGroupLayout],
}),
compute: {
module: device.createShaderModule({
code: computePickPrimitive,
}),
},
});
const forwardRenderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
// view is acquired and set in render loop.
view: undefined,
clearValue: [0.0, 0.0, 1.0, 1.0],
loadOp: 'clear',
storeOp: 'store',
},
{
view: primitiveIndexTexture.createView(),
loadOp: 'clear',
storeOp: 'store',
},
],
depthStencilAttachment: {
view: depthTexture.createView(),
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
};
const textureQuadPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
// view is acquired and set in render loop.
view: undefined,
clearValue: [0, 0, 0, 1],
loadOp: 'clear',
storeOp: 'store',
},
],
};
const settings = {
mode: 'rendering',
rotate: true,
};
const gui = new GUI();
gui.add(settings, 'mode', ['rendering', 'primitive indexes']);
gui.add(settings, 'rotate');
const kMatrixSizeBytes = Float32Array.BYTES_PER_ELEMENT * 16;
const kPickUniformsSizeBytes = Float32Array.BYTES_PER_ELEMENT * 4;
const modelUniformBuffer = device.createBuffer({
size: kMatrixSizeBytes * 2, // two 4x4 matrix
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const frameUniformBuffer = device.createBuffer({
size: kMatrixSizeBytes * 2 + kPickUniformsSizeBytes, // two 4x4 matrix + a vec4's worth of picking uniforms
usage:
GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE,
});
const sceneUniformBindGroup = device.createBindGroup({
layout: forwardRenderingPipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: {
buffer: modelUniformBuffer,
},
},
{
binding: 1,
resource: {
buffer: frameUniformBuffer,
},
},
],
});
const primitiveTextureBindGroup = device.createBindGroup({
layout: primitiveTextureBindGroupLayout,
entries: [
{
binding: 0,
resource: primitiveIndexTexture.createView(),
},
],
});
const pickBindGroup = device.createBindGroup({
layout: pickBindGroupLayout,
entries: [
{
binding: 0,
resource: frameUniformBuffer,
},
{
binding: 1,
resource: primitiveIndexTexture.createView(),
},
],
});
//--------------------
// Scene matrices
const eyePosition = vec3.fromValues(0, 12, -25);
const upVector = vec3.fromValues(0, 1, 0);
const origin = vec3.fromValues(0, 0, 0);
const projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 2000.0);
// Move the model so it's centered.
const modelMatrix = mat4.translation([0, 0, 0]);
device.queue.writeBuffer(modelUniformBuffer, 0, modelMatrix);
const invertTransposeModelMatrix = mat4.invert(modelMatrix);
mat4.transpose(invertTransposeModelMatrix, invertTransposeModelMatrix);
const normalModelData = invertTransposeModelMatrix;
device.queue.writeBuffer(
modelUniformBuffer,
64,
normalModelData.buffer,
normalModelData.byteOffset,
normalModelData.byteLength
);
// Pointer tracking
const pickCoord = vec2.fromValues(0, 0);
function onPointerEvent(event: PointerEvent) {
// Only track the primary pointer
if (event.isPrimary) {
const clientRect = (event.target as Element).getBoundingClientRect();
// Get the pixel offset from the top-left of the canvas element.
pickCoord[0] = (event.clientX - clientRect.x) * devicePixelRatio;
pickCoord[1] = (event.clientY - clientRect.y) * devicePixelRatio;
}
}
canvas.addEventListener('pointerenter', onPointerEvent);
canvas.addEventListener('pointermove', onPointerEvent);
// Rotates the camera around the origin based on time.
let rad = 0;
function getCameraViewProjMatrix() {
if (settings.rotate) {
rad = Math.PI * (Date.now() / 10000);
}
const rotation = mat4.rotateY(mat4.translation(origin), rad);
const rotatedEyePosition = vec3.transformMat4(eyePosition, rotation);
const viewMatrix = mat4.lookAt(rotatedEyePosition, origin, upVector);
return mat4.multiply(projectionMatrix, viewMatrix);
}
function frame() {
const cameraViewProj = getCameraViewProjMatrix();
device.queue.writeBuffer(
frameUniformBuffer,
0,
cameraViewProj.buffer,
cameraViewProj.byteOffset,
cameraViewProj.byteLength
);
const cameraInvViewProj = mat4.invert(cameraViewProj);
device.queue.writeBuffer(
frameUniformBuffer,
64,
cameraInvViewProj.buffer,
cameraInvViewProj.byteOffset,
cameraInvViewProj.byteLength
);
device.queue.writeBuffer(
frameUniformBuffer,
128,
pickCoord.buffer,
pickCoord.byteOffset,
pickCoord.byteLength
);
const commandEncoder = device.createCommandEncoder();
{
// Forward rendering pass
forwardRenderPassDescriptor.colorAttachments[0].view = context
.getCurrentTexture()
.createView();
const forwardPass = commandEncoder.beginRenderPass(
forwardRenderPassDescriptor
);
forwardPass.setPipeline(forwardRenderingPipeline);
forwardPass.setBindGroup(0, sceneUniformBindGroup);
forwardPass.setVertexBuffer(0, vertexBuffer);
forwardPass.setIndexBuffer(indexBuffer, 'uint16');
forwardPass.drawIndexed(indexCount);
forwardPass.end();
}
{
if (settings.mode === 'primitive indexes') {
// Primitive Index debug view
// Overwrites the canvas texture with a visualization of the primitive
// index for each primitive
textureQuadPassDescriptor.colorAttachments[0].view = context
.getCurrentTexture()
.createView();
const debugViewPass = commandEncoder.beginRenderPass(
textureQuadPassDescriptor
);
debugViewPass.setPipeline(primitivesDebugViewPipeline);
debugViewPass.setBindGroup(0, primitiveTextureBindGroup);
debugViewPass.draw(6);
debugViewPass.end();
}
}
{
// Picking pass. Executes a single instance of a compute shader that loads
// the primitive index at the pointer coordinates from the primitive index
// texture written in the forward pass. The selected primitive index is
// saved in the frameUniformBuffer and used for highlighting on the next
// render. This means that the highlighted primitive is always a frame behind.
const pickPass = commandEncoder.beginComputePass();
pickPass.setPipeline(pickPipeline);
pickPass.setBindGroup(0, pickBindGroup);
pickPass.dispatchWorkgroups(1);
pickPass.end();
}
device.queue.submit([commandEncoder.finish()]);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);