3 Copyright (c) 2019 The Khronos Group Inc.
4 Use of this source code is governed by an MIT-style license that can be
5 found in the LICENSE.txt file.
11 <meta charset=
"utf-8">
12 <title>GLSL Array of Structs Uniform
</title>
13 <link rel=
"stylesheet" href=
"../../../resources/js-test-style.css"/>
14 <link rel=
"stylesheet" href=
"../../../resources/glsl-feature-tests.css"/>
15 <script src=
"../../../js/js-test-pre.js"></script>
16 <script src=
"../../../js/webgl-test-utils.js"> </script>
18 <script id=
"vshader" type=
"x-shader/x-vertex">
19 attribute vec4 a_position;
21 gl_Position = a_position;
24 <script id=
"fshader0" type=
"x-shader/x-fragment">
25 precision mediump float;
29 uniform my_struct u_colors[
2];
31 gl_FragColor = u_colors[
0].color;
34 <script id=
"fshader1" type=
"x-shader/x-fragment">
35 precision mediump float;
39 uniform my_struct u_colors[
2];
41 gl_FragColor = u_colors[
1].color;
44 <script id=
"fshader2" type=
"x-shader/x-fragment">
45 precision mediump float;
50 uniform my_struct u_colors[
2];
52 gl_FragColor = u_colors[
0].color1 + u_colors[
0].color2 + u_colors[
1].color1 +u_colors[
1].color2;
55 <script id=
"fshader3" type=
"x-shader/x-fragment">
56 precision mediump float;
63 uniform my_struct u_colors[
2];
65 gl_FragColor = vec4(u_colors[
0].r, u_colors[
0].g, u_colors[
1].b, u_colors[
1].a);
70 <canvas id=
"canvas" width=
"50" height=
"50"></canvas>
71 <div id=
"description"></div>
72 <div id=
"console"></div>
77 var wtu
= WebGLTestUtils
;
78 var gl
= wtu
.create3DContext("canvas");
79 wtu
.setupUnitQuad(gl
);
81 for (var ii
= 0; ii
< 2; ++ii
) {
82 var program
= wtu
.setupProgram(gl
, ["vshader", "fshader" + ii
], ["a_position"]);
83 var red_loc
= gl
.getUniformLocation(program
, "u_colors[" + ii
+ "].color");
84 var green_loc
= gl
.getUniformLocation(program
, "u_colors[" + (1 - ii
) + "].color");
85 gl
.uniform4fv(red_loc
, [1, 0, 0, 1]);
86 gl
.uniform4fv(green_loc
, [0, 1, 0, 1]);
87 wtu
.clearAndDrawUnitQuad(gl
);
88 wtu
.checkCanvas(gl
, [255, 0, 0, 255], "Should be red");
91 var program
= wtu
.setupProgram(gl
, ["vshader", "fshader2"], ["a_position"]);
92 var numUniforms
= gl
.getProgramParameter(program
, gl
.ACTIVE_UNIFORMS
);
93 shouldBe("numUniforms", "4");
95 for (var ii
= 0; ii
< numUniforms
; ++ii
) {
96 uniforms
.push(gl
.getActiveUniform(program
, ii
).name
);
99 shouldBe("uniforms[0]", '"u_colors[0].color1"');
100 shouldBe("uniforms[1]", '"u_colors[0].color2"');
101 shouldBe("uniforms[2]", '"u_colors[1].color1"');
102 shouldBe("uniforms[3]", '"u_colors[1].color2"');
103 var loc00
= gl
.getUniformLocation(program
, "u_colors[0].color1");
104 var loc01
= gl
.getUniformLocation(program
, "u_colors[0].color2");
105 var loc10
= gl
.getUniformLocation(program
, "u_colors[1].color1");
106 var loc11
= gl
.getUniformLocation(program
, "u_colors[1].color2");
107 shouldBeTrue("loc00 != undefined");
108 shouldBeTrue("loc01 != undefined");
109 shouldBeTrue("loc10 != undefined");
110 shouldBeTrue("loc11 != undefined");
111 gl
.uniform4fv(loc00
, [1, 0, 0, 0]);
112 gl
.uniform4fv(loc01
, [0, 1, 0, 0]);
113 gl
.uniform4fv(loc10
, [0, 0, 1, 0]);
114 gl
.uniform4fv(loc11
, [0, 0, 0, 1]);
115 wtu
.clearAndDrawUnitQuad(gl
, [0, 0, 0, 0]);
116 wtu
.checkCanvas(gl
, [255, 255, 255, 255], "Should be white");
118 program
= wtu
.setupProgram(gl
, ["vshader", "fshader3"], ["a_position"]);
119 var loc0r
= gl
.getUniformLocation(program
, "u_colors[0].r");
120 var loc0g
= gl
.getUniformLocation(program
, "u_colors[0].g");
121 var loc0b
= gl
.getUniformLocation(program
, "u_colors[0].b");
122 var loc0a
= gl
.getUniformLocation(program
, "u_colors[0].a");
123 var loc1r
= gl
.getUniformLocation(program
, "u_colors[1].r");
124 var loc1g
= gl
.getUniformLocation(program
, "u_colors[1].g");
125 var loc1b
= gl
.getUniformLocation(program
, "u_colors[1].b");
126 var loc1a
= gl
.getUniformLocation(program
, "u_colors[1].a");
127 shouldBeTrue("loc0r != undefined");
128 shouldBeTrue("loc0g != undefined");
129 shouldBeTrue("loc1b != undefined");
130 shouldBeTrue("loc1a != undefined");
131 gl
.uniform1f(loc0r
, 1);
132 gl
.uniform1f(loc0g
, 1);
133 gl
.uniform1f(loc1b
, 1);
134 gl
.uniform1f(loc1a
, 1);
135 wtu
.clearAndDrawUnitQuad(gl
, [0, 0, 0, 0]);
136 wtu
.checkCanvas(gl
, [255, 255, 255, 255], "Should be white");
138 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "there should be no errors");
140 var successfullyParsed
= true;
142 <script src=
"../../../js/js-test-post.js"></script>