summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/validation/state/device_lost/destroy.spec.ts
blob: a50469db2a3284428f0c00993bae38c763d68378 (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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
export const description = `
Tests for device lost induced via destroy.
  - Tests that prior to device destruction, valid APIs do not generate errors (control case).
  - After device destruction, runs the same APIs. No expected observable results, so test crash or future failures are the only current failure indicators.
`;

import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import {
  allBindingEntries,
  bindingTypeInfo,
  kBindableResources,
  kBufferUsageKeys,
  kBufferUsageInfo,
  kBufferUsageCopy,
  kBufferUsageCopyInfo,
  kCompressedTextureFormats,
  kQueryTypes,
  kTextureUsageType,
  kTextureUsageTypeInfo,
  kTextureUsageCopy,
  kTextureUsageCopyInfo,
  kRegularTextureFormats,
  kRenderableColorTextureFormats,
  kShaderStageKeys,
  kTextureFormatInfo,
} from '../../../../capability_info.js';
import { CommandBufferMaker, EncoderType } from '../../../../util/command_buffer_maker.js';
import {
  canCopyFromCanvasContext,
  createCanvas,
  kAllCanvasTypes,
  kValidCanvasContextIds,
} from '../../../../util/create_elements.js';
import { ValidationTest } from '../../validation_test.js';

const kCommandValidationStages = ['finish', 'submit'];
type CommandValidationStage = typeof kCommandValidationStages[number];

class DeviceDestroyTests extends ValidationTest {
  /**
   * Expects that `fn` does not produce any errors before the device is destroyed, and then calls
   * `fn` after the device is destroyed without any specific expectation. If `awaitLost` is true, we
   * also wait for device.lost to resolve before executing `fn` in the destroy case.
   */
  async executeAfterDestroy(fn: () => void, awaitLost: boolean): Promise<void> {
    this.expectDeviceLost('destroyed');

    this.expectValidationError(fn, false);
    this.device.destroy();
    if (awaitLost) {
      const lostInfo = await this.device.lost;
      this.expect(lostInfo.reason === 'destroyed');
    }
    fn();
  }

  /**
   * Expects that encoders can finish and submit the resulting commands before the device is
   * destroyed, then repeats the same process after the device is destroyed without any specific
   * expectations.
   * There are two valid stages: 'finish' and 'submit'.
   *   'finish': Tests [encode, finish] and [encoder, destroy, finish]
   *   'submit': Tests [encoder, finish, submit] and [encoder, finish, destroy, submit]
   */
  async executeCommandsAfterDestroy<T extends EncoderType>(
    stage: CommandValidationStage,
    awaitLost: boolean,
    encoderType: T,
    fn: (maker: CommandBufferMaker<T>) => CommandBufferMaker<T>
  ): Promise<void> {
    this.expectDeviceLost('destroyed');

    switch (stage) {
      case 'finish': {
        // Control case
        fn(this.createEncoder(encoderType)).validateFinish(true);
        // Validation case
        const encoder = fn(this.createEncoder(encoderType));
        await this.executeAfterDestroy(() => {
          encoder.finish();
        }, awaitLost);
        break;
      }
      case 'submit': {
        // Control case
        fn(this.createEncoder(encoderType)).validateFinishAndSubmit(true, true);
        // Validation case
        const commands = fn(this.createEncoder(encoderType)).validateFinish(true);
        await this.executeAfterDestroy(() => {
          this.queue.submit([commands]);
        }, awaitLost);
        break;
      }
    }
  }
}

export const g = makeTestGroup(DeviceDestroyTests);

g.test('createBuffer')
  .desc(
    `
Tests creating buffers on destroyed device. Tests valid combinations of:
  - Various usages
  - Mapped at creation or not
  `
  )
  .params(u =>
    u
      .combine('usageType', kBufferUsageKeys)
      .beginSubcases()
      .combine('usageCopy', kBufferUsageCopy)
      .combine('awaitLost', [true, false])
      .filter(({ usageType, usageCopy }) => {
        if (usageType === 'COPY_SRC' || usageType === 'COPY_DST') {
          return false;
        }
        if (usageType === 'MAP_READ') {
          return usageCopy === 'COPY_NONE' || usageCopy === 'COPY_DST';
        }
        if (usageType === 'MAP_WRITE') {
          return usageCopy === 'COPY_NONE' || usageCopy === 'COPY_SRC';
        }
        return true;
      })
      .combine('mappedAtCreation', [true, false])
  )
  .fn(async t => {
    const { awaitLost, usageType, usageCopy, mappedAtCreation } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createBuffer({
        size: 16,
        usage: kBufferUsageInfo[usageType] | kBufferUsageCopyInfo[usageCopy],
        mappedAtCreation,
      });
    }, awaitLost);
  });

