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