[WHLSL] Rewrite all tests to use WHLSL and delete the isWHLSL flag
[WebKit-https.git] / LayoutTests / webgpu / whlsl / device-proper-type-checker.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 struct Point {
11     float x;
12     float y;
13 }
14
15 [numthreads(1, 1, 1)]
16 compute void computeShader(device Point[] buffer : register(u0), float3 threadID : SV_DispatchThreadID) {
17     float x = buffer[0].x;
18     float y = buffer[0].y;
19     x += 1.0;
20     y += 1.0;
21     buffer[0].x = x;
22     buffer[0].y = y;
23 }
24 `;
25
26 async function start(device) {
27     const shaderModule = device.createShaderModule({code: shaderSource});
28     const computeStage = {module: shaderModule, entryPoint: "computeShader"};
29
30     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "storage-buffer"}]};
31     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
32     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
33     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
34
35     const computePipelineDescriptor = {computeStage, layout: pipelineLayout};
36     const computePipeline = device.createComputePipeline(computePipelineDescriptor);
37
38     const size = Float32Array.BYTES_PER_ELEMENT * 2;
39
40     const bufferDescriptor = {size, usage: GPUBufferUsage.MAP_WRITE | GPUBufferUsage.TRANSFER_SRC};
41     const buffer = device.createBuffer(bufferDescriptor);
42     const bufferArrayBuffer = await buffer.mapWriteAsync();
43     const bufferFloat32Array = new Float32Array(bufferArrayBuffer);
44     bufferFloat32Array[0] = 42.0;
45     bufferFloat32Array[1] = 43.0;
46     buffer.unmap();
47
48     const resultsBufferDescriptor = {size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.TRANSFER_DST | GPUBufferUsage.MAP_READ};
49     const resultsBuffer = device.createBuffer(resultsBufferDescriptor);
50
51     const bufferBinding = {buffer: resultsBuffer, size};
52     const bindGroupBinding = {binding: 0, resource: bufferBinding};
53     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
54     const bindGroup = device.createBindGroup(bindGroupDescriptor);
55
56     const commandEncoder = device.createCommandEncoder(); // {}
57     commandEncoder.copyBufferToBuffer(buffer, 0, resultsBuffer, 0, size);
58     const computePassEncoder = commandEncoder.beginComputePass();
59     computePassEncoder.setPipeline(computePipeline);
60     computePassEncoder.setBindGroup(0, bindGroup);
61     computePassEncoder.dispatch(1, 1, 1);
62     computePassEncoder.endPass();
63     const commandBuffer = commandEncoder.finish();
64     device.getQueue().submit([commandBuffer]);
65
66     const resultsArrayBuffer = await resultsBuffer.mapReadAsync();
67     const resultsFloat32Array = new Float32Array(resultsArrayBuffer);
68     if (resultsFloat32Array[0] == 43.0
69         && resultsFloat32Array[1] == 44.0)
70         testPassed("");
71     else
72         testFailed("");
73     resultsBuffer.unmap();
74 }
75 window.jsTestIsAsync = true;
76 getBasicDevice().then(function(device) {
77     start(device).then(function() {
78         finishJSTest();
79     }, function() {
80         testFailed("");
81         finishJSTest();
82     });
83 }, function() {
84     testPassed("");
85     finishJSTest();
86 });
87 </script>
88 <script src="../../resources/js-test-post.js"></script>
89 </body>
90 </html>