g.test('createTexture,2d,uncompressed_format')
  .desc(
    `
Tests creating 2d uncompressed textures on destroyed device. Tests valid combinations of:
  - Various uncompressed texture formats
  - Various usages
  `
  )
  .params(u =>
    u
      .combine('format', kRegularTextureFormats)
      .beginSubcases()
      .combine('usageType', kTextureUsageType)
      .combine('usageCopy', kTextureUsageCopy)
      .combine('awaitLost', [true, false])
      .filter(({ format, usageType }) => {
        const info = kTextureFormatInfo[format];
        return !(
          (!info.renderable && usageType === 'render') ||
          (!info.storage && usageType === 'storage')
        );
      })
  )
  .fn(async t => {
    const { awaitLost, format, usageType, usageCopy } = t.params;
    const { blockWidth, blockHeight } = kTextureFormatInfo[format];
    await t.executeAfterDestroy(() => {
      t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: kTextureUsageTypeInfo[usageType] | kTextureUsageCopyInfo[usageCopy],
        format,
      });
    }, awaitLost);
  });

g.test('createTexture,2d,compressed_format')
  .desc(
    `
Tests creating 2d compressed textures on destroyed device. Tests valid combinations of:
  - Various compressed texture formats
  - Various usages
  `
  )
  .params(u =>
    u
      .combine('format', kCompressedTextureFormats)
      .beginSubcases()
      .combine('usageType', kTextureUsageType)
      .combine('usageCopy', kTextureUsageCopy)
      .combine('awaitLost', [true, false])
      .filter(({ format, usageType }) => {
        const info = kTextureFormatInfo[format];
        return !(
          (!info.renderable && usageType === 'render') ||
          (!info.storage && usageType === 'storage')
        );
      })
  )
  .beforeAllSubcases(t => {
    const { format } = t.params;
    t.selectDeviceOrSkipTestCase(kTextureFormatInfo[format].feature);
  })
  .fn(async t => {
    const { awaitLost, format, usageType, usageCopy } = t.params;
    const { blockWidth, blockHeight } = kTextureFormatInfo[format];
    await t.executeAfterDestroy(() => {
      t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: kTextureUsageTypeInfo[usageType] | kTextureUsageCopyInfo[usageCopy],
        format,
      });
    }, awaitLost);
  });

g.test('createView,2d,uncompressed_format')
  .desc(
    `
Tests creating texture views on 2d uncompressed textures from destroyed device. Tests valid combinations of:
  - Various uncompressed texture formats
  - Various usages
  `
  )
  .params(u =>
    u
      .combine('format', kRegularTextureFormats)
      .beginSubcases()
      .combine('usageType', kTextureUsageType)
      .combine('usageCopy', kTextureUsageCopy)
      .combine('awaitLost', [true, false])
      .filter(({ format, usageType }) => {
        const info = kTextureFormatInfo[format];
        return !(
          (!info.renderable && usageType === 'render') ||
          (!info.storage && usageType === 'storage')
        );
      })
  )
  .fn(async t => {
    const { awaitLost, format, usageType, usageCopy } = t.params;
    const { blockWidth, blockHeight } = kTextureFormatInfo[format];
    const texture = t.device.createTexture({
      size: { width: blockWidth, height: blockHeight },
      usage: kTextureUsageTypeInfo[usageType] | kTextureUsageCopyInfo[usageCopy],
      format,
    });
    await t.executeAfterDestroy(() => {
      texture.createView({ format });
    }, awaitLost);
  });

g.test('createView,2d,compressed_format')
  .desc(
    `
Tests creating texture views on 2d compressed textures from destroyed device. Tests valid combinations of:
  - Various compressed texture formats
  - Various usages
  `
  )
  .params(u =>
    u
      .combine('format', kCompressedTextureFormats)
      .beginSubcases()
      .combine('usageType', kTextureUsageType)
      .combine('usageCopy', kTextureUsageCopy)
      .combine('awaitLost', [true, false])
      .filter(({ format, usageType }) => {
        const info = kTextureFormatInfo[format];
        return !(
          (!info.renderable && usageType === 'render') ||
          (!info.storage && usageType === 'storage')
        );
      })
  )
  .beforeAllSubcases(t => {
    const { format } = t.params;
    t.selectDeviceOrSkipTestCase(kTextureFormatInfo[format].feature);
  })
  .fn(async t => {
    const { awaitLost, format, usageType, usageCopy } = t.params;
    const { blockWidth, blockHeight } = kTextureFormatInfo[format];
    const texture = t.device.createTexture({
      size: { width: blockWidth, height: blockHeight },
      usage: kTextureUsageTypeInfo[usageType] | kTextureUsageCopyInfo[usageCopy],
      format,
    });
    await t.executeAfterDestroy(() => {
      texture.createView({ format });
    }, awaitLost);
  });

