am db0c176d: am 8af07798: am 79c79d54: Merge "Remove broken line interpolation tests...
[platform/upstream/VK-GL-CTS.git] / modules / gles3 / accuracy / es3aTextureMipmapTests.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 3.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 Mipmapping accuracy tests.
22  *//*--------------------------------------------------------------------*/
23
24 #include "es3aTextureMipmapTests.hpp"
25
26 #include "glsTextureTestUtil.hpp"
27 #include "gluTexture.hpp"
28 #include "gluTextureUtil.hpp"
29 #include "gluPixelTransfer.hpp"
30 #include "tcuTextureUtil.hpp"
31 #include "tcuMatrix.hpp"
32 #include "tcuMatrixUtil.hpp"
33 #include "deStringUtil.hpp"
34 #include "deRandom.hpp"
35 #include "deString.h"
36
37 #include "glwFunctions.hpp"
38 #include "glwEnums.hpp"
39
40 namespace deqp
41 {
42 namespace gles3
43 {
44 namespace Accuracy
45 {
46
47 using std::string;
48 using std::vector;
49 using tcu::TestLog;
50 using tcu::Vec2;
51 using tcu::Vec3;
52 using tcu::Vec4;
53 using tcu::IVec4;
54 using namespace gls::TextureTestUtil;
55
56
57 enum CoordType
58 {
59         COORDTYPE_BASIC,                //!< texCoord = translateScale(position).
60         COORDTYPE_BASIC_BIAS,   //!< Like basic, but with bias values.
61         COORDTYPE_AFFINE,               //!< texCoord = translateScaleRotateShear(position).
62         COORDTYPE_PROJECTED,    //!< Projected coordinates, w != 1
63
64         COORDTYPE_LAST
65 };
66
67 // Texture2DMipmapCase
68
69 class Texture2DMipmapCase : public tcu::TestCase
70 {
71 public:
72
73                                                                 Texture2DMipmapCase                     (tcu::TestContext&                      testCtx,
74                                                                                                                          glu::RenderContext&            renderCtx,
75                                                                                                                          const glu::ContextInfo&        renderCtxInfo,
76                                                                                                                          const char*                            name,
77                                                                                                                          const char*                            desc,
78                                                                                                                          CoordType                                      coordType,
79                                                                                                                          deUint32                                       minFilter,
80                                                                                                                          deUint32                                       wrapS,
81                                                                                                                          deUint32                                       wrapT,
82                                                                                                                          deUint32                                       format,
83                                                                                                                          deUint32                                       dataType,
84                                                                                                                          int                                            width,
85                                                                                                                          int                                            height);
86                                                                 ~Texture2DMipmapCase            (void);
87
88         void                                            init                                            (void);
89         void                                            deinit                                          (void);
90         IterateResult                           iterate                                         (void);
91
92 private:
93                                                                 Texture2DMipmapCase                     (const Texture2DMipmapCase& other);
94         Texture2DMipmapCase&            operator=                                       (const Texture2DMipmapCase& other);
95
96         glu::RenderContext&                     m_renderCtx;
97         const glu::ContextInfo&         m_renderCtxInfo;
98
99         CoordType                                       m_coordType;
100         deUint32                                        m_minFilter;
101         deUint32                                        m_wrapS;
102         deUint32                                        m_wrapT;
103         deUint32                                        m_format;
104         deUint32                                        m_dataType;
105         int                                                     m_width;
106         int                                                     m_height;
107
108         glu::Texture2D*                         m_texture;
109         TextureRenderer                         m_renderer;
110 };
111
112 Texture2DMipmapCase::Texture2DMipmapCase (tcu::TestContext&                     testCtx,
113                                                                                   glu::RenderContext&           renderCtx,
114                                                                                   const glu::ContextInfo&       renderCtxInfo,
115                                                                                   const char*                           name,
116                                                                                   const char*                           desc,
117                                                                                   CoordType                                     coordType,
118                                                                                   deUint32                                      minFilter,
119                                                                                   deUint32                                      wrapS,
120                                                                                   deUint32                                      wrapT,
121                                                                                   deUint32                                      format,
122                                                                                   deUint32                                      dataType,
123                                                                                   int                                           width,
124                                                                                   int                                           height)
125         : TestCase                      (testCtx, tcu::NODETYPE_ACCURACY, name, desc)
126         , m_renderCtx           (renderCtx)
127         , m_renderCtxInfo       (renderCtxInfo)
128         , m_coordType           (coordType)
129         , m_minFilter           (minFilter)
130         , m_wrapS                       (wrapS)
131         , m_wrapT                       (wrapT)
132         , m_format                      (format)
133         , m_dataType            (dataType)
134         , m_width                       (width)
135         , m_height                      (height)
136         , m_texture                     (DE_NULL)
137         , m_renderer            (renderCtx, testCtx, glu::GLSL_VERSION_300_ES, glu::PRECISION_HIGHP)
138 {
139 }
140
141 Texture2DMipmapCase::~Texture2DMipmapCase (void)
142 {
143         deinit();
144 }
145
146 void Texture2DMipmapCase::init (void)
147 {
148         m_texture = new glu::Texture2D(m_renderCtx, m_format, m_dataType, m_width, m_height);
149
150         int numLevels = deLog2Floor32(de::max(m_width, m_height))+1;
151
152         // Fill texture with colored grid.
153         for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
154         {
155                 deUint32        step            = 0xff / (numLevels-1);
156                 deUint32        inc                     = deClamp32(step*levelNdx, 0x00, 0xff);
157                 deUint32        dec                     = 0xff - inc;
158                 deUint32        rgb                     = (inc << 16) | (dec << 8) | 0xff;
159                 deUint32        color           = 0xff000000 | rgb;
160
161                 m_texture->getRefTexture().allocLevel(levelNdx);
162                 tcu::clear(m_texture->getRefTexture().getLevel(levelNdx), toVec4(tcu::RGBA(color)));
163         }
164 }
165
166 void Texture2DMipmapCase::deinit (void)
167 {
168         delete m_texture;
169         m_texture = DE_NULL;
170
171         m_renderer.clear();
172 }
173
174 static void getBasicTexCoord2D (std::vector<float>& dst, int cellNdx)
175 {
176         static const struct
177         {
178                 Vec2 bottomLeft;
179                 Vec2 topRight;
180         } s_basicCoords[] =
181         {
182                 { Vec2(-0.1f,  0.1f), Vec2( 0.8f,  1.0f) },
183                 { Vec2(-0.3f, -0.6f), Vec2( 0.7f,  0.4f) },
184                 { Vec2(-0.3f,  0.6f), Vec2( 0.7f, -0.9f) },
185                 { Vec2(-0.8f,  0.6f), Vec2( 0.7f, -0.9f) },
186
187                 { Vec2(-0.5f, -0.5f), Vec2( 1.5f,  1.5f) },
188                 { Vec2( 1.0f, -1.0f), Vec2(-1.3f,  1.0f) },
189                 { Vec2( 1.2f, -1.0f), Vec2(-1.3f,  1.6f) },
190                 { Vec2( 2.2f, -1.1f), Vec2(-1.3f,  0.8f) },
191
192                 { Vec2(-1.5f,  1.6f), Vec2( 1.7f, -1.4f) },
193                 { Vec2( 2.0f,  1.6f), Vec2( 2.3f, -1.4f) },
194                 { Vec2( 1.3f, -2.6f), Vec2(-2.7f,  2.9f) },
195                 { Vec2(-0.8f, -6.6f), Vec2( 6.0f, -0.9f) },
196
197                 { Vec2( -8.0f,   9.0f), Vec2(  8.3f,  -7.0f) },
198                 { Vec2(-16.0f,  10.0f), Vec2( 18.3f,  24.0f) },
199                 { Vec2( 30.2f,  55.0f), Vec2(-24.3f,  -1.6f) },
200                 { Vec2(-33.2f,  64.1f), Vec2( 32.1f, -64.1f) },
201         };
202
203         DE_ASSERT(de::inBounds(cellNdx, 0, DE_LENGTH_OF_ARRAY(s_basicCoords)));
204
205         const Vec2& bottomLeft  = s_basicCoords[cellNdx].bottomLeft;
206         const Vec2& topRight    = s_basicCoords[cellNdx].topRight;
207
208         computeQuadTexCoord2D(dst, bottomLeft, topRight);
209 }
210
211 static void getAffineTexCoord2D (std::vector<float>& dst, int cellNdx)
212 {
213         // Use basic coords as base.
214         getBasicTexCoord2D(dst, cellNdx);
215
216         // Rotate based on cell index.
217         float           angle           = 2.0f*DE_PI * ((float)cellNdx / 16.0f);
218         tcu::Mat2       rotMatrix       = tcu::rotationMatrix(angle);
219
220         // Second and third row are sheared.
221         float           shearX          = de::inRange(cellNdx, 4, 11) ? (float)(15-cellNdx) / 16.0f : 0.0f;
222         tcu::Mat2       shearMatrix     = tcu::shearMatrix(tcu::Vec2(shearX, 0.0f));
223
224         tcu::Mat2       transform       = rotMatrix * shearMatrix;
225         Vec2            p0                      = transform * Vec2(dst[0], dst[1]);
226         Vec2            p1                      = transform * Vec2(dst[2], dst[3]);
227         Vec2            p2                      = transform * Vec2(dst[4], dst[5]);
228         Vec2            p3                      = transform * Vec2(dst[6], dst[7]);
229
230         dst[0] = p0.x();        dst[1] = p0.y();
231         dst[2] = p1.x();        dst[3] = p1.y();
232         dst[4] = p2.x();        dst[5] = p2.y();
233         dst[6] = p3.x();        dst[7] = p3.y();
234 }
235
236 Texture2DMipmapCase::IterateResult Texture2DMipmapCase::iterate (void)
237 {
238         // Constants.
239         const deUint32                          magFilter                       = GL_NEAREST;
240
241         const glw::Functions&           gl                                      = m_renderCtx.getFunctions();
242         TestLog&                                        log                                     = m_testCtx.getLog();
243
244         const tcu::Texture2D&           refTexture                      = m_texture->getRefTexture();
245         const tcu::TextureFormat&       texFmt                          = refTexture.getFormat();
246         tcu::TextureFormatInfo          fmtInfo                         = tcu::getTextureFormatInfo(texFmt);
247
248         int                                                     texWidth                        = refTexture.getWidth();
249         int                                                     texHeight                       = refTexture.getHeight();
250         int                                                     defViewportWidth        = texWidth*4;
251         int                                                     defViewportHeight       = texHeight*4;
252
253         RandomViewport                          viewport                        (m_renderCtx.getRenderTarget(), defViewportWidth, defViewportHeight, deStringHash(getName()));
254         ReferenceParams                         sampleParams            (TEXTURETYPE_2D);
255         vector<float>                           texCoord;
256
257         bool                                            isProjected                     = m_coordType == COORDTYPE_PROJECTED;
258         bool                                            useLodBias                      = m_coordType == COORDTYPE_BASIC_BIAS;
259
260         tcu::Surface                            renderedFrame           (viewport.width, viewport.height);
261
262         // Accuracy cases test against ideal lod computation.
263         tcu::Surface                            idealFrame                      (viewport.width, viewport.height);
264
265         // Viewport is divided into 4x4 grid.
266         int                                                     gridWidth                       = 4;
267         int                                                     gridHeight                      = 4;
268         int                                                     cellWidth                       = viewport.width / gridWidth;
269         int                                                     cellHeight                      = viewport.height / gridHeight;
270
271         // Accuracy measurements are off unless we get the expected viewport size.
272         if (viewport.width < defViewportWidth || viewport.height < defViewportHeight)
273                 throw tcu::NotSupportedError("Too small viewport", "", __FILE__, __LINE__);
274
275         // Sampling parameters.
276         sampleParams.sampler            = glu::mapGLSampler(m_wrapS, m_wrapT, m_minFilter, magFilter);
277         sampleParams.samplerType        = gls::TextureTestUtil::getSamplerType(m_texture->getRefTexture().getFormat());
278         sampleParams.colorBias          = fmtInfo.lookupBias;
279         sampleParams.colorScale         = fmtInfo.lookupScale;
280         sampleParams.flags                      = (isProjected ? ReferenceParams::PROJECTED : 0) | (useLodBias ? ReferenceParams::USE_BIAS : 0);
281
282         // Upload texture data.
283         m_texture->upload();
284
285         // Use unit 0.
286         gl.activeTexture(GL_TEXTURE0);
287
288         // Bind gradient texture and setup sampler parameters.
289         gl.bindTexture(GL_TEXTURE_2D, m_texture->getGLTexture());
290         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,              m_wrapS);
291         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,              m_wrapT);
292         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,  m_minFilter);
293         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,  magFilter);
294
295         GLU_EXPECT_NO_ERROR(gl.getError(), "After texture setup");
296
297         // Bias values.
298         static const float s_bias[] = { 1.0f, -2.0f, 0.8f, -0.5f, 1.5f, 0.9f, 2.0f, 4.0f };
299
300         // Projection values.
301         static const Vec4 s_projections[] =
302         {
303                 Vec4(1.2f, 1.0f, 0.7f, 1.0f),
304                 Vec4(1.3f, 0.8f, 0.6f, 2.0f),
305                 Vec4(0.8f, 1.0f, 1.7f, 0.6f),
306                 Vec4(1.2f, 1.0f, 1.7f, 1.5f)
307         };
308
309         // Render cells.
310         for (int gridY = 0; gridY < gridHeight; gridY++)
311         {
312                 for (int gridX = 0; gridX < gridWidth; gridX++)
313                 {
314                         int                             curX            = cellWidth*gridX;
315                         int                             curY            = cellHeight*gridY;
316                         int                             curW            = gridX+1 == gridWidth ? (viewport.width-curX) : cellWidth;
317                         int                             curH            = gridY+1 == gridHeight ? (viewport.height-curY) : cellHeight;
318                         int                             cellNdx         = gridY*gridWidth + gridX;
319
320                         // Compute texcoord.
321                         switch (m_coordType)
322                         {
323                                 case COORDTYPE_BASIC_BIAS:      // Fall-through.
324                                 case COORDTYPE_PROJECTED:
325                                 case COORDTYPE_BASIC:           getBasicTexCoord2D      (texCoord, cellNdx);    break;
326                                 case COORDTYPE_AFFINE:          getAffineTexCoord2D     (texCoord, cellNdx);    break;
327                                 default:                                        DE_ASSERT(DE_FALSE);
328                         }
329
330                         if (isProjected)
331                                 sampleParams.w = s_projections[cellNdx % DE_LENGTH_OF_ARRAY(s_projections)];
332
333                         if (useLodBias)
334                                 sampleParams.bias = s_bias[cellNdx % DE_LENGTH_OF_ARRAY(s_bias)];
335
336                         // Render with GL.
337                         gl.viewport(viewport.x+curX, viewport.y+curY, curW, curH);
338                         m_renderer.renderQuad(0, &texCoord[0], sampleParams);
339
340                         // Render reference(s).
341                         {
342                                 SurfaceAccess idealDst(idealFrame, m_renderCtx.getRenderTarget().getPixelFormat(), curX, curY, curW, curH);
343                                 sampleParams.lodMode = LODMODE_EXACT;
344                                 sampleTexture(idealDst, m_texture->getRefTexture(), &texCoord[0], sampleParams);
345                         }
346                 }
347         }
348
349         // Read result.
350         glu::readPixels(m_renderCtx, viewport.x, viewport.y, renderedFrame.getAccess());
351
352         // Compare and log.
353         {
354                 const int       bestScoreDiff   = (texWidth/16)*(texHeight/16);
355                 const int       worstScoreDiff  = texWidth*texHeight;
356
357                 int score = measureAccuracy(log, idealFrame, renderedFrame, bestScoreDiff, worstScoreDiff);
358                 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::toString(score).c_str());
359         }
360
361         return STOP;
362 }
363
364 // TextureCubeMipmapCase
365
366 class TextureCubeMipmapCase : public tcu::TestCase
367 {
368 public:
369
370                                                                 TextureCubeMipmapCase           (tcu::TestContext&                      testCtx,
371                                                                                                                          glu::RenderContext&            renderCtx,
372                                                                                                                          const glu::ContextInfo&        renderCtxInfo,
373                                                                                                                          const char*                            name,
374                                                                                                                          const char*                            desc,
375                                                                                                                          CoordType                                      coordType,
376                                                                                                                          deUint32                                       minFilter,
377                                                                                                                          deUint32                                       wrapS,
378                                                                                                                          deUint32                                       wrapT,
379                                                                                                                          deUint32                                       format,
380                                                                                                                          deUint32                                       dataType,
381                                                                                                                          int                                            size);
382                                                                 ~TextureCubeMipmapCase          (void);
383
384         void                                            init                                            (void);
385         void                                            deinit                                          (void);
386         IterateResult                           iterate                                         (void);
387
388 private:
389                                                                 TextureCubeMipmapCase           (const TextureCubeMipmapCase& other);
390         TextureCubeMipmapCase&          operator=                                       (const TextureCubeMipmapCase& other);
391
392         glu::RenderContext&                     m_renderCtx;
393         const glu::ContextInfo&         m_renderCtxInfo;
394
395         CoordType                                       m_coordType;
396         deUint32                                        m_minFilter;
397         deUint32                                        m_wrapS;
398         deUint32                                        m_wrapT;
399         deUint32                                        m_format;
400         deUint32                                        m_dataType;
401         int                                                     m_size;
402
403         glu::TextureCube*                       m_texture;
404         TextureRenderer                         m_renderer;
405 };
406
407 TextureCubeMipmapCase::TextureCubeMipmapCase (tcu::TestContext&                 testCtx,
408                                                                                           glu::RenderContext&           renderCtx,
409                                                                                           const glu::ContextInfo&       renderCtxInfo,
410                                                                                           const char*                           name,
411                                                                                           const char*                           desc,
412                                                                                           CoordType                                     coordType,
413                                                                                           deUint32                                      minFilter,
414                                                                                           deUint32                                      wrapS,
415                                                                                           deUint32                                      wrapT,
416                                                                                           deUint32                                      format,
417                                                                                           deUint32                                      dataType,
418                                                                                           int                                           size)
419         : TestCase                      (testCtx, tcu::NODETYPE_ACCURACY, name, desc)
420         , m_renderCtx           (renderCtx)
421         , m_renderCtxInfo       (renderCtxInfo)
422         , m_coordType           (coordType)
423         , m_minFilter           (minFilter)
424         , m_wrapS                       (wrapS)
425         , m_wrapT                       (wrapT)
426         , m_format                      (format)
427         , m_dataType            (dataType)
428         , m_size                        (size)
429         , m_texture                     (DE_NULL)
430         , m_renderer            (renderCtx, testCtx, glu::GLSL_VERSION_300_ES, glu::PRECISION_HIGHP)
431 {
432 }
433
434 TextureCubeMipmapCase::~TextureCubeMipmapCase (void)
435 {
436         deinit();
437 }
438
439 void TextureCubeMipmapCase::init (void)
440 {
441         m_texture = new glu::TextureCube(m_renderCtx, m_format, m_dataType, m_size);
442
443         int numLevels = deLog2Floor32(m_size)+1;
444
445         // Fill texture with colored grid.
446         for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; faceNdx++)
447         {
448                 for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
449                 {
450                         deUint32        step            = 0xff / (numLevels-1);
451                         deUint32        inc                     = deClamp32(step*levelNdx, 0x00, 0xff);
452                         deUint32        dec                     = 0xff - inc;
453                         deUint32        rgb                     = 0;
454
455                         switch (faceNdx)
456                         {
457                                 case 0: rgb = (inc << 16) | (dec << 8) | 255; break;
458                                 case 1: rgb = (255 << 16) | (inc << 8) | dec; break;
459                                 case 2: rgb = (dec << 16) | (255 << 8) | inc; break;
460                                 case 3: rgb = (dec << 16) | (inc << 8) | 255; break;
461                                 case 4: rgb = (255 << 16) | (dec << 8) | inc; break;
462                                 case 5: rgb = (inc << 16) | (255 << 8) | dec; break;
463                         }
464
465                         deUint32        color           = 0xff000000 | rgb;
466
467                         m_texture->getRefTexture().allocLevel((tcu::CubeFace)faceNdx, levelNdx);
468                         tcu::clear(m_texture->getRefTexture().getLevelFace(levelNdx, (tcu::CubeFace)faceNdx), toVec4(tcu::RGBA(color)));
469                 }
470         }
471 }
472
473 void TextureCubeMipmapCase::deinit (void)
474 {
475         delete m_texture;
476         m_texture = DE_NULL;
477
478         m_renderer.clear();
479 }
480
481 static void randomPartition (vector<IVec4>& dst, de::Random& rnd, int x, int y, int width, int height)
482 {
483         const int minWidth      = 8;
484         const int minHeight     = 8;
485
486         bool    partition               = rnd.getFloat() > 0.4f;
487         bool    partitionX              = partition && width > minWidth && rnd.getBool();
488         bool    partitionY              = partition && height > minHeight && !partitionX;
489
490         if (partitionX)
491         {
492                 int split = width/2 + rnd.getInt(-width/4, +width/4);
493                 randomPartition(dst, rnd, x, y, split, height);
494                 randomPartition(dst, rnd, x+split, y, width-split, height);
495         }
496         else if (partitionY)
497         {
498                 int split = height/2 + rnd.getInt(-height/4, +height/4);
499                 randomPartition(dst, rnd, x, y, width, split);
500                 randomPartition(dst, rnd, x, y+split, width, height-split);
501         }
502         else
503                 dst.push_back(IVec4(x, y, width, height));
504 }
505
506 static void computeGridLayout (vector<IVec4>& dst, int width, int height)
507 {
508         de::Random rnd(7);
509         randomPartition(dst, rnd, 0, 0, width, height);
510 }
511
512 TextureCubeMipmapCase::IterateResult TextureCubeMipmapCase::iterate (void)
513 {
514         // Constants.
515         const deUint32                  magFilter                       = GL_NEAREST;
516
517         int                                             texWidth                        = m_texture->getRefTexture().getSize();
518         int                                             texHeight                       = m_texture->getRefTexture().getSize();
519
520         int                                             defViewportWidth        = texWidth*2;
521         int                                             defViewportHeight       = texHeight*2;
522
523         const glw::Functions&   gl                                      = m_renderCtx.getFunctions();
524         TestLog&                                log                                     = m_testCtx.getLog();
525         RandomViewport                  viewport                        (m_renderCtx.getRenderTarget(), defViewportWidth, defViewportHeight, deStringHash(getName()));
526         tcu::Sampler                    sampler                         = glu::mapGLSampler(m_wrapS, m_wrapT, m_minFilter, magFilter);
527         sampler.seamlessCubeMap = true;
528
529         vector<float>                   texCoord;
530
531         bool                                    isProjected                     = m_coordType == COORDTYPE_PROJECTED;
532         bool                                    useLodBias                      = m_coordType == COORDTYPE_BASIC_BIAS;
533
534         tcu::Surface                    renderedFrame           (viewport.width, viewport.height);
535
536         // Accuracy cases test against ideal lod computation.
537         tcu::Surface                    idealFrame                      (viewport.width, viewport.height);
538
539         // Accuracy measurements are off unless we get the expected viewport size.
540         if (viewport.width < defViewportWidth || viewport.height < defViewportHeight)
541                 throw tcu::NotSupportedError("Too small viewport", "", __FILE__, __LINE__);
542
543         // Upload texture data.
544         m_texture->upload();
545
546         // Use unit 0.
547         gl.activeTexture(GL_TEXTURE0);
548
549         // Bind gradient texture and setup sampler parameters.
550         gl.bindTexture(GL_TEXTURE_CUBE_MAP, m_texture->getGLTexture());
551         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,                m_wrapS);
552         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,                m_wrapT);
553         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,    m_minFilter);
554         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,    magFilter);
555
556         GLU_EXPECT_NO_ERROR(gl.getError(), "After texture setup");
557
558         // Compute grid.
559         vector<IVec4> gridLayout;
560         computeGridLayout(gridLayout, viewport.width, viewport.height);
561
562         // Bias values.
563         static const float s_bias[] = { 1.0f, -2.0f, 0.8f, -0.5f, 1.5f, 0.9f, 2.0f, 4.0f };
564
565         // Projection values \note Less agressive than in 2D case due to smaller quads.
566         static const Vec4 s_projections[] =
567         {
568                 Vec4(1.2f, 1.0f, 0.7f, 1.0f),
569                 Vec4(1.3f, 0.8f, 0.6f, 1.1f),
570                 Vec4(0.8f, 1.0f, 1.2f, 0.8f),
571                 Vec4(1.2f, 1.0f, 1.3f, 0.9f)
572         };
573
574         for (int cellNdx = 0; cellNdx < (int)gridLayout.size(); cellNdx++)
575         {
576                 int                             curX            = gridLayout[cellNdx].x();
577                 int                             curY            = gridLayout[cellNdx].y();
578                 int                             curW            = gridLayout[cellNdx].z();
579                 int                             curH            = gridLayout[cellNdx].w();
580                 tcu::CubeFace   cubeFace        = (tcu::CubeFace)(cellNdx % tcu::CUBEFACE_LAST);
581                 ReferenceParams params          (TEXTURETYPE_CUBE);
582
583                 params.sampler = sampler;
584
585                 DE_ASSERT(m_coordType != COORDTYPE_AFFINE); // Not supported.
586                 computeQuadTexCoordCube(texCoord, cubeFace);
587
588                 if (isProjected)
589                 {
590                         params.flags    |= ReferenceParams::PROJECTED;
591                         params.w                 = s_projections[cellNdx % DE_LENGTH_OF_ARRAY(s_projections)];
592                 }
593
594                 if (useLodBias)
595                 {
596                         params.flags    |= ReferenceParams::USE_BIAS;
597                         params.bias              = s_bias[cellNdx % DE_LENGTH_OF_ARRAY(s_bias)];
598                 }
599
600                 // Render with GL.
601                 gl.viewport(viewport.x+curX, viewport.y+curY, curW, curH);
602                 m_renderer.renderQuad(0, &texCoord[0], params);
603
604                 // Render reference(s).
605                 {
606                         SurfaceAccess idealDst(idealFrame, m_renderCtx.getRenderTarget().getPixelFormat(), curX, curY, curW, curH);
607                         params.lodMode = LODMODE_EXACT;
608                         sampleTexture(idealDst, m_texture->getRefTexture(), &texCoord[0], params);
609                 }
610         }
611
612         // Read result.
613         glu::readPixels(m_renderCtx, viewport.x, viewport.y, renderedFrame.getAccess());
614
615         // Compare and log.
616         {
617                 const int       bestScoreDiff   = (texWidth/16)*(texHeight/16);
618                 const int       worstScoreDiff  = texWidth*texHeight;
619
620                 int score = measureAccuracy(log, idealFrame, renderedFrame, bestScoreDiff, worstScoreDiff);
621                 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::toString(score).c_str());
622         }
623
624         return STOP;
625 }
626
627 TextureMipmapTests::TextureMipmapTests (Context& context)
628         : TestCaseGroup(context, "mipmap", "Mipmapping accuracy tests")
629 {
630 }
631
632 TextureMipmapTests::~TextureMipmapTests (void)
633 {
634 }
635
636 void TextureMipmapTests::init (void)
637 {
638         tcu::TestCaseGroup* group2D             = new tcu::TestCaseGroup(m_testCtx, "2d",       "2D Texture Mipmapping");
639         tcu::TestCaseGroup*     groupCube       = new tcu::TestCaseGroup(m_testCtx, "cube",     "Cube Map Filtering");
640         addChild(group2D);
641         addChild(groupCube);
642
643         static const struct
644         {
645                 const char*             name;
646                 deUint32                mode;
647         } wrapModes[] =
648         {
649                 { "clamp",              GL_CLAMP_TO_EDGE },
650                 { "repeat",             GL_REPEAT },
651                 { "mirror",             GL_MIRRORED_REPEAT }
652         };
653
654         static const struct
655         {
656                 const char*             name;
657                 deUint32                mode;
658         } minFilterModes[] =
659         {
660                 { "nearest_nearest",    GL_NEAREST_MIPMAP_NEAREST       },
661                 { "linear_nearest",             GL_LINEAR_MIPMAP_NEAREST        },
662                 { "nearest_linear",             GL_NEAREST_MIPMAP_LINEAR        },
663                 { "linear_linear",              GL_LINEAR_MIPMAP_LINEAR         }
664         };
665
666         static const struct
667         {
668                 CoordType               type;
669                 const char*             name;
670                 const char*             desc;
671         } coordTypes[] =
672         {
673                 { COORDTYPE_BASIC,              "basic",                "Mipmapping with translated and scaled coordinates" },
674                 { COORDTYPE_AFFINE,             "affine",               "Mipmapping with affine coordinate transform"           },
675                 { COORDTYPE_PROJECTED,  "projected",    "Mipmapping with perspective projection"                        }
676         };
677
678         const int tex2DWidth    = 64;
679         const int tex2DHeight   = 64;
680
681         // 2D cases.
682         for (int coordType = 0; coordType < DE_LENGTH_OF_ARRAY(coordTypes); coordType++)
683         {
684                 tcu::TestCaseGroup* coordTypeGroup = new tcu::TestCaseGroup(m_testCtx, coordTypes[coordType].name, coordTypes[coordType].desc);
685                 group2D->addChild(coordTypeGroup);
686
687                 for (int minFilter = 0; minFilter < DE_LENGTH_OF_ARRAY(minFilterModes); minFilter++)
688                 {
689                         for (int wrapMode = 0; wrapMode < DE_LENGTH_OF_ARRAY(wrapModes); wrapMode++)
690                         {
691                                 std::ostringstream name;
692                                 name << minFilterModes[minFilter].name
693                                                 << "_" << wrapModes[wrapMode].name;
694
695                                 coordTypeGroup->addChild(new Texture2DMipmapCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
696                                                                                                                                         name.str().c_str(), "",
697                                                                                                                                         coordTypes[coordType].type,
698                                                                                                                                         minFilterModes[minFilter].mode,
699                                                                                                                                         wrapModes[wrapMode].mode,
700                                                                                                                                         wrapModes[wrapMode].mode,
701                                                                                                                                         GL_RGBA, GL_UNSIGNED_BYTE,
702                                                                                                                                         tex2DWidth, tex2DHeight));
703                         }
704                 }
705         }
706
707         const int cubeMapSize = 64;
708
709         static const struct
710         {
711                 CoordType               type;
712                 const char*             name;
713                 const char*             desc;
714         } cubeCoordTypes[] =
715         {
716                 { COORDTYPE_BASIC,              "basic",                "Mipmapping with translated and scaled coordinates" },
717                 { COORDTYPE_PROJECTED,  "projected",    "Mipmapping with perspective projection"                        }
718         };
719
720         // Cubemap cases.
721         for (int coordType = 0; coordType < DE_LENGTH_OF_ARRAY(cubeCoordTypes); coordType++)
722         {
723                 tcu::TestCaseGroup* coordTypeGroup = new tcu::TestCaseGroup(m_testCtx, cubeCoordTypes[coordType].name, cubeCoordTypes[coordType].desc);
724                 groupCube->addChild(coordTypeGroup);
725
726                 for (int minFilter = 0; minFilter < DE_LENGTH_OF_ARRAY(minFilterModes); minFilter++)
727                 {
728                         coordTypeGroup->addChild(new TextureCubeMipmapCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
729                                                                                                                            minFilterModes[minFilter].name, "",
730                                                                                                                            cubeCoordTypes[coordType].type,
731                                                                                                                            minFilterModes[minFilter].mode,
732                                                                                                                            GL_CLAMP_TO_EDGE,
733                                                                                                                            GL_CLAMP_TO_EDGE,
734                                                                                                                            GL_RGBA, GL_UNSIGNED_BYTE, cubeMapSize));
735                 }
736         }
737 }
738
739 } // Accuracy
740 } // gles3
741 } // deqp