a938224c3f9d4cb9c14277f3b54fff2f1b6f95cb
[WebKit-https.git] / LayoutTests / webgpu / whlsl / sparse-bind-group.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(u0, space1)) {
12     buffer[0] = 17;
13 }
14 `;
15
16 async function start(device) {
17     const shaderModule = device.createShaderModule({code: shaderSource});
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, 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 * 1;
29
30     const buffer1 = device.createBuffer({size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ});
31     const buffer2 = device.createBuffer({size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ});
32
33     const bindGroup1 = device.createBindGroup({layout: bindGroupLayout, bindings: [{binding: 0, resource: {buffer: buffer1, size}}]});
34     const bindGroup2 = device.createBindGroup({layout: bindGroupLayout, bindings: [{binding: 0, resource: {buffer: buffer2, size}}]});
35
36     const commandEncoder = device.createCommandEncoder(); // {}
37     const computePassEncoder = commandEncoder.beginComputePass();
38     computePassEncoder.setPipeline(computePipeline);
39     computePassEncoder.setBindGroup(0, bindGroup1);
40     computePassEncoder.setBindGroup(1, bindGroup2);
41     computePassEncoder.dispatch(1, 1, 1);
42     computePassEncoder.endPass();
43     const commandBuffer = commandEncoder.finish();
44     device.getQueue().submit([commandBuffer]);
45
46     const resultsArrayBuffer = await buffer2.mapReadAsync();
47     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
48     if (resultsFloat32Array[0] == 17)
49         testPassed("");
50     else
51         testFailed("");
52     buffer2.unmap();
53 }
54
55 window.jsTestIsAsync = true;
56 getBasicDevice().then(function(device) {
57     start(device).then(function() {
58         finishJSTest();
59     }, function() {
60         testFailed("");
61         finishJSTest();
62     });
63 }, function() {
64     testPassed("");
65     finishJSTest();
66 });
67 </script>
68 <script src="../../resources/js-test-post.js"></script>
69 </body>
70 </html>