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
416 lines (365 loc) · 11.7 KB
/
main.ts
File metadata and controls
416 lines (365 loc) · 11.7 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
import { mat4, vec3 } from 'wgpu-matrix';
import { GUI } from 'dat.gui';
import { createSphereMesh, SphereLayout } from '../../meshes/sphere';
import Stats from 'stats.js';
import meshWGSL from './mesh.wgsl';
import { quitIfWebGPUNotAvailable } from '../util';
interface Renderable {
vertices: GPUBuffer;
indices: GPUBuffer;
indexCount: number;
bindGroup?: GPUBindGroup;
}
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
const device = await adapter?.requestDevice();
quitIfWebGPUNotAvailable(adapter, device);
const settings = {
useRenderBundles: true,
asteroidCount: 5000,
};
const gui = new GUI();
gui.add(settings, 'useRenderBundles');
gui.add(settings, 'asteroidCount', 1000, 10000, 1000).onChange(() => {
// If the content of the scene changes the render bundle must be recreated.
ensureEnoughAsteroids();
updateRenderBundle();
});
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 shaderModule = device.createShaderModule({
code: meshWGSL,
});
const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {
module: shaderModule,
buffers: [
{
arrayStride: SphereLayout.vertexStride,
attributes: [
{
// position
shaderLocation: 0,
offset: SphereLayout.positionsOffset,
format: 'float32x3',
},
{
// normal
shaderLocation: 1,
offset: SphereLayout.normalOffset,
format: 'float32x3',
},
{
// uv
shaderLocation: 2,
offset: SphereLayout.uvOffset,
format: 'float32x2',
},
],
},
],
},
fragment: {
module: shaderModule,
targets: [
{
format: presentationFormat,
},
],
},
primitive: {
topology: 'triangle-list',
// Backface culling since the sphere is solid piece of geometry.
// Faces pointing away from the camera will be occluded by faces
// pointing toward the camera.
cullMode: 'back',
},
// Enable depth testing so that the fragment closest to the camera
// is rendered in front.
depthStencil: {
depthWriteEnabled: true,
depthCompare: 'less',
format: 'depth24plus',
},
});
const depthTexture = device.createTexture({
size: [canvas.width, canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT,
});
const uniformBufferSize = 4 * 16; // 4x4 matrix
const uniformBuffer = device.createBuffer({
size: uniformBufferSize,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
// Fetch the images and upload them into a GPUTexture.
let planetTexture: GPUTexture;
{
const response = await fetch('../../assets/img/saturn.jpg');
const imageBitmap = await createImageBitmap(await response.blob());
planetTexture = device.createTexture({
size: [imageBitmap.width, imageBitmap.height, 1],
format: 'rgba8unorm',
usage:
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.COPY_DST |
GPUTextureUsage.RENDER_ATTACHMENT,
});
device.queue.copyExternalImageToTexture(
{ source: imageBitmap },
{ texture: planetTexture },
[imageBitmap.width, imageBitmap.height]
);
}
let moonTexture: GPUTexture;
{
const response = await fetch('../../assets/img/moon.jpg');
const imageBitmap = await createImageBitmap(await response.blob());
moonTexture = device.createTexture({
size: [imageBitmap.width, imageBitmap.height, 1],
format: 'rgba8unorm',
usage:
GPUTextureUsage.TEXTURE_BINDING |
GPUTextureUsage.COPY_DST |
GPUTextureUsage.RENDER_ATTACHMENT,
});
device.queue.copyExternalImageToTexture(
{ source: imageBitmap },
{ texture: moonTexture },
[imageBitmap.width, imageBitmap.height]
);
}
const sampler = device.createSampler({
magFilter: 'linear',
minFilter: 'linear',
});
// Helper functions to create the required meshes and bind groups for each sphere.
function createSphereRenderable(
radius: number,
widthSegments = 32,
heightSegments = 16,
randomness = 0
): Renderable {
const sphereMesh = createSphereMesh(
radius,
widthSegments,
heightSegments,
randomness
);
// Create a vertex buffer from the sphere data.
const vertices = device.createBuffer({
size: sphereMesh.vertices.byteLength,
usage: GPUBufferUsage.VERTEX,
mappedAtCreation: true,
});
new Float32Array(vertices.getMappedRange()).set(sphereMesh.vertices);
vertices.unmap();
const indices = device.createBuffer({
size: sphereMesh.indices.byteLength,
usage: GPUBufferUsage.INDEX,
mappedAtCreation: true,
});
new Uint16Array(indices.getMappedRange()).set(sphereMesh.indices);
indices.unmap();
return {
vertices,
indices,
indexCount: sphereMesh.indices.length,
};
}
function createSphereBindGroup(
texture: GPUTexture,
transform: Float32Array
): GPUBindGroup {
const uniformBufferSize = 4 * 16; // 4x4 matrix
const uniformBuffer = device.createBuffer({
size: uniformBufferSize,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
mappedAtCreation: true,
});
new Float32Array(uniformBuffer.getMappedRange()).set(transform);
uniformBuffer.unmap();
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(1),
entries: [
{
binding: 0,
resource: {
buffer: uniformBuffer,
},
},
{
binding: 1,
resource: sampler,
},
{
binding: 2,
resource: texture.createView(),
},
],
});
return bindGroup;
}
const transform = mat4.create();
mat4.identity(transform);
// Create one large central planet surrounded by a large ring of asteroids
const planet = createSphereRenderable(1.0);
planet.bindGroup = createSphereBindGroup(planetTexture, transform);
const asteroids = [
createSphereRenderable(0.01, 8, 6, 0.15),
createSphereRenderable(0.013, 8, 6, 0.15),
createSphereRenderable(0.017, 8, 6, 0.15),
createSphereRenderable(0.02, 8, 6, 0.15),
createSphereRenderable(0.03, 16, 8, 0.15),
];
const renderables = [planet];
function ensureEnoughAsteroids() {
for (let i = renderables.length; i <= settings.asteroidCount; ++i) {
// Place copies of the asteroid in a ring.
const radius = Math.random() * 1.7 + 1.25;
const angle = Math.random() * Math.PI * 2;
const x = Math.sin(angle) * radius;
const y = (Math.random() - 0.5) * 0.015;
const z = Math.cos(angle) * radius;
mat4.identity(transform);
mat4.translate(transform, [x, y, z], transform);
mat4.rotateX(transform, Math.random() * Math.PI, transform);
mat4.rotateY(transform, Math.random() * Math.PI, transform);
renderables.push({
...asteroids[i % asteroids.length],
bindGroup: createSphereBindGroup(moonTexture, transform),
});
}
}
ensureEnoughAsteroids();
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
clearValue: [0, 0, 0, 1],
loadOp: 'clear',
storeOp: 'store',
},
],
depthStencilAttachment: {
view: depthTexture.createView(),
depthClearValue: 1.0,
depthLoadOp: 'clear',
depthStoreOp: 'store',
},
};
const aspect = canvas.width / canvas.height;
const projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 100.0);
const modelViewProjectionMatrix = mat4.create();
const frameBindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{
binding: 0,
resource: {
buffer: uniformBuffer,
},
},
],
});
function getTransformationMatrix() {
const viewMatrix = mat4.identity();
mat4.translate(viewMatrix, vec3.fromValues(0, 0, -4), viewMatrix);
const now = Date.now() / 1000;
// Tilt the view matrix so the planet looks like it's off-axis.
mat4.rotateZ(viewMatrix, Math.PI * 0.1, viewMatrix);
mat4.rotateX(viewMatrix, Math.PI * 0.1, viewMatrix);
// Rotate the view matrix slowly so the planet appears to spin.
mat4.rotateY(viewMatrix, now * 0.05, viewMatrix);
mat4.multiply(projectionMatrix, viewMatrix, modelViewProjectionMatrix);
return modelViewProjectionMatrix;
}
// Render bundles function as partial, limited render passes, so we can use the
// same code both to render the scene normally and to build the render bundle.
function renderScene(
passEncoder: GPURenderPassEncoder | GPURenderBundleEncoder
) {
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, frameBindGroup);
// Loop through every renderable object and draw them individually.
// (Because many of these meshes are repeated, with only the transforms
// differing, instancing would be highly effective here. This sample
// intentionally avoids using instancing in order to emulate a more complex
// scene, which helps demonstrate the potential time savings a render bundle
// can provide.)
let count = 0;
for (const renderable of renderables) {
passEncoder.setBindGroup(1, renderable.bindGroup);
passEncoder.setVertexBuffer(0, renderable.vertices);
passEncoder.setIndexBuffer(renderable.indices, 'uint16');
passEncoder.drawIndexed(renderable.indexCount);
if (++count > settings.asteroidCount) {
break;
}
}
}
// The render bundle can be encoded once and re-used as many times as needed.
// Because it encodes all of the commands needed to render at the GPU level,
// those commands will not need to execute the associated JavaScript code upon
// execution or be re-validated, which can represent a significant time savings.
//
// However, because render bundles are immutable once created, they are only
// appropriate for rendering content where the same commands will be executed
// every time, with the only changes being the contents of the buffers and
// textures used. Cases where the executed commands differ from frame-to-frame,
// such as when using frustrum or occlusion culling, will not benefit from
// using render bundles as much.
let renderBundle;
function updateRenderBundle() {
const renderBundleEncoder = device.createRenderBundleEncoder({
colorFormats: [presentationFormat],
depthStencilFormat: 'depth24plus',
});
renderScene(renderBundleEncoder);
renderBundle = renderBundleEncoder.finish();
}
updateRenderBundle();
const stats = new Stats();
stats.showPanel(1); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
function frame() {
stats.begin();
const transformationMatrix = getTransformationMatrix();
device.queue.writeBuffer(
uniformBuffer,
0,
transformationMatrix.buffer,
transformationMatrix.byteOffset,
transformationMatrix.byteLength
);
renderPassDescriptor.colorAttachments[0].view = context
.getCurrentTexture()
.createView();
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
if (settings.useRenderBundles) {
// Executing a bundle is equivalent to calling all of the commands encoded
// in the render bundle as part of the current render pass.
passEncoder.executeBundles([renderBundle]);
} else {
// Alternatively, the same render commands can be encoded manually, which
// can take longer since each command needs to be interpreted by the
// JavaScript virtual machine and re-validated each time.
renderScene(passEncoder);
}
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
stats.end();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);