g.test('createSampler')
  .desc(
    `
Tests creating samplers on destroyed device.
  `
  )
  .params(u => u.beginSubcases().combine('awaitLost', [true, false]))
  .fn(async t => {
    const { awaitLost } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createSampler();
    }, awaitLost);
  });

g.test('createBindGroupLayout')
  .desc(
    `
Tests creating bind group layouts on destroyed device. Tests valid combinations of:
  - Various valid binding entries
  - Maximum set of visibility for each binding entry
  `
  )
  .params(u =>
    u.combine('entry', allBindingEntries(false)).beginSubcases().combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { awaitLost, entry } = t.params;
    const visibility = bindingTypeInfo(entry).validStages;
    await t.executeAfterDestroy(() => {
      t.device.createBindGroupLayout({
        entries: [{ binding: 0, visibility, ...entry }],
      });
    }, awaitLost);
  });

g.test('createBindGroup')
  .desc(
    `
Tests creating bind group on destroyed device. Tests valid combinations of:
  - Various binded resource types
  - Various valid binding entries
  - Maximum set of visibility for each binding entry
  `
  )
  .desc(`A destroyed device should not be able to create any valid bind groups.`)
  .params(u =>
    u
      .combine('resourceType', kBindableResources)
      .combine('entry', allBindingEntries(false))
      .filter(({ resourceType, entry }) => {
        const info = bindingTypeInfo(entry);
        switch (info.resource) {
          // Either type of sampler may be bound to a filtering sampler binding.
          case 'filtSamp':
            return resourceType === 'filtSamp' || resourceType === 'nonFiltSamp';
          // But only non-filtering samplers can be used with non-filtering sampler bindings.
          case 'nonFiltSamp':
            return resourceType === 'nonFiltSamp';
          default:
            return info.resource === resourceType;
        }
      })
      .beginSubcases()
      .combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { awaitLost, resourceType, entry } = t.params;
    const visibility = bindingTypeInfo(entry).validStages;
    const layout = t.device.createBindGroupLayout({
      entries: [{ binding: 0, visibility, ...entry }],
    });
    const resource = t.getBindingResource(resourceType);
    await t.executeAfterDestroy(() => {
      t.device.createBindGroup({ layout, entries: [{ binding: 0, resource }] });
    }, awaitLost);
  });

g.test('createPipelineLayout')
  .desc(
    `
Tests creating pipeline layouts on destroyed device. Tests valid combinations of:
  - Various bind groups with valid binding entries
  - Maximum set of visibility for each binding entry
  `
  )
  .params(u =>
    u.combine('entry', allBindingEntries(false)).beginSubcases().combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { awaitLost, entry } = t.params;
    const visibility = bindingTypeInfo(entry).validStages;
    const bindGroupLayout = t.device.createBindGroupLayout({
      entries: [{ binding: 0, visibility, ...entry }],
    });
    await t.executeAfterDestroy(() => {
      t.device.createPipelineLayout({
        bindGroupLayouts: [bindGroupLayout],
      });
    }, awaitLost);
  });

g.test('createShaderModule')
  .desc(
    `
Tests creating shader modules on destroyed device.
  - Tests all shader stages: vertex, fragment, compute
  `
  )
  .params(u =>
    u.combine('stage', kShaderStageKeys).beginSubcases().combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { awaitLost, stage } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createShaderModule({ code: t.getNoOpShaderCode(stage) });
    }, awaitLost);
  });

g.test('createComputePipeline')
  .desc(
    `
Tests creating compute pipeline on destroyed device.
  - Tests with a valid no-op compute shader
  `
  )
  .params(u => u.beginSubcases().combine('awaitLost', [true, false]))
  .fn(async t => {
    const { awaitLost } = t.params;
    const cShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('COMPUTE') });
    await t.executeAfterDestroy(() => {
      t.device.createComputePipeline({
        layout: 'auto',
        compute: { module: cShader, entryPoint: 'main' },
      });
    }, awaitLost);
  });

