unary-minus-operator-in-dynamic-loop.html (6787B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 10 <head> 11 <meta charset="utf-8"> 12 <title>Unary minus operator on int or uint variables in a dynamic loop in vertex shader should work</title> 13 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 14 <script src="../../js/js-test-pre.js"></script> 15 <script src="../../js/webgl-test-utils.js"></script> 16 </head> 17 18 <body> 19 <canvas id="canvas" style="border: none;" width="1024" height="128"></canvas> 20 <div id="description"></div> 21 <div id="console"></div> 22 23 <script id="shader-vs-int" type="x-shader/x-vertex">#version 300 es 24 in highp vec4 pos; 25 26 uniform int u_one; 27 uniform int u_two; 28 uniform int u_three; 29 30 out mediump vec4 v_color; 31 void main() { 32 int array[3]; 33 array[0] = u_one; // array[0] should be 1 34 array[1] = -u_two; // array[1] should be -2 35 array[2] = u_three; // array[2] should be 3 36 int result = 0; 37 for (int i = 0; i < u_three; i++) { 38 result += array[i]; 39 } 40 v_color = (result == 2) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 41 gl_Position = pos; 42 } 43 </script> 44 45 <script id="shader-vs-uint" type="x-shader/x-vertex">#version 300 es 46 in highp vec4 pos; 47 48 uniform uint u_one; 49 uniform uint u_two; 50 uniform uint u_three; 51 52 out mediump vec4 v_color; 53 void main() { 54 uint array[3]; 55 array[0] = u_one; // array[0] should be 1u 56 array[1] = -u_two; // array[1] should be -2u 57 array[2] = u_three; // array[2] should be 3u 58 uint result = 0u; 59 for (uint i = 0u; i < u_three; i++) { 60 result += array[i]; 61 } 62 v_color = (result == 2u) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 63 gl_Position = pos; 64 } 65 </script> 66 67 <script id="shader-vs-int-multiple-brackets" type="x-shader/x-vertex">#version 300 es 68 in highp vec4 pos; 69 70 uniform int u_one; 71 uniform int u_two; 72 uniform int u_three; 73 74 out mediump vec4 v_color; 75 void main() { 76 int array[3]; 77 array[0] = u_one; // array[0] should be 1 78 array[1] = -(-(-u_two + 1) + 1); // array[1] should be -2 79 array[2] = u_three; // array[2] should be 3 80 int result = 0; 81 for (int i = 0; i < u_three; i++) { 82 result += array[i]; 83 } 84 v_color = (result == 2) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 85 gl_Position = pos; 86 } 87 </script> 88 89 <script id="shader-vs-uint-multiple-brackets" type="x-shader/x-vertex">#version 300 es 90 in highp vec4 pos; 91 92 uniform uint u_one; 93 uniform uint u_two; 94 uniform uint u_three; 95 96 out mediump vec4 v_color; 97 void main() { 98 uint array[3]; 99 array[0] = u_one; // array[0] should be 1u 100 array[1] = -(-(-u_two + 1u) + 1u); // array[1] should be -2u 101 array[2] = u_three; // array[2] should be 3u 102 uint result = 0u; 103 for (uint i = 0u; i < u_three; i++) { 104 result += array[i]; 105 } 106 v_color = (result == 2u) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 107 gl_Position = pos; 108 } 109 </script> 110 111 <script id="shader-vs-int-implicit-unary-minus" type="x-shader/x-vertex">#version 300 es 112 in highp vec4 pos; 113 114 uniform int u_one; 115 uniform int u_two; 116 uniform int u_three; 117 118 out mediump vec4 v_color; 119 void main() { 120 int array[3]; 121 array[0] = u_one; // array[0] should be 1 122 array[1] = 1 - u_two; 123 array[2] = u_three; // array[2] should be 3 124 int result = 0; 125 array[1] -= 1; // array[1] should be -u_two == -2 126 for (int i = 0; i < u_three; i++) { 127 result += array[i]; 128 } 129 v_color = (result == 2) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 130 gl_Position = pos; 131 } 132 </script> 133 134 <script id="shader-vs-uint-implicit-unary-minus" type="x-shader/x-vertex">#version 300 es 135 in highp vec4 pos; 136 137 uniform uint u_one; 138 uniform uint u_two; 139 uniform uint u_three; 140 141 out mediump vec4 v_color; 142 void main() { 143 uint array[3]; 144 array[0] = u_one; // array[0] should be 1u 145 array[1] = 1u - u_two; 146 array[2] = u_three; // array[2] should be 3u 147 uint result = 0u; 148 array[1] -= 1u; // array[1] should be -u_two == -2u 149 for (uint i = 0u; i < u_three; i++) { 150 result += array[i]; 151 } 152 v_color = (result == 2u) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1); 153 gl_Position = pos; 154 } 155 </script> 156 157 <script id="shader-fs" type="x-shader/x-fragment">#version 300 es 158 in mediump vec4 v_color; 159 out mediump vec4 o_color; 160 161 void main() { 162 o_color = v_color; 163 } 164 </script> 165 166 <script> 167 "use strict"; 168 169 function test() { 170 description(); 171 debug("This test exposes an Intel driver bug on Windows."); 172 debug(""); 173 174 var wtu = WebGLTestUtils; 175 var gl = wtu.create3DContext("canvas", undefined, 2); 176 if (!gl) { 177 testFailed("WebGL 2 context does not exist"); 178 return; 179 } 180 181 var testNum = 0; 182 var border = 10; // border between test squares for visibility 183 var squareSize = 128; 184 var expectedColor = [0, 255, 0, 255]; // green 185 186 function subTest_int(message, VertexShader) { 187 debug(message); 188 var startX = (squareSize + border) * testNum; 189 var program = wtu.setupProgram( 190 gl, [VertexShader, "shader-fs"], ["pos"], null, true); 191 gl.viewport(startX, 0, squareSize, squareSize); 192 193 var one = gl.getUniformLocation(program, "u_one"); 194 var two = gl.getUniformLocation(program, "u_two"); 195 var three = gl.getUniformLocation(program, "u_three"); 196 gl.uniform1i(one, 1); 197 gl.uniform1i(two, 2); 198 gl.uniform1i(three, 3); 199 200 wtu.drawUnitQuad(gl); 201 wtu.checkCanvasRect( 202 gl, startX, 0, squareSize, squareSize, 203 expectedColor, "square should be green", 1); 204 debug(""); 205 testNum++; 206 } 207 208 function subTest_uint(message, VertexShader) { 209 debug(message); 210 var startX = (squareSize + border) * testNum; 211 var program = wtu.setupProgram( 212 gl, [VertexShader, "shader-fs"], ["pos"], null, true); 213 gl.viewport(startX, 0, squareSize, squareSize); 214 215 var one = gl.getUniformLocation(program, "u_one"); 216 var two = gl.getUniformLocation(program, "u_two"); 217 var three = gl.getUniformLocation(program, "u_three"); 218 gl.uniform1ui(one, 1); 219 gl.uniform1ui(two, 2); 220 gl.uniform1ui(three, 3); 221 222 wtu.drawUnitQuad(gl); 223 wtu.checkCanvasRect( 224 gl, startX, 0, squareSize, squareSize, 225 expectedColor, "square should be green", 1); 226 debug(""); 227 testNum++; 228 } 229 230 if (!gl) { 231 testFailed("context does not exist"); 232 } else { 233 wtu.setupUnitQuad(gl); 234 subTest_int("Test unary minus operator on int.", "shader-vs-int"); 235 subTest_uint("Test unary minus operator on unsigned int.", "shader-vs-uint"); 236 subTest_int("Test unary minus operator on int with multiple brackets.", "shader-vs-int-multiple-brackets"); 237 subTest_uint("Test unary minus operator on unsigned int with multiple brackets.", "shader-vs-uint-multiple-brackets"); 238 subTest_int("Test implicit unary minus operator on int.", "shader-vs-int-implicit-unary-minus"); 239 subTest_uint("Test implicit unary minus operator on unsigned int.", "shader-vs-uint-implicit-unary-minus"); 240 } 241 } 242 243 test(); 244 var successfullyParsed = true; 245 finishTest(); 246 </script> 247 </body> 248 </html>