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