summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/validation/render_pass/attachment_compatibility.spec.ts
blob: 2883d3c1b94751f8c5a66f0e6a9835e75f074688 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
export const description = `
Validation for attachment compatibility between render passes, bundles, and pipelines
`;

import { makeTestGroup } from '../../../../common/framework/test_group.js';
import { range } from '../../../../common/util/util.js';
import {
  kRegularTextureFormats,
  kSizedDepthStencilFormats,
  kUnsizedDepthStencilFormats,
  kTextureSampleCounts,
  kMaxColorAttachments,
  kTextureFormatInfo,
  getFeaturesForFormats,
  filterFormatsByFeature,
} from '../../../capability_info.js';
import { ValidationTest } from '../validation_test.js';

const kColorAttachmentCounts = range(kMaxColorAttachments, i => i + 1);
const kColorAttachments = kColorAttachmentCounts
  .map(count => {
    // generate cases with 0..1 null attachments at different location
    // e.g. count == 2
    // [
    //    [1, 1],
    //    [0, 1],
    //    [1, 0],
    // ]
    // 0 (false) means null attachment, 1 (true) means non-null attachment, at the slot

    // Special cases: we need at least a color attachment, when we don't have depth stencil attachment
    if (count === 1) {
      return [[1]];
    }
    if (count === 2) {
      return [
        [1, 1],
        [0, 1],
        [1, 0],
      ];
    }

    // [1, 1, ..., 1]: all color attachment are used
    let result = [new Array<boolean>(count).fill(true)];

    // [1, 0, 1, ..., 1]: generate cases with one null attachment at different locations
    result = result.concat(
      range(count, i => {
        const r = new Array<boolean>(count).fill(true);
        r[i] = false;
        return r;
      })
    );

    // [1, 0, 1, ..., 0, 1]: generate cases with two null attachments at different locations
    // To reduce test run time, limit the attachment count to <= 4
    if (count <= 4) {
      result = result.concat(
        range(count - 1, i => {
          const cases = [] as boolean[][];
          for (let j = i + 1; j < count; j++) {
            const r = new Array<boolean>(count).fill(true);
            r[i] = false;
            r[j] = false;
            cases.push(r);
          }
          return cases;
        }).flat()
      );
    }

    return result;
  })
  .flat() as boolean[][];

const kDepthStencilAttachmentFormats = [
  undefined,
  ...kSizedDepthStencilFormats,
  ...kUnsizedDepthStencilFormats,
] as const;

const kFeaturesForDepthStencilAttachmentFormats = getFeaturesForFormats([
  ...kSizedDepthStencilFormats,
  ...kUnsizedDepthStencilFormats,
]);

class F extends ValidationTest {
  createAttachmentTextureView(format: GPUTextureFormat, sampleCount?: number) {
    return this.device
      .createTexture({
        // Size matching the "arbitrary" size used by ValidationTest helpers.
        size: [16, 16, 1],
        format,
        usage: GPUTextureUsage.RENDER_ATTACHMENT,
        sampleCount,
      })
      .createView();
  }

  createColorAttachment(
    format: GPUTextureFormat | null,
    sampleCount?: number
  ): GPURenderPassColorAttachment | null {
    return format === null
      ? null
      : {
          view: this.createAttachmentTextureView(format, sampleCount),
          clearValue: [0, 0, 0, 0],
          loadOp: 'clear',
          storeOp: 'store',
        };
  }

  createDepthAttachment(
    format: GPUTextureFormat,
    sampleCount?: number
  ): GPURenderPassDepthStencilAttachment {
    const attachment: GPURenderPassDepthStencilAttachment = {
      view: this.createAttachmentTextureView(format, sampleCount),
    };
    if (kTextureFormatInfo[format].depth) {
      attachment.depthClearValue = 0;
      attachment.depthLoadOp = 'clear';
      attachment.depthStoreOp = 'discard';
    }
    if (kTextureFormatInfo[format].stencil) {
      attachment.stencilClearValue = 1;
      attachment.stencilLoadOp = 'clear';
      attachment.stencilStoreOp = 'discard';
    }
    return attachment;
  }

