uniform-default-values.html (9631B)
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 <head> 10 <meta charset="utf-8"> 11 <title>WebGL uniform default values</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"></script> 15 <script src="../../js/test-eval.js"></script> 16 </head> 17 <body> 18 <div id="description"></div> 19 <div id="console"></div> 20 <canvas id="example" width="2" height="2"> </canvas> 21 <script id="vshader0" type="x-shader/x-vertex"> 22 attribute vec4 vPosition; 23 void main() 24 { 25 gl_Position = vPosition; 26 } 27 </script> 28 <script id="fshader0" type="x-shader/x-fragment"> 29 precision mediump float; 30 uniform $(type) u_uniform; 31 32 bool isZero($(type) value) { 33 $(check); 34 } 35 36 void main() 37 { 38 gl_FragColor = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1); 39 } 40 </script> 41 <script id="vshader1" type="x-shader/x-vertex"> 42 attribute vec4 vPosition; 43 varying vec4 v_color; 44 uniform $(type) u_uniform; 45 46 bool isZero($(type) value) { 47 $(check); 48 } 49 50 void main() 51 { 52 gl_Position = vPosition; 53 v_color = isZero(u_uniform) ? vec4(0,1,0,1) : vec4(1,0,0,1); 54 } 55 </script> 56 <script id="fshader1" type="x-shader/x-fragment"> 57 precision mediump float; 58 varying vec4 v_color; 59 void main() 60 { 61 gl_FragColor = v_color; 62 } 63 </script> 64 <script id="vshader2" type="x-shader/x-vertex"> 65 attribute vec4 vPosition; 66 void main() 67 { 68 gl_Position = vPosition; 69 } 70 </script> 71 <script id="fshader2" type="x-shader/x-fragment"> 72 precision mediump float; 73 uniform $(type) u_uniform[2]; 74 75 bool isZero($(type) value) { 76 $(check); 77 } 78 79 void main() 80 { 81 gl_FragColor = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1); 82 } 83 </script> 84 <script id="vshader3" type="x-shader/x-vertex"> 85 attribute vec4 vPosition; 86 varying vec4 v_color; 87 uniform $(type) u_uniform[2]; 88 89 bool isZero($(type) value) { 90 $(check); 91 } 92 93 void main() 94 { 95 gl_Position = vPosition; 96 v_color = isZero(u_uniform[1]) ? vec4(0,1,0,1) : vec4(1,0,0,1); 97 } 98 </script> 99 <script id="fshader3" type="x-shader/x-fragment"> 100 precision mediump float; 101 varying vec4 v_color; 102 void main() 103 { 104 gl_FragColor = v_color; 105 } 106 </script> 107 <script> 108 "use strict"; 109 description(); 110 111 var tests = [ 112 { type: 'float', 113 check: "return value == 0.0", 114 setFn: function(gl, loc) { gl.uniform1f(loc, 3.0); } 115 }, 116 { type: 'int', 117 check: "return value == 0", 118 setFn: function(gl, loc) { gl.uniform1i(loc, 3.0); } 119 }, 120 { type: 'bool', 121 check: "return value == false", 122 setFn: function(gl, loc) { gl.uniform1i(loc, 1); } 123 }, 124 { type: 'vec2', 125 check: "return value[0] == 0.0 && value[1] == 0.0", 126 setFn: function(gl, loc) { gl.uniform2f(loc, 3.0, 3.0); } 127 }, 128 { type: 'vec3', 129 check: "return value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0", 130 setFn: function(gl, loc) { gl.uniform3f(loc, 3.0, 3.0, 3.0); } 131 }, 132 { type: 'vec4', 133 check: "return value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0 && value[3] == 0.0", 134 setFn: function(gl, loc) { gl.uniform4f(loc, 3.0, 3.0, 3.0, 3.0); } 135 }, 136 { type: 'ivec2', 137 check: "return value[0] == 0 && value[1] == 0", 138 setFn: function(gl, loc) { gl.uniform2i(loc, 3, 3); } 139 }, 140 { type: 'ivec3', 141 check: "return value[0] == 0 && value[1] == 0 && value[2] == 0", 142 setFn: function(gl, loc) { gl.uniform3i(loc, 3, 3, 3); } 143 }, 144 { type: 'ivec4', 145 check: "return value[0] == 0 && value[1] == 0 && value[2] == 0 && value[3] == 0", 146 setFn: function(gl, loc) { gl.uniform4i(loc, 3, 3, 3, 3); } 147 }, 148 { type: 'bvec2', 149 check: "return value[0] == false && value[1] == false", 150 setFn: function(gl, loc) { gl.uniform2i(loc, 1, 1); } 151 }, 152 { type: 'bvec3', 153 check: "return value[0] == false && value[1] == false && value[2] == false", 154 setFn: function(gl, loc) { gl.uniform3i(loc, 1, 1, 1); } 155 }, 156 { type: 'bvec4', 157 check: "return value[0] == false && value[1] == false && value[2] == false && value[3] == false", 158 setFn: function(gl, loc) { gl.uniform4i(loc, 1, 1, 1, 1); } 159 }, 160 { type: 'mat2', 161 check: 162 "return " + 163 "value[0][0] == 0.0 && value[0][1] == 0.0 && " + 164 "value[1][0] == 0.0 && value[1][0] == 0.0", 165 valueCheck: 166 "return " + 167 "value[0] == 0.0 && value[1] == 0.0 && " + 168 "value[2] == 0.0 && value[3] == 0.0", 169 setFn: function(gl, loc) { gl.uniformMatrix2fv(loc, false, [1, 1, 1, 1]); } 170 }, 171 { type: 'mat3', 172 check: 173 "return " + 174 "value[0][0] == 0.0 && value[1][0] == 0.0 && value[2][0] == 0.0 && " + 175 "value[0][1] == 0.0 && value[1][1] == 0.0 && value[2][1] == 0.0 && " + 176 "value[0][2] == 0.0 && value[1][2] == 0.0 && value[2][2] == 0.0", 177 valueCheck: 178 "return " + 179 "value[0] == 0.0 && value[1] == 0.0 && value[2] == 0.0 && " + 180 "value[3] == 0.0 && value[4] == 0.0 && value[5] == 0.0 && " + 181 "value[6] == 0.0 && value[7] == 0.0 && value[8] == 0.0", 182 setFn: function(gl, loc) { gl.uniformMatrix3fv(loc, false, [1, 1, 1, 1, 1, 1, 1, 1, 1]); } 183 }, 184 { type: 'mat4', 185 check: 186 "return " + 187 "value[0][0] == 0.0 && value[1][0] == 0.0 && value[2][0] == 0.0 && value[3][0] == 0.0 && " + 188 "value[0][1] == 0.0 && value[1][1] == 0.0 && value[2][1] == 0.0 && value[3][1] == 0.0 && " + 189 "value[0][2] == 0.0 && value[1][2] == 0.0 && value[2][2] == 0.0 && value[3][2] == 0.0 && " + 190 "value[0][3] == 0.0 && value[1][3] == 0.0 && value[2][3] == 0.0 && value[3][3] == 0.0", 191 valueCheck: 192 "return " + 193 "value[ 0] == 0.0 && value[ 1] == 0.0 && value[ 2] == 0.0 && value[ 3] == 0.0 && " + 194 "value[ 4] == 0.0 && value[ 5] == 0.0 && value[ 6] == 0.0 && value[ 7] == 0.0 && " + 195 "value[ 8] == 0.0 && value[ 9] == 0.0 && value[10] == 0.0 && value[11] == 0.0 && " + 196 "value[12] == 0.0 && value[13] == 0.0 && value[14] == 0.0 && value[15] == 0.0", 197 setFn: function(gl, loc) { gl.uniformMatrix4fv(loc, false, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); } 198 }, 199 { type: 'sampler2D', 200 check: 201 "vec4 v = texture2D(value, vec2(0, 0));" + 202 "return v.x == 1.0 && v.y == 1.0 && v.z == 1.0 && v.w == 1.0", 203 valueCheck: 204 "return value == 0", 205 setFn: function(gl, loc) { gl.uniform1i(loc, 1); } 206 }, 207 { type: 'samplerCube', 208 check: 209 "vec4 v = textureCube(value, vec3(0, 0, 0));" + 210 "return v.x == 1.0 && v.y == 1.0 && v.z == 1.0 && v.w == 1.0", 211 valueCheck: 212 "return value == 0", 213 setFn: function(gl, loc) { gl.uniform1i(loc, 1); } 214 }, 215 ]; 216 217 var wtu = WebGLTestUtils; 218 var gl = wtu.create3DContext(); 219 var c = document.getElementById("console"); 220 var checkFn; 221 222 wtu.setupUnitQuad(gl, [0, 1]); 223 224 // Set unit 0 to a non-0 texture. 225 var haveVertexTextureImageUnits = 226 gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) >= 2; 227 var tex2D = gl.createTexture(); 228 var texCube = gl.createTexture(); 229 gl.bindTexture(gl.TEXTURE_2D, tex2D); 230 gl.bindTexture(gl.TEXTURE_CUBE_MAP, texCube); 231 232 var pixel = new Uint8Array([255, 255, 255, 255]); 233 var targets = [ 234 gl.TEXTURE_2D, 235 gl.TEXTURE_CUBE_MAP_POSITIVE_X, 236 gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 237 gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 238 gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 239 gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 240 gl.TEXTURE_CUBE_MAP_NEGATIVE_Z 241 ]; 242 for (var ii = 0; ii < targets.length; ++ii) { 243 gl.texImage2D( 244 targets[ii], 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixel); 245 } 246 247 var shaderTemplates = [ 248 { vs: "vshader0", fs: "fshader0", type: 'f' }, 249 { vs: "vshader1", fs: "fshader1", type: 'v' }, 250 { vs: "vshader2", fs: "fshader2", type: 'f' }, 251 { vs: "vshader3", fs: "fshader3", type: 'v' }, 252 ]; 253 254 // Get shader templates 255 for (var ii = 0; ii < shaderTemplates.length; ++ii) { 256 var template = shaderTemplates[ii]; 257 template.vs = wtu.getScript(template.vs); 258 template.fs = wtu.getScript(template.fs); 259 } 260 261 function testType(test) { 262 debug(""); 263 debug("testing: " + test.type); 264 265 for (var ii = 0; ii < shaderTemplates.length; ++ii) { 266 var template = shaderTemplates[ii]; 267 268 if (test.type.substring(0, 7) == "sampler" && 269 template.type == 'v' && 270 !haveVertexTextureImageUnits) { 271 continue; 272 } 273 274 var vs = wtu.replaceParams(template.vs, test); 275 var fs = wtu.replaceParams(template.fs, test); 276 277 wtu.addShaderSource(c, "vertex shader", vs); 278 wtu.addShaderSource(c, "fragment shader", fs); 279 280 var vs = wtu.loadShader(gl, vs, gl.VERTEX_SHADER); 281 var fs = wtu.loadShader(gl, fs, gl.FRAGMENT_SHADER); 282 var program = wtu.createProgram(gl, vs, fs); 283 284 gl.useProgram(program); 285 286 var loc = gl.getUniformLocation(program, "u_uniform[1]"); 287 if (!loc) { 288 var loc = gl.getUniformLocation(program, "u_uniform"); 289 } 290 291 var value = gl.getUniform(program, loc); 292 TestEval("checkFn = function(value) {" + (test.valueCheck ? test.valueCheck : test.check) + ";}"); 293 if (checkFn(value)) { 294 testPassed("uniform is zero"); 295 } else { 296 testFailed("uniform is not zero"); 297 } 298 299 debug("default value should be zero"); 300 wtu.clearAndDrawUnitQuad(gl); 301 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0); 302 303 debug("test test by setting value"); 304 test.setFn(gl, loc); 305 306 wtu.clearAndDrawUnitQuad(gl); 307 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red", 0); 308 309 debug("re-linking should reset to defaults"); 310 gl.linkProgram(program); 311 312 wtu.clearAndDrawUnitQuad(gl); 313 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0); 314 315 gl.deleteProgram(program); 316 gl.deleteShader(vs); 317 gl.deleteShader(fs); 318 319 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no GL errors"); 320 } 321 } 322 323 var testNdx = 0; 324 function runNextTest() { 325 testType(tests[testNdx++]); 326 if (testNdx >= tests.length) { 327 finishTest(); 328 } else { 329 setTimeout(runNextTest, 0); 330 } 331 } 332 333 runNextTest(); 334 335 var successfullyParsed = true; 336 337 </script> 338 </body> 339 </html>