1 #include "ANGLETest.h" 2 #include <array> 3 4 template <typename IndexType, GLenum IndexTypeName> 5 class IndexedPointsTest : public ANGLETest 6 { 7 protected: 8 IndexedPointsTest() 9 { 10 setWindowWidth(128); 11 setWindowHeight(128); 12 setConfigRedBits(8); 13 setConfigGreenBits(8); 14 setConfigBlueBits(8); 15 setConfigAlphaBits(8); 16 setConfigDepthBits(24); 17 } 18 19 float getIndexPositionX(size_t idx) 20 { 21 return (idx == 0 || idx == 3) ? -0.5f : 0.5f; 22 } 23 24 float getIndexPositionY(size_t idx) 25 { 26 return (idx == 2 || idx == 3) ? -0.5f : 0.5f; 27 } 28 29 virtual void SetUp() 30 { 31 ANGLETest::SetUp(); 32 33 const std::string vertexShaderSource = SHADER_SOURCE 34 ( 35 precision highp float; 36 attribute vec2 position; 37 38 void main() 39 { 40 gl_PointSize = 5.0; 41 gl_Position = vec4(position, 0.0, 1.0); 42 } 43 ); 44 45 const std::string fragmentShaderSource = SHADER_SOURCE 46 ( 47 precision highp float; 48 49 void main() 50 { 51 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 52 } 53 ); 54 55 mProgram = compileProgram(vertexShaderSource, fragmentShaderSource); 56 if (mProgram == 0) 57 { 58 FAIL() << "shader compilation failed."; 59 } 60 61 std::array<GLfloat, mPointCount * 2> vertices = 62 { 63 getIndexPositionX(0), getIndexPositionY(0), 64 getIndexPositionX(1), getIndexPositionY(1), 65 getIndexPositionX(2), getIndexPositionY(2), 66 getIndexPositionX(3), getIndexPositionY(3), 67 }; 68 glGenBuffers(1, &mVertexBuffer); 69 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); 70 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW); 71 72 std::array<IndexType, mPointCount> indices = { 0, 1, 2, 3 }; 73 glGenBuffers(1, &mIndexBuffer); 74 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer); 75 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(IndexType), indices.data(), GL_STATIC_DRAW); 76 } 77 78 virtual void TearDown() 79 { 80 glDeleteProgram(mProgram); 81 82 ANGLETest::TearDown(); 83 } 84 85 void runTest(GLuint firstIndex) 86 { 87 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 88 glClear(GL_COLOR_BUFFER_BIT); 89 90 GLint viewportSize[4]; 91 glGetIntegerv(GL_VIEWPORT, viewportSize); 92 93 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer); 94 GLint vertexLocation = glGetAttribLocation(mProgram, "position"); 95 glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); 96 glEnableVertexAttribArray(vertexLocation); 97 98 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer); 99 100 glUseProgram(mProgram); 101 102 glDrawElements(GL_POINTS, mPointCount - firstIndex, IndexTypeName, reinterpret_cast<void*>(firstIndex * sizeof(IndexType))); 103 104 for (size_t i = 0; i < mPointCount; i++) 105 { 106 GLuint x = viewportSize[0] + (getIndexPositionX(i) * 0.5f + 0.5f) * (viewportSize[2] - viewportSize[0]); 107 GLuint y = viewportSize[1] + (getIndexPositionY(i) * 0.5f + 0.5f) * (viewportSize[3] - viewportSize[1]); 108 109 if (i < firstIndex) 110 { 111 EXPECT_PIXEL_EQ(x, y, 0, 0, 0, 255); 112 } 113 else 114 { 115 EXPECT_PIXEL_EQ(x, y, 255, 0, 0, 255); 116 } 117 } 118 } 119 120 GLuint mProgram; 121 GLuint mVertexBuffer; 122 GLuint mIndexBuffer; 123 static const GLuint mPointCount = 4; 124 }; 125 126 typedef IndexedPointsTest<GLubyte, GL_UNSIGNED_BYTE> IndexedPointsTestUByte; 127 128 TEST_F(IndexedPointsTestUByte, unsigned_byte_offset_0) 129 { 130 runTest(0); 131 } 132 133 TEST_F(IndexedPointsTestUByte, unsigned_byte_offset_1) 134 { 135 runTest(1); 136 } 137 138 TEST_F(IndexedPointsTestUByte, unsigned_byte_offset_2) 139 { 140 runTest(2); 141 } 142 143 TEST_F(IndexedPointsTestUByte, unsigned_byte_offset_3) 144 { 145 runTest(3); 146 } 147 148 typedef IndexedPointsTest<GLushort, GL_UNSIGNED_SHORT> IndexedPointsTestUShort; 149 150 TEST_F(IndexedPointsTestUShort, unsigned_short_offset_0) 151 { 152 runTest(0); 153 } 154 155 TEST_F(IndexedPointsTestUShort, unsigned_short_offset_1) 156 { 157 runTest(1); 158 } 159 160 TEST_F(IndexedPointsTestUShort, unsigned_short_offset_2) 161 { 162 runTest(2); 163 } 164 165 TEST_F(IndexedPointsTestUShort, unsigned_short_offset_3) 166 { 167 runTest(3); 168 } 169 170 typedef IndexedPointsTest<GLuint, GL_UNSIGNED_INT> IndexedPointsTestUInt; 171 172 TEST_F(IndexedPointsTestUInt, unsigned_int_offset_0) 173 { 174 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint")) 175 { 176 return; 177 } 178 179 runTest(0); 180 } 181 182 TEST_F(IndexedPointsTestUInt, unsigned_int_offset_1) 183 { 184 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint")) 185 { 186 return; 187 } 188 189 runTest(1); 190 } 191 192 TEST_F(IndexedPointsTestUInt, unsigned_int_offset_2) 193 { 194 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint")) 195 { 196 return; 197 } 198 199 runTest(2); 200 } 201 202 TEST_F(IndexedPointsTestUInt, unsigned_int_offset_3) 203 { 204 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint")) 205 { 206 return; 207 } 208 209 runTest(3); 210 } 211