summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/validation/render_pipeline/fragment_state.spec.ts
blob: ffedddea2c919488228e19589312ddf9429dea46 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
export const description = `
This test dedicatedly tests validation of GPUFragmentState of createRenderPipeline.
`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { range } from '../../../../common/util/util.js';
import {
  kTextureFormats,
  kRenderableColorTextureFormats,
  kTextureFormatInfo,
  kBlendFactors,
  kBlendOperations,
  kMaxColorAttachments,
} from '../../../capability_info.js';
import {
  getFragmentShaderCodeWithOutput,
  getPlainTypeInfo,
  kDefaultFragmentShaderCode,
} from '../../../util/shader.js';
import { kTexelRepresentationInfo } from '../../../util/texture/texel_data.js';

import { CreateRenderPipelineValidationTest } from './common.js';

export const g = makeTestGroup(CreateRenderPipelineValidationTest);

const values = [0, 1, 0, 1];

g.test('color_target_exists')
  .desc(`Tests creating a complete render pipeline requires at least one color target state.`)
  .params(u => u.combine('isAsync', [false, true]))
  .fn(async t => {
    const { isAsync } = t.params;

    const goodDescriptor = t.getDescriptor({
      targets: [{ format: 'rgba8unorm' }],
    });

    // Control case
    t.doCreateRenderPipelineTest(isAsync, true, goodDescriptor);

    // Fail because lack of color states
    const badDescriptor = t.getDescriptor({
      targets: [],
    });

    t.doCreateRenderPipelineTest(isAsync, false, badDescriptor);
  });

g.test('targets_format_renderable')
  .desc(`Tests that color target state format must have RENDER_ATTACHMENT capability.`)
  .params(u => u.combine('isAsync', [false, true]).combine('format', kTextureFormats))
  .beforeAllSubcases(t => {
    const { format } = t.params;
    const info = kTextureFormatInfo[format];
    t.selectDeviceOrSkipTestCase(info.feature);
  })
  .fn(async t => {
    const { isAsync, format } = t.params;
    const info = kTextureFormatInfo[format];

    const descriptor = t.getDescriptor({ targets: [{ format }] });

    t.doCreateRenderPipelineTest(isAsync, info.renderable && info.color, descriptor);
  });

g.test('limits,maxColorAttachments')
  .desc(
    `Tests that color state targets length must not be larger than device.limits.maxColorAttachments.`
  )
  .params(u => u.combine('isAsync', [false, true]).combine('targetsLength', [8, 9]))
  .fn(async t => {
    const { isAsync, targetsLength } = t.params;

    const descriptor = t.getDescriptor({
      targets: range(targetsLength, i => {
        // Set writeMask to 0 for attachments without fragment output
        return { format: 'rg8unorm', writeMask: i === 0 ? 0xf : 0 };
      }),
      fragmentShaderCode: kDefaultFragmentShaderCode,
    });

    t.doCreateRenderPipelineTest(
      isAsync,
      targetsLength <= t.device.limits.maxColorAttachments,
      descriptor
    );
  });

g.test('limits,maxColorAttachmentBytesPerSample,aligned')
  .desc(
    `
  Tests that the total color attachment bytes per sample must not be larger than
  maxColorAttachmentBytesPerSample when using the same format for multiple attachments.
  `
  )
  .params(u =>
    u
      .combine('format', kRenderableColorTextureFormats)
      .beginSubcases()
      .combine(
        'attachmentCount',
        range(kMaxColorAttachments, i => i + 1)
      )
      .combine('isAsync', [false, true])
  )
  .fn(async t => {
    const { format, attachmentCount, isAsync } = t.params;
    const info = kTextureFormatInfo[format];

    const descriptor = t.getDescriptor({
      targets: range(attachmentCount, () => {
        return { format, writeMask: 0 };
      }),
    });
    const shouldError =
      info.renderTargetPixelByteCost === undefined ||
      info.renderTargetPixelByteCost * attachmentCount >
        t.device.limits.maxColorAttachmentBytesPerSample;

    t.doCreateRenderPipelineTest(isAsync, !shouldError, descriptor);
  });