  createRenderPipeline(
    targets: Iterable<GPUColorTargetState | null>,
    depthStencil?: GPUDepthStencilState,
    sampleCount?: number,
    cullMode?: GPUCullMode
  ) {
    return this.device.createRenderPipeline({
      layout: 'auto',
      vertex: {
        module: this.device.createShaderModule({
          code: `
            @vertex fn main() -> @builtin(position) vec4<f32> {
              return vec4<f32>(0.0, 0.0, 0.0, 0.0);
            }`,
        }),
        entryPoint: 'main',
      },
      fragment: {
        module: this.device.createShaderModule({
          code: '@fragment fn main() {}',
        }),
        entryPoint: 'main',
        targets,
      },
      primitive: { topology: 'triangle-list', cullMode },
      depthStencil,
      multisample: { count: sampleCount },
    });
  }
}

export const g = makeTestGroup(F);

const kColorAttachmentFormats = kRegularTextureFormats.filter(format => {
  const info = kTextureFormatInfo[format];
  return info.color && info.renderable;
});

g.test('render_pass_and_bundle,color_format')
  .desc('Test that color attachment formats in render passes and bundles must match.')
  .paramsSubcasesOnly(u =>
    u //
      .combine('passFormat', kColorAttachmentFormats)
      .combine('bundleFormat', kColorAttachmentFormats)
  )
  .fn(t => {
    const { passFormat, bundleFormat } = t.params;
    const bundleEncoder = t.device.createRenderBundleEncoder({
      colorFormats: [bundleFormat],
    });
    const bundle = bundleEncoder.finish();

    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const pass = encoder.beginRenderPass({
      colorAttachments: [t.createColorAttachment(passFormat)],
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(passFormat === bundleFormat, true);
  });

g.test('render_pass_and_bundle,color_count')
  .desc(
    `
  Test that the number of color attachments in render passes and bundles must match.
  `
  )
  .paramsSubcasesOnly(u =>
    u //
      .combine('passCount', kColorAttachmentCounts)
      .combine('bundleCount', kColorAttachmentCounts)
  )
  .fn(t => {
    const { passCount, bundleCount } = t.params;
    const bundleEncoder = t.device.createRenderBundleEncoder({
      colorFormats: range(bundleCount, () => 'rgba8uint'),
    });
    const bundle = bundleEncoder.finish();

    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const pass = encoder.beginRenderPass({
      colorAttachments: range(passCount, () => t.createColorAttachment('rgba8uint')),
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(passCount === bundleCount, true);
  });

g.test('render_pass_and_bundle,color_sparse')
  .desc(
    `
  Test that each of color attachments in render passes and bundles must match.
  `
  )
  .params(u =>
    u //
      // introduce attachmentCount to make it easier to split the test
      .combine('attachmentCount', kColorAttachmentCounts)
      .beginSubcases()
      .combine('passAttachments', kColorAttachments)
      .combine('bundleAttachments', kColorAttachments)
      .filter(
        p =>
          p.attachmentCount === p.passAttachments.length &&
          p.attachmentCount === p.bundleAttachments.length
      )
  )
  .fn(t => {
    const { passAttachments, bundleAttachments } = t.params;
    const colorFormats = bundleAttachments.map(i => (i ? 'rgba8uint' : null));
    const bundleEncoder = t.device.createRenderBundleEncoder({
      colorFormats,
    });
    const bundle = bundleEncoder.finish();

    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const colorAttachments = passAttachments.map(i =>
      t.createColorAttachment(i ? 'rgba8uint' : null)
    );
    const pass = encoder.beginRenderPass({
      colorAttachments,
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(
      passAttachments.every((v, i) => v === bundleAttachments[i]),
      true
    );
  });

g.test('render_pass_and_bundle,depth_format')
  .desc('Test that the depth attachment format in render passes and bundles must match.')
  .params(u =>
    u //
      .combine('passFeature', kFeaturesForDepthStencilAttachmentFormats)
      .combine('bundleFeature', kFeaturesForDepthStencilAttachmentFormats)
      .beginSubcases()
      .expand('passFormat', ({ passFeature }) =>
        filterFormatsByFeature(passFeature, kDepthStencilAttachmentFormats)
      )
      .expand('bundleFormat', ({ bundleFeature }) =>
        filterFormatsByFeature(bundleFeature, kDepthStencilAttachmentFormats)
      )
  )
  .beforeAllSubcases(t => {
    const { passFeature, bundleFeature } = t.params;
    t.selectDeviceOrSkipTestCase([passFeature, bundleFeature]);
  })
  .fn(async t => {
    const { passFormat, bundleFormat } = t.params;

    const bundleEncoder = t.device.createRenderBundleEncoder({
      colorFormats: ['rgba8unorm'],
      depthStencilFormat: bundleFormat,
    });
    const bundle = bundleEncoder.finish();

    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const pass = encoder.beginRenderPass({
      colorAttachments: [t.createColorAttachment('rgba8unorm')],
      depthStencilAttachment:
        passFormat !== undefined ? t.createDepthAttachment(passFormat) : undefined,
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(passFormat === bundleFormat, true);
  });

g.test('render_pass_and_bundle,sample_count')
  .desc('Test that the sample count in render passes and bundles must match.')
  .paramsSubcasesOnly(u =>
    u //
      .combine('renderSampleCount', kTextureSampleCounts)
      .combine('bundleSampleCount', kTextureSampleCounts)
  )
  .fn(t => {
    const { renderSampleCount, bundleSampleCount } = t.params;
    const bundleEncoder = t.device.createRenderBundleEncoder({
      colorFormats: ['rgba8unorm'],
      sampleCount: bundleSampleCount,
    });
    const bundle = bundleEncoder.finish();
    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const pass = encoder.beginRenderPass({
      colorAttachments: [t.createColorAttachment('rgba8unorm', renderSampleCount)],
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(renderSampleCount === bundleSampleCount, true);
  });

g.test('render_pass_and_bundle,device_mismatch')
  .desc('Test that render passes cannot be called with bundles created from another device.')
  .paramsSubcasesOnly(u => u.combine('mismatched', [true, false]))
  .beforeAllSubcases(t => {
    t.selectMismatchedDeviceOrSkipTestCase(undefined);
  })
  .fn(t => {
    const { mismatched } = t.params;
    const sourceDevice = mismatched ? t.mismatchedDevice : t.device;

    const format = 'r16float';
    const bundleEncoder = sourceDevice.createRenderBundleEncoder({
      colorFormats: [format],
    });
    const bundle = bundleEncoder.finish();

    const { encoder, validateFinishAndSubmit } = t.createEncoder('non-pass');
    const pass = encoder.beginRenderPass({
      colorAttachments: [t.createColorAttachment(format)],
    });
    pass.executeBundles([bundle]);
    pass.end();
    validateFinishAndSubmit(!mismatched, true);
  });

g.test('render_pass_or_bundle_and_pipeline,color_format')
  .desc(
    `
Test that color attachment formats in render passes or bundles match the pipeline color format.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      .beginSubcases()
      .combine('encoderFormat', kColorAttachmentFormats)
      .combine('pipelineFormat', kColorAttachmentFormats)
  )
  .fn(t => {
    const { encoderType, encoderFormat, pipelineFormat } = t.params;
    const pipeline = t.createRenderPipeline([{ format: pipelineFormat, writeMask: 0 }]);

    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: { colorFormats: [encoderFormat] },
    });
    encoder.setPipeline(pipeline);
    validateFinishAndSubmit(encoderFormat === pipelineFormat, true);
  });

g.test('render_pass_or_bundle_and_pipeline,color_count')
  .desc(
    `
Test that the number of color attachments in render passes or bundles match the pipeline color
count.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      .beginSubcases()
      .combine('encoderCount', kColorAttachmentCounts)
      .combine('pipelineCount', kColorAttachmentCounts)
  )
  .fn(t => {
    const { encoderType, encoderCount, pipelineCount } = t.params;
    const pipeline = t.createRenderPipeline(
      range(pipelineCount, () => ({ format: 'rgba8uint', writeMask: 0 }))
    );

    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: { colorFormats: range(encoderCount, () => 'rgba8uint') },
    });
    encoder.setPipeline(pipeline);
    validateFinishAndSubmit(encoderCount === pipelineCount, true);
  });

g.test('render_pass_or_bundle_and_pipeline,color_sparse')
  .desc(
    `
Test that each of color attachments in render passes or bundles match that of the pipeline.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      // introduce attachmentCount to make it easier to split the test
      .combine('attachmentCount', kColorAttachmentCounts)
      .beginSubcases()
      .combine('encoderAttachments', kColorAttachments)
      .combine('pipelineAttachments', kColorAttachments)
      .filter(
        p =>
          p.attachmentCount === p.encoderAttachments.length &&
          p.attachmentCount === p.pipelineAttachments.length
      )
  )
  .fn(t => {
    const { encoderType, encoderAttachments, pipelineAttachments } = t.params;

    const colorTargets = pipelineAttachments.map(i =>
      i ? ({ format: 'rgba8uint', writeMask: 0 } as GPUColorTargetState) : null
    );
    const pipeline = t.createRenderPipeline(colorTargets);

    const colorFormats = encoderAttachments.map(i => (i ? 'rgba8uint' : null));
    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: { colorFormats },
    });
    encoder.setPipeline(pipeline);
    validateFinishAndSubmit(
      encoderAttachments.every((v, i) => v === pipelineAttachments[i]),
      true
    );
  });

g.test('render_pass_or_bundle_and_pipeline,depth_format')
  .desc(
    `
Test that the depth attachment format in render passes or bundles match the pipeline depth format.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      .combine('encoderFormatFeature', kFeaturesForDepthStencilAttachmentFormats)
      .combine('pipelineFormatFeature', kFeaturesForDepthStencilAttachmentFormats)
      .beginSubcases()
      .expand('encoderFormat', ({ encoderFormatFeature }) =>
        filterFormatsByFeature(encoderFormatFeature, kDepthStencilAttachmentFormats)
      )
      .expand('pipelineFormat', ({ pipelineFormatFeature }) =>
        filterFormatsByFeature(pipelineFormatFeature, kDepthStencilAttachmentFormats)
      )
  )
  .beforeAllSubcases(t => {
    const { encoderFormatFeature, pipelineFormatFeature } = t.params;
    t.selectDeviceOrSkipTestCase([encoderFormatFeature, pipelineFormatFeature]);
  })
  .fn(async t => {
    const { encoderType, encoderFormat, pipelineFormat } = t.params;

    const pipeline = t.createRenderPipeline(
      [{ format: 'rgba8unorm', writeMask: 0 }],
      pipelineFormat !== undefined ? { format: pipelineFormat } : undefined
    );

    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: { colorFormats: ['rgba8unorm'], depthStencilFormat: encoderFormat },
    });
    encoder.setPipeline(pipeline);
    validateFinishAndSubmit(encoderFormat === pipelineFormat, true);
  });

const kStencilFaceStates = [
  { failOp: 'keep', depthFailOp: 'keep', passOp: 'keep' },
  { failOp: 'zero', depthFailOp: 'zero', passOp: 'zero' },
] as GPUStencilFaceState[];

g.test('render_pass_or_bundle_and_pipeline,depth_stencil_read_only_write_state')
  .desc(
    `
Test that the depth stencil read only state in render passes or bundles is compatible with the depth stencil write state of the pipeline.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      .combine('format', kDepthStencilAttachmentFormats)
      .beginSubcases()
      // pass/bundle state
      .combine('depthReadOnly', [false, true])
      .combine('stencilReadOnly', [false, true])
      .combine('stencilFront', kStencilFaceStates)
      .combine('stencilBack', kStencilFaceStates)
      // pipeline state
      .combine('depthWriteEnabled', [false, true])
      .combine('stencilWriteMask', [0, 0xffffffff])
      .combine('cullMode', ['none', 'front', 'back'] as const)
      .filter(p => {
        if (p.format) {
          const depthStencilInfo = kTextureFormatInfo[p.format];
          // For combined depth/stencil formats the depth and stencil read only state must match
          // in order to create a valid render bundle or render pass.
          if (depthStencilInfo.depth && depthStencilInfo.stencil) {
            if (p.depthReadOnly !== p.stencilReadOnly) {
              return false;
            }
          }
          // If the format has no depth aspect, the depthReadOnly, depthWriteEnabled of the pipeline must not be true
          // in order to create a valid render pipeline.
          if (!depthStencilInfo.depth && p.depthWriteEnabled) {
            return false;
          }
          // If the format has no stencil aspect, the stencil state operation must be 'keep'
          // in order to create a valid render pipeline.
          if (
            !depthStencilInfo.stencil &&
            (p.stencilFront.failOp !== 'keep' || p.stencilBack.failOp !== 'keep')
          ) {
            return false;
          }
        }
        // No depthStencil attachment
        return true;
      })
  )
  .beforeAllSubcases(t => {
    t.selectDeviceForTextureFormatOrSkipTestCase(t.params.format);
  })
  .fn(async t => {
    const {
      encoderType,
      format,
      depthReadOnly,
      stencilReadOnly,
      depthWriteEnabled,
      stencilWriteMask,
      cullMode,
      stencilFront,
      stencilBack,
    } = t.params;

    const pipeline = t.createRenderPipeline(
      [{ format: 'rgba8unorm', writeMask: 0 }],
      format === undefined
        ? undefined
        : {
            format,
            depthWriteEnabled,
            stencilWriteMask,
            stencilFront,
            stencilBack,
          },
      1,
      cullMode
    );

    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: {
        colorFormats: ['rgba8unorm'],
        depthStencilFormat: format,
        depthReadOnly,
        stencilReadOnly,
      },
    });
    encoder.setPipeline(pipeline);

    let writesDepth = false;
    let writesStencil = false;
    if (format) {
      writesDepth = depthWriteEnabled;
      if (stencilWriteMask !== 0) {
        if (
          cullMode !== 'front' &&
          (stencilFront.passOp !== 'keep' ||
            stencilFront.depthFailOp !== 'keep' ||
            stencilFront.failOp !== 'keep')
        ) {
          writesStencil = true;
        }
        if (
          cullMode !== 'back' &&
          (stencilBack.passOp !== 'keep' ||
            stencilBack.depthFailOp !== 'keep' ||
            stencilBack.failOp !== 'keep')
        ) {
          writesStencil = true;
        }
      }
    }

    let isValid = true;
    if (writesDepth) {
      isValid &&= !depthReadOnly;
    }
    if (writesStencil) {
      isValid &&= !stencilReadOnly;
    }

    validateFinishAndSubmit(isValid, true);
  });

g.test('render_pass_or_bundle_and_pipeline,sample_count')
  .desc(
    `
Test that the sample count in render passes or bundles match the pipeline sample count for both color texture and depthstencil texture.
`
  )
  .params(u =>
    u
      .combine('encoderType', ['render pass', 'render bundle'] as const)
      .combine('attachmentType', ['color', 'depthstencil'] as const)
      .beginSubcases()
      .combine('encoderSampleCount', kTextureSampleCounts)
      .combine('pipelineSampleCount', kTextureSampleCounts)
  )
  .fn(t => {
    const { encoderType, attachmentType, encoderSampleCount, pipelineSampleCount } = t.params;

    const colorFormats = attachmentType === 'color' ? ['rgba8unorm' as const] : [];
    const depthStencilFormat =
      attachmentType === 'depthstencil' ? ('depth24plus-stencil8' as const) : undefined;

    const pipeline = t.createRenderPipeline(
      colorFormats.map(format => ({ format, writeMask: 0 })),
      depthStencilFormat ? { format: depthStencilFormat } : undefined,
      pipelineSampleCount
    );

    const { encoder, validateFinishAndSubmit } = t.createEncoder(encoderType, {
      attachmentInfo: { colorFormats, depthStencilFormat, sampleCount: encoderSampleCount },
    });
    encoder.setPipeline(pipeline);
    validateFinishAndSubmit(encoderSampleCount === pipelineSampleCount, true);
  });