uniform-values-per-program.html (4885B)
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 values are per program conformance test.</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 </head> 16 <body> 17 <script id="vshader" type="x-shader/x-vertex"> 18 attribute vec4 a_position; 19 void main() { 20 gl_Position = a_position; 21 } 22 </script> 23 <script id="fshader" type="x-shader/x-fragment"> 24 precision mediump float; 25 varying vec4 v_color; 26 void main() { 27 gl_FragColor = v_color; 28 } 29 </script> 30 <script id="vshaderTest" type="x-shader/x-vertex"> 31 attribute vec4 a_position; 32 uniform $(type) $(name1); 33 uniform $(type) $(name2); 34 uniform bool u_select; 35 varying vec4 v_color; 36 void main() { 37 $(type) value = u_select ? $(name2) : $(name1); 38 v_color = $(conversion); 39 gl_Position = a_position; 40 } 41 </script> 42 <script id="fshaderTest" type="x-shader/x-fragment"> 43 precision mediump float; 44 uniform $(type) $(name1); 45 uniform $(type) $(name2); 46 uniform bool u_select; 47 void main() { 48 $(type) value = u_select ? $(name2) : $(name1); 49 gl_FragColor = $(conversion); 50 } 51 </script> 52 <canvas id="example" width="2" height="2" style="width: 40px; height: 40px;"></canvas> 53 <div id="description"></div> 54 <div id="console"></div> 55 <script> 56 "use strict"; 57 function init() { 58 description(); 59 60 var console = document.getElementById("console"); 61 var wtu = WebGLTestUtils; 62 var gl = wtu.create3DContext("example"); 63 wtu.setupUnitQuad(gl); 64 var vtemplate = wtu.getScript("vshader"); 65 var ftemplate = wtu.getScript("fshader"); 66 var vtemplateTest = wtu.getScript("vshaderTest"); 67 var ftemplateTest = wtu.getScript("fshaderTest"); 68 69 var shaders = [ 70 [vtemplate, ftemplateTest], 71 [vtemplateTest, ftemplate], 72 ]; 73 74 var names = [ 75 ["u_value1", "u_value2"], 76 ["a", "b"], 77 ["x", "y"], 78 ["y", "z"], 79 ["y", "u"], 80 ["a00000", "a00001"], 81 ]; 82 var testList = [ 83 { type: "float", 84 conversion: "vec4(value, 0, 0, 0)", 85 values: [[64], [128]], 86 func: 'uniform1fv', 87 }, 88 { type: "vec2", 89 conversion: "vec4(value, 0, 0)", 90 values: [[64, 128], [128, 64]], 91 func: 'uniform2fv', 92 }, 93 { type: "vec3", 94 conversion: "vec4(value, 0)", 95 values: [[64, 128, 192], [192, 128, 64]], 96 func: 'uniform3fv', 97 }, 98 { type: "vec4", 99 conversion: "vec4(value)", 100 values: [[64, 128, 192, 255], [255, 192, 128, 64]], 101 func: 'uniform4fv', 102 }, 103 ]; 104 105 var clone = function(obj) { 106 var n = { }; 107 for (var $key in obj) { 108 n[$key] = obj[$key]; 109 } 110 return n; 111 }; 112 113 var tests = []; 114 names.forEach(function(namePair) { 115 testList.forEach(function(test) { 116 var t = clone(test); 117 t.name1 = namePair[0]; 118 t.name2 = namePair[1]; 119 tests.push(t); 120 }); 121 }); 122 123 var runTest = function(test) { 124 debug(""); 125 debug("testing: " + test.type); 126 shaders.forEach(function(shaderPair) { 127 var progs = []; 128 for (var ii = 0; ii < 2; ++ii) { 129 var vsource = wtu.replaceParams(shaderPair[0], test); 130 var fsource = wtu.replaceParams(shaderPair[1], test); 131 if (!ii) { 132 wtu.addShaderSource(console, "vertex shader: type = " + test.type + " with names " + test.name1 + ", " + test.name2, vsource); 133 wtu.addShaderSource(console, "fragment shader: type = " + test.type + " with names " + test.name1 + ", " + test.name2, fsource); 134 } 135 var program = wtu.setupProgram(gl, [vsource, fsource], ["a_position"]); 136 var info = { 137 program: program, 138 valueLocs: [gl.getUniformLocation(program, test.name1), 139 gl.getUniformLocation(program, test.name2)], 140 selectLoc: gl.getUniformLocation(program, "u_select"), 141 }; 142 var v1 = test.values[0]; 143 var v2 = test.values[1]; 144 if (ii) { 145 var t = v1; 146 v1 = v2; 147 v2 = t; 148 } 149 info.expect = [v1, v2]; 150 for (var jj = 0; jj < 2; ++jj) { 151 var input = info.expect[jj].map(function(v) { return v / 255; }); 152 gl[test.func](info.valueLocs[jj], input); 153 } 154 progs.push(info); 155 } 156 for (var ii = 0; ii < 2; ++ii) { 157 progs.forEach(function(info) { 158 gl.useProgram(info.program); 159 gl.uniform1i(info.selectLoc, ii); 160 wtu.clearAndDrawUnitQuad(gl); 161 wtu.checkCanvas(gl, info.expect[ii], undefined, 1); 162 }); 163 } 164 progs.forEach(function(info) { 165 gl.deleteProgram(info.program); 166 }); 167 }); 168 } 169 tests.forEach(function(test){ 170 runTest(test); 171 }); 172 } 173 init(); 174 var successfullyParsed = true; 175 </script> 176 <script src="../../js/js-test-post.js"></script> 177 </body> 178 </html>