media/W3C/video/events/event_progress_manual.html is a flaky failure
[WebKit-https.git] / LayoutTests / webgpu / compute-squares.html
1 <!DOCTYPE html><!-- webkit-test-runner [ experimental:WebGPUEnabled=true ] -->
2 <meta charset=utf-8>
3 <title>Execute a simple compute shader with Web GPU.</title>
4 <body>
5 <script src="../resources/testharness.js"></script>
6 <script src="../resources/testharnessreport.js"></script>
7 <script src="js/webgpu-functions.js"></script>
8 <script>
9 if (window.testRunner)
10     testRunner.waitUntilDone();
11
12 const data = new Uint32Array([2, 3, 4, 5, 6, 7, 8, 9, 10]);
13
14 const dataBinding = 0;
15 const bindGroupIndex = 0;
16
17 const shaderCode = `
18 #include <metal_stdlib>
19
20 struct Data {
21     device unsigned* numbers [[id(${dataBinding})]];
22 };
23
24 kernel void compute(device Data& data [[buffer(${bindGroupIndex})]], unsigned gid [[thread_position_in_grid]])
25 {
26     if (gid >= ${data.length})
27         return;
28
29     unsigned original = data.numbers[gid];
30     data.numbers[gid] = original * original;
31 }
32 `
33
34 promise_test(async () => {
35     
36     const device = await getBasicDevice();
37     
38     const shaderModule = device.createShaderModule({ code: shaderCode, isWHLSL: false });
39     const computeStageDescriptor = { module: shaderModule, entryPoint: "compute" };
40     const pipeline = device.createComputePipeline({ computeStage: computeStageDescriptor });
41     
42     const dataBuffer = device.createBuffer({ size: data.byteLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ });
43     dataBuffer.setSubData(0, data.buffer);
44     
45     const bgLayoutBinding = { binding: dataBinding, visibility: GPUShaderStageBit.COMPUTE, type: "storage-buffer" };
46     const bgLayout = device.createBindGroupLayout({ bindings: [bgLayoutBinding] });
47     
48     const bufferBinding = { buffer: dataBuffer, offset: 0, size: data.byteLength };
49     const bgBinding = { binding: dataBinding, resource: bufferBinding };
50     
51     const bindGroupDescriptor = { layout: bgLayout, bindings: [bgBinding] };
52     const bindGroup = device.createBindGroup(bindGroupDescriptor);
53     
54     const commandEncoder = device.createCommandEncoder();
55     const passEncoder = commandEncoder.beginComputePass();
56     
57     passEncoder.setBindGroup(bindGroupIndex, bindGroup);
58     
59     passEncoder.setPipeline(pipeline);
60     
61     passEncoder.dispatch(data.length, 1, 1);
62     passEncoder.endPass();
63     
64     device.getQueue().submit([commandEncoder.finish()]);
65     
66     const readDataArrayBuffer = await dataBuffer.mapReadAsync();
67     assert_not_equals(readDataArrayBuffer, null, "Async read promise resolved successfully");
68     
69     const readData = new Uint32Array(readDataArrayBuffer);
70
71     for (var i = 0; i < readData.length; ++i)
72         assert_equals(readData[i], data[i] * data[i], "Data was succesfully squared");
73
74     if (window.testRunner)
75         testRunner.notifyDone();
76 }, "Successfully executed a basic compute pass");
77 </script>
78 </body>