Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_program_unittest.cc
blobb99aa58388000a0f82fd0ad51ca32e5b68329f6e
1 // Copyright 2014 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.
5 #include <GLES2/gl2.h>
6 #include <GLES2/gl2ext.h>
8 #include "gpu/command_buffer/tests/gl_manager.h"
9 #include "gpu/command_buffer/tests/gl_test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 #define SHADER(Src) #Src
15 namespace gpu {
17 class GLProgramTest : public testing::Test {
18 protected:
19 virtual void SetUp() {
20 gl_.Initialize(GLManager::Options());
23 virtual void TearDown() {
24 gl_.Destroy();
27 GLManager gl_;
30 TEST_F(GLProgramTest, GetSetUniform) {
31 static const char* v_shader_str = SHADER(
32 attribute vec4 a_vertex;
33 attribute vec3 a_normal;
35 uniform mat4 u_modelViewProjMatrix;
37 struct MyStruct
39 int x;
40 int y;
43 uniform MyStruct u_struct;
44 uniform float u_array[4];
46 varying vec3 v_normal;
48 void main()
50 v_normal = a_normal;
51 gl_Position = u_modelViewProjMatrix * a_vertex +
52 vec4(u_struct.x, u_struct.y, 0, 1) +
53 vec4(u_array[0], u_array[1], u_array[2], u_array[3]);
56 static const char* f_shader_str = SHADER(
57 varying mediump vec3 v_normal;
59 void main()
61 gl_FragColor = vec4(v_normal/2.0+vec3(0.5), 1);
65 // Load the program.
66 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
67 glUseProgram(program);
68 // Relink program.
69 glLinkProgram(program);
71 // These tests will fail on NVidia if not worked around by
72 // command buffer.
73 GLint location_sx = glGetUniformLocation(program, "u_struct.x");
74 GLint location_array_0 = glGetUniformLocation(program, "u_array[0]");
76 glUniform1i(location_sx, 3);
77 glUniform1f(location_array_0, 123);
79 GLint int_value = 0;
80 GLfloat float_value = 0;
82 glGetUniformiv(program, location_sx, &int_value);
83 EXPECT_EQ(3, int_value);
84 glGetUniformfv(program, location_array_0, &float_value);
85 EXPECT_EQ(123.0f, float_value);
87 GLTestHelper::CheckGLError("no errors", __LINE__);
90 TEST_F(GLProgramTest, NewShaderInCurrentProgram) {
91 static const char* v_shader_str = SHADER(
92 attribute vec4 a_position;
93 void main()
95 gl_Position = a_position;
98 static const char* f_red_shader_str = SHADER(
99 void main()
101 gl_FragColor = vec4(1, 0, 0, 1);
104 static const char* f_blue_shader_str = SHADER(
105 void main()
107 gl_FragColor = vec4(0, 0, 1, 1);
111 // Load the program.
112 GLuint vs = GLTestHelper::LoadShader(GL_VERTEX_SHADER, v_shader_str);
113 GLuint fs = GLTestHelper::LoadShader(GL_FRAGMENT_SHADER, f_red_shader_str);
114 GLuint program = GLTestHelper::SetupProgram(vs, fs);
115 glUseProgram(program);
116 glShaderSource(fs, 1, &f_blue_shader_str, NULL);
117 glCompileShader(fs);
118 glLinkProgram(program);
119 // We specifically don't call UseProgram again.
120 GLuint position_loc = glGetAttribLocation(program, "a_position");
121 GLTestHelper::SetupUnitQuad(position_loc);
122 glDrawArrays(GL_TRIANGLES, 0, 6);
123 uint8 expected_color[] = { 0, 0, 255, 255, };
124 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color));
125 GLTestHelper::CheckGLError("no errors", __LINE__);
128 TEST_F(GLProgramTest, UniformsInCurrentProgram) {
129 static const char* v_shader_str = SHADER(
130 attribute vec4 a_position;
131 void main()
133 gl_Position = a_position;
136 static const char* f_shader_str = SHADER(
137 precision mediump float;
138 uniform vec4 u_color;
139 void main()
141 gl_FragColor = u_color;;
145 // Load the program.
146 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
147 glUseProgram(program);
149 // Relink.
150 glLinkProgram(program);
152 // This test will fail on NVidia Linux if not worked around.
153 GLint color_location = glGetUniformLocation(program, "u_color");
154 glUniform4f(color_location, 0.0f, 0.0f, 1.0f, 1.0f);
156 // We specifically don't call UseProgram again.
157 GLuint position_loc = glGetAttribLocation(program, "a_position");
158 GLTestHelper::SetupUnitQuad(position_loc);
159 glDrawArrays(GL_TRIANGLES, 0, 6);
160 uint8 expected_color[] = { 0, 0, 255, 255, };
161 EXPECT_TRUE(GLTestHelper::CheckPixels(0, 0, 1, 1, 0, expected_color));
162 GLTestHelper::CheckGLError("no errors", __LINE__);
165 } // namespace gpu