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