blob: ff4564be8d81aa35eaf7d93307408cb26c5b8107 [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{
Jamie Madilld55d2832015-10-27 13:59:19 -0400128 GLint vertexData[] = {1, 2, 3, 1, 2, 3};
129 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
130
131 drawQuad(mProgram, "position", 0.5f);
132
133 GLint pixelValue = 0;
134 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
135
136 ASSERT_GL_NO_ERROR();
137 EXPECT_EQ(vertexData[2], pixelValue);
138}
139
140// Ensure that any provoking vertex shenanigans still gives correct vertex streams.
141TEST_P(ProvokingVertexTest, FlatTriWithTransformFeedback)
142{
Jamie Madilld55d2832015-10-27 13:59:19 -0400143 glGenTransformFeedbacks(1, &mTransformFeedback);
144 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, mTransformFeedback);
145
146 glGenBuffers(1, &mBuffer);
147 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, mBuffer);
148 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 128, nullptr, GL_STREAM_DRAW);
149
150 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mBuffer);
151
152 GLint vertexData[] = {1, 2, 3, 1, 2, 3};
153 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
154
155 glBeginTransformFeedback(GL_TRIANGLES);
156 drawQuad(mProgram, "position", 0.5f);
157 glEndTransformFeedback();
158
159 GLint pixelValue = 0;
160 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
161
162 ASSERT_GL_NO_ERROR();
163 EXPECT_EQ(vertexData[2], pixelValue);
164
165 GLvoid *mapPointer =
166 glMapBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, sizeof(int) * 6, GL_MAP_READ_BIT);
167 ASSERT_NE(nullptr, mapPointer);
168
169 int *mappedInts = static_cast<int *>(mapPointer);
170 for (unsigned int cnt = 0; cnt < 6; ++cnt)
171 {
172 EXPECT_EQ(vertexData[cnt], mappedInts[cnt]);
173 }
174}
175
176// Test drawing a simple line with flat shading, and different valued vertices.
177TEST_P(ProvokingVertexTest, FlatLine)
178{
Jamie Madilld55d2832015-10-27 13:59:19 -0400179 GLfloat halfPixel = 1.0f / static_cast<GLfloat>(getWindowWidth());
180
181 GLint vertexData[] = {1, 2};
182 GLfloat positionData[] = {-1.0f + halfPixel, -1.0f, -1.0f + halfPixel, 1.0f};
183
184 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
185
186 GLint positionLocation = glGetAttribLocation(mProgram, "position");
187 glEnableVertexAttribArray(positionLocation);
188 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
189
190 glUseProgram(mProgram);
191 glDrawArrays(GL_LINES, 0, 2);
192
193 GLint pixelValue = 0;
194 glReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_INT, &pixelValue);
195
196 ASSERT_GL_NO_ERROR();
197 EXPECT_EQ(vertexData[1], pixelValue);
198}
199
200// Test drawing a simple triangle strip with flat shading, and different valued vertices.
201TEST_P(ProvokingVertexTest, FlatTriStrip)
202{
Jamie Madilld55d2832015-10-27 13:59:19 -0400203 GLint vertexData[] = {1, 2, 3, 4, 5, 6};
204 GLfloat positionData[] = {-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f,
205 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
206
207 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
208
209 GLint positionLocation = glGetAttribLocation(mProgram, "position");
210 glEnableVertexAttribArray(positionLocation);
211 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
212
213 glUseProgram(mProgram);
214 glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
215
216 std::vector<GLint> pixelBuffer(getWindowWidth() * getWindowHeight(), 0);
217 glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RED_INTEGER, GL_INT,
218 &pixelBuffer[0]);
219
220 ASSERT_GL_NO_ERROR();
221
222 for (unsigned int triIndex = 0; triIndex < 4; ++triIndex)
223 {
224 GLfloat sumX = positionData[triIndex * 2 + 0] + positionData[triIndex * 2 + 2] +
225 positionData[triIndex * 2 + 4];
226 GLfloat sumY = positionData[triIndex * 2 + 1] + positionData[triIndex * 2 + 3] +
227 positionData[triIndex * 2 + 5];
228
229 float centerX = sumX / 3.0f * 0.5f + 0.5f;
230 float centerY = sumY / 3.0f * 0.5f + 0.5f;
231 unsigned int pixelX =
232 static_cast<unsigned int>(centerX * static_cast<GLfloat>(getWindowWidth()));
233 unsigned int pixelY =
234 static_cast<unsigned int>(centerY * static_cast<GLfloat>(getWindowHeight()));
235 unsigned int pixelIndex = pixelY * getWindowWidth() + pixelX;
236
237 unsigned int provokingVertexIndex = triIndex + 2;
238
239 EXPECT_EQ(vertexData[provokingVertexIndex], pixelBuffer[pixelIndex]);
240 }
241}
242
243// Test drawing an indexed triangle strip with flat shading and primitive restart.
244TEST_P(ProvokingVertexTest, FlatTriStripPrimitiveRestart)
245{
246 // TODO(jmadill): Implement on the D3D back-end.
247 if (isD3D11())
248 {
249 std::cout << "Test disabled on D3D11." << std::endl;
250 return;
251 }
252
253 GLint indexData[] = {0, 1, 2, -1, 1, 2, 3, 4, -1, 3, 4, 5};
254 GLint vertexData[] = {1, 2, 3, 4, 5, 6};
255 GLfloat positionData[] = {-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, -1.0f,
256 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
257
258 glVertexAttribIPointer(mIntAttribLocation, 1, GL_INT, 0, vertexData);
259
260 GLint positionLocation = glGetAttribLocation(mProgram, "position");
261 glEnableVertexAttribArray(positionLocation);
262 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, positionData);
263
264 glDisable(GL_CULL_FACE);
265 glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
266 glUseProgram(mProgram);
267 glDrawElements(GL_TRIANGLE_STRIP, 12, GL_UNSIGNED_INT, indexData);
268
269 std::vector<GLint> pixelBuffer(getWindowWidth() * getWindowHeight(), 0);
270 glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RED_INTEGER, GL_INT,
271 &pixelBuffer[0]);
272
273 ASSERT_GL_NO_ERROR();
274
275 // Account for primitive restart when checking the tris.
276 GLint triOffsets[] = {0, 4, 5, 9};
277
278 for (unsigned int triIndex = 0; triIndex < 4; ++triIndex)
279 {
280 GLint vertexA = indexData[triOffsets[triIndex] + 0];
281 GLint vertexB = indexData[triOffsets[triIndex] + 1];
282 GLint vertexC = indexData[triOffsets[triIndex] + 2];
283
284 GLfloat sumX =
285 positionData[vertexA * 2] + positionData[vertexB * 2] + positionData[vertexC * 2];
286 GLfloat sumY = positionData[vertexA * 2 + 1] + positionData[vertexB * 2 + 1] +
287 positionData[vertexC * 2 + 1];
288
289 float centerX = sumX / 3.0f * 0.5f + 0.5f;
290 float centerY = sumY / 3.0f * 0.5f + 0.5f;
291 unsigned int pixelX =
292 static_cast<unsigned int>(centerX * static_cast<GLfloat>(getWindowWidth()));
293 unsigned int pixelY =
294 static_cast<unsigned int>(centerY * static_cast<GLfloat>(getWindowHeight()));
295 unsigned int pixelIndex = pixelY * getWindowWidth() + pixelX;
296
297 unsigned int provokingVertexIndex = triIndex + 2;
298
299 EXPECT_EQ(vertexData[provokingVertexIndex], pixelBuffer[pixelIndex]);
300 }
301}
302
303ANGLE_INSTANTIATE_TEST(ProvokingVertexTest, ES3_D3D11(), ES3_OPENGL());
304
305} // anonymous namespace