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 | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 7 | import { pbool } from '../common/framework/params_builder.js'; |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 8 | import { makeTestGroup } from '../common/framework/test_group.js'; |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 9 | |
| 10 | import { GPUTest } from './gpu_test.js'; |
| 11 | |
| 12 | // To run these tests in the standalone runner, run `grunt build` or `grunt pre` then open: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 13 | // - http://localhost:8080/?runnow=1&q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 14 | // 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] | 15 | // - (wpt server url)/webgpu/cts.html?q=webgpu:examples: |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 16 | // |
| 17 | // Tests here can be run individually or in groups: |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 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 | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 28 | g.test('basic').fn(t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 29 | t.expect(true); |
| 30 | t.expect(true, 'true should be true'); |
| 31 | |
| 32 | t.shouldThrow( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 33 | // The expected '.name' of the thrown error. |
| 34 | 'TypeError', |
| 35 | // This function is run inline inside shouldThrow, and is expected to throw. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 36 | () => { |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 37 | throw new TypeError(); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 38 | }, |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 39 | // Log message. |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 40 | 'function should throw Error' |
| 41 | ); |
| 42 | }); |
| 43 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 44 | g.test('basic,async').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 45 | // 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] | 46 | t.shouldReject( |
Kai Ninomiya | bb2e71f | 2019-08-02 16:25:56 -0700 | [diff] [blame] | 47 | // The expected '.name' of the thrown error. |
| 48 | 'TypeError', |
| 49 | // Promise expected to reject. |
| 50 | Promise.reject(new TypeError()), |
| 51 | // Log message. |
| 52 | 'Promise.reject should reject' |
| 53 | ); |
| 54 | |
| 55 | // Promise can also be an IIFE. |
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 | 'TypeError', |
| 58 | (async () => { |
| 59 | throw new TypeError(); |
| 60 | })(), |
| 61 | 'Promise.reject should reject' |
| 62 | ); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 63 | }); |
| 64 | |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 65 | // A test can be parameterized with a simple array of objects. |
| 66 | // |
| 67 | // Parameters can be public (x, y) which means they're part of the case name. |
| 68 | // They can also be private by starting with an underscore (_result), which passes |
| 69 | // them into the test but does not make them part of the case name: |
| 70 | // |
Kai Ninomiya | 20bc11b | 2020-03-31 14:45:44 -0700 | [diff] [blame] | 71 | // - webgpu:examples:basic/params={"x":2,"y":4} runs with t.params = {x: 2, y: 5, _result: 6}. |
| 72 | // - webgpu:examples:basic/params={"x":-10,"y":18} runs with t.params = {x: -10, y: 18, _result: 8}. |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 73 | g.test('basic,params') |
Kai Ninomiya | fb584a5 | 2020-04-16 16:43:24 -0700 | [diff] [blame] | 74 | .params([ |
| 75 | { x: 2, y: 4, _result: 6 }, // |
| 76 | { x: -10, y: 18, _result: 8 }, |
| 77 | ]) |
| 78 | .fn(t => { |
| 79 | t.expect(t.params.x + t.params.y === t.params._result); |
| 80 | }); |
Kai Ninomiya | 1382026 | 2019-10-17 15:30:56 -0700 | [diff] [blame] | 81 | // (note the blank comment above to enforce newlines on autoformat) |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 82 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 83 | g.test('gpu,async').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 84 | const fence = t.queue.createFence(); |
| 85 | t.queue.signal(fence, 2); |
| 86 | await fence.onCompletion(1); |
| 87 | t.expect(fence.getCompletedValue() === 2); |
| 88 | }); |
| 89 | |
Kai Ninomiya | 9cbb800 | 2020-05-18 15:33:41 -0700 | [diff] [blame] | 90 | g.test('gpu,buffers').fn(async t => { |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 91 | const data = new Uint32Array([0, 1234, 0]); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 92 | const src = t.device.createBuffer({ |
| 93 | mappedAtCreation: true, |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 94 | size: 12, |
| 95 | usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST, |
| 96 | }); |
Kai Ninomiya | be420af | 2020-07-24 18:32:40 -0700 | [diff] [blame] | 97 | new Uint32Array(src.getMappedRange()).set(data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 98 | src.unmap(); |
| 99 | |
| 100 | // Use the expectContents helper to check the actual contents of a GPUBuffer. |
| 101 | // Like shouldReject, it must be awaited. |
Kai Ninomiya | c42e298 | 2019-10-23 15:44:28 -0700 | [diff] [blame] | 102 | t.expectContents(src, data); |
Kai Ninomiya | 8e4e5dd | 2019-08-02 14:00:30 -0700 | [diff] [blame] | 103 | }); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 104 | |
| 105 | // One of the following two tests should be skipped on most platforms. |
| 106 | |
| 107 | g.test('gpu,with_texture_compression,bc') |
| 108 | .params(pbool('textureCompressionBC')) |
| 109 | .fn(async t => { |
| 110 | const { textureCompressionBC } = t.params; |
| 111 | |
| 112 | if (textureCompressionBC) { |
Kai Ninomiya | 001316a | 2020-09-29 09:47:08 -0700 | [diff] [blame^] | 113 | await t.selectDeviceOrSkipTestCase({ extensions: ['texture-compression-bc'] }); |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | const shouldError = !textureCompressionBC; |
| 117 | t.expectGPUError( |
| 118 | 'validation', |
| 119 | () => { |
| 120 | t.device.createTexture({ |
| 121 | format: 'bc1-rgba-unorm', |
| 122 | size: [4, 4, 1], |
| 123 | usage: GPUTextureUsage.SAMPLED, |
| 124 | }); |
| 125 | }, |
| 126 | shouldError |
| 127 | ); |
| 128 | }); |
| 129 | |
| 130 | g.test('gpu,with_texture_compression,etc') |
| 131 | .params(pbool('textureCompressionETC')) |
| 132 | .fn(async t => { |
| 133 | const { textureCompressionETC } = t.params; |
| 134 | |
| 135 | if (textureCompressionETC) { |
Kai Ninomiya | 001316a | 2020-09-29 09:47:08 -0700 | [diff] [blame^] | 136 | await t.selectDeviceOrSkipTestCase({ |
Kai Ninomiya | fe0cbfa | 2020-09-25 10:17:24 -0700 | [diff] [blame] | 137 | extensions: ['texture-compression-etc' as GPUExtensionName], |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | t.device; |
| 142 | // TODO: Should actually test createTexture with an ETC format here. |
| 143 | }); |