[WHLSL] The checker needs to resolve types for the anonymous variables in ReadModifyW...
[WebKit-https.git] / LayoutTests / webgpu / whlsl-checker-should-set-type-of-read-modify-write-variables.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 [numthreads(1, 1, 1)]
10 compute void computeShader(device float[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
11     float4 vec;
12     vec[0] += 42.0;
13     buffer[uint(threadID.x)] = vec[0];
14 }
15 `;
16 let resultsFloat32Array;
17 async function start() {
18     const adapter = await navigator.gpu.requestAdapter();
19     const device = await adapter.requestDevice();
20
21     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
22     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
23
24     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
25     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
26     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
27     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
28
29     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
30     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
31
32     const size = Float32Array.BYTES_PER_ELEMENT * 1;
33
34     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
35     const buffer = device.createBuffer(bufferDescriptor);
36     const bufferArrayBuffer = await buffer.mapWriteAsync();
37     const bufferFloat32Array = new Float32Array(bufferArrayBuffer);
38     bufferFloat32Array[0] = 0;
39     buffer.unmap();
40
41     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
42     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
43
44     const bufferBinding = {buffer: resultsBuffer, size};
45     const bindGroupBinding = {binding: 0, resource: bufferBinding};
46     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
47     const bindGroup = device.createBindGroup(bindGroupDescriptor);
48
49     const commandEncoder = device.createCommandEncoder(); // {}
50     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
51     const computePassEncoder = commandEncoder.beginComputePass();
52     computePassEncoder.setPipeline(computePipeline);
53     computePassEncoder.setBindGroup(0, bindGroup);
54     computePassEncoder.dispatch(1, 1, 1);
55     computePassEncoder.endPass();
56     const commandBuffer = commandEncoder.finish();
57     device.getQueue().submit([commandBuffer]);
58
59     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
60     resultsFloat32Array = new Float32Array(resultsArrayBuffer);
61     shouldBe("resultsFloat32Array[0]", "42");
62     resultsBuffer.unmap();
63 }
64 if (window.testRunner)
65     testRunner.waitUntilDone();
66 window.addEventListener("load", function() {
67     start().then(function() {
68         if (window.testRunner)
69             testRunner.notifyDone();
70     }, function() {
71         if (window.testRunner)
72             testRunner.notifyDone();
73     });
74 });
75 </script>
76 <script src="../resources/js-test-post.js"></script>
77 </body>
78 </html>