1a80f7e777cc86e0716cac741b39087010889cdd
[WebKit-https.git] / LayoutTests / webgpu / whlsl-textures-getdimensions.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(Texture2D<float4> theTexture : register(t0), device uint[] theBuffer : register(u1)) {
11     uint width;
12     uint height;
13     uint numberOfLevels;
14     GetDimensions(theTexture, 0, &width, &height, &numberOfLevels);
15     theBuffer[0] = width;
16     theBuffer[1] = height;
17     theBuffer[2] = numberOfLevels;
18 }
19 `;
20 let resultsUint32Array;
21 async function start() {
22     const adapter = await navigator.gpu.requestAdapter();
23     const device = await adapter.requestDevice();
24
25     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
26     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
27
28     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "sampled-texture"}, {binding: 1, visibility: 7, type: "storage-buffer"}]};
29     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
30     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
31     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
32
33     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
34     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
35
36     const textureDescriptor = {size: {width: 2, height: 2, depth: 1}, format: "rgba8unorm", usage: GPUTextureUsage.SAMPLED};
37     const texture = device.createTexture(textureDescriptor);
38     const textureView = texture.createDefaultView();
39
40     const resultsBufferDescriptor = {size: Uint32Array.BYTES_PER_ELEMENT * 3, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.MAP_READ};
41     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
42
43     const bufferBinding = {buffer: resultsBuffer, size: Uint32Array.BYTES_PER_ELEMENT * 3};
44     const bindGroupBindings = [{binding: 0, resource: textureView}, {binding: 1, resource: bufferBinding}];
45     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: bindGroupBindings};
46     const bindGroup = device.createBindGroup(bindGroupDescriptor);
47
48     const commandEncoder = device.createCommandEncoder(); // {}
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     resultsUint32Array = new Uint32Array(resultsArrayBuffer);
59     shouldBe("resultsUint32Array[0]", "2");
60     shouldBe("resultsUint32Array[1]", "2");
61     shouldBe("resultsUint32Array[2]", "1");
62     resultsBuffer.unmap();
63 }
64 window.jsTestIsAsync = true;
65 window.addEventListener("load", function() {
66     start().then(function() {
67         finishJSTest();
68     }, function(e) {
69         finishJSTest();
70     });
71 });
72 </script>
73 <script src="../resources/js-test-post.js"></script>
74 </body>
75 </html>