Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions src/suites/cts/copyImageBitmapToTexture.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
export const description = `
copy imageBitmap To texture tests.
`;

import { TestGroup, pcombine, poptions } from '../../framework/index.js';

import { GPUTest } from './gpu_test.js';

function calculateRowPitch(width: number, bytesPerPixel: number): number {
const bytesPerRow = width * bytesPerPixel;
// Rounds up to a multiple of 256 according to WebGPU requirements.
return (((bytesPerRow - 1) >> 8) + 1) << 8;
Comment thread
kainino0x marked this conversation as resolved.
Resolved
}

class F extends GPUTest {
checkCopyImageBitmapResult(
src: GPUBuffer,
expected: ArrayBufferView,
width: number,
height: number,
bytesPerPixel: number
): void {
const exp = new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength);
const rowPitch = calculateRowPitch(width, bytesPerPixel);
const dst = this.createCopyForMapRead(src, rowPitch * height);

this.eventualAsyncExpectation(async niceStack => {
const actual = new Uint8Array(await dst.mapReadAsync());
const check = this.checkBufferWithRowPitch(
actual,
exp,
width,
height,
rowPitch,
bytesPerPixel
);
if (check !== undefined) {
niceStack.message = check;
this.rec.fail(niceStack);
}
dst.destroy();
});
}

checkBufferWithRowPitch(
actual: Uint8Array,
exp: Uint8Array,
width: number,
height: number,
rowPitch: number,
bytesPerPixel: number
): string | undefined {
const lines = [];
let failedPixels = 0;
for (let i = 0; i < height; ++i) {
const bytesPerRow = width * bytesPerPixel;
for (let j = 0; j < bytesPerRow; ++j) {
const indexExp = j + i * bytesPerRow;
const indexActual = j + rowPitch * i;
if (actual[indexActual] !== exp[indexExp]) {
if (failedPixels > 4) {
break;
}
failedPixels++;
lines.push(`at [${indexExp}], expected ${exp[indexExp]}, got ${actual[indexActual]}`);
}
}
if (failedPixels > 4) {
lines.push('... and more');
break;
}
}
return failedPixels > 0 ? lines.join('\n') : undefined;
}

// Using drawImage to extract imageBitmap content.
imageBitmapToData(imageBitmap: ImageBitmap): ArrayBufferView {
const imageCanvas = document.createElement('canvas');
imageCanvas.width = imageBitmap.width;
imageCanvas.height = imageBitmap.height;

const imageCanvasContext = imageCanvas.getContext('2d');
if (!imageCanvasContext) {
throw new Error('Cannot create canvas context for reading back contents from imageBitmap.');
}

imageCanvasContext.drawImage(imageBitmap, 0, 0, imageBitmap.width, imageBitmap.height);
return imageCanvasContext.getImageData(0, 0, imageBitmap.width, imageBitmap.height).data;
}
}
Comment thread
kainino0x marked this conversation as resolved.
Resolved

export const g = new TestGroup(F);

g.test('from image element', async t => {
const { width, height } = t.params;

// The texture format is rgba8uint, so the bytes per pixel is 4.
const bytesPerPixel = 4;

const imagePixels = new Uint8ClampedArray(bytesPerPixel * width * height);
for (let i = 0; i < width * height * bytesPerPixel; ++i) {
imagePixels[i] = i % 4 === 3 ? 255 : i % 256;
}

const imageData = new ImageData(imagePixels, width, height);
const imageBitmap = await createImageBitmap(imageData);

const dst = t.device.createTexture({
size: {
width: imageBitmap.width,
height: imageBitmap.height,
depth: 1,
},
format: 'rgba8uint',
usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
});

t.device.defaultQueue.copyImageBitmapToTexture(
{
imageBitmap,
origin: {
x: 0,
y: 0,
},
},
{
texture: dst,
},
{
width: imageBitmap.width,
height: imageBitmap.height,
depth: 1,
}
);

const data = t.imageBitmapToData(imageBitmap);

const rowPitchValue = calculateRowPitch(imageBitmap.width, bytesPerPixel);
const testBuffer = t.device.createBuffer({
size: rowPitchValue * imageBitmap.height,
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
});

const encoder = t.device.createCommandEncoder();

encoder.copyTextureToBuffer(
{ texture: dst, mipLevel: 0, origin: { x: 0, y: 0, z: 0 } },
{ buffer: testBuffer, rowPitch: rowPitchValue, imageHeight: 0 },
{ width: imageBitmap.width, height: imageBitmap.height, depth: 1 }
);
t.device.defaultQueue.submit([encoder.finish()]);

t.checkCopyImageBitmapResult(
testBuffer,
data,
imageBitmap.width,
imageBitmap.height,
bytesPerPixel
);
}).params(
pcombine(
poptions('width', [1, 2, 4, 15, 255, 256]), //
poptions('height', [1, 2, 4, 15, 255, 256])
)
);
34 changes: 27 additions & 7 deletions src/suites/cts/gpu_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,9 @@ export class GPUTest extends Fixture {
return this.device.createShaderModule({ code });
}

// TODO: add an expectContents for textures, which logs data: uris on failure

expectContents(src: GPUBuffer, expected: ArrayBufferView): void {
const exp = new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength);

const size = expected.buffer.byteLength;
createCopyForMapRead(src: GPUBuffer, size: number): GPUBuffer {
const dst = this.device.createBuffer({
size: expected.buffer.byteLength,
size,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});

Expand All @@ -103,6 +98,15 @@ export class GPUTest extends Fixture {

this.queue.submit([c.finish()]);

return dst;
}

// TODO: add an expectContents for textures, which logs data: uris on failure

expectContents(src: GPUBuffer, expected: ArrayBufferView): void {
const exp = new Uint8Array(expected.buffer, expected.byteOffset, expected.byteLength);
const dst = this.createCopyForMapRead(src, expected.buffer.byteLength);

this.eventualAsyncExpectation(async niceStack => {
const actual = new Uint8Array(await dst.mapReadAsync());
const check = this.checkBuffer(actual, exp);
Expand Down Expand Up @@ -138,6 +142,22 @@ export class GPUTest extends Fixture {
lines.push(`at [${i}], expected ${exp[i]}, got ${actual[i]}`);
}
}

// TODO: Could make a more convenient message, which could look like e.g.:
//
// Starting at offset 48,
// got 22222222 ABCDABCD 99999999
// but expected 22222222 55555555 99999999
//
// or
//
// Starting at offset 0,
// got 00000000 00000000 00000000 00000000 (... more)
// but expected 00FF00FF 00FF00FF 00FF00FF 00FF00FF (... more)
//
// Or, maybe these diffs aren't actually very useful (given we have the prints just above here),
// and we should remove them. More important will be logging of texture data in a visual format.

if (size <= 256 && failedPixels > 0) {
const expHex = Array.from(exp)
.map(x => x.toString(16).padStart(2, '0'))
Expand Down