1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include <GLES2/gl2ext.h>
7 #include <GLES2/gl2extchromium.h>
9 #include "gpu/command_buffer/tests/gl_manager.h"
10 #include "gpu/command_buffer/tests/gl_test_utils.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #define SHADER(Src) #Src
18 class BindUniformLocationTest
: public testing::Test
{
20 static const GLsizei kResolution
= 4;
21 void SetUp() override
{
22 GLManager::Options options
;
23 options
.size
= gfx::Size(kResolution
, kResolution
);
24 gl_
.Initialize(options
);
27 void TearDown() override
{ gl_
.Destroy(); }
32 TEST_F(BindUniformLocationTest
, Basic
) {
34 GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
36 static const char* v_shader_str
= SHADER(
37 attribute vec4 a_position
;
40 gl_Position
= a_position
;
43 static const char* f_shader_str
= SHADER(
44 precision mediump
float;
45 uniform vec4 u_colorC
;
46 uniform vec4 u_colorB
[2];
47 uniform vec4 u_colorA
;
50 gl_FragColor
= u_colorA
+ u_colorB
[0] + u_colorB
[1] + u_colorC
;
54 GLint color_a_location
= 3;
55 GLint color_b_location
= 10;
56 GLint color_c_location
= 5;
58 GLuint vertex_shader
= GLTestHelper::LoadShader(
59 GL_VERTEX_SHADER
, v_shader_str
);
60 GLuint fragment_shader
= GLTestHelper::LoadShader(
61 GL_FRAGMENT_SHADER
, f_shader_str
);
63 GLuint program
= glCreateProgram();
65 glBindUniformLocationCHROMIUM(program
, color_a_location
, "u_colorA");
66 glBindUniformLocationCHROMIUM(program
, color_b_location
, "u_colorB[0]");
67 glBindUniformLocationCHROMIUM(program
, color_c_location
, "u_colorC");
69 glAttachShader(program
, vertex_shader
);
70 glAttachShader(program
, fragment_shader
);
72 glLinkProgram(program
);
73 // Check the link status
75 glGetProgramiv(program
, GL_LINK_STATUS
, &linked
);
78 GLint position_loc
= glGetAttribLocation(program
, "a_position");
80 GLTestHelper::SetupUnitQuad(position_loc
);
82 glUseProgram(program
);
84 static const float color_b
[] = {
85 0.0f
, 0.50f
, 0.0f
, 0.0f
,
86 0.0f
, 0.0f
, 0.75f
, 0.0f
,
89 glUniform4f(color_a_location
, 0.25f
, 0.0f
, 0.0f
, 0.0f
);
90 glUniform4fv(color_b_location
, 2, color_b
);
91 glUniform4f(color_c_location
, 0.0f
, 0.0f
, 0.0f
, 1.0f
);
93 glDrawArrays(GL_TRIANGLES
, 0, 6);
95 static const uint8 expected
[] = { 64, 128, 192, 255 };
97 GLTestHelper::CheckPixels(0, 0, kResolution
, kResolution
, 1, expected
));
99 GLTestHelper::CheckGLError("no errors", __LINE__
);
102 TEST_F(BindUniformLocationTest
, ConflictsDetection
) {
104 GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
106 static const char* v_shader_str
= SHADER(
107 attribute vec4 a_position
;
110 gl_Position
= a_position
;
113 static const char* f_shader_str
= SHADER(
114 precision mediump
float;
115 uniform vec4 u_colorA
;
116 uniform vec4 u_colorB
;
119 gl_FragColor
= u_colorA
+ u_colorB
;
123 GLint color_a_location
= 3;
124 GLint color_b_location
= 4;
126 GLuint vertex_shader
= GLTestHelper::LoadShader(
127 GL_VERTEX_SHADER
, v_shader_str
);
128 GLuint fragment_shader
= GLTestHelper::LoadShader(
129 GL_FRAGMENT_SHADER
, f_shader_str
);
131 GLuint program
= glCreateProgram();
132 glAttachShader(program
, vertex_shader
);
133 glAttachShader(program
, fragment_shader
);
135 glBindUniformLocationCHROMIUM(program
, color_a_location
, "u_colorA");
136 // Bind u_colorB to location a, causing conflicts, link should fail.
137 glBindUniformLocationCHROMIUM(program
, color_a_location
, "u_colorB");
138 glLinkProgram(program
);
140 glGetProgramiv(program
, GL_LINK_STATUS
, &linked
);
141 EXPECT_EQ(0, linked
);
143 // Bind u_colorB to location a, no conflicts, link should succeed.
144 glBindUniformLocationCHROMIUM(program
, color_b_location
, "u_colorB");
145 glLinkProgram(program
);
147 glGetProgramiv(program
, GL_LINK_STATUS
, &linked
);
148 EXPECT_EQ(1, linked
);
150 GLTestHelper::CheckGLError("no errors", __LINE__
);
153 TEST_F(BindUniformLocationTest
, Compositor
) {
155 GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
157 static const char* v_shader_str
= SHADER(
158 attribute vec4 a_position
;
159 attribute vec2 a_texCoord
;
161 uniform vec2 color_a
[4];
162 uniform vec4 color_b
;
163 varying vec4 v_color
;
166 v_color
.xy
= color_a
[0] + color_a
[1];
167 v_color
.zw
= color_a
[2] + color_a
[3];
169 gl_Position
= matrix
* a_position
;
173 static const char* f_shader_str
= SHADER(
174 precision mediump
float;
175 varying vec4 v_color
;
177 uniform vec4 multiplier
;
178 uniform vec3 color_c
[8];
181 vec4 color_c_sum
= vec4(0.0);
182 color_c_sum
.xyz
+= color_c
[0];
183 color_c_sum
.xyz
+= color_c
[1];
184 color_c_sum
.xyz
+= color_c
[2];
185 color_c_sum
.xyz
+= color_c
[3];
186 color_c_sum
.xyz
+= color_c
[4];
187 color_c_sum
.xyz
+= color_c
[5];
188 color_c_sum
.xyz
+= color_c
[6];
189 color_c_sum
.xyz
+= color_c
[7];
190 color_c_sum
.w
= alpha
;
191 color_c_sum
*= multiplier
;
192 gl_FragColor
= v_color
+ color_c_sum
;
197 int matrix_location
= counter
++;
198 int color_a_location
= counter
++;
199 int color_b_location
= counter
++;
200 int alpha_location
= counter
++;
201 int multiplier_location
= counter
++;
202 int color_c_location
= counter
++;
204 GLuint vertex_shader
= GLTestHelper::LoadShader(
205 GL_VERTEX_SHADER
, v_shader_str
);
206 GLuint fragment_shader
= GLTestHelper::LoadShader(
207 GL_FRAGMENT_SHADER
, f_shader_str
);
209 GLuint program
= glCreateProgram();
211 glBindUniformLocationCHROMIUM(program
, matrix_location
, "matrix");
212 glBindUniformLocationCHROMIUM(program
, color_a_location
, "color_a");
213 glBindUniformLocationCHROMIUM(program
, color_b_location
, "color_b");
214 glBindUniformLocationCHROMIUM(program
, alpha_location
, "alpha");
215 glBindUniformLocationCHROMIUM(program
, multiplier_location
, "multiplier");
216 glBindUniformLocationCHROMIUM(program
, color_c_location
, "color_c");
218 glAttachShader(program
, vertex_shader
);
219 glAttachShader(program
, fragment_shader
);
221 glLinkProgram(program
);
222 // Check the link status
224 glGetProgramiv(program
, GL_LINK_STATUS
, &linked
);
225 EXPECT_EQ(1, linked
);
227 GLint position_loc
= glGetAttribLocation(program
, "a_position");
229 GLTestHelper::SetupUnitQuad(position_loc
);
231 glUseProgram(program
);
233 static const float color_a
[] = {
234 0.1f
, 0.1f
, 0.1f
, 0.1f
,
235 0.1f
, 0.1f
, 0.1f
, 0.1f
,
238 static const float color_c
[] = {
249 static const float identity
[] = {
250 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
253 glUniformMatrix4fv(matrix_location
, 1, false, identity
);
254 glUniform2fv(color_a_location
, 4, color_a
);
255 glUniform4f(color_b_location
, 0.2f
, 0.2f
, 0.2f
, 0.2f
);
256 glUniform1f(alpha_location
, 0.8f
);
257 glUniform4f(multiplier_location
, 0.5f
, 0.5f
, 0.5f
, 0.5f
);
258 glUniform3fv(color_c_location
, 8, color_c
);
260 glDrawArrays(GL_TRIANGLES
, 0, 6);
262 static const uint8 expected
[] = { 204, 204, 204, 204 };
264 GLTestHelper::CheckPixels(0, 0, kResolution
, kResolution
, 1, expected
));
266 GLTestHelper::CheckGLError("no errors", __LINE__
);