g.test('limits,maxColorAttachmentBytesPerSample,unaligned')
  .desc(
    `
  Tests that the total color attachment bytes per sample must not be larger than
  maxColorAttachmentBytesPerSample when using various sets of (potentially) unaligned formats.
  `
  )
  .params(u =>
    u
      .combineWithParams([
        // Alignment causes the first 1 byte R8Unorm to become 4 bytes. So even though
        // 1+4+8+16+1 < 32, the 4 byte alignment requirement of R32Float makes the first R8Unorm
        // become 4 and 4+4+8+16+1 > 32. Re-ordering this so the R8Unorm's are at the end, however
        // is allowed: 4+8+16+1+1 < 32.
        {
          formats: [
            'r8unorm',
            'r32float',
            'rgba8unorm',
            'rgba32float',
            'r8unorm',
          ] as GPUTextureFormat[],
          _success: true,
        },
        {
          formats: [
            'r32float',
            'rgba8unorm',
            'rgba32float',
            'r8unorm',
            'r8unorm',
          ] as GPUTextureFormat[],
          _success: false,
        },
      ])
      .beginSubcases()
      .combine('isAsync', [false, true])
  )
  .fn(async t => {
    const { formats, _success, isAsync } = t.params;

    const descriptor = t.getDescriptor({
      targets: formats.map(f => {
        return { format: f, writeMask: 0 };
      }),
    });

    t.doCreateRenderPipelineTest(isAsync, _success, descriptor);
  });

g.test('targets_format_filterable')
  .desc(`Tests that color target state format must be filterable if blend is not undefined.`)
  .params(u =>
    u
      .combine('isAsync', [false, true])
      .combine('format', kRenderableColorTextureFormats)
      .beginSubcases()
      .combine('hasBlend', [false, true])
  )
  .beforeAllSubcases(t => {
    const { format } = t.params;
    const info = kTextureFormatInfo[format];
    t.selectDeviceOrSkipTestCase(info.feature);
  })
  .fn(async t => {
    const { isAsync, format, hasBlend } = t.params;
    const info = kTextureFormatInfo[format];

    const descriptor = t.getDescriptor({
      targets: [
        {
          format,
          blend: hasBlend ? { color: {}, alpha: {} } : undefined,
        },
      ],
    });

    t.doCreateRenderPipelineTest(isAsync, !hasBlend || info.sampleType === 'float', descriptor);
  });

g.test('targets_blend')
  .desc(
    `
  For the blend components on either GPUBlendState.color or GPUBlendState.alpha:
  - Tests if the combination of 'srcFactor', 'dstFactor' and 'operation' is valid (if the blend
    operation is "min" or "max", srcFactor and dstFactor must be "one").
  `
  )
  .params(u =>
    u
      .combine('isAsync', [false, true])
      .combine('component', ['color', 'alpha'] as const)
      .beginSubcases()
      .combine('srcFactor', kBlendFactors)
      .combine('dstFactor', kBlendFactors)
      .combine('operation', kBlendOperations)
  )
  .fn(async t => {
    const { isAsync, component, srcFactor, dstFactor, operation } = t.params;

    const defaultBlendComponent: GPUBlendComponent = {
      srcFactor: 'src-alpha',
      dstFactor: 'dst-alpha',
      operation: 'add',
    };
    const blendComponentToTest: GPUBlendComponent = {
      srcFactor,
      dstFactor,
      operation,
    };
    const format = 'rgba8unorm';

    const descriptor = t.getDescriptor({
      targets: [
        {
          format,
          blend: {
            color: component === 'color' ? blendComponentToTest : defaultBlendComponent,
            alpha: component === 'alpha' ? blendComponentToTest : defaultBlendComponent,
          },
        },
      ],
    });

    if (operation === 'min' || operation === 'max') {
      const _success = srcFactor === 'one' && dstFactor === 'one';
      t.doCreateRenderPipelineTest(isAsync, _success, descriptor);
    } else {
      t.doCreateRenderPipelineTest(isAsync, true, descriptor);
    }
  });

g.test('targets_write_mask')
  .desc(`Tests that color target state write mask must be < 16.`)
  .params(u => u.combine('isAsync', [false, true]).combine('writeMask', [0, 0xf, 0x10, 0x80000001]))
  .fn(async t => {
    const { isAsync, writeMask } = t.params;

    const descriptor = t.getDescriptor({
      targets: [
        {
          format: 'rgba8unorm',
          writeMask,
        },
      ],
    });

    t.doCreateRenderPipelineTest(isAsync, writeMask < 16, descriptor);
  });

