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