Skip to content

Commit 718f983

Browse files
committed
fix buffer's location and padding of uniform values
1 parent 1c3228a commit 718f983

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

packages/reshader.gl/src/Geometry.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,20 @@ export default class Geometry {
10831083
}
10841084

10851085
getBufferDescriptor(vertexInfo) {
1086+
const attrInfos = [];
1087+
for (const p in vertexInfo) {
1088+
const info = vertexInfo[p];
1089+
attrInfos[info.location] = info;
1090+
}
10861091
const data = this.data;
10871092
const bufferDesc = [];
10881093
const bufferMapping = {};
1089-
for (const p in data) {
1094+
// bufferDesc的顺序需要和wgsl中的location对应
1095+
for (let i = 0; i < attrInfos.length; i++) {
1096+
if (!attrInfos[i]) {
1097+
continue;
1098+
}
1099+
const p = attrInfos[i].geoAttrName;
10901100
const attr = data[p];
10911101
if (!attr) {
10921102
continue;
@@ -1098,7 +1108,6 @@ export default class Geometry {
10981108
}
10991109
const accessorName = attr.accessorName;
11001110
const byteStride = attr.byteStride;
1101-
let stridePadding = 0;
11021111
if (byteStride && accessorName) {
11031112
// a GLTF accessor style attribute
11041113
const format = getFormatFromGLTFAccessor(attr.componentType, attr.itemSize);
@@ -1121,11 +1130,11 @@ export default class Geometry {
11211130
]
11221131
}
11231132
bufferMapping[accessorName] = desc;
1124-
bufferDesc.push(desc);
1133+
bufferDesc[i] = desc;
11251134
}
11261135
} else {
11271136
const desc = getAttrBufferDescriptor(attr, info);
1128-
bufferDesc.push(desc);
1137+
bufferDesc[i] = desc;
11291138
}
11301139
}
11311140
return bufferDesc;

packages/reshader.gl/src/pbr/wgsl/standard_vert.wgsl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,25 @@ fn main(input: VertexInput) -> VertexOutput {
238238
#endif
239239

240240
#if HAS_TANGENT || HAS_NORMAL
241-
let positionNormalMatrix = mat3x3f(localPositionMatrix[0].xyz, localPositionMatrix[1].xyz, localPositionMatrix[2].xyz);
241+
let positionNormalMatrix = mat3x3f(
242+
localPositionMatrix[0].xyz,
243+
localPositionMatrix[1].xyz,
244+
localPositionMatrix[2].xyz
245+
);
242246
let normalMatrix = uniforms.modelNormalMatrix * positionNormalMatrix;
243247
#if HAS_TANGENT
244248
let tangentFrame = toTangentFrameWithTangent(input.aTangent);
245249
Normal = tangentFrame.n;
246250
let t = tangentFrame.t;
247251
output.vModelTangent = vec4f(normalMatrix * t, input.aTangent.w);
248252
#else
249-
Normal = decode_getNormal(input.aNormal.xyz);
253+
Normal = decode_getNormal(vec3f(input.aNormal.xyz));
250254
#endif
251255
let localNormal = Normal;
252256
output.vModelNormal = normalMatrix * localNormal;
253257
#else
254-
Normal = vec3f(0.0);
255-
output.vModelNormal = vec3f(0.0);
258+
Normal = vec3f(0);
259+
output.vModelNormal = vec3f(0);
256260
#endif
257261

258262
#if HAS_TANGENT

packages/reshader.gl/src/webgpu/DynamicBuffer.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,37 @@ export default class DynamicBuffer {
4444
}
4545
const offset = dynamicOffset + member.offset;
4646
const size = member.size;
47-
this._fillValue(storage, offset, size, value);
47+
this._fillValue(member.type, storage, offset, size, value);
4848
}
4949
dynamicOffset += roundUp(mapping[i].size, bufferAlignment);
5050
} else if (uniform.resourceType === ResourceType.Uniform) {
5151
dynamicOffsets.addItem({ binding: uniform.binding, offset: dynamicOffset });
5252
const value = uniformValues[uniform.name];
5353
const size = isFunction(uniform.size) ? uniform.size() : uniform.size;
54-
this._fillValue(storage, dynamicOffset, size, value);
54+
this._fillValue(uniform.type, storage, dynamicOffset, size, value);
5555
dynamicOffset += roundUp(size, bufferAlignment);
5656
}
5757
}
5858

5959
// console.log(debugInfo.join());
6060
}
6161

62-
_fillValue(buffer, offset, size, value) {
62+
_fillValue(type, buffer, offset, size, value) {
6363
// we always use f32 in WGSL
6464
const view = new Float32Array(buffer, offset, size / 4);
6565
if (isArray(value)) {
66+
const padding = isPadding(type);
67+
// 需要padding的类型参考:
68+
// https://github.com/greggman/webgpu-utils/blob/dev/src/wgsl-types.ts
69+
// wgsl 1.0中只会隔3个字节,pad一个字节
6670
for (let i = 0; i < value.length; i++) {
67-
view[i] = value[i];
71+
if (padding) {
72+
const col = Math.floor(i / 3);
73+
const row = i % 3;
74+
view[col * 4 + row] = value[i];
75+
} else {
76+
view[i] = value[i];
77+
}
6878
}
6979
} else {
7080
view[0] = value;
@@ -76,3 +86,18 @@ export default class DynamicBuffer {
7686
delete this.allocation;
7787
}
7888
}
89+
90+
const PADDING_TYPES = {
91+
mat2x3f: { pad: [3, 1] },
92+
mat2x3h: { pad: [3, 1] },
93+
mat3x3f: { pad: [3, 1] },
94+
mat3x3h: { pad: [3, 1] },
95+
mat4x3f: { pad: [3, 1] },
96+
mat4x3h: { pad: [3, 1] },
97+
mat3x4f: { pad: [3, 1] },
98+
mat3x4h: { pad: [3, 1] },
99+
};
100+
101+
function isPadding(type) {
102+
return PADDING_TYPES[type.name];
103+
}

packages/vt/src/packer/pack/util/array.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function getIndexArrayType(max) {
3838

3939
export function getPosArrayType(max) {
4040
max = Math.abs(max);
41-
if (max < 128) return Int8Array;
41+
// webgpu中会因为Type不同生成新的command,所以取消Int8Array
42+
// if (max < 128) return Int8Array;
4243
if (max < 65536 / 2) return Int16Array;
4344
// https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-e
4445
if (max < Math.pow(2, 24)) return Float32Array;
@@ -47,7 +48,8 @@ export function getPosArrayType(max) {
4748
}
4849

4950
export function getUnsignedArrayType(max) {
50-
if (max < 256) return Uint8Array;
51+
// webgpu中会因为Type不同生成新的shader,所以取消Uint8Array
52+
// if (max < 256) return Uint8Array;
5153
if (max < 65536) return Uint16Array;
5254
return Float32Array;
5355
}

0 commit comments

Comments
 (0)