From 6114d547df14dac0ace0ca0da4ff46e593557f89 Mon Sep 17 00:00:00 2001 From: cmhhelgeson <62450112+cmhhelgeson@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:38:04 -0800 Subject: [PATCH] converted uniform matrix buffers to storage buffers --- src/sample/skinnedMesh/glbUtils.ts | 6 +++--- src/sample/skinnedMesh/gltf.wgsl | 14 ++------------ src/sample/skinnedMesh/grid.wgsl | 4 ++-- src/sample/skinnedMesh/main.ts | 4 ++-- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/sample/skinnedMesh/glbUtils.ts b/src/sample/skinnedMesh/glbUtils.ts index 6f5aa89a..71284244 100644 --- a/src/sample/skinnedMesh/glbUtils.ts +++ b/src/sample/skinnedMesh/glbUtils.ts @@ -657,7 +657,7 @@ export class GLTFSkin { { binding: 0, buffer: { - type: 'uniform', + type: 'read-only-storage', }, visibility: GPUShaderStage.VERTEX, }, @@ -665,7 +665,7 @@ export class GLTFSkin { { binding: 1, buffer: { - type: 'uniform', + type: 'read-only-storage', }, visibility: GPUShaderStage.VERTEX, }, @@ -699,7 +699,7 @@ export class GLTFSkin { this.joints = joints; const skinGPUBufferUsage: GPUBufferDescriptor = { size: Float32Array.BYTES_PER_ELEMENT * 16 * joints.length, - usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, }; this.jointMatricesUniformBuffer = device.createBuffer(skinGPUBufferUsage); this.inverseBindMatricesUniformBuffer = diff --git a/src/sample/skinnedMesh/gltf.wgsl b/src/sample/skinnedMesh/gltf.wgsl index c3de6847..c4b120a0 100644 --- a/src/sample/skinnedMesh/gltf.wgsl +++ b/src/sample/skinnedMesh/gltf.wgsl @@ -34,18 +34,8 @@ struct NodeUniforms { @group(0) @binding(0) var camera_uniforms: CameraUniforms; @group(1) @binding(0) var general_uniforms: GeneralUniforms; @group(2) @binding(0) var node_uniforms: NodeUniforms; -// Note that size of each of the matrix arrays below is equal to size of the number of inverseBindMatrices/joints defined in our whale glb object. -// As such, this shader can not be applied to any gltf object, as each skinned gltf object contains a different number of joints. -// Ways this shader can be made more generalizable include: -// a. Making our shader a constructable string returned from a function that takes the skin's current number of joints as an argument. -// For example, converting array to return `array joint_matrices -> var joint_matrices -// c. Reading our matrix data in as a texture -// However, for this limited, single object example, the current approach works fine, and these considerations are only -// necessary in the context of a more robust gltf parser/shader package. -@group(3) @binding(0) var joint_matrices: array, 6>; -@group(3) @binding(1) var inverse_bind_matrices: array, 6>; +@group(3) @binding(0) var joint_matrices: array>; +@group(3) @binding(1) var inverse_bind_matrices: array>; @vertex fn vertexMain(input: VertexInput) -> VertexOutput { diff --git a/src/sample/skinnedMesh/grid.wgsl b/src/sample/skinnedMesh/grid.wgsl index 115a48df..cdfc9cac 100644 --- a/src/sample/skinnedMesh/grid.wgsl +++ b/src/sample/skinnedMesh/grid.wgsl @@ -24,8 +24,8 @@ struct GeneralUniforms { @group(0) @binding(0) var camera_uniforms: CameraUniforms; @group(1) @binding(0) var general_uniforms: GeneralUniforms; -@group(2) @binding(0) var joint_matrices: array, 5>; -@group(2) @binding(1) var inverse_bind_matrices: array, 5>; +@group(2) @binding(0) var joint_matrices: array>; +@group(2) @binding(1) var inverse_bind_matrices: array>; @vertex fn vertexMain(input: VertexInput) -> VertexOutput { diff --git a/src/sample/skinnedMesh/main.ts b/src/sample/skinnedMesh/main.ts index 29956b32..9ab2c428 100644 --- a/src/sample/skinnedMesh/main.ts +++ b/src/sample/skinnedMesh/main.ts @@ -235,7 +235,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => { const skinnedGridUniformBufferUsage: GPUBufferDescriptor = { // 5 4x4 matrices, one for each bone size: MAT4X4_BYTES * 5, - usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, + usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST, }; const skinnedGridJointUniformBuffer = device.createBuffer( skinnedGridUniformBufferUsage @@ -247,7 +247,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => { [0, 1], [GPUShaderStage.VERTEX, GPUShaderStage.VERTEX], ['buffer', 'buffer'], - [{ type: 'uniform' }, { type: 'uniform' }], + [{ type: 'read-only-storage' }, { type: 'read-only-storage' }], [ [ { buffer: skinnedGridJointUniformBuffer },