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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 "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 BindUniformLocationTest : public testing::Test {
protected:
static const GLsizei kResolution = 4;
virtual void SetUp() {
GLManager::Options options;
options.size = gfx::Size(kResolution, kResolution);
gl_.Initialize(options);
}
virtual void TearDown() {
gl_.Destroy();
}
GLManager gl_;
};
TEST_F(BindUniformLocationTest, Basic) {
ASSERT_TRUE(
GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
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_colorC;
uniform vec4 u_colorB[2];
uniform vec4 u_colorA;
void main()
{
gl_FragColor = u_colorA + u_colorB[0] + u_colorB[1] + u_colorC;
}
);
GLint color_a_location = 3;
GLint color_b_location = 10;
GLint color_c_location = 5;
GLuint vertex_shader = GLTestHelper::LoadShader(
GL_VERTEX_SHADER, v_shader_str);
GLuint fragment_shader = GLTestHelper::LoadShader(
GL_FRAGMENT_SHADER, f_shader_str);
GLuint program = glCreateProgram();
glBindUniformLocationCHROMIUM(program, color_a_location, "u_colorA");
glBindUniformLocationCHROMIUM(program, color_b_location, "u_colorB[0]");
glBindUniformLocationCHROMIUM(program, color_c_location, "u_colorC");
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
// Link the program
glLinkProgram(program);
// Check the link status
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
EXPECT_EQ(1, linked);
GLint position_loc = glGetAttribLocation(program, "a_position");
GLTestHelper::SetupUnitQuad(position_loc);
glUseProgram(program);
static const float color_b[] = {
0.0f, 0.50f, 0.0f, 0.0f,
0.0f, 0.0f, 0.75f, 0.0f,
};
glUniform4f(color_a_location, 0.25f, 0.0f, 0.0f, 0.0f);
glUniform4fv(color_b_location, 2, color_b);
glUniform4f(color_c_location, 0.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 6);
static const uint8 expected[] = { 64, 128, 192, 255 };
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, kResolution, kResolution, 1, expected));
GLTestHelper::CheckGLError("no errors", __LINE__);
}
TEST_F(BindUniformLocationTest, Compositor) {
ASSERT_TRUE(
GLTestHelper::HasExtension("GL_CHROMIUM_bind_uniform_location"));
static const char* v_shader_str = SHADER(
attribute vec4 a_position;
attribute vec2 a_texCoord;
uniform mat4 matrix;
uniform vec2 color_a[4];
uniform vec4 color_b;
varying vec4 v_color;
void main()
{
v_color.xy = color_a[0] + color_a[1];
v_color.zw = color_a[2] + color_a[3];
v_color += color_b;
gl_Position = matrix * a_position;
}
);
static const char* f_shader_str = SHADER(
precision mediump float;
varying vec4 v_color;
uniform float alpha;
uniform vec4 multiplier;
uniform vec3 color_c[8];
void main()
{
vec4 color_c_sum = vec4(0.0);
color_c_sum.xyz += color_c[0];
color_c_sum.xyz += color_c[1];
color_c_sum.xyz += color_c[2];
color_c_sum.xyz += color_c[3];
color_c_sum.xyz += color_c[4];
color_c_sum.xyz += color_c[5];
color_c_sum.xyz += color_c[6];
color_c_sum.xyz += color_c[7];
color_c_sum.w = alpha;
color_c_sum *= multiplier;
gl_FragColor = v_color + color_c_sum;
}
);
int counter = 0;
int matrix_location = counter++;
int color_a_location = counter++;
int color_b_location = counter++;
int alpha_location = counter++;
int multiplier_location = counter++;
int color_c_location = counter++;
GLuint vertex_shader = GLTestHelper::LoadShader(
GL_VERTEX_SHADER, v_shader_str);
GLuint fragment_shader = GLTestHelper::LoadShader(
GL_FRAGMENT_SHADER, f_shader_str);
GLuint program = glCreateProgram();
glBindUniformLocationCHROMIUM(program, matrix_location, "matrix");
glBindUniformLocationCHROMIUM(program, color_a_location, "color_a");
glBindUniformLocationCHROMIUM(program, color_b_location, "color_b");
glBindUniformLocationCHROMIUM(program, alpha_location, "alpha");
glBindUniformLocationCHROMIUM(program, multiplier_location, "multiplier");
glBindUniformLocationCHROMIUM(program, color_c_location, "color_c");
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
// Link the program
glLinkProgram(program);
// Check the link status
GLint linked = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
EXPECT_EQ(1, linked);
GLint position_loc = glGetAttribLocation(program, "a_position");
GLTestHelper::SetupUnitQuad(position_loc);
glUseProgram(program);
static const float color_a[] = {
0.1f, 0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f, 0.1f,
};
static const float color_c[] = {
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
0.1f, 0.1f, 0.1f,
};
static const float identity[] = {
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
};
glUniformMatrix4fv(matrix_location, 1, false, identity);
glUniform2fv(color_a_location, 4, color_a);
glUniform4f(color_b_location, 0.2f, 0.2f, 0.2f, 0.2f);
glUniform1f(alpha_location, 0.8f);
glUniform4f(multiplier_location, 0.5f, 0.5f, 0.5f, 0.5f);
glUniform3fv(color_c_location, 8, color_c);
glDrawArrays(GL_TRIANGLES, 0, 6);
static const uint8 expected[] = { 204, 204, 204, 204 };
EXPECT_TRUE(
GLTestHelper::CheckPixels(0, 0, kResolution, kResolution, 1, expected));
GLTestHelper::CheckGLError("no errors", __LINE__);
}
} // namespace gpu
|