Assertion failed in JSC::createError
[WebKit-https.git] / LayoutTests / fast / canvas / webmetal / webmetal-dispatch.html
1 <script id="script" type="x-shader/x-metal">
2 using namespace metal;
3
4 kernel void get_kernel_size(device int *result[[buffer(0)]],
5                             uint3 grid_size[[threadgroups_per_grid]],
6                             uint3 group_size[[threads_per_threadgroup]],
7                             uint3 gid[[thread_position_in_grid]])
8 {
9     if (gid.x != 0 || gid.y != 0 || gid.z != 0) 
10         return;
11
12     result[0] = grid_size.x;
13     result[1] = grid_size.y;
14     result[2] = grid_size.z;
15     result[3] = group_size.x;
16     result[4] = group_size.y;
17     result[5] = group_size.z;
18 }
19 </script>
20
21 <script>
22 if (window.testRunner) {
23     window.testRunner.dumpAsText();
24     window.testRunner.waitUntilDone();
25 }
26
27 function assert(a) {
28     if (!a) {
29         throw new Error('Assertion Failed.');
30     }
31 }
32
33 function run() {
34     let output = document.getElementById("output");
35
36     if (!window.testRunner) {
37         output.textContent = "This test only runs inside DRT/WKTR.";
38         return;
39     }
40
41     window.internals.settings.setWebMetalEnabled(true);
42     let gpu = document.createElement("canvas").getContext("webmetal");
43     let commandQueue = gpu.createCommandQueue();
44
45     let library = gpu.createLibrary(document.getElementById("script").text);
46     let kernelFunction = library.functionWithName("get_kernel_size");
47     let pipelineState = gpu.createComputePipelineState(kernelFunction);
48
49     let resultBuffer = gpu.createBuffer(new Int32Array(6));
50     
51     let commandBuffer = commandQueue.createCommandBuffer();
52     let commandEncoder = commandBuffer.createComputeCommandEncoder();
53
54     let gridSize = {
55         width: 7,
56         height: 6,
57         depth: 5
58     };
59     let groupSize = {
60         width: 4,
61         height: 3,
62         depth: 2
63     };
64     
65     commandEncoder.setComputePipelineState(pipelineState);
66     commandEncoder.setBuffer(resultBuffer, 0, 0);
67     commandEncoder.dispatch(gridSize, groupSize);
68     commandEncoder.endEncoding();
69
70     commandBuffer.completed.then(() => {
71         let result = new Int32Array(resultBuffer.contents);
72
73         try {
74             assert(result[0] === gridSize.width);
75             assert(result[1] === gridSize.height);
76             assert(result[2] === gridSize.depth);
77             assert(result[3] === groupSize.width);
78             assert(result[4] === groupSize.height);
79             assert(result[5] === groupSize.depth);
80
81         } catch(err) {
82             output.textContent = `FAIL: ${err.message}`;
83             return;
84         }
85
86         output.textContent = 'PASS';
87         window.testRunner.notifyDone();
88     });
89
90     commandBuffer.commit();
91 }
92
93 window.addEventListener("load", run, false);
94
95 </script>
96 <div id="output"></div>