gl_cube_map_texture_unittest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 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. #include <GLES2/gl2.h>
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "gpu/command_buffer/tests/gl_manager.h"
  8. #include "gpu/command_buffer/tests/gl_test_utils.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace gpu {
  11. namespace {
  12. const GLenum kCubeMapTextureTargets[] = {
  13. GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  14. GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  15. GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  16. };
  17. } // namespace
  18. // A collection of tests that exercise the cube map texture.
  19. class GLCubeMapTextureTest : public testing::TestWithParam<GLenum> {
  20. protected:
  21. void SetUp() override {
  22. // ANGLE and NVidia fails ReadPixelsFromIncompleteCubeTexture without this
  23. // workaround.
  24. GpuDriverBugWorkarounds workarounds;
  25. workarounds.force_cube_complete = true;
  26. gl_.InitializeWithWorkarounds(GLManager::Options(), workarounds);
  27. DCHECK(gl_.workarounds().force_cube_complete);
  28. for (int i = 0; i < 256; i++) {
  29. pixels_[i * 4] = 255u;
  30. pixels_[(i * 4) + 1] = 0;
  31. pixels_[(i * 4) + 2] = 0;
  32. pixels_[(i * 4) + 3] = 255u;
  33. }
  34. glGenTextures(1, &texture_);
  35. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_);
  36. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  37. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  38. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  39. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  40. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  41. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  42. glGenFramebuffers(1, &framebuffer_id_);
  43. }
  44. void TearDown() override {
  45. glDeleteTextures(1, &texture_);
  46. glDeleteFramebuffers(1, &framebuffer_id_);
  47. gl_.Destroy();
  48. }
  49. GLManager gl_;
  50. uint8_t pixels_[256 * 4];
  51. const int width_ = 16;
  52. GLuint texture_;
  53. GLuint framebuffer_id_;
  54. };
  55. INSTANTIATE_TEST_SUITE_P(GLCubeMapTextureTests,
  56. GLCubeMapTextureTest,
  57. ::testing::ValuesIn(kCubeMapTextureTargets));
  58. TEST_P(GLCubeMapTextureTest, TexImage2DAfterFBOBinding) {
  59. GLenum cube_map_target = GetParam();
  60. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
  61. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, cube_map_target,
  62. texture_, 0);
  63. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  64. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_);
  65. // force_cube_map_positive_x_allocation workaround prevents Nexus 5 crash.
  66. // TODO(dshwang): remove the workaround when it's fixed. crbug.com/518889
  67. glTexImage2D(cube_map_target, 0, GL_RGBA, width_, width_, 0, GL_RGBA,
  68. GL_UNSIGNED_BYTE, pixels_);
  69. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  70. }
  71. TEST_P(GLCubeMapTextureTest, ReadPixels) {
  72. GLenum cube_map_target = GetParam();
  73. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_);
  74. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  75. // Make a cube texture complete
  76. for (unsigned i = 0; i < std::size(kCubeMapTextureTargets); i++) {
  77. glTexImage2D(kCubeMapTextureTargets[i], 0, GL_RGBA, width_, width_, 0,
  78. GL_RGBA, GL_UNSIGNED_BYTE, pixels_);
  79. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  80. }
  81. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
  82. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, cube_map_target,
  83. texture_, 0);
  84. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  85. // Check that FB is complete.
  86. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  87. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  88. GLTestHelper::CheckPixels(0, 0, width_, width_, 0, pixels_, nullptr);
  89. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  90. }
  91. TEST_P(GLCubeMapTextureTest, ReadPixelsFromIncompleteCubeTexture) {
  92. GLenum cube_map_target = GetParam();
  93. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_);
  94. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  95. glTexImage2D(cube_map_target, 0, GL_RGBA, width_, width_, 0, GL_RGBA,
  96. GL_UNSIGNED_BYTE, pixels_);
  97. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  98. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
  99. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, cube_map_target,
  100. texture_, 0);
  101. // force_cube_map_positive_x_allocation workaround prevents Nexus 5 crash.
  102. // TODO(dshwang): remove the workaround when it's fixed. crbug.com/518889
  103. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  104. // Check that FB is not complete.
  105. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT),
  106. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  107. GLsizei size = width_ * width_ * 4;
  108. std::unique_ptr<uint8_t[]> pixels(new uint8_t[size]);
  109. glReadPixels(0, 0, width_, width_, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get());
  110. EXPECT_EQ(static_cast<GLenum>(GL_INVALID_FRAMEBUFFER_OPERATION),
  111. glGetError());
  112. }
  113. } // namespace gpu