Skip to content

Commit

Permalink
Update getCurrentTexture test to match spec.
Browse files Browse the repository at this point in the history
It's not entirely clear from the current WebGPU spec if "modified" = setting
to the same dimensions. It seems like that shouldn't matter and is inconsistent with
other canvas contents.
  • Loading branch information
greggman committed Jan 26, 2024
1 parent a9f5d30 commit e5c09e6
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/webgpu/web_platform/canvas/getCurrentTexture.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class GPUContextTest extends GPUTest {

return ctx;
}

expectTextureDestroyed(texture: GPUTexture, expectDestroyed = true) {
this.expectValidationError(() => {
// Try using the texture in a render pass. Because it's a canvas texture
// it should have RENDER_ATTACHMENT usage.
assert((texture.usage & GPUTextureUsage.RENDER_ATTACHMENT) !== 0);
const encoder = this.device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [
{
view: texture.createView(),
loadOp: 'clear',
storeOp: 'store',
},
],
});
pass.end();
// Submitting should generate a validation error if the texture is destroyed.
this.queue.submit([encoder.finish()]);
}, expectDestroyed);
}
}

export const g = makeTestGroup(GPUContextTest);
Expand Down Expand Up @@ -235,6 +256,8 @@ g.test('resize')
// Trigger a resize by changing the width.
ctx.canvas.width = 4;

t.expectTextureDestroyed(prevTexture);

// When the canvas resizes the texture returned by getCurrentTexture should immediately begin
// returning a new texture matching the update dimensions.
let currentTexture = ctx.getCurrentTexture();
Expand Down Expand Up @@ -263,21 +286,35 @@ g.test('resize')
t.expect(currentTexture.height === ctx.canvas.height);
t.expect(prevTexture.width === 4);
t.expect(prevTexture.height === 2);
prevTexture = currentTexture;

// Ensure that texture contents are transparent black.
t.expectSingleColor(currentTexture, currentTexture.format, {
size: [currentTexture.width, currentTexture.height, 1],
exp: { R: 0, G: 0, B: 0, A: 0 },
});

// Simply setting the canvas width and height values to their current values should not trigger
// a change in the texture.
ctx.canvas.width = 4;
ctx.canvas.height = 4;
// Ensure canvas goes back to defaults when set to negative numbers.
ctx.canvas.width = -1;
currentTexture = ctx.getCurrentTexture();
t.expect(currentTexture.width === 300);
t.expect(currentTexture.height === 4);

ctx.canvas.height = -1;
currentTexture = ctx.getCurrentTexture();
t.expect(prevTexture === currentTexture);
t.expect(currentTexture.width === 300);
t.expect(currentTexture.height === 150);
prevTexture = currentTexture;

// Setting the canvas width and height values to their current values should
// still trigger a change in the texture.
const { width, height } = ctx.canvas;
ctx.canvas.width = width;
ctx.canvas.height = height;

t.expectTextureDestroyed(prevTexture);

currentTexture = ctx.getCurrentTexture();
t.expect(prevTexture !== currentTexture);
});

g.test('expiry')
Expand Down

0 comments on commit e5c09e6

Please sign in to comment.