Skip to content

Commit 1cee238

Browse files
author
Mike Bond
committed
Trying to get skinned meshes working with voxelization
1 parent bd93e49 commit 1cee238

File tree

4 files changed

+107
-56
lines changed

4 files changed

+107
-56
lines changed

packages/dev/core/src/Engines/WebGPU/webgpuDrawContext.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { VertexBuffer } from "../../Buffers/buffer";
1+
import type { VertexBuffer } from "../../Buffers/buffer";
22
import type { DataBuffer } from "../../Buffers/dataBuffer";
33
import type { WebGPUDataBuffer } from "../../Meshes/WebGPU/webgpuDataBuffer";
44
import type { Nullable } from "../../types";
@@ -171,10 +171,13 @@ export class WebGPUDrawContext implements IDrawContext {
171171
}
172172

173173
/**
174-
* Metadata storage for vertex buffer configurations (stride, offset, etc.)
174+
* Setup or disable vertex pulling as needed.
175+
* @param useVertexPulling Use vertex pulling or not
176+
* @param webgpuPipelineContext The WebGPU pipeline context
177+
* @param vertexBuffers The current vertex buffers
178+
* @param indexBuffer The current index buffer
179+
* @param overrideVertexBuffers The vertex buffers to override
175180
*/
176-
public vertexBufferMetadata: { [name: string]: { strideInFloats: number; offsetInFloats: number; componentCount: number } } = {};
177-
178181
public setVertexPulling(
179182
useVertexPulling: boolean,
180183
webgpuPipelineContext: WebGPUPipelineContext,
@@ -191,9 +194,6 @@ export class WebGPUDrawContext implements IDrawContext {
191194

192195
const bufferNames = webgpuPipelineContext.shaderProcessingContext.bufferNames;
193196

194-
// Clear previous metadata
195-
this.vertexBufferMetadata = {};
196-
197197
if (overrideVertexBuffers) {
198198
for (const attributeName in overrideVertexBuffers) {
199199
const vertexBuffer = overrideVertexBuffers[attributeName];
@@ -204,17 +204,6 @@ export class WebGPUDrawContext implements IDrawContext {
204204
const buffer = vertexBuffer.effectiveBuffer as Nullable<WebGPUDataBuffer>;
205205

206206
this.setBuffer(attributeName, useVertexPulling ? buffer : null);
207-
208-
// Store metadata for vertex pulling
209-
if (useVertexPulling) {
210-
const bytesPerElement =
211-
vertexBuffer.type === VertexBuffer.FLOAT ? 4 : vertexBuffer.type === VertexBuffer.UNSIGNED_INT || vertexBuffer.type === VertexBuffer.INT ? 4 : 2;
212-
this.vertexBufferMetadata[attributeName] = {
213-
strideInFloats: vertexBuffer.effectiveByteStride / bytesPerElement, // Convert bytes to float32 elements
214-
offsetInFloats: vertexBuffer.effectiveByteOffset / bytesPerElement, // Convert bytes to float32 elements
215-
componentCount: vertexBuffer.getSize(),
216-
};
217-
}
218207
}
219208
}
220209

@@ -231,17 +220,6 @@ export class WebGPUDrawContext implements IDrawContext {
231220
const buffer = vertexBuffer.effectiveBuffer as Nullable<WebGPUDataBuffer>;
232221

233222
this.setBuffer(attributeName, useVertexPulling ? buffer : null);
234-
235-
// Store metadata for vertex pulling
236-
if (useVertexPulling) {
237-
const bytesPerElement =
238-
vertexBuffer.type === VertexBuffer.FLOAT ? 4 : vertexBuffer.type === VertexBuffer.UNSIGNED_INT || vertexBuffer.type === VertexBuffer.INT ? 4 : 2;
239-
this.vertexBufferMetadata[attributeName] = {
240-
strideInFloats: vertexBuffer.effectiveByteStride / bytesPerElement, // Convert bytes to float32 elements
241-
offsetInFloats: vertexBuffer.effectiveByteOffset / bytesPerElement, // Convert bytes to float32 elements
242-
componentCount: vertexBuffer.getSize(),
243-
};
244-
}
245223
}
246224

247225
if (bufferNames.indexOf("indices") !== -1) {

packages/dev/core/src/Materials/materialHelper.functions.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { MaterialFlags } from "./materialFlags";
2222
import { Texture } from "./Textures/texture";
2323
import type { CubeTexture } from "./Textures/cubeTexture";
2424
import type { Color3 } from "core/Maths/math.color";
25+
import { GetTypeByteLength } from "../Buffers/bufferUtils";
2526

2627
// For backwards compatibility, we export everything from the pure version of this file.
2728
export * from "./materialHelper.functions.pure";
@@ -622,17 +623,14 @@ export function PrepareDefinesForVertexPullingMetadata(mesh: AbstractMesh, defin
622623
}
623624

624625
const upperName = attributeName.toUpperCase();
625-
626+
const sizeInBytes = GetTypeByteLength(vertexBuffer.type);
626627
// Calculate stride in float32 elements
627-
const strideInFloats = vertexBuffer.effectiveByteStride / 4;
628-
defines[`${upperName}_STRIDE_IN_FLOATS`] = strideInFloats || vertexBuffer.getSize();
628+
const stride = vertexBuffer.effectiveByteStride / sizeInBytes;
629+
defines[`${upperName}_STRIDE`] = stride || vertexBuffer.getSize();
629630

630631
// Calculate offset in float32 elements
631-
const offsetInFloats = vertexBuffer.effectiveByteOffset / 4;
632-
defines[`${upperName}_OFFSET_IN_FLOATS`] = offsetInFloats;
633-
634-
// Component count
635-
defines[`${upperName}_COMPONENT_COUNT`] = vertexBuffer.getSize();
632+
const offset = vertexBuffer.effectiveByteOffset / sizeInBytes;
633+
defines[`${upperName}_OFFSET`] = offset;
636634
}
637635
}
638636

packages/dev/core/src/Materials/shaderMaterial.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { EngineStore } from "../Engines/engineStore";
2121
import { Constants } from "../Engines/constants";
2222
import { AddClipPlaneUniforms, BindClipPlane, PrepareStringDefinesForClipPlanes } from "./clipPlaneMaterialHelper";
2323
import type { WebGPUEngine } from "core/Engines/webgpuEngine";
24+
import { GetTypeByteLength } from "../Buffers/bufferUtils";
2425

2526
import type { ExternalTexture } from "./Textures/externalTexture";
2627
import {
@@ -900,14 +901,14 @@ export class ShaderMaterial extends PushMaterial {
900901
for (const attributeName in vertexBuffers) {
901902
const vertexBuffer = vertexBuffers[attributeName];
902903
if (vertexBuffer) {
904+
const componentBytes = GetTypeByteLength(vertexBuffer.type);
903905
const upperName = attributeName.toUpperCase();
904-
const strideInFloats = vertexBuffer.effectiveByteStride / 4;
905-
const offsetInFloats = vertexBuffer.effectiveByteOffset / 4;
906-
const componentCount = vertexBuffer.getSize();
906+
const stride = vertexBuffer.effectiveByteStride / 4;
907+
const offset = vertexBuffer.effectiveByteOffset / 4;
907908

908-
defines.push(`#define ${upperName}_STRIDE_IN_FLOATS ${strideInFloats || componentCount}`);
909-
defines.push(`#define ${upperName}_OFFSET_IN_FLOATS ${offsetInFloats}`);
910-
defines.push(`#define ${upperName}_COMPONENT_COUNT ${componentCount}`);
909+
defines.push(`#define ${upperName}_STRIDE ${stride}`);
910+
defines.push(`#define ${upperName}_OFFSET ${offset}`);
911+
defines.push(`#define ${upperName}_COMPONENT_BYTES ${componentBytes}`);
911912
}
912913
}
913914
}

packages/dev/core/src/ShadersWGSL/iblVoxelGrid.vertex.fx

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ var<storage, read> indices : array<u32>;
1515
#endif
1616
var<storage, read> position : array<f32>;
1717
#if NUM_BONE_INFLUENCERS > 0
18-
var<storage, read> matricesIndices : array<vec4f>;
19-
var<storage, read> matricesWeights : array<vec4f>;
18+
var<storage, read> matricesIndices : array<u32>;
19+
var<storage, read> matricesWeights : array<f32>;
2020
#if NUM_BONE_INFLUENCERS > 4
21-
var<storage, read> matricesIndicesExtra : array<vec4f>;
22-
var<storage, read> matricesWeightsExtra : array<vec4f>;
21+
var<storage, read> matricesIndicesExtra : array<u32>;
22+
var<storage, read> matricesWeightsExtra : array<f32>;
2323
#endif
2424
#endif
2525

@@ -30,12 +30,12 @@ varying vNormalizedPosition : vec3f;
3030
flat varying f_swizzle: i32;
3131

3232
// Vertex buffer metadata (set via defines or defaults)
33-
#ifndef POSITION_STRIDE_IN_FLOATS
34-
#define POSITION_STRIDE_IN_FLOATS 3
33+
#ifndef POSITION_STRIDE
34+
#define POSITION_STRIDE 3
3535
#endif
3636

37-
#ifndef POSITION_OFFSET_IN_FLOATS
38-
#define POSITION_OFFSET_IN_FLOATS 0
37+
#ifndef POSITION_OFFSET
38+
#define POSITION_OFFSET 0
3939
#endif
4040

4141
#ifndef POSITION_COMPONENT_COUNT
@@ -44,13 +44,87 @@ flat varying f_swizzle: i32;
4444

4545
fn readVertexPosition(index : u32)->vec3f {
4646
var pos : vec3f;
47-
let baseOffset = POSITION_OFFSET_IN_FLOATS + index * POSITION_STRIDE_IN_FLOATS;
47+
let baseOffset = POSITION_OFFSET + index * POSITION_STRIDE;
4848
pos.x = position[baseOffset];
4949
pos.y = position[baseOffset + 1u];
5050
pos.z = position[baseOffset + 2u];
5151
return pos;
5252
}
5353

54+
#if NUM_BONE_INFLUENCERS > 0
55+
// Matrix indices are stored as UNSIGNED_BYTE (4 bytes packed into u32)
56+
#ifndef MATRICESINDICES_STRIDE
57+
#define MATRICESINDICES_STRIDE 1
58+
#endif
59+
#ifndef MATRICESINDICES_OFFSET
60+
#define MATRICESINDICES_OFFSET 0
61+
#endif
62+
63+
fn readMatrixIndices(index : u32) -> vec4f {
64+
let baseOffset = MATRICESINDICES_OFFSET + index * MATRICESINDICES_STRIDE;
65+
#if MATRICESINDICES_COMPONENT_BYTES == 1
66+
let packed = matricesIndices[baseOffset];
67+
// Extract 4 bytes from u32 and convert to floats
68+
return vec4f(
69+
f32(packed & 0xFFu),
70+
f32((packed >> 8u) & 0xFFu),
71+
f32((packed >> 16u) & 0xFFu),
72+
f32((packed >> 24u) & 0xFFu)
73+
);
74+
#elif MATRICESINDICES_COMPONENT_BYTES == 2
75+
let packed1 = matricesIndices[baseOffset];
76+
let packed2 = matricesIndices[baseOffset + 1u];
77+
// Extract 4 bytes from two u32 and convert to floats
78+
return vec4f(
79+
f32(packed1 & 0xFFFFu),
80+
f32((packed1 >> 16u) & 0xFFFFu),
81+
f32(packed2 & 0xFFFFu),
82+
f32((packed2 >> 16u) & 0xFFFFu)
83+
);
84+
#endif
85+
}
86+
87+
#ifndef MATRICESWEIGHTS_STRIDE
88+
#define MATRICESWEIGHTS_STRIDE 4
89+
#endif
90+
#ifndef MATRICESWEIGHTS_OFFSET
91+
#define MATRICESWEIGHTS_OFFSET 0
92+
#endif
93+
94+
fn readMatrixWeights(index : u32) -> vec4f {
95+
let baseOffset = MATRICESWEIGHTS_OFFSET + index * MATRICESWEIGHTS_STRIDE;
96+
return vec4f(
97+
matricesWeights[baseOffset],
98+
matricesWeights[baseOffset + 1u],
99+
matricesWeights[baseOffset + 2u],
100+
matricesWeights[baseOffset + 3u]
101+
);
102+
}
103+
104+
#if NUM_BONE_INFLUENCERS > 4
105+
fn readMatrixIndicesExtra(index : u32) -> vec4f {
106+
let baseOffset = MATRICESINDICESEXTRA_OFFSET + index * MATRICESINDICESEXTRA_STRIDE;
107+
let packed = matricesIndicesExtra[baseOffset];
108+
return vec4f(
109+
f32(packed & 0xFFu),
110+
f32((packed >> 8u) & 0xFFu),
111+
f32((packed >> 16u) & 0xFFu),
112+
f32((packed >> 24u) & 0xFFu)
113+
);
114+
}
115+
116+
fn readMatrixWeightsExtra(index : u32) -> vec4f {
117+
let baseOffset = MATRICESWEIGHTSEXTRA_OFFSET + index * MATRICESWEIGHTSEXTRA_STRIDE;
118+
return vec4f(
119+
matricesWeightsExtra[baseOffset],
120+
matricesWeightsExtra[baseOffset + 1u],
121+
matricesWeightsExtra[baseOffset + 2u],
122+
matricesWeightsExtra[baseOffset + 3u]
123+
);
124+
}
125+
#endif
126+
#endif
127+
54128
fn readVertexIndex(index : u32)->u32 {
55129
#ifndef VERTEX_PULLING_USE_INDEX_BUFFER
56130
return index;
@@ -96,11 +170,11 @@ let inputPosition: vec3f = positionUpdated;
96170
#include <bakedVertexAnimation>
97171

98172
#if NUM_BONE_INFLUENCERS > 0
99-
let matrixIndex = matricesIndices[vertIdx];
100-
let matrixWeight = matricesWeights[vertIdx];
173+
let matrixIndex = readMatrixIndices(vertIdx);
174+
let matrixWeight = readMatrixWeights(vertIdx);
101175
#if NUM_BONE_INFLUENCERS > 4
102-
let matrixIndexExtra = matricesIndicesExtra[vertIdx];
103-
let matrixWeightExtra = matricesWeightsExtra[vertIdx];
176+
let matrixIndexExtra = readMatrixIndicesExtra(vertIdx);
177+
let matrixWeightExtra = readMatrixWeightsExtra(vertIdx);
104178
#endif
105179
#endif
106180
#include <bonesVertex>(vertexInputs.matricesIndices,matrixIndex,vertexInputs.matricesWeights,matrixWeight)

0 commit comments

Comments
 (0)