gl_oob_attrib_unittest.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017 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 <stdint.h>
  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. class GLOOBAttribTest : public testing::Test {
  13. protected:
  14. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  15. void TearDown() override { gl_.Destroy(); }
  16. GLManager gl_;
  17. };
  18. // Tests that enabling a vertex array for a location that matches any column of
  19. // a matrix attribute correctly triggers out-of-bounds checks.
  20. TEST_F(GLOOBAttribTest, DrawUsingOOBMatrixAttrib) {
  21. // The passthrough command decoder uses robust buffer access behaviour. This
  22. // makes OOB error checks unimportant because OOB accesses will not cause
  23. // errors. OOB accesses will also return implementation-dependent values.
  24. // See the KHR_robust_buffer_access_behavior spec for more information.
  25. if (gl_.gpu_preferences().use_passthrough_cmd_decoder) {
  26. std::cout << "Test skipped, KHR_robust_buffer_access_behavior enabled.\n";
  27. return;
  28. }
  29. const char kVertexShader[] =
  30. "attribute mat3 attrib;\n"
  31. "varying vec4 color;\n"
  32. "void main () {\n"
  33. " color = vec4(1.0,\n"
  34. " attrib[0][0] + attrib[0][1] + attrib[0][2] +\n"
  35. " attrib[1][0] + attrib[1][1] + attrib[1][2] +\n"
  36. " attrib[2][0] + attrib[2][1] + attrib[2][2],\n"
  37. " 1.0,\n"
  38. " 1.0);\n"
  39. "}\n";
  40. const char kFragmentShader[] =
  41. "precision mediump float;\n"
  42. "varying vec4 color;\n"
  43. "void main() {\n"
  44. " gl_FragColor = color;\n"
  45. "}\n";
  46. GLuint program = GLTestHelper::LoadProgram(kVertexShader, kFragmentShader);
  47. DCHECK(program);
  48. glUseProgram(program);
  49. GLuint vbo;
  50. glGenBuffers(1, &vbo);
  51. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  52. glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
  53. GLint location = glGetAttribLocation(program, "attrib");
  54. EXPECT_GE(0, location);
  55. // All attribs disabled - no error.
  56. glDrawArrays(GL_TRIANGLES, 0, 1000);
  57. GLenum expected = GL_NO_ERROR;
  58. EXPECT_EQ(expected, glGetError());
  59. for (int i = 0; i < 3; ++i) {
  60. // Enable any of the valid locations for the attribute, should raise an
  61. // error if trying to access attributes out-of-bounds.
  62. glVertexAttribPointer(location + i, 4, GL_UNSIGNED_BYTE, false, 0, nullptr);
  63. glEnableVertexAttribArray(location + i);
  64. glDrawArrays(GL_TRIANGLES, 0, 1000);
  65. expected = GL_INVALID_OPERATION;
  66. EXPECT_EQ(expected, glGetError());
  67. // But in-bounds should pass.
  68. glDrawArrays(GL_TRIANGLES, 0, 3);
  69. expected = GL_NO_ERROR;
  70. EXPECT_EQ(expected, glGetError());
  71. glDisableVertexAttribArray(location + i);
  72. }
  73. // Enable an unused location, should not trigger out-of-bounds checks.
  74. glVertexAttribPointer(location + 3, 4, GL_UNSIGNED_BYTE, false, 0, nullptr);
  75. glEnableVertexAttribArray(location + 3);
  76. glDrawArrays(GL_TRIANGLES, 0, 1000);
  77. expected = GL_NO_ERROR;
  78. EXPECT_EQ(expected, glGetError());
  79. }
  80. } // anonymous namespace
  81. } // namespace gpu