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.
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>
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;
25 gl_Position = vPosition;
28 <script id=
"fshader0" type=
"x-shader/x-fragment">
29 precision mediump float;
30 uniform $(type) u_uniform;
32 bool isZero($(type) value) {
38 gl_FragColor = isZero(u_uniform) ? vec4(
0,
1,
0,
1) : vec4(
1,
0,
0,
1);
41 <script id=
"vshader1" type=
"x-shader/x-vertex">
42 attribute vec4 vPosition;
44 uniform $(type) u_uniform;
46 bool isZero($(type) value) {
52 gl_Position = vPosition;
53 v_color = isZero(u_uniform) ? vec4(
0,
1,
0,
1) : vec4(
1,
0,
0,
1);
56 <script id=
"fshader1" type=
"x-shader/x-fragment">
57 precision mediump float;
61 gl_FragColor = v_color;
64 <script id=
"vshader2" type=
"x-shader/x-vertex">
65 attribute vec4 vPosition;
68 gl_Position = vPosition;
71 <script id=
"fshader2" type=
"x-shader/x-fragment">
72 precision mediump float;
73 uniform $(type) u_uniform[
2];
75 bool isZero($(type) value) {
81 gl_FragColor = isZero(u_uniform[
1]) ? vec4(
0,
1,
0,
1) : vec4(
1,
0,
0,
1);
84 <script id=
"vshader3" type=
"x-shader/x-vertex">
85 attribute vec4 vPosition;
87 uniform $(type) u_uniform[
2];
89 bool isZero($(type) value) {
95 gl_Position = vPosition;
96 v_color = isZero(u_uniform[
1]) ? vec4(
0,
1,
0,
1) : vec4(
1,
0,
0,
1);
99 <script id=
"fshader3" type=
"x-shader/x-fragment">
100 precision mediump float;
101 varying vec4 v_color;
104 gl_FragColor = v_color;
113 check
: "return value == 0.0",
114 setFn: function(gl
, loc
) { gl
.uniform1f(loc
, 3.0); }
117 check
: "return value == 0",
118 setFn: function(gl
, loc
) { gl
.uniform1i(loc
, 3.0); }
121 check
: "return value == false",
122 setFn: function(gl
, loc
) { gl
.uniform1i(loc
, 1); }
125 check
: "return value[0] == 0.0 && value[1] == 0.0",
126 setFn: function(gl
, loc
) { gl
.uniform2f(loc
, 3.0, 3.0); }
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); }
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); }
137 check
: "return value[0] == 0 && value[1] == 0",
138 setFn: function(gl
, loc
) { gl
.uniform2i(loc
, 3, 3); }
141 check
: "return value[0] == 0 && value[1] == 0 && value[2] == 0",
142 setFn: function(gl
, loc
) { gl
.uniform3i(loc
, 3, 3, 3); }
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); }
149 check
: "return value[0] == false && value[1] == false",
150 setFn: function(gl
, loc
) { gl
.uniform2i(loc
, 1, 1); }
153 check
: "return value[0] == false && value[1] == false && value[2] == false",
154 setFn: function(gl
, loc
) { gl
.uniform3i(loc
, 1, 1, 1); }
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); }
163 "value[0][0] == 0.0 && value[0][1] == 0.0 && " +
164 "value[1][0] == 0.0 && value[1][0] == 0.0",
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]); }
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",
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]); }
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",
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]); }
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",
205 setFn: function(gl
, loc
) { gl
.uniform1i(loc
, 1); }
207 { type
: 'samplerCube',
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",
213 setFn: function(gl
, loc
) { gl
.uniform1i(loc
, 1); }
217 var wtu
= WebGLTestUtils
;
218 var gl
= wtu
.create3DContext();
219 var c
= document
.getElementById("console");
222 wtu
.setupUnitQuad(gl
, [0, 1]);
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
);
232 var pixel
= new Uint8Array([255, 255, 255, 255]);
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
242 for (var ii
= 0; ii
< targets
.length
; ++ii
) {
244 targets
[ii
], 0, gl
.RGBA
, 1, 1, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixel
);
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' },
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
);
261 function testType(test
) {
263 debug("testing: " + test
.type
);
265 for (var ii
= 0; ii
< shaderTemplates
.length
; ++ii
) {
266 var template
= shaderTemplates
[ii
];
268 if (test
.type
.substring(0, 7) == "sampler" &&
269 template
.type
== 'v' &&
270 !haveVertexTextureImageUnits
) {
274 var vs
= wtu
.replaceParams(template
.vs
, test
);
275 var fs
= wtu
.replaceParams(template
.fs
, test
);
277 wtu
.addShaderSource(c
, "vertex shader", vs
);
278 wtu
.addShaderSource(c
, "fragment shader", fs
);
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
);
284 gl
.useProgram(program
);
286 var loc
= gl
.getUniformLocation(program
, "u_uniform[1]");
288 var loc
= gl
.getUniformLocation(program
, "u_uniform");
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");
296 testFailed("uniform is not zero");
299 debug("default value should be zero");
300 wtu
.clearAndDrawUnitQuad(gl
);
301 wtu
.checkCanvas(gl
, [0, 255, 0, 255], "should be green", 0);
303 debug("test test by setting value");
306 wtu
.clearAndDrawUnitQuad(gl
);
307 wtu
.checkCanvas(gl
, [255, 0, 0, 255], "should be red", 0);
309 debug("re-linking should reset to defaults");
310 gl
.linkProgram(program
);
312 wtu
.clearAndDrawUnitQuad(gl
);
313 wtu
.checkCanvas(gl
, [0, 255, 0, 255], "should be green", 0);
315 gl
.deleteProgram(program
);
319 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no GL errors");
324 function runNextTest() {
325 testType(tests
[testNdx
++]);
326 if (testNdx
>= tests
.length
) {
329 setTimeout(runNextTest
, 0);
335 var successfullyParsed
= true;