g.test('pipeline_output_targets')
  .desc(
    `Pipeline fragment output types must be compatible with target color state format
  - The scalar type (f32, i32, or u32) must match the sample type of the format.
  - The componentCount of the fragment output (e.g. f32, vec2, vec3, vec4) must not have fewer
    channels than that of the color attachment texture formats. Extra components are allowed and are discarded.

  Otherwise, color state write mask must be 0.`
  )
  .params(u =>
    u
      .combine('isAsync', [false, true])
      .combine('format', [undefined, ...kRenderableColorTextureFormats] as const)
      .beginSubcases()
      .combine('shaderOutput', [
        undefined,
        ...u.combine('scalar', ['f32', 'u32', 'i32'] as const).combine('count', [1, 2, 3, 4]),
      ])
      // We only care about testing writeMask if there is an attachment but no shader output.
      .expand('writeMask', p =>
        p.format !== undefined && p.shaderOutput !== undefined ? [0, 0x1, 0x2, 0x4, 0x8] : [0xf]
      )
  )
  .beforeAllSubcases(t => {
    t.selectDeviceForTextureFormatOrSkipTestCase(t.params.format);
  })
  .fn(async t => {
    const { isAsync, format, writeMask, shaderOutput } = t.params;

    const descriptor = t.getDescriptor({
      targets: format ? [{ format, writeMask }] : [],
      // To have a dummy depthStencil attachment to avoid having no attachment at all which is invalid
      depthStencil: { format: 'depth24plus' },
      fragmentShaderCode: getFragmentShaderCodeWithOutput(
        shaderOutput
          ? [{ values, plainType: shaderOutput.scalar, componentCount: shaderOutput.count }]
          : []
      ),
    });

    let success = true;
    if (format) {
      // There is a color target
      if (shaderOutput) {
        // The shader outputs to the color target
        const info = kTextureFormatInfo[format];
        success =
          shaderOutput.scalar === getPlainTypeInfo(info.sampleType) &&
          shaderOutput.count >= kTexelRepresentationInfo[format].componentOrder.length;
      } else {
        // The shader does not output to the color target
        success = writeMask === 0;
      }
    }

    t.doCreateRenderPipelineTest(isAsync, success, descriptor);
  });

g.test('pipeline_output_targets,blend')
  .desc(
    `On top of requirements from pipeline_output_targets, when blending is enabled and alpha channel is read indicated by any blend factor, an extra requirement is added:
  - fragment output must be vec4.
  `
  )
  .params(u =>
    u
      .combine('isAsync', [false, true])
      .combine('format', ['r8unorm', 'rg8unorm', 'rgba8unorm', 'bgra8unorm'] as const)
      .combine('componentCount', [1, 2, 3, 4])
      .beginSubcases()
      // The default srcFactor and dstFactor are 'one' and 'zero'. Override just one at a time.
      .combineWithParams([
        ...u.combine('colorSrcFactor', kBlendFactors),
        ...u.combine('colorDstFactor', kBlendFactors),
        ...u.combine('alphaSrcFactor', kBlendFactors),
        ...u.combine('alphaDstFactor', kBlendFactors),
      ] as const)
  )
  .beforeAllSubcases(t => {
    const { format } = t.params;
    const info = kTextureFormatInfo[format];
    t.selectDeviceOrSkipTestCase(info.feature);
  })
  .fn(async t => {
    const sampleType = 'float';
    const {
      isAsync,
      format,
      componentCount,
      colorSrcFactor,
      colorDstFactor,
      alphaSrcFactor,
      alphaDstFactor,
    } = t.params;
    const info = kTextureFormatInfo[format];

    const descriptor = t.getDescriptor({
      targets: [
        {
          format,
          blend: {
            color: { srcFactor: colorSrcFactor, dstFactor: colorDstFactor },
            alpha: { srcFactor: alphaSrcFactor, dstFactor: alphaDstFactor },
          },
        },
      ],
      fragmentShaderCode: getFragmentShaderCodeWithOutput([
        { values, plainType: getPlainTypeInfo(sampleType), componentCount },
      ]),
    });

    const colorBlendReadsSrcAlpha =
      colorSrcFactor?.includes('src-alpha') || colorDstFactor?.includes('src-alpha');
    const meetsExtraBlendingRequirement = !colorBlendReadsSrcAlpha || componentCount === 4;
    const _success =
      info.sampleType === sampleType &&
      componentCount >= kTexelRepresentationInfo[format].componentOrder.length &&
      meetsExtraBlendingRequirement;
    t.doCreateRenderPipelineTest(isAsync, _success, descriptor);
  });