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