Skip to content

Commit

Permalink
Add some tests for GPUCanvasContext getConfiguration (#3973)
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois authored Oct 1, 2024
1 parent 9d029d3 commit c42a89c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/w3c-image-capture": "^1.0.10",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"@webgpu/types": "^0.1.46",
"@webgpu/types": "^0.1.47",
"ansi-colors": "4.1.3",
"babel-plugin-add-header-comment": "^1.0.3",
"babel-plugin-const-enum": "^1.2.0",
Expand Down
55 changes: 54 additions & 1 deletion src/webgpu/web_platform/canvas/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Tests for GPUCanvasContext.configure.
TODO:
- Test colorSpace
- Test viewFormats
- Test toneMapping
`;

import { makeTestGroup } from '../../../common/framework/test_group.js';
Expand Down Expand Up @@ -42,6 +42,16 @@ g.test('defaults')
format: 'rgba8unorm',
});

const configuration = ctx.getConfiguration();
assert(configuration !== null);
t.expect(configuration.device === t.device);
t.expect(configuration.format === 'rgba8unorm');
t.expect(configuration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect((configuration.viewFormats as GPUTextureFormat[]).length === 0);
t.expect(configuration.colorSpace === 'srgb');
t.expect(configuration.toneMapping!.mode === 'standard');
t.expect(configuration.alphaMode === 'opaque');

const currentTexture = ctx.getCurrentTexture();
t.expect(currentTexture.format === 'rgba8unorm');
t.expect(currentTexture.usage === GPUTextureUsage.RENDER_ATTACHMENT);
Expand Down Expand Up @@ -69,6 +79,9 @@ g.test('device')
const ctx = canvas.getContext('webgpu');
assert(ctx instanceof GPUCanvasContext, 'Failed to get WebGPU context from canvas');

// getConfiguration returns null before configure.
t.expect(ctx.getConfiguration() === null);

// Calling configure without a device should throw a TypeError.
t.shouldThrow('TypeError', () => {
ctx.configure({
Expand All @@ -85,8 +98,20 @@ g.test('device')
ctx.configure({
device: t.device,
format: 'rgba8unorm',
alphaMode: 'opaque',
});

// getConfiguration will succeed after configure.
const configuration = ctx.getConfiguration();
assert(configuration !== null);
t.expect(configuration.device === t.device);
t.expect(configuration.format === 'rgba8unorm');
t.expect(configuration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect((configuration.viewFormats as GPUTextureFormat[]).length === 0);
t.expect(configuration.colorSpace === 'srgb');
t.expect(configuration.toneMapping!.mode === 'standard');
t.expect(configuration.alphaMode === 'opaque');

// getCurrentTexture will succeed with a valid device.
ctx.getCurrentTexture();

Expand All @@ -96,12 +121,27 @@ g.test('device')
ctx.getCurrentTexture();
});

// getConfiguration returns null after unconfigure.
t.expect(ctx.getConfiguration() === null);

// Should be able to successfully configure again after unconfiguring.
ctx.configure({
device: t.device,
format: 'rgba8unorm',
alphaMode: 'premultiplied',
});
ctx.getCurrentTexture();

// getConfiguration will succeed after configure.
const newConfiguration = ctx.getConfiguration();
assert(newConfiguration !== null);
t.expect(newConfiguration.device === t.device);
t.expect(newConfiguration.format === 'rgba8unorm');
t.expect(newConfiguration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect((newConfiguration.viewFormats as GPUTextureFormat[]).length === 0);
t.expect(newConfiguration.colorSpace === 'srgb');
t.expect(newConfiguration.toneMapping!.mode === 'standard');
t.expect(newConfiguration.alphaMode === 'premultiplied');
});

g.test('format')
Expand Down Expand Up @@ -140,6 +180,9 @@ g.test('format')
});
}, !validFormat);

const configuration = ctx.getConfiguration();
t.expect(configuration!.format === format);

t.expectValidationError(() => {
// Should always return a texture, whether the configured format was valid or not.
const currentTexture = ctx.getCurrentTexture();
Expand Down Expand Up @@ -179,6 +222,9 @@ g.test('usage')
usage,
});

const configuration = ctx.getConfiguration();
t.expect(configuration!.usage === usage);

const currentTexture = ctx.getCurrentTexture();
t.expect(currentTexture instanceof GPUTexture);
t.expect(currentTexture.usage === usage);
Expand Down Expand Up @@ -289,6 +335,9 @@ g.test('alpha_mode')
alphaMode,
});

const configuration = ctx.getConfiguration();
t.expect(configuration!.alphaMode === alphaMode);

const currentTexture = ctx.getCurrentTexture();
t.expect(currentTexture instanceof GPUTexture);
});
Expand Down Expand Up @@ -412,6 +461,10 @@ g.test('viewFormats')
});
}, !compatible);

const viewFormats = ctx.getConfiguration()!.viewFormats!;
assert(Array.isArray(viewFormats));
t.expect(viewFormats[0] === viewFormat);

// Likewise for getCurrentTexture().
let currentTexture: GPUTexture;
t.expectValidationError(() => {
Expand Down

0 comments on commit c42a89c

Please sign in to comment.