Skip to content

Commit cdceba5

Browse files
mvaligurskyMartin Valigursky
andauthored
[Fix] Return correct typed array from texture.read() on WebGPU platform (playcanvas#8251)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent d410848 commit cdceba5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/platform/graphics/webgpu/webgpu-texture.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TRACEID_RENDER_QUEUE } from '../../../core/constants.js';
22
import { Debug, DebugHelper } from '../../../core/debug.js';
33
import { math } from '../../../core/math/math.js';
44
import {
5-
pixelFormatInfo, isCompressedPixelFormat,
5+
pixelFormatInfo, isCompressedPixelFormat, getPixelFormatArrayType,
66
ADDRESS_REPEAT, ADDRESS_CLAMP_TO_EDGE, ADDRESS_MIRRORED_REPEAT,
77
PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F, PIXELFORMAT_DEPTHSTENCIL,
88
SAMPLETYPE_UNFILTERABLE_FLOAT, SAMPLETYPE_DEPTH,
@@ -567,16 +567,20 @@ class WebgpuTexture {
567567
// async read data from the staging buffer to a temporary array
568568
return device.readBuffer(stagingBuffer, size, null, immediate).then((temp) => {
569569

570+
// determine target buffer - use user's data buffer or allocate new
571+
const ArrayType = getPixelFormatArrayType(texture.format);
572+
const targetBuffer = data?.buffer ?? new ArrayBuffer(height * bytesPerRow);
573+
const target = new Uint8Array(targetBuffer, data?.byteOffset ?? 0, height * bytesPerRow);
574+
570575
// remove the 256 alignment padding from the end of each row
571-
const target = (data?.constructor === Uint8Array) ? data : new Uint8Array(data?.buffer ?? height * bytesPerRow);
572576
for (let i = 0; i < height; i++) {
573577
const srcOffset = i * paddedBytesPerRow;
574578
const dstOffset = i * bytesPerRow;
575-
const sub = temp.subarray(srcOffset, srcOffset + bytesPerRow);
576-
target.set(sub, dstOffset);
579+
target.set(temp.subarray(srcOffset, srcOffset + bytesPerRow), dstOffset);
577580
}
578581

579-
return data ?? target;
582+
// return user's data or create correctly-typed array view
583+
return data ?? new ArrayType(targetBuffer);
580584
});
581585
}
582586
}

0 commit comments

Comments
 (0)