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 #ifndef GL_GLEXT_PROTOTYPES |
| 6 #define GL_GLEXT_PROTOTYPES |
| 7 #endif |
| 8 |
| 9 #include <GLES2/gl2.h> |
| 10 #include <GLES2/gl2ext.h> |
| 11 |
| 12 #include "gpu/command_buffer/tests/gl_manager.h" |
| 13 #include "gpu/command_buffer/tests/gl_test_utils.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 #define SHADER(Src) #Src |
| 18 |
| 19 namespace gpu { |
| 20 |
| 21 static const char* v_shader_str = SHADER( |
| 22 attribute vec4 v_position; |
| 23 void main() { |
| 24 gl_Position = v_position; |
| 25 } |
| 26 ); |
| 27 |
| 28 static const char* f_shader_str = SHADER( |
| 29 uniform sampler2D u_texture; |
| 30 void main() { |
| 31 gl_FragColor = texture2D(u_texture, vec2(0.5, 0.5)) * |
| 32 vec4(4.0, 2.0, 0.5, 0.25); |
| 33 } |
| 34 ); |
| 35 |
| 36 class GLANGLEColorBufferFloatTest : public testing::Test { |
| 37 protected: |
| 38 virtual void SetUp() { |
| 39 gl_.Initialize(GLManager::Options()); |
| 40 |
| 41 glGenTextures(2, textures); |
| 42 glActiveTexture(GL_TEXTURE0); |
| 43 glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 44 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 45 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 48 |
| 49 glGenFramebuffers(1, &fbo); |
| 50 glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 51 } |
| 52 |
| 53 virtual void TearDown() { |
| 54 glDeleteTextures(2, textures); |
| 55 glDeleteFramebuffers(1, &fbo); |
| 56 gl_.Destroy(); |
| 57 } |
| 58 |
| 59 GLuint SetupUnitQuad(GLint position_location); |
| 60 |
| 61 GLManager gl_; |
| 62 GLuint textures[2]; |
| 63 GLuint fbo; |
| 64 }; |
| 65 |
| 66 GLuint GLANGLEColorBufferFloatTest::SetupUnitQuad(GLint position_location) { |
| 67 GLuint vbo = 0; |
| 68 glGenBuffers(1, &vbo); |
| 69 glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 70 static float vertices[] = { |
| 71 1.0f, 1.0f, 1.0f, |
| 72 -1.0f, 1.0f, 0.0f, |
| 73 -1.0f, -1.0f, -1.0f, |
| 74 1.0f, 1.0f, 1.0f, |
| 75 -1.0f, -1.0f, -1.0f, |
| 76 1.0f, -1.0f, 0.0f, |
| 77 }; |
| 78 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 79 glEnableVertexAttribArray(position_location); |
| 80 glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 81 |
| 82 return vbo; |
| 83 } |
| 84 |
| 85 TEST_F(GLANGLEColorBufferFloatTest, RenderBuffers) { |
| 86 if (!GLTestHelper::HasExtension("GL_ANGLE_color_buffer_float")) { |
| 87 return; |
| 88 } |
| 89 |
| 90 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); |
| 91 glUseProgram(program); |
| 92 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 93 |
| 94 GLint position_loc = glGetAttribLocation(program, "v_position"); |
| 95 SetupUnitQuad(position_loc); |
| 96 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 97 |
| 98 GLenum internalFormats[5] = {GL_RGBA32F_EXT, GL_RGB32F_EXT, GL_RGBA4, |
| 99 GL_RGB5_A1, GL_RGB565}; |
| 100 GLint componentType[5] = {GL_FLOAT, GL_FLOAT, GL_UNSIGNED_NORMALIZED_EXT, |
| 101 GL_UNSIGNED_NORMALIZED_EXT, |
| 102 GL_UNSIGNED_NORMALIZED_EXT}; |
| 103 for (int i = 0; i < 5; i++) { |
| 104 GLuint renderbuffer_id; |
| 105 glGenRenderbuffers(1, &renderbuffer_id); |
| 106 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer_id); |
| 107 glRenderbufferStorage(GL_RENDERBUFFER, internalFormats[i], 4, 4); |
| 108 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 109 |
| 110 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 111 GL_RENDERBUFFER, renderbuffer_id); |
| 112 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 113 glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 114 |
| 115 // Query and check GL_FRAMEBUFFER_ATTACHMENT Parameters |
| 116 GLint params = 0; |
| 117 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 118 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, ¶ms); |
| 119 EXPECT_EQ(GL_RENDERBUFFER, params); |
| 120 params = 0; |
| 121 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 122 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, ¶ms); |
| 123 EXPECT_EQ(renderbuffer_id, static_cast<GLuint>(params)); |
| 124 params = 0; |
| 125 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 126 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT, |
| 127 ¶ms); |
| 128 EXPECT_EQ(componentType[i], params); |
| 129 |
| 130 float pixels[1 * 4] = {0.25, 0.5, 2.0, 4.0}; |
| 131 glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, |
| 133 0, GL_RGBA, GL_FLOAT, pixels); |
| 134 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 135 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 136 |
| 137 // Check glReadPixels |
| 138 if (internalFormats[i] != GL_RGBA32F_EXT |
| 139 && internalFormats[i] != GL_RGB32F_EXT) |
| 140 return; |
| 141 |
| 142 // Implementation-chosen format |
| 143 GLint attachmentType = 0; |
| 144 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &attachmentType); |
| 145 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 146 GLint attachmentFormat = 0; |
| 147 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &attachmentFormat); |
| 148 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 149 if (attachmentType == GL_FLOAT) { |
| 150 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 151 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_FLOAT, actual_pixels); |
| 152 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 153 for (unsigned int i = 0; i < 4; i++) |
| 154 EXPECT_EQ(1, actual_pixels[i]); |
| 155 } |
| 156 if (attachmentType == GL_HALF_FLOAT_OES) { |
| 157 uint16 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 158 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_HALF_FLOAT_OES, |
| 159 actual_pixels); |
| 160 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 161 for (unsigned int i = 0; i < 4; i++) |
| 162 EXPECT_EQ(15360, actual_pixels[i]); |
| 163 } |
| 164 if (attachmentType == GL_UNSIGNED_BYTE) { |
| 165 uint8 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 166 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_UNSIGNED_BYTE, |
| 167 actual_pixels); |
| 168 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 169 for (unsigned int i = 0; i < 4; i++) |
| 170 EXPECT_EQ(255, actual_pixels[i]); |
| 171 } |
| 172 |
| 173 // RGBA/FLOAT |
| 174 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 175 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, actual_pixels); |
| 176 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 177 for (unsigned int i = 0; i < 4; i++) |
| 178 EXPECT_EQ(1, actual_pixels[i]); |
| 179 |
| 180 glDeleteRenderbuffers(1, &renderbuffer_id); |
| 181 } |
| 182 } |
| 183 |
| 184 TEST_F(GLANGLEColorBufferFloatTest, Textures) { |
| 185 if (!GLTestHelper::HasExtension("GL_ANGLE_color_buffer_float")) { |
| 186 return; |
| 187 } |
| 188 |
| 189 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); |
| 190 glUseProgram(program); |
| 191 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 192 |
| 193 GLint position_loc = glGetAttribLocation(program, "v_position"); |
| 194 SetupUnitQuad(position_loc); |
| 195 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 196 |
| 197 GLenum formats[2] = {GL_RGBA, GL_RGB}; |
| 198 for (int i = 0; i < 2; i++) |
| 199 { |
| 200 glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 201 glTexImage2D(GL_TEXTURE_2D, 0, formats[i], 4, 4, 0, formats[i], |
| 202 GL_FLOAT, NULL); |
| 203 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 204 GL_TEXTURE_2D, textures[1], 0); |
| 205 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 206 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 207 glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 208 |
| 209 //query GL_FRAMEBUFFER_ATTACHMENT Parameters |
| 210 GLint params = 0; |
| 211 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 212 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, ¶ms); |
| 213 EXPECT_EQ(GL_TEXTURE, params); |
| 214 params = 0; |
| 215 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 216 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, ¶ms); |
| 217 EXPECT_EQ(textures[1], static_cast<GLuint>(params)); |
| 218 params = 0; |
| 219 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 220 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT, |
| 221 ¶ms); |
| 222 EXPECT_EQ(GL_FLOAT, params); |
| 223 |
| 224 float pixels[1 * 4] = {0.25, 0.5, 2.0, 4.0}; |
| 225 glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 226 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, |
| 227 GL_FLOAT, pixels); |
| 228 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 229 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 230 |
| 231 // Check glReadPixels |
| 232 // Implementation-chosen format |
| 233 GLint attachmentType = 0; |
| 234 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &attachmentType); |
| 235 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 236 GLint attachmentFormat = 0; |
| 237 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &attachmentFormat); |
| 238 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 239 if (attachmentType == GL_FLOAT) { |
| 240 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 241 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_FLOAT, actual_pixels); |
| 242 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 243 for (unsigned int i = 0; i < 4; i++) |
| 244 EXPECT_EQ(1, actual_pixels[i]); |
| 245 } |
| 246 if (attachmentType == GL_HALF_FLOAT_OES) { |
| 247 uint16 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 248 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_HALF_FLOAT_OES, |
| 249 actual_pixels); |
| 250 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 251 for (unsigned int i = 0; i < 4; i++) |
| 252 EXPECT_EQ(15360, actual_pixels[i]); |
| 253 } |
| 254 if (attachmentType == GL_UNSIGNED_BYTE) { |
| 255 uint8 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 256 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_UNSIGNED_BYTE, |
| 257 actual_pixels); |
| 258 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 259 for (unsigned int i = 0; i < 4; i++) |
| 260 EXPECT_EQ(255, actual_pixels[i]); |
| 261 } |
| 262 |
| 263 // RGBA/FLOAT |
| 264 float actual_pixels_[1 * 4] = {0, 0, 0, 0}; |
| 265 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, actual_pixels_); |
| 266 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 267 for (int i = 0; i < 4; i++) |
| 268 EXPECT_EQ(1, actual_pixels_[i]); |
| 269 |
| 270 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 271 GL_TEXTURE_2D, 0, 0); |
| 272 } |
| 273 } |
| 274 |
| 275 } // namespace gpu |
OLD | NEW |