summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/operation/shader_module/compilation_info.spec.ts
blob: 93fa4575c43917ce4a1bfeb3362079a6b833868a (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
export const description = `
ShaderModule CompilationInfo tests.
`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { keysOf } from '../../../../common/util/data_tables.js';
import { assert } from '../../../../common/util/util.js';
import { GPUTest } from '../../../gpu_test.js';

export const g = makeTestGroup(GPUTest);

const kValidShaderSources = [
  {
    valid: true,
    name: 'ascii',
    _code: `
      @vertex fn main() -> @builtin(position) vec4<f32> {
        return vec4<f32>(0.0, 0.0, 0.0, 1.0);
      }`,
  },
  {
    valid: true,
    name: 'unicode',
    _code: `
      // 頂点シェーダー 👩‍💻
      @vertex fn main() -> @builtin(position) vec4<f32> {
        return vec4<f32>(0.0, 0.0, 0.0, 1.0);
      }`,
  },
];

const kInvalidShaderSources = [
  {
    valid: false,
    name: 'ascii',
    _errorLine: 4,
    _code: `
      @vertex fn main() -> @builtin(position) vec4<f32> {
        // Expected Error: unknown function 'unknown'
        return unknown(0.0, 0.0, 0.0, 1.0);
      }`,
  },
  {
    valid: false,
    name: 'unicode',
    _errorLine: 5,
    _code: `
      // 頂点シェーダー 👩‍💻
      @vertex fn main() -> @builtin(position) vec4<f32> {
        // Expected Error: unknown function 'unknown'
        return unknown(0.0, 0.0, 0.0, 1.0);
      }`,
  },
  {
    valid: false,
    name: 'carriage-return',
    _errorLine: 5,
    _code:
      `
      @vertex fn main() -> @builtin(position) vec4<f32> {` +
      '\r\n' +
      `
        // Expected Error: unknown function 'unknown'
        return unknown(0.0, 0.0, 0.0, 1.0);
      }`,
  },
];

const kAllShaderSources = [...kValidShaderSources, ...kInvalidShaderSources];

// This is the source the sourcemap refers to.
const kOriginalSource = new Array(20)
  .fill(0)
  .map((_, i) => `original line ${i}`)
  .join('\n');

const kSourceMaps: { [name: string]: undefined | object } = {
  none: undefined,
  empty: {},
  // A valid source map. It maps `unknown` on lines 4 and line 5 to
  // `wasUnknown` from lines 20, 21 respectively
  valid: {
    version: 3,
    sources: ['myCode'],
    sourcesContent: [kOriginalSource],
    names: ['myMain', 'wasUnknown'],
    mappings: ';kBAYkCA,OACd;SAElB;gBAKOC;gBACAA',
  },
  // not a valid sourcemap
  invalid: {
    version: -123,
    notAnything: {},
  },
  // The correct format but this data is for lines 11,12 even
  // though the source only has 5 or 6 lines
  nonMatching: {
    version: 3,
    sources: ['myCode'],
    sourcesContent: [kOriginalSource],
    names: ['myMain'],
    mappings: ';;;;;;;;;;kBAYkCA,OACd;SAElB',
  },
};
const kSourceMapsKeys = keysOf(kSourceMaps);

g.test('getCompilationInfo_returns')
  .desc(
    `
    Test that getCompilationInfo() can be called on any ShaderModule.

    Note: sourcemaps are not used in the WebGPU API. We are only testing that
    browser that happen to use them don't fail or crash if the sourcemap is
    bad or invalid.

    - Test for both valid and invalid shader modules.
    - Test for shader modules containing only ASCII and those containing unicode characters.
    - Test that the compilation info for valid shader modules contains no errors.
    - Test that the compilation info for invalid shader modules contains at least one error.`
  )
  .params(u =>
    u.combineWithParams(kAllShaderSources).beginSubcases().combine('sourceMapName', kSourceMapsKeys)
  )
  .fn(async t => {
    const { _code, valid, sourceMapName } = t.params;

    const shaderModule = t.expectGPUError(
      'validation',
      () => {
        const sourceMap = kSourceMaps[sourceMapName];
        return t.device.createShaderModule({ code: _code, ...(sourceMap && { sourceMap }) });
      },
      !valid
    );

    const info = await shaderModule.getCompilationInfo();

    t.expect(
      info instanceof GPUCompilationInfo,
      'Expected a GPUCompilationInfo object to be returned'
    );

    // Expect that we get zero error messages from a valid shader.
    // Message types other than errors are OK.
    let errorCount = 0;
    for (const message of info.messages) {
      if (message.type === 'error') {
        errorCount++;
      }
    }
    if (valid) {
      t.expect(errorCount === 0, "Expected zero GPUCompilationMessages of type 'error'");
    } else {
      t.expect(errorCount > 0, "Expected at least one GPUCompilationMessages of type 'error'");
    }
  });

g.test('line_number_and_position')
  .desc(
    `
    Test that line numbers reported by compilationInfo either point at an appropriate line and
    position or at 0:0, indicating an unknown position.

    Note: sourcemaps are not used in the WebGPU API. We are only testing that
    browser that happen to use them don't fail or crash if the sourcemap is
    bad or invalid.

    - Test for invalid shader modules containing containing at least one error.
    - Test for shader modules containing only ASCII and those containing unicode characters.`
  )
  .params(u =>
    u
      .combineWithParams(kInvalidShaderSources)
      .beginSubcases()
      .combine('sourceMapName', kSourceMapsKeys)
  )
  .fn(async t => {
    const { _code, _errorLine, sourceMapName } = t.params;

    const shaderModule = t.expectGPUError('validation', () => {
      const sourceMap = kSourceMaps[sourceMapName];
      return t.device.createShaderModule({ code: _code, ...(sourceMap && { sourceMap }) });
    });

    const info = await shaderModule.getCompilationInfo();

    let foundAppropriateError = false;
    for (const message of info.messages) {
      if (message.type === 'error') {
        // Some backends may not be able to indicate a precise location for the error. In those
        // cases a line and position of 0 should be reported.
        // If a line is reported, it should point at the correct line (1-based).
        t.expect(
          (message.lineNum === 0) === (message.linePos === 0),
          "GPUCompilationMessages that don't report a line number should not report a line position."
        );

        if (message.lineNum === 0 || message.lineNum === _errorLine) {
          foundAppropriateError = true;

          // Various backends may choose to report the error at different positions within the line,
          // so it's difficult to meaningfully validate them.
          break;
        }
      }
    }
    t.expect(
      foundAppropriateError,
      'Expected to find an error which corresponded with the erroneous line'
    );
  });

g.test('offset_and_length')
  .desc(
    `Test that message offsets and lengths are valid and align with any reported lineNum and linePos.

     Note: sourcemaps are not used in the WebGPU API. We are only testing that
     browser that happen to use them don't fail or crash if the sourcemap is
     bad or invalid.

    - Test for valid and invalid shader modules.
    - Test for shader modules containing only ASCII and those containing unicode characters.`
  )
  .params(u =>
    u.combineWithParams(kAllShaderSources).beginSubcases().combine('sourceMapName', kSourceMapsKeys)
  )
  .fn(async t => {
    const { _code, valid, sourceMapName } = t.params;

    const shaderModule = t.expectGPUError(
      'validation',
      () => {
        const sourceMap = kSourceMaps[sourceMapName];
        return t.device.createShaderModule({ code: _code, ...(sourceMap && { sourceMap }) });
      },
      !valid
    );

    const info = await shaderModule.getCompilationInfo();

    for (const message of info.messages) {
      // Any offsets and lengths should reference valid spans of the shader code.
      t.expect(message.offset <= _code.length, 'Message offset should be within the shader source');
      t.expect(
        message.offset + message.length <= _code.length,
        'Message offset and length should be within the shader source'
      );

      // If a valid line number and position are given, the offset should point the the same
      // location in the shader source.
      if (message.lineNum !== 0 && message.linePos !== 0) {
        let lineOffset = 0;
        for (let i = 0; i < message.lineNum - 1; ++i) {
          lineOffset = _code.indexOf('\n', lineOffset);
          assert(lineOffset !== -1);
          lineOffset += 1;
        }

        t.expect(
          message.offset === lineOffset + message.linePos - 1,
          'lineNum and linePos should point to the same location as offset'
        );
      }
    }
  });