8a114c4b1ca78c07ff3c18f39c7a84cc0bc8da52
[WebKit-https.git] / LayoutTests / webgpu / whlsl-do-while-loop-break.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 </head>
5 <body>
6 <canvas id="canvas" width="400" height="400"></canvas>
7 <script>
8 const shaderSource = `
9 struct VertexOut {
10     float4 position : SV_Position;
11     float shade : attribute(0);
12 }
13
14 vertex VertexOut vertexShader(float4 position : attribute(0), float shade : attribute(1)) {
15     VertexOut result;
16
17     bool b = true;
18     do {
19         if (b) {
20             break;
21         }
22     } while ((shade = 1.0, false));
23
24     result.position = position;
25     result.shade = shade;
26
27     return result;
28 }
29
30 fragment float4 fragmentShader(float shade : attribute(0)) : SV_Target 0 {
31     return float4(shade, shade, shade, 1.0);
32 }
33 `;
34 async function start() {
35     const adapter = await navigator.gpu.requestAdapter();
36     const device = await adapter.requestDevice();
37
38     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
39     const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
40     const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
41     const primitiveTopology = "triangle-strip";
42     const rasterizationState = {frontFace: "cw", cullMode: "none"};
43     const alphaBlend = {};
44     const colorBlend = {};
45     const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
46     const depthStencilState = null;
47
48     const attribute0 = {shaderLocation: 0, format: "float4", offset: 0};
49     const attribute1 = {shaderLocation: 1, format: "float", offset: 16};
50     const attributes = [attribute0, attribute1];
51     const input0 = {stride: 20, attributeSet: attributes};
52     const inputs = [input0];
53     const vertexInput = {vertexBuffers: inputs};
54
55     const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
56     const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
57     const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
58     const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
59
60     const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, vertexInput, sampleCount: 1, layout: pipelineLayout};
61     const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
62
63     const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 5 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
64     const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
65     const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
66     const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
67     vertexBuffer0Float32Array[0] = -0.5;
68     vertexBuffer0Float32Array[1] = -0.5;
69     vertexBuffer0Float32Array[2] = 1.0;
70     vertexBuffer0Float32Array[3] = 1.0;
71     vertexBuffer0Float32Array[4] = 0.0;
72
73     vertexBuffer0Float32Array[5] = -0.5;
74     vertexBuffer0Float32Array[6] = 0.5;
75     vertexBuffer0Float32Array[7] = 1.0;
76     vertexBuffer0Float32Array[8] = 1.0;
77     vertexBuffer0Float32Array[9] = 0.0;
78
79     vertexBuffer0Float32Array[10] = 0.5;
80     vertexBuffer0Float32Array[11] = -0.5;
81     vertexBuffer0Float32Array[12] = 1.0;
82     vertexBuffer0Float32Array[13] = 1.0;
83     vertexBuffer0Float32Array[14] = 0.0;
84
85     vertexBuffer0Float32Array[15] = 0.5;
86     vertexBuffer0Float32Array[16] = 0.5;
87     vertexBuffer0Float32Array[17] = 1.0;
88     vertexBuffer0Float32Array[18] = 1.0;
89     vertexBuffer0Float32Array[19] = 0.0;
90     vertexBuffer0.unmap();
91
92     const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
93     const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
94     const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
95     const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
96     resourceBufferFloat32Array[0] = 1;
97     resourceBuffer.unmap();
98
99     const bufferBinding = {buffer: resourceBuffer, size: 4};
100     const bindGroupBinding = {binding: 0, resource: bufferBinding};
101     const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
102     const bindGroup = device.createBindGroup(bindGroupDescriptor);
103
104     const canvas = document.getElementById("canvas");
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 window.addEventListener("load", function() {
128     start().then(function() {
129         if (window.testRunner)
130             testRunner.notifyDone();
131     }, function() {
132         if (window.testRunner)
133             testRunner.notifyDone();
134     });
135 });
136 </script>
137 </body>
138 </html>