7037d7aaccff8f113430210fb2b5a4805a6ff81a
[platform/upstream/VK-GL-CTS.git] / modules / gles2 / functional / es2fClippingTests.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 2.0 Module
3  * -------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Clipping tests.
22  *//*--------------------------------------------------------------------*/
23
24 #include "es2fClippingTests.hpp"
25 #include "tcuRenderTarget.hpp"
26 #include "tcuTextureUtil.hpp"
27 #include "tcuImageCompare.hpp"
28 #include "tcuVectorUtil.hpp"
29 #include "deStringUtil.hpp"
30 #include "deRandom.hpp"
31
32 #include "sglrReferenceContext.hpp"
33 #include "sglrGLContext.hpp"
34
35 #include "glwEnums.hpp"
36 #include "glwDefs.hpp"
37 #include "glwFunctions.hpp"
38
39 using namespace glw; // GLint and other GL types
40
41 namespace deqp
42 {
43 namespace gles2
44 {
45 namespace Functional
46 {
47 namespace
48 {
49
50 using tcu::PixelBufferAccess;
51 using tcu::ConstPixelBufferAccess;
52 using tcu::TestLog;
53
54 static const tcu::Vec4  MASK_COLOR_OK                    = tcu::Vec4(0.0f, 0.1f, 0.0f, 1.0f);
55 static const tcu::Vec4  MASK_COLOR_DEV                   = tcu::Vec4(0.8f, 0.5f, 0.0f, 1.0f);
56 static const tcu::Vec4  MASK_COLOR_FAIL                  = tcu::Vec4(1.0f, 0.0f, 1.0f, 1.0f);
57
58 const int                                       TEST_CANVAS_SIZE  = 200;
59 const rr::WindowRectangle       VIEWPORT_WHOLE          (0,                                             0,                                      TEST_CANVAS_SIZE,               TEST_CANVAS_SIZE);
60 const rr::WindowRectangle       VIEWPORT_CENTER         (TEST_CANVAS_SIZE/4,    TEST_CANVAS_SIZE/4,     TEST_CANVAS_SIZE/2,             TEST_CANVAS_SIZE/2);
61 const rr::WindowRectangle       VIEWPORT_CORNER         (TEST_CANVAS_SIZE/2,    TEST_CANVAS_SIZE/2,     TEST_CANVAS_SIZE/2,             TEST_CANVAS_SIZE/2);
62
63
64 const char* shaderSourceVertex =        "attribute highp vec4 a_position;\n"
65                                                                         "attribute highp vec4 a_color;\n"
66                                                                         "attribute highp float a_pointSize;\n"
67                                                                         "varying mediump vec4 varFragColor;\n"
68                                                                         "void main (void)\n"
69                                                                         "{\n"
70                                                                         "       gl_Position = a_position;\n"
71                                                                         "       gl_PointSize = a_pointSize;\n"
72                                                                         "       varFragColor = a_color;\n"
73                                                                         "}\n";
74 const char* shaderSourceFragment =      "varying mediump vec4 varFragColor;\n"
75                                                                         "void main (void)\n"
76                                                                         "{\n"
77                                                                         "       gl_FragColor = varFragColor;\n"
78                                                                         "}\n";
79
80 inline bool isBlack (const tcu::IVec4& a)
81 {
82         return a.x() == 0 && a.y() == 0 && a.z() == 0;
83 }
84
85 inline bool isHalfFilled (const tcu::IVec4& a)
86 {
87         const tcu::IVec4 halfFilled     (127, 0, 0, 0);
88         const tcu::IVec4 threshold      (20, 256, 256, 256);
89
90         return tcu::boolAll(tcu::lessThanEqual(tcu::abs(a - halfFilled), threshold));
91 }
92
93 inline bool isLessThanHalfFilled (const tcu::IVec4& a)
94 {
95         const int halfFilled = 127;
96         const int threshold      = 20;
97
98         return a.x() + threshold < halfFilled;
99 }
100
101 inline bool compareBlackNonBlackPixels (const tcu::IVec4& a, const tcu::IVec4& b)
102 {
103         return isBlack(a) == isBlack(b);
104 }
105
106 inline bool compareColoredPixels (const tcu::IVec4& a, const tcu::IVec4& b)
107 {
108         const bool aIsBlack = isBlack(a);
109         const bool bIsBlack = isBlack(b);
110         const tcu::IVec4 threshold(20, 20, 20, 0);
111
112         if (aIsBlack && bIsBlack)
113                 return true;
114         if (aIsBlack != bIsBlack)
115                 return false;
116
117         return tcu::boolAll(tcu::lessThanEqual(tcu::abs(a - b), threshold));
118 }
119
120 void blitImageOnBlackSurface(const ConstPixelBufferAccess& src, const PixelBufferAccess& dst)
121 {
122         const int                       height  = src.getHeight();
123         const int                       width   = src.getWidth();
124
125         for (int y = 0; y < height; y++)
126         for (int x = 0; x < width; x++)
127         {
128                 const tcu::IVec4 cSrc = src.getPixelInt(x, y);
129                 const tcu::IVec4 cDst = tcu::IVec4(cSrc.x(), cSrc.y(), cSrc.z(), 255);
130
131                 dst.setPixel(cDst, x, y);
132         }
133 }
134
135 /*--------------------------------------------------------------------*//*!
136  * \brief Pixelwise comparison of two images.
137  * \note copied & modified from glsRasterizationTests
138  *
139  * Kernel radius defines maximum allowed distance. If radius is 0, only
140  * perfect match is allowed. Radius of 1 gives a 3x3 kernel. Pixels are
141  * equal if pixelCmp returns true..
142  *
143  * Return values:  -1 = Perfect match
144  *                                      0 = Deviation within kernel
145  *                                 >0 = Number of faulty pixels
146  *//*--------------------------------------------------------------------*/
147 inline int compareImages (tcu::TestLog& log, const ConstPixelBufferAccess& test, const ConstPixelBufferAccess& ref, const PixelBufferAccess& diffMask, int kernelRadius, bool (*pixelCmp)(const tcu::IVec4& a, const tcu::IVec4& b))
148 {
149         const int                       height                          = test.getHeight();
150         const int                       width                           = test.getWidth();
151         int                                     deviatingPixels         = 0;
152         int                                     faultyPixels            = 0;
153         int                                     compareFailed           = -1;
154
155         tcu::clear(diffMask, MASK_COLOR_OK);
156
157         for (int y = 0; y < height; y++)
158         {
159                 for (int x = 0; x < width; x++)
160                 {
161                         const tcu::IVec4 cRef   = ref.getPixelInt(x, y);
162                         const tcu::IVec4 cTest  = test.getPixelInt(x, y);
163
164                         // Pixelwise match, no deviation or fault
165                         if ((*pixelCmp)(cRef, cTest))
166                                 continue;
167
168                         // Deviation
169                         {
170                                 const int radius        = kernelRadius;
171                                 bool foundRef           = false;
172                                 bool foundTest          = false;
173
174                                 // edges are considered a "deviation" too. The suitable pixel could be "behind" the edge
175                                 if (y < radius || x < radius || y + radius >= height || x + radius >= width)
176                                 {
177                                         foundRef        = true;
178                                         foundTest       = true;
179                                 }
180                                 else
181                                 {
182                                         // find ref
183                                         for (int kY = y - radius; kY <= y + radius; kY++)
184                                         for (int kX = x - radius; kX <= x + radius; kX++)
185                                         {
186                                                 if ((*pixelCmp)(cRef, test.getPixelInt(kX, kY)))
187                                                 {
188                                                         foundRef = true;
189                                                         break;
190                                                 }
191                                         }
192
193                                         // find result
194                                         for (int kY = y - radius; kY <= y + radius; kY++)
195                                         for (int kX = x - radius; kX <= x + radius; kX++)
196                                         {
197                                                 if ((*pixelCmp)(cTest, ref.getPixelInt(kX, kY)))
198                                                 {
199                                                         foundTest = true;
200                                                         break;
201                                                 }
202                                         }
203                                 }
204
205                                 // A pixel is deviating if the reference color is found inside the kernel and (~= every pixel reference draws must be drawn by the gl too)
206                                 // the result color is found in the reference image inside the kernel         (~= every pixel gl draws must be drawn by the reference too)
207                                 if (foundRef && foundTest)
208                                 {
209                                         diffMask.setPixel(MASK_COLOR_DEV, x, y);
210                                         if (compareFailed == -1)
211                                                 compareFailed = 0;
212                                         deviatingPixels++;
213                                         continue;
214                                 }
215                         }
216
217                         diffMask.setPixel(MASK_COLOR_FAIL, x, y);
218                         faultyPixels++;                                                                 // The pixel is faulty if the color is not found
219                         compareFailed = 1;
220                 }
221         }
222
223         log << TestLog::Message << deviatingPixels      << " deviating pixel(s) found." << TestLog::EndMessage;
224         log << TestLog::Message << faultyPixels         << " faulty pixel(s) found." << TestLog::EndMessage;
225
226         return (compareFailed == 1 ? faultyPixels : compareFailed);
227 }
228
229 /*--------------------------------------------------------------------*//*!
230  * \brief Pixelwise comparison of two images.
231  *
232  * Kernel radius defines maximum allowed distance. If radius is 0, only
233  * perfect match is allowed. Radius of 1 gives a 3x3 kernel. Pixels are
234  * equal if they both are black, or both are non-black.
235  *
236  * Return values:  -1 = Perfect match
237  *                                      0 = Deviation within kernel
238  *                                 >0 = Number of faulty pixels
239  *//*--------------------------------------------------------------------*/
240 int compareBlackNonBlackImages (tcu::TestLog& log, const ConstPixelBufferAccess& test, const ConstPixelBufferAccess& ref, const PixelBufferAccess& diffMask, int kernelRadius)
241 {
242         return compareImages(log, test, ref, diffMask, kernelRadius, compareBlackNonBlackPixels);
243 }
244
245 /*--------------------------------------------------------------------*//*!
246  * \brief Pixelwise comparison of two images.
247  *
248  * Kernel radius defines maximum allowed distance. If radius is 0, only
249  * perfect match is allowed. Radius of 1 gives a 3x3 kernel. Pixels are
250  * equal if they both are black, or both are non-black with color values
251  * close to each other.
252  *
253  * Return values:  -1 = Perfect match
254  *                                      0 = Deviation within kernel
255  *                                 >0 = Number of faulty pixels
256  *//*--------------------------------------------------------------------*/
257 int compareColoredImages (tcu::TestLog& log, const ConstPixelBufferAccess& test, const ConstPixelBufferAccess& ref, const PixelBufferAccess& diffMask, int kernelRadius)
258 {
259         return compareImages(log, test, ref, diffMask, kernelRadius, compareColoredPixels);
260 }
261
262 /*--------------------------------------------------------------------*//*!
263  * \brief Overdraw check verification
264  *
265  * Check that image does not have at any point a
266  * pixel with red component value > 0.5
267  *
268  * Return values:  false = area not filled, or leaking
269  *//*--------------------------------------------------------------------*/
270 bool checkHalfFilledImageOverdraw (tcu::TestLog& log, const tcu::RenderTarget& m_renderTarget, const ConstPixelBufferAccess& image, const PixelBufferAccess& output)
271 {
272         const int                       height                          = image.getHeight();
273         const int                       width                           = image.getWidth();
274
275         bool                            faulty                          = false;
276
277         tcu::clear(output, MASK_COLOR_OK);
278
279         for (int y = 0; y < height; y++)
280         {
281                 for (int x = 0; x < width; x++)
282                 {
283                         const tcu::IVec4        cTest   = image.getPixelInt(x, y);
284
285                         const bool pixelValid = isBlack(cTest) || isHalfFilled(cTest) || (m_renderTarget.getNumSamples() > 1 && isLessThanHalfFilled(cTest));
286
287                         if (!pixelValid)
288                         {
289                                 output.setPixel(MASK_COLOR_FAIL, x, y);
290                                 faulty = true;
291                         }
292                 }
293         }
294
295         if (faulty)
296                 log << TestLog::Message << "Faulty pixel(s) found." << TestLog::EndMessage;
297
298         return !faulty;
299 }
300
301 void checkPointSize (const glw::Functions& gl, float pointSize)
302 {
303         GLfloat pointSizeRange[2] = {0,0};
304         gl.getFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
305         if (pointSizeRange[1] < pointSize)
306                 throw tcu::NotSupportedError("Maximum point size is too low for this test");
307 }
308
309 void checkLineWidth (const glw::Functions& gl, float lineWidth)
310 {
311         GLfloat lineWidthRange[2] = {0,0};
312         gl.getFloatv(GL_ALIASED_LINE_WIDTH_RANGE, lineWidthRange);
313         if (lineWidthRange[1] < lineWidth)
314                 throw tcu::NotSupportedError("Maximum line width is too low for this test");
315 }
316
317 tcu::Vec3 IVec3ToVec3 (const tcu::IVec3& v)
318 {
319         return tcu::Vec3((float)v.x(), (float)v.y(), (float)v.z());
320 }
321
322 bool pointOnTriangle (const tcu::IVec3& p, const tcu::IVec3& t0, const tcu::IVec3& t1, const tcu::IVec3& t2)
323 {
324         // Must be on the plane
325         const tcu::IVec3 n = tcu::cross(t1 - t0, t2 - t0);
326         const tcu::IVec3 d = (p - t0);
327
328         if (tcu::dot(n, d))
329                 return false;
330
331         // Must be within the triangle area
332         if (deSign32(tcu::dot(n, tcu::cross(t1 - t0, p - t0))) == deSign32(tcu::dot(n, tcu::cross(t2 - t0, p - t0))))
333                 return false;
334         if (deSign32(tcu::dot(n, tcu::cross(t2 - t1, p - t1))) == deSign32(tcu::dot(n, tcu::cross(t0 - t1, p - t1))))
335                 return false;
336         if (deSign32(tcu::dot(n, tcu::cross(t0 - t2, p - t2))) == deSign32(tcu::dot(n, tcu::cross(t1 - t2, p - t2))))
337                 return false;
338
339         return true;
340 }
341
342 bool pointsOnLine (const tcu::IVec2& t0, const tcu::IVec2& t1, const tcu::IVec2& t2)
343 {
344         return (t1 - t0).x() * (t2 - t0).y() - (t2 - t0).x() * (t1 - t0).y() == 0;
345 }
346
347 // returns true for cases where polygon is (almost) along xz or yz planes (normal.z < 0.1)
348 // \note[jarkko] Doesn't have to be accurate, just to detect some obviously bad cases
349 bool twoPointClippedTriangleInvisible(const tcu::Vec3& p, const tcu::IVec3& dir1, const tcu::IVec3& dir2)
350 {
351         // fixed-point-like coords
352         const deInt64                                   fixedScale      = 64;
353         const deInt64                                   farValue        = 1024;
354         const tcu::Vector<deInt64, 3>   d1                      = tcu::Vector<deInt64, 3>(dir1.x(), dir1.y(), dir1.z());
355         const tcu::Vector<deInt64, 3>   d2                      = tcu::Vector<deInt64, 3>(dir2.x(), dir2.y(), dir2.z());
356         const tcu::Vector<deInt64, 3>   pfixed          = tcu::Vector<deInt64, 3>(deFloorFloatToInt32(p.x() * fixedScale), deFloorFloatToInt32(p.y() * fixedScale), deFloorFloatToInt32(p.z() * fixedScale));
357         const tcu::Vector<deInt64, 3>   normalDir       = tcu::cross(d1*farValue - pfixed, d2*farValue - pfixed);
358         const deInt64                                   normalLen2      = tcu::lengthSquared(normalDir);
359
360         return (normalDir.z() * normalDir.z() - normalLen2/100) < 0;
361 }
362
363 std::string genClippingPointInfoString(const tcu::Vec4& p)
364 {
365         std::ostringstream msg;
366
367         if (p.x() < -p.w())             msg << "\t(-X clip)";
368         if (p.x() >  p.w())             msg << "\t(+X clip)";
369         if (p.y() < -p.w())             msg << "\t(-Y clip)";
370         if (p.y() >  p.w())             msg << "\t(+Y clip)";
371         if (p.z() < -p.w())             msg << "\t(-Z clip)";
372         if (p.z() >  p.w())             msg << "\t(+Z clip)";
373
374         return msg.str();
375 }
376
377 std::string genColorString(const tcu::Vec4& p)
378 {
379         const tcu::Vec4 white   (1.0f, 1.0f, 1.0f, 1.0f);
380         const tcu::Vec4 red             (1.0f, 0.0f, 0.0f, 1.0f);
381         const tcu::Vec4 yellow  (1.0f, 1.0f, 0.0f, 1.0f);
382         const tcu::Vec4 blue    (0.0f, 0.0f, 1.0f, 1.0f);
383
384         if (p == white)         return "(white)";
385         if (p == red)           return "(red)";
386         if (p == yellow)        return "(yellow)";
387         if (p == blue)          return "(blue)";
388         return "";
389 }
390
391 class PositionColorShader : public sglr::ShaderProgram
392 {
393 public:
394         enum
395         {
396                 VARYINGLOC_COLOR = 0
397         };
398
399                         PositionColorShader (void);
400
401         void    shadeVertices           (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const;
402         void    shadeFragments          (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const;
403 };
404
405 PositionColorShader::PositionColorShader (void)
406         : sglr::ShaderProgram(sglr::pdec::ShaderProgramDeclaration()
407                                                         << sglr::pdec::VertexAttribute("a_position", rr::GENERICVECTYPE_FLOAT)
408                                                         << sglr::pdec::VertexAttribute("a_color", rr::GENERICVECTYPE_FLOAT)
409                                                         << sglr::pdec::VertexAttribute("a_pointSize", rr::GENERICVECTYPE_FLOAT)
410                                                         << sglr::pdec::VertexToFragmentVarying(rr::GENERICVECTYPE_FLOAT)
411                                                         << sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT)
412                                                         << sglr::pdec::VertexSource(shaderSourceVertex)
413                                                         << sglr::pdec::FragmentSource(shaderSourceFragment))
414 {
415 }
416
417 void PositionColorShader::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
418 {
419         for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
420         {
421                 const int positionAttrLoc = 0;
422                 const int colorAttrLoc = 1;
423                 const int pointSizeAttrLoc = 2;
424
425                 rr::VertexPacket& packet = *packets[packetNdx];
426
427                 // Transform to position
428                 packet.position = rr::readVertexAttribFloat(inputs[positionAttrLoc], packet.instanceNdx, packet.vertexNdx);
429
430                 // output point size
431                 packet.pointSize = rr::readVertexAttribFloat(inputs[pointSizeAttrLoc], packet.instanceNdx, packet.vertexNdx).x();
432
433                 // Pass color to FS
434                 packet.outputs[VARYINGLOC_COLOR] = rr::readVertexAttribFloat(inputs[colorAttrLoc], packet.instanceNdx, packet.vertexNdx);
435         }
436 }
437
438 void PositionColorShader::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
439 {
440         for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
441         {
442                 rr::FragmentPacket& packet = packets[packetNdx];
443
444                 for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
445                         rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, rr::readVarying<float>(packet, context, VARYINGLOC_COLOR, fragNdx));
446         }
447 }
448
449 class RenderTestCase : public TestCase
450 {
451 public:
452                                         RenderTestCase  (Context& context, const char* name, const char* description);
453
454         virtual void    testRender              (void) = DE_NULL;
455         virtual void    init                    (void) { }
456
457         IterateResult   iterate                 (void);
458 };
459
460 RenderTestCase::RenderTestCase (Context& context, const char* name, const char* description)
461         : TestCase      (context, name, description)
462 {
463 }
464
465 RenderTestCase::IterateResult RenderTestCase::iterate (void)
466 {
467         const int width  = m_context.getRenderTarget().getWidth();
468         const int height = m_context.getRenderTarget().getHeight();
469
470         m_testCtx.getLog() << TestLog::Message << "Render target size: " << width << "x" << height << TestLog::EndMessage;
471         if (width < TEST_CANVAS_SIZE || height < TEST_CANVAS_SIZE)
472                 throw tcu::NotSupportedError(std::string("Render target size must be at least ") + de::toString(TEST_CANVAS_SIZE) + "x" + de::toString(TEST_CANVAS_SIZE));
473
474         m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass"); // success by default
475         testRender();
476
477         return STOP;
478 }
479
480 class PointCase : public RenderTestCase
481 {
482 public:
483                                                                         PointCase       (Context& context, const char* name, const char* description, const tcu::Vec4* pointsBegin, const tcu::Vec4* pointsEnd, float pointSize, const rr::WindowRectangle& viewport);
484
485         void                                                    init            (void);
486         void                                                    testRender      (void);
487
488 private:
489         const std::vector<tcu::Vec4>    m_points;
490         const float                                             m_pointSize;
491         const rr::WindowRectangle               m_viewport;
492 };
493
494 PointCase::PointCase (Context& context, const char* name, const char* description, const tcu::Vec4* pointsBegin, const tcu::Vec4* pointsEnd, float pointSize, const rr::WindowRectangle& viewport)
495         : RenderTestCase(context, name, description)
496         , m_points              (pointsBegin, pointsEnd)
497         , m_pointSize   (pointSize)
498         , m_viewport    (viewport)
499 {
500 }
501
502 void PointCase::init (void)
503 {
504         const glw::Functions& gl = m_context.getRenderContext().getFunctions();
505         checkPointSize (gl, m_pointSize);
506 }
507
508 void PointCase::testRender (void)
509 {
510         using tcu::TestLog;
511
512         const int numSamples                    = de::max(m_context.getRenderTarget().getNumSamples(), 1);
513
514         tcu::TestLog&                                   log                     = m_testCtx.getLog();
515         sglr::GLContext                                 glesContext     (m_context.getRenderContext(), log, 0, tcu::IVec4(0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE));
516         sglr::ReferenceContextLimits    limits;
517         sglr::ReferenceContextBuffers   buffers         (m_context.getRenderTarget().getPixelFormat(), m_context.getRenderTarget().getDepthBits(), 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE, numSamples);
518         sglr::ReferenceContext                  refContext      (limits, buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
519         PositionColorShader                             program;
520         tcu::Surface                                    testSurface     (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
521         tcu::Surface                                    refSurface      (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
522         sglr::Context*                                  contexts[2] = {&glesContext, &refContext};
523         tcu::Surface*                                   surfaces[2] = {&testSurface, &refSurface};
524
525         // log the purpose of the test
526         log << TestLog::Message << "Viewport: left=" << m_viewport.left << "\tbottom=" << m_viewport.bottom << "\twidth=" << m_viewport.width << "\theight=" << m_viewport.height << TestLog::EndMessage;
527         log << TestLog::Message << "Rendering points with point size " << m_pointSize << ". Coordinates:" << TestLog::EndMessage;
528         for (size_t ndx = 0; ndx < m_points.size(); ++ndx)
529                 log << TestLog::Message
530                                 << "\tx=" << m_points[ndx].x()
531                                 << "\ty=" << m_points[ndx].y()
532                                 << "\tz=" << m_points[ndx].z()
533                                 << "\tw=" << m_points[ndx].w()
534                                 << "\t" << genClippingPointInfoString(m_points[ndx])
535                                 << TestLog::EndMessage;
536
537         for (int contextNdx = 0; contextNdx < 2; ++contextNdx)
538         {
539                 sglr::Context&  ctx                             = *contexts[contextNdx];
540                 tcu::Surface&   dstSurface              = *surfaces[contextNdx];
541                 const deUint32  programId               = ctx.createProgram(&program);
542                 const GLint             positionLoc             = ctx.getAttribLocation(programId, "a_position");
543                 const GLint             pointSizeLoc    = ctx.getAttribLocation(programId, "a_pointSize");
544                 const GLint             colorLoc                = ctx.getAttribLocation(programId, "a_color");
545
546                 ctx.clearColor                                  (0, 0, 0, 1);
547                 ctx.clearDepthf                                 (1.0f);
548                 ctx.clear                                               (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
549                 ctx.viewport                                    (m_viewport.left, m_viewport.bottom, m_viewport.width, m_viewport.height);
550                 ctx.useProgram                                  (programId);
551                 ctx.enableVertexAttribArray             (positionLoc);
552                 ctx.vertexAttribPointer                 (positionLoc, 4, GL_FLOAT, GL_FALSE, 0, &m_points[0]);
553                 ctx.vertexAttrib1f                              (pointSizeLoc, m_pointSize);
554                 ctx.vertexAttrib4f                              (colorLoc, 1.0f, 1.0f, 1.0f, 1.0f);
555                 ctx.drawArrays                                  (GL_POINTS, 0, (glw::GLsizei)m_points.size());
556                 ctx.disableVertexAttribArray    (positionLoc);
557                 ctx.useProgram                                  (0);
558                 ctx.deleteProgram                               (programId);
559                 ctx.finish                                              ();
560
561                 ctx.readPixels(dstSurface, 0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
562         }
563
564         // do the comparison
565         {
566                 tcu::Surface            diffMask                (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
567                 const int                       kernelRadius    = 1;
568                 int                                     faultyPixels;
569
570                 log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
571                 log << TestLog::Message << "Deviation within radius of " << kernelRadius << " is allowed." << TestLog::EndMessage;
572
573                 faultyPixels = compareBlackNonBlackImages(log, testSurface.getAccess(), refSurface.getAccess(), diffMask.getAccess(), kernelRadius);
574
575                 if (faultyPixels > 0)
576                 {
577                         log << TestLog::ImageSet("Images", "Image comparison")
578                                 << TestLog::Image("TestImage", "Test image", testSurface.getAccess())
579                                 << TestLog::Image("ReferenceImage", "Reference image", refSurface.getAccess())
580                                 << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
581                                 << TestLog::EndImageSet
582                                 << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
583
584                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
585                 }
586         }
587 }
588
589 class LineRenderTestCase : public RenderTestCase
590 {
591 public:
592         struct ColoredLineData
593         {
594                 tcu::Vec4 p0;
595                 tcu::Vec4 c0;
596                 tcu::Vec4 p1;
597                 tcu::Vec4 c1;
598         };
599
600         struct ColorlessLineData
601         {
602                 tcu::Vec4 p0;
603                 tcu::Vec4 p1;
604         };
605                                                                                 LineRenderTestCase              (Context& context, const char* name, const char* description, const ColoredLineData*   linesBegin, const ColoredLineData*   linesEnd, float lineWidth, const rr::WindowRectangle& viewport);
606                                                                                 LineRenderTestCase              (Context& context, const char* name, const char* description, const ColorlessLineData* linesBegin, const ColorlessLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport);
607
608         virtual void                                            verifyImage                             (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess) = DE_NULL;
609         void                                                            init                                    (void);
610         void                                                            testRender                              (void);
611
612 private:
613         std::vector<ColoredLineData>            convertToColoredLines   (const ColorlessLineData* linesBegin, const ColorlessLineData* linesEnd);
614
615         const std::vector<ColoredLineData>      m_lines;
616         const float                                                     m_lineWidth;
617         const rr::WindowRectangle                       m_viewport;
618 };
619
620 LineRenderTestCase::LineRenderTestCase (Context& context, const char* name, const char* description, const ColoredLineData* linesBegin, const ColoredLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport)
621         : RenderTestCase        (context, name, description)
622         , m_lines                       (linesBegin, linesEnd)
623         , m_lineWidth           (lineWidth)
624         , m_viewport            (viewport)
625 {
626 }
627
628 LineRenderTestCase::LineRenderTestCase (Context& context, const char* name, const char* description, const ColorlessLineData* linesBegin, const ColorlessLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport)
629         : RenderTestCase        (context, name, description)
630         , m_lines                       (convertToColoredLines(linesBegin, linesEnd))
631         , m_lineWidth           (lineWidth)
632         , m_viewport            (viewport)
633 {
634 }
635
636 void LineRenderTestCase::init (void)
637 {
638         const glw::Functions& gl = m_context.getRenderContext().getFunctions();
639         checkLineWidth (gl, m_lineWidth);
640 }
641
642 void LineRenderTestCase::testRender (void)
643 {
644         using tcu::TestLog;
645
646         const int numSamples                    = de::max(m_context.getRenderTarget().getNumSamples(), 1);
647         const int verticesPerLine               = 2;
648
649         tcu::TestLog&                                   log                     = m_testCtx.getLog();
650         sglr::GLContext                                 glesContext     (m_context.getRenderContext(), log, 0, tcu::IVec4(0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE));
651         sglr::ReferenceContextLimits    limits;
652         sglr::ReferenceContextBuffers   buffers         (m_context.getRenderTarget().getPixelFormat(), m_context.getRenderTarget().getDepthBits(), 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE, numSamples);
653         sglr::ReferenceContext                  refContext      (limits, buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
654         PositionColorShader                             program;
655         tcu::Surface                                    testSurface     (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
656         tcu::Surface                                    refSurface      (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
657         sglr::Context*                                  contexts[2] = {&glesContext, &refContext};
658         tcu::Surface*                                   surfaces[2] = {&testSurface, &refSurface};
659
660         // log the purpose of the test
661         log << TestLog::Message << "Viewport: left=" << m_viewport.left << "\tbottom=" << m_viewport.bottom << "\twidth=" << m_viewport.width << "\theight=" << m_viewport.height << TestLog::EndMessage;
662         log << TestLog::Message << "Rendering lines with line width " << m_lineWidth << ". Coordinates:" << TestLog::EndMessage;
663         for (size_t ndx = 0; ndx < m_lines.size(); ++ndx)
664         {
665                 const std::string fromProperties = genClippingPointInfoString(m_lines[ndx].p0);
666                 const std::string toProperties   = genClippingPointInfoString(m_lines[ndx].p1);
667
668                 log << TestLog::Message << "\tfrom (x=" << m_lines[ndx].p0.x() << "\ty=" << m_lines[ndx].p0.y() << "\tz=" << m_lines[ndx].p0.z() << "\tw=" << m_lines[ndx].p0.w() << ")\t" << fromProperties << TestLog::EndMessage;
669                 log << TestLog::Message << "\tto   (x=" << m_lines[ndx].p1.x() << "\ty=" << m_lines[ndx].p1.y() << "\tz=" << m_lines[ndx].p1.z() << "\tw=" << m_lines[ndx].p1.w() << ")\t" << toProperties   << TestLog::EndMessage;
670                 log << TestLog::Message << TestLog::EndMessage;
671         }
672
673         // render test image
674         for (int contextNdx = 0; contextNdx < 2; ++contextNdx)
675         {
676                 sglr::Context&  ctx                             = *contexts[contextNdx];
677                 tcu::Surface&   dstSurface              = *surfaces[contextNdx];
678                 const deUint32  programId               = ctx.createProgram(&program);
679                 const GLint             positionLoc             = ctx.getAttribLocation(programId, "a_position");
680                 const GLint             colorLoc                = ctx.getAttribLocation(programId, "a_color");
681
682                 ctx.clearColor                                  (0, 0, 0, 1);
683                 ctx.clearDepthf                                 (1.0f);
684                 ctx.clear                                               (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
685                 ctx.viewport                                    (m_viewport.left, m_viewport.bottom, m_viewport.width, m_viewport.height);
686                 ctx.useProgram                                  (programId);
687                 ctx.enableVertexAttribArray             (positionLoc);
688                 ctx.enableVertexAttribArray             (colorLoc);
689                 ctx.vertexAttribPointer                 (positionLoc,   4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_lines[0].p0);
690                 ctx.vertexAttribPointer                 (colorLoc,              4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_lines[0].c0);
691                 ctx.lineWidth                                   (m_lineWidth);
692                 ctx.drawArrays                                  (GL_LINES, 0, verticesPerLine * (glw::GLsizei)m_lines.size());
693                 ctx.disableVertexAttribArray    (positionLoc);
694                 ctx.disableVertexAttribArray    (colorLoc);
695                 ctx.useProgram                                  (0);
696                 ctx.deleteProgram                               (programId);
697                 ctx.finish                                              ();
698
699                 ctx.readPixels(dstSurface, 0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
700         }
701
702         // compare
703         verifyImage(testSurface.getAccess(), refSurface.getAccess());
704 }
705
706 std::vector<LineRenderTestCase::ColoredLineData> LineRenderTestCase::convertToColoredLines(const ColorlessLineData* linesBegin, const ColorlessLineData* linesEnd)
707 {
708         std::vector<ColoredLineData> ret;
709
710         for (const ColorlessLineData* it = linesBegin; it != linesEnd; ++it)
711         {
712                 ColoredLineData r;
713
714                 r.p0 = (*it).p0;
715                 r.c0 = tcu::Vec4(1, 1, 1, 1);
716                 r.p1 = (*it).p1;
717                 r.c1 = tcu::Vec4(1, 1, 1, 1);
718
719                 ret.push_back(r);
720         }
721
722         return ret;
723 }
724
725 class LineCase : public LineRenderTestCase
726 {
727 public:
728                                 LineCase                        (Context& context, const char* name, const char* description, const LineRenderTestCase::ColorlessLineData* linesBegin, const LineRenderTestCase::ColorlessLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport, int searchKernelSize = 1);
729
730         void            verifyImage                     (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess);
731
732 private:
733         const int       m_searchKernelSize;
734 };
735
736 LineCase::LineCase (Context& context, const char* name, const char* description, const LineRenderTestCase::ColorlessLineData* linesBegin, const LineRenderTestCase::ColorlessLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport, int searchKernelSize)
737         : LineRenderTestCase    (context, name, description, linesBegin, linesEnd, lineWidth, viewport)
738         , m_searchKernelSize    (searchKernelSize)
739 {
740 }
741
742 void LineCase::verifyImage (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess)
743 {
744         const int       faultyLimit = 6;
745         int                     faultyPixels;
746
747         tcu::TestLog&           log                     = m_testCtx.getLog();
748         tcu::Surface            diffMask        (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
749
750         log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
751         log << TestLog::Message << "Deviation within radius of " << m_searchKernelSize << " is allowed." << TestLog::EndMessage;
752         log << TestLog::Message << faultyLimit << " faulty pixels are allowed." << TestLog::EndMessage;
753
754         faultyPixels = compareBlackNonBlackImages(log, testImageAccess, referenceImageAccess, diffMask.getAccess(), m_searchKernelSize);
755
756         if (faultyPixels > faultyLimit)
757         {
758                 log << TestLog::ImageSet("Images", "Image comparison")
759                         << TestLog::Image("TestImage", "Test image", testImageAccess)
760                         << TestLog::Image("ReferenceImage", "Reference image", referenceImageAccess)
761                         << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
762                         << TestLog::EndImageSet
763                         << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
764
765                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
766         }
767 }
768
769 class ColoredLineCase : public LineRenderTestCase
770 {
771 public:
772         ColoredLineCase (Context& context, const char* name, const char* description, const LineRenderTestCase::ColoredLineData* linesBegin, const LineRenderTestCase::ColoredLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport);
773
774         void verifyImage (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess);
775 };
776
777 ColoredLineCase::ColoredLineCase (Context& context, const char* name, const char* description, const LineRenderTestCase::ColoredLineData* linesBegin, const LineRenderTestCase::ColoredLineData* linesEnd, float lineWidth, const rr::WindowRectangle& viewport)
778         : LineRenderTestCase (context, name, description, linesBegin, linesEnd, lineWidth, viewport)
779 {
780 }
781
782 void ColoredLineCase::verifyImage (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess)
783 {
784         const bool              msaa    = m_context.getRenderTarget().getNumSamples() > 1;
785         tcu::TestLog&   log             = m_testCtx.getLog();
786
787         if (!msaa)
788         {
789                 const int               kernelRadius    = 1;
790                 const int               faultyLimit             = 6;
791                 int                             faultyPixels;
792                 tcu::Surface    diffMask                (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
793
794                 log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
795                 log << TestLog::Message << "Deviation within radius of " << kernelRadius << " is allowed." << TestLog::EndMessage;
796                 log << TestLog::Message << faultyLimit << " faulty pixels are allowed." << TestLog::EndMessage;
797
798                 faultyPixels = compareColoredImages(log, testImageAccess, referenceImageAccess, diffMask.getAccess(), kernelRadius);
799
800                 if (faultyPixels > faultyLimit)
801                 {
802                         log << TestLog::ImageSet("Images", "Image comparison")
803                                 << TestLog::Image("TestImage", "Test image", testImageAccess)
804                                 << TestLog::Image("ReferenceImage", "Reference image", referenceImageAccess)
805                                 << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
806                                 << TestLog::EndImageSet
807                                 << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
808
809                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
810                 }
811         }
812         else
813         {
814                 const float threshold = 0.3f;
815                 if (!tcu::fuzzyCompare(log, "Images", "", referenceImageAccess, testImageAccess, threshold, tcu::COMPARE_LOG_ON_ERROR))
816                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
817         }
818 }
819
820 class TriangleCaseBase : public RenderTestCase
821 {
822 public:
823         struct TriangleData
824         {
825                 tcu::Vec4 p0;
826                 tcu::Vec4 c0;
827                 tcu::Vec4 p1;
828                 tcu::Vec4 c1;
829                 tcu::Vec4 p2;
830                 tcu::Vec4 c2;
831         };
832
833                                                                                 TriangleCaseBase        (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport);
834
835         virtual void                                            verifyImage                     (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess) = DE_NULL;
836         void                                                            testRender                      (void);
837
838 private:
839         const std::vector<TriangleData>         m_polys;
840         const rr::WindowRectangle                       m_viewport;
841 };
842
843 TriangleCaseBase::TriangleCaseBase (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport)
844         : RenderTestCase(context, name, description)
845         , m_polys               (polysBegin, polysEnd)
846         , m_viewport    (viewport)
847 {
848 }
849
850 void TriangleCaseBase::testRender (void)
851 {
852         using tcu::TestLog;
853
854         const int numSamples                    = de::max(m_context.getRenderTarget().getNumSamples(), 1);
855         const int verticesPerTriangle   = 3;
856
857         tcu::TestLog&                                   log                     = m_testCtx.getLog();
858         sglr::GLContext                                 glesContext     (m_context.getRenderContext(), log, 0, tcu::IVec4(0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE));
859         sglr::ReferenceContextLimits    limits;
860         sglr::ReferenceContextBuffers   buffers         (m_context.getRenderTarget().getPixelFormat(), m_context.getRenderTarget().getDepthBits(), 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE, numSamples);
861         sglr::ReferenceContext                  refContext      (limits, buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
862         PositionColorShader                             program;
863         tcu::Surface                                    testSurface     (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
864         tcu::Surface                                    refSurface      (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
865         sglr::Context*                                  contexts[2] = {&glesContext, &refContext};
866         tcu::Surface*                                   surfaces[2] = {&testSurface, &refSurface};
867
868         // log the purpose of the test
869         log << TestLog::Message << "Viewport: left=" << m_viewport.left << "\tbottom=" << m_viewport.bottom << "\twidth=" << m_viewport.width << "\theight=" << m_viewport.height << TestLog::EndMessage;
870         log << TestLog::Message << "Rendering triangles. Coordinates:" << TestLog::EndMessage;
871         for (size_t ndx = 0; ndx < m_polys.size(); ++ndx)
872         {
873                 const std::string v0Properties = genClippingPointInfoString(m_polys[ndx].p0);
874                 const std::string v1Properties = genClippingPointInfoString(m_polys[ndx].p1);
875                 const std::string v2Properties = genClippingPointInfoString(m_polys[ndx].p2);
876                 const std::string c0Properties = genColorString(m_polys[ndx].c0);
877                 const std::string c1Properties = genColorString(m_polys[ndx].c1);
878                 const std::string c2Properties = genColorString(m_polys[ndx].c2);
879
880                 log << TestLog::Message << "\tv0 (x=" << m_polys[ndx].p0.x() << "\ty=" << m_polys[ndx].p0.y() << "\tz=" << m_polys[ndx].p0.z() << "\tw=" << m_polys[ndx].p0.w() << ")\t" << v0Properties << "\t" << c0Properties << TestLog::EndMessage;
881                 log << TestLog::Message << "\tv1 (x=" << m_polys[ndx].p1.x() << "\ty=" << m_polys[ndx].p1.y() << "\tz=" << m_polys[ndx].p1.z() << "\tw=" << m_polys[ndx].p1.w() << ")\t" << v1Properties << "\t" << c1Properties << TestLog::EndMessage;
882                 log << TestLog::Message << "\tv2 (x=" << m_polys[ndx].p2.x() << "\ty=" << m_polys[ndx].p2.y() << "\tz=" << m_polys[ndx].p2.z() << "\tw=" << m_polys[ndx].p2.w() << ")\t" << v2Properties << "\t" << c2Properties << TestLog::EndMessage;
883                 log << TestLog::Message << TestLog::EndMessage;
884         }
885
886         // render test image
887         for (int contextNdx = 0; contextNdx < 2; ++contextNdx)
888         {
889                 sglr::Context&  ctx                             = *contexts[contextNdx];
890                 tcu::Surface&   dstSurface              = *surfaces[contextNdx];
891                 const deUint32  programId               = ctx.createProgram(&program);
892                 const GLint             positionLoc             = ctx.getAttribLocation(programId, "a_position");
893                 const GLint             colorLoc                = ctx.getAttribLocation(programId, "a_color");
894
895                 ctx.clearColor                                  (0, 0, 0, 1);
896                 ctx.clearDepthf                                 (1.0f);
897                 ctx.clear                                               (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
898                 ctx.viewport                                    (m_viewport.left, m_viewport.bottom, m_viewport.width, m_viewport.height);
899                 ctx.useProgram                                  (programId);
900                 ctx.enableVertexAttribArray             (positionLoc);
901                 ctx.enableVertexAttribArray             (colorLoc);
902                 ctx.vertexAttribPointer                 (positionLoc,   4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_polys[0].p0);
903                 ctx.vertexAttribPointer                 (colorLoc,              4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_polys[0].c0);
904                 ctx.drawArrays                                  (GL_TRIANGLES, 0, verticesPerTriangle * (glw::GLsizei)m_polys.size());
905                 ctx.disableVertexAttribArray    (positionLoc);
906                 ctx.disableVertexAttribArray    (colorLoc);
907                 ctx.useProgram                                  (0);
908                 ctx.deleteProgram                               (programId);
909                 ctx.finish                                              ();
910
911                 ctx.readPixels(dstSurface, 0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
912         }
913
914         verifyImage(testSurface.getAccess(), refSurface.getAccess());
915 }
916
917 class TriangleCase : public TriangleCaseBase
918 {
919 public:
920                         TriangleCase    (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport);
921
922         void    verifyImage             (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess);
923 };
924
925 TriangleCase::TriangleCase (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport)
926         : TriangleCaseBase(context, name, description, polysBegin, polysEnd, viewport)
927 {
928 }
929
930 void TriangleCase::verifyImage (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess)
931 {
932         const int                       kernelRadius    = 1;
933         const int                       faultyLimit             = 6;
934         tcu::TestLog&           log                             = m_testCtx.getLog();
935         tcu::Surface            diffMask                (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
936         int                                     faultyPixels;
937
938         log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
939         log << TestLog::Message << "Deviation within radius of " << kernelRadius << " is allowed." << TestLog::EndMessage;
940         log << TestLog::Message << faultyLimit << " faulty pixels are allowed." << TestLog::EndMessage;
941
942         faultyPixels = compareBlackNonBlackImages(log, testImageAccess, referenceImageAccess, diffMask.getAccess(), kernelRadius);
943
944         if (faultyPixels > faultyLimit)
945         {
946                 log << TestLog::ImageSet("Images", "Image comparison")
947                         << TestLog::Image("TestImage", "Test image", testImageAccess)
948                         << TestLog::Image("ReferenceImage", "Reference image", referenceImageAccess)
949                         << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
950                         << TestLog::EndImageSet
951                         << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
952
953                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
954         }
955 }
956
957 class TriangleAttributeCase : public TriangleCaseBase
958 {
959 public:
960                         TriangleAttributeCase   (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport);
961
962         void    verifyImage                             (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess);
963 };
964
965 TriangleAttributeCase::TriangleAttributeCase (Context& context, const char* name, const char* description, const TriangleData* polysBegin, const TriangleData* polysEnd, const rr::WindowRectangle& viewport)
966         : TriangleCaseBase(context, name, description, polysBegin, polysEnd, viewport)
967 {
968 }
969
970 void TriangleAttributeCase::verifyImage (const tcu::ConstPixelBufferAccess& testImageAccess, const tcu::ConstPixelBufferAccess& referenceImageAccess)
971 {
972         const bool              msaa    = m_context.getRenderTarget().getNumSamples() > 1;
973         tcu::TestLog&   log             = m_testCtx.getLog();
974
975         if (!msaa)
976         {
977                 const int               kernelRadius    = 1;
978                 const int               faultyLimit             = 6;
979                 int                             faultyPixels;
980                 tcu::Surface    diffMask                (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
981
982                 log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
983                 log << TestLog::Message << "Deviation within radius of " << kernelRadius << " is allowed." << TestLog::EndMessage;
984                 log << TestLog::Message << faultyLimit << " faulty pixels are allowed." << TestLog::EndMessage;
985                 faultyPixels = compareColoredImages(log, testImageAccess, referenceImageAccess, diffMask.getAccess(), kernelRadius);
986
987                 if (faultyPixels > faultyLimit)
988                 {
989                         log << TestLog::ImageSet("Images", "Image comparison")
990                                 << TestLog::Image("TestImage", "Test image", testImageAccess)
991                                 << TestLog::Image("ReferenceImage", "Reference image", referenceImageAccess)
992                                 << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
993                                 << TestLog::EndImageSet
994                                 << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
995
996                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
997                 }
998         }
999         else
1000         {
1001                 const float threshold = 0.3f;
1002                 if (!tcu::fuzzyCompare(log, "Images", "", referenceImageAccess, testImageAccess, threshold, tcu::COMPARE_LOG_ON_ERROR))
1003                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
1004         }
1005 }
1006
1007 class FillTest : public RenderTestCase
1008 {
1009 public:
1010                                                                 FillTest        (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport);
1011
1012         virtual void                            render          (sglr::Context& ctx) = DE_NULL;
1013         void                                            testRender      (void);
1014
1015 protected:
1016         const rr::WindowRectangle       m_viewport;
1017 };
1018
1019 FillTest::FillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport)
1020         : RenderTestCase(context, name, description)
1021         , m_viewport    (viewport)
1022 {
1023 }
1024
1025 void FillTest::testRender (void)
1026 {
1027         using tcu::TestLog;
1028
1029         const int                                               numSamples      = 1;
1030
1031         tcu::TestLog&                                   log                     = m_testCtx.getLog();
1032         sglr::GLContext                                 glesContext     (m_context.getRenderContext(), log, 0, tcu::IVec4(0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE));
1033         sglr::ReferenceContextLimits    limits;
1034         sglr::ReferenceContextBuffers   buffers         (m_context.getRenderTarget().getPixelFormat(), m_context.getRenderTarget().getDepthBits(), 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE, numSamples);
1035         sglr::ReferenceContext                  refContext      (limits, buffers.getColorbuffer(), buffers.getDepthbuffer(), buffers.getStencilbuffer());
1036         tcu::Surface                                    testSurface     (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1037         tcu::Surface                                    refSurface      (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1038
1039         render(glesContext);
1040         glesContext.readPixels(testSurface, 0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1041
1042         render(refContext);
1043         refContext.readPixels(refSurface, 0, 0, TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1044
1045         // check overdraw
1046         {
1047                 bool                            overdrawOk;
1048                 tcu::Surface            outputImage(TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1049
1050                 log << TestLog::Message << "Checking for overdraw " << TestLog::EndMessage;
1051                 overdrawOk = checkHalfFilledImageOverdraw(log, m_context.getRenderTarget(), testSurface.getAccess(), outputImage.getAccess());
1052
1053                 if (!overdrawOk)
1054                 {
1055                         log << TestLog::ImageSet("Images", "Image comparison")
1056                                 << TestLog::Image("TestImage", "Test image", testSurface.getAccess())
1057                                 << TestLog::Image("InvalidPixels", "Invalid pixels", outputImage.getAccess())
1058                                 << TestLog::EndImageSet
1059                                 << tcu::TestLog::Message << "Got overdraw." << tcu::TestLog::EndMessage;
1060
1061                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got overdraw");
1062                 }
1063         }
1064
1065         // compare & check missing pixels
1066         {
1067                 const int                       kernelRadius    = 1;
1068                 tcu::Surface            diffMask                (TEST_CANVAS_SIZE, TEST_CANVAS_SIZE);
1069                 int                                     faultyPixels;
1070
1071                 log << TestLog::Message << "Comparing images... " << TestLog::EndMessage;
1072                 log << TestLog::Message << "Deviation within radius of " << kernelRadius << " is allowed." << TestLog::EndMessage;
1073
1074                 blitImageOnBlackSurface(refSurface.getAccess(), refSurface.getAccess()); // makes images look right in Candy
1075
1076                 faultyPixels = compareBlackNonBlackImages(log, testSurface.getAccess(), refSurface.getAccess(), diffMask.getAccess(), kernelRadius);
1077
1078                 if (faultyPixels > 0)
1079                 {
1080                         log << TestLog::ImageSet("Images", "Image comparison")
1081                                 << TestLog::Image("TestImage", "Test image", testSurface.getAccess())
1082                                 << TestLog::Image("ReferenceImage", "Reference image", refSurface.getAccess())
1083                                 << TestLog::Image("DifferenceMask", "Difference mask", diffMask.getAccess())
1084                                 << TestLog::EndImageSet
1085                                 << tcu::TestLog::Message << "Got " << faultyPixels << " faulty pixel(s)." << tcu::TestLog::EndMessage;
1086
1087                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Got faulty pixels");
1088                 }
1089         }
1090 }
1091
1092 class TriangleFillTest : public FillTest
1093 {
1094 public:
1095         struct FillTriangle
1096         {
1097                 tcu::Vec4 v0;
1098                 tcu::Vec4 c0;
1099                 tcu::Vec4 v1;
1100                 tcu::Vec4 c1;
1101                 tcu::Vec4 v2;
1102                 tcu::Vec4 c2;
1103         };
1104
1105                                                                 TriangleFillTest        (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport);
1106
1107         void                                            render                          (sglr::Context& ctx);
1108
1109 protected:
1110         std::vector<FillTriangle>       m_triangles;
1111 };
1112
1113 TriangleFillTest::TriangleFillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport)
1114         : FillTest(context, name, description, viewport)
1115 {
1116 }
1117
1118 void TriangleFillTest::render (sglr::Context& ctx)
1119 {
1120         const int                       verticesPerTriangle             = 3;
1121         PositionColorShader program;
1122         const deUint32          programId                               = ctx.createProgram(&program);
1123         const GLint                     positionLoc                             = ctx.getAttribLocation(programId, "a_position");
1124         const GLint                     colorLoc                                = ctx.getAttribLocation(programId, "a_color");
1125         tcu::TestLog&           log                                             = m_testCtx.getLog();
1126
1127         // log the purpose of the test
1128         log << TestLog::Message << "Viewport: left=" << m_viewport.left << "\tbottom=" << m_viewport.bottom << "\twidth=" << m_viewport.width << "\theight=" << m_viewport.height << TestLog::EndMessage;
1129         log << TestLog::Message << "Rendering triangles. Coordinates:" << TestLog::EndMessage;
1130         for (size_t ndx = 0; ndx < m_triangles.size(); ++ndx)
1131         {
1132                 const std::string v0Properties = genClippingPointInfoString(m_triangles[ndx].v0);
1133                 const std::string v1Properties = genClippingPointInfoString(m_triangles[ndx].v1);
1134                 const std::string v2Properties = genClippingPointInfoString(m_triangles[ndx].v2);
1135
1136                 log << TestLog::Message << "\tv0 (x=" << m_triangles[ndx].v0.x() << "\ty=" << m_triangles[ndx].v0.y() << "\tz=" << m_triangles[ndx].v0.z() << "\tw=" << m_triangles[ndx].v0.w() << ")\t" << v0Properties << TestLog::EndMessage;
1137                 log << TestLog::Message << "\tv1 (x=" << m_triangles[ndx].v1.x() << "\ty=" << m_triangles[ndx].v1.y() << "\tz=" << m_triangles[ndx].v1.z() << "\tw=" << m_triangles[ndx].v1.w() << ")\t" << v1Properties << TestLog::EndMessage;
1138                 log << TestLog::Message << "\tv2 (x=" << m_triangles[ndx].v2.x() << "\ty=" << m_triangles[ndx].v2.y() << "\tz=" << m_triangles[ndx].v2.z() << "\tw=" << m_triangles[ndx].v2.w() << ")\t" << v2Properties << TestLog::EndMessage;
1139                 log << TestLog::Message << TestLog::EndMessage;
1140         }
1141
1142         ctx.clearColor                                  (0, 0, 0, 1);
1143         ctx.clearDepthf                                 (1.0f);
1144         ctx.clear                                               (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1145         ctx.viewport                                    (m_viewport.left, m_viewport.bottom, m_viewport.width, m_viewport.height);
1146         ctx.useProgram                                  (programId);
1147         ctx.blendFunc                                   (GL_ONE, GL_ONE);
1148         ctx.enable                                              (GL_BLEND);
1149         ctx.enableVertexAttribArray             (positionLoc);
1150         ctx.enableVertexAttribArray             (colorLoc);
1151         ctx.vertexAttribPointer                 (positionLoc,   4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_triangles[0].v0);
1152         ctx.vertexAttribPointer                 (colorLoc,              4, GL_FLOAT, GL_FALSE, sizeof(GLfloat[8]), &m_triangles[0].c0);
1153         ctx.drawArrays                                  (GL_TRIANGLES, 0, verticesPerTriangle * (glw::GLsizei)m_triangles.size());
1154         ctx.disableVertexAttribArray    (positionLoc);
1155         ctx.disableVertexAttribArray    (colorLoc);
1156         ctx.useProgram                                  (0);
1157         ctx.deleteProgram                               (programId);
1158         ctx.finish                                              ();
1159 }
1160
1161 class QuadFillTest : public TriangleFillTest
1162 {
1163 public:
1164         QuadFillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport, const tcu::Vec3& d1, const tcu::Vec3& d2, const tcu::Vec3& center_ = tcu::Vec3(0, 0, 0));
1165 };
1166
1167 QuadFillTest::QuadFillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport, const tcu::Vec3& d1, const tcu::Vec3& d2, const tcu::Vec3& center_)
1168         : TriangleFillTest(context, name, description, viewport)
1169 {
1170         const float             radius          = 40000.0f;
1171         const tcu::Vec4 center          = tcu::Vec4(center_.x(), center_.y(), center_.z(), 1.0f);
1172         const tcu::Vec4 halfWhite       = tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f);
1173         const tcu::Vec4 halfRed         = tcu::Vec4(0.5f, 0.0f, 0.0f, 0.5f);
1174         const tcu::Vec4 e1                      = radius * tcu::Vec4(d1.x(), d1.y(), d1.z(), 0.0f);
1175         const tcu::Vec4 e2                      = radius * tcu::Vec4(d2.x(), d2.y(), d2.z(), 0.0f);
1176
1177         FillTriangle triangle1;
1178         FillTriangle triangle2;
1179
1180         triangle1.c0 = halfWhite;
1181         triangle1.c1 = halfWhite;
1182         triangle1.c2 = halfWhite;
1183         triangle1.v0 = center + e1 + e2;
1184         triangle1.v1 = center + e1 - e2;
1185         triangle1.v2 = center - e1 - e2;
1186         m_triangles.push_back(triangle1);
1187
1188         triangle2.c0 = halfRed;
1189         triangle2.c1 = halfRed;
1190         triangle2.c2 = halfRed;
1191         triangle2.v0 = center + e1 + e2;
1192         triangle2.v1 = center - e1 - e2;
1193         triangle2.v2 = center - e1 + e2;
1194         m_triangles.push_back(triangle2);
1195 }
1196
1197 class TriangleFanFillTest : public TriangleFillTest
1198 {
1199 public:
1200         TriangleFanFillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport);
1201 };
1202
1203 TriangleFanFillTest::TriangleFanFillTest (Context& context, const char* name, const char* description, const rr::WindowRectangle& viewport)
1204         : TriangleFillTest(context, name, description, viewport)
1205 {
1206         const float             radius                          = 70000.0f;
1207         const int               trianglesPerVisit       = 40;
1208         const tcu::Vec4 center                          = tcu::Vec4(0, 0, 0, 1.0f);
1209         const tcu::Vec4 halfWhite                       = tcu::Vec4(0.5f, 0.5f, 0.5f, 0.5f);
1210         const tcu::Vec4 oddSliceColor           = tcu::Vec4(0.0f, 0.0f, 0.5f, 0.0f);
1211
1212         // create a continuous surface that goes through all 6 clip planes
1213
1214         /*
1215                 *   /           /
1216                 *  /_ _ _ _ _  /x
1217                 * |           |  |
1218                 * |           | /
1219                 * |       / --xe /
1220                 * |      |    | /
1221                 * |_ _ _ e _ _|/
1222                 *
1223                 * e = enter
1224                 * x = exit
1225                 */
1226         const struct ClipPlaneVisit
1227         {
1228                 const tcu::Vec3 corner;
1229                 const tcu::Vec3 entryPoint;
1230                 const tcu::Vec3 exitPoint;
1231         } visits[] =
1232         {
1233                 { tcu::Vec3( 1, 1, 1),  tcu::Vec3( 0, 1, 1),    tcu::Vec3( 1, 0, 1) },
1234                 { tcu::Vec3( 1,-1, 1),  tcu::Vec3( 1, 0, 1),    tcu::Vec3( 1,-1, 0) },
1235                 { tcu::Vec3( 1,-1,-1),  tcu::Vec3( 1,-1, 0),    tcu::Vec3( 0,-1,-1) },
1236                 { tcu::Vec3(-1,-1,-1),  tcu::Vec3( 0,-1,-1),    tcu::Vec3(-1, 0,-1) },
1237                 { tcu::Vec3(-1, 1,-1),  tcu::Vec3(-1, 0,-1),    tcu::Vec3(-1, 1, 0) },
1238                 { tcu::Vec3(-1, 1, 1),  tcu::Vec3(-1, 1, 0),    tcu::Vec3( 0, 1, 1) },
1239         };
1240
1241         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(visits); ++ndx)
1242         {
1243                 const ClipPlaneVisit& visit = visits[ndx];
1244
1245                 for (int tri = 0; tri < trianglesPerVisit; ++tri)
1246                 {
1247                         tcu::Vec3 vertex0;
1248                         tcu::Vec3 vertex1;
1249
1250                         if (tri == 0) // first vertex is magic
1251                         {
1252                                 vertex0 = visit.entryPoint;
1253                         }
1254                         else
1255                         {
1256                                 const tcu::Vec3 v1 = visit.entryPoint - visit.corner;
1257                                 const tcu::Vec3 v2 = visit.exitPoint  - visit.corner;
1258
1259                                 vertex0 = visit.corner + tcu::normalize(tcu::mix(v1, v2, tcu::Vec3(float(tri)/trianglesPerVisit)));
1260                         }
1261
1262                         if (tri == trianglesPerVisit-1) // last vertex is magic
1263                         {
1264                                 vertex1 = visit.exitPoint;
1265                         }
1266                         else
1267                         {
1268                                 const tcu::Vec3 v1 = visit.entryPoint - visit.corner;
1269                                 const tcu::Vec3 v2 = visit.exitPoint  - visit.corner;
1270
1271                                 vertex1 = visit.corner + tcu::normalize(tcu::mix(v1, v2, tcu::Vec3(float(tri+1)/trianglesPerVisit)));
1272                         }
1273
1274                         // write vec out
1275                         {
1276                                 FillTriangle triangle;
1277
1278                                 triangle.c0 = (tri % 2) ? halfWhite : halfWhite + oddSliceColor;
1279                                 triangle.c1 = (tri % 2) ? halfWhite : halfWhite + oddSliceColor;
1280                                 triangle.c2 = (tri % 2) ? halfWhite : halfWhite + oddSliceColor;
1281                                 triangle.v0 = center;
1282                                 triangle.v1 = tcu::Vec4(vertex0.x() * radius, vertex0.y() * radius, vertex0.z() * radius, 1.0f);
1283                                 triangle.v2 = tcu::Vec4(vertex1.x() * radius, vertex1.y() * radius, vertex1.z() * radius, 1.0f);
1284
1285                                 m_triangles.push_back(triangle);
1286                         }
1287
1288                 }
1289         }
1290 }
1291
1292 class PointsTestGroup : public TestCaseGroup
1293 {
1294 public:
1295                         PointsTestGroup (Context& context);
1296
1297         void    init                    (void);
1298 };
1299
1300 PointsTestGroup::PointsTestGroup (Context& context)
1301         : TestCaseGroup(context, "point", "Point clipping tests")
1302 {
1303 }
1304
1305 void PointsTestGroup::init (void)
1306 {
1307         const float littleOverViewport = 1.0f + (2.0f / (TEST_CANVAS_SIZE)); // one pixel over the viewport edge in VIEWPORT_WHOLE, half pixels over in the reduced viewport.
1308
1309         const tcu::Vec4 viewportTestPoints[] =
1310         {
1311                 // in clip volume
1312                 tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),
1313                 tcu::Vec4( 0.1f,  0.1f,  0.1f,  1.0f),
1314                 tcu::Vec4(-0.1f,  0.1f, -0.1f,  1.0f),
1315                 tcu::Vec4(-0.1f, -0.1f,  0.1f,  1.0f),
1316                 tcu::Vec4( 0.1f, -0.1f, -0.1f,  1.0f),
1317
1318                 // in clip volume with w != 1
1319                 tcu::Vec4( 2.0f,  2.0f,  2.0f,  3.0f),
1320                 tcu::Vec4(-2.0f, -2.0f,  2.0f,  3.0f),
1321                 tcu::Vec4( 0.5f, -0.5f,  0.5f,  0.7f),
1322                 tcu::Vec4(-0.5f,  0.5f, -0.5f,  0.7f),
1323
1324                 // near the edge
1325                 tcu::Vec4(-2.0f, -2.0f,  0.0f,  2.2f),
1326                 tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.1f),
1327                 tcu::Vec4(-1.0f,  1.0f,  0.0f,  1.1f),
1328
1329                 // not in the volume but still between near and far planes
1330                 tcu::Vec4( 1.3f,  0.0f,  0.0f,  1.0f),
1331                 tcu::Vec4(-1.3f,  0.0f,  0.0f,  1.0f),
1332                 tcu::Vec4( 0.0f,  1.3f,  0.0f,  1.0f),
1333                 tcu::Vec4( 0.0f, -1.3f,  0.0f,  1.0f),
1334
1335                 tcu::Vec4(-1.3f, -1.3f,  0.0f,  1.0f),
1336                 tcu::Vec4(-1.3f,  1.3f,  0.0f,  1.0f),
1337                 tcu::Vec4( 1.3f,  1.3f,  0.0f,  1.0f),
1338                 tcu::Vec4( 1.3f, -1.3f,  0.0f,  1.0f),
1339
1340                 // outside the viewport, wide points have fragments in the viewport
1341                 tcu::Vec4( littleOverViewport,  littleOverViewport,  0.0f,  1.0f),
1342                 tcu::Vec4(               0.0f,  littleOverViewport,  0.0f,  1.0f),
1343                 tcu::Vec4( littleOverViewport,                0.0f,  0.0f,  1.0f),
1344         };
1345         const tcu::Vec4 depthTestPoints[] =
1346         {
1347                 // in clip volume
1348                 tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),
1349                 tcu::Vec4( 0.1f,  0.1f,  0.1f,  1.0f),
1350                 tcu::Vec4(-0.1f,  0.1f, -0.1f,  1.0f),
1351                 tcu::Vec4(-0.1f, -0.1f,  0.1f,  1.0f),
1352                 tcu::Vec4( 0.1f, -0.1f, -0.1f,  1.0f),
1353
1354                 // not between the near and the far planes. These should be clipped
1355                 tcu::Vec4( 0.1f,  0.0f,  1.1f,  1.0f),
1356                 tcu::Vec4(-0.1f,  0.0f, -1.1f,  1.0f),
1357                 tcu::Vec4(-0.0f, -0.1f,  1.1f,  1.0f),
1358                 tcu::Vec4( 0.0f,  0.1f, -1.1f,  1.0f)
1359         };
1360
1361         addChild(new PointCase(m_context, "point_z_clip",                                               "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          1.0f,   VIEWPORT_WHOLE));
1362         addChild(new PointCase(m_context, "point_z_clip_viewport_center",               "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          1.0f,   VIEWPORT_CENTER));
1363         addChild(new PointCase(m_context, "point_z_clip_viewport_corner",               "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          1.0f,   VIEWPORT_CORNER));
1364
1365         addChild(new PointCase(m_context, "point_clip_viewport_center",                 "point viewport clipping",              DE_ARRAY_BEGIN(viewportTestPoints), DE_ARRAY_END(viewportTestPoints),   1.0f,   VIEWPORT_CENTER));
1366         addChild(new PointCase(m_context, "point_clip_viewport_corner",                 "point viewport clipping",              DE_ARRAY_BEGIN(viewportTestPoints), DE_ARRAY_END(viewportTestPoints),   1.0f,   VIEWPORT_CORNER));
1367
1368         addChild(new PointCase(m_context, "wide_point_z_clip",                                  "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          5.0f,   VIEWPORT_WHOLE));
1369         addChild(new PointCase(m_context, "wide_point_z_clip_viewport_center",  "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          5.0f,   VIEWPORT_CENTER));
1370         addChild(new PointCase(m_context, "wide_point_z_clip_viewport_corner",  "point z clipping",                             DE_ARRAY_BEGIN(depthTestPoints),        DE_ARRAY_END(depthTestPoints),          5.0f,   VIEWPORT_CORNER));
1371
1372         addChild(new PointCase(m_context, "wide_point_clip",                                    "point viewport clipping",              DE_ARRAY_BEGIN(viewportTestPoints), DE_ARRAY_END(viewportTestPoints),   5.0f,   VIEWPORT_WHOLE));
1373         addChild(new PointCase(m_context, "wide_point_clip_viewport_center",    "point viewport clipping",              DE_ARRAY_BEGIN(viewportTestPoints), DE_ARRAY_END(viewportTestPoints),   5.0f,   VIEWPORT_CENTER));
1374         addChild(new PointCase(m_context, "wide_point_clip_viewport_corner",    "point viewport clipping",              DE_ARRAY_BEGIN(viewportTestPoints), DE_ARRAY_END(viewportTestPoints),   5.0f,   VIEWPORT_CORNER));
1375 }
1376
1377 class LinesTestGroup : public TestCaseGroup
1378 {
1379 public:
1380                         LinesTestGroup  (Context& context);
1381
1382         void    init                    (void);
1383 };
1384
1385 LinesTestGroup::LinesTestGroup (Context& context)
1386         : TestCaseGroup(context, "line", "Line clipping tests")
1387 {
1388 }
1389
1390 void LinesTestGroup::init (void)
1391 {
1392         const float littleOverViewport = 1.0f + (2.0f / (TEST_CANVAS_SIZE)); // one pixel over the viewport edge in VIEWPORT_WHOLE, half pixels over in the reduced viewport.
1393
1394         // lines
1395         const LineRenderTestCase::ColorlessLineData viewportTestLines[] =
1396         {
1397                 // from center to outside of viewport
1398                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 0.0f,  1.5f,  0.0f,  1.0f)},
1399                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4(-1.5f,  1.0f,  0.0f,  1.0f)},
1400                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4(-1.5f,  0.0f,  0.0f,  1.0f)},
1401                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 0.2f,  0.4f,  1.5f,  1.0f)},
1402                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4(-2.0f, -1.0f,  0.0f,  1.0f)},
1403                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 1.0f,  0.1f,  0.0f,  0.6f)},
1404
1405                 // from outside to inside of viewport
1406                 {tcu::Vec4( 1.5f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 0.8f, -0.2f,  0.0f,  1.0f)},
1407                 {tcu::Vec4( 0.0f, -1.5f,  0.0f,  1.0f),         tcu::Vec4( 0.9f, -0.7f,  0.0f,  1.0f)},
1408
1409                 // from outside to outside
1410                 {tcu::Vec4( 0.0f, -1.3f,  0.0f,  1.0f),         tcu::Vec4( 1.3f,  0.0f,  0.0f,  1.0f)},
1411
1412                 // outside the viewport, wide lines have fragments in the viewport
1413                 {tcu::Vec4(-0.8f,                      -littleOverViewport,  0.0f,  1.0f),      tcu::Vec4( 0.0f, -littleOverViewport,         0.0f,  1.0f)},
1414                 {tcu::Vec4(-littleOverViewport - 1.0f,  0.0f,                0.0f,  1.0f),      tcu::Vec4( 0.0f, -littleOverViewport - 1.0f,  0.0f,  1.0f)},
1415         };
1416         const LineRenderTestCase::ColorlessLineData depthTestLines[] =
1417         {
1418                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 1.3f,  1.0f,  2.0f,  1.0f)},
1419                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 1.3f, -1.0f,  2.0f,  1.0f)},
1420                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4(-1.0f, -1.1f, -2.0f,  1.0f)},
1421                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4(-1.0f,  1.1f, -2.0f,  1.0f)},
1422                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f),         tcu::Vec4( 1.0f,  0.1f,  2.0f,  0.6f)},
1423         };
1424         const LineRenderTestCase::ColorlessLineData longTestLines[] =
1425         {
1426                 {tcu::Vec4( -41000.0f,          -40000.0f,              -1000000.0f,    1.0f),  tcu::Vec4( 41000.0f,            40000.0f,               1000000.0f,     1.0f)},
1427                 {tcu::Vec4(  41000.0f,          -40000.0f,               1000000.0f,    1.0f),  tcu::Vec4(-41000.0f,            40000.0f,          -1000000.0f, 1.0f)},
1428                 {tcu::Vec4(  0.5f,                      -40000.0f,               100000.0f,             1.0f),  tcu::Vec4( 0.5f,                        40000.0f,          -100000.0f,  1.0f)},
1429                 {tcu::Vec4( -0.5f,                       40000.0f,               100000.0f,             1.0f),  tcu::Vec4(-0.5f,                   -40000.0f,      -100000.0f,  1.0f)},
1430         };
1431
1432         // line attribute clipping
1433         const tcu::Vec4 red                     (1.0f, 0.0f, 0.0f, 1.0f);
1434         const tcu::Vec4 yellow          (1.0f, 1.0f, 0.0f, 1.0f);
1435         const tcu::Vec4 lightBlue       (0.3f, 0.3f, 1.0f, 1.0f);
1436         const LineRenderTestCase::ColoredLineData colorTestLines[] =
1437         {
1438                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f), red,    tcu::Vec4( 1.3f,  1.0f,  2.0f,  1.0f),  yellow          },
1439                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f), red,    tcu::Vec4( 1.3f, -1.0f,  2.0f,  1.0f),  lightBlue       },
1440                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f), red,    tcu::Vec4(-1.0f, -1.0f, -2.0f,  1.0f),  yellow          },
1441                 {tcu::Vec4( 0.0f,  0.0f,  0.0f,  1.0f), red,    tcu::Vec4(-1.0f,  1.0f, -2.0f,  1.0f),  lightBlue       },
1442         };
1443
1444         // line clipping
1445         addChild(new LineCase(m_context, "line_z_clip",                                                 "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           1.0f,   VIEWPORT_WHOLE));
1446         addChild(new LineCase(m_context, "line_z_clip_viewport_center",                 "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           1.0f,   VIEWPORT_CENTER));
1447         addChild(new LineCase(m_context, "line_z_clip_viewport_corner",                 "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           1.0f,   VIEWPORT_CORNER));
1448
1449         addChild(new LineCase(m_context, "line_clip_viewport_center",                   "line viewport clipping",               DE_ARRAY_BEGIN(viewportTestLines),      DE_ARRAY_END(viewportTestLines),        1.0f,   VIEWPORT_CENTER));
1450         addChild(new LineCase(m_context, "line_clip_viewport_corner",                   "line viewport clipping",               DE_ARRAY_BEGIN(viewportTestLines),      DE_ARRAY_END(viewportTestLines),        1.0f,   VIEWPORT_CORNER));
1451
1452         addChild(new LineCase(m_context, "wide_line_z_clip",                                    "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           5.0f,   VIEWPORT_WHOLE));
1453         addChild(new LineCase(m_context, "wide_line_z_clip_viewport_center",    "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           5.0f,   VIEWPORT_CENTER));
1454         addChild(new LineCase(m_context, "wide_line_z_clip_viewport_corner",    "line z clipping",                              DE_ARRAY_BEGIN(depthTestLines),         DE_ARRAY_END(depthTestLines),           5.0f,   VIEWPORT_CORNER));
1455
1456         addChild(new LineCase(m_context, "wide_line_clip",                                              "line viewport clipping",               DE_ARRAY_BEGIN(viewportTestLines),      DE_ARRAY_END(viewportTestLines),        5.0f,   VIEWPORT_WHOLE));
1457         addChild(new LineCase(m_context, "wide_line_clip_viewport_center",              "line viewport clipping",               DE_ARRAY_BEGIN(viewportTestLines),      DE_ARRAY_END(viewportTestLines),        5.0f,   VIEWPORT_CENTER));
1458         addChild(new LineCase(m_context, "wide_line_clip_viewport_corner",              "line viewport clipping",               DE_ARRAY_BEGIN(viewportTestLines),      DE_ARRAY_END(viewportTestLines),        5.0f,   VIEWPORT_CORNER));
1459
1460         addChild(new LineCase(m_context, "long_line_clip",                                              "line viewport clipping",               DE_ARRAY_BEGIN(longTestLines),          DE_ARRAY_END(longTestLines),            1.0f,   VIEWPORT_WHOLE, 2));
1461         addChild(new LineCase(m_context, "long_wide_line_clip",                                 "line viewport clipping",               DE_ARRAY_BEGIN(longTestLines),          DE_ARRAY_END(longTestLines),            5.0f,   VIEWPORT_WHOLE, 2));
1462
1463         // line attribute clipping
1464         addChild(new ColoredLineCase(m_context, "line_attrib_clip",                             "line attribute clipping",              DE_ARRAY_BEGIN(colorTestLines),         DE_ARRAY_END(colorTestLines),           1.0f,   VIEWPORT_WHOLE));
1465         addChild(new ColoredLineCase(m_context, "wide_line_attrib_clip",                "line attribute clipping",              DE_ARRAY_BEGIN(colorTestLines),         DE_ARRAY_END(colorTestLines),           5.0f,   VIEWPORT_WHOLE));
1466 }
1467
1468 class PolysTestGroup : public TestCaseGroup
1469 {
1470 public:
1471                         PolysTestGroup  (Context& context);
1472
1473         void    init                    (void);
1474 };
1475
1476 PolysTestGroup::PolysTestGroup (Context& context)
1477         : TestCaseGroup(context, "polygon", "Polygon clipping tests")
1478 {
1479 }
1480
1481 void PolysTestGroup::init (void)
1482 {
1483         const float             large = 100000.0f;
1484         const float             offset = 0.9f;
1485         const tcu::Vec4 white   (1.0f, 1.0f, 1.0f, 1.0f);
1486         const tcu::Vec4 red             (1.0f, 0.0f, 0.0f, 1.0f);
1487         const tcu::Vec4 yellow  (1.0f, 1.0f, 0.0f, 1.0f);
1488         const tcu::Vec4 blue    (0.0f, 0.0f, 1.0f, 1.0f);
1489
1490         // basic cases
1491         {
1492                 const TriangleCase::TriangleData viewportPolys[] =
1493                 {
1494                         // one vertex clipped
1495                         {tcu::Vec4(-0.8f, -0.2f,  0.0f,  1.0f), white, tcu::Vec4(-0.8f,  0.2f,  0.0f,  1.0f), white, tcu::Vec4(-1.3f,  0.05f,  0.0f,  1.0f), white},
1496
1497                         // two vertices clipped
1498                         {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), white, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), white, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), white},
1499
1500                         // three vertices clipped
1501                         {tcu::Vec4(-1.1f,  0.6f,  0.0f,  1.0f), white, tcu::Vec4(-1.1f,  1.1f,  0.0f,  1.0f), white, tcu::Vec4(-0.6f,  1.1f,  0.0f,  1.0f), white},
1502                         {tcu::Vec4( 0.8f,  1.1f,  0.0f,  1.0f), white, tcu::Vec4( 0.95f,-1.1f,  0.0f,  1.0f), white, tcu::Vec4( 3.0f,  0.0f,  0.0f,  1.0f), white},
1503                 };
1504                 const TriangleCase::TriangleData depthPolys[] =
1505                 {
1506                         // one vertex clipped to Z+
1507                         {tcu::Vec4(-0.2f,  0.7f,  0.0f,  1.0f), white, tcu::Vec4( 0.2f,  0.7f,  0.0f,  1.0f), white, tcu::Vec4( 0.0f,  0.9f,  2.0f,  1.0f), white},
1508
1509                         // two vertices clipped to Z-
1510                         {tcu::Vec4( 0.9f, 0.4f,  -1.5f,  1.0f), white, tcu::Vec4( 0.9f, -0.4f, -1.5f,  1.0f), white, tcu::Vec4( 0.6f,  0.0f,  0.0f,  1.0f), white},
1511
1512                         // three vertices clipped
1513                         {tcu::Vec4(-0.9f, 0.6f,  -2.0f,  1.0f), white, tcu::Vec4(-0.9f, -0.6f, -2.0f,  1.0f), white, tcu::Vec4(-0.4f,  0.0f,  2.0f,  1.0f), white},
1514
1515                         // three vertices clipped by X, Y and Z
1516                         {tcu::Vec4( 0.0f, -1.2f,  0.0f,  1.0f), white, tcu::Vec4( 0.0f,  0.5f,  -1.5f, 1.0f), white, tcu::Vec4( 1.2f, -0.9f,  0.0f,  1.0f), white},
1517                 };
1518                 const TriangleCase::TriangleData largePolys[] =
1519                 {
1520                         // one vertex clipped
1521                         {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), white, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), white, tcu::Vec4( 0.0f, -large,  2.0f,  1.0f), white},
1522
1523                         // two vertices clipped
1524                         {tcu::Vec4( 0.5f, 0.5f,  0.0f,  1.0f), white, tcu::Vec4( large, 0.5f, 0.0f,  1.0f), white, tcu::Vec4( 0.5f,  large,  0.0f,  1.0f), white},
1525
1526                         // three vertices clipped
1527                         {tcu::Vec4(-0.9f, -large, 0.0f,  1.0f), white, tcu::Vec4(-1.1f, -large, 0.0f,  1.0f), white, tcu::Vec4(-0.9f,  large,  0.0f,  1.0f), white},
1528                 };
1529                 const TriangleCase::TriangleData largeDepthPolys[] =
1530                 {
1531                         // one vertex clipped
1532                         {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), white, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), white, tcu::Vec4( 0.0f, -large, large,  1.0f), white},
1533
1534                         // two vertices clipped
1535                         {tcu::Vec4( 0.5f, 0.5f,  0.0f,  1.0f), white, tcu::Vec4( 0.9f, large/2, -large,  1.0f), white, tcu::Vec4( large/4, 0.0f, -large,  1.0f), white},
1536
1537                         // three vertices clipped
1538                         {tcu::Vec4(-0.9f, large/4, large,  1.0f), white, tcu::Vec4(-0.5f, -large/4, -large,  1.0f), white, tcu::Vec4(-0.2f, large/4, large,  1.0f), white},
1539                 };
1540                 const TriangleCase::TriangleData attribPolys[] =
1541                 {
1542                         // one vertex clipped to edge, large
1543                         {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -large,  2.0f,  1.0f), blue},
1544
1545                         // two vertices clipped to edges
1546                         {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1547
1548                         // two vertices clipped to edges, with non-uniform w
1549                         {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1550
1551                         // three vertices clipped, large, Z
1552                         {tcu::Vec4(-0.9f, large/4, large,  1.0f), red, tcu::Vec4(-0.5f, -large/4, -large,  1.0f), yellow, tcu::Vec4(-0.2f, large/4, large,  1.0f), blue},
1553                 };
1554
1555                 addChild(new TriangleCase(m_context, "poly_clip_viewport_center",                       "polygon viewport clipping",    DE_ARRAY_BEGIN(viewportPolys),          DE_ARRAY_END(viewportPolys),    VIEWPORT_CENTER));
1556                 addChild(new TriangleCase(m_context, "poly_clip_viewport_corner",                       "polygon viewport clipping",    DE_ARRAY_BEGIN(viewportPolys),          DE_ARRAY_END(viewportPolys),    VIEWPORT_CORNER));
1557
1558                 addChild(new TriangleCase(m_context, "poly_z_clip",                                                     "polygon z clipping",                   DE_ARRAY_BEGIN(depthPolys),                     DE_ARRAY_END(depthPolys),               VIEWPORT_WHOLE));
1559                 addChild(new TriangleCase(m_context, "poly_z_clip_viewport_center",                     "polygon z clipping",                   DE_ARRAY_BEGIN(depthPolys),                     DE_ARRAY_END(depthPolys),               VIEWPORT_CENTER));
1560                 addChild(new TriangleCase(m_context, "poly_z_clip_viewport_corner",                     "polygon z clipping",                   DE_ARRAY_BEGIN(depthPolys),                     DE_ARRAY_END(depthPolys),               VIEWPORT_CORNER));
1561
1562                 addChild(new TriangleCase(m_context, "large_poly_clip_viewport_center",         "polygon viewport clipping",    DE_ARRAY_BEGIN(largePolys),                     DE_ARRAY_END(largePolys),               VIEWPORT_CENTER));
1563                 addChild(new TriangleCase(m_context, "large_poly_clip_viewport_corner",         "polygon viewport clipping",    DE_ARRAY_BEGIN(largePolys),                     DE_ARRAY_END(largePolys),               VIEWPORT_CORNER));
1564
1565                 addChild(new TriangleCase(m_context, "large_poly_z_clip",                                       "polygon z clipping",                   DE_ARRAY_BEGIN(largeDepthPolys),        DE_ARRAY_END(largeDepthPolys),  VIEWPORT_WHOLE));
1566                 addChild(new TriangleCase(m_context, "large_poly_z_clip_viewport_center",       "polygon z clipping",                   DE_ARRAY_BEGIN(largeDepthPolys),        DE_ARRAY_END(largeDepthPolys),  VIEWPORT_CENTER));
1567                 addChild(new TriangleCase(m_context, "large_poly_z_clip_viewport_corner",       "polygon z clipping",                   DE_ARRAY_BEGIN(largeDepthPolys),        DE_ARRAY_END(largeDepthPolys),  VIEWPORT_CORNER));
1568
1569                 addChild(new TriangleAttributeCase(m_context, "poly_attrib_clip",                                       "polygon clipping",             DE_ARRAY_BEGIN(attribPolys),            DE_ARRAY_END(attribPolys),              VIEWPORT_WHOLE));
1570                 addChild(new TriangleAttributeCase(m_context, "poly_attrib_clip_viewport_center",       "polygon clipping",             DE_ARRAY_BEGIN(attribPolys),            DE_ARRAY_END(attribPolys),              VIEWPORT_CENTER));
1571                 addChild(new TriangleAttributeCase(m_context, "poly_attrib_clip_viewport_corner",       "polygon clipping",             DE_ARRAY_BEGIN(attribPolys),            DE_ARRAY_END(attribPolys),              VIEWPORT_CORNER));
1572         }
1573
1574         // multiple polygons
1575         {
1576                 {
1577                         const TriangleAttributeCase::TriangleData polys[] =
1578                         {
1579                                 // one vertex clipped to edge
1580                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1581
1582                                 // two vertices clipped to edges
1583                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1584
1585                                 // two vertices clipped to edges, with non-uniform w
1586                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1587                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1588                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1589                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1590
1591                                 // three vertices clipped, Z
1592                                 {tcu::Vec4(-0.9f, offset/4, offset,  1.0f), red, tcu::Vec4(-0.5f, -offset/4, -offset,  1.0f), yellow, tcu::Vec4(-0.2f, offset/4, offset,  1.0f), blue},
1593                         };
1594
1595                         addChild(new TriangleAttributeCase(m_context, "multiple_0",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1596                         addChild(new TriangleAttributeCase(m_context, "multiple_0_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1597                         addChild(new TriangleAttributeCase(m_context, "multiple_0_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1598                 }
1599
1600                 {
1601                         const TriangleAttributeCase::TriangleData polys[] =
1602                         {
1603                                 // one vertex clipped to z
1604                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1605
1606                                 // two vertices clipped to edges
1607                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1608
1609                                 // two vertices clipped to edges, with non-uniform w
1610                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1611                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1612                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1613                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, 16.0f*tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1614                         };
1615
1616                         addChild(new TriangleAttributeCase(m_context, "multiple_1",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1617                         addChild(new TriangleAttributeCase(m_context, "multiple_1_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1618                         addChild(new TriangleAttributeCase(m_context, "multiple_1_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1619                 }
1620
1621                 {
1622                         const TriangleAttributeCase::TriangleData polys[] =
1623                         {
1624                                 // one vertex clipped to z
1625                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1626
1627                                 // two vertices clipped to edges
1628                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1629
1630                                 // two vertices clipped to edges
1631                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1632                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1633                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1634                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1635                         };
1636
1637                         addChild(new TriangleAttributeCase(m_context, "multiple_2",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1638                         addChild(new TriangleAttributeCase(m_context, "multiple_2_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1639                         addChild(new TriangleAttributeCase(m_context, "multiple_2_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1640                 }
1641
1642                 {
1643                         const TriangleAttributeCase::TriangleData polys[] =
1644                         {
1645                                 // one vertex clipped to z
1646                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset, -2.0f,  1.0f), blue},
1647
1648                                 // two vertices clipped to edges
1649                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1650                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1651                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1652                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1653                         };
1654
1655                         addChild(new TriangleAttributeCase(m_context, "multiple_3",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1656                         addChild(new TriangleAttributeCase(m_context, "multiple_3_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1657                         addChild(new TriangleAttributeCase(m_context, "multiple_3_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1658                 }
1659
1660                 {
1661                         const TriangleAttributeCase::TriangleData polys[] =
1662                         {
1663                                 // one vertex clipped to z
1664                                 {tcu::Vec4(0.3f,  0.2f,  0.0f,  1.0f), red, tcu::Vec4( 0.3f, -0.2f,  0.0f,  1.0f), yellow, tcu::Vec4( offset, 0.0f,  2.0f,  1.0f), blue},
1665
1666                                 // two vertices clipped to edges
1667                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1668                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1669                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1670                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1671                         };
1672
1673                         addChild(new TriangleAttributeCase(m_context, "multiple_4",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1674                         addChild(new TriangleAttributeCase(m_context, "multiple_4_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1675                         addChild(new TriangleAttributeCase(m_context, "multiple_4_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1676                 }
1677
1678                 {
1679                         const TriangleAttributeCase::TriangleData polys[] =
1680                         {
1681                                 // one vertex clipped to z
1682                                 {tcu::Vec4(-0.3f,  0.2f,  0.0f,  1.0f), red, tcu::Vec4(-0.3f, -0.2f,  0.0f,  1.0f), yellow, tcu::Vec4(-offset, 0.0f,  2.0f,  1.0f), blue},
1683
1684                                 // two vertices clipped to edges
1685                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1686                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1687                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1688                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1689                         };
1690
1691                         addChild(new TriangleAttributeCase(m_context, "multiple_5",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1692                         addChild(new TriangleAttributeCase(m_context, "multiple_5_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1693                         addChild(new TriangleAttributeCase(m_context, "multiple_5_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1694                 }
1695
1696                 {
1697                         const TriangleAttributeCase::TriangleData polys[] =
1698                         {
1699                                 // one vertex clipped to z
1700                                 {tcu::Vec4(-0.2f,  0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, 0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, offset,  2.0f,  1.0f), blue},
1701
1702                                 // two vertices clipped to edges
1703                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1704                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1705                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1706                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1707                         };
1708
1709                         addChild(new TriangleAttributeCase(m_context, "multiple_6",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1710                         addChild(new TriangleAttributeCase(m_context, "multiple_6_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1711                         addChild(new TriangleAttributeCase(m_context, "multiple_6_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1712                 }
1713
1714                 {
1715                         const TriangleAttributeCase::TriangleData polys[] =
1716                         {
1717                                 // two vertices clipped to edges
1718                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1719
1720                                 // two vertices clipped to edges
1721                                 {tcu::Vec4( 0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f, -0.6f,  0.0f,  1.0f), blue},
1722                                 {tcu::Vec4( 0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4( 1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.6f,  0.6f,  0.0f,  1.0f), blue},
1723                                 {tcu::Vec4(-0.6f,  1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f,  0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f,  0.6f,  0.0f,  1.0f), blue},
1724                                 {tcu::Vec4(-0.6f, -1.2f,  0.0f,  1.0f), red, tcu::Vec4(-1.2f, -0.6f,  0.0f,  1.0f), yellow, tcu::Vec4(-0.6f, -0.6f,  0.0f,  1.0f), blue},
1725                         };
1726
1727                         addChild(new TriangleAttributeCase(m_context, "multiple_7",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1728                         addChild(new TriangleAttributeCase(m_context, "multiple_7_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1729                         addChild(new TriangleAttributeCase(m_context, "multiple_7_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1730                 }
1731
1732                 {
1733                         const TriangleAttributeCase::TriangleData polys[] =
1734                         {
1735                                 // one vertex clipped to z
1736                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1737
1738                                 // fill
1739                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), white, tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), white, tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), white},
1740                                 {tcu::Vec4( -1.0f,  1.0f,  0.0f,  1.0f), blue,  tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), blue, tcu::Vec4(  1.0f, 1.0f,  0.0f,  1.0f), blue},
1741                         };
1742
1743                         addChild(new TriangleAttributeCase(m_context, "multiple_8",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1744                         addChild(new TriangleAttributeCase(m_context, "multiple_8_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1745                         addChild(new TriangleAttributeCase(m_context, "multiple_8_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1746                 }
1747
1748                 {
1749                         const TriangleAttributeCase::TriangleData polys[] =
1750                         {
1751                                 // one vertex clipped to z
1752                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1753
1754                                 // fill
1755                                 {tcu::Vec4( -1.0f,  1.0f,  0.0f,  1.0f), red,  tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), red,  tcu::Vec4(  1.0f, 1.0f,  0.0f,  1.0f), red},
1756                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), blue, tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), blue, tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), blue},
1757                         };
1758
1759                         addChild(new TriangleAttributeCase(m_context, "multiple_9",                                     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1760                         addChild(new TriangleAttributeCase(m_context, "multiple_9_viewport_center",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1761                         addChild(new TriangleAttributeCase(m_context, "multiple_9_viewport_corner",     "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1762                 }
1763
1764                 {
1765                         const TriangleAttributeCase::TriangleData polys[] =
1766                         {
1767                                 // one vertex clipped to z
1768                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1769
1770                                 // fill
1771                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), white, tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), white, tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), white},
1772                                 {tcu::Vec4( -1.0f,  1.0f,  0.0f,  1.0f), red,   tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), red,   tcu::Vec4(  1.0f, 1.0f,  0.0f,  1.0f), red},
1773                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), blue,  tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), blue,  tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), blue},
1774                         };
1775
1776                         addChild(new TriangleAttributeCase(m_context, "multiple_10",                                    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1777                         addChild(new TriangleAttributeCase(m_context, "multiple_10_viewport_center",    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1778                         addChild(new TriangleAttributeCase(m_context, "multiple_10_viewport_corner",    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1779                 }
1780
1781                 {
1782                         const TriangleAttributeCase::TriangleData polys[] =
1783                         {
1784                                 // one vertex clipped to z
1785                                 {tcu::Vec4(-0.2f,  -0.3f,  0.0f,  1.0f), red, tcu::Vec4( 0.2f, -0.3f,  0.0f,  1.0f), yellow, tcu::Vec4( 0.0f, -offset,  2.0f,  1.0f), blue},
1786
1787                                 // fill
1788                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), white,  tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), white,  tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), white},
1789                                 {tcu::Vec4( -1.0f,  1.0f,  0.0f,  1.0f), red,    tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), red,    tcu::Vec4(  1.0f, 1.0f,  0.0f,  1.0f), red},
1790                                 {tcu::Vec4( -1.0f, -1.0f,  0.0f,  1.0f), blue,   tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), blue,   tcu::Vec4( -1.0f, 1.0f,  0.0f,  1.0f), blue},
1791                                 {tcu::Vec4( -1.0f,  1.0f,  0.0f,  1.0f), yellow, tcu::Vec4( 1.0f, -1.0f,  0.0f,  1.0f), yellow, tcu::Vec4(  1.0f, 1.0f,  0.0f,  1.0f), yellow},
1792                         };
1793
1794                         addChild(new TriangleAttributeCase(m_context, "multiple_11",                                    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_WHOLE));
1795                         addChild(new TriangleAttributeCase(m_context, "multiple_11_viewport_center",    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CENTER));
1796                         addChild(new TriangleAttributeCase(m_context, "multiple_11_viewport_corner",    "polygon clipping",             DE_ARRAY_BEGIN(polys),          DE_ARRAY_END(polys),            VIEWPORT_CORNER));
1797                 }
1798         }
1799 }
1800
1801 class PolyEdgesTestGroup : public TestCaseGroup
1802 {
1803 public:
1804                         PolyEdgesTestGroup      (Context& context);
1805
1806         void    init                            (void);
1807 };
1808
1809 PolyEdgesTestGroup::PolyEdgesTestGroup (Context& context)
1810         : TestCaseGroup(context, "polygon_edge", "Polygon clipping edge tests")
1811 {
1812 }
1813
1814 void PolyEdgesTestGroup::init (void)
1815 {
1816         // Quads via origin
1817         const struct Quad
1818         {
1819                 tcu::Vec3 d1; // tangent
1820                 tcu::Vec3 d2; // bi-tangent
1821         } quads[] =
1822         {
1823                 { tcu::Vec3( 1, 1, 1),  tcu::Vec3( 1,   -1, 1) },
1824                 { tcu::Vec3( 1, 1, 1),  tcu::Vec3(-1, 1.1f, 1) },
1825                 { tcu::Vec3( 1, 1, 0),  tcu::Vec3(-1,    1, 0) },
1826                 { tcu::Vec3( 0, 1, 0),  tcu::Vec3( 1,    0, 0) },
1827                 { tcu::Vec3( 0, 1, 0),  tcu::Vec3( 1, 0.1f, 0) },
1828         };
1829
1830         // Quad near edge
1831         const struct EdgeQuad
1832         {
1833                 tcu::Vec3 d1;           // tangent
1834                 tcu::Vec3 d2;           // bi-tangent
1835                 tcu::Vec3 center;       // center
1836         } edgeQuads[] =
1837         {
1838                 { tcu::Vec3( 1,     0.01f, 0    ),      tcu::Vec3( 0,      0.01f,  0),  tcu::Vec3( 0,     0.99f, 0    ) }, // edge near x-plane
1839                 { tcu::Vec3( 0.01f, 1,     0    ),      tcu::Vec3( 0.01f,  0,      0),  tcu::Vec3( 0.99f, 0,     0    ) }, // edge near y-plane
1840                 { tcu::Vec3( 1,     1,     0.01f),      tcu::Vec3( 0.01f,  -0.01f, 0),  tcu::Vec3( 0,     0,     0.99f) }, // edge near z-plane
1841         };
1842
1843         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(quads); ++ndx)
1844                 addChild(new QuadFillTest(m_context, (std::string("quad_at_origin_") + de::toString(ndx)).c_str(), "polygon edge clipping", VIEWPORT_CENTER, quads[ndx].d1, quads[ndx].d2));
1845         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(edgeQuads); ++ndx)
1846                 addChild(new QuadFillTest(m_context, (std::string("quad_near_edge_") + de::toString(ndx)).c_str(), "polygon edge clipping", VIEWPORT_CENTER, edgeQuads[ndx].d1, edgeQuads[ndx].d2, edgeQuads[ndx].center));
1847
1848         // Polyfan
1849         addChild(new TriangleFanFillTest(m_context, "poly_fan", "polygon edge clipping", VIEWPORT_CENTER));
1850 }
1851
1852 class PolyVertexClipTestGroup : public TestCaseGroup
1853 {
1854 public:
1855                         PolyVertexClipTestGroup (Context& context);
1856
1857         void    init                                    (void);
1858 };
1859
1860 PolyVertexClipTestGroup::PolyVertexClipTestGroup (Context& context)
1861         : TestCaseGroup(context, "triangle_vertex", "Clip n vertices")
1862 {
1863 }
1864
1865 void PolyVertexClipTestGroup::init (void)
1866 {
1867         const float far = 30000.0f;
1868         const tcu::IVec3 outside[] =
1869         {
1870                 // outside one clipping plane
1871                 tcu::IVec3(-1,  0,  0),
1872                 tcu::IVec3( 1,  0,  0),
1873                 tcu::IVec3( 0,  1,  0),
1874                 tcu::IVec3( 0, -1,  0),
1875                 tcu::IVec3( 0,  0,  1),
1876                 tcu::IVec3( 0,  0, -1),
1877
1878                 // outside two clipping planes
1879                 tcu::IVec3(-1, -1,  0),
1880                 tcu::IVec3( 1, -1,  0),
1881                 tcu::IVec3( 1,  1,  0),
1882                 tcu::IVec3(-1,  1,  0),
1883
1884                 tcu::IVec3(-1,  0, -1),
1885                 tcu::IVec3( 1,  0, -1),
1886                 tcu::IVec3( 1,  0,  1),
1887                 tcu::IVec3(-1,  0,  1),
1888
1889                 tcu::IVec3( 0, -1, -1),
1890                 tcu::IVec3( 0,  1, -1),
1891                 tcu::IVec3( 0,  1,  1),
1892                 tcu::IVec3( 0, -1,  1),
1893
1894                 // outside three clipping planes
1895                 tcu::IVec3(-1, -1,  1),
1896                 tcu::IVec3( 1, -1,  1),
1897                 tcu::IVec3( 1,  1,  1),
1898                 tcu::IVec3(-1,  1,  1),
1899
1900                 tcu::IVec3(-1, -1, -1),
1901                 tcu::IVec3( 1, -1, -1),
1902                 tcu::IVec3( 1,  1, -1),
1903                 tcu::IVec3(-1,  1, -1),
1904         };
1905
1906         de::Random rnd(0xabcdef);
1907
1908         TestCaseGroup* clipOne          = new TestCaseGroup(m_context, "clip_one",              "Clip one vertex");
1909         TestCaseGroup* clipTwo          = new TestCaseGroup(m_context, "clip_two",              "Clip two vertices");
1910         TestCaseGroup* clipThree        = new TestCaseGroup(m_context, "clip_three",    "Clip three vertices");
1911
1912         addChild(clipOne);
1913         addChild(clipTwo);
1914         addChild(clipThree);
1915
1916         // Test 1 point clipped
1917         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(outside); ++ndx)
1918         {
1919                 const float             w0              = rnd.getFloat(0.2f, 16.0f);
1920                 const float             w1              = rnd.getFloat(0.2f, 16.0f);
1921                 const float             w2              = rnd.getFloat(0.2f, 16.0f);
1922                 const tcu::Vec4 white   = tcu::Vec4(    1,          1,  1,      1);
1923                 const tcu::Vec3 r0              = tcu::Vec3( 0.2f,       0.3f,  0);
1924                 const tcu::Vec3 r1              = tcu::Vec3(-0.3f,      -0.4f,  0);
1925                 const tcu::Vec3 r2              = IVec3ToVec3(outside[ndx]) * far;
1926                 const tcu::Vec4 p0              = tcu::Vec4(r0.x() * w0, r0.y() * w0, r0.z() * w0, w0);
1927                 const tcu::Vec4 p1              = tcu::Vec4(r1.x() * w1, r1.y() * w1, r1.z() * w1, w1);
1928                 const tcu::Vec4 p2              = tcu::Vec4(r2.x() * w2, r2.y() * w2, r2.z() * w2, w2);
1929
1930                 const std::string name  = std::string("clip") +
1931                         (outside[ndx].x() > 0 ? "_pos_x" : (outside[ndx].x() < 0 ? "_neg_x" : "")) +
1932                         (outside[ndx].y() > 0 ? "_pos_y" : (outside[ndx].y() < 0 ? "_neg_y" : "")) +
1933                         (outside[ndx].z() > 0 ? "_pos_z" : (outside[ndx].z() < 0 ? "_neg_z" : ""));
1934
1935                 const TriangleCase::TriangleData triangle =     {p0, white, p1, white, p2, white};
1936
1937                 // don't try to test with degenerate (or almost degenerate) triangles
1938                 if (outside[ndx].x() == 0 && outside[ndx].y() == 0)
1939                         continue;
1940
1941                 clipOne->addChild(new TriangleCase(m_context, name.c_str(), "clip one vertex", &triangle, &triangle + 1, VIEWPORT_CENTER));
1942         }
1943
1944         // Special triangles for "clip_z" cases, default triangles is not good, since it has very small visible area => problems with MSAA
1945         {
1946                 const tcu::Vec4 white = tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
1947
1948                 const TriangleCase::TriangleData posZTriangle =
1949                 {
1950                         tcu::Vec4( 0.0f, -0.7f, -0.9f, 1.0f), white,
1951                         tcu::Vec4( 0.8f,  0.0f, -0.7f, 1.0f), white,
1952                         tcu::Vec4(-0.9f,  0.9f,  3.0f, 1.0f), white
1953                 };
1954                 const TriangleCase::TriangleData negZTriangle =
1955                 {
1956                         tcu::Vec4( 0.0f, -0.7f,  0.9f, 1.0f), white,
1957                         tcu::Vec4( 0.4f,  0.0f,  0.7f, 1.0f), white,
1958                         tcu::Vec4(-0.9f,  0.9f, -3.0f, 1.0f), white
1959                 };
1960
1961                 clipOne->addChild(new TriangleCase(m_context, "clip_pos_z", "clip one vertex", &posZTriangle, &posZTriangle + 1, VIEWPORT_CENTER));
1962                 clipOne->addChild(new TriangleCase(m_context, "clip_neg_z", "clip one vertex", &negZTriangle, &negZTriangle + 1, VIEWPORT_CENTER));
1963         }
1964
1965         // Test 2 points clipped
1966         for (int ndx1 = 0; ndx1 < DE_LENGTH_OF_ARRAY(outside); ++ndx1)
1967         for (int ndx2 = ndx1 + 1; ndx2 < DE_LENGTH_OF_ARRAY(outside); ++ndx2)
1968         {
1969                 const float             w0              = rnd.getFloat(0.2f, 16.0f);
1970                 const float             w1              = rnd.getFloat(0.2f, 16.0f);
1971                 const float             w2              = rnd.getFloat(0.2f, 16.0f);
1972                 const tcu::Vec4 white   = tcu::Vec4(    1,          1,  1,      1);
1973                 const tcu::Vec3 r0              = tcu::Vec3( 0.2f,       0.3f,  0);
1974                 const tcu::IVec3 r1             = outside[ndx1];
1975                 const tcu::IVec3 r2             = outside[ndx2];
1976                 const tcu::Vec4 p0              = tcu::Vec4(r0.x() * w0, r0.y() * w0, r0.z() * w0, w0);
1977                 const tcu::Vec4 p1              = tcu::Vec4(r1.x() * far * w1, r1.y() * far * w1, r1.z() * far * w1, w1);
1978                 const tcu::Vec4 p2              = tcu::Vec4(r2.x() * far * w2, r2.y() * far * w2, r2.z() * far * w2, w2);
1979
1980                 const std::string name  = std::string("clip") +
1981                         (outside[ndx1].x() > 0 ? "_pos_x" : (outside[ndx1].x() < 0 ? "_neg_x" : "")) +
1982                         (outside[ndx1].y() > 0 ? "_pos_y" : (outside[ndx1].y() < 0 ? "_neg_y" : "")) +
1983                         (outside[ndx1].z() > 0 ? "_pos_z" : (outside[ndx1].z() < 0 ? "_neg_z" : "")) +
1984                         "_and" +
1985                         (outside[ndx2].x() > 0 ? "_pos_x" : (outside[ndx2].x() < 0 ? "_neg_x" : "")) +
1986                         (outside[ndx2].y() > 0 ? "_pos_y" : (outside[ndx2].y() < 0 ? "_neg_y" : "")) +
1987                         (outside[ndx2].z() > 0 ? "_pos_z" : (outside[ndx2].z() < 0 ? "_neg_z" : ""));
1988
1989                 const TriangleCase::TriangleData triangle =     {p0, white, p1, white, p2, white};
1990
1991                 if (twoPointClippedTriangleInvisible(r0, r1, r2))
1992                         continue;
1993
1994                 clipTwo->addChild(new TriangleCase(m_context, name.c_str(), "clip two vertices", &triangle, &triangle + 1, VIEWPORT_CENTER));
1995         }
1996
1997         // Test 3 points clipped
1998         for (int ndx1 = 0; ndx1 < DE_LENGTH_OF_ARRAY(outside); ++ndx1)
1999         for (int ndx2 = ndx1 + 1; ndx2 < DE_LENGTH_OF_ARRAY(outside); ++ndx2)
2000         for (int ndx3 = ndx2 + 1; ndx3 < DE_LENGTH_OF_ARRAY(outside); ++ndx3)
2001         {
2002                 const float             w0              = rnd.getFloat(0.2f, 16.0f);
2003                 const float             w1              = rnd.getFloat(0.2f, 16.0f);
2004                 const float             w2              = rnd.getFloat(0.2f, 16.0f);
2005                 const tcu::Vec4 white   = tcu::Vec4(1, 1, 1, 1);
2006                 const tcu::IVec3 r0             = outside[ndx1];
2007                 const tcu::IVec3 r1             = outside[ndx2];
2008                 const tcu::IVec3 r2             = outside[ndx3];
2009                 const tcu::Vec4 p0              = tcu::Vec4(r0.x() * far * w0, r0.y() * far * w0, r0.z() * far * w0, w0);
2010                 const tcu::Vec4 p1              = tcu::Vec4(r1.x() * far * w1, r1.y() * far * w1, r1.z() * far * w1, w1);
2011                 const tcu::Vec4 p2              = tcu::Vec4(r2.x() * far * w2, r2.y() * far * w2, r2.z() * far * w2, w2);
2012
2013                 // ignore cases where polygon is along xz or yz planes
2014                 if (pointsOnLine(r0.swizzle(0, 1), r1.swizzle(0, 1), r2.swizzle(0, 1)))
2015                         continue;
2016
2017                 // triangle is visible only if it intersects the origin
2018                 if (pointOnTriangle(tcu::IVec3(0, 0, 0), r0, r1, r2))
2019                 {
2020                         const TriangleCase::TriangleData triangle =     {p0, white, p1, white, p2, white};
2021                         const std::string name  = std::string("clip") +
2022                                 (outside[ndx1].x() > 0 ? "_pos_x" : (outside[ndx1].x() < 0 ? "_neg_x" : "")) +
2023                                 (outside[ndx1].y() > 0 ? "_pos_y" : (outside[ndx1].y() < 0 ? "_neg_y" : "")) +
2024                                 (outside[ndx1].z() > 0 ? "_pos_z" : (outside[ndx1].z() < 0 ? "_neg_z" : "")) +
2025                                 "_and" +
2026                                 (outside[ndx2].x() > 0 ? "_pos_x" : (outside[ndx2].x() < 0 ? "_neg_x" : "")) +
2027                                 (outside[ndx2].y() > 0 ? "_pos_y" : (outside[ndx2].y() < 0 ? "_neg_y" : "")) +
2028                                 (outside[ndx2].z() > 0 ? "_pos_z" : (outside[ndx2].z() < 0 ? "_neg_z" : "")) +
2029                                 "_and" +
2030                                 (outside[ndx3].x() > 0 ? "_pos_x" : (outside[ndx3].x() < 0 ? "_neg_x" : "")) +
2031                                 (outside[ndx3].y() > 0 ? "_pos_y" : (outside[ndx3].y() < 0 ? "_neg_y" : "")) +
2032                                 (outside[ndx3].z() > 0 ? "_pos_z" : (outside[ndx3].z() < 0 ? "_neg_z" : ""));
2033
2034                         clipThree->addChild(new TriangleCase(m_context, name.c_str(), "clip three vertices", &triangle, &triangle + 1, VIEWPORT_CENTER));
2035                 }
2036         }
2037 }
2038
2039 } // anonymous
2040
2041 ClippingTests::ClippingTests (Context& context)
2042         : TestCaseGroup(context, "clipping", "Clipping tests")
2043 {
2044 }
2045
2046 ClippingTests::~ClippingTests (void)
2047 {
2048 }
2049
2050 void ClippingTests::init (void)
2051 {
2052         addChild(new PointsTestGroup            (m_context));
2053         addChild(new LinesTestGroup                     (m_context));
2054         addChild(new PolysTestGroup                     (m_context));
2055         addChild(new PolyEdgesTestGroup         (m_context));
2056         addChild(new PolyVertexClipTestGroup(m_context));
2057 }
2058
2059 } // Functional
2060 } // gles2
2061 } // deqp