summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/mochitest/test_queue_copyExternalImageToTexture.html
blob: 279b4a52b4c22302065e0e50226138fa5b80f929 (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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
  </head>

  <body>
    <script type="text/javascript">
      "use strict";

      ok(
        SpecialPowers.getBoolPref("dom.webgpu.enabled"),
        "WebGPU pref should be enabled."
      );
      ok(
        SpecialPowers.getBoolPref("gfx.offscreencanvas.enabled"),
        "OffscreenCanvas pref should be enabled."
      );

      SimpleTest.waitForExplicitFinish();

      function requestAnimationFramePromise() {
        return new Promise(requestAnimationFrame);
      }

      function createSourceCanvasWebgl() {
        const offscreenCanvas = new OffscreenCanvas(200, 200);
        const gl = offscreenCanvas.getContext("webgl");

        const COLOR_VALUE = 127.0 / 255.0;
        const ALPHA_VALUE = 127.0 / 255.0;

        gl.enable(gl.SCISSOR_TEST);

        gl.scissor(0, 0, 100, 100);
        gl.clearColor(COLOR_VALUE, 0.0, 0.0, ALPHA_VALUE);
        gl.clear(gl.COLOR_BUFFER_BIT);

        gl.scissor(100, 0, 100, 100);
        gl.clearColor(0.0, COLOR_VALUE, 0.0, ALPHA_VALUE);
        gl.clear(gl.COLOR_BUFFER_BIT);

        gl.scissor(0, 100, 100, 100);
        gl.clearColor(0.0, 0.0, COLOR_VALUE, ALPHA_VALUE);
        gl.clear(gl.COLOR_BUFFER_BIT);

        gl.scissor(100, 100, 100, 100);
        gl.clearColor(0.0, 0.0, 0.0, ALPHA_VALUE);
        gl.clear(gl.COLOR_BUFFER_BIT);

        return {
          source: offscreenCanvas,
          origin: { x: 0, y: 0 },
          flipY: true,
        };
      }

      function createSourceCanvas2d() {
        const offscreenCanvas = new OffscreenCanvas(200, 200);
        const context = offscreenCanvas.getContext("2d");

        context.fillStyle = "rgba(255,0,0,0.498)";
        context.fillRect(0, 0, 100, 100);

        context.fillStyle = "rgba(0,255,0,0.498)";
        context.fillRect(100, 0, 100, 100);

        context.fillStyle = "rgba(0,0,255,0.498)";
        context.fillRect(0, 100, 100, 100);

        context.fillStyle = "rgba(0,0,0,0.498)";
        context.fillRect(100, 100, 100, 100);

        return {
          source: offscreenCanvas,
          origin: { x: 0, y: 0 },
          flipY: false,
        };
      }

      function createSourceImageBitmap() {
        const sourceCanvas = createSourceCanvas2d();
        return {
          source: sourceCanvas.source.transferToImageBitmap(),
          origin: { x: 0, y: 0 },
          flipY: false,
        };
      }

      async function mapDestTexture(
        device,
        source,
        destFormat,
        premultiply,
        copySize
      ) {
        const bytesPerRow = 256 * 4; // 256 aligned for 200 pixels
        const texture = device.createTexture({
          format: destFormat,
          size: copySize,
          usage: GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST,
        });

        device.queue.copyExternalImageToTexture(
          source,
          { texture, premultipliedAlpha: premultiply },
          copySize
        );

        const buffer = device.createBuffer({
          size: 1024 * 200,
          usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
        });

        const encoder = device.createCommandEncoder();
        encoder.copyTextureToBuffer(
          { texture },
          { buffer, bytesPerRow },
          copySize
        );
        device.queue.submit([encoder.finish()]);

        await buffer.mapAsync(GPUMapMode.READ);
        return buffer;
      }

      async function verifyBuffer(
        test,
        device,
        source,
        format,
        premultiply,
        copyDim,
        topLeftPixelData
      ) {
        try {
          const buffer = await mapDestTexture(
            device,
            source,
            format,
            premultiply,
            copyDim
          );
          const arrayBuffer = buffer.getMappedRange();
          const view = new Uint8Array(arrayBuffer);
          for (let i = 0; i < topLeftPixelData.length; ++i) {
            is(
              view[i],
              topLeftPixelData[i],
              test +
                " " +
                format +
                " (" +
                source.origin.x +
                "," +
                source.origin.y +
                ") channel " +
                i
            );
          }
        } catch (e) {
          ok(false, "WebGPU exception: " + e);
        }
      }

      async function verifySourceCanvas(test, device, source) {
        await verifyBuffer(
          test,
          device,
          source,
          "rgba8unorm",
          /* premultiply */ true,
          { width: 200, height: 200 },
          [127, 0, 0, 127]
        );
        await verifyBuffer(
          test,
          device,
          source,
          "bgra8unorm",
          /* premultiply */ true,
          { width: 200, height: 200 },
          [0, 0, 127, 127]
        );
        await verifyBuffer(
          test,
          device,
          source,
          "rgba8unorm",
          /* premultiply */ false,
          { width: 200, height: 200 },
          [255, 0, 0, 127]
        );
        await verifyBuffer(
          test,
          device,
          source,
          "bgra8unorm",
          /* premultiply */ false,
          { width: 200, height: 200 },
          [0, 0, 255, 127]
        );

        // The copy is flipped but the origin is relative to the original source data,
        // so we need to invert for WebGL.
        const topRightPixelData =
          test === "webgl" ? [0, 0, 0, 127] : [0, 127, 0, 127];
        const topRightOrigin = { origin: { x: 100, y: 0 } };
        await verifyBuffer(
          test,
          device,
          { ...source, ...topRightOrigin },
          "bgra8unorm",
          /* premultiply */ true,
          { width: 100, height: 100 },
          topRightPixelData
        );

        const bottomLeftPixelData =
          test === "webgl" ? [0, 0, 127, 127] : [127, 0, 0, 127];
        const bottomLeftOrigin = { origin: { x: 0, y: 100 } };
        await verifyBuffer(
          test,
          device,
          { ...source, ...bottomLeftOrigin },
          "bgra8unorm",
          /* premultiply */ true,
          { width: 100, height: 100 },
          bottomLeftPixelData
        );
      }

      async function writeDestCanvas(source2d, sourceWebgl, sourceImageBitmap) {
        const adapter = await navigator.gpu.requestAdapter();
        const device = await adapter.requestDevice();
        await verifySourceCanvas("2d", device, source2d);
        await verifySourceCanvas("imageBitmap", device, sourceImageBitmap);
        await verifySourceCanvas("webgl", device, sourceWebgl);
      }

      async function runTest() {
        try {
          const source2d = createSourceCanvas2d();
          const sourceWebgl = createSourceCanvasWebgl();
          const sourceImageBitmap = createSourceImageBitmap();
          await requestAnimationFramePromise();
          await requestAnimationFramePromise();
          await writeDestCanvas(source2d, sourceWebgl, sourceImageBitmap);
        } catch (e) {
          ok(false, "Uncaught exception: " + e);
        } finally {
          SimpleTest.finish();
        }
      }

      runTest();
    </script>
  </body>
</html>