| OLD | NEW |
| (Empty) | |
| 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 |
| 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" |
| 12 |
| 13 #define SHADER(Src) #Src |
| 14 |
| 15 namespace gpu { |
| 16 |
| 17 class DepthTextureTest : public testing::Test { |
| 18 protected: |
| 19 static const GLsizei kResolution = 64; |
| 20 virtual void SetUp() { |
| 21 gl_.Initialize(gfx::Size(kResolution, kResolution)); |
| 22 } |
| 23 |
| 24 virtual void TearDown() { |
| 25 gl_.Destroy(); |
| 26 } |
| 27 |
| 28 GLuint SetupUnitQuad(GLint position_location); |
| 29 |
| 30 GLManager gl_; |
| 31 }; |
| 32 |
| 33 |
| 34 |
| 35 GLuint DepthTextureTest::SetupUnitQuad(GLint position_location) { |
| 36 GLuint vbo = 0; |
| 37 glGenBuffers(1, &vbo); |
| 38 glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 39 static float vertices[] = { |
| 40 1.0f, 1.0f, 1.0f, |
| 41 -1.0f, 1.0f, 0.0f, |
| 42 -1.0f, -1.0f, -1.0f, |
| 43 1.0f, 1.0f, 1.0f, |
| 44 -1.0f, -1.0f, -1.0f, |
| 45 1.0f, -1.0f, 0.0f, |
| 46 }; |
| 47 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 48 glEnableVertexAttribArray(position_location); |
| 49 glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 50 |
| 51 return vbo; |
| 52 } |
| 53 |
| 54 namespace { |
| 55 |
| 56 struct FormatType { |
| 57 GLenum format; |
| 58 GLenum type; |
| 59 }; |
| 60 |
| 61 } // anonymous namespace |
| 62 |
| 63 TEST_F(DepthTextureTest, RenderTo) { |
| 64 if (!GLTestHelper::HasExtension("GL_CHROMIUM_depth_texture")) { |
| 65 return; |
| 66 } |
| 67 |
| 68 bool have_depth_stencil = GLTestHelper::HasExtension( |
| 69 "GL_OES_packed_depth_stencil"); |
| 70 |
| 71 static const char* v_shader_str = SHADER( |
| 72 attribute vec4 v_position; |
| 73 void main() |
| 74 { |
| 75 gl_Position = v_position; |
| 76 } |
| 77 ); |
| 78 static const char* f_shader_str = SHADER( |
| 79 precision mediump float; |
| 80 uniform sampler2D u_texture; |
| 81 uniform vec2 u_resolution; |
| 82 void main() |
| 83 { |
| 84 vec2 texcoord = gl_FragCoord.xy / u_resolution; |
| 85 gl_FragColor = texture2D(u_texture, texcoord); |
| 86 } |
| 87 ); |
| 88 |
| 89 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); |
| 90 |
| 91 GLint position_loc = glGetAttribLocation(program, "v_position"); |
| 92 GLint resolution_loc = glGetUniformLocation(program, "u_resolution"); |
| 93 |
| 94 SetupUnitQuad(position_loc); |
| 95 |
| 96 // Depth test needs to be on for the depth buffer to be updated. |
| 97 glEnable(GL_DEPTH_TEST); |
| 98 |
| 99 // create an fbo |
| 100 GLuint fbo = 0; |
| 101 glGenFramebuffers(1, &fbo); |
| 102 glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 103 |
| 104 // create a depth texture. |
| 105 GLuint color_texture = 0; |
| 106 GLuint depth_texture = 0; |
| 107 |
| 108 glGenTextures(1, &color_texture); |
| 109 glBindTexture(GL_TEXTURE_2D, color_texture); |
| 110 glTexImage2D( |
| 111 GL_TEXTURE_2D, 0, GL_RGBA, kResolution, kResolution, |
| 112 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 113 glFramebufferTexture2D( |
| 114 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_texture, 0); |
| 115 |
| 116 glGenTextures(1, &depth_texture); |
| 117 glBindTexture(GL_TEXTURE_2D, depth_texture); |
| 118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 122 glFramebufferTexture2D( |
| 123 GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0); |
| 124 |
| 125 glUseProgram(program); |
| 126 glUniform2f(resolution_loc, kResolution, kResolution); |
| 127 |
| 128 static const FormatType format_types[] = { |
| 129 { GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, }, |
| 130 { GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, }, |
| 131 { GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, }, |
| 132 }; |
| 133 for (size_t ii = 0; ii < arraysize(format_types); ++ii) { |
| 134 GLenum format = format_types[ii].format; |
| 135 GLenum type = format_types[ii].type; |
| 136 |
| 137 if (format == GL_DEPTH_STENCIL_OES && !have_depth_stencil) { |
| 138 continue; |
| 139 } |
| 140 |
| 141 glBindTexture(GL_TEXTURE_2D, depth_texture); |
| 142 glTexImage2D( |
| 143 GL_TEXTURE_2D, 0, format, kResolution, kResolution, |
| 144 0, format, type, NULL); |
| 145 |
| 146 glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 147 EXPECT_TRUE( |
| 148 glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); |
| 149 |
| 150 GLTestHelper::CheckGLError("no errors", __LINE__); |
| 151 |
| 152 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
| 153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 154 |
| 155 // Disconnect the texture so we'll render with the default texture. |
| 156 glBindTexture(GL_TEXTURE_2D, 0); |
| 157 |
| 158 // Render to the fbo. |
| 159 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 160 |
| 161 // Render with the depth texture. |
| 162 glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 163 glBindTexture(GL_TEXTURE_2D, depth_texture); |
| 164 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 165 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 166 |
| 167 uint8 actual_pixels[kResolution * kResolution * 4] = { 0, }; |
| 168 glReadPixels( |
| 169 0, 0, kResolution, kResolution, GL_RGBA, GL_UNSIGNED_BYTE, |
| 170 actual_pixels); |
| 171 |
| 172 // Check that each pixel's RGB are the same and that it's value is less |
| 173 // than the previous pixel in either direction. Basically verify we have a |
| 174 // gradient. |
| 175 for (GLint yy = 0; yy < kResolution; ++yy) { |
| 176 for (GLint xx = 0; xx < kResolution; ++xx) { |
| 177 const uint8* actual = &actual_pixels[(yy * kResolution + xx) * 4]; |
| 178 const uint8* left = actual - 4; |
| 179 const uint8* down = actual - kResolution * 4; |
| 180 |
| 181 EXPECT_EQ(actual[0], actual[1]); |
| 182 EXPECT_EQ(actual[1], actual[2]); |
| 183 EXPECT_EQ(0xFF, actual[3]); |
| 184 |
| 185 if (xx > 0) { |
| 186 EXPECT_GT(actual[0], left[0]); |
| 187 } |
| 188 if (yy > 0) { |
| 189 EXPECT_GT(actual[0], down[0]); |
| 190 } |
| 191 } |
| 192 } |
| 193 |
| 194 // Check that bottom left corner is vastly different thatn top right. |
| 195 EXPECT_GT( |
| 196 actual_pixels[(kResolution * kResolution - 1) * 4] - actual_pixels[0], |
| 197 0xC0); |
| 198 GLTestHelper::CheckGLError("no errors", __LINE__); |
| 199 } |
| 200 } |
| 201 |
| 202 } // namespace gpu |
| 203 |
| 204 |
| 205 |
| OLD | NEW |