Add a runtime-disabled dialog element skeleton
[WebKit-https.git] / LayoutTests / webgpu / whlsl / matrix-memory-layout.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 float4x4[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
12     float4x4 result;
13     result[0] = float4(1, 2, 3, 4);
14     result[1] = float4(5, 6, 7, 8);
15     result[2] = float4(9, 10, 11, 12);
16     result[3] = float4(13, 14, 15, 16);
17
18     float4x4 mat = buffer[0];
19
20     if (mat[0][0] == 0
21         && mat[0][1] == 1
22         && mat[0][2] == 2
23         && mat[0][3] == 3
24
25         && mat[1][0] == 4
26         && mat[1][1] == 5
27         && mat[1][2] == 6
28         && mat[1][3] == 7
29
30         && mat[2][0] == 8
31         && mat[2][1] == 9
32         && mat[2][2] == 10
33         && mat[2][3] == 11
34
35         && mat[3][0] == 12
36         && mat[3][1] == 13
37         && mat[3][2] == 14
38         && mat[3][3] == 15) {
39         buffer[0] = result;
40     }
41 }
42 `;
43
44 async function start(device) {
45     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
46     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
47
48     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
49     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
50     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
51     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
52
53     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
54     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
55
56     const size = Float32Array.BYTES_PER_ELEMENT * 16;
57
58     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
59     const buffer = device.createBuffer(bufferDescriptor);
60     const bufferArrayBuffer = await buffer.mapWriteAsync();
61     const bufferFloat32Array = new Float32Array(bufferArrayBuffer);
62     bufferFloat32Array[0] = 0;
63     bufferFloat32Array[1] = 4;
64     bufferFloat32Array[2] = 8;
65     bufferFloat32Array[3] = 12;
66     bufferFloat32Array[4] = 1;
67     bufferFloat32Array[5] = 5;
68     bufferFloat32Array[6] = 9;
69     bufferFloat32Array[7] = 13;
70     bufferFloat32Array[8] = 2;
71     bufferFloat32Array[9] = 6;
72     bufferFloat32Array[10] = 10;
73     bufferFloat32Array[11] = 14;
74     bufferFloat32Array[12] = 3;
75     bufferFloat32Array[13] = 7;
76     bufferFloat32Array[14] = 11;
77     bufferFloat32Array[15] = 15;
78     buffer.unmap();
79
80     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
81     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
82
83     const bufferBinding = {buffer: resultsBuffer, size};
84     const bindGroupBinding = {binding: 0, resource: bufferBinding};
85     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
86     const bindGroup = device.createBindGroup(bindGroupDescriptor);
87
88     const commandEncoder = device.createCommandEncoder(); // {}
89     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
90     const computePassEncoder = commandEncoder.beginComputePass();
91     computePassEncoder.setPipeline(computePipeline);
92     computePassEncoder.setBindGroup(0, bindGroup);
93     computePassEncoder.dispatch(2, 1, 1);
94     computePassEncoder.endPass();
95     const commandBuffer = commandEncoder.finish();
96     device.getQueue().submit([commandBuffer]);
97
98     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
99     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
100     if (resultsFloat32Array[0] === 1
101         && resultsFloat32Array[1] === 5
102         && resultsFloat32Array[2] === 9
103         && resultsFloat32Array[3] === 13
104
105         && resultsFloat32Array[4] === 2
106         && resultsFloat32Array[5] === 6
107         && resultsFloat32Array[6] === 10
108         && resultsFloat32Array[7] === 14
109
110         && resultsFloat32Array[8] === 3
111         && resultsFloat32Array[9] === 7
112         && resultsFloat32Array[10] === 11
113         && resultsFloat32Array[11] === 15
114
115         && resultsFloat32Array[12] === 4
116         && resultsFloat32Array[13] === 8
117         && resultsFloat32Array[14] === 12
118         && resultsFloat32Array[15] === 16)
119         testPassed("");
120     else
121         testFailed("");
122     resultsBuffer.unmap();
123 }
124 window.jsTestIsAsync = true;
125 getBasicDevice().then(function(device) {
126     start(device).then(function() {
127         finishJSTest();
128     }, function() {
129         testFailed("");
130         finishJSTest();
131     });
132 }, function() {
133     testPassed("");
134     finishJSTest();
135 });
136 </script>
137 <script src="../../resources/js-test-post.js"></script>
138 </body>
139 </html>