g.test('createRenderPipeline')
  .desc(
    `
Tests creating render pipeline on destroyed device.
  - Tests with valid no-op vertex and fragment shaders
  `
  )
  .params(u => u.beginSubcases().combine('awaitLost', [true, false]))
  .fn(async t => {
    const { awaitLost } = t.params;
    const vShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('VERTEX') });
    const fShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('FRAGMENT') });
    await t.executeAfterDestroy(() => {
      t.device.createRenderPipeline({
        layout: 'auto',
        vertex: { module: vShader, entryPoint: 'main' },
        fragment: {
          module: fShader,
          entryPoint: 'main',
          targets: [{ format: 'rgba8unorm', writeMask: 0 }],
        },
      });
    }, awaitLost);
  });

g.test('createCommandEncoder')
  .desc(
    `
Tests creating command encoders on destroyed device.
  `
  )
  .params(u => u.beginSubcases().combine('awaitLost', [true, false]))
  .fn(async t => {
    const { awaitLost } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createCommandEncoder();
    }, awaitLost);
  });

g.test('createRenderBundleEncoder')
  .desc(
    `
Tests creating render bundle encoders on destroyed device.
  - Tests various renderable texture color formats
  `
  )
  .params(u =>
    u
      .combine('format', kRenderableColorTextureFormats)
      .beginSubcases()
      .combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { awaitLost, format } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createRenderBundleEncoder({ colorFormats: [format] });
    }, awaitLost);
  });

g.test('createQuerySet')
  .desc(
    `
Tests creating query sets on destroyed device.
  - Tests various query set types
  `
  )
  .params(u => u.combine('type', kQueryTypes).beginSubcases().combine('awaitLost', [true, false]))
  .beforeAllSubcases(t => {
    const { type } = t.params;
    t.selectDeviceForQueryTypeOrSkipTestCase(type);
  })
  .fn(async t => {
    const { awaitLost, type } = t.params;
    await t.executeAfterDestroy(() => {
      t.device.createQuerySet({ type, count: 4 });
    }, awaitLost);
  });

g.test('command,copyBufferToBuffer')
  .desc(
    `
Tests copyBufferToBuffer command with various uncompressed formats on destroyed device.
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const kBufferSize = 16;
    const src = t.device.createBuffer({
      size: kBufferSize,
      usage: GPUBufferUsage.COPY_SRC,
    });
    const dst = t.device.createBuffer({
      size: kBufferSize,
      usage: GPUBufferUsage.COPY_DST,
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.copyBufferToBuffer(src, 0, dst, 0, kBufferSize);
      return maker;
    });
  });

g.test('command,copyBufferToTexture')
  .desc(
    `
Tests copyBufferToTexture command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const format = 'rgba32uint';
    const { bytesPerBlock, blockWidth, blockHeight } = kTextureFormatInfo[format];
    const src = {
      buffer: t.device.createBuffer({
        size: bytesPerBlock,
        usage: GPUBufferUsage.COPY_SRC,
      }),
    };
    const dst = {
      texture: t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: GPUTextureUsage.COPY_DST,
        format,
      }),
    };
    const copySize = { width: blockWidth, height: blockHeight };
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.copyBufferToTexture(src, dst, copySize);
      return maker;
    });
  });

g.test('command,copyTextureToBuffer')
  .desc(
    `
Tests copyTextureToBuffer command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const format = 'rgba32uint';
    const { bytesPerBlock, blockWidth, blockHeight } = kTextureFormatInfo[format];
    const src = {
      texture: t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: GPUTextureUsage.COPY_SRC,
        format,
      }),
    };
    const dst = {
      buffer: t.device.createBuffer({
        size: bytesPerBlock,
        usage: GPUBufferUsage.COPY_DST,
      }),
    };
    const copySize = { width: blockWidth, height: blockHeight };
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.copyTextureToBuffer(src, dst, copySize);
      return maker;
    });
  });

g.test('command,copyTextureToTexture')
  .desc(
    `
Tests copyTextureToTexture command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const format = 'rgba32uint';
    const { blockWidth, blockHeight } = kTextureFormatInfo[format];
    const src = {
      texture: t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: GPUTextureUsage.COPY_SRC,
        format,
      }),
    };
    const dst = {
      texture: t.device.createTexture({
        size: { width: blockWidth, height: blockHeight },
        usage: GPUBufferUsage.COPY_DST,
        format,
      }),
    };
    const copySize = { width: blockWidth, height: blockHeight };
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.copyTextureToTexture(src, dst, copySize);
      return maker;
    });
  });

