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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
gpu / command_buffer / tests / gl_program_unittest.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2extchromium.h>
#include <stdint.h>
#include "gpu/command_buffer/service/context_group.h"
#include "gpu/command_buffer/tests/gl_manager.h"
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#define SHADER(Src) #Src
namespace gpu {
class GLProgramTest : public testing::Test {
protected:
void SetUp() override { gl_.Initialize(GLManager::Options()); }
void TearDown() override { gl_.Destroy(); }
GLManager gl_;
};
class WebGLProgramTest : public GLProgramTest {
protected:
void SetUp() override {
GLManager::Options options;
options.context_type = CONTEXT_TYPE_WEBGL1;
gl_.Initialize(options);
}
};
TEST_F(GLProgramTest, GetSetUniform) {
static const char* v_shader_str = SHADER(
attribute vec4 a_vertex;
attribute vec3 a_normal;
uniform mat4 u_modelViewProjMatrix;
struct MyStruct
{
int x;
int y;
};
uniform MyStruct u_struct;
uniform float u_array[4];
varying vec3 v_normal;
void main()
{
v_normal = a_normal;
gl_Position = u_modelViewProjMatrix * a_vertex +
vec4(u_struct.x, u_struct.y, 0, 1) +
vec4(u_array[0], u_array[1], u_array[2], u_array[3]);
}
);
static const char* f_shader_str = SHADER(
varying mediump vec3 v_normal;
void main()
{
gl_FragColor = vec4(v_normal/2.0+vec3(0.5), 1);
}
);
// Load the program.
GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
glUseProgram(program);
// Relink program.
glLinkProgram(program);
// These tests will fail on NVidia if not worked around by
// command buffer.
GLint location_sx = glGetUniformLocation(program, "u_struct.x");
GLint location_array_0 = glGetUniformLocation(program, "u_array[0]");
glUniform1i(location_sx, 3);
glUniform1f(location_array_0, 123);
GLint int_value = 0;
GLfloat float_value = 0;
glGetUniformiv(program, location_sx, &int_value);
EXPECT_EQ(3, int_value);
glGetUniformfv(program, location_array_0, &float_value);
EXPECT_EQ(123.0f, float_value);
GLTestHelper::CheckGLError("no errors", __LINE__);
}
TEST_F(GLProgramTest, NewShaderInCurrentProgram) {
static const char* v_shader_str = SHADER(
attribute vec4 a_position;
void main()
{
gl_Position = a_position;
}
);
static const char* f_red_shader_str = SHADER(
void main()
{
gl_FragColor = vec4(1, 0, 0, 1);
}
);
static const char* f_blue_shader_str = SHADER(
void main()
{
gl_FragColor = vec4(0, 0, 1, 1);
}
);
// Load the program.
GLuint vs = GLTestHelper::LoadShader(GL_VERTEX_SHADER, v_shader_str);
GLuint fs = GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, f_red_shader_str);
GLuint program = GLTestHelper::SetupProgram(vs, fs);
glUseProgram(program);
glShaderSource(fs, 1, &f_blue_shader_str, nullptr);
glCompileShader(fs);
glLinkProgram(program);
// We specifically don't call UseProgram again.
GLuint position_loc = glGetAttribLocation(program, "a_position");
GLTestHelper::SetupUnitQuad(position_loc);
glDrawArrays(GL_TRIANGLES, 0, 6);
uint8_t expected_color[] = {
0, 0, 255, 255,
};
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color, nullptr));
GLTestHelper::CheckGLError("no errors", __LINE__);
}
TEST_F(GLProgramTest, ShaderLengthSpecified) {
const std::string valid_shader_str = SHADER(
attribute vec4 a_position;
void main()
{
gl_Position = a_position;
}
);
const std::string invalid_shader = valid_shader_str + "invalid suffix";
// Compiling invalid program should fail.
const char* invalid_shader_strings[] = { invalid_shader.c_str() };
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, invalid_shader_strings, nullptr);
glCompileShader(vs);
GLint compile_state = 0;
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_state);
EXPECT_EQ(GL_FALSE, compile_state);
// Compiling program cutting off invalid parts should succeed.
const GLint lengths[] = {static_cast<GLint>(valid_shader_str.length())};
glShaderSource(vs, 1, invalid_shader_strings, lengths);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_state);
EXPECT_EQ(GL_TRUE, compile_state);
}
TEST_F(GLProgramTest, UniformsInCurrentProgram) {
static const char* v_shader_str = SHADER(
attribute vec4 a_position;
void main()
{
gl_Position = a_position;
}
);
static const char* f_shader_str = SHADER(
precision mediump float;
uniform vec4 u_color;
void main()
{
gl_FragColor = u_color;
}
);
// Load the program.
GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
glUseProgram(program);
// Relink.
glLinkProgram(program);
// This test will fail on NVidia Linux if not worked around.
GLint color_location = glGetUniformLocation(program, "u_color");
glUniform4f(color_location, 0.0f, 0.0f, 1.0f, 1.0f);
// We specifically don't call UseProgram again.
GLuint position_loc = glGetAttribLocation(program, "a_position");
GLTestHelper::SetupUnitQuad(position_loc);
glDrawArrays(GL_TRIANGLES, 0, 6);
uint8_t expected_color[] = {
0, 0, 255, 255,
};
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color, nullptr));
GLTestHelper::CheckGLError("no errors", __LINE__);
}
TEST_F(WebGLProgramTest, DeferCompileWithExt) {
// This test must have extensions enabled.
gles2::ContextGroup* context_group = gl_.decoder()->GetContextGroup();
gles2::FeatureInfo* feature_info = context_group->feature_info();
const gles2::FeatureInfo::FeatureFlags& flags = feature_info->feature_flags();
if (!flags.ext_frag_depth)
return;
static const char* v_shdr_str = R"(
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
)";
static const char* f_shdr_str = R"(
#extension GL_EXT_frag_depth : enable
void main()
{
gl_FragDepthEXT = 1.0;
}
)";
// Extension is disabled by default and be sure shader does not compile.
GLuint vs_bad = GLTestHelper::CompileShader(GL_VERTEX_SHADER, v_shdr_str);
GLuint fs_bad = GLTestHelper::CompileShader(GL_FRAGMENT_SHADER, f_shdr_str);
GLuint program_bad = GLTestHelper::LinkProgram(vs_bad, fs_bad);
GLint linked_bad = 0;
glGetProgramiv(program_bad, GL_LINK_STATUS, &linked_bad);
EXPECT_EQ(0, linked_bad);
// linking bad compilations with extension enabled should still not link.
glRequestExtensionCHROMIUM("GL_EXT_frag_depth");
program_bad = GLTestHelper::LinkProgram(vs_bad, fs_bad);
linked_bad = 0;
glGetProgramiv(program_bad, GL_LINK_STATUS, &linked_bad);
EXPECT_EQ(0, linked_bad);
// Be sure extension was actually turned on by recompiling.
GLuint vs_good = GLTestHelper::LoadShader(GL_VERTEX_SHADER, v_shdr_str);
GLuint fs_good = GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, f_shdr_str);
GLuint program_good = GLTestHelper::SetupProgram(vs_good, fs_good);
EXPECT_NE(0u, program_good);
}
TEST_F(GLProgramTest, DeleteAttachedShaderLinks) {
static const char* v_shdr_str = R"(
attribute vec4 vPosition;
void main()
{
gl_Position = vPosition;
}
)";
static const char* f_shdr_str = R"(
void main()
{
gl_FragColor = vec4(1, 1, 1, 1);
}
)";
// Compiling the shaders, attaching, then deleting before linking should work.
GLuint vs = GLTestHelper::CompileShader(GL_VERTEX_SHADER, v_shdr_str);
GLuint fs = GLTestHelper::CompileShader(GL_FRAGMENT_SHADER, f_shdr_str);
GLuint program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glDeleteShader(vs);
glDeleteShader(fs);
glLinkProgram(program);
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
EXPECT_NE(0, linked);
}
} // namespace gpu