blob: de21ad81acf2de3fc9e0534a7c085fdfc87008e3 [file] [log] [blame]
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07001export const description = `
2Examples of writing CTS tests with various features.
Kai Ninomiyacb13fba2019-10-24 16:59:36 -07003
4Start here when looking for examples of basic framework usage.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07005`;
6
Kai Ninomiyad4c4dde2020-12-08 10:54:28 -08007import { params, pbool, poptions } from '../common/framework/params_builder.js';
Kai Ninomiya9cbb8002020-05-18 15:33:41 -07008import { makeTestGroup } from '../common/framework/test_group.js';
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -07009
10import { GPUTest } from './gpu_test.js';
11
Kai Ninomiyae7561052021-04-29 15:37:09 -070012// To run these tests in the standalone runner, run `npm start` then open:
13// - http://localhost:XXXX/standalone/?runnow=1&q=webgpu:examples:
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070014// To run in WPT, copy/symlink the out-wpt/ directory as the webgpu/ directory in WPT, then open:
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070015// - (wpt server url)/webgpu/cts.html?q=webgpu:examples:
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070016//
17// Tests here can be run individually or in groups:
Kai Ninomiyae7561052021-04-29 15:37:09 -070018// - ?q=webgpu:examples:basic,async:
19// - ?q=webgpu:examples:basic,async:*
20// - ?q=webgpu:examples:basic,*
21// - ?q=webgpu:examples:*
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070022
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070023export const g = makeTestGroup(GPUTest);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070024
Kai Ninomiya20bc11b2020-03-31 14:45:44 -070025// Note: spaces in test names are replaced with underscores: webgpu:examples:test_name=
Austin Eng639527e2020-04-14 16:20:36 -070026/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070027g.test('test_name').fn(t => {});
Kai Ninomiya04ec6b62019-10-25 00:35:32 -070028
Kai Ninomiyafba26302020-11-04 13:38:05 -080029g.test('not_implemented_yet,without_plan').unimplemented();
30g.test('not_implemented_yet,with_plan')
31 .desc(
32 `
33Plan for this test. What it tests. Summary of how it tests that functionality.
34- Description of cases, by describing parameters {a, b, c}
35- x= more parameters {x, y, z}
36`
37 )
38 .unimplemented();
39
Kai Ninomiyafb584a52020-04-16 16:43:24 -070040g.test('basic').fn(t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070041 t.expect(true);
42 t.expect(true, 'true should be true');
43
44 t.shouldThrow(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070045 // The expected '.name' of the thrown error.
46 'TypeError',
47 // This function is run inline inside shouldThrow, and is expected to throw.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070048 () => {
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070049 throw new TypeError();
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070050 },
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070051 // Log message.
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070052 'function should throw Error'
53 );
54});
55
Kai Ninomiya9cbb8002020-05-18 15:33:41 -070056g.test('basic,async').fn(async t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070057 // shouldReject must be awaited to ensure it can wait for the promise before the test ends.
Kai Ninomiyac42e2982019-10-23 15:44:28 -070058 t.shouldReject(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070059 // The expected '.name' of the thrown error.
60 'TypeError',
61 // Promise expected to reject.
62 Promise.reject(new TypeError()),
63 // Log message.
64 'Promise.reject should reject'
65 );
66
67 // Promise can also be an IIFE.
Kai Ninomiyac42e2982019-10-23 15:44:28 -070068 t.shouldReject(
Kai Ninomiyabb2e71f2019-08-02 16:25:56 -070069 'TypeError',
70 (async () => {
71 throw new TypeError();
72 })(),
73 'Promise.reject should reject'
74 );
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070075});
76
Enrico Galli9bec7342021-01-27 15:58:01 -080077// A test can be parameterized with a simple array of objects using .cases().
78// Each such instance of the test is a "case".
Kai Ninomiya13820262019-10-17 15:30:56 -070079//
80// Parameters can be public (x, y) which means they're part of the case name.
81// They can also be private by starting with an underscore (_result), which passes
82// them into the test but does not make them part of the case name:
83//
Enrico Galli9bec7342021-01-27 15:58:01 -080084// In this example, the following cases are generated (identified by their "query string"):
85// - webgpu:examples:basic,cases:x=2;y=4 runs once, with t.params set to:
86// - { x: 2, y: 4, _result: 6 }
87// - webgpu:examples:basic,cases:x=-10;y=18 runs once, with t.params set to:
88// - { x: -10, y: 18, _result: 8 }
89
90g.test('basic,cases')
91 .cases([
Kai Ninomiyafb584a52020-04-16 16:43:24 -070092 { x: 2, y: 4, _result: 6 }, //
93 { x: -10, y: 18, _result: 8 },
94 ])
95 .fn(t => {
96 t.expect(t.params.x + t.params.y === t.params._result);
97 });
Kai Ninomiya13820262019-10-17 15:30:56 -070098// (note the blank comment above to enforce newlines on autoformat)
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -070099
Enrico Galli9bec7342021-01-27 15:58:01 -0800100// Each case can be further parameterized using .subcases().
101// Each such instance of the test is a "subcase", which cannot be run independently of other
102// subcases. It is analogous to wrapping the entire fn body in a for-loop.
103//
104// In this example, the following cases are generated (identified by their "query string"):
105// - webgpu:examples:basic,cases:x=1 runs twice, with t.params set to each of:
106// - { x: 1, a: 2 }
107// - { x: 1, b: 2 }
108// - webgpu:examples:basic,cases:x=2 runs twice, with t.params set to each of:
109// - { x: 2, a: 3 }
110// - { x: 2, b: 2 }
111
112g.test('basic,subcases')
113 .cases([{ x: 1 }, { x: 2 }])
114 .subcases(p => [{ a: p.x + 1 }, { b: 2 }])
115 .fn(t => {
116 t.expect(
117 ((t.params.a === 2 || t.params.a === 3) && t.params.b === undefined) ||
118 (t.params.a === undefined && t.params.b === 2)
119 );
120 });
121
Kai Ninomiyad4c4dde2020-12-08 10:54:28 -0800122// Runs the following cases:
123// { x: 2, y: 2 }
124// { x: 2, z: 3 }
125// { x: 3, y: 2 }
126// { x: 3, z: 3 }
127g.test('basic,params_builder')
Enrico Galli9bec7342021-01-27 15:58:01 -0800128 .cases(
Kai Ninomiyad4c4dde2020-12-08 10:54:28 -0800129 params()
130 .combine(poptions('x', [2, 3]))
131 .combine([{ y: 2 }, { z: 3 }])
132 )
133 .fn(() => {});
134
Kai Ninomiya9cbb8002020-05-18 15:33:41 -0700135g.test('gpu,async').fn(async t => {
Kai Ninomiya2f242cf2021-04-14 14:12:03 -0700136 const x = await t.queue.onSubmittedWorkDone();
137 t.expect(x === undefined);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700138});
139
Kai Ninomiya9cbb8002020-05-18 15:33:41 -0700140g.test('gpu,buffers').fn(async t => {
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700141 const data = new Uint32Array([0, 1234, 0]);
Kai Ninomiyabe420af2020-07-24 18:32:40 -0700142 const src = t.device.createBuffer({
143 mappedAtCreation: true,
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700144 size: 12,
145 usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
146 });
Kai Ninomiyabe420af2020-07-24 18:32:40 -0700147 new Uint32Array(src.getMappedRange()).set(data);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700148 src.unmap();
149
150 // Use the expectContents helper to check the actual contents of a GPUBuffer.
151 // Like shouldReject, it must be awaited.
Kai Ninomiyac42e2982019-10-23 15:44:28 -0700152 t.expectContents(src, data);
Kai Ninomiya8e4e5dd2019-08-02 14:00:30 -0700153});
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700154
155// One of the following two tests should be skipped on most platforms.
156
157g.test('gpu,with_texture_compression,bc')
Kai Ninomiyafeb406c2020-11-19 15:11:13 -0800158 .desc(
159 `Example of a test using a device descriptor.
160Tests that a BC format passes validation iff the feature is enabled.`
161 )
Enrico Galli9bec7342021-01-27 15:58:01 -0800162 .cases(pbool('textureCompressionBC'))
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700163 .fn(async t => {
164 const { textureCompressionBC } = t.params;
165
166 if (textureCompressionBC) {
Kai Ninomiya0d21f752021-04-14 14:41:48 -0700167 await t.selectDeviceOrSkipTestCase('texture-compression-bc');
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700168 }
169
170 const shouldError = !textureCompressionBC;
171 t.expectGPUError(
172 'validation',
173 () => {
174 t.device.createTexture({
175 format: 'bc1-rgba-unorm',
176 size: [4, 4, 1],
177 usage: GPUTextureUsage.SAMPLED,
178 });
179 },
180 shouldError
181 );
182 });
183
184g.test('gpu,with_texture_compression,etc')
Kai Ninomiyafeb406c2020-11-19 15:11:13 -0800185 .desc(
186 `Example of a test using a device descriptor.
187
188TODO: Test that an ETC format passes validation iff the feature is enabled.`
189 )
Enrico Galli9bec7342021-01-27 15:58:01 -0800190 .cases(pbool('textureCompressionETC'))
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700191 .fn(async t => {
192 const { textureCompressionETC } = t.params;
193
194 if (textureCompressionETC) {
Kai Ninomiya2f242cf2021-04-14 14:12:03 -0700195 await t.selectDeviceOrSkipTestCase('texture-compression-etc' as GPUFeatureName);
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700196 }
197
Kai Ninomiyafe0cbfa2020-09-25 10:17:24 -0700198 // TODO: Should actually test createTexture with an ETC format here.
199 });