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
567 lines (513 loc) · 16.2 KB
/
main.ts
File metadata and controls
567 lines (513 loc) · 16.2 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
import { GUI } from 'dat.gui';
import { convertGLBToJSONAndBinary, GLTFSkin } from './glbUtils';
import gltfWGSL from './gltf.wgsl';
import gridWGSL from './grid.wgsl';
import { Mat4, mat4, quat } from 'wgpu-matrix';
import { createBindGroupCluster } from '../bitonicSort/utils';
import {
createSkinnedGridBuffers,
createSkinnedGridRenderPipeline,
} from './gridUtils';
import { gridIndices } from './gridData';
import {
quitIfWebGPUNotAvailableOrMissingFeatures,
quitIfLimitLessThan,
} from '../util';
const MAT4X4_BYTES = 64;
interface BoneObject {
transforms: Mat4[];
bindPoses: Mat4[];
bindPosesInv: Mat4[];
}
enum RenderMode {
NORMAL,
JOINTS,
WEIGHTS,
}
enum SkinMode {
ON,
OFF,
}
/*
// Copied from toji/gl-matrix
const getRotation = (mat: Mat4): Quat => {
// Initialize our output quaternion
const out = [0, 0, 0, 0];
// Extract the scaling factor from the final matrix transformation
// to normalize our rotation;
const scaling = mat4.getScaling(mat);
const is1 = 1 / scaling[0];
const is2 = 1 / scaling[1];
const is3 = 1 / scaling[2];
// Scale the matrix elements by the scaling factors
const sm11 = mat[0] * is1;
const sm12 = mat[1] * is2;
const sm13 = mat[2] * is3;
const sm21 = mat[4] * is1;
const sm22 = mat[5] * is2;
const sm23 = mat[6] * is3;
const sm31 = mat[8] * is1;
const sm32 = mat[9] * is2;
const sm33 = mat[10] * is3;
// The trace of a square matrix is the sum of its diagonal entries
// While the matrix trace has many interesting mathematical properties,
// the primary purpose of the trace is to assess the characteristics of the rotation.
const trace = sm11 + sm22 + sm33;
let S = 0;
// If all matrix elements contribute equally to the rotation.
if (trace > 0) {
S = Math.sqrt(trace + 1.0) * 2;
out[3] = 0.25 * S;
out[0] = (sm23 - sm32) / S;
out[1] = (sm31 - sm13) / S;
out[2] = (sm12 - sm21) / S;
// If the rotation is primarily around the x-axis
} else if (sm11 > sm22 && sm11 > sm33) {
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
out[3] = (sm23 - sm32) / S;
out[0] = 0.25 * S;
out[1] = (sm12 + sm21) / S;
out[2] = (sm31 + sm13) / S;
// If rotation is primarily around the y-axis
} else if (sm22 > sm33) {
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
out[3] = (sm31 - sm13) / S;
out[0] = (sm12 + sm21) / S;
out[1] = 0.25 * S;
out[2] = (sm23 + sm32) / S;
// If the rotation is primarily around the z-axis
} else {
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
out[3] = (sm12 - sm21) / S;
out[0] = (sm31 + sm13) / S;
out[1] = (sm23 + sm32) / S;
out[2] = 0.25 * S;
}
return out;
};
*/
//Normal setup
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
const limits: Record<string, GPUSize32> = {};
quitIfLimitLessThan(adapter, 'maxStorageBuffersInVertexStage', 2, limits);
const device = await adapter?.requestDevice({
requiredLimits: limits,
});
quitIfWebGPUNotAvailableOrMissingFeatures(adapter, device);
const context = canvas.getContext('webgpu');
const devicePixelRatio = window.devicePixelRatio || 1;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context.configure({
device,
format: presentationFormat,
});
const settings = {
cameraX: 0,
cameraY: -5.1,
cameraZ: -14.6,
objectScale: 1,
angle: 0.2,
speed: 50,
object: 'Whale',
renderMode: 'NORMAL',
skinMode: 'ON',
};
const gui = new GUI();
// Determine whether we want to render our whale or our skinned grid
gui.add(settings, 'object', ['Whale', 'Skinned Grid']).onChange(() => {
if (settings.object === 'Skinned Grid') {
settings.cameraX = -10;
settings.cameraY = 0;
settings.objectScale = 1.27;
} else {
if (settings.skinMode === 'OFF') {
settings.cameraX = 0;
settings.cameraY = 0;
settings.cameraZ = -11;
} else {
settings.cameraX = 0;
settings.cameraY = -5.1;
settings.cameraZ = -14.6;
}
}
});
// Output the mesh normals, its joints, or the weights that influence the movement of the joints
gui
.add(settings, 'renderMode', ['NORMAL', 'JOINTS', 'WEIGHTS'])
.onChange(() => {
device.queue.writeBuffer(
generalUniformsBuffer,
0,
new Uint32Array([RenderMode[settings.renderMode]])
);
});
// Determine whether the mesh is static or whether skinning is activated
gui.add(settings, 'skinMode', ['ON', 'OFF']).onChange(() => {
if (settings.object === 'Whale') {
if (settings.skinMode === 'OFF') {
settings.cameraX = 0;
settings.cameraY = 0;
settings.cameraZ = -11;
} else {
settings.cameraX = 0;
settings.cameraY = -5.1;
settings.cameraZ = -14.6;
}
}
device.queue.writeBuffer(
generalUniformsBuffer,
4,
new Uint32Array([SkinMode[settings.skinMode]])
);
});
const animFolder = gui.addFolder('Animation Settings');
animFolder.add(settings, 'angle', 0.05, 0.5).step(0.05);
animFolder.add(settings, 'speed', 10, 100).step(10);
const depthTexture = device.createTexture({
size: [canvas.width, canvas.height],
format: 'depth24plus',
usage: GPUTextureUsage.RENDER_ATTACHMENT,
});
const cameraBuffer = device.createBuffer({
size: MAT4X4_BYTES * 3,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const cameraBGCluster = createBindGroupCluster(
[0],
[GPUShaderStage.VERTEX],
['buffer'],
[{ type: 'uniform' }],
[[{ buffer: cameraBuffer }]],
'Camera',
device
);
const generalUniformsBuffer = device.createBuffer({
size: Uint32Array.BYTES_PER_ELEMENT * 2,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const generalUniformsBGCLuster = createBindGroupCluster(
[0],
[GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT],
['buffer'],
[{ type: 'uniform' }],
[[{ buffer: generalUniformsBuffer }]],
'General',
device
);
// Same bindGroupLayout as in main file.
const nodeUniformsBindGroupLayout = device.createBindGroupLayout({
label: 'NodeUniforms.bindGroupLayout',
entries: [
{
binding: 0,
buffer: {
type: 'uniform',
},
visibility: GPUShaderStage.VERTEX,
},
],
});
// Fetch whale resources from the glb file
const whaleScene = await fetch('../../assets/gltf/whale.glb')
.then((res) => res.arrayBuffer())
.then((buffer) => convertGLBToJSONAndBinary(buffer, device));
// Builds a render pipeline for our whale mesh
// Since we are building a lightweight gltf parser around a gltf scene with a known
// quantity of meshes, we only build a renderPipeline for the singular mesh present
// within our scene. A more robust gltf parser would loop through all the meshes,
// cache replicated pipelines, and perform other optimizations.
whaleScene.meshes[0].buildRenderPipeline(
device,
gltfWGSL,
gltfWGSL,
presentationFormat,
depthTexture.format,
[
cameraBGCluster.bindGroupLayout,
generalUniformsBGCLuster.bindGroupLayout,
nodeUniformsBindGroupLayout,
GLTFSkin.skinBindGroupLayout,
]
);
// Create skinned grid resources
const skinnedGridVertexBuffers = createSkinnedGridBuffers(device);
// Buffer for our uniforms, joints, and inverse bind matrices
const skinnedGridUniformBufferUsage: GPUBufferDescriptor = {
// 5 4x4 matrices, one for each bone
size: MAT4X4_BYTES * 5,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
};
const skinnedGridJointUniformBuffer = device.createBuffer(
skinnedGridUniformBufferUsage
);
const skinnedGridInverseBindUniformBuffer = device.createBuffer(
skinnedGridUniformBufferUsage
);
const skinnedGridBoneBGCluster = createBindGroupCluster(
[0, 1],
[GPUShaderStage.VERTEX, GPUShaderStage.VERTEX],
['buffer', 'buffer'],
[{ type: 'read-only-storage' }, { type: 'read-only-storage' }],
[
[
{ buffer: skinnedGridJointUniformBuffer },
{ buffer: skinnedGridInverseBindUniformBuffer },
],
],
'SkinnedGridJointUniforms',
device
);
const skinnedGridPipeline = createSkinnedGridRenderPipeline(
device,
presentationFormat,
gridWGSL,
gridWGSL,
[
cameraBGCluster.bindGroupLayout,
generalUniformsBGCLuster.bindGroupLayout,
skinnedGridBoneBGCluster.bindGroupLayout,
]
);
// Global Calc
const aspect = canvas.width / canvas.height;
const perspectiveProjection = mat4.perspective(
(2 * Math.PI) / 5,
aspect,
0.1,
100.0
);
const orthographicProjection = mat4.ortho(-20, 20, -10, 10, -100, 100);
function getProjectionMatrix() {
if (settings.object !== 'Skinned Grid') {
return perspectiveProjection;
}
return orthographicProjection;
}
function getViewMatrix() {
const viewMatrix = mat4.identity();
if (settings.object === 'Skinned Grid') {
mat4.translate(
viewMatrix,
[
settings.cameraX * settings.objectScale,
settings.cameraY * settings.objectScale,
settings.cameraZ,
],
viewMatrix
);
} else {
mat4.translate(
viewMatrix,
[settings.cameraX, settings.cameraY, settings.cameraZ],
viewMatrix
);
}
return viewMatrix;
}
function getModelMatrix() {
const modelMatrix = mat4.identity();
const scaleVector = [
settings.objectScale,
settings.objectScale,
settings.objectScale,
];
mat4.scale(modelMatrix, scaleVector, modelMatrix);
if (settings.object === 'Whale') {
mat4.rotateY(modelMatrix, (Date.now() / 1000) * 0.5, modelMatrix);
}
return modelMatrix;
}
// Pass Descriptor for GLTFs
const gltfRenderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
clearValue: [0.3, 0.3, 0.3, 1.0],
loadOp: 'clear',
storeOp: 'store',
},
],
depthStencilAttachment: {
view: depthTexture.createView(),
depthLoadOp: 'clear',
depthClearValue: 1.0,
depthStoreOp: 'store',
},
};
// Pass descriptor for grid with no depth testing
const skinnedGridRenderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
clearValue: [0.3, 0.3, 0.3, 1.0],
loadOp: 'clear',
storeOp: 'store',
},
],
};
const animSkinnedGrid = (boneTransforms: Mat4[], angle: number) => {
const m = mat4.identity();
mat4.rotateZ(m, angle, boneTransforms[0]);
mat4.translate(boneTransforms[0], [4, 0, 0], m);
mat4.rotateZ(m, angle, boneTransforms[1]);
mat4.translate(boneTransforms[1], [4, 0, 0], m);
mat4.rotateZ(m, angle, boneTransforms[2]);
};
// Create a group of bones
// Each index associates an actual bone to its transforms, bindPoses, uniforms, etc
const createBoneCollection = (numBones: number): BoneObject => {
// Initial bone transformation
const transforms: Mat4[] = [];
// Bone bind poses, an extra matrix per joint/bone that represents the starting point
// of the bone before any transformations are applied
const bindPoses: Mat4[] = [];
// Create a transform, bind pose, and inverse bind pose for each bone
for (let i = 0; i < numBones; i++) {
transforms.push(mat4.identity());
bindPoses.push(mat4.identity());
}
// Get initial bind pose positions
animSkinnedGrid(bindPoses, 0);
const bindPosesInv = bindPoses.map((bindPose) => {
return mat4.inverse(bindPose);
});
return {
transforms,
bindPoses,
bindPosesInv,
};
};
// Create bones of the skinned grid and write the inverse bind positions to
// the skinned grid's inverse bind matrix array
const gridBoneCollection = createBoneCollection(5);
for (let i = 0; i < gridBoneCollection.bindPosesInv.length; i++) {
device.queue.writeBuffer(
skinnedGridInverseBindUniformBuffer,
i * 64,
gridBoneCollection.bindPosesInv[i]
);
}
// A map that maps a joint index to the original matrix transformation of a bone
const origMatrices = new Map<number, Mat4>();
const animWhaleSkin = (skin: GLTFSkin, angle: number) => {
for (let i = 0; i < skin.joints.length; i++) {
// Index into the current joint
const joint = skin.joints[i];
// If our map does
if (!origMatrices.has(joint)) {
origMatrices.set(joint, whaleScene.nodes[joint].source.getMatrix());
}
// Get the original position, rotation, and scale of the current joint
const origMatrix = origMatrices.get(joint);
let m = mat4.create();
// Depending on which bone we are accessing, apply a specific rotation to the bone's original
// transformation to animate it
if (joint === 1 || joint === 0) {
m = mat4.rotateY(origMatrix, -angle);
} else if (joint === 3 || joint === 4) {
m = mat4.rotateX(origMatrix, joint === 3 ? angle : -angle);
} else {
m = mat4.rotateZ(origMatrix, angle);
}
// Apply the current transformation to the transform values within the relevant nodes
// (these nodes, of course, each being nodes that represent joints/bones)
whaleScene.nodes[joint].source.position = mat4.getTranslation(m);
whaleScene.nodes[joint].source.scale = mat4.getScaling(m);
whaleScene.nodes[joint].source.rotation = quat.fromMat(m);
}
};
function frame() {
// Calculate camera matrices
const projectionMatrix = getProjectionMatrix();
const viewMatrix = getViewMatrix();
const modelMatrix = getModelMatrix();
// Calculate bone transformation
const t = (Date.now() / 20000) * settings.speed;
const angle = Math.sin(t) * settings.angle;
// Compute Transforms when angle is applied
animSkinnedGrid(gridBoneCollection.transforms, angle);
// Write to mvp to camera buffer
device.queue.writeBuffer(
cameraBuffer,
0,
projectionMatrix.buffer,
projectionMatrix.byteOffset,
projectionMatrix.byteLength
);
device.queue.writeBuffer(
cameraBuffer,
64,
viewMatrix.buffer,
viewMatrix.byteOffset,
viewMatrix.byteLength
);
device.queue.writeBuffer(
cameraBuffer,
128,
modelMatrix.buffer,
modelMatrix.byteOffset,
modelMatrix.byteLength
);
// Write to skinned grid bone uniform buffer
for (let i = 0; i < gridBoneCollection.transforms.length; i++) {
device.queue.writeBuffer(
skinnedGridJointUniformBuffer,
i * 64,
gridBoneCollection.transforms[i]
);
}
// Difference between these two render passes is just the presence of depthTexture
gltfRenderPassDescriptor.colorAttachments[0].view = context
.getCurrentTexture()
.createView();
skinnedGridRenderPassDescriptor.colorAttachments[0].view = context
.getCurrentTexture()
.createView();
// Update node matrixes
for (const scene of whaleScene.scenes) {
scene.root.updateWorldMatrix(device);
}
// Updates skins (we index into skins in the renderer, which is not the best approach but hey)
animWhaleSkin(whaleScene.skins[0], Math.sin(t) * settings.angle);
// Node 6 should be the only node with a drawable mesh so hopefully this works fine
whaleScene.skins[0].update(device, 6, whaleScene.nodes);
const commandEncoder = device.createCommandEncoder();
if (settings.object === 'Whale') {
const passEncoder = commandEncoder.beginRenderPass(
gltfRenderPassDescriptor
);
for (const scene of whaleScene.scenes) {
scene.root.renderDrawables(passEncoder, [
cameraBGCluster.bindGroups[0],
generalUniformsBGCLuster.bindGroups[0],
]);
}
passEncoder.end();
} else {
// Our skinned grid isn't checking for depth, so we pass it
// a separate render descriptor that does not take in a depth texture
const passEncoder = commandEncoder.beginRenderPass(
skinnedGridRenderPassDescriptor
);
passEncoder.setPipeline(skinnedGridPipeline);
passEncoder.setBindGroup(0, cameraBGCluster.bindGroups[0]);
passEncoder.setBindGroup(1, generalUniformsBGCLuster.bindGroups[0]);
passEncoder.setBindGroup(2, skinnedGridBoneBGCluster.bindGroups[0]);
// Pass in vertex and index buffers generated from our static skinned grid
// data at ./gridData.ts
passEncoder.setVertexBuffer(0, skinnedGridVertexBuffers.positions);
passEncoder.setVertexBuffer(1, skinnedGridVertexBuffers.joints);
passEncoder.setVertexBuffer(2, skinnedGridVertexBuffers.weights);
passEncoder.setIndexBuffer(skinnedGridVertexBuffers.indices, 'uint16');
passEncoder.drawIndexed(gridIndices.length, 1);
passEncoder.end();
}
device.queue.submit([commandEncoder.finish()]);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);