blob: af59eb5b4e1b1328224243b7d0cf30f2a2cc74a0 [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Jamie Madillfa05f602015-05-07 13:47:11 -04008
Jamie Madillfa05f602015-05-07 13:47:11 -04009using namespace angle;
10
11template <typename IndexType, GLenum IndexTypeName>
Geoff Langf8c2f5c2013-12-05 13:52:33 -050012class IndexedPointsTest : public ANGLETest
13{
Jamie Madillfa05f602015-05-07 13:47:11 -040014 protected:
15 IndexedPointsTest()
Geoff Langf8c2f5c2013-12-05 13:52:33 -050016 {
17 setWindowWidth(128);
18 setWindowHeight(128);
19 setConfigRedBits(8);
20 setConfigGreenBits(8);
21 setConfigBlueBits(8);
22 setConfigAlphaBits(8);
23 setConfigDepthBits(24);
24 }
25
26 float getIndexPositionX(size_t idx)
27 {
28 return (idx == 0 || idx == 3) ? -0.5f : 0.5f;
29 }
30
31 float getIndexPositionY(size_t idx)
32 {
33 return (idx == 2 || idx == 3) ? -0.5f : 0.5f;
34 }
35
36 virtual void SetUp()
37 {
38 ANGLETest::SetUp();
39
40 const std::string vertexShaderSource = SHADER_SOURCE
41 (
42 precision highp float;
43 attribute vec2 position;
44
45 void main()
46 {
47 gl_PointSize = 5.0;
48 gl_Position = vec4(position, 0.0, 1.0);
49 }
50 );
51
52 const std::string fragmentShaderSource = SHADER_SOURCE
53 (
54 precision highp float;
55
56 void main()
57 {
58 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
59 }
60 );
61
Jamie Madill5599c8f2014-08-26 13:16:39 -040062 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
Cooper Partin558f2b52015-06-02 09:34:11 -070063 ASSERT_NE(0u, mProgram);
Geoff Langf8c2f5c2013-12-05 13:52:33 -050064
Cooper Partin558f2b52015-06-02 09:34:11 -070065 const std::string vertexShaderSource2 = SHADER_SOURCE
66 (
67 precision highp float;
68 attribute vec2 position;
69 attribute vec4 color;
70 varying vec4 vcolor;
71
72 void main()
73 {
74 gl_PointSize = 5.0;
75 gl_Position = vec4(position, 0.0, 1.0);
76 vcolor = color;
77 }
78 );
79
80 const std::string fragmentShaderSource2 = SHADER_SOURCE
81 (
82 precision highp float;
83 varying vec4 vcolor;
84 void main()
85 {
86 gl_FragColor = vec4(vcolor.xyz, 1.0);
87 }
88 );
89
90 mVertexWithColorBufferProgram = CompileProgram(vertexShaderSource2, fragmentShaderSource2);
91 ASSERT_NE(0u, mVertexWithColorBufferProgram);
92
93 // Construct a vertex buffer of position values and color values
94 // contained in a single structure
95 const float verticesWithColor[] =
96 {
97 getIndexPositionX(0), getIndexPositionY(0), 0.0f, 1.0f, 0.0f,
98 getIndexPositionX(2), getIndexPositionY(2), 0.0f, 1.0f, 0.0f,
99 getIndexPositionX(1), getIndexPositionY(1), 0.0f, 1.0f, 0.0f,
100 getIndexPositionX(3), getIndexPositionY(3), 0.0f, 1.0f, 0.0f,
101 };
102
103 glGenBuffers(1, &mVertexWithColorBuffer);
104 glBindBuffer(GL_ARRAY_BUFFER, mVertexWithColorBuffer);
105 glBufferData(GL_ARRAY_BUFFER, sizeof(verticesWithColor), &verticesWithColor[0], GL_STATIC_DRAW);
106
107 // Construct a vertex buffer of position values only
Corentin Wallez91c2fad2015-05-15 10:34:14 -0400108 const GLfloat vertices[] =
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500109 {
110 getIndexPositionX(0), getIndexPositionY(0),
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500111 getIndexPositionX(2), getIndexPositionY(2),
Cooper Partin558f2b52015-06-02 09:34:11 -0700112 getIndexPositionX(1), getIndexPositionY(1),
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500113 getIndexPositionX(3), getIndexPositionY(3),
114 };
115 glGenBuffers(1, &mVertexBuffer);
116 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
Corentin Wallez91c2fad2015-05-15 10:34:14 -0400117 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500118
Cooper Partin558f2b52015-06-02 09:34:11 -0700119 // The indices buffer is shared between both variations of tests
120 const IndexType indices[] = { 0, 2, 1, 3 };
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500121 glGenBuffers(1, &mIndexBuffer);
122 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
Corentin Wallez91c2fad2015-05-15 10:34:14 -0400123 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500124 }
125
126 virtual void TearDown()
127 {
Cooper Partin558f2b52015-06-02 09:34:11 -0700128 glDeleteBuffers(1, &mVertexBuffer);
129 glDeleteBuffers(1, &mIndexBuffer);
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500130 glDeleteProgram(mProgram);
131
Cooper Partin558f2b52015-06-02 09:34:11 -0700132 glDeleteBuffers(1, &mVertexWithColorBuffer);
133 glDeleteProgram(mVertexWithColorBufferProgram);
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500134 ANGLETest::TearDown();
135 }
136
Cooper Partin558f2b52015-06-02 09:34:11 -0700137 void runTest(GLuint firstIndex, bool useVertexBufferWithColor = false)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500138 {
139 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
140 glClear(GL_COLOR_BUFFER_BIT);
141
142 GLint viewportSize[4];
143 glGetIntegerv(GL_VIEWPORT, viewportSize);
144
Cooper Partin558f2b52015-06-02 09:34:11 -0700145 // Choose appropriate program to apply for the test
146 GLuint program = useVertexBufferWithColor ? mVertexWithColorBufferProgram : mProgram;
147
148 if (useVertexBufferWithColor)
149 {
150 glBindBuffer(GL_ARRAY_BUFFER, mVertexWithColorBuffer);
151 GLint vertexLocation = glGetAttribLocation(program, "position");
152 glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, VertexWithColorSize, 0);
153 glEnableVertexAttribArray(vertexLocation);
154
155 GLint vertexColorLocation = glGetAttribLocation(program, "color");
156 glVertexAttribPointer(vertexColorLocation, 3, GL_FLOAT, GL_FALSE, VertexWithColorSize, (GLvoid*)((sizeof(float) * 2)));
157 glEnableVertexAttribArray(vertexColorLocation);
158 }
159 else
160 {
161 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
162 GLint vertexLocation = glGetAttribLocation(program, "position");
163 glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
164 glEnableVertexAttribArray(vertexLocation);
165 }
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500166
167 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
Cooper Partin558f2b52015-06-02 09:34:11 -0700168 glUseProgram(program);
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500169
170 glDrawElements(GL_POINTS, mPointCount - firstIndex, IndexTypeName, reinterpret_cast<void*>(firstIndex * sizeof(IndexType)));
171
172 for (size_t i = 0; i < mPointCount; i++)
173 {
Minmin Gong794e0002015-04-07 18:31:54 -0700174 GLuint x = static_cast<GLuint>(viewportSize[0] + (getIndexPositionX(i) * 0.5f + 0.5f) * (viewportSize[2] - viewportSize[0]));
175 GLuint y = static_cast<GLuint>(viewportSize[1] + (getIndexPositionY(i) * 0.5f + 0.5f) * (viewportSize[3] - viewportSize[1]));
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500176
177 if (i < firstIndex)
178 {
179 EXPECT_PIXEL_EQ(x, y, 0, 0, 0, 255);
180 }
181 else
182 {
Cooper Partin558f2b52015-06-02 09:34:11 -0700183 if (useVertexBufferWithColor)
184 {
185 // Pixel data is assumed to be GREEN
186 EXPECT_PIXEL_EQ(x, y, 0, 255, 0, 255);
187 }
188 else
189 {
190 // Pixel data is assumed to be RED
191 EXPECT_PIXEL_EQ(x, y, 255, 0, 0, 255);
192 }
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500193 }
194 }
Cooper Partin558f2b52015-06-02 09:34:11 -0700195 swapBuffers();
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500196 }
197
198 GLuint mProgram;
199 GLuint mVertexBuffer;
200 GLuint mIndexBuffer;
Cooper Partin558f2b52015-06-02 09:34:11 -0700201
202 GLuint mVertexWithColorBufferProgram;
203 GLuint mVertexWithColorBuffer;
204
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500205 static const GLuint mPointCount = 4;
Cooper Partin558f2b52015-06-02 09:34:11 -0700206
207 private:
208 const size_t VertexWithColorSize = sizeof(float) * 5;
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500209};
210
Jamie Madillfa05f602015-05-07 13:47:11 -0400211typedef IndexedPointsTest<GLubyte, GL_UNSIGNED_BYTE> IndexedPointsTestUByte;
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500212
Jamie Madillfa05f602015-05-07 13:47:11 -0400213TEST_P(IndexedPointsTestUByte, UnsignedByteOffset0)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500214{
215 runTest(0);
216}
217
Jamie Madillfa05f602015-05-07 13:47:11 -0400218TEST_P(IndexedPointsTestUByte, UnsignedByteOffset1)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500219{
220 runTest(1);
221}
222
Jamie Madillfa05f602015-05-07 13:47:11 -0400223TEST_P(IndexedPointsTestUByte, UnsignedByteOffset2)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500224{
225 runTest(2);
226}
227
Jamie Madillfa05f602015-05-07 13:47:11 -0400228TEST_P(IndexedPointsTestUByte, UnsignedByteOffset3)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500229{
230 runTest(3);
231}
232
Cooper Partin558f2b52015-06-02 09:34:11 -0700233TEST_P(IndexedPointsTestUByte, VertexWithColorUnsignedByteOffset0)
234{
235 runTest(0, true);
236}
237
238TEST_P(IndexedPointsTestUByte, VertexWithColorUnsignedByteOffset1)
239{
240 runTest(1, true);
241}
242
243TEST_P(IndexedPointsTestUByte, VertexWithColorUnsignedByteOffset2)
244{
245 runTest(2, true);
246}
247
248TEST_P(IndexedPointsTestUByte, VertexWithColorUnsignedByteOffset3)
249{
250 runTest(3, true);
251}
252
Jamie Madillfa05f602015-05-07 13:47:11 -0400253typedef IndexedPointsTest<GLushort, GL_UNSIGNED_SHORT> IndexedPointsTestUShort;
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500254
Jamie Madillfa05f602015-05-07 13:47:11 -0400255TEST_P(IndexedPointsTestUShort, UnsignedShortOffset0)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500256{
257 runTest(0);
258}
259
Jamie Madillfa05f602015-05-07 13:47:11 -0400260TEST_P(IndexedPointsTestUShort, UnsignedShortOffset1)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500261{
262 runTest(1);
263}
264
Jamie Madillfa05f602015-05-07 13:47:11 -0400265TEST_P(IndexedPointsTestUShort, UnsignedShortOffset2)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500266{
267 runTest(2);
268}
269
Jamie Madillfa05f602015-05-07 13:47:11 -0400270TEST_P(IndexedPointsTestUShort, UnsignedShortOffset3)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500271{
272 runTest(3);
273}
274
Cooper Partin558f2b52015-06-02 09:34:11 -0700275TEST_P(IndexedPointsTestUShort, VertexWithColorUnsignedShortOffset0)
276{
277 runTest(0, true);
278}
279
280TEST_P(IndexedPointsTestUShort, VertexWithColorUnsignedShortOffset1)
281{
282 runTest(1, true);
283}
284
285TEST_P(IndexedPointsTestUShort, VertexWithColorUnsignedShortOffset2)
286{
287 runTest(2, true);
288}
289
290TEST_P(IndexedPointsTestUShort, VertexWithColorUnsignedShortOffset3)
291{
292 runTest(3, true);
293}
294
295TEST_P(IndexedPointsTestUShort, VertexWithColorUnsignedShortOffsetChangingIndices)
296{
297 runTest(3, true);
298 runTest(1, true);
299 runTest(0, true);
300 runTest(2, true);
301}
302
Jamie Madillfa05f602015-05-07 13:47:11 -0400303typedef IndexedPointsTest<GLuint, GL_UNSIGNED_INT> IndexedPointsTestUInt;
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500304
Jamie Madillfa05f602015-05-07 13:47:11 -0400305TEST_P(IndexedPointsTestUInt, UnsignedIntOffset0)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500306{
307 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
308 {
309 return;
310 }
311
312 runTest(0);
313}
314
Jamie Madillfa05f602015-05-07 13:47:11 -0400315TEST_P(IndexedPointsTestUInt, UnsignedIntOffset1)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500316{
317 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
318 {
319 return;
320 }
321
322 runTest(1);
323}
324
Jamie Madillfa05f602015-05-07 13:47:11 -0400325TEST_P(IndexedPointsTestUInt, UnsignedIntOffset2)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500326{
327 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
328 {
329 return;
330 }
331
332 runTest(2);
333}
334
Jamie Madillfa05f602015-05-07 13:47:11 -0400335TEST_P(IndexedPointsTestUInt, UnsignedIntOffset3)
Geoff Langf8c2f5c2013-12-05 13:52:33 -0500336{
337 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
338 {
339 return;
340 }
341
342 runTest(3);
343}
Jamie Madillfa05f602015-05-07 13:47:11 -0400344
Cooper Partin558f2b52015-06-02 09:34:11 -0700345TEST_P(IndexedPointsTestUInt, VertexWithColorUnsignedIntOffset0)
346{
347 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
348 {
349 return;
350 }
351
352 runTest(0, false);
353}
354
355TEST_P(IndexedPointsTestUInt, VertexWithColorUnsignedIntOffset1)
356{
357 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
358 {
359 return;
360 }
361
362 runTest(1, false);
363}
364
365TEST_P(IndexedPointsTestUInt, VertexWithColorUnsignedIntOffset2)
366{
367 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
368 {
369 return;
370 }
371
372 runTest(2, false);
373}
374
375TEST_P(IndexedPointsTestUInt, VertexWithColorUnsignedIntOffset3)
376{
377 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_element_index_uint"))
378 {
379 return;
380 }
381
382 runTest(3, false);
383}
384
Geoff Langc4222072015-05-25 13:19:48 -0400385// TODO(geofflang): Figure out why this test fails on Intel OpenGL
Cooper Partin558f2b52015-06-02 09:34:11 -0700386ANGLE_INSTANTIATE_TEST(IndexedPointsTestUByte, ES2_D3D11(), ES2_D3D11_FL9_3());
387ANGLE_INSTANTIATE_TEST(IndexedPointsTestUShort, ES2_D3D11(), ES2_D3D11_FL9_3());
388ANGLE_INSTANTIATE_TEST(IndexedPointsTestUInt, ES2_D3D11(), ES2_D3D11_FL9_3());