g.test('command,clearBuffer')
  .desc(
    `
Tests encoding and finishing a clearBuffer command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const kBufferSize = 16;
    const buffer = t.device.createBuffer({
      size: kBufferSize,
      usage: GPUBufferUsage.COPY_SRC,
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.clearBuffer(buffer, 0, kBufferSize);
      return maker;
    });
  });

g.test('command,writeTimestamp')
  .desc(
    `
Tests encoding and finishing a writeTimestamp command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u
      .combine('type', kQueryTypes)
      .beginSubcases()
      .combine('stage', kCommandValidationStages)
      .combine('awaitLost', [true, false])
  )
  .beforeAllSubcases(t => {
    const { type } = t.params;

    // writeTimestamp is only available for devices that enable the 'timestamp-query' feature.
    const queryTypes: GPUQueryType[] = ['timestamp'];
    if (type !== 'timestamp') {
      queryTypes.push(type);
    }

    t.selectDeviceForQueryTypeOrSkipTestCase(queryTypes);
  })
  .fn(async t => {
    const { type, stage, awaitLost } = t.params;
    const querySet = t.device.createQuerySet({ type, count: 2 });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.writeTimestamp(querySet, 0);
      return maker;
    });
  });

g.test('command,resolveQuerySet')
  .desc(
    `
Tests encoding and finishing a resolveQuerySet command on destroyed device.
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const kQueryCount = 2;
    const querySet = t.createQuerySetWithState('valid');
    const destination = t.createBufferWithState('valid', {
      size: kQueryCount * 8,
      usage: GPUBufferUsage.QUERY_RESOLVE,
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'non-pass', maker => {
      maker.encoder.resolveQuerySet(querySet, 0, 1, destination, 0);
      return maker;
    });
  });

g.test('command,computePass,dispatch')
  .desc(
    `
Tests encoding and dispatching a simple valid compute pass on destroyed device.
  - Binds valid pipeline and bindgroups, then dispatches
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const cShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('COMPUTE') });
    const pipeline = t.device.createComputePipeline({
      layout: 'auto',
      compute: { module: cShader, entryPoint: 'main' },
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'compute pass', maker => {
      maker.encoder.setPipeline(pipeline);
      maker.encoder.dispatchWorkgroups(1);
      return maker;
    });
  });

