cffd18126df72bcf960b8efc06333bc16f3b6b48
[WebKit-https.git] / LayoutTests / webgpu / whlsl / sparse-bind-group-3.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(1, 1, 1)]
11 compute void computeShader(device float[] buffer : register(u1)) {
12     buffer[0] = 17;
13 }
14 `;
15
16 async function start(device) {
17     device.pushErrorScope("validation");
18     const shaderModule = device.createShaderModule({code: shaderSource});
19     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
20
21     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 0, type: "storage-buffer"}, {binding: 1, visibility: 7, type: "storage-buffer"}]};
22     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
23     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
24     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
25
26     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
27     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
28     device.popErrorScope().then(function(e) {
29         alert(e.message);
30     });
31
32     const size = Float32Array.BYTES_PER_ELEMENT * 1;
33
34     const buffer1 = device.createBuffer({size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ});
35     const buffer2 = device.createBuffer({size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ});
36
37     const bindGroup = device.createBindGroup({layout: bindGroupLayout, bindings: [{binding: 0, resource: {buffer: buffer1, size}}, {binding: 1, resource: {buffer: buffer2, size}}]});
38
39     const commandEncoder = device.createCommandEncoder(); // {}
40     const computePassEncoder = commandEncoder.beginComputePass();
41     computePassEncoder.setPipeline(computePipeline);
42     computePassEncoder.setBindGroup(0, bindGroup);
43     computePassEncoder.dispatch(1, 1, 1);
44     computePassEncoder.endPass();
45     const commandBuffer = commandEncoder.finish();
46     device.getQueue().submit([commandBuffer]);
47
48     const resultsArrayBuffer = await buffer2.mapReadAsync();
49     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
50     if (resultsFloat32Array[0] == 17)
51         testPassed("");
52     else
53         testFailed("");
54     buffer2.unmap();
55 }
56
57 window.jsTestIsAsync = true;
58 getBasicDevice().then(function(device) {
59     start(device).then(function() {
60         finishJSTest();
61     }, function() {
62         testFailed("");
63         finishJSTest();
64     });
65 }, function() {
66     testPassed("");
67     finishJSTest();
68 });
69 </script>
70 <script src="../../resources/js-test-post.js"></script>
71 </body>
72 </html>