[WHLSL] Move WHLSL tests to their own folder
[WebKit-https.git] / LayoutTests / webgpu / whlsl / simple-arrays.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="../js/webgpu-functions.js"></script>
6 </head>
7 <body>
8 <script>
9 const shaderSource = `
10 void fill(thread int[] array, int value) {
11     for (uint i = 0; i < array.length; i++) {
12         array[i] = value;
13     }
14 }
15
16 [numthreads(1, 1, 1)]
17 compute void computeShader(device int[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
18     int[42] array;
19     if (array.length != 42)
20         return;
21     for (uint i = 0; i < array.length; ++i) {
22         if (array[i] != 0)
23             return;
24     }
25
26     array[0] = 517;
27     if (array[0] != 517)
28         return;
29
30     thread int[] arrayPtr = @array;
31     if (arrayPtr.length != 42)
32         return;
33
34     int[42] array2;
35     array2 = array;
36     if (array2.length != 42)
37         return;
38     if (array2[0] != 517)
39         return;
40
41     fill(arrayPtr, 1337);
42     for (uint i = 0; i < arrayPtr.length; ++i) {
43         if (arrayPtr[i] != 1337)
44             return;
45         if (array[i] != 1337)
46             return;
47     }
48
49     if (array2[0] != 517)
50         return;
51     if (array2.length != 42)
52         return;
53     for (uint i = 1; i < array2.length; ++i) {
54         if (array2[i] != 0)
55             return;
56     }
57
58     buffer[0] = 1;
59 }
60 `;
61 async function start(device) {
62     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
63     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
64
65     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
66     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
67     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
68     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
69
70     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
71     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
72
73     const size = Int32Array.BYTES_PER_ELEMENT * 1;
74
75     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
76     const buffer = device.createBuffer(bufferDescriptor);
77     const bufferArrayBuffer = await buffer.mapWriteAsync();
78     const bufferFloat32Array = new Int32Array(bufferArrayBuffer);
79     bufferFloat32Array[0] = 0;
80     buffer.unmap();
81
82     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
83     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
84
85     const bufferBinding = {buffer: resultsBuffer, size};
86     const bindGroupBinding = {binding: 0, resource: bufferBinding};
87     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
88     const bindGroup = device.createBindGroup(bindGroupDescriptor);
89
90     const commandEncoder = device.createCommandEncoder(); // {}
91     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
92     const computePassEncoder = commandEncoder.beginComputePass();
93     computePassEncoder.setPipeline(computePipeline);
94     computePassEncoder.setBindGroup(0, bindGroup);
95     computePassEncoder.dispatch(1, 1, 1);
96     computePassEncoder.endPass();
97     const commandBuffer = commandEncoder.finish();
98     device.getQueue().submit([commandBuffer]);
99
100     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
101     let resultsInt32Array = new Int32Array(resultsArrayBuffer);
102     if (resultsInt32Array[0] === 1)
103         testPassed("");
104     else
105         testFailed("");
106     resultsBuffer.unmap();
107 }
108 window.jsTestIsAsync = true;
109 getBasicDevice().then(function(device) {
110     start(device).then(function() {
111         finishJSTest();
112     }, function() {
113         testFailed("");
114         finishJSTest();
115     });
116 }, function() {
117     testPassed("");
118     finishJSTest();
119 });
120 </script>
121 <script src="../../resources/js-test-post.js"></script>
122 </body>
123 </html>