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