4 <meta charset=
"utf-8" />
5 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css" />
10 <script type=
"text/javascript">
14 SpecialPowers
.getBoolPref("dom.webgpu.enabled"),
15 "WebGPU pref should be enabled."
18 SimpleTest
.waitForExplicitFinish();
20 function requestAnimationFramePromise() {
21 return new Promise(requestAnimationFrame
);
24 function createSourceCanvasWebgl() {
25 const offscreenCanvas
= new OffscreenCanvas(200, 200);
26 const gl
= offscreenCanvas
.getContext("webgl");
28 const COLOR_VALUE
= 127.0 / 255.0;
29 const ALPHA_VALUE
= 127.0 / 255.0;
31 gl
.enable(gl
.SCISSOR_TEST
);
33 gl
.scissor(0, 0, 100, 100);
34 gl
.clearColor(COLOR_VALUE
, 0.0, 0.0, ALPHA_VALUE
);
35 gl
.clear(gl
.COLOR_BUFFER_BIT
);
37 gl
.scissor(100, 0, 100, 100);
38 gl
.clearColor(0.0, COLOR_VALUE
, 0.0, ALPHA_VALUE
);
39 gl
.clear(gl
.COLOR_BUFFER_BIT
);
41 gl
.scissor(0, 100, 100, 100);
42 gl
.clearColor(0.0, 0.0, COLOR_VALUE
, ALPHA_VALUE
);
43 gl
.clear(gl
.COLOR_BUFFER_BIT
);
45 gl
.scissor(100, 100, 100, 100);
46 gl
.clearColor(0.0, 0.0, 0.0, ALPHA_VALUE
);
47 gl
.clear(gl
.COLOR_BUFFER_BIT
);
50 source
: offscreenCanvas
,
51 origin
: { x
: 0, y
: 0 },
56 function createSourceCanvas2d() {
57 const offscreenCanvas
= new OffscreenCanvas(200, 200);
58 const context
= offscreenCanvas
.getContext("2d");
60 context
.fillStyle
= "rgba(255,0,0,0.498)";
61 context
.fillRect(0, 0, 100, 100);
63 context
.fillStyle
= "rgba(0,255,0,0.498)";
64 context
.fillRect(100, 0, 100, 100);
66 context
.fillStyle
= "rgba(0,0,255,0.498)";
67 context
.fillRect(0, 100, 100, 100);
69 context
.fillStyle
= "rgba(0,0,0,0.498)";
70 context
.fillRect(100, 100, 100, 100);
73 source
: offscreenCanvas
,
74 origin
: { x
: 0, y
: 0 },
79 function createSourceImageBitmap() {
80 const sourceCanvas
= createSourceCanvas2d();
82 source
: sourceCanvas
.source
.transferToImageBitmap(),
83 origin
: { x
: 0, y
: 0 },
88 async
function mapDestTexture(
95 const bytesPerRow
= 256 * 4; // 256 aligned for 200 pixels
96 const texture
= device
.createTexture({
99 usage
: GPUTextureUsage
.COPY_SRC
| GPUTextureUsage
.COPY_DST
,
102 device
.queue
.copyExternalImageToTexture(
104 { texture
, premultipliedAlpha
: premultiply
},
108 const buffer
= device
.createBuffer({
110 usage
: GPUBufferUsage
.MAP_READ
| GPUBufferUsage
.COPY_DST
,
113 const encoder
= device
.createCommandEncoder();
114 encoder
.copyTextureToBuffer(
116 { buffer
, bytesPerRow
},
119 device
.queue
.submit([encoder
.finish()]);
121 await buffer
.mapAsync(GPUMapMode
.READ
);
125 async
function verifyBuffer(
135 const buffer
= await
mapDestTexture(
142 const arrayBuffer
= buffer
.getMappedRange();
143 const view
= new Uint8Array(arrayBuffer
);
144 for (let i
= 0; i
< topLeftPixelData
.length
; ++i
) {
160 ok(false, "WebGPU exception: " + e
);
164 async
function verifySourceCanvas(test
, device
, source
) {
170 /* premultiply */ true,
171 { width
: 200, height
: 200 },
179 /* premultiply */ true,
180 { width
: 200, height
: 200 },
188 /* premultiply */ false,
189 { width
: 200, height
: 200 },
197 /* premultiply */ false,
198 { width
: 200, height
: 200 },
202 // The copy is flipped but the origin is relative to the original source data,
203 // so we need to invert for WebGL.
204 const topRightPixelData
=
205 test
=== "webgl" ? [0, 0, 0, 127] : [0, 127, 0, 127];
206 const topRightOrigin
= { origin
: { x
: 100, y
: 0 } };
210 { ...source
, ...topRightOrigin
},
212 /* premultiply */ true,
213 { width
: 100, height
: 100 },
217 const bottomLeftPixelData
=
218 test
=== "webgl" ? [0, 0, 127, 127] : [127, 0, 0, 127];
219 const bottomLeftOrigin
= { origin
: { x
: 0, y
: 100 } };
223 { ...source
, ...bottomLeftOrigin
},
225 /* premultiply */ true,
226 { width
: 100, height
: 100 },
231 async
function writeDestCanvas(source2d
, sourceWebgl
, sourceImageBitmap
) {
232 const adapter
= await navigator
.gpu
.requestAdapter();
233 const device
= await adapter
.requestDevice();
234 await
verifySourceCanvas("2d", device
, source2d
);
235 await
verifySourceCanvas("imageBitmap", device
, sourceImageBitmap
);
236 await
verifySourceCanvas("webgl", device
, sourceWebgl
);
239 async
function runTest() {
241 const source2d
= createSourceCanvas2d();
242 const sourceWebgl
= createSourceCanvasWebgl();
243 const sourceImageBitmap
= createSourceImageBitmap();
244 await
requestAnimationFramePromise();
245 await
requestAnimationFramePromise();
246 await
writeDestCanvas(source2d
, sourceWebgl
, sourceImageBitmap
);
248 ok(false, "Uncaught exception: " + e
);