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
335 lines (297 loc) · 8.81 KB
/
main.ts
File metadata and controls
335 lines (297 loc) · 8.81 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
import { GUI } from 'dat.gui';
import { mat4 } from 'wgpu-matrix';
import solidColorLitWGSL from './solidColorLit.wgsl';
import { quitIfWebGPUNotAvailableOrMissingFeatures } from '../util';
const settings = {
animate: true,
};
const gui = new GUI();
gui.add(settings, 'animate');
type TypedArrayView =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array;
export type TypedArrayConstructor =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor;
const info = document.querySelector('#info');
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
const device = await adapter?.requestDevice();
quitIfWebGPUNotAvailableOrMissingFeatures(adapter, device);
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const context = canvas.getContext('webgpu');
const devicePixelRatio = window.devicePixelRatio;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat,
});
const depthFormat = 'depth24plus';
const module = device.createShaderModule({
code: solidColorLitWGSL,
});
const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
module,
buffers: [
{
arrayStride: 6 * 4, // 3x2 floats, 4 bytes each
attributes: [
{ shaderLocation: 0, offset: 0, format: 'float32x3' }, // position
{ shaderLocation: 1, offset: 12, format: 'float32x3' }, // normal
],
},
],
},
fragment: {
module,
targets: [{ format: presentationFormat }],
},
primitive: {
topology: 'triangle-list',
cullMode: 'back',
},
depthStencil: {
depthWriteEnabled: true,
depthCompare: 'less',
format: depthFormat,
},
});
// prettier-ignore
const cubePositions = [
{ position: [-1, 0, 0], id: '🟥', color: [1, 0, 0, 1] },
{ position: [ 1, 0, 0], id: '🟨', color: [1, 1, 0, 1] },
{ position: [ 0, -1, 0], id: '🟩', color: [0, 0.5, 0, 1] },
{ position: [ 0, 1, 0], id: '🟧', color: [1, 0.6, 0, 1] },
{ position: [ 0, 0, -1], id: '🟦', color: [0, 0, 1, 1] },
{ position: [ 0, 0, 1], id: '🟪', color: [0.5, 0, 0.5, 1] },
];
const objectInfos = cubePositions.map(({ position, id, color }) => {
const uniformBufferSize = (2 * 16 + 3 + 1 + 4) * 4;
const uniformBuffer = device.createBuffer({
size: uniformBufferSize,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const uniformValues = new Float32Array(uniformBufferSize / 4);
const worldViewProjection = uniformValues.subarray(0, 16);
const worldInverseTranspose = uniformValues.subarray(16, 32);
const colorValue = uniformValues.subarray(32, 36);
colorValue.set(color);
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
});
return {
id,
position: position.map((v) => v * 10),
bindGroup,
uniformBuffer,
uniformValues,
worldInverseTranspose,
worldViewProjection,
};
});
const querySet = device.createQuerySet({
type: 'occlusion',
count: objectInfos.length,
});
const resolveBuf = device.createBuffer({
label: 'resolveBuffer',
// Query results are 64bit unsigned integers.
size: objectInfos.length * BigUint64Array.BYTES_PER_ELEMENT,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
const resultBuf = device.createBuffer({
label: 'resultBuffer',
size: resolveBuf.size,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});
function createBufferWithData(
device: GPUDevice,
data: TypedArrayView,
usage: GPUBufferUsageFlags,
label: string
) {
const buffer = device.createBuffer({
label,
size: data.byteLength,
usage,
mappedAtCreation: true,
});
const Ctor = data.constructor as TypedArrayConstructor;
const dst = new Ctor(buffer.getMappedRange());
dst.set(data);
buffer.unmap();
return buffer;
}
// prettier-ignore
const vertexData = new Float32Array([
// position normal
1, 1, -1, 1, 0, 0,
1, 1, 1, 1, 0, 0,
1, -1, 1, 1, 0, 0,
1, -1, -1, 1, 0, 0,
-1, 1, 1, -1, 0, 0,
-1, 1, -1, -1, 0, 0,
-1, -1, -1, -1, 0, 0,
-1, -1, 1, -1, 0, 0,
-1, 1, 1, 0, 1, 0,
1, 1, 1, 0, 1, 0,
1, 1, -1, 0, 1, 0,
-1, 1, -1, 0, 1, 0,
-1, -1, -1, 0, -1, 0,
1, -1, -1, 0, -1, 0,
1, -1, 1, 0, -1, 0,
-1, -1, 1, 0, -1, 0,
1, 1, 1, 0, 0, 1,
-1, 1, 1, 0, 0, 1,
-1, -1, 1, 0, 0, 1,
1, -1, 1, 0, 0, 1,
-1, 1, -1, 0, 0, -1,
1, 1, -1, 0, 0, -1,
1, -1, -1, 0, 0, -1,
-1, -1, -1, 0, 0, -1,
]);
// prettier-ignore
const indices = new Uint16Array([
0, 1, 2, 0, 2, 3, // +x face
4, 5, 6, 4, 6, 7, // -x face
8, 9, 10, 8, 10, 11, // +y face
12, 13, 14, 12, 14, 15, // -y face
16, 17, 18, 16, 18, 19, // +z face
20, 21, 22, 20, 22, 23, // -z face
]);
const vertexBuf = createBufferWithData(
device,
vertexData,
GPUBufferUsage.VERTEX,
'vertexBuffer'
);
const indicesBuf = createBufferWithData(
device,
indices,
GPUBufferUsage.INDEX,
'indexBuffer'
);
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
clearValue: [0.5, 0.5, 0.5, 1.0],
loadOp: 'clear',
storeOp: 'store',
},
],
depthStencilAttachment: {
view: undefined, // Assigned later
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
occlusionQuerySet: querySet,
};
const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
const lerpV = (a: number[], b: number[], t: number) =>
a.map((v, i) => lerp(v, b[i], t));
const pingPongSine = (t: number) => Math.sin(t * Math.PI * 2) * 0.5 + 0.5;
let depthTexture: GPUTexture | undefined;
let time = 0;
let then = 0;
function render(now: number) {
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
if (settings.animate) {
time += deltaTime;
}
const projection = mat4.perspective(
(30 * Math.PI) / 180,
canvas.clientWidth / canvas.clientHeight,
0.5,
100
);
const m = mat4.identity();
mat4.rotateX(m, time, m);
mat4.rotateY(m, time * 0.7, m);
mat4.translate(m, lerpV([0, 0, 5], [0, 0, 40], pingPongSine(time * 0.2)), m);
const view = mat4.inverse(m);
const viewProjection = mat4.multiply(projection, view);
const canvasTexture = context.getCurrentTexture();
if (
!depthTexture ||
depthTexture.width !== canvasTexture.width ||
depthTexture.height !== canvasTexture.height
) {
depthTexture?.destroy();
depthTexture = device.createTexture({
size: canvasTexture, // canvasTexture has width, height, and depthOrArrayLayers properties
format: depthFormat,
usage: GPUTextureUsage.RENDER_ATTACHMENT,
});
}
const colorTexture = context.getCurrentTexture();
renderPassDescriptor.colorAttachments[0].view = colorTexture.createView();
renderPassDescriptor.depthStencilAttachment.view = depthTexture.createView();
const encoder = device.createCommandEncoder();
const pass = encoder.beginRenderPass(renderPassDescriptor);
pass.setPipeline(pipeline);
pass.setVertexBuffer(0, vertexBuf);
pass.setIndexBuffer(indicesBuf, 'uint16');
objectInfos.forEach(
(
{
bindGroup,
uniformBuffer,
uniformValues,
worldViewProjection,
worldInverseTranspose,
position,
},
i
) => {
const world = mat4.translation(position);
mat4.transpose(mat4.inverse(world), worldInverseTranspose);
mat4.multiply(viewProjection, world, worldViewProjection);
device.queue.writeBuffer(uniformBuffer, 0, uniformValues);
pass.setBindGroup(0, bindGroup);
pass.beginOcclusionQuery(i);
pass.drawIndexed(indices.length);
pass.endOcclusionQuery();
}
);
pass.end();
encoder.resolveQuerySet(querySet, 0, objectInfos.length, resolveBuf, 0);
if (resultBuf.mapState === 'unmapped') {
encoder.copyBufferToBuffer(resolveBuf, resultBuf);
}
device.queue.submit([encoder.finish()]);
if (resultBuf.mapState === 'unmapped') {
resultBuf.mapAsync(GPUMapMode.READ).then(() => {
const results = new BigUint64Array(resultBuf.getMappedRange());
const visible = objectInfos
.filter((_, i) => results[i])
.map(({ id }) => id)
.join('');
info.textContent = `visible: ${visible}`;
resultBuf.unmap();
});
}
requestAnimationFrame(render);
}
requestAnimationFrame(render);