1import { unreachable } from '../../../common/framework/util/util.js';
2
3import { runRefTest } from './gpu_ref_test.js';
4
5// <canvas> element from html page
6declare const cvs: HTMLCanvasElement;
7
8export function run(format: GPUTextureFormat) {
9  runRefTest(async t => {
10    const ctx = (cvs.getContext('gpupresent') as unknown) as GPUCanvasContext;
11
12    switch (format) {
13      case 'bgra8unorm':
14      case 'rgba16float':
15        break;
16      default:
17        unreachable();
18    }
19
20    const swapChain = ctx.configureSwapChain({
21      device: t.device,
22      format,
23      usage: GPUTextureUsage.COPY_DST,
24    });
25
26    const rows = 2;
27    const bytesPerRow = 256;
28    const buffer = t.device.createBuffer({
29      mappedAtCreation: true,
30      size: rows * bytesPerRow,
31      usage: GPUBufferUsage.COPY_SRC,
32    });
33    const mapping = buffer.getMappedRange();
34    switch (format) {
35      case 'bgra8unorm':
36        {
37          const data = new Uint8Array(mapping);
38          data.set(new Uint8Array([0x00, 0x00, 0x7f, 0xff]), 0); // red
39          data.set(new Uint8Array([0x00, 0x7f, 0x00, 0xff]), 4); // green
40          data.set(new Uint8Array([0x7f, 0x00, 0x00, 0xff]), 256 + 0); // blue
41          data.set(new Uint8Array([0x00, 0x7f, 0x7f, 0xff]), 256 + 4); // yellow
42        }
43        break;
44      case 'rgba16float':
45        {
46          // Untested!
47          const zero = 0x0000;
48          const half = 0x3800;
49          const one = 0x3c00;
50          const data = new DataView(mapping);
51          data.setUint16(0x000, half, false); // red
52          data.setUint16(0x002, zero, false);
53          data.setUint16(0x004, zero, false);
54          data.setUint16(0x008, one, false);
55          data.setUint16(0x010, zero, false); // green
56          data.setUint16(0x020, half, false);
57          data.setUint16(0x040, zero, false);
58          data.setUint16(0x080, one, false);
59          data.setUint16(0x100, zero, false); // blue
60          data.setUint16(0x102, zero, false);
61          data.setUint16(0x104, half, false);
62          data.setUint16(0x108, one, false);
63          data.setUint16(0x110, half, false); // yellow
64          data.setUint16(0x120, half, false);
65          data.setUint16(0x140, zero, false);
66          data.setUint16(0x180, one, false);
67        }
68        break;
69    }
70    buffer.unmap();
71
72    const texture = swapChain.getCurrentTexture();
73
74    const encoder = t.device.createCommandEncoder();
75    encoder.copyBufferToTexture({ buffer, bytesPerRow }, { texture }, [2, 2, 1]);
76    t.device.defaultQueue.submit([encoder.finish()]);
77  });
78}
79