Skip to content

Commit 4854502

Browse files
committed
Make sure bytes are aligned in p5.Geometry
1 parent 0c7fd4a commit 4854502

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/webgpu/p5.RendererWebGPU.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function rendererWebGPU(p5, fn) {
324324
const device = this.device;
325325

326326
const buffer = device.createBuffer({
327-
size: indices.length * indexType.BYTES_PER_ELEMENT,
327+
size: Math.ceil((indices.length * indexType.BYTES_PER_ELEMENT) / 4) * 4,
328328
usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
329329
mappedAtCreation: true,
330330
});

test/unit/visual/cases/webgpu.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,42 @@ visualSuite("WebGPU", function () {
716716
});
717717
});
718718

719+
visualSuite("Buffer Alignment", function () {
720+
visualTest(
721+
"buildGeometry with non-4-byte-aligned index buffer size",
722+
async function (p5, screenshot) {
723+
await p5.createCanvas(50, 50, p5.WEBGPU);
724+
725+
p5.randomSeed(1);
726+
p5.colorMode(p5.HSB, 360, 100, 100);
727+
728+
// Create a geometry that will result in a non-4-byte-aligned buffer size
729+
// We want to create an odd number of triangles that results in
730+
// indices.length * indexType.BYTES_PER_ELEMENT not being divisible by 4
731+
const geom = p5.buildGeometry(() => {
732+
p5.noStroke();
733+
// Create 1323 triangles (3969 indices total)
734+
// With Uint16 indices (2 bytes each): 3969 * 2 = 7938 bytes
735+
// 7938 is not divisible by 4, we want to ensure the renderer
736+
// pads this to keep bytes aligned
737+
for (let i = 0; i < 1323; i++) {
738+
p5.fill(p5.random(360), 80, 90);
739+
p5.triangle(
740+
p5.random(-25, 25), p5.random(-25, 25),
741+
p5.random(-25, 25), p5.random(-25, 25),
742+
p5.random(-25, 25), p5.random(-25, 25)
743+
);
744+
}
745+
});
746+
747+
p5.background(20);
748+
p5.model(geom);
749+
750+
await screenshot();
751+
}
752+
);
753+
});
754+
719755
visualSuite("Image Based Lighting", function () {
720756
const shinesses = [50, 150];
721757
for (const shininess of shinesses) {
4.75 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)