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 GLExtColorBufferHalfFloatTest : 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 GLExtColorBufferHalfFloatTest::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(GLExtColorBufferHalfFloatTest, RenderBuffers) { |
| 86 if (!GLTestHelper::HasExtension("EXT_color_buffer_half_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_RGBA16F_EXT, GL_RGB16F_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 // (0.25, 0.5, 2.0, 4.0) / (0x3400, 0x3800, 0x4000, 0x4400) |
| 131 int16 pixels[1 * 4] = {0x3400, 0x3800, 0x4000, 0x4400}; |
| 132 glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, |
| 134 0, GL_RGBA, GL_HALF_FLOAT_OES, pixels); |
| 135 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 136 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 137 |
| 138 // Check glReadPixels |
| 139 if (internalFormats[i] != GL_RGBA16F_EXT |
| 140 && internalFormats[i] != GL_RGB16F_EXT) |
| 141 return; |
| 142 |
| 143 // Implementation-chosen format |
| 144 GLint attachmentType = 0; |
| 145 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &attachmentType); |
| 146 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 147 GLint attachmentFormat = 0; |
| 148 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &attachmentFormat); |
| 149 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 150 if (attachmentType == GL_FLOAT) { |
| 151 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 152 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_FLOAT, actual_pixels); |
| 153 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 154 for (unsigned int i = 0; i < 4; i++) |
| 155 EXPECT_EQ(1, actual_pixels[i]); |
| 156 } |
| 157 if (attachmentType == GL_HALF_FLOAT_OES) { |
| 158 uint16 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 159 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_HALF_FLOAT_OES, |
| 160 actual_pixels); |
| 161 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 162 for (unsigned int i = 0; i < 4; i++) |
| 163 EXPECT_EQ(15360, actual_pixels[i]); |
| 164 } |
| 165 if (attachmentType == GL_UNSIGNED_BYTE) { |
| 166 uint8 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 167 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_UNSIGNED_BYTE, |
| 168 actual_pixels); |
| 169 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 170 for (unsigned int i = 0; i < 4; i++) |
| 171 EXPECT_EQ(255, actual_pixels[i]); |
| 172 } |
| 173 |
| 174 // RGBA/FLOAT |
| 175 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 176 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, actual_pixels); |
| 177 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 178 for (unsigned int i = 0; i < 4; i++) |
| 179 EXPECT_EQ(1, actual_pixels[i]); |
| 180 |
| 181 glDeleteRenderbuffers(1, &renderbuffer_id); |
| 182 } |
| 183 } |
| 184 |
| 185 TEST_F(GLExtColorBufferHalfFloatTest, Textures) { |
| 186 if (!GLTestHelper::HasExtension("EXT_color_buffer_half_float")) { |
| 187 return; |
| 188 } |
| 189 |
| 190 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str); |
| 191 glUseProgram(program); |
| 192 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 193 |
| 194 GLint position_loc = glGetAttribLocation(program, "v_position"); |
| 195 SetupUnitQuad(position_loc); |
| 196 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 197 |
| 198 GLenum formats[2] = {GL_RGBA, GL_RGB}; |
| 199 for (int i = 0; i < 2; i++) |
| 200 { |
| 201 glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 202 glTexImage2D(GL_TEXTURE_2D, 0, formats[i], 4, 4, 0, formats[i], |
| 203 GL_HALF_FLOAT_OES, NULL); |
| 204 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 205 GL_TEXTURE_2D, textures[1], 0); |
| 206 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 207 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 208 glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 209 |
| 210 //Query GL_FRAMEBUFFER_ATTACHMENT Parameters |
| 211 GLint params = 0; |
| 212 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 213 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, ¶ms); |
| 214 EXPECT_EQ(GL_TEXTURE, params); |
| 215 params = 0; |
| 216 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 217 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, ¶ms); |
| 218 EXPECT_EQ(textures[1], static_cast<GLuint>(params)); |
| 219 params = 0; |
| 220 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, |
| 221 GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT, |
| 222 ¶ms); |
| 223 EXPECT_EQ(GL_FLOAT, params); |
| 224 |
| 225 // (0.25, 0.5, 0.5, 4.0) / (0x3400, 0x3800, 0x3800, 0x4400) |
| 226 int16 pixels[1 * 4] = { 0x3400, 0x3800, 0x4000, 0x4400 }; |
| 227 glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 228 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, |
| 229 GL_HALF_FLOAT_OES, pixels); |
| 230 glDrawArrays(GL_TRIANGLES, 0, 6); |
| 231 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 232 |
| 233 // Check glReadPixels |
| 234 // Implementation-chosen format |
| 235 GLint attachmentType = 0; |
| 236 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &attachmentType); |
| 237 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 238 GLint attachmentFormat = 0; |
| 239 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &attachmentFormat); |
| 240 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 241 if (attachmentType == GL_FLOAT) { |
| 242 float actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 243 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_FLOAT, actual_pixels); |
| 244 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 245 for (unsigned int i = 0; i < 4; i++) |
| 246 EXPECT_EQ(1, actual_pixels[i]); |
| 247 } |
| 248 if (attachmentType == GL_HALF_FLOAT_OES) { |
| 249 uint16 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 250 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_HALF_FLOAT_OES, |
| 251 actual_pixels); |
| 252 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 253 for (unsigned int i = 0; i < 4; i++) |
| 254 EXPECT_EQ(15360, actual_pixels[i]); |
| 255 } |
| 256 if (attachmentType == GL_UNSIGNED_BYTE) { |
| 257 uint8 actual_pixels[1 * 4] = {0, 0, 0, 0}; |
| 258 glReadPixels(0, 0, 1, 1, attachmentFormat, GL_UNSIGNED_BYTE, |
| 259 actual_pixels); |
| 260 EXPECT_TRUE(GL_NO_ERROR == glGetError()); |
| 261 for (unsigned int i = 0; i < 4; i++) |
| 262 EXPECT_EQ(255, actual_pixels[i]); |
| 263 } |
| 264 |
| 265 //RGBA/FLOAT |
| 266 float actual_pixels_[1 * 4] = {0, 0, 0, 0}; |
| 267 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, actual_pixels_); |
| 268 EXPECT_TRUE(glGetError() == GL_NO_ERROR); |
| 269 for (int i = 0; i < 4; i++) |
| 270 EXPECT_EQ(1, actual_pixels_[i]); |
| 271 |
| 272 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 273 GL_TEXTURE_2D, 0, 0); |
| 274 } |
| 275 } |
| 276 |
| 277 } // namespace gpu |
OLD | NEW |