Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 1 | export const description = ` |
| 2 | Examples of writing CTS tests with various features. |
Kai Ninomiya | cb13fba | 2019-10-24 16:59:36 -0700 | [diff] [blame] | 3 | |
| 4 | Start here when looking for examples of basic framework usage. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 5 | `; |
| 6 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 7 | import { makeTestGroup } from '../common/framework/test_group.js'; |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 8 | |
| 9 | import { GPUTest } from './gpu_test.js'; |
| 10 | |
Kai Ninomiya | e756105 | 2021-04-29 15:37:09 -0700 | [diff] [blame] | 11 | // To run these tests in the standalone runner, run `npm start` then open: |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 12 | // - http://localhost:XXXX/standalone/?runnow=1&q=webgpu:examples:* |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 13 | // To run in WPT, copy/symlink the out-wpt/ directory as the webgpu/ directory in WPT, then open: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 14 | // - (wpt server url)/webgpu/cts.html?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 15 | // |
| 16 | // Tests here can be run individually or in groups: |
Kai Ninomiya | e756105 | 2021-04-29 15:37:09 -0700 | [diff] [blame] | 17 | // - ?q=webgpu:examples:basic,async: |
| 18 | // - ?q=webgpu:examples:basic,async:* |
| 19 | // - ?q=webgpu:examples:basic,* |
| 20 | // - ?q=webgpu:examples:* |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 21 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 22 | export const g = makeTestGroup(GPUTest); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 23 | |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 24 | // Note: spaces in test names are replaced with underscores: webgpu:examples:test_name= |
Austin Eng | 639527e | 2020-04-14 16:20:36 -0700 | [diff] [blame] | 25 | /* eslint-disable-next-line @typescript-eslint/no-unused-vars */ |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 26 | g.test('test_name').fn(t => {}); |
Kai Ninomiya | 04ec6b6 | 2019-10-25 00:35:32 -0700 | [diff] [blame] | 27 | |
Kai Ninomiya | fba2630 | 2020-11-04 13:38:05 -0800 | [diff] [blame] | 28 | g.test('not_implemented_yet,without_plan').unimplemented(); |
| 29 | g.test('not_implemented_yet,with_plan') |
| 30 | .desc( |
| 31 | ` |
| 32 | Plan for this test. What it tests. Summary of how it tests that functionality. |
| 33 | - Description of cases, by describing parameters {a, b, c} |
| 34 | - x= more parameters {x, y, z} |
| 35 | ` |
| 36 | ) |
| 37 | .unimplemented(); |
| 38 | |
Kai Ninomiya | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 39 | g.test('basic').fn(t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 40 | t.expect(true); |
| 41 | t.expect(true, 'true should be true'); |
| 42 | |
| 43 | t.shouldThrow( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 44 | // The expected '.name' of the thrown error. |
| 45 | 'TypeError', |
| 46 | // This function is run inline inside shouldThrow, and is expected to throw. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 47 | () => { |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 48 | throw new TypeError(); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 49 | }, |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 50 | // Log message. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 51 | 'function should throw Error' |
| 52 | ); |
| 53 | }); |
| 54 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 55 | g.test('basic,async').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 56 | // shouldReject must be awaited to ensure it can wait for the promise before the test ends. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 57 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 58 | // The expected '.name' of the thrown error. |
| 59 | 'TypeError', |
| 60 | // Promise expected to reject. |
| 61 | Promise.reject(new TypeError()), |
| 62 | // Log message. |
| 63 | 'Promise.reject should reject' |
| 64 | ); |
| 65 | |
| 66 | // Promise can also be an IIFE. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 67 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 68 | 'TypeError', |
| 69 | (async () => { |
| 70 | throw new TypeError(); |
| 71 | })(), |
| 72 | 'Promise.reject should reject' |
| 73 | ); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 74 | }); |
| 75 | |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 76 | g.test('basic,plain_cases') |
| 77 | .desc( |
| 78 | ` |
| 79 | A test can be parameterized with a simple array of objects using .paramsSimple([ ... ]). |
| 80 | Each such instance of the test is a "case". |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 81 | |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 82 | In this example, the following cases are generated (identified by their "query string"), |
| 83 | each with just one subcase: |
| 84 | - webgpu:examples:basic,cases:x=2;y=2 runs 1 subcase, with t.params set to: |
| 85 | - { x: 2, y: 2 } |
| 86 | - webgpu:examples:basic,cases:x=-10;y=-10 runs 1 subcase, with t.params set to: |
| 87 | - { x: -10, y: -10 } |
| 88 | ` |
| 89 | ) |
| 90 | .paramsSimple([ |
| 91 | { x: 2, y: 2 }, // |
| 92 | { x: -10, y: -10 }, |
| 93 | ]) |
| 94 | .fn(t => { |
| 95 | t.expect(t.params.x === t.params.y); |
| 96 | }); |
| 97 | |
| 98 | g.test('basic,plain_cases_private') |
| 99 | .desc( |
| 100 | ` |
| 101 | Parameters can be public ("x", "y") which means they're part of the case name. |
| 102 | They can also be private by starting with an underscore ("_result"), which passes |
| 103 | them into the test but does not make them part of the case name: |
| 104 | |
| 105 | In this example, the following cases are generated, each with just one subcase: |
| 106 | - webgpu:examples:basic,cases:x=2;y=4 runs 1 subcase, with t.params set to: |
| 107 | - { x: 2, y: 4, _result: 6 } |
| 108 | - webgpu:examples:basic,cases:x=-10;y=18 runs 1 subcase, with t.params set to: |
| 109 | - { x: -10, y: 18, _result: 8 } |
| 110 | ` |
| 111 | ) |
| 112 | .paramsSimple([ |
Kai Ninomiya | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 113 | { x: 2, y: 4, _result: 6 }, // |
| 114 | { x: -10, y: 18, _result: 8 }, |
| 115 | ]) |
| 116 | .fn(t => { |
| 117 | t.expect(t.params.x + t.params.y === t.params._result); |
| 118 | }); |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 119 | // (note the blank comment above to enforce newlines on autoformat) |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 120 | |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 121 | g.test('basic,builder_cases') |
| 122 | .desc( |
| 123 | ` |
| 124 | A "CaseParamsBuilder" or "SubcaseParamsBuilder" can be passed to .params() instead. |
| 125 | The params builder provides facilities for generating tests combinatorially (by cartesian |
| 126 | product). For convenience, the "unit" CaseParamsBuilder is passed as an argument ("u" below). |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 127 | |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 128 | In this example, the following cases are generated, each with just one subcase: |
| 129 | - webgpu:examples:basic,cases:x=1,y=1 runs 1 subcase, with t.params set to: |
| 130 | - { x: 1, y: 1 } |
| 131 | - webgpu:examples:basic,cases:x=1,y=2 runs 1 subcase, with t.params set to: |
| 132 | - { x: 1, y: 2 } |
| 133 | - webgpu:examples:basic,cases:x=2,y=1 runs 1 subcase, with t.params set to: |
| 134 | - { x: 2, y: 1 } |
| 135 | - webgpu:examples:basic,cases:x=2,y=2 runs 1 subcase, with t.params set to: |
| 136 | - { x: 2, y: 2 } |
| 137 | ` |
| 138 | ) |
| 139 | .params(u => |
| 140 | u // |
| 141 | .combineWithParams([{ x: 1 }, { x: 2 }]) |
| 142 | .combineWithParams([{ y: 1 }, { y: 2 }]) |
| 143 | ) |
| 144 | .fn(() => {}); |
Enrico Galli | 9bec734 | 2021-01-27 15:58:01 -0800 | [diff] [blame] | 145 | |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 146 | g.test('basic,builder_cases_subcases') |
| 147 | .desc( |
| 148 | ` |
| 149 | Each case sub-parameterized using .beginSubcases(). |
| 150 | Each such instance of the test is a "subcase", which cannot be run independently of other |
| 151 | subcases. It is somewhat like wrapping the entire fn body in a for-loop. |
| 152 | |
| 153 | In this example, the following cases are generated: |
| 154 | - webgpu:examples:basic,cases:x=1 runs 2 subcases, with t.params set to: |
| 155 | - { x: 1, y: 1 } |
| 156 | - { x: 1, y: 2 } |
| 157 | - webgpu:examples:basic,cases:x=2 runs 2 subcases, with t.params set to: |
| 158 | - { x: 2, y: 1 } |
| 159 | - { x: 2, y: 2 } |
| 160 | ` |
| 161 | ) |
| 162 | .params(u => |
| 163 | u // |
| 164 | .combineWithParams([{ x: 1 }, { x: 2 }]) |
| 165 | .beginSubcases() |
| 166 | .combineWithParams([{ y: 1 }, { y: 2 }]) |
| 167 | ) |
| 168 | .fn(() => {}); |
| 169 | |
| 170 | g.test('basic,builder_subcases') |
| 171 | .desc( |
| 172 | ` |
| 173 | In this example, the following single case is generated: |
| 174 | - webgpu:examples:basic,cases: runs 4 subcases, with t.params set to: |
| 175 | - { x: 1, y: 1 } |
| 176 | - { x: 1, y: 2 } |
| 177 | - { x: 2, y: 1 } |
| 178 | - { x: 2, y: 2 } |
| 179 | ` |
| 180 | ) |
| 181 | .params(u => |
| 182 | u // |
| 183 | .beginSubcases() |
| 184 | .combineWithParams([{ x: 1 }, { x: 2 }]) |
| 185 | .combineWithParams([{ y: 1 }, { y: 2 }]) |
| 186 | ) |
| 187 | .fn(() => {}); |
| 188 | |
| 189 | g.test('basic,builder_subcases_short') |
| 190 | .desc( |
| 191 | ` |
| 192 | As a shorthand, .paramsSubcasesOnly() can be used. |
| 193 | |
| 194 | In this example, the following single case is generated: |
| 195 | - webgpu:examples:basic,cases: runs 4 subcases, with t.params set to: |
| 196 | - { x: 1, y: 1 } |
| 197 | - { x: 1, y: 2 } |
| 198 | - { x: 2, y: 1 } |
| 199 | - { x: 2, y: 2 } |
| 200 | ` |
| 201 | ) |
| 202 | .paramsSubcasesOnly(u => |
| 203 | u // |
| 204 | .combineWithParams([{ x: 1 }, { x: 2 }]) |
| 205 | .combineWithParams([{ y: 1 }, { y: 2 }]) |
Kai Ninomiya | d4c4dde | 2020-12-08 10:54:28 -0800 | [diff] [blame] | 206 | ) |
| 207 | .fn(() => {}); |
| 208 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 209 | g.test('gpu,async').fn(async t => { |
Kai Ninomiya | 2f242cf | 2021-04-14 14:12:03 -0700 | [diff] [blame] | 210 | const x = await t.queue.onSubmittedWorkDone(); |
| 211 | t.expect(x === undefined); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 212 | }); |
| 213 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 214 | g.test('gpu,buffers').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 215 | const data = new Uint32Array([0, 1234, 0]); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 216 | const src = t.device.createBuffer({ |
| 217 | mappedAtCreation: true, |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 218 | size: 12, |
| 219 | usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, |
| 220 | }); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 221 | new Uint32Array(src.getMappedRange()).set(data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 222 | src.unmap(); |
| 223 | |
| 224 | // Use the expectContents helper to check the actual contents of a GPUBuffer. |
| 225 | // Like shouldReject, it must be awaited. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 226 | t.expectContents(src, data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 227 | }); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 228 | |
| 229 | // One of the following two tests should be skipped on most platforms. |
| 230 | |
| 231 | g.test('gpu,with_texture_compression,bc') |
Kai Ninomiya | feb406c | 2020-11-19 15:11:13 -0800 | [diff] [blame] | 232 | .desc( |
| 233 | `Example of a test using a device descriptor. |
| 234 | Tests that a BC format passes validation iff the feature is enabled.` |
| 235 | ) |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 236 | .params(u => u.combine('textureCompressionBC', [false, true])) |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 237 | .fn(async t => { |
| 238 | const { textureCompressionBC } = t.params; |
| 239 | |
| 240 | if (textureCompressionBC) { |
Kai Ninomiya | 0d21f75 | 2021-04-14 14:41:48 -0700 | [diff] [blame] | 241 | await t.selectDeviceOrSkipTestCase('texture-compression-bc'); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | const shouldError = !textureCompressionBC; |
| 245 | t.expectGPUError( |
| 246 | 'validation', |
| 247 | () => { |
| 248 | t.device.createTexture({ |
| 249 | format: 'bc1-rgba-unorm', |
| 250 | size: [4, 4, 1], |
| 251 | usage: GPUTextureUsage.SAMPLED, |
| 252 | }); |
| 253 | }, |
| 254 | shouldError |
| 255 | ); |
| 256 | }); |
| 257 | |
| 258 | g.test('gpu,with_texture_compression,etc') |
Kai Ninomiya | feb406c | 2020-11-19 15:11:13 -0800 | [diff] [blame] | 259 | .desc( |
| 260 | `Example of a test using a device descriptor. |
| 261 | |
| 262 | TODO: Test that an ETC format passes validation iff the feature is enabled.` |
| 263 | ) |
Kai Ninomiya | 119b953 | 2021-06-03 19:55:56 -0700 | [diff] [blame^] | 264 | .params(u => u.combine('textureCompressionETC', [false, true])) |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 265 | .fn(async t => { |
| 266 | const { textureCompressionETC } = t.params; |
| 267 | |
| 268 | if (textureCompressionETC) { |
Kai Ninomiya | 2f242cf | 2021-04-14 14:12:03 -0700 | [diff] [blame] | 269 | await t.selectDeviceOrSkipTestCase('texture-compression-etc' as GPUFeatureName); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 272 | // TODO: Should actually test createTexture with an ETC format here. |
| 273 | }); |