Skip to content

Commit

Permalink
Add some tests for GPUCanvasContext.getConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Sep 30, 2024
1 parent 3f6f6b7 commit 9d9a458
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
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
49 changes: 48 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,15 @@ g.test('defaults')
format: 'rgba8unorm',
});

const configuration = ctx.getConfiguration();
t.expect(configuration.device === t.device);
t.expect(configuration.format === 'rgba8unorm');
t.expect(configuration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect(configuration.viewFormats?.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 @@ -85,8 +94,19 @@ g.test('device')
ctx.configure({
device: t.device,
format: 'rgba8unorm',
alphaMode: 'opaque',
});

// getConfiguration will succeed after configure.
const configuration = ctx.getConfiguration();
t.expect(configuration.device === t.device);
t.expect(configuration.format === 'rgba8unorm');
t.expect(configuration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect(configuration.viewFormats?.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 +116,26 @@ g.test('device')
ctx.getCurrentTexture();
});

// getConfiguration returns null after unconfiguring.
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();
t.expect(newConfiguration.device === t.device);
t.expect(newConfiguration.format === 'rgba8unorm');
t.expect(newConfiguration.usage === GPUTextureUsage.RENDER_ATTACHMENT);
t.expect(newConfiguration.viewFormats?.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 +174,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 +216,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 +329,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 +455,10 @@ g.test('viewFormats')
});
}, !compatible);

const configuration = ctx.getConfiguration();
t.expect(configuration.viewFormats?.length === 1);
t.expect(configuration.viewFormats[0] === viewFormat);

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

0 comments on commit 9d9a458

Please sign in to comment.