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