-
Notifications
You must be signed in to change notification settings - Fork 669
/
shader-with-array-of-structs-uniform.html
145 lines (135 loc) · 4.49 KB
/
shader-with-array-of-structs-uniform.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GLSL Array of Structs Uniform</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<link rel="stylesheet" href="../../../resources/glsl-feature-tests.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"> </script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 a_position;
void main(void) {
gl_Position = a_position;
}
</script>
<script id="fshader0" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
vec4 color;
};
uniform my_struct u_colors[2];
void main(void) {
gl_FragColor = u_colors[0].color;
}
</script>
<script id="fshader1" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
vec4 color;
};
uniform my_struct u_colors[2];
void main(void) {
gl_FragColor = u_colors[1].color;
}
</script>
<script id="fshader2" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
vec4 color1;
vec4 color2;
};
uniform my_struct u_colors[2];
void main(void) {
gl_FragColor = u_colors[0].color1 + u_colors[0].color2 + u_colors[1].color1 +u_colors[1].color2;
}
</script>
<script id="fshader3" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
float r;
float g;
float b;
float a;
};
uniform my_struct u_colors[2];
void main(void) {
gl_FragColor = vec4(u_colors[0].r, u_colors[0].g, u_colors[1].b, u_colors[1].a);
}
</script>
</head>
<body>
<canvas id="canvas" width="50" height="50"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description();
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas");
wtu.setupUnitQuad(gl);
for (var ii = 0; ii < 2; ++ii) {
var program = wtu.setupProgram(gl, ["vshader", "fshader" + ii], ["a_position"]);
var red_loc = gl.getUniformLocation(program, "u_colors[" + ii + "].color");
var green_loc = gl.getUniformLocation(program, "u_colors[" + (1 - ii) + "].color");
gl.uniform4fv(red_loc, [1, 0, 0, 1]);
gl.uniform4fv(green_loc, [0, 1, 0, 1]);
wtu.clearAndDrawUnitQuad(gl);
wtu.checkCanvas(gl, [255, 0, 0, 255], "Should be red");
}
var program = wtu.setupProgram(gl, ["vshader", "fshader2"], ["a_position"]);
var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
shouldBe("numUniforms", "4");
var uniforms = []
for (var ii = 0; ii < numUniforms; ++ii) {
uniforms.push(gl.getActiveUniform(program, ii).name);
}
uniforms.sort();
shouldBe("uniforms[0]", '"u_colors[0].color1"');
shouldBe("uniforms[1]", '"u_colors[0].color2"');
shouldBe("uniforms[2]", '"u_colors[1].color1"');
shouldBe("uniforms[3]", '"u_colors[1].color2"');
var loc00 = gl.getUniformLocation(program, "u_colors[0].color1");
var loc01 = gl.getUniformLocation(program, "u_colors[0].color2");
var loc10 = gl.getUniformLocation(program, "u_colors[1].color1");
var loc11 = gl.getUniformLocation(program, "u_colors[1].color2");
shouldBeTrue("loc00 != undefined");
shouldBeTrue("loc01 != undefined");
shouldBeTrue("loc10 != undefined");
shouldBeTrue("loc11 != undefined");
gl.uniform4fv(loc00, [1, 0, 0, 0]);
gl.uniform4fv(loc01, [0, 1, 0, 0]);
gl.uniform4fv(loc10, [0, 0, 1, 0]);
gl.uniform4fv(loc11, [0, 0, 0, 1]);
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 0]);
wtu.checkCanvas(gl, [255, 255, 255, 255], "Should be white");
program = wtu.setupProgram(gl, ["vshader", "fshader3"], ["a_position"]);
var loc0r = gl.getUniformLocation(program, "u_colors[0].r");
var loc0g = gl.getUniformLocation(program, "u_colors[0].g");
var loc0b = gl.getUniformLocation(program, "u_colors[0].b");
var loc0a = gl.getUniformLocation(program, "u_colors[0].a");
var loc1r = gl.getUniformLocation(program, "u_colors[1].r");
var loc1g = gl.getUniformLocation(program, "u_colors[1].g");
var loc1b = gl.getUniformLocation(program, "u_colors[1].b");
var loc1a = gl.getUniformLocation(program, "u_colors[1].a");
shouldBeTrue("loc0r != undefined");
shouldBeTrue("loc0g != undefined");
shouldBeTrue("loc1b != undefined");
shouldBeTrue("loc1a != undefined");
gl.uniform1f(loc0r, 1);
gl.uniform1f(loc0g, 1);
gl.uniform1f(loc1b, 1);
gl.uniform1f(loc1a, 1);
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 0]);
wtu.checkCanvas(gl, [255, 255, 255, 255], "Should be white");
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>
</body>
</html>