[WHLSL] Rewrite all tests to use WHLSL and delete the isWHLSL flag
[WebKit-https.git] / LayoutTests / webgpu / whlsl / do-while-loop-break.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../js/webgpu-functions.js"></script>
5 </head>
6 <body>
7 <canvas id="canvas" width="400" height="400"></canvas>
8 <script>
9 const shaderSource = `
10 struct VertexOut {
11     float4 position : SV_Position;
12     float shade : attribute(0);
13 }
14
15 vertex VertexOut vertexShader(float4 position : attribute(0), float shade : attribute(1)) {
16     VertexOut result;
17
18     bool b = true;
19     do {
20         if (b) {
21             break;
22         }
23     } while ((shade = 1.0, false));
24
25     result.position = position;
26     result.shade = shade;
27
28     return result;
29 }
30
31 fragment float4 fragmentShader(float shade : attribute(0)) : SV_Target 0 {
32     return float4(shade, shade, shade, 1.0);
33 }
34 `;
35
36 const canvas = document.getElementById("canvas");
37
38 async function start(device) {
39     const shaderModule = device.createShaderModule({code: shaderSource});
40     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
41     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
42     const primitiveTopology = "triangle-strip";
43     const rasterizationState = {frontFace: "cw", cullMode: "none"};
44     const alphaBlend = {};
45     const colorBlend = {};
46     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
47     const depthStencilState = null;
48
49     const attribute0 = {shaderLocation: 0, format: "float4", offset: 0};
50     const attribute1 = {shaderLocation: 1, format: "float", offset: 16};
51     const attributes = [attribute0, attribute1];
52     const input0 = {stride: 20, attributeSet: attributes};
53     const inputs = [input0];
54     const vertexInput = {vertexBuffers: inputs};
55
56     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
57     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
58     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
59     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
60
61     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
62     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
63
64     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 5 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
65     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
66     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
67     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
68     vertexBuffer0Float32Array[0] = -0.5;
69     vertexBuffer0Float32Array[1] = -0.5;
70     vertexBuffer0Float32Array[2] = 1.0;
71     vertexBuffer0Float32Array[3] = 1.0;
72     vertexBuffer0Float32Array[4] = 0.0;
73
74     vertexBuffer0Float32Array[5] = -0.5;
75     vertexBuffer0Float32Array[6] = 0.5;
76     vertexBuffer0Float32Array[7] = 1.0;
77     vertexBuffer0Float32Array[8] = 1.0;
78     vertexBuffer0Float32Array[9] = 0.0;
79
80     vertexBuffer0Float32Array[10] = 0.5;
81     vertexBuffer0Float32Array[11] = -0.5;
82     vertexBuffer0Float32Array[12] = 1.0;
83     vertexBuffer0Float32Array[13] = 1.0;
84     vertexBuffer0Float32Array[14] = 0.0;
85
86     vertexBuffer0Float32Array[15] = 0.5;
87     vertexBuffer0Float32Array[16] = 0.5;
88     vertexBuffer0Float32Array[17] = 1.0;
89     vertexBuffer0Float32Array[18] = 1.0;
90     vertexBuffer0Float32Array[19] = 0.0;
91     vertexBuffer0.unmap();
92
93     const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
94     const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
95     const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
96     const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
97     resourceBufferFloat32Array[0] = 1;
98     resourceBuffer.unmap();
99
100     const bufferBinding = {buffer: resourceBuffer, size: 4};
101     const bindGroupBinding = {binding: 0, resource: bufferBinding};
102     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
103     const bindGroup = device.createBindGroup(bindGroupDescriptor);
104
105     const context = canvas.getContext("gpu");
106     const swapChainDescriptor = {device, format: "bgra8unorm"};
107     const swapChain = context.configureSwapChain(swapChainDescriptor);
108     const outputTexture = swapChain.getCurrentTexture();
109     const outputTextureView = outputTexture.createDefaultView();
110
111     const commandEncoder = device.createCommandEncoder(); // {}
112     const red = {r: 0, g: 0, b: 1, a: 1};
113     const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}];
114     const depthStencilAttachment = null;
115     const renderPassDescriptor = {colorAttachments, depthStencilAttachment};
116     const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
117     renderPassEncoder.setPipeline(renderPipeline);
118     renderPassEncoder.setBindGroup(0, bindGroup);
119     renderPassEncoder.setVertexBuffers(0, [vertexBuffer0], [0]);
120     renderPassEncoder.draw(4, 1, 0, 0);
121     renderPassEncoder.endPass();
122     const commandBuffer = commandEncoder.finish();
123     device.getQueue().submit([commandBuffer]);
124 }
125 if (window.testRunner)
126     testRunner.waitUntilDone();
127 getBasicDevice().then(function(device) {
128     start(device).then(function() {
129         if (window.testRunner)
130             testRunner.notifyDone();
131     }, function() {
132         if (window.testRunner)
133             testRunner.notifyDone();
134     });
135 }, function() {
136     drawBlackSquareOnBlueBackgroundInSoftware(canvas);
137     if (window.testRunner)
138         testRunner.notifyDone();
139 });
140 </script>
141 </body>
142 </html>