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
636 lines (568 loc) · 16.6 KB
/
main.ts
File metadata and controls
636 lines (568 loc) · 16.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import { mat4, vec3 } from 'wgpu-matrix';
import { GUI } from 'dat.gui';
import {
quitIfWebGPUNotAvailableOrMissingFeatures,
quitIfLimitLessThan,
} from '../util';
import { mesh } from '../../meshes/teapot';
import opaqueWGSL from './opaque.wgsl';
import translucentWGSL from './translucent.wgsl';
import compositeWGSL from './composite.wgsl';
function roundUp(n: number, k: number): number {
return Math.ceil(n / k) * k;
}
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
const limits: Record<string, GPUSize32> = {};
quitIfLimitLessThan(adapter, 'maxStorageBuffersInFragmentStage', 2, limits);
const device = await adapter?.requestDevice({
requiredLimits: limits,
});
quitIfWebGPUNotAvailableOrMissingFeatures(adapter, device);
const context = canvas.getContext('webgpu');
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat,
alphaMode: 'opaque',
});
const params = new URLSearchParams(window.location.search);
const settings = {
memoryStrategy: params.get('memoryStrategy') || 'multipass',
};
// Create the model vertex buffer
const vertexBuffer = device.createBuffer({
size: 3 * mesh.positions.length * Float32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.VERTEX,
mappedAtCreation: true,
label: 'vertexBuffer',
});
{
const mapping = new Float32Array(vertexBuffer.getMappedRange());
for (let i = 0; i < mesh.positions.length; ++i) {
mapping.set(mesh.positions[i], 3 * i);
}
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,
label: 'indexBuffer',
});
{
const mapping = new Uint16Array(indexBuffer.getMappedRange());
for (let i = 0; i < mesh.triangles.length; ++i) {
mapping.set(mesh.triangles[i], 3 * i);
}
indexBuffer.unmap();
}
// Uniforms contains:
// * modelViewProjectionMatrix: mat4x4f
// * maxStorableFragments: u32
// * targetWidth: u32
const uniformsSize = roundUp(
16 * Float32Array.BYTES_PER_ELEMENT + 2 * Uint32Array.BYTES_PER_ELEMENT,
16
);
const uniformBuffer = device.createBuffer({
size: uniformsSize,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
label: 'uniformBuffer',
});
const opaqueModule = device.createShaderModule({
code: opaqueWGSL,
label: 'opaqueModule',
});
const opaquePipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
module: opaqueModule,
buffers: [
{
arrayStride: 3 * Float32Array.BYTES_PER_ELEMENT,
attributes: [
{
// position
format: 'float32x3',
offset: 0,
shaderLocation: 0,
},
],
},
],
},
fragment: {
module: opaqueModule,
targets: [
{
format: presentationFormat,
},
],
},
primitive: {
topology: 'triangle-list',
},
depthStencil: {
depthWriteEnabled: true,
depthCompare: 'less',
format: 'depth24plus',
},
label: 'opaquePipeline',
});
const opaquePassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined,
clearValue: [0, 0, 0, 1.0],
loadOp: 'clear',
storeOp: 'store',
},
],
depthStencilAttachment: {
view: undefined,
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
label: 'opaquePassDescriptor',
};
const opaqueBindGroup = device.createBindGroup({
layout: opaquePipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: {
buffer: uniformBuffer,
size: 16 * Float32Array.BYTES_PER_ELEMENT,
label: 'modelViewProjection',
},
},
],
label: 'opaquePipeline',
});
const translucentModule = device.createShaderModule({
code: translucentWGSL,
label: 'translucentModule',
});
const translucentBindGroupLayout = device.createBindGroupLayout({
label: 'translucentBindGroupLayout',
entries: [
{
binding: 0,
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
buffer: {
type: 'uniform',
},
},
{
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'storage',
},
},
{
binding: 2,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'storage',
},
},
{
binding: 3,
visibility: GPUShaderStage.FRAGMENT,
texture: { sampleType: 'unfilterable-float' },
},
{
binding: 4,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'uniform',
hasDynamicOffset: true,
},
},
],
});
const translucentPipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [translucentBindGroupLayout],
label: 'translucentPipelineLayout',
}),
vertex: {
module: translucentModule,
buffers: [
{
arrayStride: 3 * Float32Array.BYTES_PER_ELEMENT,
attributes: [
{
format: 'float32x3',
offset: 0,
shaderLocation: 0,
},
],
},
],
},
fragment: {
module: translucentModule,
targets: [
{
format: presentationFormat,
writeMask: 0x0,
},
],
},
primitive: {
topology: 'triangle-list',
},
label: 'translucentPipeline',
});
const translucentPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
loadOp: 'load',
storeOp: 'store',
view: undefined,
},
],
label: 'translucentPassDescriptor',
};
const compositeModule = device.createShaderModule({
code: compositeWGSL,
label: 'compositeModule',
});
const compositeBindGroupLayout = device.createBindGroupLayout({
label: 'compositeBindGroupLayout',
entries: [
{
binding: 0,
visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
buffer: {
type: 'uniform',
},
},
{
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'storage',
},
},
{
binding: 2,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'storage',
},
},
{
binding: 3,
visibility: GPUShaderStage.FRAGMENT,
buffer: {
type: 'uniform',
hasDynamicOffset: true,
},
},
],
});
const compositePipeline = device.createRenderPipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [compositeBindGroupLayout],
label: 'compositePipelineLayout',
}),
vertex: {
module: compositeModule,
},
fragment: {
module: compositeModule,
targets: [
{
format: presentationFormat,
blend: {
color: {
srcFactor: 'one',
operation: 'add',
dstFactor: 'one-minus-src-alpha',
},
alpha: {},
},
},
],
},
primitive: {
topology: 'triangle-list',
},
label: 'compositePipeline',
});
const compositePassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined,
loadOp: 'load',
storeOp: 'store',
},
],
label: 'compositePassDescriptor',
};
const configure = () => {
let devicePixelRatio = window.devicePixelRatio;
// The default maximum storage buffer binding size is 128Mib. The amount
// of memory we need to store transparent fragments depends on the size
// of the canvas and the average number of layers per fragment we want to
// support. When the devicePixelRatio is 1, we know that 128Mib is enough
// to store 4 layers per pixel at 600x600. However, when the device pixel
// ratio is high enough we will exceed this limit.
//
// We provide 2 choices of mitigations to this issue:
// 1) Clamp the device pixel ratio to a value which we know will not break
// the limit. The tradeoff here is that the canvas resolution will not
// match the native resolution and therefore may have a reduction in
// quality.
// 2) Break the frame into a series of horizontal slices using the scissor
// functionality and process a single slice at a time. This limits memory
// usage because we only need enough memory to process the dimensions
// of the slice. The tradeoff is the performance reduction due to multiple
// passes.
if (settings.memoryStrategy === 'clamp-pixel-ratio') {
devicePixelRatio = Math.min(window.devicePixelRatio, 3);
}
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const depthTexture = device.createTexture({
size: [canvas.width, canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
label: 'depthTexture',
});
const depthTextureView = depthTexture.createView({
label: 'depthTextureView',
});
// Determines how much memory is allocated to store linked-list elements
const averageLayersPerFragment = 4;
// Each element stores
// * color : vec4f
// * depth : f32
// * index of next element in the list : u32
const linkedListElementSize =
5 * Float32Array.BYTES_PER_ELEMENT + 1 * Uint32Array.BYTES_PER_ELEMENT;
// We want to keep the linked-list buffer size under the maxStorageBufferBindingSize.
// Split the frame into enough slices to meet that constraint.
const bytesPerline =
canvas.width * averageLayersPerFragment * linkedListElementSize;
const maxLinesSupported = Math.floor(
device.limits.maxStorageBufferBindingSize / bytesPerline
);
const numSlices = Math.ceil(canvas.height / maxLinesSupported);
const sliceHeight = Math.ceil(canvas.height / numSlices);
const linkedListBufferSize = sliceHeight * bytesPerline;
const linkedListBuffer = device.createBuffer({
size: linkedListBufferSize,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
label: 'linkedListBuffer',
});
// To slice up the frame we need to pass the starting fragment y position of the slice.
// We do this using a uniform buffer with a dynamic offset.
const sliceInfoBuffer = device.createBuffer({
size: numSlices * device.limits.minUniformBufferOffsetAlignment,
usage: GPUBufferUsage.UNIFORM,
mappedAtCreation: true,
label: 'sliceInfoBuffer',
});
{
const mapping = new Int32Array(sliceInfoBuffer.getMappedRange());
// This assumes minUniformBufferOffsetAlignment is a multiple of 4
const stride =
device.limits.minUniformBufferOffsetAlignment /
Int32Array.BYTES_PER_ELEMENT;
for (let i = 0; i < numSlices; ++i) {
mapping[i * stride] = i * sliceHeight;
}
sliceInfoBuffer.unmap();
}
// `Heads` struct contains the start index of the linked-list of translucent fragments
// for a given pixel.
// * numFragments : u32
// * data : array<u32>
const headsBuffer = device.createBuffer({
size: (1 + canvas.width * sliceHeight) * Uint32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
label: 'headsBuffer',
});
const headsInitBuffer = device.createBuffer({
size: (1 + canvas.width * sliceHeight) * Uint32Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.COPY_SRC,
mappedAtCreation: true,
label: 'headsInitBuffer',
});
{
const buffer = new Uint32Array(headsInitBuffer.getMappedRange());
for (let i = 0; i < buffer.length; ++i) {
buffer[i] = 0xffffffff;
}
headsInitBuffer.unmap();
}
const translucentBindGroup = device.createBindGroup({
layout: translucentBindGroupLayout,
entries: [
{
binding: 0,
resource: uniformBuffer,
},
{
binding: 1,
resource: headsBuffer,
},
{
binding: 2,
resource: linkedListBuffer,
},
{
binding: 3,
resource: depthTextureView,
},
{
binding: 4,
resource: {
buffer: sliceInfoBuffer,
size: device.limits.minUniformBufferOffsetAlignment,
},
},
],
label: 'translucentBindGroup',
});
const compositeBindGroup = device.createBindGroup({
layout: compositePipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: uniformBuffer,
},
{
binding: 1,
resource: headsBuffer,
},
{
binding: 2,
resource: linkedListBuffer,
},
{
binding: 3,
resource: {
buffer: sliceInfoBuffer,
size: device.limits.minUniformBufferOffsetAlignment,
},
},
],
});
opaquePassDescriptor.depthStencilAttachment.view = depthTextureView;
// Rotates the camera around the origin based on time.
function getCameraViewProjMatrix() {
const aspect = canvas.width / canvas.height;
const projectionMatrix = mat4.perspective(
(2 * Math.PI) / 5,
aspect,
1,
2000.0
);
const upVector = [0, 1, 0];
const origin = [0, 0, 0];
const eyePosition = [0, 5, -100];
const rad = Math.PI * (Date.now() / 5000);
const rotation = mat4.rotateY(mat4.translation(origin), rad);
vec3.transformMat4(eyePosition, rotation, eyePosition);
const viewMatrix = mat4.lookAt(eyePosition, origin, upVector);
const viewProjMatrix = mat4.multiply(projectionMatrix, viewMatrix);
return viewProjMatrix;
}
return function doDraw() {
// update the uniform buffer
{
const buffer = new ArrayBuffer(uniformBuffer.size);
new Float32Array(buffer).set(getCameraViewProjMatrix());
new Uint32Array(buffer, 16 * Float32Array.BYTES_PER_ELEMENT).set([
averageLayersPerFragment * canvas.width * sliceHeight,
canvas.width,
]);
device.queue.writeBuffer(uniformBuffer, 0, buffer);
}
const commandEncoder = device.createCommandEncoder();
const textureView = context.getCurrentTexture().createView();
// Draw the opaque objects
opaquePassDescriptor.colorAttachments[0].view = textureView;
const opaquePassEncoder =
commandEncoder.beginRenderPass(opaquePassDescriptor);
opaquePassEncoder.setPipeline(opaquePipeline);
opaquePassEncoder.setBindGroup(0, opaqueBindGroup);
opaquePassEncoder.setVertexBuffer(0, vertexBuffer);
opaquePassEncoder.setIndexBuffer(indexBuffer, 'uint16');
opaquePassEncoder.drawIndexed(mesh.triangles.length * 3, 8);
opaquePassEncoder.end();
for (let slice = 0; slice < numSlices; ++slice) {
// initialize the heads buffer
commandEncoder.copyBufferToBuffer(headsInitBuffer, headsBuffer);
const scissorX = 0;
const scissorY = slice * sliceHeight;
const scissorWidth = canvas.width;
const scissorHeight =
Math.min((slice + 1) * sliceHeight, canvas.height) -
slice * sliceHeight;
// Draw the translucent objects
translucentPassDescriptor.colorAttachments[0].view = textureView;
const translucentPassEncoder = commandEncoder.beginRenderPass(
translucentPassDescriptor
);
// Set the scissor to only process a horizontal slice of the frame
translucentPassEncoder.setScissorRect(
scissorX,
scissorY,
scissorWidth,
scissorHeight
);
translucentPassEncoder.setPipeline(translucentPipeline);
translucentPassEncoder.setBindGroup(0, translucentBindGroup, [
slice * device.limits.minUniformBufferOffsetAlignment,
]);
translucentPassEncoder.setVertexBuffer(0, vertexBuffer);
translucentPassEncoder.setIndexBuffer(indexBuffer, 'uint16');
translucentPassEncoder.drawIndexed(mesh.triangles.length * 3, 8);
translucentPassEncoder.end();
// Composite the opaque and translucent objects
compositePassDescriptor.colorAttachments[0].view = textureView;
const compositePassEncoder = commandEncoder.beginRenderPass(
compositePassDescriptor
);
// Set the scissor to only process a horizontal slice of the frame
compositePassEncoder.setScissorRect(
scissorX,
scissorY,
scissorWidth,
scissorHeight
);
compositePassEncoder.setPipeline(compositePipeline);
compositePassEncoder.setBindGroup(0, compositeBindGroup, [
slice * device.limits.minUniformBufferOffsetAlignment,
]);
compositePassEncoder.draw(6);
compositePassEncoder.end();
}
device.queue.submit([commandEncoder.finish()]);
};
};
let doDraw = configure();
const updateSettings = () => {
doDraw = configure();
};
const gui = new GUI();
gui
.add(settings, 'memoryStrategy', ['multipass', 'clamp-pixel-ratio'])
.onFinishChange(updateSettings);
function frame() {
doDraw();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);