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.
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
17 class PointCoordTest
: public testing::Test
{
19 static const GLsizei kResolution
= 256;
22 virtual void SetUp() {
23 GLManager::Options options
;
24 options
.size
= gfx::Size(kResolution
, kResolution
);
25 gl_
.Initialize(options
);
28 virtual void TearDown() {
32 GLuint
SetupQuad(GLint position_location
, GLfloat pixel_offset
);
37 GLuint
PointCoordTest::SetupQuad(
38 GLint position_location
, GLfloat pixel_offset
) {
40 glGenBuffers(1, &vbo
);
41 glBindBuffer(GL_ARRAY_BUFFER
, vbo
);
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
,
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);
62 GLfloat
s2p(GLfloat s
) {
63 return (s
+ 1.0) * 0.5 * PointCoordTest::kResolution
;
66 } // anonymous namespace
69 // Flaky on Linux ATI bot.
70 #if (defined(OS_LINUX) && defined(NDEBUG))
71 #define MAYBE_RenderTo DISABLED_RenderTo
73 #define MAYBE_RenderTo RenderTo
76 TEST_F(PointCoordTest
, MAYBE_RenderTo
) {
77 static const char* v_shader_str
= SHADER(
78 attribute vec4 a_position
;
79 uniform
float u_pointsize
;
82 gl_PointSize
= u_pointsize
;
83 gl_Position
= a_position
;
86 static const char* f_shader_str
= SHADER(
87 precision mediump
float;
98 GLuint program
= GLTestHelper::LoadProgram(v_shader_str
, f_shader_str
);
99 glUseProgram(program
);
101 GLint position_loc
= glGetAttribLocation(program
, "a_position");
102 GLint pointsize_loc
= glGetUniformLocation(program
, "u_pointsize");
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);
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
);
114 glUniform1f(pointsize_loc
, max_point_size
);
116 GLfloat pixel_offset
= (max_point_size
% 2) ? (1.0f
/ kResolution
) : 0;
118 SetupQuad(position_loc
, pixel_offset
);
120 glClear(GL_COLOR_BUFFER_BIT
);
121 glDrawArrays(GL_POINTS
, 0, 4);
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
;
139 static_cast<uint8
>(s
* 255),
140 static_cast<uint8
>((1 - t
) * 255),
144 GLTestHelper::CheckPixels(xf
, yf
, 1, 1, 4, color
);
150 GLTestHelper::CheckGLError("no errors", __LINE__
);