REGRESSION: ( r246394 ) webgpu/whlsl-buffer-fragment.html and webgpu/whlsl-buffer...
[WebKit-https.git] / LayoutTests / webgpu / whlsl-compute.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="js/webgpu-functions.js"></script>
5 <script src="../resources/js-test-pre.js"></script>
6 </head>
7 <body>
8 <script>
9 const shaderSource = `
10 [numthreads(2, 1, 1)]
11 compute void computeShader(device float[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
12     buffer[uint(threadID.x)] = buffer[uint(threadID.x)] * 2.0;
13 }
14 `;
15
16 async function start(device) {
17     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
18     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
19
20     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
21     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
22     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
23     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
24
25     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
26     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
27
28     const size = Float32Array.BYTES_PER_ELEMENT * 8;
29
30     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
31     const buffer = device.createBuffer(bufferDescriptor);
32     const bufferArrayBuffer = await buffer.mapWriteAsync();
33     const bufferFloat32Array = new Float32Array(bufferArrayBuffer);
34     bufferFloat32Array[0] = 1;
35     bufferFloat32Array[1] = 2;
36     bufferFloat32Array[2] = 3;
37     bufferFloat32Array[3] = 4;
38     bufferFloat32Array[4] = 5;
39     bufferFloat32Array[5] = 6;
40     bufferFloat32Array[6] = 7;
41     bufferFloat32Array[7] = 8;
42     buffer.unmap();
43
44     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
45     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
46
47     const bufferBinding = {buffer: resultsBuffer, size};
48     const bindGroupBinding = {binding: 0, resource: bufferBinding};
49     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
50     const bindGroup = device.createBindGroup(bindGroupDescriptor);
51
52     const commandEncoder = device.createCommandEncoder(); // {}
53     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
54     const computePassEncoder = commandEncoder.beginComputePass();
55     computePassEncoder.setPipeline(computePipeline);
56     computePassEncoder.setBindGroup(0, bindGroup);
57     computePassEncoder.dispatch(2, 1, 1);
58     computePassEncoder.endPass();
59     const commandBuffer = commandEncoder.finish();
60     device.getQueue().submit([commandBuffer]);
61
62     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
63     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
64     if (resultsFloat32Array[0] == 2
65         && resultsFloat32Array[1] == 4
66         && resultsFloat32Array[2] == 6
67         && resultsFloat32Array[3] == 8
68         && resultsFloat32Array[4] == 5
69         && resultsFloat32Array[5] == 6
70         && resultsFloat32Array[6] == 7
71         && resultsFloat32Array[7] == 8)
72         testPassed("");
73     else
74         testFailed("");
75     resultsBuffer.unmap();
76 }
77 window.jsTestIsAsync = true;
78 getBasicDevice().then(function(device) {
79     start(device).then(function() {
80         finishJSTest();
81     }, function() {
82         testFailed("");
83         finishJSTest();
84     });
85 }, function() {
86     testPassed("");
87     finishJSTest();
88 });
89 </script>
90 <script src="../resources/js-test-post.js"></script>
91 </body>
92 </html>