Skip to content

Commit 9643f54

Browse files
committed
fix 3d mip upload
1 parent 126af18 commit 9643f54

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/texture-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function uploadDataToTexture(
122122
const bytesPerRow = blocksAcross * bytesPerBlock!;
123123
const bytesPerLayer = bytesPerRow * blocksDown;
124124
const numLayers = texture.dimension === '3d'
125-
? data.byteLength / bytesPerLayer
125+
? size[2]
126126
: 1;
127127
size[0] = blocksAcross * blockWidth;
128128
size[1] = blocksDown * blockHeight;

test/tests/texture-utils-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,59 @@ describe('texture-utils tests', () => {
378378
});
379379
}));
380380

381+
it('test uploads multiple mip levels to 3d texture', testWithDevice(async device => {
382+
const r = [255, 0, 0, 255];
383+
const y = [255, 255, 0, 255];
384+
const g = [ 0, 255, 0, 255];
385+
386+
const mips = [
387+
[
388+
y, y, y, y,
389+
y, y, y, y,
390+
y, y, y, y,
391+
y, y, y, y,
392+
393+
y, y, y, y,
394+
y, y, y, y,
395+
y, y, y, y,
396+
y, y, y, y,
397+
398+
y, y, y, y,
399+
y, y, y, y,
400+
y, y, y, y,
401+
y, y, y, y,
402+
403+
y, y, y, y,
404+
y, y, y, y,
405+
y, y, y, y,
406+
y, y, y, y,
407+
],
408+
[
409+
r, r,
410+
r, r,
411+
412+
r, r,
413+
r, r,
414+
],
415+
[
416+
g,
417+
],
418+
];
419+
420+
const data = new Uint8Array(mips.flat(2));
421+
const texture = createTextureFromSource(device, data, {
422+
size: [4, 4, 4],
423+
mipLevelCount: 3,
424+
dimension: '3d',
425+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC,
426+
});
427+
const mipTexels = await Promise.all(mips.map((_, i) => readTextureUnpadded(device, texture, i, 0)));
428+
429+
mipTexels.forEach((texels, i) => {
430+
assertArrayEqual(texels, new Uint8Array(mips[i].flat()), `mipLevel: ${i}`);
431+
});
432+
}));
433+
381434
it(`works with format bc7-rgba-unorm`, testWithDevice(async device => {
382435
if (!device.features.has('texture-compression-bc')) {
383436
this.skip();

0 commit comments

Comments
 (0)