g.test('command,renderPass,draw')
  .desc(
    `
Tests encoding and finishing a simple valid render pass on destroyed device.
  - Binds valid pipeline and bindgroups, then draws
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const vShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('VERTEX') });
    const fShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('FRAGMENT') });
    const pipeline = t.device.createRenderPipeline({
      layout: 'auto',
      vertex: { module: vShader, entryPoint: 'main' },
      fragment: {
        module: fShader,
        entryPoint: 'main',
        targets: [{ format: 'rgba8unorm', writeMask: 0 }],
      },
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'render pass', maker => {
      maker.encoder.setPipeline(pipeline);
      maker.encoder.draw(0);
      return maker;
    });
  });

g.test('command,renderPass,renderBundle')
  .desc(
    `
Tests encoding and drawing a render pass including a render bundle on destroyed device.
  - Binds valid pipeline and bindgroups, executes render bundle, then draws
  - Tests finishing encoding on destroyed device
  - Tests submitting command on destroyed device
  `
  )
  .params(u =>
    u.beginSubcases().combine('stage', kCommandValidationStages).combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { stage, awaitLost } = t.params;
    const vShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('VERTEX') });
    const fShader = t.device.createShaderModule({ code: t.getNoOpShaderCode('FRAGMENT') });
    const pipeline = t.device.createRenderPipeline({
      layout: 'auto',
      vertex: { module: vShader, entryPoint: 'main' },
      fragment: {
        module: fShader,
        entryPoint: 'main',
        targets: [{ format: 'rgba8unorm', writeMask: 0 }],
      },
    });
    await t.executeCommandsAfterDestroy(stage, awaitLost, 'render bundle', maker => {
      maker.encoder.setPipeline(pipeline);
      maker.encoder.draw(0);
      return maker;
    });
  });

g.test('queue,writeBuffer')
  .desc(
    `
Tests writeBuffer on queue on destroyed device.
  `
  )
  .params(u =>
    u.combine('numElements', [4, 8, 16]).beginSubcases().combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { numElements, awaitLost } = t.params;
    const buffer = t.device.createBuffer({
      size: numElements,
      usage: GPUBufferUsage.COPY_DST,
    });
    const data = new Uint8Array(numElements);
    await t.executeAfterDestroy(() => {
      t.device.queue.writeBuffer(buffer, 0, data);
    }, awaitLost);
  });

g.test('queue,writeTexture,2d,uncompressed_format')
  .desc(
    `
Tests writeTexture on queue on destroyed device with uncompressed formats.
  `
  )
  .params(u =>
    u.combine('format', kRegularTextureFormats).beginSubcases().combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { format, awaitLost } = t.params;
    const { blockWidth, blockHeight, bytesPerBlock } = kTextureFormatInfo[format];
    const data = new Uint8Array(bytesPerBlock);
    const texture = t.device.createTexture({
      size: { width: blockWidth, height: blockHeight },
      usage: GPUTextureUsage.COPY_DST,
      format,
    });
    await t.executeAfterDestroy(() => {
      t.device.queue.writeTexture(
        { texture },
        data,
        {},
        { width: blockWidth, height: blockHeight }
      );
    }, awaitLost);
  });

g.test('queue,writeTexture,2d,compressed_format')
  .desc(
    `
Tests writeTexture on queue on destroyed device with compressed formats.
  `
  )
  .params(u =>
    u
      .combine('format', kCompressedTextureFormats)
      .beginSubcases()
      .combine('awaitLost', [true, false])
  )
  .beforeAllSubcases(t => {
    const { format } = t.params;
    t.selectDeviceOrSkipTestCase(kTextureFormatInfo[format].feature);
  })
  .fn(async t => {
    const { format, awaitLost } = t.params;
    const { blockWidth, blockHeight, bytesPerBlock } = kTextureFormatInfo[format];
    const data = new Uint8Array(bytesPerBlock);
    const texture = t.device.createTexture({
      size: { width: blockWidth, height: blockHeight },
      usage: GPUTextureUsage.COPY_DST,
      format,
    });
    await t.executeAfterDestroy(() => {
      t.device.queue.writeTexture(
        { texture },
        data,
        {},
        { width: blockWidth, height: blockHeight }
      );
    }, awaitLost);
  });

g.test('queue,copyExternalImageToTexture,canvas')
  .desc(
    `
Tests copyExternalImageToTexture from canvas on queue on destroyed device.
  `
  )
  .params(u =>
    u
      .combine('canvasType', kAllCanvasTypes)
      .combine('contextType', kValidCanvasContextIds)
      .filter(({ contextType }) => {
        return canCopyFromCanvasContext(contextType);
      })
      .beginSubcases()
      .combine('awaitLost', [true, false])
  )
  .fn(async t => {
    const { canvasType, contextType, awaitLost } = t.params;
    const canvas = createCanvas(t, canvasType, 1, 1);
    const texture = t.device.createTexture({
      size: { width: 1, height: 1 },
      format: 'bgra8unorm',
      usage: GPUTextureUsage.COPY_DST,
    });

    const ctx = ((canvas as unknown) as HTMLCanvasElement).getContext(contextType);
    if (ctx === null) {
      t.skip('Failed to get context for canvas element');
      return;
    }
    t.tryTrackForCleanup(ctx);

    await t.executeAfterDestroy(() => {
      t.device.queue.copyExternalImageToTexture(
        { source: canvas },
        { texture },
        { width: 1, height: 1 }
      );
    }, awaitLost);
  });

g.test('queue,copyExternalImageToTexture,imageBitmap')
  .desc(
    `
Tests copyExternalImageToTexture from canvas on queue on destroyed device.
  `
  )
  .params(u => u.beginSubcases().combine('awaitLost', [true, false]))
  .fn(async t => {
    const { awaitLost } = t.params;
    if (typeof createImageBitmap === 'undefined') {
      t.skip('Creating ImageBitmaps is not supported.');
    }
    const imageBitmap = await createImageBitmap(new ImageData(new Uint8ClampedArray(4), 1, 1));

    const texture = t.device.createTexture({
      size: { width: 1, height: 1 },
      format: 'bgra8unorm',
      usage: GPUTextureUsage.COPY_DST,
    });

    await t.executeAfterDestroy(() => {
      t.device.queue.copyExternalImageToTexture(
        { source: imageBitmap },
        { texture },
        { width: 1, height: 1 }
      );
    }, awaitLost);
  });