summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/shader/validation/shader_io/builtins.spec.ts
blob: 4b32d055394ec3e745f26dd6db8d5f01ac5bf4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
export const description = `Validation tests for entry point built-in variables`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { ShaderValidationTest } from '../shader_validation_test.js';

import { generateShader } from './util.js';

export const g = makeTestGroup(ShaderValidationTest);

// List of all built-in variables and their stage, in|out usage, and type.
// Taken from table in Section 15:
// https://www.w3.org/TR/2021/WD-WGSL-20211013/#builtin-variables
export const kBuiltins = [
  { name: 'vertex_index', stage: 'vertex', io: 'in', type: 'u32' },
  { name: 'instance_index', stage: 'vertex', io: 'in', type: 'u32' },
  { name: 'position', stage: 'vertex', io: 'out', type: 'vec4<f32>' },
  { name: 'position', stage: 'fragment', io: 'in', type: 'vec4<f32>' },
  { name: 'front_facing', stage: 'fragment', io: 'in', type: 'bool' },
  { name: 'frag_depth', stage: 'fragment', io: 'out', type: 'f32' },
  { name: 'local_invocation_id', stage: 'compute', io: 'in', type: 'vec3<u32>' },
  { name: 'local_invocation_index', stage: 'compute', io: 'in', type: 'u32' },
  { name: 'global_invocation_id', stage: 'compute', io: 'in', type: 'vec3<u32>' },
  { name: 'workgroup_id', stage: 'compute', io: 'in', type: 'vec3<u32>' },
  { name: 'num_workgroups', stage: 'compute', io: 'in', type: 'vec3<u32>' },
  { name: 'sample_index', stage: 'fragment', io: 'in', type: 'u32' },
  { name: 'sample_mask', stage: 'fragment', io: 'in', type: 'u32' },
  { name: 'sample_mask', stage: 'fragment', io: 'out', type: 'u32' },
] as const;

// List of types to test against.
const kTestTypes = [
  'bool',
  'u32',
  'i32',
  'f32',
  'vec2<bool>',
  'vec2<u32>',
  'vec2<i32>',
  'vec2<f32>',
  'vec3<bool>',
  'vec3<u32>',
  'vec3<i32>',
  'vec3<f32>',
  'vec4<bool>',
  'vec4<u32>',
  'vec4<i32>',
  'vec4<f32>',
  'mat2x2<f32>',
  'mat2x3<f32>',
  'mat2x4<f32>',
  'mat3x2<f32>',
  'mat3x3<f32>',
  'mat3x4<f32>',
  'mat4x2<f32>',
  'mat4x3<f32>',
  'mat4x4<f32>',
  'atomic<u32>',
  'atomic<i32>',
  'array<bool,4>',
  'array<u32,4>',
  'array<i32,4>',
  'array<f32,4>',
  'MyStruct',
] as const;

g.test('stage_inout')
  .desc(
    `Test that each @builtin attribute is validated against the required stage and in/out usage for that built-in variable.`
  )
  .params(u =>
    u
      .combineWithParams(kBuiltins)
      .combine('use_struct', [true, false] as const)
      .combine('target_stage', ['', 'vertex', 'fragment', 'compute'] as const)
      .combine('target_io', ['in', 'out'] as const)
      .beginSubcases()
  )
  .fn(t => {
    const code = generateShader({
      attribute: `@builtin(${t.params.name})`,
      type: t.params.type,
      stage: t.params.target_stage,
      io: t.params.target_io,
      use_struct: t.params.use_struct,
    });

    // Expect to pass iff the built-in table contains an entry that matches.
    const expectation = kBuiltins.some(
      x =>
        x.name === t.params.name &&
        (x.stage === t.params.target_stage ||
          (t.params.use_struct && t.params.target_stage === '')) &&
        (x.io === t.params.target_io || t.params.target_stage === '') &&
        x.type === t.params.type
    );
    t.expectCompileResult(expectation, code);
  });

g.test('type')
  .desc(
    `Test that each @builtin attribute is validated against the required type of that built-in variable.`
  )
  .params(u =>
    u
      .combineWithParams(kBuiltins)
      .combine('use_struct', [true, false] as const)
      .combine('target_type', kTestTypes)
      .beginSubcases()
  )
  .fn(t => {
    let code = '';

    if (t.params.target_type === 'MyStruct') {
      // Generate a struct that contains the correct built-in type.
      code += 'struct MyStruct {\n';
      code += `  value : ${t.params.type}\n`;
      code += '};\n\n';
    }

    code += generateShader({
      attribute: `@builtin(${t.params.name})`,
      type: t.params.target_type,
      stage: t.params.stage,
      io: t.params.io,
      use_struct: t.params.use_struct,
    });

    // Expect to pass iff the built-in table contains an entry that matches.
    const expectation = kBuiltins.some(
      x =>
        x.name === t.params.name &&
        x.stage === t.params.stage &&
        x.io === t.params.io &&
        x.type === t.params.target_type
    );
    t.expectCompileResult(expectation, code);
  });

