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