Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / tests / gl_bind_uniform_location_unittest.cc
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.
4
5 #include <GLES2/gl2.h>
6 #include <GLES2/gl2ext.h>
7 #include <GLES2/gl2extchromium.h>
8
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"
13
14 #define SHADER(Src) #Src
15
16 namespace gpu {
17
18 class BindUniformLocationTest : public testing::Test {
19  protected:
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);
25   }
26
27   void TearDown() override { gl_.Destroy(); }
28
29   GLManager gl_;
30 };
31
32 TEST_F(BindUniformLocationTest, Basic) {
33   ASSERT_TRUE(
34       GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
35
36   static const char* v_shader_str = SHADER(
37       attribute vec4 a_position;
38       void main()
39       {
40          gl_Position = a_position;
41       }
42   );
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;
48       void main()
49       {
50         gl_FragColor = u_colorA + u_colorB[0] + u_colorB[1] + u_colorC;
51       }
52   );
53
54   GLint color_a_location = 3;
55   GLint color_b_location = 10;
56   GLint color_c_location = 5;
57
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);
62
63   GLuint program = glCreateProgram();
64
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");
68
69   glAttachShader(program, vertex_shader);
70   glAttachShader(program, fragment_shader);
71   // Link the program
72   glLinkProgram(program);
73   // Check the link status
74   GLint linked = 0;
75   glGetProgramiv(program, GL_LINK_STATUS, &linked);
76   EXPECT_EQ(1, linked);
77
78   GLint position_loc = glGetAttribLocation(program, "a_position");
79
80   GLTestHelper::SetupUnitQuad(position_loc);
81
82   glUseProgram(program);
83
84   static const float color_b[] = {
85     0.0f, 0.50f, 0.0f, 0.0f,
86     0.0f, 0.0f, 0.75f, 0.0f,
87   };
88
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);
92
93   glDrawArrays(GL_TRIANGLES, 0, 6);
94
95   static const uint8 expected[] = { 64, 128, 192, 255 };
96   EXPECT_TRUE(
97       GLTestHelper::CheckPixels(0, 0, kResolution, kResolution, 1, expected));
98
99   GLTestHelper::CheckGLError("no errors", __LINE__);
100 }
101
102 TEST_F(BindUniformLocationTest, Compositor) {
103   ASSERT_TRUE(
104       GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
105
106   static const char* v_shader_str = SHADER(
107       attribute vec4 a_position;
108       attribute vec2 a_texCoord;
109       uniform mat4 matrix;
110       uniform vec2 color_a[4];
111       uniform vec4 color_b;
112       varying vec4 v_color;
113       void main()
114       {
115           v_color.xy = color_a[0] + color_a[1];
116           v_color.zw = color_a[2] + color_a[3];
117           v_color += color_b;
118           gl_Position = matrix * a_position;
119       }
120   );
121
122   static const char* f_shader_str =  SHADER(
123       precision mediump float;
124       varying vec4 v_color;
125       uniform float alpha;
126       uniform vec4 multiplier;
127       uniform vec3 color_c[8];
128       void main()
129       {
130           vec4 color_c_sum = vec4(0.0);
131           color_c_sum.xyz += color_c[0];
132           color_c_sum.xyz += color_c[1];
133           color_c_sum.xyz += color_c[2];
134           color_c_sum.xyz += color_c[3];
135           color_c_sum.xyz += color_c[4];
136           color_c_sum.xyz += color_c[5];
137           color_c_sum.xyz += color_c[6];
138           color_c_sum.xyz += color_c[7];
139           color_c_sum.w = alpha;
140           color_c_sum *= multiplier;
141           gl_FragColor = v_color + color_c_sum;
142       }
143   );
144
145   int counter = 0;
146   int matrix_location = counter++;
147   int color_a_location = counter++;
148   int color_b_location = counter++;
149   int alpha_location = counter++;
150   int multiplier_location = counter++;
151   int color_c_location = counter++;
152
153   GLuint vertex_shader = GLTestHelper::LoadShader(
154       GL_VERTEX_SHADER, v_shader_str);
155   GLuint fragment_shader = GLTestHelper::LoadShader(
156       GL_FRAGMENT_SHADER, f_shader_str);
157
158   GLuint program = glCreateProgram();
159
160   glBindUniformLocationCHROMIUM(program, matrix_location, "matrix");
161   glBindUniformLocationCHROMIUM(program, color_a_location, "color_a");
162   glBindUniformLocationCHROMIUM(program, color_b_location, "color_b");
163   glBindUniformLocationCHROMIUM(program, alpha_location, "alpha");
164   glBindUniformLocationCHROMIUM(program, multiplier_location, "multiplier");
165   glBindUniformLocationCHROMIUM(program, color_c_location, "color_c");
166
167   glAttachShader(program, vertex_shader);
168   glAttachShader(program, fragment_shader);
169   // Link the program
170   glLinkProgram(program);
171   // Check the link status
172   GLint linked = 0;
173   glGetProgramiv(program, GL_LINK_STATUS, &linked);
174   EXPECT_EQ(1, linked);
175
176   GLint position_loc = glGetAttribLocation(program, "a_position");
177
178   GLTestHelper::SetupUnitQuad(position_loc);
179
180   glUseProgram(program);
181
182   static const float color_a[] = {
183     0.1f, 0.1f, 0.1f, 0.1f,
184     0.1f, 0.1f, 0.1f, 0.1f,
185   };
186
187   static const float color_c[] = {
188     0.1f, 0.1f, 0.1f,
189     0.1f, 0.1f, 0.1f,
190     0.1f, 0.1f, 0.1f,
191     0.1f, 0.1f, 0.1f,
192     0.1f, 0.1f, 0.1f,
193     0.1f, 0.1f, 0.1f,
194     0.1f, 0.1f, 0.1f,
195     0.1f, 0.1f, 0.1f,
196   };
197
198   static const float identity[] = {
199     1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
200   };
201
202   glUniformMatrix4fv(matrix_location, 1, false, identity);
203   glUniform2fv(color_a_location, 4, color_a);
204   glUniform4f(color_b_location, 0.2f, 0.2f, 0.2f, 0.2f);
205   glUniform1f(alpha_location, 0.8f);
206   glUniform4f(multiplier_location, 0.5f, 0.5f, 0.5f, 0.5f);
207   glUniform3fv(color_c_location, 8, color_c);
208
209   glDrawArrays(GL_TRIANGLES, 0, 6);
210
211   static const uint8 expected[] = { 204, 204, 204, 204 };
212   EXPECT_TRUE(
213       GLTestHelper::CheckPixels(0, 0, kResolution, kResolution, 1, expected));
214
215   GLTestHelper::CheckGLError("no errors", __LINE__);
216
217 }
218
219 }  // namespace gpu
220
221
222