g.test('nesting')
  .desc(`Test validation of nested built-in variables`)
  .params(u =>
    u
      .combine('target_stage', ['fragment', ''] as const)
      .combine('target_io', ['in', 'out'] as const)
      .beginSubcases()
  )
  .fn(t => {
    // Generate a struct that contains a sample_mask builtin, nested inside another struct.
    let code = `
    struct Inner {
      @builtin(sample_mask) value : u32
    };
    struct Outer {
      inner : Inner
    };`;

    code += generateShader({
      attribute: '',
      type: 'Outer',
      stage: t.params.target_stage,
      io: t.params.target_io,
      use_struct: false,
    });

    // Expect to pass only if the struct is not used for entry point IO.
    t.expectCompileResult(t.params.target_stage === '', code);
  });

g.test('duplicates')
  .desc(`Test that duplicated built-in variables are validated.`)
  .params(u =>
    u
      // Place two @builtin(sample_mask) attributes onto the entry point function.
      // We use `sample_mask` as it is valid as both an input and output for the same entry point.
      // The function:
      // - has two non-struct parameters (`p1` and `p2`)
      // - has two struct parameters each with two members (`s1{a,b}` and `s2{a,b}`)
      // - returns a struct with two members (`ra` and `rb`)
      // By default, all of these variables will have unique @location() attributes.
      .combine('first', ['p1', 's1a', 's2a', 'ra'] as const)
      .combine('second', ['p2', 's1b', 's2b', 'rb'] as const)
      .beginSubcases()
  )
  .fn(t => {
    const p1 =
      t.params.first === 'p1' ? '@builtin(sample_mask)' : '@location(1) @interpolate(flat)';
    const p2 =
      t.params.second === 'p2' ? '@builtin(sample_mask)' : '@location(2) @interpolate(flat)';
    const s1a =
      t.params.first === 's1a' ? '@builtin(sample_mask)' : '@location(3) @interpolate(flat)';
    const s1b =
      t.params.second === 's1b' ? '@builtin(sample_mask)' : '@location(4) @interpolate(flat)';
    const s2a =
      t.params.first === 's2a' ? '@builtin(sample_mask)' : '@location(5) @interpolate(flat)';
    const s2b =
      t.params.second === 's2b' ? '@builtin(sample_mask)' : '@location(6) @interpolate(flat)';
    const ra =
      t.params.first === 'ra' ? '@builtin(sample_mask)' : '@location(1) @interpolate(flat)';
    const rb =
      t.params.second === 'rb' ? '@builtin(sample_mask)' : '@location(2) @interpolate(flat)';
    const code = `
    struct S1 {
      ${s1a} a : u32,
      ${s1b} b : u32,
    };
    struct S2 {
      ${s2a} a : u32,
      ${s2b} b : u32,
    };
    struct R {
      ${ra} a : u32,
      ${rb} b : u32,
    };
    @fragment
    fn main(${p1} p1 : u32,
            ${p2} p2 : u32,
            s1 : S1,
            s2 : S2,
            ) -> R {
      return R();
    }
    `;

    // The test should fail if both @builtin(sample_mask) attributes are on the input parameters
    // or structures, or it they are both on the output struct. Otherwise it should pass.
    const firstIsRet = t.params.first === 'ra';
    const secondIsRet = t.params.second === 'rb';
    const expectation = firstIsRet !== secondIsRet;
    t.expectCompileResult(expectation, code);
  });

g.test('missing_vertex_position')
  .desc(`Test that vertex shaders are required to output @builtin(position).`)
  .params(u =>
    u
      .combine('use_struct', [true, false] as const)
      .combine('attribute', ['@builtin(position)', '@location(0)'] as const)
      .beginSubcases()
  )
  .fn(t => {
    const code = `
    struct S {
      ${t.params.attribute} value : vec4<f32>
    };

    @vertex
    fn main() -> ${t.params.use_struct ? 'S' : `${t.params.attribute} vec4<f32>`} {
      return ${t.params.use_struct ? 'S' : 'vec4<f32>'}();
    }
    `;

    // Expect to pass only when using @builtin(position).
    t.expectCompileResult(t.params.attribute === '@builtin(position)', code);
  });

g.test('reuse_builtin_name')
  .desc(`Test that a builtin name can be used in different contexts`)
  .params(u =>
    u
      .combineWithParams(kBuiltins)
      .combine('use', ['alias', 'struct', 'function', 'module-var', 'function-var'])
  )
  .fn(t => {
    let code = '';
    if (t.params.use === 'alias') {
      code += `alias ${t.params.name} = i32;`;
    } else if (t.params.use === `struct`) {
      code += `struct ${t.params.name} { i: f32, }`;
    } else if (t.params.use === `function`) {
      code += `fn ${t.params.name}() {}`;
    } else if (t.params.use === `module-var`) {
      code += `const ${t.params.name} = 1;`;
    } else if (t.params.use === `function-var`) {
      code += `fn test() { let ${t.params.name} = 1; }`;
    }
    t.expectCompileResult(true, code);
  });