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
|
// 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 "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 DepthTextureTest : public testing::Test {
protected:
static const GLsizei kResolution = 64;
virtual void SetUp() {
gl_.Initialize(gfx::Size(kResolution, kResolution));
}
virtual void TearDown() {
gl_.Destroy();
}
GLuint SetupUnitQuad(GLint position_location);
GLManager gl_;
};
GLuint DepthTextureTest::SetupUnitQuad(GLint position_location) {
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
static float vertices[] = {
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 0.0f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(position_location);
glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
return vbo;
}
namespace {
struct FormatType {
GLenum format;
GLenum type;
};
} // anonymous namespace
TEST_F(DepthTextureTest, RenderTo) {
if (!GLTestHelper::HasExtension("GL_CHROMIUM_depth_texture")) {
return;
}
bool have_depth_stencil = GLTestHelper::HasExtension(
"GL_OES_packed_depth_stencil");
static const char* v_shader_str = SHADER(
attribute vec4 v_position;
void main()
{
gl_Position = v_position;
}
);
static const char* f_shader_str = SHADER(
precision mediump float;
uniform sampler2D u_texture;
uniform vec2 u_resolution;
void main()
{
vec2 texcoord = gl_FragCoord.xy / u_resolution;
gl_FragColor = texture2D(u_texture, texcoord);
}
);
GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
GLint position_loc = glGetAttribLocation(program, "v_position");
GLint resolution_loc = glGetUniformLocation(program, "u_resolution");
SetupUnitQuad(position_loc);
// Depth test needs to be on for the depth buffer to be updated.
glEnable(GL_DEPTH_TEST);
// create an fbo
GLuint fbo = 0;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// create a depth texture.
GLuint color_texture = 0;
GLuint depth_texture = 0;
glGenTextures(1, &color_texture);
glBindTexture(GL_TEXTURE_2D, color_texture);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, kResolution, kResolution,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_texture, 0);
glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D, depth_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(
GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0);
glUseProgram(program);
glUniform2f(resolution_loc, kResolution, kResolution);
static const FormatType format_types[] = {
{ GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, },
{ GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, },
{ GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, },
};
for (size_t ii = 0; ii < arraysize(format_types); ++ii) {
GLenum format = format_types[ii].format;
GLenum type = format_types[ii].type;
if (format == GL_DEPTH_STENCIL_OES && !have_depth_stencil) {
continue;
}
glBindTexture(GL_TEXTURE_2D, depth_texture);
glTexImage2D(
GL_TEXTURE_2D, 0, format, kResolution, kResolution,
0, format, type, NULL);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
EXPECT_TRUE(
glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
GLTestHelper::CheckGLError("no errors", __LINE__);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Disconnect the texture so we'll render with the default texture.
glBindTexture(GL_TEXTURE_2D, 0);
// Render to the fbo.
glDrawArrays(GL_TRIANGLES, 0, 6);
// Render with the depth texture.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, depth_texture);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
uint8 actual_pixels[kResolution * kResolution * 4] = { 0, };
glReadPixels(
0, 0, kResolution, kResolution, GL_RGBA, GL_UNSIGNED_BYTE,
actual_pixels);
// Check that each pixel's RGB are the same and that it's value is less
// than the previous pixel in either direction. Basically verify we have a
// gradient.
for (GLint yy = 0; yy < kResolution; ++yy) {
for (GLint xx = 0; xx < kResolution; ++xx) {
const uint8* actual = &actual_pixels[(yy * kResolution + xx) * 4];
const uint8* left = actual - 4;
const uint8* down = actual - kResolution * 4;
EXPECT_EQ(actual[0], actual[1]);
EXPECT_EQ(actual[1], actual[2]);
if (xx > 0) {
EXPECT_GT(actual[0], left[0]);
}
if (yy > 0) {
EXPECT_GT(actual[0], down[0]);
}
EXPECT_TRUE(actual[3] == actual[0] || actual[3] == 0xFF);
}
}
// Check that bottom left corner is vastly different thatn top right.
EXPECT_GT(
actual_pixels[(kResolution * kResolution - 1) * 4] - actual_pixels[0],
0xC0);
GLTestHelper::CheckGLError("no errors", __LINE__);
}
}
} // namespace gpu
|