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