gl_pointcoord_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <stdint.h>
  7. #include "build/build_config.h"
  8. #include "gpu/command_buffer/tests/gl_manager.h"
  9. #include "gpu/command_buffer/tests/gl_test_utils.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #define SHADER(Src) #Src
  13. namespace gpu {
  14. class PointCoordTest : public testing::Test {
  15. public:
  16. static const GLsizei kResolution = 256;
  17. protected:
  18. void SetUp() override {
  19. GLManager::Options options;
  20. options.size = gfx::Size(kResolution, kResolution);
  21. gl_.Initialize(options);
  22. }
  23. void TearDown() override { gl_.Destroy(); }
  24. GLuint SetupQuad(GLint position_location, GLfloat pixel_offset);
  25. GLManager gl_;
  26. };
  27. GLuint PointCoordTest::SetupQuad(
  28. GLint position_location, GLfloat pixel_offset) {
  29. GLuint vbo = 0;
  30. glGenBuffers(1, &vbo);
  31. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  32. float vertices[] = {
  33. -0.5f + pixel_offset, -0.5f + pixel_offset,
  34. 0.5f + pixel_offset, -0.5f + pixel_offset,
  35. -0.5f + pixel_offset, 0.5f + pixel_offset,
  36. 0.5f + pixel_offset, 0.5f + pixel_offset,
  37. };
  38. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  39. glEnableVertexAttribArray(position_location);
  40. glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
  41. return vbo;
  42. }
  43. namespace {
  44. struct FormatType {
  45. GLenum format;
  46. GLenum type;
  47. };
  48. GLfloat s2p(GLfloat s) {
  49. return (s + 1.0) * 0.5 * PointCoordTest::kResolution;
  50. }
  51. } // anonymous namespace
  52. // crbug.com/162976
  53. // Flaky on Linux ATI bot.
  54. #if ((BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(NDEBUG))
  55. #define MAYBE_RenderTo DISABLED_RenderTo
  56. #else
  57. #define MAYBE_RenderTo RenderTo
  58. #endif
  59. TEST_F(PointCoordTest, MAYBE_RenderTo) {
  60. static const char* v_shader_str = SHADER(
  61. attribute vec4 a_position;
  62. uniform float u_pointsize;
  63. void main()
  64. {
  65. gl_PointSize = u_pointsize;
  66. gl_Position = a_position;
  67. }
  68. );
  69. static const char* f_shader_str = SHADER(
  70. precision mediump float;
  71. void main()
  72. {
  73. gl_FragColor = vec4(
  74. gl_PointCoord.x,
  75. gl_PointCoord.y,
  76. 0,
  77. 1);
  78. }
  79. );
  80. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  81. glUseProgram(program);
  82. GLint position_loc = glGetAttribLocation(program, "a_position");
  83. GLint pointsize_loc = glGetUniformLocation(program, "u_pointsize");
  84. GLint range[2] = { 0, 0 };
  85. glGetIntegerv(GL_ALIASED_POINT_SIZE_RANGE, &range[0]);
  86. GLint max_point_size = range[1];
  87. EXPECT_GE(max_point_size, 1);
  88. max_point_size = std::min(max_point_size, 64);
  89. GLint point_width = max_point_size / kResolution;
  90. GLint point_step = max_point_size / 4;
  91. point_step = std::max(1, point_step);
  92. glUniform1f(pointsize_loc, max_point_size);
  93. GLfloat pixel_offset = (max_point_size % 2) ? (1.0f / kResolution) : 0;
  94. SetupQuad(position_loc, pixel_offset);
  95. glClear(GL_COLOR_BUFFER_BIT);
  96. glDrawArrays(GL_POINTS, 0, 4);
  97. for (GLint py = 0; py < 2; ++py) {
  98. for (GLint px = 0; px < 2; ++px) {
  99. GLfloat point_x = -0.5 + px + pixel_offset;
  100. GLfloat point_y = -0.5 + py + pixel_offset;
  101. for (GLint yy = 0; yy < max_point_size; yy += point_step) {
  102. for (GLint xx = 0; xx < max_point_size; xx += point_step) {
  103. // formula for s and t from OpenGL ES 2.0 spec section 3.3
  104. GLfloat xw = s2p(point_x);
  105. GLfloat yw = s2p(point_y);
  106. GLfloat u = xx / max_point_size * 2 - 1;
  107. GLfloat v = yy / max_point_size * 2 - 1;
  108. GLint xf = s2p(point_x + u * point_width);
  109. GLint yf = s2p(point_y + v * point_width);
  110. GLfloat s = 0.5 + (xf + 0.5 - xw) / max_point_size;
  111. GLfloat t = 0.5 + (yf + 0.5 - yw) / max_point_size;
  112. uint8_t color[4] = {
  113. static_cast<uint8_t>(s * 255),
  114. static_cast<uint8_t>((1 - t) * 255), 0, 255,
  115. };
  116. GLTestHelper::CheckPixels(xf, yf, 1, 1, 4, color, nullptr);
  117. }
  118. }
  119. }
  120. }
  121. GLTestHelper::CheckGLError("no errors", __LINE__);
  122. }
  123. } // namespace gpu