blob: c9dd19912b21e0e47cce1814ca54704bdbbce71a [file] [log] [blame]
Jamie Madilld55d2832015-10-27 13:59:19 -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// ProvkingVertexTest:
7// Tests on the conformance of the provoking vertex, which applies to flat
8// shading and compatibility with D3D. See the section on 'flatshading'
9// in the ES 3 specs.
10//
11
12#include "test_utils/ANGLETest.h"
13
14using namespace angle;
15
16namespace
17{
18
19class ProvokingVertexTest : public ANGLETest
20{
21 protected:
22 ProvokingVertexTest()
23 : mProgram(0),
24 mFramebuffer(0),
25 mTexture(0),
26 mTransformFeedback(0),
27 mBuffer(0),
28 mIntAttribLocation(-1)
29 {
30 setWindowWidth(64);
31 setWindowHeight(64);
32 setConfigRedBits(8);
33 setConfigGreenBits(8);
34 setConfigBlueBits(8);
35 setConfigAlphaBits(8);
36 setConfigDepthBits(24);
37 }
38
39 void SetUp() override
40 {
41 ANGLETest::SetUp();
42
43 const std::string &vertexShader =
44 "#version 300 es\n"
45 "in int intAttrib;\n"
46 "in vec2 position;\n"
47 "flat out int attrib;\n"
48 "void main() {\n"
49 " gl_Position = vec4(position, 0, 1);\n"
50 " attrib = intAttrib;\n"
51 "}";
52
53 const std::string &fragmentShader =
54 "#version 300 es\n"
55 "flat in int attrib;\n"
56 "out int fragColor;\n"
57 "void main() {\n"
58 " fragColor = attrib;\n"
59 "}";
60
61 std::vector<std::string> tfVaryings;
62 tfVaryings.push_back("attrib");
63 mProgram = CompileProgramWithTransformFeedback(vertexShader, fragmentShader, tfVaryings,
64 GL_SEPARATE_ATTRIBS);
65 ASSERT_NE(0u, mProgram);
66
67 glGenTextures(1, &mTexture);
68 glBindTexture(GL_TEXTURE_2D, mTexture);
69 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32I, getWindowWidth(), getWindowHeight());
70
71 glGenFramebuffers(1, &mFramebuffer);
72 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
73 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture, 0);
74
75 mIntAttribLocation = glGetAttribLocation(mProgram, "intAttrib");
76 ASSERT_NE(-1, mIntAttribLocation);
77 glEnableVertexAttribArray(mIntAttribLocation);
78
79 ASSERT_GL_NO_ERROR();
80 }
81
82 void TearDown() override
83 {
84 if (mProgram != 0)
85 {
86 glDeleteProgram(mProgram);
87 mProgram = 0;
88 }
89
90 if (mFramebuffer != 0)
91 {
92 glDeleteFramebuffers(1, &mFramebuffer);
93 mFramebuffer = 0;
94 }
95
96 if (mTexture != 0)
97 {
98 glDeleteTextures(1, &mTexture);
99 mTexture = 0;
100 }
101
102 if (mTransformFeedback != 0)
103 {
104 glDeleteTransformFeedbacks(1, &mTransformFeedback);
105 mTransformFeedback = 0;
106 }
107
108 if (mBuffer != 0)
109 {
110 glDeleteBuffers(1, &mBuffer);
111 mBuffer = 0;
112 }
113
114 ANGLETest::TearDown();
115 }
116
117 GLuint mProgram;
118 GLuint mFramebuffer;
119 GLuint mTexture;
120 GLuint mTransformFeedback;
121 GLuint mBuffer;
122 GLint mIntAttribLocation;
123};
124
125// Test drawing a simple triangle with flat shading, and different valued vertices.
126TEST_P(ProvokingVertexTest, FlatTriangle)
127{
128 // TODO(jmadill): Implement on the D3D back-end.
129 if (isD3D11())
130 {
131 std::cout << "Test disabled on D3D11." << std::endl;
132 return;
133 }
134
135 GLint vertexData[] = {1, 2, 3, 1, 2, 3};
136 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
137
138 drawQuad(mProgram, "position", 0.5f);
139
140 GLint pixelValue = 0;
141 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
142
143 ASSERT_GL_NO_ERROR();
144 EXPECT_EQ(vertexData[2], pixelValue);
145}
146
147// Ensure that any provoking vertex shenanigans still gives correct vertex streams.
148TEST_P(ProvokingVertexTest, FlatTriWithTransformFeedback)
149{
150 // TODO(jmadill): Implement on the D3D back-end.
151 if (isD3D11())
152 {
153 std::cout << "Test disabled on D3D11." << std::endl;
154 return;
155 }
156
157 glGenTransformFeedbacks(1, &mTransformFeedback);
158 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, mTransformFeedback);
159
160 glGenBuffers(1, &mBuffer);
161 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, mBuffer);
162 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 128, nullptr, GL_STREAM_DRAW);
163
164 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBuffer);
165
166 GLint vertexData[] = {1, 2, 3, 1, 2, 3};
167 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
168
169 glBeginTransformFeedback(GL_TRIANGLES);
170 drawQuad(mProgram, "position", 0.5f);
171 glEndTransformFeedback();
172
173 GLint pixelValue = 0;
174 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
175
176 ASSERT_GL_NO_ERROR();
177 EXPECT_EQ(vertexData[2], pixelValue);
178
179 GLvoid *mapPointer =
180 glMapBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(int) * 6, GL_MAP_READ_BIT);
181 ASSERT_NE(nullptr, mapPointer);
182
183 int *mappedInts = static_cast<int *>(mapPointer);
184 for (unsigned int cnt = 0; cnt < 6; ++cnt)
185 {
186 EXPECT_EQ(vertexData[cnt], mappedInts[cnt]);
187 }
188}
189
190// Test drawing a simple line with flat shading, and different valued vertices.
191TEST_P(ProvokingVertexTest, FlatLine)
192{
193 // TODO(jmadill): Implement on the D3D back-end.
194 if (isD3D11())
195 {
196 std::cout << "Test disabled on D3D11." << std::endl;
197 return;
198 }
199
200 GLfloat halfPixel = 1.0f / static_cast<GLfloat>(getWindowWidth());
201
202 GLint vertexData[] = {1, 2};
203 GLfloat positionData[] = {-1.0f + halfPixel, -1.0f, -1.0f + halfPixel, 1.0f};
204
205 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
206
207 GLint positionLocation = glGetAttribLocation(mProgram, "position");
208 glEnableVertexAttribArray(positionLocation);
209 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
210
211 glUseProgram(mProgram);
212 glDrawArrays(GL_LINES, 0, 2);
213
214 GLint pixelValue = 0;
215 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
216
217 ASSERT_GL_NO_ERROR();
218 EXPECT_EQ(vertexData[1], pixelValue);
219}
220
221// Test drawing a simple triangle strip with flat shading, and different valued vertices.
222TEST_P(ProvokingVertexTest, FlatTriStrip)
223{
224 // TODO(jmadill): Implement on the D3D back-end.
225 if (isD3D11())
226 {
227 std::cout << "Test disabled on D3D11." << std::endl;
228 return;
229 }
230
231 GLint vertexData[] = {1, 2, 3, 4, 5, 6};
232 GLfloat positionData[] = {-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f,
233 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
234
235 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
236
237 GLint positionLocation = glGetAttribLocation(mProgram, "position");
238 glEnableVertexAttribArray(positionLocation);
239 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
240
241 glUseProgram(mProgram);
242 glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
243
244 std::vector<GLint> pixelBuffer(getWindowWidth() * getWindowHeight(), 0);
245 glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RED_INTEGER, GL_INT,
246 &pixelBuffer[0]);
247
248 ASSERT_GL_NO_ERROR();
249
250 for (unsigned int triIndex = 0; triIndex < 4; ++triIndex)
251 {
252 GLfloat sumX = positionData[triIndex * 2 + 0] + positionData[triIndex * 2 + 2] +
253 positionData[triIndex * 2 + 4];
254 GLfloat sumY = positionData[triIndex * 2 + 1] + positionData[triIndex * 2 + 3] +
255 positionData[triIndex * 2 + 5];
256
257 float centerX = sumX / 3.0f * 0.5f + 0.5f;
258 float centerY = sumY / 3.0f * 0.5f + 0.5f;
259 unsigned int pixelX =
260 static_cast<unsigned int>(centerX * static_cast<GLfloat>(getWindowWidth()));
261 unsigned int pixelY =
262 static_cast<unsigned int>(centerY * static_cast<GLfloat>(getWindowHeight()));
263 unsigned int pixelIndex = pixelY * getWindowWidth() + pixelX;
264
265 unsigned int provokingVertexIndex = triIndex + 2;
266
267 EXPECT_EQ(vertexData[provokingVertexIndex], pixelBuffer[pixelIndex]);
268 }
269}
270
271// Test drawing an indexed triangle strip with flat shading and primitive restart.
272TEST_P(ProvokingVertexTest, FlatTriStripPrimitiveRestart)
273{
274 // TODO(jmadill): Implement on the D3D back-end.
275 if (isD3D11())
276 {
277 std::cout << "Test disabled on D3D11." << std::endl;
278 return;
279 }
280
281 GLint indexData[] = {0, 1, 2, -1, 1, 2, 3, 4, -1, 3, 4, 5};
282 GLint vertexData[] = {1, 2, 3, 4, 5, 6};
283 GLfloat positionData[] = {-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f,
284 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
285
286 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
287
288 GLint positionLocation = glGetAttribLocation(mProgram, "position");
289 glEnableVertexAttribArray(positionLocation);
290 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
291
292 glDisable(GL_CULL_FACE);
293 glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
294 glUseProgram(mProgram);
295 glDrawElements(GL_TRIANGLE_STRIP, 12, GL_UNSIGNED_INT, indexData);
296
297 std::vector<GLint> pixelBuffer(getWindowWidth() * getWindowHeight(), 0);
298 glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RED_INTEGER, GL_INT,
299 &pixelBuffer[0]);
300
301 ASSERT_GL_NO_ERROR();
302
303 // Account for primitive restart when checking the tris.
304 GLint triOffsets[] = {0, 4, 5, 9};
305
306 for (unsigned int triIndex = 0; triIndex < 4; ++triIndex)
307 {
308 GLint vertexA = indexData[triOffsets[triIndex] + 0];
309 GLint vertexB = indexData[triOffsets[triIndex] + 1];
310 GLint vertexC = indexData[triOffsets[triIndex] + 2];
311
312 GLfloat sumX =
313 positionData[vertexA * 2] + positionData[vertexB * 2] + positionData[vertexC * 2];
314 GLfloat sumY = positionData[vertexA * 2 + 1] + positionData[vertexB * 2 + 1] +
315 positionData[vertexC * 2 + 1];
316
317 float centerX = sumX / 3.0f * 0.5f + 0.5f;
318 float centerY = sumY / 3.0f * 0.5f + 0.5f;
319 unsigned int pixelX =
320 static_cast<unsigned int>(centerX * static_cast<GLfloat>(getWindowWidth()));
321 unsigned int pixelY =
322 static_cast<unsigned int>(centerY * static_cast<GLfloat>(getWindowHeight()));
323 unsigned int pixelIndex = pixelY * getWindowWidth() + pixelX;
324
325 unsigned int provokingVertexIndex = triIndex + 2;
326
327 EXPECT_EQ(vertexData[provokingVertexIndex], pixelBuffer[pixelIndex]);
328 }
329}
330
331ANGLE_INSTANTIATE_TEST(ProvokingVertexTest, ES3_D3D11(), ES3_OPENGL());
332
333} // anonymous namespace