|
| 1 | +<script src="../../../../resources/js-test.js"></script> |
| 2 | +<script> |
| 3 | +jsTestIsAsync = true; |
| 4 | + |
| 5 | +// https://www.w3.org/TR/WGSL/#scope: the scope of a function-scope declaration starts just after |
| 6 | +// the declaration, so its own initializer resolves names against the enclosing scope. Two of WGSL's |
| 7 | +// visitors introduced the name before visiting the declaration, which made a module-scope |
| 8 | +// declaration invisible to them whenever a local shadowed it. This test covers the two |
| 9 | +// consequences that are not about resource bindings; repro_186926179.html covers that one. |
| 10 | + |
| 11 | +// The `w` in the initializer is the module-scope workgroup variable, so this vertex entry point uses |
| 12 | +// workgroup storage and has to be rejected. It used to be accepted, and the emitted Metal declared |
| 13 | +// a threadgroup variable and a threadgroup_barrier inside a [[vertex]] function, which the Metal |
| 14 | +// compiler rejects. |
| 15 | +const workgroupInVertex = ` |
| 16 | +var<workgroup> w : u32; |
| 17 | +@vertex fn main() -> @builtin(position) vec4<f32> { |
| 18 | + var w : u32 = w; |
| 19 | + return vec4<f32>(f32(w)); |
| 20 | +}`; |
| 21 | + |
| 22 | +// The same reference without the shadowing local. This was always rejected, so it shows the check |
| 23 | +// itself is present rather than the test passing because every module is refused. |
| 24 | +const workgroupInVertexUnshadowed = ` |
| 25 | +var<workgroup> w : u32; |
| 26 | +@vertex fn main() -> @builtin(position) vec4<f32> { |
| 27 | + let v : u32 = w; |
| 28 | + return vec4<f32>(f32(v)); |
| 29 | +}`; |
| 30 | + |
| 31 | +// A compute entry point may use workgroup storage, shadowing local or not. |
| 32 | +const workgroupInCompute = ` |
| 33 | +var<workgroup> w : u32; |
| 34 | +@group(0) @binding(0) var<storage, read_write> out : array<u32>; |
| 35 | +@compute @workgroup_size(1) fn main() { |
| 36 | + var w : u32 = w; |
| 37 | + out[0] = w; |
| 38 | +}`; |
| 39 | + |
| 40 | +// `g` in the initializer is the module-scope function, so `f` depends on `g` and `g` has to be |
| 41 | +// sorted before it. The dependency edge used to be dropped, `g` stayed after `f`, and this valid |
| 42 | +// module was rejected with "unresolved call target 'g'". |
| 43 | +const forwardDependency = ` |
| 44 | +fn f() -> u32 { var g : u32 = g(); return g; } |
| 45 | +fn g() -> u32 { return 7u; } |
| 46 | +@compute @workgroup_size(1) fn main() { _ = f(); }`; |
| 47 | + |
| 48 | +// The same module with `g` already ahead of `f`, which compiled either way: it isolates the failure |
| 49 | +// above to the sorting rather than to name resolution. |
| 50 | +const forwardDependencyPresorted = ` |
| 51 | +fn g() -> u32 { return 7u; } |
| 52 | +fn f() -> u32 { var g : u32 = g(); return g; } |
| 53 | +@compute @workgroup_size(1) fn main() { _ = f(); }`; |
| 54 | + |
| 55 | +// The same lost edge for a module-scope variable rather than a function. This was rejected with |
| 56 | +// "unresolved identifier 'g'". |
| 57 | +const forwardVariableDependency = ` |
| 58 | +fn f() -> u32 { var g : u32 = g; return g; } |
| 59 | +var<private> g : u32 = 7u; |
| 60 | +@compute @workgroup_size(1) fn main() { _ = f(); }`; |
| 61 | + |
| 62 | +let device; |
| 63 | + |
| 64 | +async function compile(code) |
| 65 | +{ |
| 66 | + device.pushErrorScope('validation'); |
| 67 | + const module = device.createShaderModule({ code }); |
| 68 | + const messages = (await module.getCompilationInfo()).messages; |
| 69 | + const error = await device.popErrorScope(); |
| 70 | + const errors = messages.filter(message => message.type === 'error').map(message => message.message); |
| 71 | + if (error && !errors.length) |
| 72 | + errors.push(error.message); |
| 73 | + return errors; |
| 74 | +} |
| 75 | + |
| 76 | +async function shouldReject(description, code, expectedSubstring) |
| 77 | +{ |
| 78 | + const errors = await compile(code); |
| 79 | + if (errors.some(message => message.includes(expectedSubstring))) |
| 80 | + testPassed(description); |
| 81 | + else |
| 82 | + testFailed(`${description}: expected an error containing "${expectedSubstring}", got ${JSON.stringify(errors)}`); |
| 83 | +} |
| 84 | + |
| 85 | +async function shouldCompile(description, code) |
| 86 | +{ |
| 87 | + const errors = await compile(code); |
| 88 | + if (!errors.length) |
| 89 | + testPassed(description); |
| 90 | + else |
| 91 | + testFailed(`${description}: ${JSON.stringify(errors)}`); |
| 92 | +} |
| 93 | + |
| 94 | +onload = async () => { |
| 95 | + try { |
| 96 | + const adapter = await navigator.gpu.requestAdapter(); |
| 97 | + device = await adapter.requestDevice(); |
| 98 | + |
| 99 | + const workgroupInVertexError = 'cannot be used by `vertex` pipeline stage'; |
| 100 | + await shouldReject('a shadowing local does not hide workgroup storage from a vertex entry point', |
| 101 | + workgroupInVertex, workgroupInVertexError); |
| 102 | + await shouldReject('workgroup storage in a vertex entry point is rejected without shadowing', |
| 103 | + workgroupInVertexUnshadowed, workgroupInVertexError); |
| 104 | + await shouldCompile('workgroup storage in a compute entry point still compiles', |
| 105 | + workgroupInCompute); |
| 106 | + |
| 107 | + await shouldCompile('a shadowing local does not drop the dependency on the declaration it shadows', |
| 108 | + forwardDependency); |
| 109 | + await shouldCompile('the same module compiles when the dependency is already declared first', |
| 110 | + forwardDependencyPresorted); |
| 111 | + await shouldCompile('a shadowing local does not drop the dependency on a later module-scope variable', |
| 112 | + forwardVariableDependency); |
| 113 | + } catch (e) { |
| 114 | + testFailed(`${e}`); |
| 115 | + } |
| 116 | + finishJSTest(); |
| 117 | +}; |
| 118 | +</script> |
0 commit comments