2e38f7e1a36b0f100137a92d76e90191fb67d9e3
[platform/upstream/VK-GL-CTS.git] / modules / gles2 / functional / es2fTextureFilteringTests.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 Texture filtering tests.
22  *//*--------------------------------------------------------------------*/
23
24 #include "es2fTextureFilteringTests.hpp"
25 #include "glsTextureTestUtil.hpp"
26 #include "gluTexture.hpp"
27 #include "gluStrUtil.hpp"
28 #include "gluTextureUtil.hpp"
29 #include "gluPixelTransfer.hpp"
30 #include "tcuTestLog.hpp"
31 #include "tcuTextureUtil.hpp"
32 #include "tcuTexLookupVerifier.hpp"
33 #include "tcuVectorUtil.hpp"
34 #include "deStringUtil.hpp"
35 #include "glwFunctions.hpp"
36 #include "glwEnums.hpp"
37
38 namespace deqp
39 {
40 namespace gles2
41 {
42 namespace Functional
43 {
44
45 using tcu::TestLog;
46 using std::vector;
47 using std::string;
48 using tcu::Sampler;
49 using namespace glu;
50 using namespace gls::TextureTestUtil;
51 using namespace glu::TextureTestUtil;
52
53 enum
54 {
55         VIEWPORT_WIDTH          = 64,
56         VIEWPORT_HEIGHT         = 64,
57         MIN_VIEWPORT_WIDTH      = 64,
58         MIN_VIEWPORT_HEIGHT     = 64
59 };
60
61 class Texture2DFilteringCase : public tcu::TestCase
62 {
63 public:
64                                                                         Texture2DFilteringCase          (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, deUint32 format, deUint32 dataType, int width, int height);
65                                                                         Texture2DFilteringCase          (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, const std::vector<std::string>& filenames);
66                                                                         ~Texture2DFilteringCase         (void);
67
68         void                                                    init                                            (void);
69         void                                                    deinit                                          (void);
70         IterateResult                                   iterate                                         (void);
71
72 private:
73                                                                         Texture2DFilteringCase          (const Texture2DFilteringCase& other);
74         Texture2DFilteringCase&                 operator=                                       (const Texture2DFilteringCase& other);
75
76         glu::RenderContext&                             m_renderCtx;
77         const glu::ContextInfo&                 m_renderCtxInfo;
78
79         const deUint32                                  m_minFilter;
80         const deUint32                                  m_magFilter;
81         const deUint32                                  m_wrapS;
82         const deUint32                                  m_wrapT;
83
84         const deUint32                                  m_format;
85         const deUint32                                  m_dataType;
86         const int                                               m_width;
87         const int                                               m_height;
88
89         const std::vector<std::string>  m_filenames;
90
91         struct FilterCase
92         {
93                 const glu::Texture2D*   texture;
94                 tcu::Vec2                               minCoord;
95                 tcu::Vec2                               maxCoord;
96
97                 FilterCase (void)
98                         : texture(DE_NULL)
99                 {
100                 }
101
102                 FilterCase (const glu::Texture2D* tex_, const tcu::Vec2& minCoord_, const tcu::Vec2& maxCoord_)
103                         : texture       (tex_)
104                         , minCoord      (minCoord_)
105                         , maxCoord      (maxCoord_)
106                 {
107                 }
108         };
109
110         std::vector<glu::Texture2D*>    m_textures;
111         std::vector<FilterCase>                 m_cases;
112
113         TextureRenderer                                 m_renderer;
114
115         int                                                             m_caseNdx;
116 };
117
118 Texture2DFilteringCase::Texture2DFilteringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, deUint32 format, deUint32 dataType, int width, int height)
119         : TestCase                      (testCtx, name, desc)
120         , m_renderCtx           (renderCtx)
121         , m_renderCtxInfo       (ctxInfo)
122         , m_minFilter           (minFilter)
123         , m_magFilter           (magFilter)
124         , m_wrapS                       (wrapS)
125         , m_wrapT                       (wrapT)
126         , m_format                      (format)
127         , m_dataType            (dataType)
128         , m_width                       (width)
129         , m_height                      (height)
130         , m_renderer            (renderCtx, testCtx.getLog(), glu::GLSL_VERSION_100_ES, glu::PRECISION_MEDIUMP)
131         , m_caseNdx                     (0)
132 {
133 }
134
135 Texture2DFilteringCase::Texture2DFilteringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, const std::vector<std::string>& filenames)
136         : TestCase                      (testCtx, name, desc)
137         , m_renderCtx           (renderCtx)
138         , m_renderCtxInfo       (ctxInfo)
139         , m_minFilter           (minFilter)
140         , m_magFilter           (magFilter)
141         , m_wrapS                       (wrapS)
142         , m_wrapT                       (wrapT)
143         , m_format                      (GL_NONE)
144         , m_dataType            (GL_NONE)
145         , m_width                       (0)
146         , m_height                      (0)
147         , m_filenames           (filenames)
148         , m_renderer            (renderCtx, testCtx.getLog(), glu::GLSL_VERSION_100_ES, glu::PRECISION_MEDIUMP)
149         , m_caseNdx                     (0)
150 {
151 }
152
153 Texture2DFilteringCase::~Texture2DFilteringCase (void)
154 {
155         deinit();
156 }
157
158 void Texture2DFilteringCase::init (void)
159 {
160         try
161         {
162                 if (!m_filenames.empty())
163                 {
164                         m_textures.reserve(1);
165                         m_textures.push_back(glu::Texture2D::create(m_renderCtx, m_renderCtxInfo, m_testCtx.getArchive(), (int)m_filenames.size(), m_filenames));
166                 }
167                 else
168                 {
169                         // Create 2 textures.
170                         m_textures.reserve(2);
171                         for (int ndx = 0; ndx < 2; ndx++)
172                                 m_textures.push_back(new glu::Texture2D(m_renderCtx, m_format, m_dataType, m_width, m_height));
173
174                         bool                                    mipmaps         = deIsPowerOfTwo32(m_width) && deIsPowerOfTwo32(m_height);
175                         int                                             numLevels       = mipmaps ? deLog2Floor32(de::max(m_width, m_height))+1 : 1;
176                         tcu::TextureFormatInfo  fmtInfo         = tcu::getTextureFormatInfo(m_textures[0]->getRefTexture().getFormat());
177                         tcu::Vec4                               cBias           = fmtInfo.valueMin;
178                         tcu::Vec4                               cScale          = fmtInfo.valueMax-fmtInfo.valueMin;
179
180                         // Fill first gradient texture.
181                         for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
182                         {
183                                 tcu::Vec4 gMin = tcu::Vec4(-0.5f, -0.5f, -0.5f, 2.0f)*cScale + cBias;
184                                 tcu::Vec4 gMax = tcu::Vec4( 1.0f,  1.0f,  1.0f, 0.0f)*cScale + cBias;
185
186                                 m_textures[0]->getRefTexture().allocLevel(levelNdx);
187                                 tcu::fillWithComponentGradients(m_textures[0]->getRefTexture().getLevel(levelNdx), gMin, gMax);
188                         }
189
190                         // Fill second with grid texture.
191                         for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
192                         {
193                                 deUint32        step    = 0x00ffffff / numLevels;
194                                 deUint32        rgb             = step*levelNdx;
195                                 deUint32        colorA  = 0xff000000 | rgb;
196                                 deUint32        colorB  = 0xff000000 | ~rgb;
197
198                                 m_textures[1]->getRefTexture().allocLevel(levelNdx);
199                                 tcu::fillWithGrid(m_textures[1]->getRefTexture().getLevel(levelNdx), 4, tcu::RGBA(colorA).toVec()*cScale + cBias, tcu::RGBA(colorB).toVec()*cScale + cBias);
200                         }
201
202                         // Upload.
203                         for (std::vector<glu::Texture2D*>::iterator i = m_textures.begin(); i != m_textures.end(); i++)
204                                 (*i)->upload();
205                 }
206
207                 // Compute cases.
208                 {
209                         const struct
210                         {
211                                 int             texNdx;
212                                 float   lodX;
213                                 float   lodY;
214                                 float   oX;
215                                 float   oY;
216                         } cases[] =
217                         {
218                                 { 0,    1.6f,   2.9f,   -1.0f,  -2.7f   },
219                                 { 0,    -2.0f,  -1.35f, -0.2f,  0.7f    },
220                                 { 1,    0.14f,  0.275f, -1.5f,  -1.1f   },
221                                 { 1,    -0.92f, -2.64f, 0.4f,   -0.1f   },
222                         };
223
224                         const float     viewportW       = (float)de::min<int>(VIEWPORT_WIDTH, m_renderCtx.getRenderTarget().getWidth());
225                         const float     viewportH       = (float)de::min<int>(VIEWPORT_HEIGHT, m_renderCtx.getRenderTarget().getHeight());
226
227                         for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); caseNdx++)
228                         {
229                                 const int       texNdx  = de::clamp(cases[caseNdx].texNdx, 0, (int)m_textures.size()-1);
230                                 const float     lodX    = cases[caseNdx].lodX;
231                                 const float     lodY    = cases[caseNdx].lodY;
232                                 const float     oX              = cases[caseNdx].oX;
233                                 const float     oY              = cases[caseNdx].oY;
234                                 const float     sX              = deFloatExp2(lodX)*viewportW / float(m_textures[texNdx]->getRefTexture().getWidth());
235                                 const float     sY              = deFloatExp2(lodY)*viewportH / float(m_textures[texNdx]->getRefTexture().getHeight());
236
237                                 m_cases.push_back(FilterCase(m_textures[texNdx], tcu::Vec2(oX, oY), tcu::Vec2(oX+sX, oY+sY)));
238                         }
239                 }
240
241                 m_caseNdx = 0;
242                 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
243         }
244         catch (...)
245         {
246                 // Clean up to save memory.
247                 Texture2DFilteringCase::deinit();
248                 throw;
249         }
250 }
251
252 void Texture2DFilteringCase::deinit (void)
253 {
254         for (std::vector<glu::Texture2D*>::iterator i = m_textures.begin(); i != m_textures.end(); i++)
255                 delete *i;
256         m_textures.clear();
257
258         m_renderer.clear();
259         m_cases.clear();
260 }
261
262 Texture2DFilteringCase::IterateResult Texture2DFilteringCase::iterate (void)
263 {
264         const glw::Functions&                   gl                      = m_renderCtx.getFunctions();
265         const RandomViewport                    viewport        (m_renderCtx.getRenderTarget(), VIEWPORT_WIDTH, VIEWPORT_HEIGHT, deStringHash(getName()) ^ deInt32Hash(m_caseNdx));
266         const tcu::TextureFormat                texFmt          = m_textures[0]->getRefTexture().getFormat();
267         const tcu::TextureFormatInfo    fmtInfo         = tcu::getTextureFormatInfo(texFmt);
268         const FilterCase&                               curCase         = m_cases[m_caseNdx];
269         const tcu::ScopedLogSection             section         (m_testCtx.getLog(), string("Test") + de::toString(m_caseNdx), string("Test ") + de::toString(m_caseNdx));
270         ReferenceParams                                 refParams       (TEXTURETYPE_2D);
271         tcu::Surface                                    rendered        (viewport.width, viewport.height);
272         vector<float>                                   texCoord;
273
274         if (viewport.width < MIN_VIEWPORT_WIDTH || viewport.height < MIN_VIEWPORT_HEIGHT)
275                 throw tcu::NotSupportedError("Too small viewport", "", __FILE__, __LINE__);
276
277         // Setup params for reference.
278         refParams.sampler               = mapGLSampler(m_wrapS, m_wrapT, m_minFilter, m_magFilter);
279         refParams.samplerType   = getSamplerType(texFmt);
280         refParams.lodMode               = LODMODE_EXACT;
281         refParams.colorBias             = fmtInfo.lookupBias;
282         refParams.colorScale    = fmtInfo.lookupScale;
283
284         // Compute texture coordinates.
285         m_testCtx.getLog() << TestLog::Message << "Texture coordinates: " << curCase.minCoord << " -> " << curCase.maxCoord << TestLog::EndMessage;
286         computeQuadTexCoord2D(texCoord, curCase.minCoord, curCase.maxCoord);
287
288         gl.bindTexture  (GL_TEXTURE_2D, curCase.texture->getGLTexture());
289         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,  m_minFilter);
290         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,  m_magFilter);
291         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,              m_wrapS);
292         gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,              m_wrapT);
293
294         gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
295         m_renderer.renderQuad(0, &texCoord[0], refParams);
296         glu::readPixels(m_renderCtx, viewport.x, viewport.y, rendered.getAccess());
297
298         {
299                 const bool                              isNearestOnly   = m_minFilter == GL_NEAREST && m_magFilter == GL_NEAREST;
300                 const tcu::PixelFormat  pixelFormat             = m_renderCtx.getRenderTarget().getPixelFormat();
301                 const tcu::IVec4                colorBits               = max(getBitsVec(pixelFormat) - (isNearestOnly ? 1 : 2), tcu::IVec4(0)); // 1 inaccurate bit if nearest only, 2 otherwise
302                 tcu::LodPrecision               lodPrecision;
303                 tcu::LookupPrecision    lookupPrecision;
304
305                 lodPrecision.derivateBits               = 7;
306                 lodPrecision.lodBits                    = 4;
307                 lookupPrecision.colorThreshold  = tcu::computeFixedPointThreshold(colorBits) / refParams.colorScale;
308                 lookupPrecision.coordBits               = tcu::IVec3(9,9,0); // mediump interpolation
309                 lookupPrecision.uvwBits                 = tcu::IVec3(5,5,0);
310                 lookupPrecision.colorMask               = getCompareMask(pixelFormat);
311
312                 const bool isOk = verifyTextureResult(m_testCtx, rendered.getAccess(), curCase.texture->getRefTexture(),
313                                                                                           &texCoord[0], refParams, lookupPrecision, lodPrecision, pixelFormat);
314
315                 if (!isOk)
316                         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image verification failed");
317         }
318
319         m_caseNdx += 1;
320         return m_caseNdx < (int)m_cases.size() ? CONTINUE : STOP;
321 }
322
323
324 class TextureCubeFilteringCase : public tcu::TestCase
325 {
326 public:
327                                                                         TextureCubeFilteringCase        (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, deUint32 format, deUint32 dataType, int width, int height);
328                                                                         TextureCubeFilteringCase        (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, const std::vector<std::string>& filenames);
329                                                                         ~TextureCubeFilteringCase       (void);
330
331         void                                                    init                                            (void);
332         void                                                    deinit                                          (void);
333         IterateResult                                   iterate                                         (void);
334
335 private:
336                                                                         TextureCubeFilteringCase        (const TextureCubeFilteringCase& other);
337         TextureCubeFilteringCase&               operator=                                       (const TextureCubeFilteringCase& other);
338
339         glu::RenderContext&                             m_renderCtx;
340         const glu::ContextInfo&                 m_renderCtxInfo;
341
342         const deUint32                                  m_minFilter;
343         const deUint32                                  m_magFilter;
344         const deUint32                                  m_wrapS;
345         const deUint32                                  m_wrapT;
346
347         const deUint32                                  m_format;
348         const deUint32                                  m_dataType;
349         const int                                               m_width;
350         const int                                               m_height;
351
352         const std::vector<std::string>  m_filenames;
353
354         struct FilterCase
355         {
356                 const glu::TextureCube* texture;
357                 tcu::Vec2                               bottomLeft;
358                 tcu::Vec2                               topRight;
359
360                 FilterCase (void)
361                         : texture(DE_NULL)
362                 {
363                 }
364
365                 FilterCase (const glu::TextureCube* tex_, const tcu::Vec2& bottomLeft_, const tcu::Vec2& topRight_)
366                         : texture       (tex_)
367                         , bottomLeft(bottomLeft_)
368                         , topRight      (topRight_)
369                 {
370                 }
371         };
372
373         std::vector<glu::TextureCube*>  m_textures;
374         std::vector<FilterCase>                 m_cases;
375
376         TextureRenderer                                 m_renderer;
377
378         int                                                             m_caseNdx;
379 };
380
381 TextureCubeFilteringCase::TextureCubeFilteringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, deUint32 format, deUint32 dataType, int width, int height)
382         : TestCase                                      (testCtx, name, desc)
383         , m_renderCtx                           (renderCtx)
384         , m_renderCtxInfo                       (ctxInfo)
385         , m_minFilter                           (minFilter)
386         , m_magFilter                           (magFilter)
387         , m_wrapS                                       (wrapS)
388         , m_wrapT                                       (wrapT)
389         , m_format                                      (format)
390         , m_dataType                            (dataType)
391         , m_width                                       (width)
392         , m_height                                      (height)
393         , m_renderer                            (renderCtx, testCtx.getLog(), glu::GLSL_VERSION_100_ES, glu::PRECISION_MEDIUMP)
394         , m_caseNdx                                     (0)
395 {
396 }
397
398 TextureCubeFilteringCase::TextureCubeFilteringCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo, const char* name, const char* desc, deUint32 minFilter, deUint32 magFilter, deUint32 wrapS, deUint32 wrapT, const std::vector<std::string>& filenames)
399         : TestCase                                      (testCtx, name, desc)
400         , m_renderCtx                           (renderCtx)
401         , m_renderCtxInfo                       (ctxInfo)
402         , m_minFilter                           (minFilter)
403         , m_magFilter                           (magFilter)
404         , m_wrapS                                       (wrapS)
405         , m_wrapT                                       (wrapT)
406         , m_format                                      (GL_NONE)
407         , m_dataType                            (GL_NONE)
408         , m_width                                       (0)
409         , m_height                                      (0)
410         , m_filenames                           (filenames)
411         , m_renderer                            (renderCtx, testCtx.getLog(), glu::GLSL_VERSION_100_ES, glu::PRECISION_MEDIUMP)
412         , m_caseNdx                                     (0)
413 {
414 }
415
416 TextureCubeFilteringCase::~TextureCubeFilteringCase (void)
417 {
418         deinit();
419 }
420
421 void TextureCubeFilteringCase::init (void)
422 {
423         try
424         {
425                 if (!m_filenames.empty())
426                 {
427                         m_textures.reserve(1);
428                         m_textures.push_back(glu::TextureCube::create(m_renderCtx, m_renderCtxInfo, m_testCtx.getArchive(), (int)m_filenames.size() / 6, m_filenames));
429                 }
430                 else
431                 {
432                         DE_ASSERT(m_width == m_height);
433                         m_textures.reserve(2);
434                         for (int ndx = 0; ndx < 2; ndx++)
435                                 m_textures.push_back(new glu::TextureCube(m_renderCtx, m_format, m_dataType, m_width));
436
437                         const bool                              mipmaps         = deIsPowerOfTwo32(m_width) && deIsPowerOfTwo32(m_height);
438                         const int                               numLevels       = mipmaps ? deLog2Floor32(de::max(m_width, m_height))+1 : 1;
439                         tcu::TextureFormatInfo  fmtInfo         = tcu::getTextureFormatInfo(m_textures[0]->getRefTexture().getFormat());
440                         tcu::Vec4                               cBias           = fmtInfo.valueMin;
441                         tcu::Vec4                               cScale          = fmtInfo.valueMax-fmtInfo.valueMin;
442
443                         // Fill first with gradient texture.
444                         static const tcu::Vec4 gradients[tcu::CUBEFACE_LAST][2] =
445                         {
446                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 0.0f) }, // negative x
447                                 { tcu::Vec4(0.5f, 0.0f, 0.0f, 1.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 0.0f) }, // positive x
448                                 { tcu::Vec4(0.0f, 0.5f, 0.0f, 1.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 0.0f) }, // negative y
449                                 { tcu::Vec4(0.0f, 0.0f, 0.5f, 1.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 0.0f) }, // positive y
450                                 { tcu::Vec4(0.0f, 0.0f, 0.0f, 0.5f), tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f) }, // negative z
451                                 { tcu::Vec4(0.5f, 0.5f, 0.5f, 1.0f), tcu::Vec4(1.0f, 1.0f, 1.0f, 0.0f) }  // positive z
452                         };
453                         for (int face = 0; face < tcu::CUBEFACE_LAST; face++)
454                         {
455                                 for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
456                                 {
457                                         m_textures[0]->getRefTexture().allocLevel((tcu::CubeFace)face, levelNdx);
458                                         tcu::fillWithComponentGradients(m_textures[0]->getRefTexture().getLevelFace(levelNdx, (tcu::CubeFace)face), gradients[face][0]*cScale + cBias, gradients[face][1]*cScale + cBias);
459                                 }
460                         }
461
462                         // Fill second with grid texture.
463                         for (int face = 0; face < tcu::CUBEFACE_LAST; face++)
464                         {
465                                 for (int levelNdx = 0; levelNdx < numLevels; levelNdx++)
466                                 {
467                                         deUint32        step    = 0x00ffffff / (numLevels*tcu::CUBEFACE_LAST);
468                                         deUint32        rgb             = step*levelNdx*face;
469                                         deUint32        colorA  = 0xff000000 | rgb;
470                                         deUint32        colorB  = 0xff000000 | ~rgb;
471
472                                         m_textures[1]->getRefTexture().allocLevel((tcu::CubeFace)face, levelNdx);
473                                         tcu::fillWithGrid(m_textures[1]->getRefTexture().getLevelFace(levelNdx, (tcu::CubeFace)face), 4, tcu::RGBA(colorA).toVec()*cScale + cBias, tcu::RGBA(colorB).toVec()*cScale + cBias);
474                                 }
475                         }
476
477                         // Upload.
478                         for (std::vector<glu::TextureCube*>::iterator i = m_textures.begin(); i != m_textures.end(); i++)
479                                 (*i)->upload();
480                 }
481
482                 // Compute cases
483                 {
484                         const glu::TextureCube* tex0    = m_textures[0];
485                         const glu::TextureCube* tex1    = m_textures.size() > 1 ? m_textures[1] : tex0;
486
487                         // \note Coordinates are chosen so that they only sample face interior. ES3 has changed edge sampling behavior
488                         //               and hw is not expected to implement both modes.
489                         m_cases.push_back(FilterCase(tex0, tcu::Vec2(-0.8f, -0.8f), tcu::Vec2(0.8f, 0.8f)));    // minification
490                         m_cases.push_back(FilterCase(tex0, tcu::Vec2(0.5f, 0.65f), tcu::Vec2(0.8f, 0.8f)));             // magnification
491                         m_cases.push_back(FilterCase(tex1, tcu::Vec2(-0.8f, -0.8f), tcu::Vec2(0.8f, 0.8f)));    // minification
492                         m_cases.push_back(FilterCase(tex1, tcu::Vec2(0.2f, 0.2f), tcu::Vec2(0.6f, 0.5f)));              // magnification
493                 }
494
495                 m_caseNdx = 0;
496                 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
497         }
498         catch (...)
499         {
500                 // Clean up to save memory.
501                 TextureCubeFilteringCase::deinit();
502                 throw;
503         }
504 }
505
506 void TextureCubeFilteringCase::deinit (void)
507 {
508         for (std::vector<glu::TextureCube*>::iterator i = m_textures.begin(); i != m_textures.end(); i++)
509                 delete *i;
510         m_textures.clear();
511
512         m_renderer.clear();
513         m_cases.clear();
514 }
515
516 static const char* getFaceDesc (const tcu::CubeFace face)
517 {
518         switch (face)
519         {
520                 case tcu::CUBEFACE_NEGATIVE_X:  return "-X";
521                 case tcu::CUBEFACE_POSITIVE_X:  return "+X";
522                 case tcu::CUBEFACE_NEGATIVE_Y:  return "-Y";
523                 case tcu::CUBEFACE_POSITIVE_Y:  return "+Y";
524                 case tcu::CUBEFACE_NEGATIVE_Z:  return "-Z";
525                 case tcu::CUBEFACE_POSITIVE_Z:  return "+Z";
526                 default:
527                         DE_ASSERT(false);
528                         return DE_NULL;
529         }
530 }
531
532 TextureCubeFilteringCase::IterateResult TextureCubeFilteringCase::iterate (void)
533 {
534         const glw::Functions&                   gl                              = m_renderCtx.getFunctions();
535         const int                                               viewportSize    = 28;
536         const RandomViewport                    viewport                (m_renderCtx.getRenderTarget(), viewportSize, viewportSize, deStringHash(getName()) ^ deInt32Hash(m_caseNdx));
537         const tcu::ScopedLogSection             iterSection             (m_testCtx.getLog(), string("Test") + de::toString(m_caseNdx), string("Test ") + de::toString(m_caseNdx));
538         const FilterCase&                               curCase                 = m_cases[m_caseNdx];
539         const tcu::TextureFormat&               texFmt                  = curCase.texture->getRefTexture().getFormat();
540         const tcu::TextureFormatInfo    fmtInfo                 = tcu::getTextureFormatInfo(texFmt);
541         ReferenceParams                                 sampleParams    (TEXTURETYPE_CUBE);
542
543         if (viewport.width < viewportSize || viewport.height < viewportSize)
544                 throw tcu::NotSupportedError("Too small render target", DE_NULL, __FILE__, __LINE__);
545
546         // Setup texture
547         gl.bindTexture  (GL_TEXTURE_CUBE_MAP, curCase.texture->getGLTexture());
548         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,    m_minFilter);
549         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,    m_magFilter);
550         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,                m_wrapS);
551         gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,                m_wrapT);
552
553         // Other state
554         gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
555
556         // Params for reference computation.
557         sampleParams.sampler                                    = glu::mapGLSampler(m_wrapS, m_wrapT, m_minFilter, m_magFilter);
558         sampleParams.sampler.seamlessCubeMap    = true;
559         sampleParams.samplerType                                = getSamplerType(texFmt);
560         sampleParams.colorBias                                  = fmtInfo.lookupBias;
561         sampleParams.colorScale                                 = fmtInfo.lookupScale;
562         sampleParams.lodMode                                    = LODMODE_EXACT;
563
564         m_testCtx.getLog() << TestLog::Message << "Coordinates: " << curCase.bottomLeft << " -> " << curCase.topRight << TestLog::EndMessage;
565
566         for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; faceNdx++)
567         {
568                 const tcu::CubeFace             face            = tcu::CubeFace(faceNdx);
569                 tcu::Surface                    result          (viewport.width, viewport.height);
570                 vector<float>                   texCoord;
571
572                 computeQuadTexCoordCube(texCoord, face, curCase.bottomLeft, curCase.topRight);
573
574                 m_testCtx.getLog() << TestLog::Message << "Face " << getFaceDesc(face) << TestLog::EndMessage;
575
576                 // \todo Log texture coordinates.
577
578                 m_renderer.renderQuad(0, &texCoord[0], sampleParams);
579                 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw");
580
581                 glu::readPixels(m_renderCtx, viewport.x, viewport.y, result.getAccess());
582                 GLU_EXPECT_NO_ERROR(gl.getError(), "Read pixels");
583
584                 {
585                         const bool                              isNearestOnly   = m_minFilter == GL_NEAREST && m_magFilter == GL_NEAREST;
586                         const tcu::PixelFormat  pixelFormat             = m_renderCtx.getRenderTarget().getPixelFormat();
587                         const tcu::IVec4                colorBits               = max(getBitsVec(pixelFormat) - (isNearestOnly ? 1 : 2), tcu::IVec4(0)); // 1 inaccurate bit if nearest only, 2 otherwise
588                         tcu::LodPrecision               lodPrecision;
589                         tcu::LookupPrecision    lookupPrecision;
590
591                         lodPrecision.derivateBits               = 5;
592                         lodPrecision.lodBits                    = 3;
593                         lookupPrecision.colorThreshold  = tcu::computeFixedPointThreshold(colorBits) / sampleParams.colorScale;
594                         lookupPrecision.coordBits               = tcu::IVec3(9,9,9); // mediump interpolation
595                         lookupPrecision.uvwBits                 = tcu::IVec3(3,3,0);
596                         lookupPrecision.colorMask               = getCompareMask(pixelFormat);
597
598                         const bool isOk = verifyTextureResult(m_testCtx, result.getAccess(), curCase.texture->getRefTexture(),
599                                                                                                   &texCoord[0], sampleParams, lookupPrecision, lodPrecision, pixelFormat);
600
601                         if (!isOk)
602                                 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image verification failed");
603                 }
604         }
605
606         m_caseNdx += 1;
607         return m_caseNdx < (int)m_cases.size() ? CONTINUE : STOP;
608 }
609
610 TextureFilteringTests::TextureFilteringTests (Context& context)
611         : TestCaseGroup(context, "filtering", "Texture Filtering Tests")
612 {
613 }
614
615 TextureFilteringTests::~TextureFilteringTests (void)
616 {
617 }
618
619 void TextureFilteringTests::init (void)
620 {
621         tcu::TestCaseGroup* group2D             = new tcu::TestCaseGroup(m_testCtx, "2d",       "2D Texture Filtering");
622         tcu::TestCaseGroup*     groupCube       = new tcu::TestCaseGroup(m_testCtx, "cube",     "Cube Map Filtering");
623         addChild(group2D);
624         addChild(groupCube);
625
626         static const struct
627         {
628                 const char*             name;
629                 deUint32                mode;
630         } wrapModes[] =
631         {
632                 { "clamp",              GL_CLAMP_TO_EDGE },
633                 { "repeat",             GL_REPEAT },
634                 { "mirror",             GL_MIRRORED_REPEAT }
635         };
636
637         static const struct
638         {
639                 const char*             name;
640                 deUint32                mode;
641         } minFilterModes[] =
642         {
643                 { "nearest",                            GL_NEAREST                                      },
644                 { "linear",                                     GL_LINEAR                                       },
645                 { "nearest_mipmap_nearest",     GL_NEAREST_MIPMAP_NEAREST       },
646                 { "linear_mipmap_nearest",      GL_LINEAR_MIPMAP_NEAREST        },
647                 { "nearest_mipmap_linear",      GL_NEAREST_MIPMAP_LINEAR        },
648                 { "linear_mipmap_linear",       GL_LINEAR_MIPMAP_LINEAR         }
649         };
650
651         static const struct
652         {
653                 const char*             name;
654                 deUint32                mode;
655         } magFilterModes[] =
656         {
657                 { "nearest",    GL_NEAREST },
658                 { "linear",             GL_LINEAR }
659         };
660
661         static const struct
662         {
663                 const char*             name;
664                 int                             width;
665                 int                             height;
666         } sizes2D[] =
667         {
668                 { "pot",                32, 64 },
669                 { "npot",               31, 55 }
670         };
671
672         static const struct
673         {
674                 const char*             name;
675                 int                             width;
676                 int                             height;
677         } sizesCube[] =
678         {
679                 { "pot",                64, 64 },
680                 { "npot",               63, 63 }
681         };
682
683         static const struct
684         {
685                 const char*             name;
686                 deUint32                format;
687                 deUint32                dataType;
688         } formats[] =
689         {
690                 { "rgba8888",   GL_RGBA,                        GL_UNSIGNED_BYTE                        },
691                 { "rgb888",             GL_RGB,                         GL_UNSIGNED_BYTE                        },
692                 { "rgba4444",   GL_RGBA,                        GL_UNSIGNED_SHORT_4_4_4_4       },
693                 { "l8",                 GL_LUMINANCE,           GL_UNSIGNED_BYTE                        }
694         };
695
696 #define FOR_EACH(ITERATOR, ARRAY, BODY) \
697         for (int (ITERATOR) = 0; (ITERATOR) < DE_LENGTH_OF_ARRAY(ARRAY); (ITERATOR)++)  \
698                 BODY
699
700         // 2D cases.
701         FOR_EACH(minFilter,             minFilterModes,
702         FOR_EACH(magFilter,             magFilterModes,
703         FOR_EACH(wrapMode,              wrapModes,
704         FOR_EACH(format,                formats,
705         FOR_EACH(size,                  sizes2D,
706                 {
707                         bool isMipmap           = minFilterModes[minFilter].mode != GL_NEAREST && minFilterModes[minFilter].mode != GL_LINEAR;
708                         bool isClamp            = wrapModes[wrapMode].mode == GL_CLAMP_TO_EDGE;
709                         bool isRepeat           = wrapModes[wrapMode].mode == GL_REPEAT;
710                         bool isMagNearest       = magFilterModes[magFilter].mode == GL_NEAREST;
711                         bool isPotSize          = deIsPowerOfTwo32(sizes2D[size].width) && deIsPowerOfTwo32(sizes2D[size].height);
712
713                         if ((isMipmap || !isClamp) && !isPotSize)
714                                 continue; // Not supported.
715
716                         if ((format != 0) && !(!isMipmap || (isRepeat && isMagNearest)))
717                                 continue; // Skip.
718
719                         string name = string("") + minFilterModes[minFilter].name + "_" + magFilterModes[magFilter].name + "_" + wrapModes[wrapMode].name + "_" + formats[format].name;
720
721                         if (!isMipmap)
722                                 name += string("_") + sizes2D[size].name;
723
724                         group2D->addChild(new Texture2DFilteringCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
725                                                                                                                  name.c_str(), "",
726                                                                                                                  minFilterModes[minFilter].mode,
727                                                                                                                  magFilterModes[magFilter].mode,
728                                                                                                                  wrapModes[wrapMode].mode,
729                                                                                                                  wrapModes[wrapMode].mode,
730                                                                                                                  formats[format].format, formats[format].dataType,
731                                                                                                                  sizes2D[size].width, sizes2D[size].height));
732                 })))));
733
734         // 2D ETC1 texture cases.
735         {
736                 std::vector<std::string> filenames;
737                 for (int i = 0; i <= 7; i++)
738                         filenames.push_back(string("data/etc1/photo_helsinki_mip_") + de::toString(i) + ".pkm");
739
740                 FOR_EACH(minFilter,             minFilterModes,
741                 FOR_EACH(magFilter,             magFilterModes,
742                 FOR_EACH(wrapMode,              wrapModes,
743                         {
744                                 string name = string("") + minFilterModes[minFilter].name + "_" + magFilterModes[magFilter].name + "_" + wrapModes[wrapMode].name + "_etc1";
745
746                                 group2D->addChild(new Texture2DFilteringCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
747                                                                                                                          name.c_str(), "",
748                                                                                                                          minFilterModes[minFilter].mode,
749                                                                                                                          magFilterModes[magFilter].mode,
750                                                                                                                          wrapModes[wrapMode].mode,
751                                                                                                                          wrapModes[wrapMode].mode,
752                                                                                                                          filenames));
753                         })));
754         }
755
756         // Cubemap cases.
757         FOR_EACH(minFilter,             minFilterModes,
758         FOR_EACH(magFilter,             magFilterModes,
759         FOR_EACH(wrapMode,              wrapModes,
760         FOR_EACH(format,                formats,
761         FOR_EACH(size,                  sizesCube,
762                 {
763                         bool isMipmap           = minFilterModes[minFilter].mode != GL_NEAREST && minFilterModes[minFilter].mode != GL_LINEAR;
764                         bool isClamp            = wrapModes[wrapMode].mode == GL_CLAMP_TO_EDGE;
765                         bool isRepeat           = wrapModes[wrapMode].mode == GL_REPEAT;
766                         bool isMagNearest       = magFilterModes[magFilter].mode == GL_NEAREST;
767                         bool isPotSize          = deIsPowerOfTwo32(sizesCube[size].width) && deIsPowerOfTwo32(sizesCube[size].height);
768
769                         if ((isMipmap || !isClamp) && !isPotSize)
770                                 continue; // Not supported.
771
772                         if (format != 0 && !(!isMipmap || (isRepeat && isMagNearest)))
773                                 continue; // Skip.
774
775                         string name = string("") + minFilterModes[minFilter].name + "_" + magFilterModes[magFilter].name + "_" + wrapModes[wrapMode].name + "_" + formats[format].name;
776
777                         if (!isMipmap)
778                                 name += string("_") + sizesCube[size].name;
779
780                         groupCube->addChild(new TextureCubeFilteringCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
781                                                                                                                          name.c_str(), "",
782                                                                                                                          minFilterModes[minFilter].mode,
783                                                                                                                          magFilterModes[magFilter].mode,
784                                                                                                                          wrapModes[wrapMode].mode,
785                                                                                                                          wrapModes[wrapMode].mode,
786                                                                                                                          formats[format].format, formats[format].dataType,
787                                                                                                                          sizesCube[size].width, sizesCube[size].height));
788                 })))));
789
790         // Cubemap ETC1 cases
791         {
792                 static const char* faceExt[] = { "neg_x", "pos_x", "neg_y", "pos_y", "neg_z", "pos_z" };
793
794                 const int               numLevels       = 7;
795                 vector<string>  filenames;
796                 for (int level = 0; level < numLevels; level++)
797                         for (int face = 0; face < tcu::CUBEFACE_LAST; face++)
798                                 filenames.push_back(string("data/etc1/skybox_") + faceExt[face] + "_mip_" + de::toString(level) + ".pkm");
799
800                 FOR_EACH(minFilter,             minFilterModes,
801                 FOR_EACH(magFilter,             magFilterModes,
802                         {
803                                 string name = string("") + minFilterModes[minFilter].name + "_" + magFilterModes[magFilter].name + "_clamp_etc1";
804
805                                 groupCube->addChild(new TextureCubeFilteringCase(m_testCtx, m_context.getRenderContext(), m_context.getContextInfo(),
806                                                                                                                                  name.c_str(), "",
807                                                                                                                                  minFilterModes[minFilter].mode,
808                                                                                                                                  magFilterModes[magFilter].mode,
809                                                                                                                                  GL_CLAMP_TO_EDGE,
810                                                                                                                                  GL_CLAMP_TO_EDGE,
811                                                                                                                                  filenames));
812                         }));
813         }
814 }
815
816 } // Functional
817 } // gles2
818 } // deqp