Skip to content

Commit 3c55c42

Browse files
committed
fix: new implementation of Geometry.padGPUBufferAlignment to eliminate errors caused by inconsistent type with wgsl attributes
1 parent e3c0de9 commit 3c55c42

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

packages/gl/src/reshader/Geometry.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,24 @@ export default class Geometry {
9696
const ctor = array.constructor as any;
9797
const itemSize = array.length / vertexCount;
9898
const bytesPerElement = itemBytes / itemSize;
99-
// 无法对齐时,itemSize 一定是1或者3,补位成2或者4就能对齐了
100-
101-
const newItemSize = ensureAlignment(itemSize, bytesPerElement);
102-
const newArray = new ctor(newItemSize * vertexCount);
103-
for (let i = 0; i < vertexCount; i++) {
104-
for (let ii = 0; ii < itemSize; ii++) {
105-
newArray[i * newItemSize + ii] = array[i * itemSize + ii];
99+
// 无法对齐时,itemSize 一定是1,2,3,不可能是4
100+
if (itemSize === 3) {
101+
// itemSize 为 3时,补位为 4,wgsl中需要相应修改类型声明
102+
const newItemSize = ensureAlignment(itemSize, bytesPerElement);
103+
const newArray = new ctor(newItemSize * vertexCount);
104+
for (let i = 0; i < vertexCount; i++) {
105+
for (let ii = 0; ii < itemSize; ii++) {
106+
newArray[i * newItemSize + ii] = array[i * itemSize + ii];
107+
}
106108
}
109+
return newArray;
110+
} else {
111+
// itemSize 为 1或2时,提升数组的类型,不影响wgsl中的类型声明
112+
const newCtor = getPadArrayCtor(ctor, itemSize);
113+
const newArray = new newCtor(itemSize * vertexCount);
114+
newArray.set(array);
115+
return newArray;
107116
}
108-
return newArray;
109117
}
110118

111119
data: Record<string, AttributeData>
@@ -1316,3 +1324,25 @@ function ensureAlignment(itemSize: number, bytesPerElement: number): any {
13161324
return itemSize + (4 - (itemBytes % 4)) / bytesPerElement;
13171325
}
13181326

1327+
// itemSize 只可能是1,2
1328+
function getPadArrayCtor(ctor, itemSize): any {
1329+
if (ctor === Uint8Array || ctor === Uint8ClampedArray) {
1330+
if (itemSize === 1) {
1331+
return Uint32Array;
1332+
} else {
1333+
return Uint16Array;
1334+
}
1335+
} else if (ctor === Int8Array) {
1336+
if (itemSize === 1) {
1337+
return Int32Array;
1338+
} else {
1339+
return Int16Array;
1340+
}
1341+
} else if (ctor === Uint16Array) {
1342+
return Uint32Array;
1343+
} else if (ctor === Int16Array) {
1344+
return Int32Array;
1345+
} else {
1346+
throw new Error('sth unexpected in getPadArrayCtor');
1347+
}
1348+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct VertexInput {
3535
#endif
3636

3737
#ifdef HAS_OPACITY
38-
@location($i) aOpacity: vec4u,
38+
@location($i) aOpacity: u32,
3939
#endif
4040

4141
#if HAS_COLOR0
@@ -278,7 +278,7 @@ fn main(input: VertexInput) -> VertexOutput {
278278
#endif
279279

280280
#ifdef HAS_OPACITY
281-
output.vOpacity = f32(input.aOpacity[0]) / 255.0;
281+
output.vOpacity = f32(input.aOpacity) / 255.0;
282282
#else
283283
output.vOpacity = 1.0;
284284
#endif

packages/vt/src/layer/plugins/painters/wgsl/line_vert.wgsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,13 @@ fn main(input: VertexInput) -> VertexOutput {
279279
#endif
280280

281281
#if HAS_PATTERN || HAS_DASHARRAY || HAS_GRADIENT
282+
let aLinesofar = f32(input.aLinesofar);
282283
#ifdef HAS_GRADIENT
283-
output.vLinesofar = input.aLinesofar / MAX_LINE_DISTANCE;
284+
output.vLinesofar = aLinesofar / MAX_LINE_DISTANCE;
284285
output.vGradIndex = f32(input.aGradIndex);
285286
#else
286287
// /resScale * tileRatio 是为了把像素宽度转换为瓦片内的值域(即tile extent 8192或4096)
287-
let linesofar = input.aLinesofar - halfwidth * input.aExtrude.z / EXTRUDE_SCALE / resScale * uniforms.tileRatio;
288+
let linesofar = aLinesofar - halfwidth * f32(input.aExtrude.z) / EXTRUDE_SCALE / resScale * uniforms.tileRatio;
288289
output.vLinesofar = linesofar / uniforms.tileRatio * resScale;
289290
#endif
290291
#endif

packages/vt/src/layer/plugins/painters/wgsl/marker_vert.wgsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ struct VertexInput {
6262
@location($i) aDxDy: vec4i,
6363
#endif
6464
#ifdef HAS_MARKER_WIDTH
65-
@location($i) aMarkerWidth: vec2u,
65+
@location($i) aMarkerWidth: u32,
6666
#endif
6767
#ifdef HAS_MARKER_HEIGHT
68-
@location($i) aMarkerHeight: vec2u,
68+
@location($i) aMarkerHeight: u32,
6969
#endif
7070
#if HAS_MARKER_PITCH_ALIGN || HAS_TEXT_PITCH_ALIGN
7171
@location($i) aPitchAlign: vec2u,
@@ -142,13 +142,13 @@ fn main(vertexInput: VertexInput) -> VertexOutput {
142142
#endif
143143

144144
#ifdef HAS_MARKER_WIDTH
145-
var myMarkerWidth = f32(vertexInput.aMarkerWidth[0]);
145+
var myMarkerWidth = f32(vertexInput.aMarkerWidth);
146146
#else
147147
var myMarkerWidth = uniforms.markerWidth;
148148
#endif
149149

150150
#ifdef HAS_MARKER_HEIGHT
151-
var myMarkerHeight = f32(vertexInput.aMarkerHeight[0]);
151+
var myMarkerHeight = f32(vertexInput.aMarkerHeight);
152152
#else
153153
var myMarkerHeight = uniforms.markerHeight;
154154
#endif

packages/vt/src/layer/plugins/painters/wgsl/text_line_vert.wgsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct VertexInput {
4242
@location($i) aOffset: vec2i,
4343
#endif
4444
#ifdef ENABLE_COLLISION
45-
@location($i) aOpacity: vec4u,
45+
@location($i) aOpacity: u32,
4646
#endif
4747
#ifdef HAS_OPACITY
4848
@location($i) aColorOpacity: u32,
@@ -57,7 +57,7 @@ struct VertexInput {
5757
@location($i) aTextDy: i32,
5858
#endif
5959
#ifdef HAS_PITCH_ALIGN
60-
@location($i) aPitchAlign: vec2u,
60+
@location($i) aPitchAlign: u32,
6161
#endif
6262
#ifdef HAS_TEXT_FILL
6363
@location($i) aTextFill: vec4u,
@@ -177,7 +177,7 @@ fn main(input: VertexInput) -> VertexOutput {
177177
output.vTexCoord = texCoord / uniforms.glyphTexSize;
178178
output.vTextSize = myTextSize;
179179
#ifdef ENABLE_COLLISION
180-
output.vOpacity = f32(input.aOpacity[0]) / 255.0;
180+
output.vOpacity = f32(input.aOpacity) / 255.0;
181181
#else
182182
output.vOpacity = 1.0;
183183
#endif

packages/vt/src/layer/plugins/painters/wgsl/text_vert.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct VertexInput {
4040
#endif
4141
@location($i) aShape: vec4i,
4242
#ifdef ENABLE_COLLISION
43-
@location($i) aOpacity: vec4u,
43+
@location($i) aOpacity: u32,
4444
#endif
4545
#ifdef HAS_OPACITY
4646
@location($i) aColorOpacity: u32,
@@ -216,7 +216,7 @@ fn main(input: VertexInput) -> VertexOutput {
216216

217217
output.vTextSize = myTextSize;
218218
#ifdef ENABLE_COLLISION
219-
output.vOpacity = f32(input.aOpacity[0]) / 255.0;
219+
output.vOpacity = f32(input.aOpacity) / 255.0;
220220
#else
221221
output.vOpacity = 1.0;
222222
#endif

0 commit comments

Comments
 (0)