REGRESSION: ( r246394 ) webgpu/whlsl-buffer-fragment.html and webgpu/whlsl-buffer...
[WebKit-https.git] / LayoutTests / webgpu / whlsl-matrix.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 bool allEqual(float2x3 mat, float value)
11 {
12     for (uint i = 0; i < 2; i = i + 1) {
13         for (uint j = 0; j < 3; j = j + 1) {
14             if (mat[i][j] != value)
15                 return false;
16         }
17     }
18     return true;
19 }
20
21 [numthreads(1, 1, 1)]
22 compute void computeShader(device float[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
23     float2x3 foo;
24     if (!allEqual(foo, 0))
25         return;
26
27     foo = foo + 1.0;
28     if (!allEqual(foo, 1))
29         return;
30
31     foo = foo * 8.5;
32     if (!allEqual(foo, 8.5))
33         return;
34
35     foo = foo - 7.5;
36     if (!allEqual(foo, 1))
37         return;
38
39     foo = foo + 1.0;
40     if (!allEqual(foo, 2))
41         return;
42
43     foo = foo + foo;
44     if (!allEqual(foo, 4))
45         return;
46
47     foo = foo + foo;
48     if (!allEqual(foo, 8))
49         return;
50
51     float3 fourtyTwo;
52     fourtyTwo.x = 42;
53     fourtyTwo.y = 42;
54     fourtyTwo.z = 42;
55
56     foo[0] = fourtyTwo;
57     foo[1] = fourtyTwo;
58     if (!allEqual(foo, 42))
59         return;
60
61     foo[1337] = fourtyTwo;
62     if (!allEqual(foo, 42))
63         return;
64
65     foo[1000000] = fourtyTwo;
66     if (!allEqual(foo, 42))
67         return;
68
69     fourtyTwo[1337] = 50;
70     foo[1] = fourtyTwo;
71     if (!allEqual(foo, 42))
72         return;
73
74     fourtyTwo[1000000] = 50;
75     foo[1] = fourtyTwo;
76     if (!allEqual(foo, 42))
77         return;
78
79     float3 shouldBeZero = foo[100000];
80     if (shouldBeZero.x != 0 || shouldBeZero.y != 0 || shouldBeZero.z != 0)
81         return;
82
83     if (fourtyTwo[10000000] != 0)
84         return;
85
86     buffer[uint(threadID.x)] = buffer[uint(threadID.x)] * 2.0;
87 }
88 `;
89
90 async function start(device) {
91     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
92     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
93
94     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
95     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
96     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
97     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
98
99     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
100     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
101
102     const size = Float32Array.BYTES_PER_ELEMENT * 8;
103
104     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
105     const buffer = device.createBuffer(bufferDescriptor);
106     const bufferArrayBuffer = await buffer.mapWriteAsync();
107     const bufferFloat32Array = new Float32Array(bufferArrayBuffer);
108     bufferFloat32Array[0] = 1;
109     bufferFloat32Array[1] = 2;
110     bufferFloat32Array[2] = 3;
111     bufferFloat32Array[3] = 4;
112     bufferFloat32Array[4] = 5;
113     bufferFloat32Array[5] = 6;
114     bufferFloat32Array[6] = 7;
115     bufferFloat32Array[7] = 8;
116     buffer.unmap();
117
118     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
119     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
120
121     const bufferBinding = {buffer: resultsBuffer, size};
122     const bindGroupBinding = {binding: 0, resource: bufferBinding};
123     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
124     const bindGroup = device.createBindGroup(bindGroupDescriptor);
125
126     const commandEncoder = device.createCommandEncoder(); // {}
127     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
128     const computePassEncoder = commandEncoder.beginComputePass();
129     computePassEncoder.setPipeline(computePipeline);
130     computePassEncoder.setBindGroup(0, bindGroup);
131     computePassEncoder.dispatch(4, 1, 1);
132     computePassEncoder.endPass();
133     const commandBuffer = commandEncoder.finish();
134     device.getQueue().submit([commandBuffer]);
135
136     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
137     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
138     if (resultsFloat32Array[0] == 2
139         && resultsFloat32Array[1] == 4
140         && resultsFloat32Array[2] == 6
141         && resultsFloat32Array[3] == 8
142         && resultsFloat32Array[4] == 5
143         && resultsFloat32Array[5] == 6
144         && resultsFloat32Array[6] == 7
145         && resultsFloat32Array[7] == 8)
146         testPassed("");
147     else
148         testFailed("");
149     resultsBuffer.unmap();
150 }
151 window.jsTestIsAsync = true;
152 getBasicDevice().then(function(device) {
153     start(device).then(function() {
154         finishJSTest();
155     }, function() {
156         testFailed("");
157         finishJSTest();
158     });
159 }, function() {
160     testPassed("");
161     finishJSTest();
162 });
163 </script>
164 <script src="../resources/js-test-post.js"></script>
165 </body>
166 </html>