gl_unallocated_texture_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 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 <GLES2/gl2ext.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "gpu/GLES2/gl2extchromium.h"
  9. #include "gpu/command_buffer/client/gles2_implementation.h"
  10. #include "gpu/command_buffer/client/gles2_lib.h"
  11. #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
  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. #include "ui/gl/gl_share_group.h"
  17. namespace gpu {
  18. class GLUnallocatedTextureTest : public testing::Test {
  19. protected:
  20. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  21. void TearDown() override { gl_.Destroy(); }
  22. GLuint MakeProgram(const char* fragment_shader) {
  23. constexpr const char kVertexShader[] =
  24. "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
  25. GLuint program = GLTestHelper::LoadProgram(kVertexShader, fragment_shader);
  26. if (!program)
  27. return 0;
  28. glUseProgram(program);
  29. GLint location_sampler = glGetUniformLocation(program, "sampler");
  30. glUniform1i(location_sampler, 0);
  31. return program;
  32. }
  33. // Creates a texture on target, setting up filters but without setting any
  34. // level image.
  35. GLuint MakeUninitializedTexture(GLenum target) {
  36. GLuint texture = 0;
  37. glGenTextures(1, &texture);
  38. glBindTexture(target, texture);
  39. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  40. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  41. glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  42. glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  43. return texture;
  44. }
  45. GLManager gl_;
  46. };
  47. // Test that we can render with GL_TEXTURE_2D textures that are unallocated.
  48. // This should not generate errors or assert.
  49. TEST_F(GLUnallocatedTextureTest, RenderUnallocatedTexture2D) {
  50. constexpr const char kFragmentShader[] =
  51. "uniform sampler2D sampler;\n"
  52. "void main() { gl_FragColor = texture2D(sampler, vec2(0.0, 0.0)); }\n";
  53. GLuint program = MakeProgram(kFragmentShader);
  54. ASSERT_TRUE(program);
  55. GLuint texture = MakeUninitializedTexture(GL_TEXTURE_2D);
  56. glDrawArrays(GL_TRIANGLES, 0, 3);
  57. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  58. glDeleteTextures(1, &texture);
  59. glDeleteProgram(program);
  60. }
  61. // Test that we can render with GL_TEXTURE_EXTERNAL_OES textures that are
  62. // unallocated. This should not generate errors or assert.
  63. TEST_F(GLUnallocatedTextureTest, RenderUnallocatedTextureExternal) {
  64. if (!gl_.GetCapabilities().egl_image_external) {
  65. LOG(INFO) << "GL_OES_EGL_image_external not supported, skipping test";
  66. return;
  67. }
  68. constexpr const char kFragmentShader[] =
  69. "#extension GL_OES_EGL_image_external : enable\n"
  70. "uniform samplerExternalOES sampler;\n"
  71. "void main() { gl_FragColor = texture2D(sampler, vec2(0.0, 0.0)); }\n";
  72. GLuint program = MakeProgram(kFragmentShader);
  73. ASSERT_TRUE(program);
  74. GLuint texture = MakeUninitializedTexture(GL_TEXTURE_EXTERNAL_OES);
  75. glDrawArrays(GL_TRIANGLES, 0, 3);
  76. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  77. glDeleteTextures(1, &texture);
  78. glDeleteProgram(program);
  79. }
  80. // Test that we can render with GL_TEXTURE_RECTANGLE_ARB textures that are
  81. // unallocated. This should not generate errors or assert.
  82. TEST_F(GLUnallocatedTextureTest, RenderUnallocatedTextureRectange) {
  83. if (!gl_.GetCapabilities().texture_rectangle) {
  84. LOG(INFO) << "GL_ARB_texture_rectangle not supported, skipping test";
  85. return;
  86. }
  87. constexpr const char kFragmentShader[] =
  88. "#extension GL_ARB_texture_rectangle : enable\n"
  89. "uniform sampler2DRect sampler;\n"
  90. "void main() {\n"
  91. " gl_FragColor = texture2DRect(sampler, vec2(0.0, 0.0));\n"
  92. "}\n";
  93. GLuint program = MakeProgram(kFragmentShader);
  94. ASSERT_TRUE(program);
  95. GLuint texture = MakeUninitializedTexture(GL_TEXTURE_RECTANGLE_ARB);
  96. glDrawArrays(GL_TRIANGLES, 0, 3);
  97. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  98. glDeleteTextures(1, &texture);
  99. glDeleteProgram(program);
  100. }
  101. } // namespace gpu