• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * OpenGL Conformance Test Suite
3  * -----------------------------
4  *
5  * Copyright (c) 2017 The Khronos Group Inc.
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 glcPackedPixelsTests.cpp
21  * \brief
22  */ /*-------------------------------------------------------------------*/
23 
24 #include "glcPackedPixelsTests.hpp"
25 #include "deMath.h"
26 #include "glcMisc.hpp"
27 #include "gluContextInfo.hpp"
28 #include "gluShaderProgram.hpp"
29 #include "gluStrUtil.hpp"
30 #include "glwEnums.hpp"
31 #include "glwFunctions.hpp"
32 #include "tcuRenderTarget.hpp"
33 #include "tcuTestLog.hpp"
34 #include <algorithm>
35 #include <cstring>
36 #include <limits>
37 #include <map>
38 #include <stdio.h>
39 
40 using namespace glw;
41 using namespace glu;
42 
43 namespace glcts
44 {
45 
46 enum
47 {
48     GRADIENT_WIDTH  = 7,
49     GRADIENT_HEIGHT = 3
50 };
51 
52 enum InputOutputOperation
53 {
54     OUTPUT_GETTEXIMAGE,
55     OUTPUT_READPIXELS,
56     INPUT_TEXIMAGE,
57 };
58 
59 enum ComponentFormat
60 {
61     FORMAT_STENCIL,       // stencil, unsigned int
62     FORMAT_DEPTH,         // depth, unsigned [fp|float]
63     FORMAT_DEPTH_STENCIL, // depth+stencil, unsigned [fp|float]
64     FORMAT_COLOR,         // color, [signed|unsigned] fp
65     FORMAT_COLOR_INTEGER, // color, [signed|unsigned] int
66 };
67 
68 enum TypeStorage
69 {
70     STORAGE_UNSIGNED, // unsigned fp/int
71     STORAGE_SIGNED,   // signed fp/int
72     STORAGE_FLOAT,    // signed/unsigned float
73 };
74 
75 union InternalFormatBits
76 {
77     struct Bits
78     {
79         int red;       // red bits
80         int green;     // green bits
81         int blue;      // blue bits
82         int alpha;     // alpha bits
83         int intensity; // intensity bits
84         int luminance; // luminance bits
85         int depth;     // depth bits
86         int stencil;   // stencil bits
87         int exponent;  // shared exponent bits
88     } bits;
89     int array[9]; // all the bits
90 };
91 
92 struct PixelType
93 {
94     GLenum type;
95     int size;
96     int storage;
97     bool special;
98     bool reversed;
99     InternalFormatBits bits;
100     bool clamp;
101 };
102 
103 struct PixelFormat
104 {
105     GLenum format;                     // format name
106     int components;                    // number of components
107     int componentFormat;               // element meaning
108     GLenum attachment;                 // target buffer
109     InternalFormatBits componentOrder; // zero based element order, -1 for N/A
110 };
111 
112 enum InternalFormatSamplerType
113 {
114     SAMPLER_UNORM = 0, // unsigned normalized
115     SAMPLER_NORM,      // normalized
116     SAMPLER_UINT,      // unsigned integer
117     SAMPLER_INT,       // integer
118     SAMPLER_FLOAT      // floating-point
119 };
120 
121 enum InternalFormatFlag
122 {
123     FLAG_PACKED       = 1,                                     // packed pixel format
124     FLAG_COMPRESSED   = 2,                                     // compressed format
125     FLAG_REQ_RBO_GL42 = 4,                                     // required RBO & tex format in OpenGL 4.2
126     FLAG_REQ_RBO_ES30 = 8,                                     // required RBO & tex format in OpenGL ES 3.0
127     FLAG_REQ_RBO      = FLAG_REQ_RBO_GL42 | FLAG_REQ_RBO_ES30, // Required RBO & tex format in both
128 };
129 
130 struct InternalFormat
131 {
132     GLenum sizedFormat;
133     GLenum baseFormat;
134     GLenum format;
135     GLenum type;
136     InternalFormatSamplerType sampler;
137     InternalFormatBits bits;
138     int flags; // InternalFormatFlag
139 };
140 
141 struct EnumFormats
142 {
143     GLenum internalformat;
144     GLenum format;
145     GLenum type;
146     int size;
147     bool bRenderable;
148 };
149 
150 #define PACK_DEFAULTI (0)
151 #define PACK_DEFAULTUI (0)
152 #define PACK_DEFAULTF (-2.0f)
153 
154 static const InternalFormat coreInternalformats[] = {
155     {GL_DEPTH_COMPONENT,
156      GL_DEPTH_COMPONENT,
157      GL_DEPTH_COMPONENT,
158      GL_UNSIGNED_SHORT,
159      SAMPLER_UNORM,
160      {{0, 0, 0, 0, 0, 0, 16, 0, 0}},
161      0},
162     {GL_DEPTH_STENCIL,
163      GL_DEPTH_STENCIL,
164      GL_DEPTH_STENCIL,
165      GL_UNSIGNED_INT,
166      SAMPLER_UNORM,
167      {{0, 0, 0, 0, 0, 0, 24, 8, 0}},
168      0},
169     {GL_RED, GL_RED, GL_RED, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, 0},
170     {GL_RG, GL_RG, GL_RG, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, 0},
171     {GL_R8, GL_RED, GL_RED, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
172     {GL_R8_SNORM, GL_RED, GL_RED, GL_BYTE, SAMPLER_NORM, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, 0},
173     {GL_R16, GL_RED, GL_RED, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
174     {GL_R16_SNORM, GL_RED, GL_RED, GL_SHORT, SAMPLER_NORM, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, 0},
175     {GL_RG8, GL_RG, GL_RG, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
176     {GL_RG8_SNORM, GL_RG, GL_RG, GL_BYTE, SAMPLER_NORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, 0},
177     {GL_RG16, GL_RG, GL_RG, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
178     {GL_RG16_SNORM, GL_RG, GL_RG, GL_SHORT, SAMPLER_NORM, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, 0},
179     {GL_R3_G3_B2, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE_3_3_2, SAMPLER_UNORM, {{3, 3, 2, 0, 0, 0, 0, 0, 0}}, FLAG_PACKED},
180     {GL_RGB4, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{4, 4, 4, 0, 0, 0, 0, 0, 0}}, 0},
181     {GL_RGB5, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{5, 5, 5, 0, 0, 0, 0, 0, 0}}, 0},
182     {GL_RGB8, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_ES30},
183     {GL_RGB8_SNORM, GL_RGB, GL_RGB, GL_BYTE, SAMPLER_NORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
184     {GL_RGB10, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{10, 10, 10, 0, 0, 0, 0, 0, 0}}, 0},
185     {GL_RGB12, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{12, 12, 12, 0, 0, 0, 0, 0, 0}}, 0},
186     {GL_RGB16, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
187     {GL_RGB16_SNORM, GL_RGB, GL_RGB, GL_SHORT, SAMPLER_NORM, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
188     {GL_RGBA2, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{2, 2, 2, 2, 0, 0, 0, 0, 0}}, 0},
189     {GL_RGBA4,
190      GL_RGBA,
191      GL_RGBA,
192      GL_UNSIGNED_SHORT_4_4_4_4,
193      SAMPLER_UNORM,
194      {{4, 4, 4, 4, 0, 0, 0, 0, 0}},
195      FLAG_PACKED | FLAG_REQ_RBO},
196     {GL_RGB5_A1,
197      GL_RGBA,
198      GL_RGBA,
199      GL_UNSIGNED_SHORT_5_5_5_1,
200      SAMPLER_UNORM,
201      {{5, 5, 5, 1, 0, 0, 0, 0, 0}},
202      FLAG_PACKED | FLAG_REQ_RBO},
203     {GL_RGBA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
204     {GL_RGBA8_SNORM, GL_RGBA, GL_RGBA, GL_BYTE, SAMPLER_NORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, 0},
205     {GL_RGB10_A2,
206      GL_RGBA,
207      GL_RGBA,
208      GL_UNSIGNED_INT_10_10_10_2,
209      SAMPLER_UNORM,
210      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
211      FLAG_PACKED | FLAG_REQ_RBO_GL42},
212     {GL_RGB10_A2UI,
213      GL_RGBA,
214      GL_RGBA_INTEGER,
215      GL_UNSIGNED_INT_10_10_10_2,
216      SAMPLER_UINT,
217      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
218      FLAG_PACKED | FLAG_REQ_RBO_GL42},
219     {GL_RGBA12, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT, SAMPLER_UNORM, {{12, 12, 12, 12, 0, 0, 0, 0, 0}}, 0},
220     {GL_RGBA16,
221      GL_RGBA,
222      GL_RGBA,
223      GL_UNSIGNED_SHORT,
224      SAMPLER_UNORM,
225      {{16, 16, 16, 16, 0, 0, 0, 0, 0}},
226      FLAG_REQ_RBO_GL42},
227     {GL_RGBA16_SNORM, GL_RGBA, GL_RGBA, GL_SHORT, SAMPLER_NORM, {{16, 16, 16, 16, 0, 0, 0, 0, 0}}, 0},
228     {GL_SRGB8, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
229     {GL_SRGB8_ALPHA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
230     {GL_R16F, GL_RED, GL_RED, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
231     {GL_RG16F, GL_RG, GL_RG, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
232     {GL_RGB16F, GL_RGB, GL_RGB, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
233     {GL_RGBA16F, GL_RGBA, GL_RGBA, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 16, 16, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
234     {GL_R32F, GL_RED, GL_RED, GL_FLOAT, SAMPLER_FLOAT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
235     {GL_RG32F, GL_RG, GL_RG, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
236     {GL_RGB32F, GL_RGB, GL_RGB, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
237     {GL_RGBA32F, GL_RGBA, GL_RGBA, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 32, 32, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
238     {GL_R11F_G11F_B10F,
239      GL_RGB,
240      GL_RGB,
241      GL_UNSIGNED_INT_10F_11F_11F_REV,
242      SAMPLER_FLOAT,
243      {{11, 11, 10, 0, 0, 0, 0, 0, 0}},
244      FLAG_PACKED | FLAG_REQ_RBO_GL42},
245     {GL_RGB9_E5,
246      GL_RGB,
247      GL_RGB,
248      GL_UNSIGNED_INT_5_9_9_9_REV,
249      SAMPLER_FLOAT,
250      {{9, 9, 9, 0, 0, 0, 0, 0, 5}},
251      FLAG_PACKED},
252     {GL_R8I, GL_RED, GL_RED_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
253     {GL_R8UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
254     {GL_R16I, GL_RED, GL_RED_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
255     {GL_R16UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
256     {GL_R32I, GL_RED, GL_RED_INTEGER, GL_INT, SAMPLER_INT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
257     {GL_R32UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
258     {GL_RG8I, GL_RG, GL_RG_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
259     {GL_RG8UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
260     {GL_RG16I, GL_RG, GL_RG_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
261     {GL_RG16UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
262     {GL_RG32I, GL_RG, GL_RG_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
263     {GL_RG32UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
264     {GL_RGB8I, GL_RGB, GL_RGB_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
265     {GL_RGB8UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
266     {GL_RGB16I, GL_RGB, GL_RGB_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
267     {GL_RGB16UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
268     {GL_RGB32I, GL_RGB, GL_RGB_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
269     {GL_RGB32UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
270     {GL_RGBA8I, GL_RGBA, GL_RGBA_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
271     {GL_RGBA8UI, GL_RGBA, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
272     {GL_RGBA16I, GL_RGBA, GL_RGBA_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 16, 16, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
273     {GL_RGBA16UI,
274      GL_RGBA,
275      GL_RGBA_INTEGER,
276      GL_UNSIGNED_SHORT,
277      SAMPLER_UINT,
278      {{16, 16, 16, 16, 0, 0, 0, 0, 0}},
279      FLAG_REQ_RBO},
280     {GL_RGBA32I, GL_RGBA, GL_RGBA_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 32, 32, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
281     {GL_RGBA32UI,
282      GL_RGBA,
283      GL_RGBA_INTEGER,
284      GL_UNSIGNED_INT,
285      SAMPLER_UINT,
286      {{32, 32, 32, 32, 0, 0, 0, 0, 0}},
287      FLAG_REQ_RBO},
288     {GL_DEPTH_COMPONENT16,
289      GL_DEPTH_COMPONENT,
290      GL_DEPTH_COMPONENT,
291      GL_UNSIGNED_SHORT,
292      SAMPLER_UNORM,
293      {{0, 0, 0, 0, 0, 0, 16, 0, 0}},
294      FLAG_REQ_RBO},
295     {GL_DEPTH_COMPONENT24,
296      GL_DEPTH_COMPONENT,
297      GL_DEPTH_COMPONENT,
298      GL_UNSIGNED_INT,
299      SAMPLER_UNORM,
300      {{0, 0, 0, 0, 0, 0, 24, 0, 0}},
301      FLAG_REQ_RBO},
302     {GL_DEPTH_COMPONENT32,
303      GL_DEPTH_COMPONENT,
304      GL_DEPTH_COMPONENT,
305      GL_UNSIGNED_INT,
306      SAMPLER_UNORM,
307      {{0, 0, 0, 0, 0, 0, 32, 0, 0}},
308      0},
309     {GL_DEPTH_COMPONENT32F,
310      GL_DEPTH_COMPONENT,
311      GL_DEPTH_COMPONENT,
312      GL_FLOAT,
313      SAMPLER_FLOAT,
314      {{0, 0, 0, 0, 0, 0, 32, 0, 0}},
315      FLAG_REQ_RBO},
316     {GL_DEPTH24_STENCIL8,
317      GL_DEPTH_STENCIL,
318      GL_DEPTH_STENCIL,
319      GL_UNSIGNED_INT_24_8,
320      SAMPLER_UNORM,
321      {{0, 0, 0, 0, 0, 0, 24, 8, 0}},
322      FLAG_REQ_RBO},
323     {GL_DEPTH32F_STENCIL8,
324      GL_DEPTH_STENCIL,
325      GL_DEPTH_STENCIL,
326      GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
327      SAMPLER_FLOAT,
328      {{0, 0, 0, 0, 0, 0, 32, 8, 0}},
329      FLAG_PACKED | FLAG_REQ_RBO},
330     {GL_COMPRESSED_RED,
331      GL_RED,
332      GL_RED,
333      GL_UNSIGNED_BYTE,
334      SAMPLER_UNORM,
335      {{8, 0, 0, 0, 0, 0, 0, 0, 0}},
336      FLAG_COMPRESSED},
337     {GL_COMPRESSED_RG, GL_RG, GL_RG, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_COMPRESSED},
338     {GL_COMPRESSED_RGB,
339      GL_RGB,
340      GL_RGB,
341      GL_UNSIGNED_BYTE,
342      SAMPLER_UNORM,
343      {{8, 8, 8, 0, 0, 0, 0, 0, 0}},
344      FLAG_COMPRESSED},
345     {GL_COMPRESSED_RGBA,
346      GL_RGBA,
347      GL_RGBA,
348      GL_UNSIGNED_BYTE,
349      SAMPLER_UNORM,
350      {{8, 8, 8, 8, 0, 0, 0, 0, 0}},
351      FLAG_COMPRESSED},
352     {GL_COMPRESSED_SRGB,
353      GL_RGB,
354      GL_RGB,
355      GL_UNSIGNED_BYTE,
356      SAMPLER_UNORM,
357      {{8, 8, 8, 0, 0, 0, 0, 0, 0}},
358      FLAG_COMPRESSED},
359     {GL_COMPRESSED_SRGB_ALPHA,
360      GL_RGBA,
361      GL_RGBA,
362      GL_UNSIGNED_BYTE,
363      SAMPLER_UNORM,
364      {{8, 8, 8, 8, 0, 0, 0, 0, 0}},
365      FLAG_COMPRESSED},
366     {GL_COMPRESSED_RED_RGTC1,
367      GL_RED,
368      GL_RED,
369      GL_UNSIGNED_BYTE,
370      SAMPLER_UNORM,
371      {{8, 0, 0, 0, 0, 0, 0, 0, 0}},
372      FLAG_COMPRESSED},
373     {GL_COMPRESSED_SIGNED_RED_RGTC1,
374      GL_RED,
375      GL_RED,
376      GL_BYTE,
377      SAMPLER_NORM,
378      {{8, 0, 0, 0, 0, 0, 0, 0, 0}},
379      FLAG_COMPRESSED},
380     {GL_COMPRESSED_RG_RGTC2,
381      GL_RG,
382      GL_RG,
383      GL_UNSIGNED_BYTE,
384      SAMPLER_UNORM,
385      {{8, 8, 0, 0, 0, 0, 0, 0, 0}},
386      FLAG_COMPRESSED},
387     {GL_COMPRESSED_SIGNED_RG_RGTC2,
388      GL_RG,
389      GL_RG,
390      GL_BYTE,
391      SAMPLER_NORM,
392      {{8, 8, 0, 0, 0, 0, 0, 0, 0}},
393      FLAG_COMPRESSED},
394 };
395 
396 static InternalFormat esInternalformats[] = {
397     {GL_LUMINANCE, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{0, 0, 0, 0, 0, 8, 0, 0, 0}}, 0},
398     {GL_ALPHA, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{0, 0, 0, 8, 0, 0, 0, 0, 0}}, 0},
399     {GL_LUMINANCE_ALPHA,
400      GL_LUMINANCE_ALPHA,
401      GL_LUMINANCE_ALPHA,
402      GL_UNSIGNED_BYTE,
403      SAMPLER_UNORM,
404      {{0, 0, 0, 8, 0, 8, 0, 0, 0}},
405      0},
406     {GL_RGB, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
407     {GL_RGBA, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, 0},
408     {GL_R8, GL_RED, GL_RED, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
409     {GL_R8_SNORM, GL_RED, GL_RED, GL_BYTE, SAMPLER_NORM, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, 0},
410     {GL_RG8, GL_RG, GL_RG, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
411     {GL_RG8_SNORM, GL_RG, GL_RG, GL_BYTE, SAMPLER_NORM, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, 0},
412     {GL_RGB8, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_ES30},
413     {GL_RGB8_SNORM, GL_RGB, GL_RGB, GL_BYTE, SAMPLER_NORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
414     {GL_RGB565,
415      GL_RGB,
416      GL_RGB,
417      GL_UNSIGNED_SHORT_5_6_5,
418      SAMPLER_UNORM,
419      {{5, 6, 5, 0, 0, 0, 0, 0, 0}},
420      FLAG_PACKED | FLAG_REQ_RBO},
421     {GL_RGBA4,
422      GL_RGBA,
423      GL_RGBA,
424      GL_UNSIGNED_SHORT_4_4_4_4,
425      SAMPLER_UNORM,
426      {{4, 4, 4, 4, 0, 0, 0, 0, 0}},
427      FLAG_PACKED | FLAG_REQ_RBO},
428     {GL_RGB5_A1,
429      GL_RGBA,
430      GL_RGBA,
431      GL_UNSIGNED_SHORT_5_5_5_1,
432      SAMPLER_UNORM,
433      {{5, 5, 5, 1, 0, 0, 0, 0, 0}},
434      FLAG_PACKED | FLAG_REQ_RBO},
435     {GL_RGBA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
436     {GL_RGBA8_SNORM, GL_RGBA, GL_RGBA, GL_BYTE, SAMPLER_NORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, 0},
437     {GL_RGB10_A2,
438      GL_RGBA,
439      GL_RGBA,
440      GL_UNSIGNED_INT_2_10_10_10_REV,
441      SAMPLER_UNORM,
442      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
443      FLAG_PACKED | FLAG_REQ_RBO_ES30},
444     {GL_RGB10_A2UI,
445      GL_RGBA,
446      GL_RGBA_INTEGER,
447      GL_UNSIGNED_INT_2_10_10_10_REV,
448      SAMPLER_UINT,
449      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
450      FLAG_PACKED | FLAG_REQ_RBO_ES30},
451     {GL_SRGB8, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
452     {GL_SRGB8_ALPHA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, SAMPLER_UNORM, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
453     {GL_R16F, GL_RED, GL_RED, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
454     {GL_RG16F, GL_RG, GL_RG, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
455     {GL_RGB16F, GL_RGB, GL_RGB, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
456     {GL_RGBA16F, GL_RGBA, GL_RGBA, GL_HALF_FLOAT, SAMPLER_FLOAT, {{16, 16, 16, 16, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
457     {GL_R32F, GL_RED, GL_RED, GL_FLOAT, SAMPLER_FLOAT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
458     {GL_RG32F, GL_RG, GL_RG, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
459     {GL_RGB32F, GL_RGB, GL_RGB, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
460     {GL_RGBA32F, GL_RGBA, GL_RGBA, GL_FLOAT, SAMPLER_FLOAT, {{32, 32, 32, 32, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO_GL42},
461     {GL_R11F_G11F_B10F,
462      GL_RGB,
463      GL_RGB,
464      GL_UNSIGNED_INT_10F_11F_11F_REV,
465      SAMPLER_FLOAT,
466      {{11, 11, 10, 0, 0, 0, 0, 0, 0}},
467      FLAG_PACKED | FLAG_REQ_RBO_GL42},
468     {GL_RGB9_E5,
469      GL_RGB,
470      GL_RGB,
471      GL_UNSIGNED_INT_5_9_9_9_REV,
472      SAMPLER_FLOAT,
473      {{9, 9, 9, 0, 0, 0, 0, 0, 5}},
474      FLAG_PACKED},
475     {GL_R8I, GL_RED, GL_RED_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
476     {GL_R8UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
477     {GL_R16I, GL_RED, GL_RED_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
478     {GL_R16UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
479     {GL_R32I, GL_RED, GL_RED_INTEGER, GL_INT, SAMPLER_INT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
480     {GL_R32UI, GL_RED, GL_RED_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 0, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
481     {GL_RG8I, GL_RG, GL_RG_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
482     {GL_RG8UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
483     {GL_RG16I, GL_RG, GL_RG_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
484     {GL_RG16UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 16, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
485     {GL_RG32I, GL_RG, GL_RG_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
486     {GL_RG32UI, GL_RG, GL_RG_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 32, 0, 0, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
487     {GL_RGB8I, GL_RGB, GL_RGB_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
488     {GL_RGB8UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 8, 0, 0, 0, 0, 0, 0}}, 0},
489     {GL_RGB16I, GL_RGB, GL_RGB_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
490     {GL_RGB16UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, SAMPLER_UINT, {{16, 16, 16, 0, 0, 0, 0, 0, 0}}, 0},
491     {GL_RGB32I, GL_RGB, GL_RGB_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
492     {GL_RGB32UI, GL_RGB, GL_RGB_INTEGER, GL_UNSIGNED_INT, SAMPLER_UINT, {{32, 32, 32, 0, 0, 0, 0, 0, 0}}, 0},
493     {GL_RGBA8I, GL_RGBA, GL_RGBA_INTEGER, GL_BYTE, SAMPLER_INT, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
494     {GL_RGBA8UI, GL_RGBA, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, SAMPLER_UINT, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
495     {GL_RGBA16I, GL_RGBA, GL_RGBA_INTEGER, GL_SHORT, SAMPLER_INT, {{16, 16, 16, 16, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
496     {GL_RGBA16UI,
497      GL_RGBA,
498      GL_RGBA_INTEGER,
499      GL_UNSIGNED_SHORT,
500      SAMPLER_UINT,
501      {{16, 16, 16, 16, 0, 0, 0, 0, 0}},
502      FLAG_REQ_RBO},
503     {GL_RGBA32I, GL_RGBA, GL_RGBA_INTEGER, GL_INT, SAMPLER_INT, {{32, 32, 32, 32, 0, 0, 0, 0, 0}}, FLAG_REQ_RBO},
504     {GL_RGBA32UI,
505      GL_RGBA,
506      GL_RGBA_INTEGER,
507      GL_UNSIGNED_INT,
508      SAMPLER_UINT,
509      {{32, 32, 32, 32, 0, 0, 0, 0, 0}},
510      FLAG_REQ_RBO},
511     {GL_DEPTH_COMPONENT16,
512      GL_DEPTH_COMPONENT,
513      GL_DEPTH_COMPONENT,
514      GL_UNSIGNED_SHORT,
515      SAMPLER_UNORM,
516      {{0, 0, 0, 0, 0, 0, 16, 0, 0}},
517      FLAG_REQ_RBO},
518     {GL_DEPTH_COMPONENT24,
519      GL_DEPTH_COMPONENT,
520      GL_DEPTH_COMPONENT,
521      GL_UNSIGNED_INT,
522      SAMPLER_UNORM,
523      {{0, 0, 0, 0, 0, 0, 24, 0, 0}},
524      FLAG_REQ_RBO},
525     {GL_DEPTH_COMPONENT32F,
526      GL_DEPTH_COMPONENT,
527      GL_DEPTH_COMPONENT,
528      GL_FLOAT,
529      SAMPLER_FLOAT,
530      {{0, 0, 0, 0, 0, 0, 32, 0, 0}},
531      FLAG_REQ_RBO},
532     {GL_DEPTH24_STENCIL8,
533      GL_DEPTH_STENCIL,
534      GL_DEPTH_STENCIL,
535      GL_UNSIGNED_INT_24_8,
536      SAMPLER_UNORM,
537      {{0, 0, 0, 0, 0, 0, 24, 8, 0}},
538      FLAG_REQ_RBO},
539     {GL_DEPTH32F_STENCIL8,
540      GL_DEPTH_STENCIL,
541      GL_DEPTH_STENCIL,
542      GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
543      SAMPLER_FLOAT,
544      {{0, 0, 0, 0, 0, 0, 32, 8, 0}},
545      FLAG_PACKED | FLAG_REQ_RBO},
546 };
547 
548 static const PixelFormat coreFormats[] = {
549     {GL_STENCIL_INDEX, 1, FORMAT_STENCIL, GL_STENCIL_ATTACHMENT, {{-1, -1, -1, -1, -1, -1, -1, 0, -1}}},
550     {GL_DEPTH_COMPONENT, 1, FORMAT_DEPTH, GL_DEPTH_ATTACHMENT, {{-1, -1, -1, -1, -1, -1, 0, -1, -1}}},
551     {GL_DEPTH_STENCIL, 2, FORMAT_DEPTH_STENCIL, GL_DEPTH_STENCIL_ATTACHMENT, {{-1, -1, -1, -1, -1, -1, 0, 1, -1}}},
552     {GL_RED, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, -1, -1, -1, -1, -1, -1, -1, -1}}},
553     {GL_GREEN, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{-1, 0, -1, -1, -1, -1, -1, -1, -1}}},
554     {GL_BLUE, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{-1, -1, 0, -1, -1, -1, -1, -1, -1}}},
555     {GL_RG, 2, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, -1, -1, -1, -1, -1, -1, -1}}},
556     {GL_RGB, 3, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, 2, -1, -1, -1, -1, -1, -1}}},
557     {GL_RGBA, 4, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, 2, 3, -1, -1, -1, -1, -1}}},
558     {GL_BGR, 3, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{2, 1, 0, -1, -1, -1, -1, -1, -1}}},
559     {GL_BGRA, 4, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{2, 1, 0, 3, -1, -1, -1, -1, -1}}},
560     {GL_RED_INTEGER, 1, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, -1, -1, -1, -1, -1, -1, -1, -1}}},
561     {GL_GREEN_INTEGER, 1, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{-1, 0, -1, -1, -1, -1, -1, -1, -1}}},
562     {GL_BLUE_INTEGER, 1, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{-1, -1, 0, -1, -1, -1, -1, -1, -1}}},
563     {GL_RG_INTEGER, 2, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, -1, -1, -1, -1, -1, -1, -1}}},
564     {GL_RGB_INTEGER, 3, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, 2, -1, -1, -1, -1, -1, -1}}},
565     {GL_RGBA_INTEGER, 4, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, 2, 3, -1, -1, -1, -1, -1}}},
566     {GL_BGR_INTEGER, 3, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{2, 1, 0, -1, -1, -1, -1, -1, -1}}},
567     {GL_BGRA_INTEGER, 4, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{2, 1, 0, 3, -1, -1, -1, -1, -1}}},
568 };
569 
570 static const PixelFormat esFormats[] = {
571     {GL_DEPTH_COMPONENT, 1, FORMAT_DEPTH, GL_DEPTH_ATTACHMENT, {{-1, -1, -1, -1, -1, -1, 0, -1, -1}}},
572     {GL_DEPTH_STENCIL, 2, FORMAT_DEPTH_STENCIL, GL_DEPTH_STENCIL_ATTACHMENT, {{-1, -1, -1, -1, -1, -1, 0, 1, -1}}},
573     {GL_RED, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, -1, -1, -1, -1, -1, -1, -1, -1}}},
574     {GL_RG, 2, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, -1, -1, -1, -1, -1, -1, -1}}},
575     {GL_RGB, 3, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, 2, -1, -1, -1, -1, -1, -1}}},
576     {GL_RGBA, 4, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{0, 1, 2, 3, -1, -1, -1, -1, -1}}},
577     {GL_LUMINANCE, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{-1, -1, -1, -1, -1, 0, -1, -1, -1}}},
578     {GL_ALPHA, 1, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{-1, -1, -1, 0, -1, -1, -1, -1, -1}}},
579     {GL_LUMINANCE_ALPHA, 2, FORMAT_COLOR, GL_COLOR_ATTACHMENT0, {{-1, -1, -1, 1, -1, 0, -1, -1, -1}}},
580     {GL_RED_INTEGER, 1, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, -1, -1, -1, -1, -1, -1, -1, -1}}},
581     {GL_RG_INTEGER, 2, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, -1, -1, -1, -1, -1, -1, -1}}},
582     {GL_RGB_INTEGER, 3, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, 2, -1, -1, -1, -1, -1, -1}}},
583     {GL_RGBA_INTEGER, 4, FORMAT_COLOR_INTEGER, GL_COLOR_ATTACHMENT0, {{0, 1, 2, 3, -1, -1, -1, -1, -1}}},
584 };
585 
586 static const PixelType coreTypes[] = {
587     {GL_UNSIGNED_BYTE, sizeof(GLubyte), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
588     {GL_BYTE, sizeof(GLbyte), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
589     {GL_UNSIGNED_SHORT, sizeof(GLushort), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
590     {GL_SHORT, sizeof(GLshort), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
591     {GL_UNSIGNED_INT, sizeof(GLuint), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
592     {GL_INT, sizeof(GLint), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
593     {GL_HALF_FLOAT, sizeof(GLhalf), STORAGE_FLOAT, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
594     {GL_FLOAT, sizeof(GLfloat), STORAGE_FLOAT, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
595     {GL_UNSIGNED_SHORT_5_6_5, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{5, 6, 5, 0, 0, 0, 0, 0, 0}}, false},
596     {GL_UNSIGNED_SHORT_4_4_4_4, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{4, 4, 4, 4, 0, 0, 0, 0, 0}}, false},
597     {GL_UNSIGNED_SHORT_5_5_5_1, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{5, 5, 5, 1, 0, 0, 0, 0, 0}}, false},
598     {GL_UNSIGNED_INT_2_10_10_10_REV,
599      sizeof(GLuint),
600      STORAGE_UNSIGNED,
601      true,
602      true,
603      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
604      false},
605     {GL_UNSIGNED_INT_24_8, sizeof(GLuint), STORAGE_UNSIGNED, true, false, {{0, 0, 0, 0, 0, 0, 24, 8, 0}}, false},
606     {GL_UNSIGNED_INT_10F_11F_11F_REV, sizeof(GLuint), STORAGE_FLOAT, true, true, {{6, 7, 7, 0, 0, 0, 0, 0, 0}}, false},
607     {GL_UNSIGNED_INT_5_9_9_9_REV, sizeof(GLuint), STORAGE_FLOAT, true, true, {{9, 9, 9, 5, 0, 0, 0, 0, 0}}, false},
608     {GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
609      sizeof(GLfloat) + sizeof(GLuint),
610      STORAGE_FLOAT,
611      true,
612      true,
613      {{0, 0, 0, 0, 0, 0, 32, 8, 24}},
614      false},
615     {GL_UNSIGNED_BYTE_3_3_2, sizeof(GLubyte), STORAGE_UNSIGNED, true, false, {{3, 3, 2, 0, 0, 0, 0, 0, 0}}, false},
616     {GL_UNSIGNED_BYTE_2_3_3_REV, sizeof(GLubyte), STORAGE_UNSIGNED, true, true, {{3, 3, 2, 0, 0, 0, 0, 0, 0}}, false},
617     {GL_UNSIGNED_SHORT_5_6_5_REV, sizeof(GLushort), STORAGE_UNSIGNED, true, true, {{5, 6, 5, 0, 0, 0, 0, 0, 0}}, false},
618     {GL_UNSIGNED_SHORT_4_4_4_4_REV,
619      sizeof(GLushort),
620      STORAGE_UNSIGNED,
621      true,
622      true,
623      {{4, 4, 4, 4, 0, 0, 0, 0, 0}},
624      false},
625     {GL_UNSIGNED_SHORT_1_5_5_5_REV,
626      sizeof(GLushort),
627      STORAGE_UNSIGNED,
628      true,
629      true,
630      {{5, 5, 5, 1, 0, 0, 0, 0, 0}},
631      false},
632     {GL_UNSIGNED_INT_8_8_8_8, sizeof(GLuint), STORAGE_UNSIGNED, true, false, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, false},
633     {GL_UNSIGNED_INT_8_8_8_8_REV, sizeof(GLuint), STORAGE_UNSIGNED, true, true, {{8, 8, 8, 8, 0, 0, 0, 0, 0}}, false},
634     {GL_UNSIGNED_INT_10_10_10_2, sizeof(GLuint), STORAGE_UNSIGNED, true, true, {{10, 10, 10, 2, 0, 0, 0, 0, 0}}, false},
635 };
636 
637 static const PixelType esTypes[] = {
638     {GL_UNSIGNED_BYTE, sizeof(GLubyte), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
639     {GL_BYTE, sizeof(GLbyte), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
640     {GL_UNSIGNED_SHORT, sizeof(GLushort), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
641     {GL_SHORT, sizeof(GLshort), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, true},
642     {GL_UNSIGNED_INT, sizeof(GLuint), STORAGE_UNSIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
643     {GL_INT, sizeof(GLint), STORAGE_SIGNED, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
644     {GL_HALF_FLOAT, sizeof(GLhalf), STORAGE_FLOAT, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
645     {GL_FLOAT, sizeof(GLfloat), STORAGE_FLOAT, false, false, {{0, 0, 0, 0, 0, 0, 0, 0, 0}}, false},
646     {GL_UNSIGNED_SHORT_5_6_5, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{5, 6, 5, 0, 0, 0, 0, 0, 0}}, false},
647     {GL_UNSIGNED_SHORT_4_4_4_4, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{4, 4, 4, 4, 0, 0, 0, 0, 0}}, false},
648     {GL_UNSIGNED_SHORT_5_5_5_1, sizeof(GLushort), STORAGE_UNSIGNED, true, false, {{5, 5, 5, 1, 0, 0, 0, 0, 0}}, false},
649     {GL_UNSIGNED_INT_2_10_10_10_REV,
650      sizeof(GLuint),
651      STORAGE_UNSIGNED,
652      true,
653      true,
654      {{10, 10, 10, 2, 0, 0, 0, 0, 0}},
655      false},
656     {GL_UNSIGNED_INT_24_8, sizeof(GLuint), STORAGE_UNSIGNED, true, false, {{0, 0, 0, 0, 0, 0, 24, 8, 0}}, false},
657     {GL_UNSIGNED_INT_10F_11F_11F_REV, sizeof(GLuint), STORAGE_FLOAT, true, true, {{6, 7, 7, 0, 0, 0, 0, 0, 0}}, false},
658     {GL_UNSIGNED_INT_5_9_9_9_REV, sizeof(GLuint), STORAGE_FLOAT, true, true, {{9, 9, 9, 5, 0, 0, 0, 0, 0}}, false},
659     {GL_FLOAT_32_UNSIGNED_INT_24_8_REV,
660      sizeof(GLfloat) + sizeof(GLuint),
661      STORAGE_FLOAT,
662      true,
663      true,
664      {{0, 0, 0, 0, 0, 0, 32, 8, 24}},
665      false},
666 };
667 
668 static const EnumFormats esValidFormats[] = {
669     {GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, true},
670     {GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_BYTE, 4, true},
671     {GL_RGBA4, GL_RGBA, GL_UNSIGNED_BYTE, 4, true},
672     {GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, 4, true},
673     {GL_RGBA8_SNORM, GL_RGBA, GL_BYTE, 4, false},
674     {GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2, true},
675     {GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2, true},
676     {GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
677     {GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
678     {GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, 8, false},
679     {GL_RGBA32F, GL_RGBA, GL_FLOAT, 16, false},
680     {GL_RGBA16F, GL_RGBA, GL_FLOAT, 16, false},
681     {GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, 4, true},
682     {GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, 4, true},
683     {GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, 8, true},
684     {GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT, 8, true},
685     {GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, 16, true},
686     {GL_RGBA32I, GL_RGBA_INTEGER, GL_INT, 16, true},
687     {GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
688     {GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3, true},
689     {GL_RGB565, GL_RGB, GL_UNSIGNED_BYTE, 3, true},
690     {GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, 3, false},
691     {GL_RGB8_SNORM, GL_RGB, GL_BYTE, 3, false},
692     {GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2, true},
693     {GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, 4, false},
694     {GL_R11F_G11F_B10F, GL_RGB, GL_HALF_FLOAT, 6, false},
695     {GL_R11F_G11F_B10F, GL_RGB, GL_FLOAT, 12, false},
696     {GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, 4, false},
697     {GL_RGB9_E5, GL_RGB, GL_HALF_FLOAT, 6, false},
698     {GL_RGB9_E5, GL_RGB, GL_FLOAT, 12, false},
699     {GL_RGB16F, GL_RGB, GL_HALF_FLOAT, 6, false},
700     {GL_RGB32F, GL_RGB, GL_FLOAT, 12, false},
701     {GL_RGB16F, GL_RGB, GL_FLOAT, 12, false},
702     {GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, 3, false},
703     {GL_RGB8I, GL_RGB_INTEGER, GL_BYTE, 3, false},
704     {GL_RGB16UI, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, 6, false},
705     {GL_RGB16I, GL_RGB_INTEGER, GL_SHORT, 6, false},
706     {GL_RGB32UI, GL_RGB_INTEGER, GL_UNSIGNED_INT, 12, false},
707     {GL_RGB32I, GL_RGB_INTEGER, GL_INT, 12, false},
708     {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, 2, true},
709     {GL_RG8_SNORM, GL_RG, GL_BYTE, 2, false},
710     {GL_RG16F, GL_RG, GL_HALF_FLOAT, 4, false},
711     {GL_RG32F, GL_RG, GL_FLOAT, 8, false},
712     {GL_RG16F, GL_RG, GL_FLOAT, 8, false},
713     {GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE, 2, true},
714     {GL_RG8I, GL_RG_INTEGER, GL_BYTE, 2, true},
715     {GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT, 4, true},
716     {GL_RG16I, GL_RG_INTEGER, GL_SHORT, 4, true},
717     {GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, 8, true},
718     {GL_RG32I, GL_RG_INTEGER, GL_INT, 8, true},
719     {GL_R8, GL_RED, GL_UNSIGNED_BYTE, 1, true},
720     {GL_R8_SNORM, GL_RED, GL_BYTE, 1, false},
721     {GL_R16F, GL_RED, GL_HALF_FLOAT, 2, false},
722     {GL_R32F, GL_RED, GL_FLOAT, 4, false},
723     {GL_R16F, GL_RED, GL_FLOAT, 4, false},
724     {GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, 1, true},
725     {GL_R8I, GL_RED_INTEGER, GL_BYTE, 1, true},
726     {GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT, 2, true},
727     {GL_R16I, GL_RED_INTEGER, GL_SHORT, 2, true},
728     {GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, 4, true},
729     {GL_R32I, GL_RED_INTEGER, GL_INT, 4, true},
730     {GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4, true},
731     {GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 4, true},
732     {GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, 2, true},
733     {GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, 4, true},
734     {GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 4, true},
735     {GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 8, true},
736     {GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4, true},
737     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2, true},
738     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2, true},
739     {GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 3, true},
740     {GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2, true},
741     {GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 2, false},
742     {GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, false},
743     {GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, 1, false},
744 };
745 
746 static const EnumFormats coreValidFormats[] = {
747     {GL_RGB, GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 3, true},
748     {GL_RGB_INTEGER, GL_RGB_INTEGER, GL_UNSIGNED_BYTE_3_3_2, 3, true},
749     {GL_RGB, GL_RGB, GL_UNSIGNED_BYTE_2_3_3_REV, 3, true},
750     {GL_RGB_INTEGER, GL_RGB_INTEGER, GL_UNSIGNED_BYTE_2_3_3_REV, 3, true},
751     {GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 3, true},
752     {GL_RGB_INTEGER, GL_RGB_INTEGER, GL_UNSIGNED_SHORT_5_6_5, 3, true},
753     {GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, 3, true},
754     {GL_RGB_INTEGER, GL_RGB_INTEGER, GL_UNSIGNED_SHORT_5_6_5_REV, 3, true},
755     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 4, true},
756     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 4, true},
757     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT_4_4_4_4, 4, true},
758     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT_4_4_4_4_REV, 4, true},
759     {GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 4, true},
760     {GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4, 4, true},
761     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_SHORT_4_4_4_4_REV, 4, true},
762     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_SHORT_4_4_4_4, 4, true},
763     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 4, true},
764     {GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_5_5_5_1, 4, true},
765     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT_5_5_5_1, 4, true},
766     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_SHORT_5_5_5_1, 4, true},
767     {GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 4, true},
768     {GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 4, true},
769     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT_1_5_5_5_REV, 4, true},
770     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_SHORT_1_5_5_5_REV, 4, true},
771     {GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4, true},
772     {GL_BGRA, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4, true},
773     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8, 4, true},
774     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_INT_8_8_8_8, 4, true},
775     {GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, true},
776     {GL_BGRA, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, true},
777     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT_8_8_8_8_REV, 4, true},
778     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_INT_8_8_8_8_REV, 4, true},
779     {GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_10_10_10_2, 4, true},
780     {GL_BGRA, GL_BGRA, GL_UNSIGNED_INT_10_10_10_2, 4, true},
781     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT_10_10_10_2, 4, true},
782     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_INT_10_10_10_2, 4, true},
783     {GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
784     {GL_BGRA, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
785     {GL_RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
786     {GL_BGRA_INTEGER, GL_BGRA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, 4, true},
787     {GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 2, true},
788     {GL_RGB, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, 3, true},
789     {GL_RGB, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, 4, true},
790     {GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, 2, true},
791 };
792 
793 static const EnumFormats validformats_EXT_texture_type_2_10_10_10_REV[] = {
794     {GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV_EXT, 4, false},
795     {GL_RGB, GL_RGB, GL_UNSIGNED_INT_2_10_10_10_REV_EXT, 3, false}};
796 
797 // Valid combinations given by GL_EXT_texture_type_2_10_10_10_REV and
798 // GL_OES_required_internalformat extensions
799 static const EnumFormats validformats_OES_required_internalformat[] = {
800     {GL_RGB8_OES, GL_RGB, GL_UNSIGNED_INT_2_10_10_10_REV_EXT, 3, true},
801     {GL_RGB565, GL_RGB, GL_UNSIGNED_INT_2_10_10_10_REV_EXT, 4, true}};
802 
803 // Companion type for GL_FLOAT_32_UNSIGNED_INT_24_8_REV. Stencil part was
804 // not split into 24/8 to avoid any packing related issues from compiler.
805 struct F_32_UINT_24_8_REV
806 {
807     GLfloat d;
808     GLuint s;
809 };
810 
811 // custom pixel data type. holds both float and integer pixel data. memory consuming, but
812 // it is not that relavant in this case. makes comparing more reliable and flexible
813 struct FloatPixel
814 {
815     int i_r;
816     int i_g;
817     int i_b;
818     int i_a;
819     int i_d;
820     int i_s;
821 
822     unsigned int ui_r;
823     unsigned int ui_g;
824     unsigned int ui_b;
825     unsigned int ui_a;
826     unsigned int ui_d;
827     unsigned int ui_s;
828 
829     float r;
830     float g;
831     float b;
832     float a;
833     float d;
834     float s;
835 };
836 
837 static const int NUM_FLOAT_PIXEL_COUNT = sizeof(FloatPixel) / sizeof(float);
838 
839 typedef int rawIntPixel[4];
840 typedef unsigned int rawUintPixel[4];
841 typedef float rawFloatPixel[4];
842 
843 struct PackedPixelsBufferProperties
844 {
845     int elementsInGroup;      // number of elements in a group
846     int rowLength;            // number of groups in the row
847     int alignment;            // alignment (in bytes)
848     int elementSize;          // size of an element (in bytes)
849     int elementsInRow;        // row size (in elements)
850     int elementsInRowNoAlign; // row size (in elements) without alignment
851     int rowCount;             // number of rows in 2D image
852     int imagesCount;          // number of 2D images in 3D image
853     int skipPixels;           // (UN)PACK_SKIP_PIXELS
854     int skipRows;             // (UN)PACK_SKIP_ROWS
855     int skipImages;           // (UN)PACK_SKIP_IMAGES
856     int swapBytes;
857     int lsbFirst;
858 };
859 
getTypeStr(GLenum type)860 std::string getTypeStr(GLenum type)
861 {
862     // this function extends glu::getTypeStr by types used in this tests
863 
864     typedef std::map<GLenum, std::string> TypeMap;
865     static TypeMap typeMap;
866     if (typeMap.empty())
867     {
868         typeMap[GL_UNSIGNED_BYTE_3_3_2]        = "GL_UNSIGNED_BYTE_3_3_2";
869         typeMap[GL_UNSIGNED_BYTE_2_3_3_REV]    = "GL_UNSIGNED_BYTE_2_3_3_REV";
870         typeMap[GL_UNSIGNED_SHORT_5_6_5_REV]   = "GL_UNSIGNED_SHORT_5_6_5_REV";
871         typeMap[GL_UNSIGNED_SHORT_4_4_4_4_REV] = "GL_UNSIGNED_SHORT_4_4_4_4_REV";
872         typeMap[GL_UNSIGNED_SHORT_1_5_5_5_REV] = "GL_UNSIGNED_SHORT_1_5_5_5_REV";
873         typeMap[GL_UNSIGNED_INT_8_8_8_8]       = "GL_UNSIGNED_INT_8_8_8_8";
874         typeMap[GL_UNSIGNED_INT_8_8_8_8_REV]   = "GL_UNSIGNED_INT_8_8_8_8_REV";
875         typeMap[GL_UNSIGNED_INT_10_10_10_2]    = "GL_UNSIGNED_INT_10_10_10_2";
876     }
877 
878     TypeMap::iterator it = typeMap.find(type);
879     if (it == typeMap.end())
880     {
881         // if type is not in map use glu function
882         return glu::getTypeStr(type).toString();
883     }
884     return it->second;
885 }
886 
getFormatStr(GLenum format)887 std::string getFormatStr(GLenum format)
888 {
889     // this function extends glu::getTextureFormatStr by types used in this tests
890 
891     typedef std::map<GLenum, std::string> FormatMap;
892     static FormatMap formatMap;
893     if (formatMap.empty())
894     {
895         formatMap[GL_GREEN]                       = "GL_GREEN";
896         formatMap[GL_BLUE]                        = "GL_BLUE";
897         formatMap[GL_GREEN_INTEGER]               = "GL_GREEN_INTEGER";
898         formatMap[GL_BLUE_INTEGER]                = "GL_BLUE_INTEGER";
899         formatMap[GL_BGR]                         = "GL_BGR";
900         formatMap[GL_BGR_INTEGER]                 = "GL_BGR_INTEGER";
901         formatMap[GL_BGRA_INTEGER]                = "GL_BGRA_INTEGER";
902         formatMap[GL_R3_G3_B2]                    = "GL_R3_G3_B2";
903         formatMap[GL_RGB4]                        = "GL_RGB4";
904         formatMap[GL_RGB5]                        = "GL_RGB5";
905         formatMap[GL_RGB12]                       = "GL_RGB12";
906         formatMap[GL_RGBA2]                       = "GL_RGBA2";
907         formatMap[GL_RGBA12]                      = "GL_RGBA12";
908         formatMap[GL_COMPRESSED_RED]              = "GL_COMPRESSED_RED";
909         formatMap[GL_COMPRESSED_RG]               = "GL_COMPRESSED_RG";
910         formatMap[GL_COMPRESSED_RGB]              = "GL_COMPRESSED_RGB";
911         formatMap[GL_COMPRESSED_RGBA]             = "GL_COMPRESSED_RGBA";
912         formatMap[GL_COMPRESSED_SRGB]             = "GL_COMPRESSED_SRGB";
913         formatMap[GL_COMPRESSED_SRGB_ALPHA]       = "GL_COMPRESSED_SRGB_ALPHA";
914         formatMap[GL_COMPRESSED_RED_RGTC1]        = "GL_COMPRESSED_RED_RGTC1";
915         formatMap[GL_COMPRESSED_SIGNED_RED_RGTC1] = "GL_COMPRESSED_SIGNED_RED_RGTC1";
916         formatMap[GL_COMPRESSED_RG_RGTC2]         = "GL_COMPRESSED_RG_RGTC2";
917         formatMap[GL_COMPRESSED_SIGNED_RG_RGTC2]  = "GL_COMPRESSED_SIGNED_RG_RGTC2";
918         formatMap[GL_STENCIL_INDEX]               = "GL_STENCIL_INDEX";
919     }
920 
921     FormatMap::iterator it = formatMap.find(format);
922     if (it == formatMap.end())
923     {
924         // if format is not in map use glu function
925         return glu::getTextureFormatStr(format).toString();
926     }
927     return it->second;
928 }
929 
getModeStr(GLenum type)930 std::string getModeStr(GLenum type)
931 {
932     typedef std::map<GLenum, std::string> ModeMap;
933     static ModeMap modeMap;
934     if (modeMap.empty())
935     {
936         modeMap[GL_UNPACK_ROW_LENGTH]   = "GL_UNPACK_ROW_LENGTH";
937         modeMap[GL_UNPACK_SKIP_ROWS]    = "GL_UNPACK_SKIP_ROWS";
938         modeMap[GL_UNPACK_SKIP_PIXELS]  = "GL_UNPACK_SKIP_PIXELS";
939         modeMap[GL_UNPACK_ALIGNMENT]    = "GL_UNPACK_ALIGNMENT";
940         modeMap[GL_UNPACK_IMAGE_HEIGHT] = "GL_UNPACK_IMAGE_HEIGHT";
941         modeMap[GL_UNPACK_SKIP_IMAGES]  = "GL_UNPACK_SKIP_IMAGES";
942         modeMap[GL_PACK_ROW_LENGTH]     = "GL_PACK_ROW_LENGTH";
943         modeMap[GL_PACK_SKIP_ROWS]      = "GL_PACK_SKIP_ROWS";
944         modeMap[GL_PACK_SKIP_PIXELS]    = "GL_PACK_SKIP_PIXELS";
945         modeMap[GL_PACK_ALIGNMENT]      = "GL_PACK_ALIGNMENT";
946         modeMap[GL_UNPACK_SWAP_BYTES]   = "GL_UNPACK_SWAP_BYTES";
947         modeMap[GL_UNPACK_LSB_FIRST]    = "GL_UNPACK_LSB_FIRST";
948         modeMap[GL_PACK_SWAP_BYTES]     = "GL_PACK_SWAP_BYTES";
949         modeMap[GL_PACK_LSB_FIRST]      = "GL_PACK_LSB_FIRST";
950         modeMap[GL_PACK_IMAGE_HEIGHT]   = "GL_PACK_IMAGE_HEIGHT";
951         modeMap[GL_PACK_SKIP_IMAGES]    = "GL_PACK_SKIP_IMAGES";
952     }
953 
954     ModeMap::iterator it = modeMap.find(type);
955     if (it == modeMap.end())
956         TCU_FAIL("Unknown mode name");
957     return it->second;
958 }
959 
960 class RectangleTest : public deqp::TestCase
961 {
962 public:
963     RectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat, PixelFormat inputFormat);
964     virtual ~RectangleTest();
965 
966     void resetInitialStorageModes();
967     void applyInitialStorageModes();
968     void testAllFormatsAndTypes();
969 
970     virtual tcu::TestNode::IterateResult iterate(void);
971 
972 protected:
973     void createGradient();
974     void swapBytes(int typeSize, std::vector<GLbyte> &dataBuffer);
975 
976     template <typename Type>
977     void makeGradient(Type (*unpack)(float));
978 
979     template <typename Type>
980     static Type unpackSizedComponents(float value, int s1, int s2, int s3, int s4);
981 
982     template <typename Type>
983     static Type unpackSizedComponentsRev(float value, int s1, int s2, int s3, int s4);
984 
985     static GLubyte unpack_UNSIGNED_BYTE(float value);
986     static GLbyte unpack_BYTE(float value);
987     static GLushort unpack_UNSIGNED_SHORT(float value);
988     static GLshort unpack_SHORT(float value);
989     static GLuint unpack_UNSIGNED_INT(float value);
990     static GLint unpack_INT(float value);
991     static GLhalf unpack_HALF_FLOAT(float value);
992     static GLfloat unpack_FLOAT(float value);
993     static GLubyte unpack_UNSIGNED_BYTE_3_3_2(float value);
994     static GLubyte unpack_UNSIGNED_BYTE_2_3_3_REV(float value);
995     static GLushort unpack_UNSIGNED_SHORT_5_6_5_REV(float value);
996     static GLushort unpack_UNSIGNED_SHORT_4_4_4_4_REV(float value);
997     static GLushort unpack_UNSIGNED_SHORT_1_5_5_5_REV(float value);
998     static GLuint unpack_UNSIGNED_INT_8_8_8_8(float value);
999     static GLuint unpack_UNSIGNED_INT_8_8_8_8_REV(float value);
1000     static GLuint unpack_UNSIGNED_INT_10_10_10_2(float value);
1001     static GLushort unpack_UNSIGNED_SHORT_5_6_5(float value);
1002     static GLushort unpack_UNSIGNED_SHORT_4_4_4_4(float value);
1003     static GLushort unpack_UNSIGNED_SHORT_5_5_5_1(float value);
1004     static GLuint unpack_UNSIGNED_INT_2_10_10_10_REV(float value);
1005     static GLuint unpack_UNSIGNED_INT_24_8(float value);
1006     static GLuint unpack_UNSIGNED_INT_5_9_9_9_REV(float value);
1007     static GLuint unpack_UNSIGNED_INT_10F_11F_11F_REV(float value);
1008     static F_32_UINT_24_8_REV unpack_FLOAT_32_UNSIGNED_INT_24_8_REV(float value);
1009 
1010     bool isFormatValid(const PixelFormat &format, const PixelType &type, const struct InternalFormat &internalformat,
1011                        bool checkInput, bool checkOutput, int operation) const;
1012     bool isUnsizedFormat(GLenum format) const;
1013     bool isSRGBFormat(const InternalFormat &internalFormat) const;
1014     bool isSNORMFormat(const InternalFormat &internalFormat) const;
1015     bool isCopyValid(const InternalFormat &copyInternalFormat, const InternalFormat &internalFormat) const;
1016     bool isFBOImageAttachValid(const InternalFormat &internalformat, GLenum format, GLenum type) const;
1017 
1018     const PixelFormat &getPixelFormat(GLenum format) const;
1019     const PixelType &getPixelType(GLenum type) const;
1020     const EnumFormats *getCanonicalFormat(const InternalFormat &internalformat, GLenum format, GLenum type) const;
1021     InternalFormatSamplerType getSampler(const PixelType &type, const PixelFormat &format) const;
1022 
1023     GLenum readOutputData(const PixelFormat &outputFormat, const PixelType &outputType, int operation);
1024 
1025     bool doCopy();
1026 
1027     bool doCopyInner();
1028 
1029     bool compare(GLvoid *gradient, GLvoid *data, const PixelFormat &outputFormat, const PixelType &outputType,
1030                  bool isCopy) const;
1031 
1032     void getFloatBuffer(GLvoid *gradient, int samplerIsIntUintFloat, const PixelFormat &format, const PixelType &type,
1033                         int elementCount, std::vector<FloatPixel> &result) const;
1034 
1035     void getBits(const PixelType &type, const PixelFormat &format, std::vector<int> &resultTable) const;
1036 
1037     template <typename Type>
1038     void makeBuffer(const GLvoid *gradient, const PixelFormat &format, int samplerIsIntUintFloat, int elementCount,
1039                     int componentCount, float (*pack)(Type), std::vector<FloatPixel> &result) const;
1040 
1041     template <typename Type>
1042     void makeBufferPackedInt(const GLvoid *gradient, const PixelFormat &format, int elementCount,
1043                              void (*pack)(rawIntPixel *, Type), std::vector<FloatPixel> &result) const;
1044 
1045     template <typename Type>
1046     void makeBufferPackedUint(const GLvoid *gradient, const PixelFormat &format, int elementCount,
1047                               void (*pack)(rawUintPixel *, Type), std::vector<FloatPixel> &result) const;
1048 
1049     template <typename Type>
1050     void makeBufferPackedFloat(const GLvoid *gradient, const PixelFormat &format, int elementCount,
1051                                void (*pack)(rawFloatPixel *, Type), std::vector<FloatPixel> &result) const;
1052 
1053     FloatPixel orderComponentsInt(rawIntPixel values, const PixelFormat &format) const;
1054     FloatPixel orderComponentsUint(rawUintPixel values, const PixelFormat &format) const;
1055     FloatPixel orderComponentsFloat(rawFloatPixel values, const PixelFormat &format) const;
1056 
1057     unsigned int getRealBitPrecision(int bits, bool isFloat) const;
1058 
1059     bool stripBuffer(const PackedPixelsBufferProperties &props, const GLubyte *orginalBuffer,
1060                      std::vector<GLubyte> &newBuffer, bool validate) const;
1061 
1062     int clampSignedValue(int bits, int value) const;
1063     unsigned int clampUnsignedValue(int bits, unsigned int value) const;
1064 
1065     static float pack_UNSIGNED_BYTE(GLubyte value);
1066     static float pack_BYTE(GLbyte value);
1067     static float pack_UNSIGNED_SHORT(GLushort value);
1068     static float pack_SHORT(GLshort value);
1069     static float pack_UNSIGNED_INT(GLuint value);
1070     static float pack_INT(GLint value);
1071     static float pack_HALF_FLOAT(GLhalf value);
1072     static float pack_FLOAT(GLfloat value);
1073     static void pack_UNSIGNED_BYTE_3_3_2(rawFloatPixel *values, GLubyte value);
1074     static void pack_UNSIGNED_BYTE_3_3_2_UINT(rawUintPixel *values, GLubyte value);
1075     static void pack_UNSIGNED_BYTE_3_3_2_INT(rawIntPixel *values, GLubyte value);
1076     static void pack_UNSIGNED_BYTE_2_3_3_REV(rawFloatPixel *values, GLubyte value);
1077     static void pack_UNSIGNED_BYTE_2_3_3_REV_UINT(rawUintPixel *values, GLubyte value);
1078     static void pack_UNSIGNED_BYTE_2_3_3_REV_INT(rawIntPixel *values, GLubyte value);
1079     static void pack_UNSIGNED_SHORT_5_6_5(rawFloatPixel *values, GLushort value);
1080     static void pack_UNSIGNED_SHORT_5_6_5_UINT(rawUintPixel *values, GLushort value);
1081     static void pack_UNSIGNED_SHORT_5_6_5_INT(rawIntPixel *values, GLushort value);
1082     static void pack_UNSIGNED_SHORT_5_6_5_REV(rawFloatPixel *values, GLushort value);
1083     static void pack_UNSIGNED_SHORT_5_6_5_REV_UINT(rawUintPixel *values, GLushort value);
1084     static void pack_UNSIGNED_SHORT_5_6_5_REV_INT(rawIntPixel *values, GLushort value);
1085     static void pack_UNSIGNED_SHORT_4_4_4_4(rawFloatPixel *values, GLushort value);
1086     static void pack_UNSIGNED_SHORT_4_4_4_4_UINT(rawUintPixel *values, GLushort value);
1087     static void pack_UNSIGNED_SHORT_4_4_4_4_INT(rawIntPixel *values, GLushort value);
1088     static void pack_UNSIGNED_SHORT_4_4_4_4_REV(rawFloatPixel *values, GLushort value);
1089     static void pack_UNSIGNED_SHORT_4_4_4_4_REV_UINT(rawUintPixel *values, GLushort value);
1090     static void pack_UNSIGNED_SHORT_4_4_4_4_REV_INT(rawIntPixel *values, GLushort value);
1091     static void pack_UNSIGNED_SHORT_5_5_5_1(rawFloatPixel *values, GLushort value);
1092     static void pack_UNSIGNED_SHORT_5_5_5_1_UINT(rawUintPixel *values, GLushort value);
1093     static void pack_UNSIGNED_SHORT_5_5_5_1_INT(rawIntPixel *values, GLushort value);
1094     static void pack_UNSIGNED_SHORT_1_5_5_5_REV(rawFloatPixel *values, GLushort value);
1095     static void pack_UNSIGNED_SHORT_1_5_5_5_REV_UINT(rawUintPixel *values, GLushort value);
1096     static void pack_UNSIGNED_SHORT_1_5_5_5_REV_INT(rawIntPixel *values, GLushort value);
1097     static void pack_UNSIGNED_INT_8_8_8_8(rawFloatPixel *values, GLuint value);
1098     static void pack_UNSIGNED_INT_8_8_8_8_UINT(rawUintPixel *values, GLuint value);
1099     static void pack_UNSIGNED_INT_8_8_8_8_INT(rawIntPixel *values, GLuint value);
1100     static void pack_UNSIGNED_INT_8_8_8_8_REV(rawFloatPixel *values, GLuint value);
1101     static void pack_UNSIGNED_INT_8_8_8_8_REV_UINT(rawUintPixel *values, GLuint value);
1102     static void pack_UNSIGNED_INT_8_8_8_8_REV_INT(rawIntPixel *values, GLuint value);
1103     static void pack_UNSIGNED_INT_10_10_10_2(rawFloatPixel *values, GLuint value);
1104     static void pack_UNSIGNED_INT_10_10_10_2_UINT(rawUintPixel *values, GLuint value);
1105     static void pack_UNSIGNED_INT_10_10_10_2_INT(rawIntPixel *values, GLuint value);
1106     static void pack_UNSIGNED_INT_2_10_10_10_REV(rawFloatPixel *values, GLuint value);
1107     static void pack_UNSIGNED_INT_2_10_10_10_REV_UINT(rawUintPixel *values, GLuint value);
1108     static void pack_UNSIGNED_INT_2_10_10_10_REV_INT(rawIntPixel *values, GLuint value);
1109     static void pack_UNSIGNED_INT_24_8(rawFloatPixel *values, GLuint value);
1110     static void pack_UNSIGNED_INT_10F_11F_11F_REV(rawFloatPixel *values, GLuint value);
1111     static void pack_UNSIGNED_INT_5_9_9_9_REV(rawFloatPixel *values, GLuint value);
1112     static void pack_FLOAT_32_UNSIGNED_INT_24_8_REV(rawFloatPixel *values, F_32_UINT_24_8_REV value);
1113 
1114     bool getTexImage();
1115     bool getTexImageInner(const PixelFormat &outputFormat, const PixelType &outputType);
1116 
1117     bool doRead(GLuint texture);
1118     bool readPixels(bool isCopy);
1119     bool readPixelsInner(const PixelFormat &outputFormat, const PixelType &outputType, bool isCopy);
1120 
1121 protected:
1122     const InternalFormat m_internalFormat;
1123 
1124     bool m_usePBO;
1125     GLenum m_textureTarget;
1126     PackedPixelsBufferProperties m_initialPackProperties;
1127     PackedPixelsBufferProperties m_initialUnpackProperties;
1128 
1129     std::vector<GLbyte> m_gradient;
1130     const GLubyte m_defaultFillValue;
1131 
1132 public:
1133     // debuf counters
1134     static int m_countReadPixels;
1135     static int m_countReadPixelsOK;
1136     static int m_countGetTexImage;
1137     static int m_countGetTexImageOK;
1138     static int m_countCompare;
1139     static int m_countCompareOK;
1140 
1141 private:
1142     // those attribute change multiple times during test execution
1143     PixelFormat m_inputFormat;
1144     PixelType m_inputType;
1145     InternalFormat m_copyInternalFormat;
1146     PackedPixelsBufferProperties m_packProperties;
1147     PackedPixelsBufferProperties m_unpackProperties;
1148     std::vector<GLbyte> m_outputBuffer;
1149 };
1150 
1151 int RectangleTest::m_countReadPixels    = 0;
1152 int RectangleTest::m_countReadPixelsOK  = 0;
1153 int RectangleTest::m_countGetTexImage   = 0;
1154 int RectangleTest::m_countGetTexImageOK = 0;
1155 int RectangleTest::m_countCompare       = 0;
1156 int RectangleTest::m_countCompareOK     = 0;
1157 
RectangleTest(deqp::Context & context,std::string & name,InternalFormat internalFormat,PixelFormat inputFormat)1158 RectangleTest::RectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat,
1159                              PixelFormat inputFormat)
1160     : deqp::TestCase(context, name.c_str(), "")
1161     , m_internalFormat(internalFormat)
1162     , m_usePBO(false)
1163     , m_textureTarget(GL_TEXTURE_2D)
1164     , m_defaultFillValue(0xaa)
1165     , m_inputFormat(inputFormat)
1166 {
1167     TestCase::m_name.append("_format_" + getFormatStr(inputFormat.format).substr(3));
1168     std::transform(TestCase::m_name.begin(), TestCase::m_name.end(), TestCase::m_name.begin(), tolower);
1169 }
1170 
~RectangleTest()1171 RectangleTest::~RectangleTest()
1172 {
1173 }
1174 
resetInitialStorageModes()1175 void RectangleTest::resetInitialStorageModes()
1176 {
1177     m_initialPackProperties.skipPixels = 0;
1178     m_initialPackProperties.skipRows   = 0;
1179     m_initialPackProperties.rowLength  = 0;
1180     m_initialPackProperties.alignment  = 4;
1181     m_initialPackProperties.rowCount   = 0;
1182     m_initialPackProperties.skipImages = 0;
1183     m_initialPackProperties.lsbFirst   = 0;
1184     m_initialPackProperties.swapBytes  = 0;
1185 
1186     m_initialUnpackProperties.skipPixels = 0;
1187     m_initialUnpackProperties.skipRows   = 0;
1188     m_initialUnpackProperties.rowLength  = 0;
1189     m_initialUnpackProperties.alignment  = 4;
1190     m_initialUnpackProperties.rowCount   = 0;
1191     m_initialUnpackProperties.skipImages = 0;
1192     m_initialUnpackProperties.lsbFirst   = 0;
1193     m_initialUnpackProperties.swapBytes  = 0;
1194 }
1195 
applyInitialStorageModes()1196 void RectangleTest::applyInitialStorageModes()
1197 {
1198     glu::RenderContext &renderContext = m_context.getRenderContext();
1199     const Functions &gl               = renderContext.getFunctions();
1200 
1201     PackedPixelsBufferProperties &up = m_initialUnpackProperties;
1202     PackedPixelsBufferProperties &pp = m_initialPackProperties;
1203 
1204     m_unpackProperties = up;
1205     m_packProperties   = pp;
1206 
1207     gl.pixelStorei(GL_PACK_ROW_LENGTH, pp.rowLength);
1208     gl.pixelStorei(GL_PACK_SKIP_ROWS, pp.skipRows);
1209     gl.pixelStorei(GL_PACK_SKIP_PIXELS, pp.skipPixels);
1210     gl.pixelStorei(GL_PACK_ALIGNMENT, pp.alignment);
1211 
1212     gl.pixelStorei(GL_UNPACK_ROW_LENGTH, up.rowLength);
1213     gl.pixelStorei(GL_UNPACK_SKIP_ROWS, up.skipRows);
1214     gl.pixelStorei(GL_UNPACK_SKIP_PIXELS, up.skipPixels);
1215     gl.pixelStorei(GL_UNPACK_ALIGNMENT, up.alignment);
1216     gl.pixelStorei(GL_UNPACK_IMAGE_HEIGHT, up.rowCount);
1217     gl.pixelStorei(GL_UNPACK_SKIP_IMAGES, up.skipImages);
1218 
1219     if (!isContextTypeES(renderContext.getType()))
1220     {
1221         gl.pixelStorei(GL_PACK_IMAGE_HEIGHT, pp.rowCount);
1222         gl.pixelStorei(GL_PACK_SKIP_IMAGES, pp.skipImages);
1223 
1224         gl.pixelStorei(GL_PACK_SWAP_BYTES, pp.swapBytes);
1225         gl.pixelStorei(GL_PACK_LSB_FIRST, pp.lsbFirst);
1226 
1227         gl.pixelStorei(GL_UNPACK_SWAP_BYTES, up.swapBytes);
1228         gl.pixelStorei(GL_UNPACK_LSB_FIRST, up.lsbFirst);
1229     }
1230 }
1231 
swapBytes(int typeSize,std::vector<GLbyte> & dataBuffer)1232 void RectangleTest::swapBytes(int typeSize, std::vector<GLbyte> &dataBuffer)
1233 {
1234     int bufferSize = static_cast<int>(dataBuffer.size());
1235     switch (typeSize)
1236     {
1237     case 1:
1238         break; // no swapping
1239     case 2:
1240     {
1241         GLushort *data = reinterpret_cast<GLushort *>(&dataBuffer[0]);
1242         for (int i = 0; i < bufferSize / 2; i++)
1243         {
1244             GLushort v = data[i];
1245             data[i]    = ((v & 0xff) << 8) + ((v & 0xff00) >> 8);
1246         }
1247         break;
1248     }
1249     case 4:
1250     case 8: // typeSize is 2 x 32bit, behaves the same this time
1251     {
1252         GLuint *data = reinterpret_cast<GLuint *>(&dataBuffer[0]);
1253         for (int i = 0; i < bufferSize / 4; i++)
1254         {
1255             GLuint v = data[i];
1256             data[i]  = ((v & 0xff) << 24) + ((v & 0xff00) << 8) + ((v & 0xff0000) >> 8) + ((v & 0xff000000) >> 24);
1257         }
1258         break;
1259     }
1260     default:
1261         TCU_FAIL("Invalid size for swapBytes");
1262     }
1263 }
1264 
getPixelFormat(GLenum format) const1265 const PixelFormat &RectangleTest::getPixelFormat(GLenum format) const
1266 {
1267     const PixelFormat *formats;
1268     int formatsCount;
1269     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
1270     {
1271         formats      = esFormats;
1272         formatsCount = DE_LENGTH_OF_ARRAY(esFormats);
1273     }
1274     else
1275     {
1276         formats      = coreFormats;
1277         formatsCount = DE_LENGTH_OF_ARRAY(coreFormats);
1278     }
1279 
1280     // Look up pixel format from a GL enum
1281     for (int i = 0; i < formatsCount; i++)
1282     {
1283         if (formats[i].format == format)
1284             return formats[i];
1285     }
1286 
1287     TCU_FAIL("Unsuported format.");
1288     return formats[0];
1289 }
1290 
getPixelType(GLenum type) const1291 const PixelType &RectangleTest::getPixelType(GLenum type) const
1292 {
1293     const PixelType *types;
1294     int typesCount;
1295     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
1296     {
1297         types      = esTypes;
1298         typesCount = DE_LENGTH_OF_ARRAY(esTypes);
1299     }
1300     else
1301     {
1302         types      = coreTypes;
1303         typesCount = DE_LENGTH_OF_ARRAY(coreTypes);
1304     }
1305 
1306     for (int i = 0; i < typesCount; i++)
1307     {
1308         if (types[i].type == type)
1309             return types[i];
1310     }
1311 
1312     TCU_FAIL("Unsuported type.");
1313     return types[0];
1314 }
1315 
getCanonicalFormat(const InternalFormat & internalformat,GLenum format,GLenum type) const1316 const EnumFormats *RectangleTest::getCanonicalFormat(const InternalFormat &internalformat, GLenum format,
1317                                                      GLenum type) const
1318 {
1319     // function returns a canonical format from internal format. for example
1320     // GL_RGBA16F => { GL_RGBA, GL_FLOAT }; used mostly for GLES tests
1321 
1322     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
1323     {
1324         for (int i = 0; i < DE_LENGTH_OF_ARRAY(esValidFormats); ++i)
1325         {
1326             if ((esValidFormats[i].internalformat == internalformat.sizedFormat) &&
1327                 (esValidFormats[i].format == format) && (esValidFormats[i].type == type))
1328             {
1329                 return &(esValidFormats[i]);
1330             }
1331         }
1332 
1333         const glu::ContextInfo &contextInfo = m_context.getContextInfo();
1334         if (contextInfo.isExtensionSupported("GL_EXT_texture_type_2_10_10_10_REV"))
1335         {
1336             for (int i = 0; i < DE_LENGTH_OF_ARRAY(validformats_EXT_texture_type_2_10_10_10_REV); ++i)
1337             {
1338                 if (validformats_EXT_texture_type_2_10_10_10_REV[i].internalformat == internalformat.sizedFormat &&
1339                     validformats_EXT_texture_type_2_10_10_10_REV[i].format == format &&
1340                     validformats_EXT_texture_type_2_10_10_10_REV[i].type == type)
1341                 {
1342                     return &(validformats_EXT_texture_type_2_10_10_10_REV[i]);
1343                 }
1344             }
1345 
1346             if (contextInfo.isExtensionSupported("GL_OES_required_internalformat"))
1347             {
1348                 for (int i = 0; i < DE_LENGTH_OF_ARRAY(validformats_OES_required_internalformat); ++i)
1349                 {
1350                     if (validformats_OES_required_internalformat[i].internalformat == internalformat.sizedFormat &&
1351                         validformats_OES_required_internalformat[i].format == format &&
1352                         validformats_OES_required_internalformat[i].type == type)
1353                     {
1354                         return &(validformats_OES_required_internalformat[i]);
1355                     }
1356                 }
1357             }
1358         }
1359     }
1360     else
1361     {
1362         for (int i = 0; i < DE_LENGTH_OF_ARRAY(coreValidFormats); ++i)
1363         {
1364             if ((coreValidFormats[i].internalformat == internalformat.sizedFormat) &&
1365                 (coreValidFormats[i].format == format) && (coreValidFormats[i].type == type))
1366             {
1367                 return &(coreValidFormats[i]);
1368             }
1369         }
1370     }
1371 
1372     return 0;
1373 }
1374 
getSampler(const PixelType & type,const PixelFormat & format) const1375 InternalFormatSamplerType RectangleTest::getSampler(const PixelType &type, const PixelFormat &format) const
1376 {
1377     switch (type.storage)
1378     {
1379     case STORAGE_FLOAT:
1380         return SAMPLER_FLOAT;
1381 
1382     case STORAGE_UNSIGNED:
1383         if ((format.componentFormat == FORMAT_COLOR_INTEGER) || (format.componentFormat == FORMAT_STENCIL))
1384             return SAMPLER_UINT;
1385         return SAMPLER_UNORM;
1386 
1387     case STORAGE_SIGNED:
1388         if (format.componentFormat == FORMAT_COLOR_INTEGER)
1389             return SAMPLER_INT;
1390         return SAMPLER_NORM;
1391 
1392     default:
1393         TCU_FAIL("Invalid storage specifier");
1394     }
1395 }
1396 
createGradient()1397 void RectangleTest::createGradient()
1398 {
1399     switch (m_inputType.type)
1400     {
1401     case GL_UNSIGNED_BYTE:
1402         makeGradient(unpack_UNSIGNED_BYTE);
1403         break;
1404     case GL_BYTE:
1405         makeGradient<GLbyte>(unpack_BYTE);
1406         break;
1407     case GL_UNSIGNED_SHORT:
1408         makeGradient<GLushort>(unpack_UNSIGNED_SHORT);
1409         break;
1410     case GL_SHORT:
1411         makeGradient<GLshort>(unpack_SHORT);
1412         break;
1413     case GL_UNSIGNED_INT:
1414         makeGradient<GLuint>(unpack_UNSIGNED_INT);
1415         break;
1416     case GL_INT:
1417         makeGradient<GLint>(unpack_INT);
1418         break;
1419     case GL_HALF_FLOAT:
1420         makeGradient<GLhalf>(unpack_HALF_FLOAT);
1421         break;
1422     case GL_FLOAT:
1423         makeGradient<GLfloat>(unpack_FLOAT);
1424         break;
1425     case GL_UNSIGNED_SHORT_5_6_5:
1426         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_5_6_5);
1427         break;
1428     case GL_UNSIGNED_SHORT_4_4_4_4:
1429         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_4_4_4_4);
1430         break;
1431     case GL_UNSIGNED_SHORT_5_5_5_1:
1432         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_5_5_5_1);
1433         break;
1434     case GL_UNSIGNED_INT_2_10_10_10_REV:
1435         makeGradient<GLuint>(unpack_UNSIGNED_INT_2_10_10_10_REV);
1436         break;
1437     case GL_UNSIGNED_INT_24_8:
1438         makeGradient<GLuint>(unpack_UNSIGNED_INT_24_8);
1439         break;
1440     case GL_UNSIGNED_INT_10F_11F_11F_REV:
1441         makeGradient<GLuint>(unpack_UNSIGNED_INT_10F_11F_11F_REV);
1442         break;
1443     case GL_UNSIGNED_INT_5_9_9_9_REV:
1444         makeGradient<GLuint>(unpack_UNSIGNED_INT_5_9_9_9_REV);
1445         break;
1446     case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1447         makeGradient<F_32_UINT_24_8_REV>(unpack_FLOAT_32_UNSIGNED_INT_24_8_REV);
1448         break;
1449     case GL_UNSIGNED_BYTE_3_3_2:
1450         makeGradient<GLubyte>(unpack_UNSIGNED_BYTE_3_3_2);
1451         break;
1452     case GL_UNSIGNED_BYTE_2_3_3_REV:
1453         makeGradient<GLubyte>(unpack_UNSIGNED_BYTE_2_3_3_REV);
1454         break;
1455     case GL_UNSIGNED_SHORT_5_6_5_REV:
1456         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_5_6_5_REV);
1457         break;
1458     case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1459         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_4_4_4_4_REV);
1460         break;
1461     case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1462         makeGradient<GLushort>(unpack_UNSIGNED_SHORT_1_5_5_5_REV);
1463         break;
1464     case GL_UNSIGNED_INT_8_8_8_8:
1465         makeGradient<GLuint>(unpack_UNSIGNED_INT_8_8_8_8);
1466         break;
1467     case GL_UNSIGNED_INT_8_8_8_8_REV:
1468         makeGradient<GLuint>(unpack_UNSIGNED_INT_8_8_8_8_REV);
1469         break;
1470     case GL_UNSIGNED_INT_10_10_10_2:
1471         makeGradient<GLuint>(unpack_UNSIGNED_INT_10_10_10_2);
1472         break;
1473     default:
1474         TCU_FAIL("Unsupported type");
1475     }
1476 }
1477 
1478 template <typename Type>
makeGradient(Type (* unpack)(float))1479 void RectangleTest::makeGradient(Type (*unpack)(float))
1480 {
1481     // number of elements in a group
1482     int elementsInGroup = m_inputFormat.components;
1483     if (m_inputType.special)
1484         elementsInGroup = 1;
1485 
1486     int rowCount = m_unpackProperties.rowCount;
1487     if (rowCount == 0)
1488         rowCount = GRADIENT_HEIGHT + m_unpackProperties.skipRows;
1489 
1490     // number of groups in the row
1491     int rowLength = m_unpackProperties.rowLength;
1492     if (rowLength == 0)
1493         rowLength = GRADIENT_WIDTH + m_unpackProperties.skipPixels;
1494 
1495     int elementSize = m_inputType.size;
1496 
1497     // row size (in elements)
1498     int elementsInRowNoAlign = elementsInGroup * rowLength;
1499     int elementsInRow        = elementsInRowNoAlign;
1500     if (elementSize < m_unpackProperties.alignment)
1501     {
1502         int alignment = m_unpackProperties.alignment;
1503         elementsInRow = (int)(alignment * deFloatCeil(elementSize * elementsInGroup * rowLength / ((float)alignment))) /
1504                         elementSize;
1505     }
1506 
1507     if (m_textureTarget == GL_TEXTURE_2D)
1508         m_unpackProperties.skipImages = 0;
1509 
1510     // "depth" will be 1 + skipped image layers.
1511     // We still want to work on a 2D-ish image later.
1512     int depth = 1 + m_unpackProperties.skipImages;
1513 
1514     m_unpackProperties.elementsInGroup      = elementsInGroup;
1515     m_unpackProperties.rowCount             = rowCount;
1516     m_unpackProperties.rowLength            = rowLength;
1517     m_unpackProperties.elementSize          = elementSize;
1518     m_unpackProperties.elementsInRowNoAlign = elementsInRowNoAlign;
1519     m_unpackProperties.elementsInRow        = elementsInRow;
1520     m_unpackProperties.imagesCount          = depth;
1521 
1522     // element size * elements in row * number of rows * number of 2d images
1523     std::size_t bufferSize = elementSize * elementsInRow * rowCount * depth;
1524 
1525     m_gradient.resize(bufferSize);
1526     Type *data = reinterpret_cast<Type *>(&m_gradient[0]);
1527 
1528     std::size_t dataToSkip  = m_unpackProperties.skipImages * rowCount * elementsInRow;
1529     std::size_t index       = dataToSkip;
1530     const Type defaultValue = unpack(0.5f);
1531     std::fill(data, data + dataToSkip, defaultValue);
1532 
1533     for (int k = m_unpackProperties.skipImages; k < depth; k++)
1534     {
1535         for (int j = 0; j < rowCount; j++)
1536         {
1537             for (int i = 0; i < elementsInRow; i++)
1538             {
1539                 DE_ASSERT(index < bufferSize);
1540                 int x = i / elementsInGroup;
1541                 if ((k == depth - 1) && (m_unpackProperties.skipRows <= j) &&
1542                     (j < m_unpackProperties.skipRows + GRADIENT_HEIGHT) && (m_unpackProperties.skipPixels <= x) &&
1543                     (x < m_unpackProperties.skipPixels + GRADIENT_WIDTH))
1544                 {
1545                     float value = static_cast<float>(x - m_unpackProperties.skipPixels) / GRADIENT_WIDTH;
1546                     int channel = i - elementsInGroup * x;
1547                     value *= 1.0f - 0.25f * channel;
1548                     data[index] = unpack(value);
1549                 }
1550                 else
1551                 {
1552                     data[index] = defaultValue;
1553                 }
1554                 index++;
1555             }
1556         }
1557     }
1558 }
1559 
1560 template <typename Type>
unpackSizedComponents(float value,int s1,int s2,int s3,int s4)1561 Type RectangleTest::unpackSizedComponents(float value, int s1, int s2, int s3, int s4)
1562 {
1563     int typeBits = sizeof(Type) * 8;
1564     double v     = static_cast<double>(value);
1565     Type c1      = static_cast<Type>(v * 1.00 * ((1 << s1) - 1));
1566     Type c2      = static_cast<Type>(v * 0.75 * ((1 << s2) - 1));
1567     Type c3      = static_cast<Type>(v * 0.50 * ((1 << s3) - 1));
1568     Type c4      = static_cast<Type>(v * 0.25 * ((1 << s4) - 1));
1569     return ((c1) << (typeBits - s1)) | ((c2) << (typeBits - s1 - s2)) | ((c3) << (typeBits - s1 - s2 - s3)) |
1570            ((c4) << (typeBits - s1 - s2 - s3 - s4));
1571 }
1572 
1573 template <typename Type>
unpackSizedComponentsRev(float value,int s1,int s2,int s3,int s4)1574 Type RectangleTest::unpackSizedComponentsRev(float value, int s1, int s2, int s3, int s4)
1575 {
1576     int typeBits = sizeof(Type) * 8;
1577     double v     = static_cast<double>(value);
1578     Type c1      = static_cast<Type>(v * 1.00 * ((1 << s4) - 1));
1579     Type c2      = static_cast<Type>(v * 0.75 * ((1 << s3) - 1));
1580     Type c3      = static_cast<Type>(v * 0.50 * ((1 << s2) - 1));
1581     Type c4      = static_cast<Type>(v * 0.25 * ((1 << s1) - 1));
1582     return ((c4) << (typeBits - s1)) | ((c3) << (typeBits - s1 - s2)) | ((c2) << (typeBits - s1 - s2 - s3)) |
1583            ((c1) << (typeBits - s1 - s2 - s3 - s4));
1584 }
1585 
unpack_UNSIGNED_BYTE(float value)1586 GLubyte RectangleTest::unpack_UNSIGNED_BYTE(float value)
1587 {
1588     return static_cast<GLubyte>(value * std::numeric_limits<GLubyte>::max());
1589 }
1590 
unpack_BYTE(float value)1591 GLbyte RectangleTest::unpack_BYTE(float value)
1592 {
1593     return static_cast<GLbyte>(value * std::numeric_limits<GLbyte>::max());
1594 }
1595 
unpack_UNSIGNED_SHORT(float value)1596 GLushort RectangleTest::unpack_UNSIGNED_SHORT(float value)
1597 {
1598     return static_cast<GLushort>(value * std::numeric_limits<GLushort>::max());
1599 }
1600 
unpack_SHORT(float value)1601 GLshort RectangleTest::unpack_SHORT(float value)
1602 {
1603     return static_cast<GLshort>(value * std::numeric_limits<GLshort>::max());
1604 }
1605 
unpack_UNSIGNED_INT(float value)1606 GLuint RectangleTest::unpack_UNSIGNED_INT(float value)
1607 {
1608     return static_cast<GLuint>(value * std::numeric_limits<GLuint>::max());
1609 }
1610 
unpack_INT(float value)1611 GLint RectangleTest::unpack_INT(float value)
1612 {
1613     return static_cast<GLint>(value * std::numeric_limits<GLint>::max());
1614 }
1615 
unpack_HALF_FLOAT(float value)1616 GLhalf RectangleTest::unpack_HALF_FLOAT(float value)
1617 {
1618     return floatToHalfFloat(value);
1619 }
1620 
unpack_FLOAT(float value)1621 GLfloat RectangleTest::unpack_FLOAT(float value)
1622 {
1623     return value;
1624 }
1625 
unpack_UNSIGNED_BYTE_3_3_2(float value)1626 GLubyte RectangleTest::unpack_UNSIGNED_BYTE_3_3_2(float value)
1627 {
1628     return unpackSizedComponents<GLubyte>(value, 3, 3, 2, 0);
1629 }
1630 
unpack_UNSIGNED_BYTE_2_3_3_REV(float value)1631 GLubyte RectangleTest::unpack_UNSIGNED_BYTE_2_3_3_REV(float value)
1632 {
1633     return unpackSizedComponentsRev<GLubyte>(value, 2, 3, 3, 0);
1634 }
1635 
unpack_UNSIGNED_SHORT_5_6_5_REV(float value)1636 GLushort RectangleTest::unpack_UNSIGNED_SHORT_5_6_5_REV(float value)
1637 {
1638     return unpackSizedComponentsRev<GLushort>(value, 5, 6, 5, 0);
1639 }
1640 
unpack_UNSIGNED_SHORT_4_4_4_4_REV(float value)1641 GLushort RectangleTest::unpack_UNSIGNED_SHORT_4_4_4_4_REV(float value)
1642 {
1643     return unpackSizedComponentsRev<GLushort>(value, 4, 4, 4, 4);
1644 }
1645 
unpack_UNSIGNED_SHORT_1_5_5_5_REV(float value)1646 GLushort RectangleTest::unpack_UNSIGNED_SHORT_1_5_5_5_REV(float value)
1647 {
1648     return unpackSizedComponentsRev<GLushort>(value, 1, 5, 5, 5);
1649 }
1650 
unpack_UNSIGNED_INT_8_8_8_8(float value)1651 GLuint RectangleTest::unpack_UNSIGNED_INT_8_8_8_8(float value)
1652 {
1653     return unpackSizedComponents<GLuint>(value, 8, 8, 8, 8);
1654 }
1655 
unpack_UNSIGNED_INT_8_8_8_8_REV(float value)1656 GLuint RectangleTest::unpack_UNSIGNED_INT_8_8_8_8_REV(float value)
1657 {
1658     return unpackSizedComponentsRev<GLuint>(value, 8, 8, 8, 8);
1659 }
1660 
unpack_UNSIGNED_INT_10_10_10_2(float value)1661 GLuint RectangleTest::unpack_UNSIGNED_INT_10_10_10_2(float value)
1662 {
1663     return unpackSizedComponents<GLuint>(value, 10, 10, 10, 2);
1664 }
1665 
unpack_UNSIGNED_SHORT_5_6_5(float value)1666 GLushort RectangleTest::unpack_UNSIGNED_SHORT_5_6_5(float value)
1667 {
1668     return unpackSizedComponents<GLushort>(value, 5, 6, 5, 0);
1669 }
1670 
unpack_UNSIGNED_SHORT_4_4_4_4(float value)1671 GLushort RectangleTest::unpack_UNSIGNED_SHORT_4_4_4_4(float value)
1672 {
1673     return unpackSizedComponents<GLushort>(value, 4, 4, 4, 4);
1674 }
1675 
unpack_UNSIGNED_SHORT_5_5_5_1(float value)1676 GLushort RectangleTest::unpack_UNSIGNED_SHORT_5_5_5_1(float value)
1677 {
1678     return unpackSizedComponents<GLushort>(value, 5, 5, 5, 1);
1679 }
1680 
unpack_UNSIGNED_INT_2_10_10_10_REV(float value)1681 GLuint RectangleTest::unpack_UNSIGNED_INT_2_10_10_10_REV(float value)
1682 {
1683     return unpackSizedComponentsRev<GLuint>(value, 2, 10, 10, 10);
1684 }
1685 
unpack_UNSIGNED_INT_24_8(float value)1686 GLuint RectangleTest::unpack_UNSIGNED_INT_24_8(float value)
1687 {
1688     return unpackSizedComponents<GLuint>(value, 24, 8, 0, 0);
1689 }
1690 
unpack_UNSIGNED_INT_5_9_9_9_REV(float value)1691 GLuint RectangleTest::unpack_UNSIGNED_INT_5_9_9_9_REV(float value)
1692 {
1693     const int N     = 9;
1694     const int B     = 15;
1695     const int E_max = 31;
1696 
1697     GLfloat red   = value * 1.00f;
1698     GLfloat green = value * 0.75f;
1699     GLfloat blue  = value * 0.50f;
1700 
1701     GLfloat sharedExpMax =
1702         (deFloatPow(2.0f, (float)N) - 1.0f) / deFloatPow(2.0f, (float)N) * deFloatPow(2.0f, (float)(E_max - B));
1703 
1704     GLfloat red_c   = deFloatMax(0, deFloatMin(sharedExpMax, red));
1705     GLfloat green_c = deFloatMax(0, deFloatMin(sharedExpMax, green));
1706     GLfloat blue_c  = deFloatMax(0, deFloatMin(sharedExpMax, blue));
1707 
1708     GLfloat max_c = deFloatMax(deFloatMax(red_c, green_c), blue_c);
1709 
1710     GLfloat exp_p = deFloatMax(-B - 1, deFloatFloor(deFloatLog2(max_c))) + 1 + B;
1711 
1712     GLfloat max_s = deFloatFloor(max_c / deFloatPow(2.0f, exp_p - (float)B - (float)N) + 0.5f);
1713 
1714     GLfloat exp_s;
1715 
1716     if (0 <= max_s && max_s < deFloatPow(2.0f, (float)N))
1717         exp_s = exp_p;
1718     else
1719         exp_s = exp_p + 1;
1720 
1721     GLfloat red_s   = deFloatFloor(red_c / deFloatPow(2.0f, exp_s - (float)B - (float)N) + 0.5f);
1722     GLfloat green_s = deFloatFloor(green_c / deFloatPow(2.0f, exp_s - (float)B - (float)N) + 0.5f);
1723     GLfloat blue_s  = deFloatFloor(blue_c / deFloatPow(2.0f, exp_s - (float)B - (float)N) + 0.5f);
1724 
1725     GLuint c1 = (static_cast<GLuint>(red_s)) & 511;
1726     GLuint c2 = (static_cast<GLuint>(green_s)) & 511;
1727     GLuint c3 = (static_cast<GLuint>(blue_s)) & 511;
1728     GLuint c4 = (static_cast<GLuint>(exp_s)) & 31;
1729 
1730     return (c1) | (c2 << 9) | (c3 << 18) | (c4 << 27);
1731 }
1732 
unpack_UNSIGNED_INT_10F_11F_11F_REV(float value)1733 GLuint RectangleTest::unpack_UNSIGNED_INT_10F_11F_11F_REV(float value)
1734 {
1735     GLuint c1 = floatToUnisgnedF11(value * 1.00f);
1736     GLuint c2 = floatToUnisgnedF11(value * 0.75f);
1737     GLuint c3 = floatToUnisgnedF10(value * 0.50f);
1738     return (c3 << 22) | (c2 << 11) | (c1);
1739 }
1740 
unpack_FLOAT_32_UNSIGNED_INT_24_8_REV(float value)1741 F_32_UINT_24_8_REV RectangleTest::unpack_FLOAT_32_UNSIGNED_INT_24_8_REV(float value)
1742 {
1743     F_32_UINT_24_8_REV ret;
1744     ret.d = value;
1745     ret.s = (GLuint)(value * 255.0 * 0.75);
1746     ret.s &= 0xff;
1747     return ret;
1748 }
1749 
isFormatValid(const PixelFormat & format,const PixelType & type,const struct InternalFormat & internalformat,bool checkInput,bool checkOutput,int operation) const1750 bool RectangleTest::isFormatValid(const PixelFormat &format, const PixelType &type,
1751                                   const struct InternalFormat &internalformat, bool checkInput, bool checkOutput,
1752                                   int operation) const
1753 {
1754     glu::RenderContext &renderContext   = m_context.getRenderContext();
1755     glu::ContextType contextType        = renderContext.getType();
1756     const glu::ContextInfo &contextInfo = m_context.getContextInfo();
1757     const Functions &gl                 = renderContext.getFunctions();
1758 
1759     int i;
1760 
1761     // Test the combination of input format, input type and internalFormat
1762     if (glu::isContextTypeES(contextType))
1763     {
1764         if (checkInput)
1765         {
1766             // GLES30 has more restricted requirement on combination than GL for input
1767             for (i = 0; i < DE_LENGTH_OF_ARRAY(esValidFormats); ++i)
1768             {
1769                 if (internalformat.sizedFormat == esValidFormats[i].internalformat &&
1770                     format.format == esValidFormats[i].format && type.type == esValidFormats[i].type)
1771                 {
1772                     break;
1773                 }
1774             }
1775 
1776             if (i == DE_LENGTH_OF_ARRAY(esValidFormats))
1777             {
1778                 // Check for support of OES_texture_float extension
1779                 if (((GL_LUMINANCE_ALPHA == format.format) && (GL_LUMINANCE_ALPHA == internalformat.sizedFormat)) ||
1780                     ((GL_LUMINANCE == format.format) && (GL_LUMINANCE == internalformat.sizedFormat)) ||
1781                     ((GL_ALPHA == format.format) && (GL_ALPHA == internalformat.sizedFormat)) ||
1782                     ((GL_RGBA == format.format) && (GL_RGBA == internalformat.sizedFormat)) ||
1783                     ((GL_RGB == format.format) && (GL_RGB == internalformat.sizedFormat)))
1784                 {
1785                     if ((contextInfo.isExtensionSupported("GL_OES_texture_float") && (GL_FLOAT == type.type)) ||
1786                         (contextInfo.isExtensionSupported("GL_OES_texture_half_float") &&
1787                          (GL_HALF_FLOAT_OES == type.type)))
1788                     {
1789                         return true;
1790                     }
1791                 }
1792 
1793                 // Check for support of EXT_texture_type_2_10_10_10_REV extension
1794                 if (((GL_RGBA == format.format) && (GL_RGBA == internalformat.sizedFormat)) ||
1795                     ((GL_RGB == format.format) && (GL_RGB == internalformat.sizedFormat)))
1796                 {
1797                     if (contextInfo.isExtensionSupported("GL_EXT_texture_type_2_10_10_10_REV") &&
1798                         ((GL_UNSIGNED_INT_2_10_10_10_REV_EXT == type.type)))
1799                         return true;
1800                 }
1801 
1802                 // Check for support of NV_packed_float extension
1803                 if ((GL_RGB == format.format) && (GL_RGB == internalformat.sizedFormat))
1804                 {
1805                     if (contextInfo.isExtensionSupported("GL_NV_packed_float") &&
1806                         ((GL_UNSIGNED_INT_10F_11F_11F_REV == type.type)))
1807                         return true;
1808                 }
1809 
1810                 // Check for support of EXT_texture_type_2_10_10_10_REV and GL_OES_required_internalformat extensions
1811                 if (contextInfo.isExtensionSupported("GL_EXT_texture_type_2_10_10_10_REV") &&
1812                     contextInfo.isExtensionSupported("GL_OES_required_internalformat"))
1813                 {
1814                     for (i = 0; i < DE_LENGTH_OF_ARRAY(validformats_OES_required_internalformat); ++i)
1815                     {
1816                         if (internalformat.sizedFormat == validformats_OES_required_internalformat[i].internalformat &&
1817                             format.format == validformats_OES_required_internalformat[i].format &&
1818                             type.type == validformats_OES_required_internalformat[i].type)
1819                         {
1820                             return true;
1821                         }
1822                     }
1823                 }
1824 
1825                 return false;
1826             }
1827 
1828             if ((m_textureTarget == GL_TEXTURE_3D) &&
1829                 ((format.format == GL_DEPTH_COMPONENT) || (format.format == GL_DEPTH_STENCIL)))
1830                 return false;
1831         }
1832         else if (checkOutput)
1833         {
1834             // GLES30 has more restricted requirement on combination than GL for output
1835             // As stated in Section Reading Pixels
1836             InternalFormatSamplerType sampler = internalformat.sampler;
1837 
1838             const PixelFormat &inputFormat = getPixelFormat(internalformat.format);
1839 
1840             if (inputFormat.attachment == GL_DEPTH_ATTACHMENT && contextInfo.isExtensionSupported("GL_NV_read_depth") &&
1841                 format.format == GL_DEPTH_COMPONENT &&
1842                 ((sampler == SAMPLER_FLOAT && type.type == GL_FLOAT) ||
1843                  (sampler != SAMPLER_FLOAT && (type.type == GL_UNSIGNED_SHORT || type.type == GL_UNSIGNED_INT ||
1844                                                type.type == GL_UNSIGNED_INT_24_8))))
1845             {
1846                 return true;
1847             }
1848 
1849             if (inputFormat.attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
1850                 contextInfo.isExtensionSupported("GL_NV_read_depth_stencil") &&
1851                 ((format.format == GL_DEPTH_STENCIL &&
1852                   ((sampler == SAMPLER_FLOAT && type.type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) ||
1853                    (sampler != SAMPLER_FLOAT && type.type == GL_UNSIGNED_INT_24_8))) ||
1854                  (format.format == GL_DEPTH_COMPONENT &&
1855                   ((sampler == SAMPLER_FLOAT && type.type == GL_FLOAT) ||
1856                    (sampler != SAMPLER_FLOAT && (type.type == GL_UNSIGNED_SHORT || type.type == GL_UNSIGNED_INT ||
1857                                                  type.type == GL_UNSIGNED_INT_24_8))))))
1858             {
1859                 return true;
1860             }
1861 
1862             if (inputFormat.attachment != GL_COLOR_ATTACHMENT0)
1863             {
1864                 return false;
1865             }
1866 
1867             if ((sampler == SAMPLER_UNORM) &&
1868                 (((type.type == GL_UNSIGNED_BYTE) && (format.format == GL_RGB) &&
1869                   (internalformat.sizedFormat == GL_SRGB8)) ||
1870                  ((type.type == GL_UNSIGNED_BYTE) && (format.format == GL_RGBA) &&
1871                   (internalformat.sizedFormat == GL_SRGB8_ALPHA8))) &&
1872                 contextInfo.isExtensionSupported("GL_NV_sRGB_formats"))
1873             {
1874                 return true;
1875             }
1876 
1877             if ((sampler == SAMPLER_UNORM) && (type.type == GL_UNSIGNED_SHORT) && (format.format == GL_RGBA) &&
1878                 ((internalformat.sizedFormat == GL_R16) || (internalformat.sizedFormat == GL_RG16) ||
1879                  (internalformat.sizedFormat == GL_RGBA16)) &&
1880                 contextInfo.isExtensionSupported("GL_EXT_texture_norm16"))
1881             {
1882                 return true;
1883             }
1884 
1885             if ((sampler == SAMPLER_NORM) && (type.type == GL_SHORT) && (format.format == GL_RGBA) &&
1886                 ((internalformat.sizedFormat == GL_R16_SNORM) || (internalformat.sizedFormat == GL_RG16_SNORM) ||
1887                  (internalformat.sizedFormat == GL_RGBA16_SNORM)) &&
1888                 (contextInfo.isExtensionSupported("GL_EXT_texture_norm16") &&
1889                  contextInfo.isExtensionSupported("GL_EXT_render_snorm")))
1890             {
1891                 return true;
1892             }
1893 
1894             if ((sampler == SAMPLER_NORM) && (type.type == GL_BYTE) && (format.format == GL_RGBA) &&
1895                 ((internalformat.sizedFormat == GL_R8_SNORM) || (internalformat.sizedFormat == GL_RG8_SNORM) ||
1896                  (internalformat.sizedFormat == GL_RGBA8_SNORM)) &&
1897                 contextInfo.isExtensionSupported("GL_EXT_render_snorm"))
1898             {
1899                 return true;
1900             }
1901 
1902             GLint implementType;
1903             GLint implementFormat;
1904             gl.getIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &implementType);
1905             gl.getIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &implementFormat);
1906             GLenum err                 = gl.getError();
1907             GLenum implementTypeEnum   = static_cast<GLenum>(implementType);
1908             GLenum implementFormatEnum = static_cast<GLenum>(implementFormat);
1909 
1910             if (((sampler == SAMPLER_UNORM) && (type.type == GL_UNSIGNED_BYTE) && (format.format == GL_RGBA)) ||
1911                 ((sampler == SAMPLER_UINT) && (type.type == GL_UNSIGNED_INT) && (format.format == GL_RGBA_INTEGER)) ||
1912                 ((sampler == SAMPLER_INT) && (type.type == GL_INT) && (format.format == GL_RGBA_INTEGER)) ||
1913                 ((sampler == SAMPLER_FLOAT) && (type.type == GL_FLOAT) && (format.format == GL_RGBA)) ||
1914                 ((err == GL_NO_ERROR) && (type.type == implementTypeEnum) && (format.format == implementFormatEnum)) ||
1915                 ((internalformat.sizedFormat == GL_RGB10_A2) && (type.type == GL_UNSIGNED_INT_2_10_10_10_REV) &&
1916                  (format.format == GL_RGBA)))
1917             {
1918                 return true;
1919             }
1920             else
1921             {
1922                 return false;
1923             }
1924         }
1925     }
1926     else
1927     {
1928         if (format.format == GL_DEPTH_STENCIL)
1929         {
1930             if (type.type != GL_UNSIGNED_INT_24_8 && type.type != GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
1931             {
1932                 return false;
1933             }
1934         }
1935 
1936         if ((format.componentFormat == FORMAT_COLOR_INTEGER) && (type.type == GL_FLOAT || type.type == GL_HALF_FLOAT))
1937         {
1938             return false;
1939         }
1940 
1941         if ((internalformat.baseFormat == GL_DEPTH_STENCIL || internalformat.baseFormat == GL_STENCIL_INDEX ||
1942              internalformat.baseFormat == GL_DEPTH_COMPONENT) !=
1943             (format.format == GL_DEPTH_STENCIL || format.format == GL_STENCIL_INDEX ||
1944              format.format == GL_DEPTH_COMPONENT))
1945         {
1946             return false;
1947         }
1948 
1949         if (operation == INPUT_TEXIMAGE)
1950         {
1951             if (format.format == GL_STENCIL_INDEX || internalformat.baseFormat == GL_STENCIL_INDEX)
1952             {
1953                 return false;
1954             }
1955 
1956             if ((format.format == GL_DEPTH_COMPONENT || format.format == GL_DEPTH_STENCIL) &&
1957                 !(internalformat.baseFormat == GL_DEPTH_STENCIL || internalformat.baseFormat == GL_DEPTH_COMPONENT))
1958             {
1959                 return false;
1960             }
1961         }
1962         else if (operation == OUTPUT_GETTEXIMAGE)
1963         {
1964             if ((format.format == GL_STENCIL_INDEX &&
1965                  ((internalformat.baseFormat != GL_STENCIL_INDEX && internalformat.baseFormat != GL_DEPTH_STENCIL) ||
1966                   !contextInfo.isExtensionSupported("GL_ARB_texture_stencil8"))))
1967             {
1968                 return false;
1969             }
1970 
1971             if (format.format == GL_DEPTH_STENCIL && internalformat.baseFormat != GL_DEPTH_STENCIL)
1972             {
1973                 return false;
1974             }
1975         }
1976         else if (operation == OUTPUT_READPIXELS)
1977         {
1978             if (format.format == GL_DEPTH_STENCIL && internalformat.baseFormat != GL_DEPTH_STENCIL)
1979             {
1980                 return false;
1981             }
1982 
1983             if (format.format == GL_DEPTH_COMPONENT && internalformat.baseFormat != GL_DEPTH_STENCIL &&
1984                 internalformat.baseFormat != GL_DEPTH_COMPONENT)
1985             {
1986                 return false;
1987             }
1988 
1989             if (format.format == GL_STENCIL_INDEX && internalformat.baseFormat != GL_DEPTH_STENCIL &&
1990                 internalformat.baseFormat != GL_STENCIL_INDEX)
1991             {
1992                 return false;
1993             }
1994         }
1995 
1996         if (type.special == true)
1997         {
1998             bool valid = false;
1999 
2000             for (i = 0; i < DE_LENGTH_OF_ARRAY(coreValidFormats); ++i)
2001             {
2002                 if (coreValidFormats[i].format == format.format && coreValidFormats[i].type == type.type)
2003                 {
2004                     valid = true;
2005                     break;
2006                 }
2007             }
2008 
2009             if (!valid)
2010                 return false;
2011         }
2012 
2013         if ((format.componentFormat == FORMAT_COLOR_INTEGER) &&
2014             !(internalformat.sampler == SAMPLER_INT || internalformat.sampler == SAMPLER_UINT))
2015         {
2016             return false;
2017         }
2018 
2019         if (!(format.componentFormat == FORMAT_COLOR_INTEGER) &&
2020             (internalformat.sampler == SAMPLER_INT || internalformat.sampler == SAMPLER_UINT))
2021         {
2022             return false;
2023         }
2024 
2025         if ((m_textureTarget == GL_TEXTURE_3D) &&
2026             ((internalformat.baseFormat == GL_DEPTH_COMPONENT) || (internalformat.baseFormat == GL_DEPTH_STENCIL) ||
2027              (internalformat.sizedFormat == GL_COMPRESSED_RED_RGTC1) ||
2028              (internalformat.sizedFormat == GL_COMPRESSED_SIGNED_RED_RGTC1) ||
2029              (internalformat.sizedFormat == GL_COMPRESSED_RG_RGTC2) ||
2030              (internalformat.sizedFormat == GL_COMPRESSED_SIGNED_RG_RGTC2)))
2031         {
2032             return false;
2033         }
2034     }
2035 
2036     return true;
2037 }
2038 
isUnsizedFormat(GLenum format) const2039 bool RectangleTest::isUnsizedFormat(GLenum format) const
2040 {
2041     GLenum formats[]   = {GL_RGBA, GL_RGB, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_ALPHA};
2042     GLenum *formatsEnd = formats + DE_LENGTH_OF_ARRAY(formats);
2043     return (std::find(formats, formatsEnd, format) < formatsEnd);
2044 }
2045 
isSRGBFormat(const InternalFormat & internalFormat) const2046 bool RectangleTest::isSRGBFormat(const InternalFormat &internalFormat) const
2047 {
2048     return (internalFormat.sizedFormat == GL_SRGB8) || (internalFormat.sizedFormat == GL_SRGB8_ALPHA8);
2049 }
2050 
isSNORMFormat(const InternalFormat & internalFormat) const2051 bool RectangleTest::isSNORMFormat(const InternalFormat &internalFormat) const
2052 {
2053     GLenum formats[]   = {GL_R8_SNORM,  GL_RG8_SNORM,  GL_RGB8_SNORM,  GL_RGBA8_SNORM,
2054                           GL_R16_SNORM, GL_RG16_SNORM, GL_RGB16_SNORM, GL_RGBA16_SNORM};
2055     GLenum *formatsEnd = formats + DE_LENGTH_OF_ARRAY(formats);
2056     return (std::find(formats, formatsEnd, internalFormat.sizedFormat) < formatsEnd);
2057 }
2058 
isCopyValid(const InternalFormat & copyInternalFormat,const InternalFormat & internalFormat) const2059 bool RectangleTest::isCopyValid(const InternalFormat &copyInternalFormat, const InternalFormat &internalFormat) const
2060 {
2061     // check if copy between two internal formats is allowed
2062 
2063     int b1 = getPixelFormat(internalFormat.format).components;
2064     int b2 = getPixelFormat(copyInternalFormat.format).components;
2065 
2066     if (b2 > b1)
2067         return false;
2068 
2069     //Check that the types can be converted in CopyTexImage.
2070     if (((copyInternalFormat.sampler == SAMPLER_UINT) && (internalFormat.sampler != SAMPLER_UINT)) ||
2071         ((copyInternalFormat.sampler == SAMPLER_INT) && (internalFormat.sampler != SAMPLER_INT)) ||
2072         (((copyInternalFormat.sampler == SAMPLER_FLOAT) || (internalFormat.sampler == SAMPLER_UNORM) ||
2073           (copyInternalFormat.sampler == SAMPLER_NORM)) &&
2074          (!((copyInternalFormat.sampler == SAMPLER_FLOAT) || (internalFormat.sampler == SAMPLER_UNORM) ||
2075             (internalFormat.sampler == SAMPLER_NORM)))))
2076     {
2077         return false;
2078     }
2079 
2080     // Core GL is less restricted then ES - check it first
2081     if (!glu::isContextTypeES(m_context.getRenderContext().getType()))
2082     {
2083         if ((copyInternalFormat.format == GL_DEPTH_COMPONENT && internalFormat.format != GL_DEPTH_COMPONENT) ||
2084             (copyInternalFormat.format == GL_DEPTH_STENCIL && internalFormat.format != GL_DEPTH_STENCIL) ||
2085             (copyInternalFormat.format == GL_ALPHA && internalFormat.format != GL_ALPHA) ||
2086             (copyInternalFormat.format == GL_LUMINANCE && internalFormat.format != GL_LUMINANCE) ||
2087             (copyInternalFormat.format == GL_LUMINANCE_ALPHA && internalFormat.format != GL_LUMINANCE_ALPHA))
2088         {
2089             return false;
2090         }
2091 
2092         return true;
2093     }
2094 
2095     const glu::ContextInfo &contextInfo = m_context.getContextInfo();
2096 
2097     // GLES30 has more restricted requirement on glCopyTexImage2D
2098     // As stated in Table 3.15 and comment to glCopyTexImage2D
2099     if ((internalFormat.baseFormat == GL_DEPTH_COMPONENT) || (internalFormat.baseFormat == GL_DEPTH_STENCIL) ||
2100         (copyInternalFormat.baseFormat == GL_DEPTH_COMPONENT) || (copyInternalFormat.baseFormat == GL_DEPTH_STENCIL) ||
2101         ((internalFormat.baseFormat != GL_RGBA && internalFormat.baseFormat != GL_ALPHA) &&
2102          ((copyInternalFormat.baseFormat == GL_ALPHA) || (copyInternalFormat.baseFormat == GL_LUMINANCE_ALPHA))) ||
2103         ((internalFormat.baseFormat == GL_ALPHA) &&
2104          ((copyInternalFormat.baseFormat != GL_RGBA) && (copyInternalFormat.baseFormat != GL_ALPHA) &&
2105           (copyInternalFormat.baseFormat != GL_LUMINANCE_ALPHA))) ||
2106         (isSRGBFormat(internalFormat) != isSRGBFormat(copyInternalFormat)) ||
2107         // GLES30 does not define ReadPixels types for signed normalized fixed point formats in Table 3.14,
2108         // and conversions to SNORM internalformats are not allowed by Table 3.2
2109         (copyInternalFormat.sampler == SAMPLER_NORM) ||
2110         ((copyInternalFormat.sizedFormat == GL_RGB9_E5) &&
2111          (!contextInfo.isExtensionSupported("GL_APPLE_color_buffer_packed_float") &&
2112           !contextInfo.isExtensionSupported("GL_QCOM_render_shared_exponent"))))
2113     {
2114         /* Some formats are activated by extensions, check. */
2115         if (((internalFormat.baseFormat == GL_LUMINANCE && copyInternalFormat.baseFormat == GL_LUMINANCE) ||
2116              (internalFormat.baseFormat == GL_ALPHA && copyInternalFormat.baseFormat == GL_ALPHA) ||
2117              (internalFormat.baseFormat == GL_LUMINANCE_ALPHA &&
2118               (copyInternalFormat.baseFormat == GL_LUMINANCE_ALPHA || copyInternalFormat.baseFormat == GL_LUMINANCE ||
2119                copyInternalFormat.baseFormat == GL_ALPHA))) &&
2120             contextInfo.isExtensionSupported("GL_NV_render_luminance_alpha"))
2121         {
2122             return true;
2123         }
2124         else if (contextInfo.isExtensionSupported("GL_EXT_render_snorm") && isSNORMFormat(copyInternalFormat) &&
2125                  (internalFormat.sampler == copyInternalFormat.sampler) &&
2126                  (((copyInternalFormat.baseFormat == GL_RED) &&
2127                    (internalFormat.baseFormat == GL_RED || internalFormat.baseFormat == GL_RG ||
2128                     internalFormat.baseFormat == GL_RGB || internalFormat.baseFormat == GL_RGBA ||
2129                     copyInternalFormat.baseFormat == GL_LUMINANCE)) ||
2130                   ((copyInternalFormat.baseFormat == GL_RG) &&
2131                    (internalFormat.baseFormat == GL_RG || internalFormat.baseFormat == GL_RGB ||
2132                     internalFormat.baseFormat == GL_RGBA)) ||
2133                   ((copyInternalFormat.baseFormat == GL_RGB) &&
2134                    (internalFormat.baseFormat == GL_RGB || internalFormat.baseFormat == GL_RGBA)) ||
2135                   ((copyInternalFormat.baseFormat == GL_RGBA) && (internalFormat.baseFormat == GL_RGBA))))
2136         {
2137             return true;
2138         }
2139 
2140         return false;
2141     }
2142     else
2143     {
2144         if (internalFormat.sampler != copyInternalFormat.sampler)
2145         {
2146             // You can't convert between different base types, for example NORM<->FLOAT.
2147             return false;
2148         }
2149         if (!isUnsizedFormat(copyInternalFormat.sizedFormat))
2150         {
2151             if ((internalFormat.bits.bits.red && copyInternalFormat.bits.bits.red &&
2152                  internalFormat.bits.bits.red != copyInternalFormat.bits.bits.red) ||
2153                 (internalFormat.bits.bits.green && copyInternalFormat.bits.bits.green &&
2154                  internalFormat.bits.bits.green != copyInternalFormat.bits.bits.green) ||
2155                 (internalFormat.bits.bits.blue && copyInternalFormat.bits.bits.blue &&
2156                  internalFormat.bits.bits.blue != copyInternalFormat.bits.bits.blue) ||
2157                 (internalFormat.bits.bits.alpha && copyInternalFormat.bits.bits.alpha &&
2158                  internalFormat.bits.bits.alpha != copyInternalFormat.bits.bits.alpha))
2159             {
2160                 // If the destination internalFormat is sized we don't allow component size changes.
2161                 return false;
2162             }
2163         }
2164         else
2165         {
2166             if (internalFormat.sizedFormat == GL_RGB10_A2)
2167             {
2168                 // Not allowed to convert from a GL_RGB10_A2 surface.
2169                 return false;
2170             }
2171         }
2172     }
2173 
2174     return true;
2175 }
2176 
isFBOImageAttachValid(const InternalFormat & internalformat,GLenum format,GLenum type) const2177 bool RectangleTest::isFBOImageAttachValid(const InternalFormat &internalformat, GLenum format, GLenum type) const
2178 {
2179     const glu::ContextInfo &contextInfo = m_context.getContextInfo();
2180 
2181     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
2182     {
2183         const EnumFormats *validFormat = getCanonicalFormat(internalformat, format, type);
2184         if (validFormat != 0)
2185         {
2186             if (!validFormat->bRenderable)
2187             {
2188                 /* Some formats are activated by extensions, check. */
2189                 if ((GL_RGBA32F == validFormat->internalformat || GL_RGBA16F == validFormat->internalformat ||
2190                      GL_RG32F == validFormat->internalformat || GL_RG16F == validFormat->internalformat ||
2191                      GL_R32F == validFormat->internalformat || GL_R16F == validFormat->internalformat ||
2192                      GL_R11F_G11F_B10F == validFormat->internalformat) &&
2193                     contextInfo.isExtensionSupported("GL_EXT_color_buffer_float"))
2194                 {
2195                     return true;
2196                 }
2197 
2198                 if ((GL_RGBA16F == validFormat->internalformat || GL_RGB16F == validFormat->internalformat ||
2199                      GL_RG16F == validFormat->internalformat || GL_R16F == validFormat->internalformat) &&
2200                     contextInfo.isExtensionSupported("GL_EXT_color_buffer_half_float"))
2201                 {
2202                     return true;
2203                 }
2204 
2205                 if ((GL_R11F_G11F_B10F == validFormat->internalformat || GL_RGB9_E5 == validFormat->internalformat) &&
2206                     contextInfo.isExtensionSupported("GL_APPLE_color_buffer_packed_float"))
2207                 {
2208                     return true;
2209                 }
2210 
2211                 if ((GL_RGB9_E5 == validFormat->internalformat) &&
2212                     contextInfo.isExtensionSupported("GL_QCOM_render_shared_exponent"))
2213                 {
2214                     return true;
2215                 }
2216 
2217                 if ((GL_LUMINANCE == validFormat->internalformat || GL_ALPHA == validFormat->internalformat ||
2218                      GL_LUMINANCE_ALPHA == validFormat->internalformat) &&
2219                     contextInfo.isExtensionSupported("GL_NV_render_luminance_alpha"))
2220                 {
2221                     return true;
2222                 }
2223 
2224                 if ((GL_SRGB8 == validFormat->internalformat) && contextInfo.isExtensionSupported("GL_NV_sRGB_formats"))
2225                 {
2226                     return true;
2227                 }
2228 
2229                 if (((GL_R8_SNORM == validFormat->internalformat) || (GL_RG8_SNORM == validFormat->internalformat) ||
2230                      (GL_RGBA8_SNORM == validFormat->internalformat) || (GL_R16_SNORM == validFormat->internalformat) ||
2231                      (GL_RG16_SNORM == validFormat->internalformat) ||
2232                      (GL_RGBA16_SNORM == validFormat->internalformat)) &&
2233                     contextInfo.isExtensionSupported("GL_EXT_render_snorm"))
2234                 {
2235                     return true;
2236                 }
2237             }
2238             return validFormat->bRenderable;
2239         }
2240         else
2241         {
2242             // Check for NV_packed_float
2243             if (GL_RGB == internalformat.sizedFormat && GL_RGB == format && GL_UNSIGNED_INT_10F_11F_11F_REV == type &&
2244                 contextInfo.isExtensionSupported("GL_NV_packed_float"))
2245             {
2246                 return true;
2247             }
2248             return false;
2249         }
2250     }
2251     else
2252     {
2253         if (format == GL_DEPTH_STENCIL && internalformat.sizedFormat != GL_DEPTH24_STENCIL8 &&
2254             internalformat.sizedFormat != GL_DEPTH32F_STENCIL8)
2255         {
2256             // We can't make a complete DEPTH_STENCIL attachment with a
2257             // texture that does not have both DEPTH and STENCIL components.
2258             return false;
2259         }
2260 
2261         GLenum colorRenderableFrmats[] = {GL_RGBA32F,    GL_RGBA32I, GL_RGBA32UI,     GL_RGBA16,
2262                                           GL_RGBA16F,    GL_RGBA16I, GL_RGBA16UI,     GL_RGBA8,
2263                                           GL_RGBA8I,     GL_RGBA8UI, GL_SRGB8_ALPHA8, GL_RGB10_A2,
2264                                           GL_RGB10_A2UI, GL_RGB5_A1, GL_RGBA4,        GL_R11F_G11F_B10F,
2265                                           GL_RGB565,     GL_RG32F,   GL_RG32I,        GL_RG32UI,
2266                                           GL_RG16,       GL_RG16F,   GL_RG16I,        GL_RG16UI,
2267                                           GL_RG8,        GL_RG8I,    GL_RG8UI,        GL_R32F,
2268                                           GL_R32I,       GL_R32UI,   GL_R16F,         GL_R16I,
2269                                           GL_R16UI,      GL_R16,     GL_R8,           GL_R8I,
2270                                           GL_R8UI};
2271         GLenum *formatsEnd             = colorRenderableFrmats + DE_LENGTH_OF_ARRAY(colorRenderableFrmats);
2272         if (std::find(colorRenderableFrmats, formatsEnd, internalformat.sizedFormat) < formatsEnd)
2273             return true;
2274 
2275         GLenum dsRenderableFormats[] = {
2276             GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT16,
2277             GL_DEPTH32F_STENCIL8,  GL_DEPTH24_STENCIL8,
2278         };
2279 
2280         formatsEnd = dsRenderableFormats + DE_LENGTH_OF_ARRAY(dsRenderableFormats);
2281         if (std::find(dsRenderableFormats, formatsEnd, internalformat.sizedFormat) < formatsEnd)
2282             return true;
2283 
2284         return false;
2285     }
2286 }
2287 
doRead(GLuint texture)2288 bool RectangleTest::doRead(GLuint texture)
2289 {
2290     glu::RenderContext &renderContext = m_context.getRenderContext();
2291     const Functions &gl               = renderContext.getFunctions();
2292 
2293     GLuint fboId;
2294     gl.genFramebuffers(1, &fboId);
2295     gl.bindFramebuffer(GL_FRAMEBUFFER, fboId);
2296     GLU_EXPECT_NO_ERROR(gl.getError(), "glBindFramebuffer");
2297 
2298     bool validImageAttach = isFBOImageAttachValid(m_internalFormat, m_inputFormat.format, m_inputType.type);
2299     if (m_textureTarget == GL_TEXTURE_2D)
2300         gl.framebufferTexture2D(GL_FRAMEBUFFER, m_inputFormat.attachment, GL_TEXTURE_2D, texture, 0);
2301     else if (glu::isContextTypeES(renderContext.getType()))
2302         gl.framebufferTextureLayer(GL_FRAMEBUFFER, m_inputFormat.attachment, texture, 0, 0);
2303     else
2304         gl.framebufferTexture3D(GL_FRAMEBUFFER, m_inputFormat.attachment, GL_TEXTURE_3D, texture, 0, 0);
2305     GLenum status = gl.checkFramebufferStatus(GL_FRAMEBUFFER);
2306 
2307     bool result = true;
2308     if (status == GL_FRAMEBUFFER_COMPLETE)
2309     {
2310         if (!validImageAttach && glu::isContextTypeES(renderContext.getType()))
2311         {
2312             m_testCtx.getLog() << tcu::TestLog::Message << "FBO is complete but expected incomplete with sizedFormat: "
2313                                << getFormatStr(m_internalFormat.sizedFormat) << tcu::TestLog::EndMessage;
2314             result = false;
2315         }
2316         else
2317         {
2318             result &= readPixels(false);
2319             result &= doCopy();
2320         }
2321     }
2322     else if (validImageAttach)
2323     {
2324         m_testCtx.getLog() << tcu::TestLog::Message << "FBO is not complete but expected complete with sizedFormat: "
2325                            << getFormatStr(m_internalFormat.sizedFormat) << tcu::TestLog::EndMessage;
2326         result = false;
2327     }
2328 
2329     gl.deleteFramebuffers(1, &fboId);
2330 
2331     return result;
2332 }
2333 
readPixels(bool isCopy)2334 bool RectangleTest::readPixels(bool isCopy)
2335 {
2336     bool result = true;
2337 
2338     const PixelType *types;
2339     int typesCount;
2340     const PixelFormat *formats;
2341     int formatsCount;
2342 
2343     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
2344     {
2345         types        = esTypes;
2346         typesCount   = DE_LENGTH_OF_ARRAY(esTypes);
2347         formats      = esFormats;
2348         formatsCount = DE_LENGTH_OF_ARRAY(esFormats);
2349     }
2350     else
2351     {
2352         types        = coreTypes;
2353         typesCount   = DE_LENGTH_OF_ARRAY(coreTypes);
2354         formats      = coreFormats;
2355         formatsCount = DE_LENGTH_OF_ARRAY(coreFormats);
2356     }
2357 
2358     // for each output format
2359     for (int m = 0; m < formatsCount; ++m)
2360     {
2361         const PixelFormat &outputFormat = formats[m];
2362 
2363         // for each output type
2364         for (int n = 0; n < typesCount; ++n)
2365         {
2366             const PixelType &outputType = types[n];
2367 
2368             if (isCopy)
2369             {
2370                 // continue when input format,type != canonical format,type and
2371                 // output format,type != canonical format,type
2372                 if ((outputFormat.format != m_copyInternalFormat.format ||
2373                      outputType.type != m_copyInternalFormat.type))
2374                 {
2375                     // invalid output format and type - skipping case
2376                     continue;
2377                 }
2378             }
2379             else if ((m_inputFormat.format != m_internalFormat.format || m_inputType.type != m_internalFormat.type) &&
2380                      (outputFormat.format != m_internalFormat.format || outputType.type != m_internalFormat.type))
2381             {
2382                 // invalid output format and type - skipping case
2383                 continue;
2384             }
2385 
2386             result &= readPixelsInner(outputFormat, outputType, isCopy);
2387         }
2388     }
2389 
2390     return result;
2391 }
2392 
readPixelsInner(const PixelFormat & outputFormat,const PixelType & outputType,bool isCopy)2393 bool RectangleTest::readPixelsInner(const PixelFormat &outputFormat, const PixelType &outputType, bool isCopy)
2394 {
2395     const char *copyStage = "Copy stage: ";
2396 
2397     GLenum readerror = readOutputData(outputFormat, outputType, OUTPUT_READPIXELS);
2398     if (m_outputBuffer.empty())
2399     {
2400         m_testCtx.getLog() << tcu::TestLog::Message << "No buffer allocated" << tcu::TestLog::EndMessage;
2401         return false;
2402     }
2403 
2404     m_countReadPixels++;
2405 
2406     // Check if output format is valid
2407     bool outputFormatValid = isFormatValid(outputFormat, outputType, isCopy ? m_copyInternalFormat : m_internalFormat,
2408                                            false, true, OUTPUT_READPIXELS);
2409 
2410     // Even if this is a valid glReadPixels format, we can't read non-existant components
2411     if (outputFormatValid && outputFormat.format == GL_DEPTH_STENCIL && m_inputFormat.format != GL_DEPTH_STENCIL)
2412         outputFormatValid = false;
2413 
2414     if (outputFormatValid)
2415     {
2416         if (readerror != GL_NO_ERROR)
2417         {
2418             m_testCtx.getLog() << tcu::TestLog::Message << (isCopy ? copyStage : "")
2419                                << "Valid format used but glReadPixels failed for input = ["
2420                                << getFormatStr(m_inputFormat.format) << ", " << getTypeStr(m_inputType.type)
2421                                << "] output = [" << getFormatStr(outputFormat.format) << ", "
2422                                << getTypeStr(outputType.type) << "]" << tcu::TestLog::EndMessage;
2423             return false;
2424         }
2425         else
2426         {
2427             m_countReadPixelsOK++;
2428             m_countCompare++;
2429 
2430             // compare output gradient to input gradient
2431             if (!compare(&m_gradient[0], &m_outputBuffer[0], outputFormat, outputType, isCopy))
2432             {
2433                 m_testCtx.getLog() << tcu::TestLog::Message << (isCopy ? copyStage : "")
2434                                    << "Gradient comparison failed during ReadPixels for input = ["
2435                                    << getFormatStr(m_inputFormat.format) << ", " << getTypeStr(m_inputType.type)
2436                                    << "] output = [" << getFormatStr(outputFormat.format) << ", "
2437                                    << getTypeStr(outputType.type) << "]" << tcu::TestLog::EndMessage;
2438                 return false;
2439             }
2440 
2441             m_countCompareOK++;
2442         }
2443     }
2444     else if (readerror == GL_NO_ERROR)
2445     {
2446         m_testCtx.getLog() << tcu::TestLog::Message << (isCopy ? copyStage : "")
2447                            << "Invalid format used but glReadPixels succeeded for input = ["
2448                            << getFormatStr(m_inputFormat.format) << ", " << getTypeStr(m_inputType.type)
2449                            << "] output = [" << getFormatStr(outputFormat.format) << ", " << getTypeStr(outputType.type)
2450                            << "]" << tcu::TestLog::EndMessage;
2451         return false;
2452     }
2453 
2454     return true;
2455 }
2456 
readOutputData(const PixelFormat & outputFormat,const PixelType & outputType,int operation)2457 GLenum RectangleTest::readOutputData(const PixelFormat &outputFormat, const PixelType &outputType, int operation)
2458 {
2459     // If using PBOs buffer object for GL_PIXEL_PACK_BUFFER must
2460     // be bound and not allocated before calling when using PBOs
2461 
2462     glu::RenderContext &renderContext = m_context.getRenderContext();
2463     const Functions &gl               = renderContext.getFunctions();
2464 
2465     PackedPixelsBufferProperties &props = m_packProperties;
2466     props.elementSize                   = outputType.size;
2467     props.elementsInGroup               = outputType.special ? 1 : outputFormat.components;
2468     props.rowLength            = (props.rowLength == 0) ? (GRADIENT_WIDTH + props.skipPixels) : props.rowLength;
2469     props.elementsInRowNoAlign = props.elementsInGroup * props.rowLength;
2470     props.elementsInRow        = props.elementsInRowNoAlign;
2471 
2472     if (glu::isContextTypeES(renderContext.getType()))
2473     {
2474         props.rowCount   = 0;
2475         props.skipImages = 0;
2476     }
2477     else if ((operation == OUTPUT_READPIXELS) || (m_textureTarget == GL_TEXTURE_2D))
2478         props.skipImages = 0;
2479 
2480     if (props.rowCount == 0)
2481         props.rowCount = GRADIENT_HEIGHT + props.skipRows;
2482     props.imagesCount = props.skipImages + 1;
2483     if (props.elementSize < props.alignment)
2484     {
2485         props.elementsInRow = (int)(props.alignment * deFloatCeil(props.elementSize * props.elementsInGroup *
2486                                                                   props.rowLength / ((float)props.alignment))) /
2487                               props.elementSize;
2488     }
2489 
2490     int bufferSize =
2491         props.elementSize * props.elementsInRow * props.rowCount * props.imagesCount * (props.skipImages + 1);
2492 
2493     // The output buffer allocated should be initialized to a known value. After
2494     // a pack operation, any extra memory allocated for skipping should be
2495     // verified to be the original known value, untouched by the GL.
2496     const GLubyte defaultFillValue = 0xaa;
2497     m_outputBuffer.resize(static_cast<std::size_t>(bufferSize));
2498     std::fill(m_outputBuffer.begin(), m_outputBuffer.end(), defaultFillValue);
2499 
2500     GLuint packPBO;
2501     if (m_usePBO)
2502     {
2503         gl.genBuffers(1, &packPBO);
2504         gl.bindBuffer(GL_PIXEL_PACK_BUFFER, packPBO);
2505         gl.bufferData(GL_PIXEL_PACK_BUFFER, bufferSize, &m_outputBuffer[0], GL_STATIC_READ);
2506     }
2507 
2508     GLenum readError = GL_NO_ERROR;
2509     switch (operation)
2510     {
2511     case OUTPUT_GETTEXIMAGE:
2512         gl.getTexImage(m_textureTarget, 0, outputFormat.format, outputType.type, m_usePBO ? 0 : &m_outputBuffer[0]);
2513         break;
2514 
2515     case OUTPUT_READPIXELS:
2516         if (m_inputFormat.attachment != GL_DEPTH_ATTACHMENT &&
2517             m_inputFormat.attachment != GL_DEPTH_STENCIL_ATTACHMENT &&
2518             m_inputFormat.attachment != GL_STENCIL_ATTACHMENT)
2519             gl.readBuffer(m_inputFormat.attachment);
2520 
2521         readError = gl.getError();
2522         if (readError == GL_NO_ERROR)
2523             gl.readPixels(0, 0, GRADIENT_WIDTH, GRADIENT_HEIGHT, outputFormat.format, outputType.type,
2524                           m_usePBO ? 0 : &m_outputBuffer[0]);
2525         break;
2526     }
2527 
2528     if (readError == GL_NO_ERROR)
2529         readError = gl.getError();
2530 
2531     if (m_usePBO)
2532     {
2533         if (readError == GL_NO_ERROR)
2534         {
2535             GLvoid *mappedData = gl.mapBufferRange(GL_PIXEL_PACK_BUFFER, 0, bufferSize, GL_MAP_READ_BIT);
2536             if (!mappedData)
2537                 return GL_INVALID_INDEX;
2538 
2539             std::memcpy(&m_outputBuffer[0], mappedData, bufferSize);
2540             gl.unmapBuffer(GL_PIXEL_PACK_BUFFER);
2541         }
2542 
2543         gl.bindBuffer(GL_PIXEL_PACK_BUFFER, 0);
2544         gl.deleteBuffers(1, &packPBO);
2545     }
2546 
2547     if (m_packProperties.swapBytes && (readError == GL_NO_ERROR))
2548         swapBytes(outputType.size, m_outputBuffer);
2549 
2550     return readError;
2551 }
2552 
doCopy()2553 bool RectangleTest::doCopy()
2554 {
2555     bool result = true;
2556 
2557     const InternalFormat *copyInternalFormats;
2558     int internalFormatsCount;
2559     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
2560     {
2561         copyInternalFormats  = esInternalformats;
2562         internalFormatsCount = DE_LENGTH_OF_ARRAY(esInternalformats);
2563     }
2564     else
2565     {
2566         copyInternalFormats  = coreInternalformats;
2567         internalFormatsCount = DE_LENGTH_OF_ARRAY(coreInternalformats);
2568     }
2569 
2570     if ((m_inputFormat.format == m_internalFormat.format) && (m_inputType.type == m_internalFormat.type))
2571     {
2572         for (int i = 0; i < internalFormatsCount; ++i)
2573         {
2574             m_copyInternalFormat = copyInternalFormats[i];
2575             result &= doCopyInner();
2576         }
2577     }
2578 
2579     return result;
2580 }
2581 
doCopyInner()2582 bool RectangleTest::doCopyInner()
2583 {
2584     glu::RenderContext &renderContext = m_context.getRenderContext();
2585     const Functions &gl               = renderContext.getFunctions();
2586     bool result                       = true;
2587 
2588     const EnumFormats *copyFormatEnum =
2589         getCanonicalFormat(m_copyInternalFormat, m_copyInternalFormat.format, m_copyInternalFormat.type);
2590 
2591     if (copyFormatEnum != 0)
2592     {
2593         GLuint texture2;
2594         GLenum status;
2595 
2596         bool validcopy = isCopyValid(m_copyInternalFormat, m_internalFormat);
2597 
2598         gl.genTextures(1, &texture2);
2599         // Target is always GL_TEXTURE_2D
2600         gl.bindTexture(GL_TEXTURE_2D, texture2);
2601         GLenum error = gl.getError();
2602 
2603         // CopyTexImage to copy_internalformat (GL converts, but PixelStore is ignored)
2604         // Target is always GL_TEXTURE_2D
2605         gl.copyTexImage2D(GL_TEXTURE_2D, 0, m_copyInternalFormat.sizedFormat, 0, 0, GRADIENT_WIDTH, GRADIENT_HEIGHT, 0);
2606         error = gl.getError();
2607 
2608         // if this combination of copy_internalformat,internalformat is invalid
2609         if (validcopy == false)
2610         {
2611             // expect error and continue
2612             if (error != GL_NO_ERROR)
2613             {
2614                 // Invalid format used and glCopyTexImage2D failed
2615                 result = true;
2616             }
2617             else
2618             {
2619                 m_testCtx.getLog() << tcu::TestLog::Message << "Invalid format used but glCopyTexImage2D succeeded"
2620                                    << tcu::TestLog::EndMessage;
2621                 result = false;
2622             }
2623         }
2624         else // validcopy == true
2625         {
2626             // expect no error and continue
2627             if (error != GL_NO_ERROR)
2628             {
2629                 m_testCtx.getLog() << tcu::TestLog::Message << "Valid format used but glCopyTexImage2D failed"
2630                                    << tcu::TestLog::EndMessage;
2631                 result = false;
2632             }
2633             else
2634             {
2635                 if (!glu::isContextTypeES(renderContext.getType()))
2636                 {
2637                     // if GetTexImage is supported we call the
2638                     // inner function only as no loop needed
2639                     const PixelFormat &outputFormat = getPixelFormat(copyFormatEnum->format);
2640                     const PixelType &outputType     = getPixelType(copyFormatEnum->type);
2641                     result &= getTexImageInner(outputFormat, outputType);
2642                 }
2643 
2644                 GLint orginalFboId;
2645                 gl.getIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &orginalFboId);
2646                 GLU_EXPECT_NO_ERROR(gl.getError(), "getIntegerv");
2647 
2648                 GLuint fboId;
2649                 gl.genFramebuffers(1, &fboId);
2650                 gl.bindFramebuffer(GL_FRAMEBUFFER, fboId);
2651 
2652                 bool validImageAttach =
2653                     isFBOImageAttachValid(m_copyInternalFormat, copyFormatEnum->format, copyFormatEnum->type);
2654 
2655                 // attach copy_internalformat texture to FBO
2656                 // Target is always GL_TEXTURE_2D
2657                 const PixelFormat &copyFormat = getPixelFormat(copyFormatEnum->format);
2658                 gl.framebufferTexture2D(GL_FRAMEBUFFER, copyFormat.attachment, GL_TEXTURE_2D, texture2, 0);
2659                 GLU_EXPECT_NO_ERROR(gl.getError(), "glFramebufferTexture2D");
2660 
2661                 status = gl.checkFramebufferStatus(GL_FRAMEBUFFER);
2662                 GLU_EXPECT_NO_ERROR(gl.getError(), "glCheckFramebufferStatus");
2663 
2664                 // if an unsized sizedFormat was given, then implementation chosen copy format
2665                 // destination might be different than what's expected;
2666                 // we cannot continue checking since ReadPixels might not choose a compatible
2667                 // format, also the format may not be renderable as expected
2668                 if (isUnsizedFormat(m_copyInternalFormat.sizedFormat))
2669                 {
2670                     result &= true;
2671                 }
2672                 else if (status == GL_FRAMEBUFFER_COMPLETE)
2673                 {
2674                     if (validImageAttach)
2675                         result &= readPixels(true);
2676                     else
2677                     {
2678                         m_testCtx.getLog()
2679                             << tcu::TestLog::Message << "Copy FBO is complete but expected incomplete with sizedFormat "
2680                             << getFormatStr(m_copyInternalFormat.sizedFormat) << ", attachement "
2681                             << copyFormat.attachment << tcu::TestLog::EndMessage;
2682                         result = false;
2683                     }
2684                 }
2685                 else if (validImageAttach)
2686                 {
2687                     m_testCtx.getLog() << tcu::TestLog::Message
2688                                        << "Copy FBO is not complete but expected complete with sizedFormat "
2689                                        << getFormatStr(m_copyInternalFormat.sizedFormat) << ", attachement "
2690                                        << copyFormat.attachment << tcu::TestLog::EndMessage;
2691                     result = false;
2692                 }
2693 
2694                 // bind original FBO
2695                 gl.bindFramebuffer(GL_FRAMEBUFFER, orginalFboId);
2696                 gl.deleteFramebuffers(1, &fboId);
2697             }
2698         }
2699 
2700         gl.deleteTextures(1, &texture2);
2701     }
2702 
2703     return result;
2704 }
2705 
compare(GLvoid * gradient,GLvoid * data,const PixelFormat & outputFormat,const PixelType & outputType,bool isCopy) const2706 bool RectangleTest::compare(GLvoid *gradient, GLvoid *data, const PixelFormat &outputFormat,
2707                             const PixelType &outputType, bool isCopy) const
2708 {
2709     // Compares the reference gradient data to the output data
2710 
2711     int iformatSampler = m_internalFormat.sampler;
2712     int outputSampler  = getSampler(outputType, outputFormat);
2713     int inputSampler   = getSampler(m_inputType, m_inputFormat);
2714 
2715     if (isCopy)
2716         iformatSampler = m_copyInternalFormat.sampler;
2717 
2718     int samplerIsIntUintFloat = 3; // 1: INT | 2: UINT | 3: FLOAT/UNORM/NORM
2719     if (m_internalFormat.sampler == SAMPLER_INT)
2720         samplerIsIntUintFloat = 1;
2721     else if (m_internalFormat.sampler == SAMPLER_UINT)
2722         samplerIsIntUintFloat = 2;
2723 
2724     std::vector<GLubyte> gradientStrip;
2725     if (!stripBuffer(m_unpackProperties, static_cast<const GLubyte *>(gradient), gradientStrip, false))
2726         return false;
2727     std::vector<GLubyte> dataStrip;
2728     if (!stripBuffer(m_packProperties, static_cast<const GLubyte *>(data), dataStrip, true))
2729         return false;
2730 
2731     if (gradientStrip.empty() || dataStrip.empty())
2732         return false;
2733 
2734     std::vector<FloatPixel> inputBuffer;
2735     getFloatBuffer(&gradientStrip[0], samplerIsIntUintFloat, m_inputFormat, m_inputType,
2736                    GRADIENT_WIDTH * GRADIENT_HEIGHT, inputBuffer);
2737 
2738     std::vector<FloatPixel> outputBuffer;
2739     getFloatBuffer(&dataStrip[0], samplerIsIntUintFloat, outputFormat, outputType, GRADIENT_WIDTH * GRADIENT_HEIGHT,
2740                    outputBuffer);
2741 
2742     std::vector<int> inputBitTable;
2743     getBits(m_inputType, m_inputFormat, inputBitTable);
2744     std::vector<int> outputBitTable;
2745     getBits(outputType, outputFormat, outputBitTable);
2746     const int *internalformatBitTable = reinterpret_cast<const int *>(&m_internalFormat.bits);
2747     const int *copyFormatBitTable     = m_copyInternalFormat.bits.array;
2748 
2749     // make struct field iterable
2750     float *inputBufferFloat  = reinterpret_cast<float *>(&inputBuffer[0]);
2751     float *outputBufferFloat = reinterpret_cast<float *>(&outputBuffer[0]);
2752 
2753     int *inputBufferInt  = reinterpret_cast<int *>(&inputBuffer[0]);
2754     int *outputBufferInt = reinterpret_cast<int *>(&outputBuffer[0]);
2755 
2756     unsigned int *inputBufferUint  = reinterpret_cast<unsigned int *>(&inputBuffer[0]);
2757     unsigned int *outputBufferUint = reinterpret_cast<unsigned int *>(&outputBuffer[0]);
2758 
2759     for (int i = 0; i < GRADIENT_WIDTH * GRADIENT_HEIGHT; ++i)
2760     {
2761         for (int j = 0; j < NUM_FLOAT_PIXEL_COUNT / 3; ++j)
2762         {
2763             int bit1 = getRealBitPrecision(inputBitTable[j], inputSampler == SAMPLER_FLOAT);
2764             int bit2 = getRealBitPrecision(outputBitTable[j], outputSampler == SAMPLER_FLOAT);
2765             int bit3 = getRealBitPrecision(internalformatBitTable[j], iformatSampler == SAMPLER_FLOAT);
2766             int bitdiff;
2767 
2768             if (bit1 >= bit3)
2769             {
2770                 if ((inputSampler == SAMPLER_UNORM && m_internalFormat.sampler == SAMPLER_NORM))
2771                     bit3 -= 1;
2772             }
2773 
2774             if (bit2 <= bit3)
2775             {
2776                 if (outputSampler == SAMPLER_NORM && m_internalFormat.sampler == SAMPLER_UNORM)
2777                     bit2 -= 1;
2778             }
2779 
2780             if (!(m_internalFormat.flags & FLAG_REQ_RBO) && bit3 > 8 && m_internalFormat.sampler != SAMPLER_UINT &&
2781                 m_internalFormat.sampler != SAMPLER_INT)
2782             {
2783                 // If this internalFormat is not a required format there is no requirement
2784                 // that the implementation uses exactly the bit width requested. For example,
2785                 // it may substitute RGB10 for RGB12. The implementation can't make subtitutions
2786                 // for integer formats.
2787                 bit3 = 8;
2788             }
2789 
2790             bitdiff = std::min(std::min(bit1, bit2), bit3);
2791             if (isCopy)
2792             {
2793                 bitdiff = std::min(bitdiff, copyFormatBitTable[j]);
2794             }
2795 
2796             if (bitdiff > 0)
2797             {
2798                 if (samplerIsIntUintFloat == 1) // 1: INT
2799                 {
2800                     int inputValue  = inputBufferInt[NUM_FLOAT_PIXEL_COUNT * i + j];
2801                     int outputValue = outputBufferInt[NUM_FLOAT_PIXEL_COUNT * i + j];
2802 
2803                     if (inputSampler == SAMPLER_UINT)
2804                         // If input data was unsigned, it should be clamped to fit into
2805                         // internal format positive range (otherwise it may wrap and
2806                         // yield negative internalformat values)
2807                         inputValue = clampUnsignedValue(bit3 - 1, inputValue);
2808 
2809                     inputValue = clampSignedValue(bit3, inputValue);
2810                     if (isCopy)
2811                     {
2812                         inputValue = clampSignedValue(copyFormatBitTable[j], inputValue);
2813                     }
2814                     if (outputSampler == SAMPLER_UINT)
2815                         inputValue = clampUnsignedValue(bit2, inputValue);
2816                     else
2817                         inputValue = clampSignedValue(bit2, inputValue);
2818 
2819                     if (inputValue != outputValue)
2820                     {
2821                         m_testCtx.getLog() << tcu::TestLog::Message << "Integer comparison: " << i << ", " << j << ", "
2822                                            << NUM_FLOAT_PIXEL_COUNT * i + j << ": " << inputValue
2823                                            << " == " << outputValue << ": not equal." << tcu::TestLog::EndMessage;
2824                         return false;
2825                     }
2826                 }
2827                 else if (samplerIsIntUintFloat == 2) // 2: UINT
2828                 {
2829                     unsigned int inputValue  = inputBufferUint[NUM_FLOAT_PIXEL_COUNT * i + j + 6];
2830                     unsigned int outputValue = outputBufferUint[NUM_FLOAT_PIXEL_COUNT * i + j + 6];
2831 
2832                     inputValue = clampUnsignedValue(bit3, inputValue);
2833                     if (isCopy)
2834                         inputValue = clampUnsignedValue(copyFormatBitTable[j], inputValue);
2835 
2836                     if (outputSampler == SAMPLER_UINT)
2837                         inputValue = clampUnsignedValue(bit2, inputValue);
2838                     else if (outputSampler == SAMPLER_INT)
2839                         inputValue = clampUnsignedValue(bit2 - 1, inputValue);
2840                     if (inputValue != outputValue)
2841                     {
2842                         m_testCtx.getLog() << tcu::TestLog::Message << "Integer comparison: " << i << ", " << j << ", "
2843                                            << NUM_FLOAT_PIXEL_COUNT * i + j << ": " << inputValue
2844                                            << " == " << outputValue << ": not equal." << tcu::TestLog::EndMessage;
2845                         return false;
2846                     }
2847                 }
2848                 else if (samplerIsIntUintFloat == 3) // 3: FLOAT / UNORM / NORM
2849                 {
2850                     float inputValue  = inputBufferFloat[NUM_FLOAT_PIXEL_COUNT * i + j + 12];
2851                     float outputValue = outputBufferFloat[NUM_FLOAT_PIXEL_COUNT * i + j + 12];
2852                     float epsilon;
2853 
2854                     if ((outputSampler == SAMPLER_UNORM) || (outputSampler == SAMPLER_NORM))
2855                     {
2856                         // Check if format is UNORM/NORM which needs different
2857                         // precision since it implies a int->float conversion.
2858                         epsilon = 1.0f / ((float)((1 << (bitdiff - 1))) - 1);
2859                     }
2860                     else if ((inputSampler == SAMPLER_FLOAT) != (outputSampler == SAMPLER_FLOAT))
2861                     {
2862                         // Allow for rounding in either direction for float->int conversions.
2863                         epsilon = 1.0f / ((float)((1 << (bitdiff - 1))) - 1);
2864                     }
2865                     else
2866                         epsilon = 1.0f / ((float)((1 << bitdiff) - 1));
2867 
2868                     if (inputSampler == SAMPLER_FLOAT || outputSampler == SAMPLER_FLOAT)
2869                     {
2870                         // According to GL spec the precision requirement
2871                         // of floating-point values is 1 part in 10^5.
2872                         epsilon = deFloatMax(epsilon, 0.00001f);
2873                     }
2874 
2875                     if (m_internalFormat.flags & FLAG_COMPRESSED)
2876                         epsilon = deFloatMax(epsilon, 0.1f);
2877 
2878                     // Clamp input value to range of output
2879                     if (iformatSampler == SAMPLER_UNORM || outputSampler == SAMPLER_UNORM)
2880                         inputValue = deFloatMax(deFloatMin(inputValue, 1.0f), 0.0f);
2881                     else if (iformatSampler == SAMPLER_NORM || outputSampler == SAMPLER_NORM)
2882                         inputValue = deFloatMax(deFloatMin(inputValue, 1.0f), -1.0f);
2883 
2884                     // Compare the input and output
2885                     if (deFloatAbs(inputValue - outputValue) > epsilon)
2886                     {
2887                         m_testCtx.getLog()
2888                             << tcu::TestLog::Message << "Non-integer comparison: " << i << ", " << j << ", "
2889                             << NUM_FLOAT_PIXEL_COUNT * i + j << ", " << epsilon << ": " << inputValue
2890                             << " == " << outputValue << ": not equal." << tcu::TestLog::EndMessage;
2891                         return false;
2892                     }
2893                 }
2894                 else
2895                 {
2896                     m_testCtx.getLog() << tcu::TestLog::Message << "Sampler type cannot be recognised, so no comparison"
2897                                        << tcu::TestLog::EndMessage;
2898                     return false;
2899                 }
2900             }
2901         }
2902     }
2903 
2904     return true;
2905 }
2906 
getFloatBuffer(GLvoid * gradient,int samplerIsIntUintFloat,const PixelFormat & format,const PixelType & type,int elementCount,std::vector<FloatPixel> & result) const2907 void RectangleTest::getFloatBuffer(GLvoid *gradient, int samplerIsIntUintFloat, const PixelFormat &format,
2908                                    const PixelType &type, int elementCount, std::vector<FloatPixel> &result) const
2909 {
2910     int componentCount = format.components;
2911     switch (type.type)
2912     {
2913     case GL_UNSIGNED_BYTE:
2914         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_UNSIGNED_BYTE, result);
2915         break;
2916     case GL_BYTE:
2917         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_BYTE, result);
2918         break;
2919     case GL_UNSIGNED_SHORT:
2920         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_UNSIGNED_SHORT, result);
2921         break;
2922     case GL_SHORT:
2923         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_SHORT, result);
2924         break;
2925     case GL_UNSIGNED_INT:
2926         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_UNSIGNED_INT, result);
2927         break;
2928     case GL_INT:
2929         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_INT, result);
2930         break;
2931     case GL_HALF_FLOAT:
2932         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_HALF_FLOAT, result);
2933         break;
2934     case GL_FLOAT:
2935         makeBuffer(gradient, format, samplerIsIntUintFloat, elementCount, componentCount, pack_FLOAT, result);
2936         break;
2937     case GL_UNSIGNED_SHORT_5_6_5:
2938         if (samplerIsIntUintFloat == 1)
2939             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5_INT, result);
2940         else if (samplerIsIntUintFloat == 2)
2941             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5_UINT, result);
2942         else if (samplerIsIntUintFloat == 3)
2943             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5, result);
2944         break;
2945     case GL_UNSIGNED_SHORT_4_4_4_4:
2946         if (samplerIsIntUintFloat == 1)
2947             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4_INT, result);
2948         else if (samplerIsIntUintFloat == 2)
2949             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4_UINT, result);
2950         else if (samplerIsIntUintFloat == 3)
2951             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4, result);
2952         break;
2953     case GL_UNSIGNED_SHORT_5_5_5_1:
2954         if (samplerIsIntUintFloat == 1)
2955             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_5_5_1_INT, result);
2956         else if (samplerIsIntUintFloat == 2)
2957             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_5_5_1_UINT, result);
2958         else if (samplerIsIntUintFloat == 3)
2959             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_5_5_1, result);
2960         break;
2961     case GL_UNSIGNED_INT_2_10_10_10_REV:
2962         if (samplerIsIntUintFloat == 1)
2963             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_INT_2_10_10_10_REV_INT, result);
2964         else if (samplerIsIntUintFloat == 2)
2965             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_INT_2_10_10_10_REV_UINT, result);
2966         else if (samplerIsIntUintFloat == 3)
2967             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_2_10_10_10_REV, result);
2968         break;
2969     case GL_UNSIGNED_INT_24_8:
2970         makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_24_8, result);
2971         break;
2972     case GL_UNSIGNED_INT_10F_11F_11F_REV:
2973         makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_10F_11F_11F_REV, result);
2974         break;
2975     case GL_UNSIGNED_INT_5_9_9_9_REV:
2976         makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_5_9_9_9_REV, result);
2977         break;
2978     case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
2979         makeBufferPackedFloat(gradient, format, elementCount, pack_FLOAT_32_UNSIGNED_INT_24_8_REV, result);
2980         break;
2981     case GL_UNSIGNED_BYTE_3_3_2:
2982         if (samplerIsIntUintFloat == 1)
2983             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_BYTE_3_3_2_INT, result);
2984         else if (samplerIsIntUintFloat == 2)
2985             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_BYTE_3_3_2_UINT, result);
2986         else if (samplerIsIntUintFloat == 3)
2987             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_BYTE_3_3_2, result);
2988         break;
2989     case GL_UNSIGNED_BYTE_2_3_3_REV:
2990         if (samplerIsIntUintFloat == 1)
2991             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_BYTE_2_3_3_REV_INT, result);
2992         else if (samplerIsIntUintFloat == 2)
2993             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_BYTE_2_3_3_REV_UINT, result);
2994         else if (samplerIsIntUintFloat == 3)
2995             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_BYTE_2_3_3_REV, result);
2996         break;
2997     case GL_UNSIGNED_SHORT_5_6_5_REV:
2998         if (samplerIsIntUintFloat == 1)
2999             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5_REV_INT, result);
3000         else if (samplerIsIntUintFloat == 2)
3001             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5_REV_UINT, result);
3002         else if (samplerIsIntUintFloat == 3)
3003             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_5_6_5_REV, result);
3004         break;
3005     case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3006         if (samplerIsIntUintFloat == 1)
3007             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4_REV_INT, result);
3008         else if (samplerIsIntUintFloat == 2)
3009             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4_REV_UINT, result);
3010         else if (samplerIsIntUintFloat == 3)
3011             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_4_4_4_4_REV, result);
3012         break;
3013     case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3014         if (samplerIsIntUintFloat == 1)
3015             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_SHORT_1_5_5_5_REV_INT, result);
3016         else if (samplerIsIntUintFloat == 2)
3017             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_SHORT_1_5_5_5_REV_UINT, result);
3018         else if (samplerIsIntUintFloat == 3)
3019             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_SHORT_1_5_5_5_REV, result);
3020         break;
3021     case GL_UNSIGNED_INT_8_8_8_8:
3022         if (samplerIsIntUintFloat == 1)
3023             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8_INT, result);
3024         else if (samplerIsIntUintFloat == 2)
3025             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8_UINT, result);
3026         else if (samplerIsIntUintFloat == 3)
3027             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8, result);
3028         break;
3029     case GL_UNSIGNED_INT_8_8_8_8_REV:
3030         if (samplerIsIntUintFloat == 1)
3031             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8_REV_INT, result);
3032         else if (samplerIsIntUintFloat == 2)
3033             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8_REV_UINT, result);
3034         else if (samplerIsIntUintFloat == 3)
3035             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_8_8_8_8_REV, result);
3036         break;
3037     case GL_UNSIGNED_INT_10_10_10_2:
3038         if (samplerIsIntUintFloat == 1)
3039             makeBufferPackedInt(gradient, format, elementCount, pack_UNSIGNED_INT_10_10_10_2_INT, result);
3040         else if (samplerIsIntUintFloat == 2)
3041             makeBufferPackedUint(gradient, format, elementCount, pack_UNSIGNED_INT_10_10_10_2_UINT, result);
3042         else if (samplerIsIntUintFloat == 3)
3043             makeBufferPackedFloat(gradient, format, elementCount, pack_UNSIGNED_INT_10_10_10_2, result);
3044         break;
3045     default:
3046         TCU_FAIL("Unsuported type");
3047     }
3048 }
3049 
3050 template <typename Type>
makeBuffer(const GLvoid * gradient,const PixelFormat & format,int samplerIsIntUintFloat,int elementCount,int componentCount,float (* pack)(Type),std::vector<FloatPixel> & result) const3051 void RectangleTest::makeBuffer(const GLvoid *gradient, const PixelFormat &format, int samplerIsIntUintFloat,
3052                                int elementCount, int componentCount, float (*pack)(Type),
3053                                std::vector<FloatPixel> &result) const
3054 {
3055     const Type *sourceData = static_cast<const Type *>(gradient);
3056     result.resize(sizeof(FloatPixel) * elementCount);
3057     for (int i = 0; i < elementCount; ++i)
3058     {
3059         rawFloatPixel values;
3060         rawIntPixel valuesInt;
3061         rawUintPixel valuesUint;
3062         for (int j = 0; j < componentCount; j++)
3063         {
3064             if (samplerIsIntUintFloat == 1)
3065                 valuesInt[j] = (int)static_cast<Type>(sourceData[componentCount * i + j]);
3066             else if (samplerIsIntUintFloat == 2)
3067                 valuesUint[j] = (unsigned int)static_cast<Type>(sourceData[componentCount * i + j]);
3068             else if (samplerIsIntUintFloat == 3)
3069                 values[j] = pack(sourceData[componentCount * i + j]);
3070         }
3071         if (samplerIsIntUintFloat == 1)
3072             result[i] = orderComponentsInt(valuesInt, format);
3073         else if (samplerIsIntUintFloat == 2)
3074             result[i] = orderComponentsUint(valuesUint, format);
3075         else if (samplerIsIntUintFloat == 3)
3076             result[i] = orderComponentsFloat(values, format);
3077     }
3078 }
3079 
getBits(const PixelType & type,const PixelFormat & format,std::vector<int> & resultTable) const3080 void RectangleTest::getBits(const PixelType &type, const PixelFormat &format, std::vector<int> &resultTable) const
3081 {
3082     // return bit depth table based on type and format pair;
3083     // table is always NUM_FLOAT_PIXEL_COUNT digit long
3084 
3085     resultTable.resize(NUM_FLOAT_PIXEL_COUNT);
3086     std::fill(resultTable.begin(), resultTable.end(), 0);
3087 
3088     if (type.special == true)
3089     {
3090         std::memcpy(&resultTable[0], &type.bits, sizeof(type.bits));
3091         if (type.type == GL_UNSIGNED_INT_5_9_9_9_REV)
3092         {
3093             //this type is another special case: it is always converted to 3-channel color (no A).
3094             //as results of this function are used for comparison of converted values we set A bits to 0.
3095             resultTable[3] = 0;
3096         }
3097     }
3098     else
3099     {
3100         int bits;
3101 
3102         if (type.type == GL_FLOAT)
3103             bits = 32;
3104         else if (type.type == GL_HALF_FLOAT)
3105             bits = 16;
3106         else
3107             bits = type.size << 3;
3108 
3109         if (format.format == GL_DEPTH_STENCIL || format.format == GL_DEPTH_COMPONENT)
3110             resultTable[4] = bits;
3111 
3112         if (format.format == GL_DEPTH_STENCIL || format.format == GL_STENCIL_INDEX ||
3113             format.format == GL_STENCIL_INDEX8)
3114         {
3115             resultTable[5] = bits;
3116         }
3117 
3118         if (format.format == GL_RED || format.format == GL_RG || format.format == GL_RGB || format.format == GL_RGBA ||
3119             format.format == GL_BGR || format.format == GL_BGRA || format.format == GL_RED_INTEGER ||
3120             format.format == GL_RG_INTEGER || format.format == GL_RGB_INTEGER || format.format == GL_RGBA_INTEGER ||
3121             format.format == GL_BGR_INTEGER || format.format == GL_BGRA_INTEGER)
3122         {
3123             resultTable[0] = bits;
3124         }
3125 
3126         if (format.format == GL_RG || format.format == GL_RGB || format.format == GL_RGBA || format.format == GL_BGR ||
3127             format.format == GL_BGRA || format.format == GL_RG_INTEGER || format.format == GL_RGB_INTEGER ||
3128             format.format == GL_RGBA_INTEGER || format.format == GL_BGR_INTEGER || format.format == GL_BGRA_INTEGER)
3129         {
3130             resultTable[1] = bits;
3131         }
3132 
3133         if (format.format == GL_RGB || format.format == GL_RGBA || format.format == GL_BGR ||
3134             format.format == GL_BGRA || format.format == GL_RGB_INTEGER || format.format == GL_RGBA_INTEGER ||
3135             format.format == GL_BGR_INTEGER || format.format == GL_BGRA_INTEGER)
3136         {
3137             resultTable[2] = bits;
3138         }
3139 
3140         if (format.format == GL_RGBA || format.format == GL_BGRA || format.format == GL_RGBA_INTEGER ||
3141             format.format == GL_BGRA_INTEGER)
3142         {
3143             resultTable[3] = bits;
3144         }
3145     }
3146 }
3147 
3148 template <typename Type>
makeBufferPackedInt(const GLvoid * gradient,const PixelFormat & format,int elementCount,void (* pack)(rawIntPixel *,Type),std::vector<FloatPixel> & result) const3149 void RectangleTest::makeBufferPackedInt(const GLvoid *gradient, const PixelFormat &format, int elementCount,
3150                                         void (*pack)(rawIntPixel *, Type), std::vector<FloatPixel> &result) const
3151 {
3152     const Type *sourceData = static_cast<const Type *>(gradient);
3153     result.resize(sizeof(FloatPixel) * elementCount);
3154     for (int i = 0; i < elementCount; ++i)
3155     {
3156         rawIntPixel values;
3157         pack(&values, sourceData[i]);
3158         result[i] = orderComponentsInt(values, format);
3159     }
3160 }
3161 
3162 template <typename Type>
makeBufferPackedUint(const GLvoid * gradient,const PixelFormat & format,int elementCount,void (* pack)(rawUintPixel *,Type),std::vector<FloatPixel> & result) const3163 void RectangleTest::makeBufferPackedUint(const GLvoid *gradient, const PixelFormat &format, int elementCount,
3164                                          void (*pack)(rawUintPixel *, Type), std::vector<FloatPixel> &result) const
3165 {
3166     const Type *sourceData = static_cast<const Type *>(gradient);
3167     result.resize(sizeof(FloatPixel) * elementCount);
3168     for (int i = 0; i < elementCount; ++i)
3169     {
3170         rawUintPixel values;
3171         pack(&values, sourceData[i]);
3172         result[i] = orderComponentsUint(values, format);
3173     }
3174 }
3175 
3176 template <typename Type>
makeBufferPackedFloat(const GLvoid * gradient,const PixelFormat & format,int elementCount,void (* pack)(rawFloatPixel *,Type),std::vector<FloatPixel> & result) const3177 void RectangleTest::makeBufferPackedFloat(const GLvoid *gradient, const PixelFormat &format, int elementCount,
3178                                           void (*pack)(rawFloatPixel *, Type), std::vector<FloatPixel> &result) const
3179 {
3180     const Type *sourceData = static_cast<const Type *>(gradient);
3181     result.resize(sizeof(FloatPixel) * elementCount);
3182     for (int i = 0; i < elementCount; ++i)
3183     {
3184         rawFloatPixel values;
3185         pack(&values, sourceData[i]);
3186         result[i] = orderComponentsFloat(values, format);
3187     }
3188 }
3189 
orderComponentsInt(rawIntPixel values,const PixelFormat & format) const3190 FloatPixel RectangleTest::orderComponentsInt(rawIntPixel values, const PixelFormat &format) const
3191 {
3192     FloatPixel fp = {PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,
3193                      PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI,
3194                      PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF};
3195 
3196     if (format.componentOrder.bits.red >= 0)
3197         fp.i_r = values[format.componentOrder.bits.red];
3198     if (format.componentOrder.bits.green >= 0)
3199         fp.i_g = values[format.componentOrder.bits.green];
3200     if (format.componentOrder.bits.blue >= 0)
3201         fp.i_b = values[format.componentOrder.bits.blue];
3202     if (format.componentOrder.bits.alpha >= 0)
3203         fp.i_a = values[format.componentOrder.bits.alpha];
3204     if (format.componentOrder.bits.depth >= 0)
3205         fp.i_d = values[format.componentOrder.bits.depth];
3206     if (format.componentOrder.bits.stencil >= 0)
3207         fp.i_s = values[format.componentOrder.bits.stencil];
3208 
3209     return fp;
3210 }
3211 
orderComponentsUint(rawUintPixel values,const PixelFormat & format) const3212 FloatPixel RectangleTest::orderComponentsUint(rawUintPixel values, const PixelFormat &format) const
3213 {
3214     FloatPixel fp = {PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,
3215                      PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI,
3216                      PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF};
3217 
3218     if (format.componentOrder.bits.red >= 0)
3219         fp.ui_r = values[format.componentOrder.bits.red];
3220     if (format.componentOrder.bits.green >= 0)
3221         fp.ui_g = values[format.componentOrder.bits.green];
3222     if (format.componentOrder.bits.blue >= 0)
3223         fp.ui_b = values[format.componentOrder.bits.blue];
3224     if (format.componentOrder.bits.alpha >= 0)
3225         fp.ui_a = values[format.componentOrder.bits.alpha];
3226     if (format.componentOrder.bits.depth >= 0)
3227         fp.ui_d = values[format.componentOrder.bits.depth];
3228     if (format.componentOrder.bits.stencil >= 0)
3229         fp.ui_s = values[format.componentOrder.bits.stencil];
3230 
3231     return fp;
3232 }
3233 
orderComponentsFloat(rawFloatPixel values,const PixelFormat & format) const3234 FloatPixel RectangleTest::orderComponentsFloat(rawFloatPixel values, const PixelFormat &format) const
3235 {
3236     FloatPixel fp = {PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,  PACK_DEFAULTI,
3237                      PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI, PACK_DEFAULTUI,
3238                      PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF,  PACK_DEFAULTF};
3239 
3240     if (format.componentOrder.bits.red >= 0)
3241         fp.r = values[format.componentOrder.bits.red];
3242     if (format.componentOrder.bits.green >= 0)
3243         fp.g = values[format.componentOrder.bits.green];
3244     if (format.componentOrder.bits.blue >= 0)
3245         fp.b = values[format.componentOrder.bits.blue];
3246     if (format.componentOrder.bits.alpha >= 0)
3247         fp.a = values[format.componentOrder.bits.alpha];
3248     if (format.componentOrder.bits.depth >= 0)
3249         fp.d = values[format.componentOrder.bits.depth];
3250     if (format.componentOrder.bits.stencil >= 0)
3251         fp.s = values[format.componentOrder.bits.stencil];
3252 
3253     return fp;
3254 }
3255 
getRealBitPrecision(int bits,bool isFloat) const3256 unsigned int RectangleTest::getRealBitPrecision(int bits, bool isFloat) const
3257 {
3258     if (!isFloat)
3259         return bits;
3260     switch (bits)
3261     {
3262     case 32:
3263         return 23;
3264     case 16:
3265         return 10;
3266     case 11:
3267         return 6;
3268     case 10:
3269         return 5;
3270     }
3271     return bits;
3272 }
3273 
stripBuffer(const PackedPixelsBufferProperties & props,const GLubyte * orginalBuffer,std::vector<GLubyte> & newBuffer,bool validate) const3274 bool RectangleTest::stripBuffer(const PackedPixelsBufferProperties &props, const GLubyte *orginalBuffer,
3275                                 std::vector<GLubyte> &newBuffer, bool validate) const
3276 {
3277     // Extracts pixel data from a buffer with specific
3278     // pixel store configuration into a flat buffer
3279 
3280     int newBufferSize = props.elementSize * props.elementsInRowNoAlign * GRADIENT_HEIGHT;
3281     if (!newBufferSize)
3282         return false;
3283     newBuffer.resize(newBufferSize);
3284 
3285     int skipBottom = ((props.skipImages * props.rowCount + props.skipRows) * props.elementsInRow) * props.elementSize;
3286     int skipTop    = (props.rowCount - GRADIENT_HEIGHT - props.skipRows) * props.elementsInRow * props.elementSize;
3287     int skipLeft   = props.skipPixels * props.elementsInGroup * props.elementSize;
3288     int skipRight  = (props.elementsInRow - GRADIENT_WIDTH * props.elementsInGroup) * props.elementSize - skipLeft;
3289     int copy       = GRADIENT_WIDTH * props.elementsInGroup * props.elementSize;
3290     int skipAlign  = (props.elementsInRow - props.elementsInRowNoAlign) * props.elementSize;
3291 
3292     if (validate)
3293     {
3294         for (int i = 0; i < skipBottom; i++)
3295         {
3296             if (orginalBuffer[i] != m_defaultFillValue)
3297                 return false;
3298         }
3299     }
3300 
3301     int index_src = skipBottom;
3302     int index_dst = 0;
3303 
3304     for (int j = 0; j < GRADIENT_HEIGHT; j++)
3305     {
3306         if (validate)
3307         {
3308             for (int i = 0; i < skipLeft; i++)
3309             {
3310                 if (orginalBuffer[index_src + i] != m_defaultFillValue)
3311                     return false;
3312             }
3313         }
3314         index_src += skipLeft;
3315 
3316         std::memcpy(&newBuffer[0] + index_dst, &orginalBuffer[0] + index_src, copy);
3317         index_src += copy;
3318         index_dst += copy;
3319 
3320         if (validate)
3321         {
3322             for (int i = skipAlign; i < skipRight; i++)
3323             {
3324                 if (orginalBuffer[index_src + i] != m_defaultFillValue)
3325                     return false;
3326             }
3327         }
3328         index_src += skipRight;
3329     }
3330 
3331     if (validate)
3332     {
3333         for (int i = 0; i < skipTop; i++)
3334         {
3335             if (orginalBuffer[index_src + i] != m_defaultFillValue)
3336                 return false;
3337         }
3338     }
3339     index_src += skipTop;
3340 
3341     return true;
3342 }
3343 
clampSignedValue(int bits,int value) const3344 int RectangleTest::clampSignedValue(int bits, int value) const
3345 {
3346     int max = 2147483647;
3347     int min = 0x80000000;
3348 
3349     if (bits < 32)
3350     {
3351         max = (1 << (bits - 1)) - 1;
3352         min = (1 << (bits - 1)) - (1 << bits);
3353     }
3354 
3355     if (value >= max)
3356         return max;
3357     else if (value <= min)
3358         return min;
3359 
3360     return value;
3361 }
3362 
clampUnsignedValue(int bits,unsigned int value) const3363 unsigned int RectangleTest::clampUnsignedValue(int bits, unsigned int value) const
3364 {
3365     unsigned int max = 4294967295u;
3366     unsigned int min = 0;
3367 
3368     if (bits < 32)
3369     {
3370         max = (1 << bits) - 1;
3371     }
3372 
3373     if (value >= max)
3374         return max;
3375     else if (value <= min)
3376         return min;
3377 
3378     return value;
3379 }
3380 
pack_UNSIGNED_BYTE(GLubyte value)3381 float RectangleTest::pack_UNSIGNED_BYTE(GLubyte value)
3382 {
3383     return static_cast<GLfloat>(value) / std::numeric_limits<GLubyte>::max();
3384 }
3385 
pack_BYTE(GLbyte value)3386 float RectangleTest::pack_BYTE(GLbyte value)
3387 {
3388     return deFloatMax(static_cast<GLfloat>(value) / std::numeric_limits<GLbyte>::max(), -1.0f);
3389 }
3390 
pack_UNSIGNED_SHORT(GLushort value)3391 float RectangleTest::pack_UNSIGNED_SHORT(GLushort value)
3392 {
3393     return static_cast<GLfloat>(value) / std::numeric_limits<GLushort>::max();
3394 }
3395 
pack_SHORT(GLshort value)3396 float RectangleTest::pack_SHORT(GLshort value)
3397 {
3398     return deFloatMax(static_cast<GLfloat>(value) / std::numeric_limits<GLshort>::max(), -1.0f);
3399 }
3400 
pack_UNSIGNED_INT(GLuint value)3401 float RectangleTest::pack_UNSIGNED_INT(GLuint value)
3402 {
3403     return static_cast<GLfloat>(value) / std::numeric_limits<GLuint>::max();
3404 }
3405 
pack_INT(GLint value)3406 float RectangleTest::pack_INT(GLint value)
3407 {
3408     return deFloatMax(static_cast<GLfloat>(value) / std::numeric_limits<GLint>::max(), -1.0f);
3409 }
3410 
pack_HALF_FLOAT(GLhalf value)3411 float RectangleTest::pack_HALF_FLOAT(GLhalf value)
3412 {
3413     return halfFloatToFloat(value);
3414 }
3415 
pack_FLOAT(GLfloat value)3416 float RectangleTest::pack_FLOAT(GLfloat value)
3417 {
3418     return value;
3419 }
3420 
pack_UNSIGNED_BYTE_3_3_2(rawFloatPixel * values,GLubyte value)3421 void RectangleTest::pack_UNSIGNED_BYTE_3_3_2(rawFloatPixel *values, GLubyte value)
3422 {
3423     (*values)[0] = ((value >> 5) & 7) / 7.0f;
3424     (*values)[1] = ((value >> 2) & 7) / 7.0f;
3425     (*values)[2] = ((value >> 0) & 3) / 3.0f;
3426 }
3427 
pack_UNSIGNED_BYTE_3_3_2_UINT(rawUintPixel * values,GLubyte value)3428 void RectangleTest::pack_UNSIGNED_BYTE_3_3_2_UINT(rawUintPixel *values, GLubyte value)
3429 {
3430     (*values)[0] = (value >> 5) & 7;
3431     (*values)[1] = (value >> 2) & 7;
3432     (*values)[2] = (value >> 0) & 3;
3433 }
3434 
pack_UNSIGNED_BYTE_3_3_2_INT(rawIntPixel * values,GLubyte value)3435 void RectangleTest::pack_UNSIGNED_BYTE_3_3_2_INT(rawIntPixel *values, GLubyte value)
3436 {
3437     (*values)[0] = (static_cast<GLbyte>(value) >> 5) & 7;
3438     (*values)[1] = (static_cast<GLbyte>(value) >> 2) & 7;
3439     (*values)[2] = (static_cast<GLbyte>(value) >> 0) & 3;
3440 }
3441 
pack_UNSIGNED_BYTE_2_3_3_REV(rawFloatPixel * values,GLubyte value)3442 void RectangleTest::pack_UNSIGNED_BYTE_2_3_3_REV(rawFloatPixel *values, GLubyte value)
3443 {
3444     (*values)[2] = ((value >> 6) & 3) / 3.0f;
3445     (*values)[1] = ((value >> 3) & 7) / 7.0f;
3446     (*values)[0] = ((value >> 0) & 7) / 7.0f;
3447 }
3448 
pack_UNSIGNED_BYTE_2_3_3_REV_UINT(rawUintPixel * values,GLubyte value)3449 void RectangleTest::pack_UNSIGNED_BYTE_2_3_3_REV_UINT(rawUintPixel *values, GLubyte value)
3450 {
3451     (*values)[2] = (value >> 6) & 3;
3452     (*values)[1] = (value >> 3) & 7;
3453     (*values)[0] = (value >> 0) & 7;
3454 }
3455 
pack_UNSIGNED_BYTE_2_3_3_REV_INT(rawIntPixel * values,GLubyte value)3456 void RectangleTest::pack_UNSIGNED_BYTE_2_3_3_REV_INT(rawIntPixel *values, GLubyte value)
3457 {
3458     (*values)[2] = (static_cast<GLbyte>(value) >> 6) & 3;
3459     (*values)[1] = (static_cast<GLbyte>(value) >> 3) & 7;
3460     (*values)[0] = (static_cast<GLbyte>(value) >> 0) & 7;
3461 }
3462 
pack_UNSIGNED_SHORT_5_6_5(rawFloatPixel * values,GLushort value)3463 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5(rawFloatPixel *values, GLushort value)
3464 {
3465     (*values)[0] = ((value >> 11) & 31) / 31.0f;
3466     (*values)[1] = ((value >> 5) & 63) / 63.0f;
3467     (*values)[2] = ((value >> 0) & 31) / 31.0f;
3468 }
3469 
pack_UNSIGNED_SHORT_5_6_5_UINT(rawUintPixel * values,GLushort value)3470 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5_UINT(rawUintPixel *values, GLushort value)
3471 {
3472     (*values)[0] = (value >> 11) & 31;
3473     (*values)[1] = (value >> 5) & 63;
3474     (*values)[2] = (value >> 0) & 31;
3475 }
3476 
pack_UNSIGNED_SHORT_5_6_5_INT(rawIntPixel * values,GLushort value)3477 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5_INT(rawIntPixel *values, GLushort value)
3478 {
3479     (*values)[0] = (static_cast<GLshort>(value) >> 11) & 31;
3480     (*values)[1] = (static_cast<GLshort>(value) >> 5) & 63;
3481     (*values)[2] = (static_cast<GLshort>(value) >> 0) & 31;
3482 }
3483 
pack_UNSIGNED_SHORT_5_6_5_REV(rawFloatPixel * values,GLushort value)3484 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5_REV(rawFloatPixel *values, GLushort value)
3485 {
3486     (*values)[2] = ((value >> 11) & 31) / 31.0f;
3487     (*values)[1] = ((value >> 5) & 63) / 63.0f;
3488     (*values)[0] = ((value >> 0) & 31) / 31.0f;
3489 }
3490 
pack_UNSIGNED_SHORT_5_6_5_REV_UINT(rawUintPixel * values,GLushort value)3491 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5_REV_UINT(rawUintPixel *values, GLushort value)
3492 {
3493     (*values)[2] = (value >> 11) & 31;
3494     (*values)[1] = (value >> 5) & 63;
3495     (*values)[0] = (value >> 0) & 31;
3496 }
3497 
pack_UNSIGNED_SHORT_5_6_5_REV_INT(rawIntPixel * values,GLushort value)3498 void RectangleTest::pack_UNSIGNED_SHORT_5_6_5_REV_INT(rawIntPixel *values, GLushort value)
3499 {
3500     (*values)[2] = (static_cast<GLshort>(value) >> 11) & 31;
3501     (*values)[1] = (static_cast<GLshort>(value) >> 5) & 63;
3502     (*values)[0] = (static_cast<GLshort>(value) >> 0) & 31;
3503 }
3504 
pack_UNSIGNED_SHORT_4_4_4_4(rawFloatPixel * values,GLushort value)3505 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4(rawFloatPixel *values, GLushort value)
3506 {
3507     (*values)[0] = ((value >> 12) & 15) / 15.0f;
3508     (*values)[1] = ((value >> 8) & 15) / 15.0f;
3509     (*values)[2] = ((value >> 4) & 15) / 15.0f;
3510     (*values)[3] = ((value >> 0) & 15) / 15.0f;
3511 }
3512 
pack_UNSIGNED_SHORT_4_4_4_4_UINT(rawUintPixel * values,GLushort value)3513 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4_UINT(rawUintPixel *values, GLushort value)
3514 {
3515     (*values)[0] = (value >> 12) & 15;
3516     (*values)[1] = (value >> 8) & 15;
3517     (*values)[2] = (value >> 4) & 15;
3518     (*values)[3] = (value >> 0) & 15;
3519 }
3520 
pack_UNSIGNED_SHORT_4_4_4_4_INT(rawIntPixel * values,GLushort value)3521 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4_INT(rawIntPixel *values, GLushort value)
3522 {
3523     (*values)[0] = (static_cast<GLshort>(value) >> 12) & 15;
3524     (*values)[1] = (static_cast<GLshort>(value) >> 8) & 15;
3525     (*values)[2] = (static_cast<GLshort>(value) >> 4) & 15;
3526     (*values)[3] = (static_cast<GLshort>(value) >> 0) & 15;
3527 }
3528 
pack_UNSIGNED_SHORT_4_4_4_4_REV(rawFloatPixel * values,GLushort value)3529 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4_REV(rawFloatPixel *values, GLushort value)
3530 {
3531     (*values)[3] = ((value >> 12) & 15) / 15.0f;
3532     (*values)[2] = ((value >> 8) & 15) / 15.0f;
3533     (*values)[1] = ((value >> 4) & 15) / 15.0f;
3534     (*values)[0] = ((value >> 0) & 15) / 15.0f;
3535 }
3536 
pack_UNSIGNED_SHORT_4_4_4_4_REV_UINT(rawUintPixel * values,GLushort value)3537 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4_REV_UINT(rawUintPixel *values, GLushort value)
3538 {
3539     (*values)[3] = (value >> 12) & 15;
3540     (*values)[2] = (value >> 8) & 15;
3541     (*values)[1] = (value >> 4) & 15;
3542     (*values)[0] = (value >> 0) & 15;
3543 }
3544 
pack_UNSIGNED_SHORT_4_4_4_4_REV_INT(rawIntPixel * values,GLushort value)3545 void RectangleTest::pack_UNSIGNED_SHORT_4_4_4_4_REV_INT(rawIntPixel *values, GLushort value)
3546 {
3547     (*values)[3] = (static_cast<GLshort>(value) >> 12) & 15;
3548     (*values)[2] = (static_cast<GLshort>(value) >> 8) & 15;
3549     (*values)[1] = (static_cast<GLshort>(value) >> 4) & 15;
3550     (*values)[0] = (static_cast<GLshort>(value) >> 0) & 15;
3551 }
3552 
pack_UNSIGNED_SHORT_5_5_5_1(rawFloatPixel * values,GLushort value)3553 void RectangleTest::pack_UNSIGNED_SHORT_5_5_5_1(rawFloatPixel *values, GLushort value)
3554 {
3555     (*values)[0] = ((value >> 11) & 31) / 31.0f;
3556     (*values)[1] = ((value >> 6) & 31) / 31.0f;
3557     (*values)[2] = ((value >> 1) & 31) / 31.0f;
3558     (*values)[3] = ((value >> 0) & 1) / 1.0f;
3559 }
3560 
pack_UNSIGNED_SHORT_5_5_5_1_UINT(rawUintPixel * values,GLushort value)3561 void RectangleTest::pack_UNSIGNED_SHORT_5_5_5_1_UINT(rawUintPixel *values, GLushort value)
3562 {
3563     (*values)[0] = (value >> 11) & 31;
3564     (*values)[1] = (value >> 6) & 31;
3565     (*values)[2] = (value >> 1) & 31;
3566     (*values)[3] = (value >> 0) & 1;
3567 }
3568 
pack_UNSIGNED_SHORT_5_5_5_1_INT(rawIntPixel * values,GLushort value)3569 void RectangleTest::pack_UNSIGNED_SHORT_5_5_5_1_INT(rawIntPixel *values, GLushort value)
3570 {
3571     (*values)[0] = (static_cast<GLshort>(value) >> 11) & 31;
3572     (*values)[1] = (static_cast<GLshort>(value) >> 6) & 31;
3573     (*values)[2] = (static_cast<GLshort>(value) >> 1) & 31;
3574     (*values)[3] = (static_cast<GLshort>(value) >> 0) & 1;
3575 }
3576 
pack_UNSIGNED_SHORT_1_5_5_5_REV(rawFloatPixel * values,GLushort value)3577 void RectangleTest::pack_UNSIGNED_SHORT_1_5_5_5_REV(rawFloatPixel *values, GLushort value)
3578 {
3579     (*values)[3] = ((value >> 15) & 1) / 1.0f;
3580     (*values)[2] = ((value >> 10) & 31) / 31.0f;
3581     (*values)[1] = ((value >> 5) & 31) / 31.0f;
3582     (*values)[0] = ((value >> 0) & 31) / 31.0f;
3583 }
3584 
pack_UNSIGNED_SHORT_1_5_5_5_REV_UINT(rawUintPixel * values,GLushort value)3585 void RectangleTest::pack_UNSIGNED_SHORT_1_5_5_5_REV_UINT(rawUintPixel *values, GLushort value)
3586 {
3587     (*values)[3] = (value >> 15) & 1;
3588     (*values)[2] = (value >> 10) & 31;
3589     (*values)[1] = (value >> 5) & 31;
3590     (*values)[0] = (value >> 0) & 31;
3591 }
3592 
pack_UNSIGNED_SHORT_1_5_5_5_REV_INT(rawIntPixel * values,GLushort value)3593 void RectangleTest::pack_UNSIGNED_SHORT_1_5_5_5_REV_INT(rawIntPixel *values, GLushort value)
3594 {
3595     (*values)[3] = (static_cast<GLshort>(value) >> 15) & 1;
3596     (*values)[2] = (static_cast<GLshort>(value) >> 10) & 31;
3597     (*values)[1] = (static_cast<GLshort>(value) >> 5) & 31;
3598     (*values)[0] = (static_cast<GLshort>(value) >> 0) & 31;
3599 }
3600 
pack_UNSIGNED_INT_8_8_8_8(rawFloatPixel * values,GLuint value)3601 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8(rawFloatPixel *values, GLuint value)
3602 {
3603     (*values)[0] = ((value >> 24) & 255) / 255.0f;
3604     (*values)[1] = ((value >> 16) & 255) / 255.0f;
3605     (*values)[2] = ((value >> 8) & 255) / 255.0f;
3606     (*values)[3] = ((value >> 0) & 255) / 255.0f;
3607 }
3608 
pack_UNSIGNED_INT_8_8_8_8_UINT(rawUintPixel * values,GLuint value)3609 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8_UINT(rawUintPixel *values, GLuint value)
3610 {
3611     (*values)[0] = (value >> 24) & 255;
3612     (*values)[1] = (value >> 16) & 255;
3613     (*values)[2] = (value >> 8) & 255;
3614     (*values)[3] = (value >> 0) & 255;
3615 }
3616 
pack_UNSIGNED_INT_8_8_8_8_INT(rawIntPixel * values,GLuint value)3617 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8_INT(rawIntPixel *values, GLuint value)
3618 {
3619     (*values)[0] = (static_cast<GLint>(value) >> 24) & 255;
3620     (*values)[1] = (static_cast<GLint>(value) >> 16) & 255;
3621     (*values)[2] = (static_cast<GLint>(value) >> 8) & 255;
3622     (*values)[3] = (static_cast<GLint>(value) >> 0) & 255;
3623 }
3624 
pack_UNSIGNED_INT_8_8_8_8_REV(rawFloatPixel * values,GLuint value)3625 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8_REV(rawFloatPixel *values, GLuint value)
3626 {
3627     (*values)[3] = ((value >> 24) & 255) / 255.0f;
3628     (*values)[2] = ((value >> 16) & 255) / 255.0f;
3629     (*values)[1] = ((value >> 8) & 255) / 255.0f;
3630     (*values)[0] = ((value >> 0) & 255) / 255.0f;
3631 }
3632 
pack_UNSIGNED_INT_8_8_8_8_REV_UINT(rawUintPixel * values,GLuint value)3633 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8_REV_UINT(rawUintPixel *values, GLuint value)
3634 {
3635     (*values)[3] = (value >> 24) & 255;
3636     (*values)[2] = (value >> 16) & 255;
3637     (*values)[1] = (value >> 8) & 255;
3638     (*values)[0] = (value >> 0) & 255;
3639 }
3640 
pack_UNSIGNED_INT_8_8_8_8_REV_INT(rawIntPixel * values,GLuint value)3641 void RectangleTest::pack_UNSIGNED_INT_8_8_8_8_REV_INT(rawIntPixel *values, GLuint value)
3642 {
3643     (*values)[3] = (static_cast<GLint>(value) >> 24) & 255;
3644     (*values)[2] = (static_cast<GLint>(value) >> 16) & 255;
3645     (*values)[1] = (static_cast<GLint>(value) >> 8) & 255;
3646     (*values)[0] = (static_cast<GLint>(value) >> 0) & 255;
3647 }
3648 
pack_UNSIGNED_INT_10_10_10_2(rawFloatPixel * values,GLuint value)3649 void RectangleTest::pack_UNSIGNED_INT_10_10_10_2(rawFloatPixel *values, GLuint value)
3650 {
3651     (*values)[0] = ((value >> 22) & 1023) / 1023.0f;
3652     (*values)[1] = ((value >> 12) & 1023) / 1023.0f;
3653     (*values)[2] = ((value >> 2) & 1023) / 1023.0f;
3654     (*values)[3] = ((value >> 0) & 3) / 3.0f;
3655 }
3656 
pack_UNSIGNED_INT_10_10_10_2_UINT(rawUintPixel * values,GLuint value)3657 void RectangleTest::pack_UNSIGNED_INT_10_10_10_2_UINT(rawUintPixel *values, GLuint value)
3658 {
3659     (*values)[0] = ((value >> 22) & 1023);
3660     (*values)[1] = ((value >> 12) & 1023);
3661     (*values)[2] = ((value >> 2) & 1023);
3662     (*values)[3] = ((value >> 0) & 3);
3663 }
3664 
pack_UNSIGNED_INT_10_10_10_2_INT(rawIntPixel * values,GLuint value)3665 void RectangleTest::pack_UNSIGNED_INT_10_10_10_2_INT(rawIntPixel *values, GLuint value)
3666 {
3667     (*values)[0] = ((static_cast<GLint>(value) >> 22) & 1023);
3668     (*values)[1] = ((static_cast<GLint>(value) >> 12) & 1023);
3669     (*values)[2] = ((static_cast<GLint>(value) >> 2) & 1023);
3670     (*values)[3] = ((static_cast<GLint>(value) >> 0) & 3);
3671 }
3672 
pack_UNSIGNED_INT_2_10_10_10_REV(rawFloatPixel * values,GLuint value)3673 void RectangleTest::pack_UNSIGNED_INT_2_10_10_10_REV(rawFloatPixel *values, GLuint value)
3674 {
3675     (*values)[3] = ((value >> 30) & 3) / 3.0f;
3676     (*values)[2] = ((value >> 20) & 1023) / 1023.0f;
3677     (*values)[1] = ((value >> 10) & 1023) / 1023.0f;
3678     (*values)[0] = ((value >> 0) & 1023) / 1023.0f;
3679 }
3680 
pack_UNSIGNED_INT_2_10_10_10_REV_UINT(rawUintPixel * values,GLuint value)3681 void RectangleTest::pack_UNSIGNED_INT_2_10_10_10_REV_UINT(rawUintPixel *values, GLuint value)
3682 {
3683     (*values)[3] = (value >> 30) & 3;
3684     (*values)[2] = (value >> 20) & 1023;
3685     (*values)[1] = (value >> 10) & 1023;
3686     (*values)[0] = (value >> 0) & 1023;
3687 }
3688 
pack_UNSIGNED_INT_2_10_10_10_REV_INT(rawIntPixel * values,GLuint value)3689 void RectangleTest::pack_UNSIGNED_INT_2_10_10_10_REV_INT(rawIntPixel *values, GLuint value)
3690 {
3691     (*values)[3] = (static_cast<GLint>(value) >> 30) & 3;
3692     (*values)[2] = (static_cast<GLint>(value) >> 20) & 1023;
3693     (*values)[1] = (static_cast<GLint>(value) >> 10) & 1023;
3694     (*values)[0] = (static_cast<GLint>(value) >> 0) & 1023;
3695 }
3696 
pack_UNSIGNED_INT_24_8(rawFloatPixel * values,GLuint value)3697 void RectangleTest::pack_UNSIGNED_INT_24_8(rawFloatPixel *values, GLuint value)
3698 {
3699     (*values)[0] = ((value >> 8) & 16777215) / 16777215.0f;
3700     (*values)[1] = ((value >> 0) & 255) / 255.0f;
3701 }
3702 
pack_UNSIGNED_INT_10F_11F_11F_REV(rawFloatPixel * values,GLuint value)3703 void RectangleTest::pack_UNSIGNED_INT_10F_11F_11F_REV(rawFloatPixel *values, GLuint value)
3704 {
3705     (*values)[2] = unsignedF10ToFloat((value >> 22) & 1023);
3706     (*values)[1] = unsignedF11ToFloat((value >> 11) & 2047);
3707     (*values)[0] = unsignedF11ToFloat((value >> 0) & 2047);
3708 }
3709 
pack_UNSIGNED_INT_5_9_9_9_REV(rawFloatPixel * values,GLuint value)3710 void RectangleTest::pack_UNSIGNED_INT_5_9_9_9_REV(rawFloatPixel *values, GLuint value)
3711 {
3712     const int B   = 15;
3713     const int N   = 9;
3714     GLint pExp    = ((value >> 27) & 31);
3715     GLuint pBlue  = ((value >> 18) & 511);
3716     GLuint pGreen = ((value >> 9) & 511);
3717     GLuint pRed   = ((value >> 0) & 511);
3718 
3719     (*values)[2] = (float)(pBlue * pow(2.0, pExp - B - N));
3720     (*values)[1] = (float)(pGreen * pow(2.0, pExp - B - N));
3721     (*values)[0] = (float)(pRed * pow(2.0, pExp - B - N));
3722     (*values)[3] = 1.0f;
3723 }
3724 
pack_FLOAT_32_UNSIGNED_INT_24_8_REV(rawFloatPixel * values,F_32_UINT_24_8_REV value)3725 void RectangleTest::pack_FLOAT_32_UNSIGNED_INT_24_8_REV(rawFloatPixel *values, F_32_UINT_24_8_REV value)
3726 {
3727     (*values)[0] = value.d;
3728     (*values)[1] = (value.s & 255) / 255.0f;
3729 }
3730 
getTexImage()3731 bool RectangleTest::getTexImage()
3732 {
3733     // for each output format
3734     for (int m = 0; m < DE_LENGTH_OF_ARRAY(coreFormats); ++m)
3735     {
3736         const PixelFormat &outputFormat = coreFormats[m];
3737 
3738         // for each output type
3739         for (int n = 0; n < DE_LENGTH_OF_ARRAY(coreTypes); ++n)
3740         {
3741             const PixelType &outputType = coreTypes[n];
3742 
3743             if ((m_inputFormat.format != m_internalFormat.format || m_inputType.type != m_internalFormat.type) &&
3744                 (outputFormat.format != m_internalFormat.format || outputType.type != m_internalFormat.type))
3745             {
3746                 continue;
3747             }
3748 
3749             if (!getTexImageInner(outputFormat, outputType))
3750                 return false;
3751         }
3752     }
3753 
3754     return true;
3755 }
3756 
getTexImageInner(const PixelFormat & outputFormat,const PixelType & outputType)3757 bool RectangleTest::getTexImageInner(const PixelFormat &outputFormat, const PixelType &outputType)
3758 {
3759     bool outputFormatValid = isFormatValid(outputFormat, outputType, m_internalFormat, false, true, OUTPUT_GETTEXIMAGE);
3760 
3761     GLenum error = readOutputData(outputFormat, outputType, OUTPUT_GETTEXIMAGE);
3762     m_countGetTexImage++;
3763 
3764     if (!outputFormatValid)
3765     {
3766         if (error)
3767         {
3768             m_countGetTexImageOK++;
3769             return true;
3770         }
3771 
3772         m_testCtx.getLog() << tcu::TestLog::Message << "Expected error but got no GL error" << tcu::TestLog::EndMessage;
3773         return false;
3774     }
3775     else if (error)
3776     {
3777         m_testCtx.getLog() << tcu::TestLog::Message << "Error during glGetTexImage" << tcu::TestLog::EndMessage;
3778         return false;
3779     }
3780 
3781     m_countGetTexImageOK++;
3782     m_countCompare++;
3783 
3784     // compare output gradient to input gradient
3785     if (compare(&m_gradient[0], &m_outputBuffer[0], outputFormat, outputType, false))
3786     {
3787         m_countCompareOK++;
3788         return true;
3789     }
3790 
3791     m_testCtx.getLog() << tcu::TestLog::Message << "Gradient comparison failed during GetTexImage for input = ["
3792                        << getFormatStr(m_inputFormat.format) << ", " << getTypeStr(m_inputType.type) << "] output = ["
3793                        << getFormatStr(outputFormat.format) << ", " << getTypeStr(outputType.type) << "]"
3794                        << tcu::TestLog::EndMessage;
3795     return false;
3796 }
3797 
testAllFormatsAndTypes()3798 void RectangleTest::testAllFormatsAndTypes()
3799 {
3800     DE_ASSERT((m_textureTarget == GL_TEXTURE_2D) || (m_textureTarget == GL_TEXTURE_3D));
3801 
3802     glu::RenderContext &renderContext = m_context.getRenderContext();
3803     const Functions &gl               = renderContext.getFunctions();
3804     bool result                       = true;
3805 
3806     gl.clear(GL_COLOR_BUFFER_BIT);
3807 
3808     const PixelType *types;
3809     int typesCount;
3810 
3811     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
3812     {
3813         types      = esTypes;
3814         typesCount = DE_LENGTH_OF_ARRAY(esTypes);
3815     }
3816     else
3817     {
3818         types      = coreTypes;
3819         typesCount = DE_LENGTH_OF_ARRAY(coreTypes);
3820     }
3821 
3822     for (int inputTypeIndex = 0; inputTypeIndex < typesCount; inputTypeIndex++)
3823     {
3824         GLenum error = 0;
3825         m_inputType  = types[inputTypeIndex];
3826 
3827         applyInitialStorageModes();
3828 
3829         // Create input gradient in format,type, with appropriate range
3830         createGradient();
3831         if (m_gradient.empty())
3832             TCU_FAIL("Could not create gradient.");
3833 
3834         if (m_unpackProperties.swapBytes)
3835             swapBytes(m_inputType.size, m_gradient);
3836 
3837         GLuint texture;
3838         gl.genTextures(1, &texture);
3839         gl.bindTexture(m_textureTarget, texture);
3840         GLU_EXPECT_NO_ERROR(gl.getError(), "glBindTexture");
3841         if (m_textureTarget == GL_TEXTURE_3D)
3842         {
3843             gl.texImage3D(GL_TEXTURE_3D, 0, m_internalFormat.sizedFormat, GRADIENT_WIDTH, GRADIENT_HEIGHT, 1, 0,
3844                           m_inputFormat.format, m_inputType.type, &m_gradient[0]);
3845         }
3846         else
3847         {
3848             gl.texImage2D(GL_TEXTURE_2D, 0, m_internalFormat.sizedFormat, GRADIENT_WIDTH, GRADIENT_HEIGHT, 0,
3849                           m_inputFormat.format, m_inputType.type, &m_gradient[0]);
3850         }
3851 
3852         if (m_unpackProperties.swapBytes)
3853             swapBytes(m_inputType.size, m_gradient);
3854 
3855         error = gl.getError();
3856         if (isFormatValid(m_inputFormat, m_inputType, m_internalFormat, true, false, INPUT_TEXIMAGE))
3857         {
3858             if (error == GL_NO_ERROR)
3859             {
3860                 if (!glu::isContextTypeES(renderContext.getType()))
3861                     result &= getTexImage();
3862                 result &= doRead(texture);
3863             }
3864             else
3865             {
3866                 m_testCtx.getLog() << tcu::TestLog::Message << "Valid format used but glTexImage2D/3D failed"
3867                                    << tcu::TestLog::EndMessage;
3868                 result = false;
3869             }
3870         }
3871         else if (error == GL_NO_ERROR)
3872         {
3873             m_testCtx.getLog() << tcu::TestLog::Message << "Invalid format used but glTexImage2D/3D succeeded"
3874                                << tcu::TestLog::EndMessage;
3875             result = false;
3876         }
3877 
3878         gl.deleteTextures(1, &texture);
3879     }
3880 
3881     if (result)
3882         m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
3883     else
3884         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
3885 }
3886 
iterate(void)3887 tcu::TestNode::IterateResult RectangleTest::iterate(void)
3888 {
3889     resetInitialStorageModes();
3890     testAllFormatsAndTypes();
3891     return STOP;
3892 }
3893 
3894 class InitialValuesTest : public deqp::TestCase
3895 {
3896 public:
3897     InitialValuesTest(deqp::Context &context);
3898     virtual ~InitialValuesTest();
3899 
3900     tcu::TestNode::IterateResult iterate(void);
3901 };
3902 
InitialValuesTest(deqp::Context & context)3903 InitialValuesTest::InitialValuesTest(deqp::Context &context)
3904     : deqp::TestCase(context, "initial_values",
3905                      "Verify if all UNPACK and PACK initial "
3906                      "state matches the values in table 6.28 (6.23, in ES.)")
3907 {
3908 }
3909 
~InitialValuesTest()3910 InitialValuesTest::~InitialValuesTest()
3911 {
3912 }
3913 
iterate(void)3914 tcu::TestNode::IterateResult InitialValuesTest::iterate(void)
3915 {
3916     glu::RenderContext &renderContext = m_context.getRenderContext();
3917     const Functions &gl               = renderContext.getFunctions();
3918 
3919     bool result = true;
3920 
3921     GLenum commonIntModes[] = {GL_UNPACK_ROW_LENGTH,   GL_UNPACK_SKIP_ROWS,   GL_UNPACK_SKIP_PIXELS,
3922                                GL_UNPACK_IMAGE_HEIGHT, GL_UNPACK_SKIP_IMAGES, GL_PACK_ROW_LENGTH,
3923                                GL_PACK_SKIP_ROWS,      GL_PACK_SKIP_PIXELS};
3924 
3925     // check if following eight storage modes are 0
3926     GLint i = 1;
3927     for (int mode = 0; mode < DE_LENGTH_OF_ARRAY(commonIntModes); mode++)
3928     {
3929         gl.getIntegerv(commonIntModes[mode], &i);
3930         result &= (i == 0);
3931     }
3932 
3933     // check if following two storage modes are 4
3934     gl.getIntegerv(GL_UNPACK_ALIGNMENT, &i);
3935     result &= (i == 4);
3936     gl.getIntegerv(GL_PACK_ALIGNMENT, &i);
3937     result &= (i == 4);
3938 
3939     // check storage modes available only in core GL
3940     if (!glu::isContextTypeES(renderContext.getType()))
3941     {
3942         // check if following four boolean modes are false
3943         GLboolean b = true;
3944         gl.getBooleanv(GL_UNPACK_SWAP_BYTES, &b);
3945         result &= (b == false);
3946         gl.getBooleanv(GL_UNPACK_LSB_FIRST, &b);
3947         result &= (b == false);
3948         gl.getBooleanv(GL_PACK_SWAP_BYTES, &b);
3949         result &= (b == false);
3950         gl.getBooleanv(GL_PACK_LSB_FIRST, &b);
3951         result &= (b == false);
3952 
3953         // check if following two modes are 0
3954         gl.getIntegerv(GL_PACK_IMAGE_HEIGHT, &i);
3955         result &= (i == 0);
3956         gl.getIntegerv(GL_PACK_SKIP_IMAGES, &i);
3957         result &= (i == 0);
3958     }
3959 
3960     // make sure that no pack/unpack buffers are bound
3961     gl.getIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &i);
3962     result &= (i == 0);
3963     gl.getIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &i);
3964     result &= (i == 0);
3965 
3966     if (result)
3967         m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
3968     else
3969         m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
3970     return STOP;
3971 }
3972 
3973 class PBORectangleTest : public RectangleTest
3974 {
3975 public:
3976     PBORectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat, PixelFormat inputFormat);
3977     virtual ~PBORectangleTest();
3978 };
3979 
PBORectangleTest(deqp::Context & context,std::string & name,InternalFormat internalFormat,PixelFormat inputFormat)3980 PBORectangleTest::PBORectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat,
3981                                    PixelFormat inputFormat)
3982     : RectangleTest(context, name, internalFormat, inputFormat)
3983 {
3984     m_usePBO = true;
3985 }
3986 
~PBORectangleTest()3987 PBORectangleTest::~PBORectangleTest()
3988 {
3989 }
3990 
3991 class VariedRectangleTest : public RectangleTest
3992 {
3993 public:
3994     VariedRectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat,
3995                         PixelFormat inputFormat);
3996     virtual ~VariedRectangleTest();
3997 
3998     tcu::TestNode::IterateResult iterate(void);
3999 
4000 protected:
4001     struct StoreMode
4002     {
4003         GLenum parameter;
4004         GLint *property;
4005         GLint value;
4006     };
4007 };
4008 
VariedRectangleTest(deqp::Context & context,std::string & name,InternalFormat internalFormat,PixelFormat inputFormat)4009 VariedRectangleTest::VariedRectangleTest(deqp::Context &context, std::string &name, InternalFormat internalFormat,
4010                                          PixelFormat inputFormat)
4011     : RectangleTest(context, name, internalFormat, inputFormat)
4012 {
4013 }
4014 
~VariedRectangleTest()4015 VariedRectangleTest::~VariedRectangleTest()
4016 {
4017 }
4018 
iterate(void)4019 tcu::TestNode::IterateResult VariedRectangleTest::iterate(void)
4020 {
4021     const int IMAGE_WIDTH_1  = 10;
4022     const int IMAGE_WIDTH_2  = 15;
4023     const int IMAGE_HEIGHT_1 = 10;
4024     const int IMAGE_HEIGHT_2 = 15;
4025 
4026     PackedPixelsBufferProperties &up = m_initialUnpackProperties;
4027     PackedPixelsBufferProperties &pp = m_initialPackProperties;
4028 
4029     StoreMode commonCases[] = {
4030         {GL_UNPACK_ROW_LENGTH, &up.rowLength, 0},
4031         {GL_UNPACK_ROW_LENGTH, &up.rowLength, IMAGE_WIDTH_1},
4032         {GL_UNPACK_ROW_LENGTH, &up.rowLength, IMAGE_WIDTH_2},
4033         {GL_UNPACK_SKIP_ROWS, &up.skipRows, 0},
4034         {GL_UNPACK_SKIP_ROWS, &up.skipRows, 1},
4035         {GL_UNPACK_SKIP_ROWS, &up.skipRows, 2},
4036         {GL_UNPACK_SKIP_PIXELS, &up.skipPixels, 0},
4037         {GL_UNPACK_SKIP_PIXELS, &up.skipPixels, 1},
4038         {GL_UNPACK_SKIP_PIXELS, &up.skipPixels, 2},
4039         {GL_UNPACK_ALIGNMENT, &up.alignment, 1},
4040         {GL_UNPACK_ALIGNMENT, &up.alignment, 2},
4041         {GL_UNPACK_ALIGNMENT, &up.alignment, 4},
4042         {GL_UNPACK_ALIGNMENT, &up.alignment, 8},
4043         {GL_UNPACK_IMAGE_HEIGHT, &up.rowCount, 0},
4044         {GL_UNPACK_IMAGE_HEIGHT, &up.rowCount, IMAGE_HEIGHT_1},
4045         {GL_UNPACK_IMAGE_HEIGHT, &up.rowCount, IMAGE_HEIGHT_2},
4046         {GL_UNPACK_SKIP_IMAGES, &up.skipImages, 0},
4047         {GL_UNPACK_SKIP_IMAGES, &up.skipImages, 1},
4048         {GL_UNPACK_SKIP_IMAGES, &up.skipImages, 2},
4049         {GL_PACK_ROW_LENGTH, &pp.rowLength, 0},
4050         {GL_PACK_ROW_LENGTH, &pp.rowLength, IMAGE_WIDTH_1},
4051         {GL_PACK_ROW_LENGTH, &pp.rowLength, IMAGE_WIDTH_2},
4052         {GL_PACK_SKIP_ROWS, &pp.skipRows, 0},
4053         {GL_PACK_SKIP_ROWS, &pp.skipRows, 1},
4054         {GL_PACK_SKIP_ROWS, &pp.skipRows, 2},
4055         {GL_PACK_SKIP_PIXELS, &pp.skipPixels, 0},
4056         {GL_PACK_SKIP_PIXELS, &pp.skipPixels, 1},
4057         {GL_PACK_SKIP_PIXELS, &pp.skipPixels, 2},
4058         {GL_PACK_ALIGNMENT, &pp.alignment, 1},
4059         {GL_PACK_ALIGNMENT, &pp.alignment, 2},
4060         {GL_PACK_ALIGNMENT, &pp.alignment, 4},
4061         {GL_PACK_ALIGNMENT, &pp.alignment, 8},
4062     };
4063 
4064     StoreMode coreCases[] = {
4065         {GL_UNPACK_SWAP_BYTES, &up.swapBytes, GL_FALSE},
4066         {GL_UNPACK_SWAP_BYTES, &up.swapBytes, GL_TRUE},
4067         {GL_UNPACK_LSB_FIRST, &up.lsbFirst, GL_FALSE},
4068         {GL_UNPACK_LSB_FIRST, &up.lsbFirst, GL_TRUE},
4069         {GL_PACK_SWAP_BYTES, &pp.swapBytes, GL_FALSE},
4070         {GL_PACK_SWAP_BYTES, &pp.swapBytes, GL_TRUE},
4071         {GL_PACK_LSB_FIRST, &pp.lsbFirst, GL_FALSE},
4072         {GL_PACK_LSB_FIRST, &pp.lsbFirst, GL_TRUE},
4073         {GL_PACK_IMAGE_HEIGHT, &pp.rowCount, 0},
4074         {GL_PACK_IMAGE_HEIGHT, &pp.rowCount, IMAGE_HEIGHT_1},
4075         {GL_PACK_IMAGE_HEIGHT, &pp.rowCount, IMAGE_HEIGHT_2},
4076         {GL_PACK_SKIP_IMAGES, &pp.skipImages, 0},
4077         {GL_PACK_SKIP_IMAGES, &pp.skipImages, 1},
4078         {GL_PACK_SKIP_IMAGES, &pp.skipImages, 2},
4079     };
4080 
4081     std::vector<StoreMode> testModes(commonCases, commonCases + DE_LENGTH_OF_ARRAY(commonCases));
4082     glu::RenderContext &renderContext = m_context.getRenderContext();
4083     bool contextTypeIsCoreGL          = !glu::isContextTypeES(renderContext.getType());
4084     if (contextTypeIsCoreGL)
4085         testModes.insert(testModes.end(), coreCases, coreCases + DE_LENGTH_OF_ARRAY(coreCases));
4086 
4087     std::vector<StoreMode>::iterator currentCase = testModes.begin();
4088     while (currentCase != testModes.end())
4089     {
4090         resetInitialStorageModes();
4091 
4092         GLenum parameter = currentCase->parameter;
4093         GLint value      = currentCase->value;
4094 
4095         *(currentCase->property) = value;
4096 
4097         // for some parameters an additional parameter needs to be set
4098         if (parameter == GL_PACK_SKIP_ROWS)
4099         {
4100             if (contextTypeIsCoreGL)
4101                 m_initialPackProperties.rowCount = GRADIENT_HEIGHT + value;
4102         }
4103         else if (parameter == GL_PACK_SKIP_PIXELS)
4104             m_initialPackProperties.rowLength = GRADIENT_WIDTH + value;
4105         else if (parameter == GL_UNPACK_SKIP_ROWS)
4106             m_initialUnpackProperties.rowCount = GRADIENT_HEIGHT + value;
4107         else if (parameter == GL_UNPACK_SKIP_PIXELS)
4108             m_initialUnpackProperties.rowLength = GRADIENT_WIDTH + value;
4109 
4110         m_textureTarget = GL_TEXTURE_2D;
4111         if ((parameter == GL_PACK_IMAGE_HEIGHT) || (parameter == GL_PACK_SKIP_IMAGES) ||
4112             (parameter == GL_UNPACK_IMAGE_HEIGHT) || (parameter == GL_UNPACK_SKIP_IMAGES))
4113             m_textureTarget = GL_TEXTURE_3D;
4114 
4115         testAllFormatsAndTypes();
4116 
4117         if (m_testCtx.getTestResult() != QP_TEST_RESULT_PASS)
4118         {
4119             m_testCtx.getLog() << tcu::TestLog::Message
4120                                << "Case for: " << glu::getGettableStateStr(parameter).toString() << " = " << value
4121                                << " failed." << tcu::TestLog::EndMessage;
4122             return STOP;
4123         }
4124 
4125         ++currentCase;
4126     }
4127 
4128     return STOP;
4129 }
4130 
PackedPixelsTests(deqp::Context & context)4131 PackedPixelsTests::PackedPixelsTests(deqp::Context &context) : TestCaseGroup(context, "packed_pixels", "")
4132 {
4133 }
4134 
~PackedPixelsTests(void)4135 PackedPixelsTests::~PackedPixelsTests(void)
4136 {
4137 #ifdef LOG_PACKED_PIXELS_STATISTICS
4138     m_testCtx.getLog() << tcu::TestLog::Message << "PackedPixelsTests statistics:"
4139                        << "\n  countReadPixels: " << RectangleTest::m_countReadPixels
4140                        << "\n  countReadPixelsOK: " << RectangleTest::m_countReadPixelsOK
4141                        << "\n  countGetTexImage: " << RectangleTest::m_countGetTexImage
4142                        << "\n  countGetTexImageOK: " << RectangleTest::m_countGetTexImageOK
4143                        << "\n  countCompare: " << RectangleTest::m_countCompare
4144                        << "\n  countCompareOK: " << RectangleTest::m_countCompareOK << tcu::TestLog::EndMessage;
4145 #endif
4146 }
4147 
init(void)4148 void PackedPixelsTests::init(void)
4149 {
4150     const InternalFormat *internalFormats;
4151     unsigned int internalFormatsCount;
4152 
4153     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
4154     {
4155         internalFormats      = esInternalformats;
4156         internalFormatsCount = DE_LENGTH_OF_ARRAY(esInternalformats);
4157     }
4158     else
4159     {
4160         internalFormats      = coreInternalformats;
4161         internalFormatsCount = DE_LENGTH_OF_ARRAY(coreInternalformats);
4162     }
4163 
4164     TestCaseGroup *rectangleGroup = new deqp::TestCaseGroup(m_context, "rectangle", "");
4165     rectangleGroup->addChild(new InitialValuesTest(m_context));
4166     TestCaseGroup *pboRectangleGroup    = new deqp::TestCaseGroup(m_context, "pbo_rectangle", "");
4167     TestCaseGroup *variedRectangleGroup = new deqp::TestCaseGroup(m_context, "varied_rectangle", "");
4168 
4169     for (unsigned int internalFormatIndex = 0; internalFormatIndex < internalFormatsCount; internalFormatIndex++)
4170     {
4171         const InternalFormat &internalFormat = internalFormats[internalFormatIndex];
4172         std::string internalFormatString     = getFormatStr(internalFormat.sizedFormat);
4173 
4174         std::string name = internalFormatString.substr(3);
4175         std::transform(name.begin(), name.end(), name.begin(), tolower);
4176 
4177         addRectangleTest(rectangleGroup, name, internalFormat);
4178         addPBORectangleTest(pboRectangleGroup, name, internalFormat);
4179         addVariedRectangleTest(variedRectangleGroup, name, internalFormat);
4180     }
4181 
4182     addChild(rectangleGroup);
4183     addChild(pboRectangleGroup);
4184     addChild(variedRectangleGroup);
4185 }
4186 
4187 template <class T>
addRectangleTests(TestCaseGroup * testCaseGroup,std::string & name,const InternalFormat & internalFormat)4188 void PackedPixelsTests::addRectangleTests(TestCaseGroup *testCaseGroup, std::string &name,
4189                                           const InternalFormat &internalFormat)
4190 {
4191     const PixelFormat *formats;
4192     int formatsCount;
4193 
4194     if (glu::isContextTypeES(m_context.getRenderContext().getType()))
4195     {
4196         formats      = esFormats;
4197         formatsCount = DE_LENGTH_OF_ARRAY(esFormats);
4198     }
4199     else
4200     {
4201         formats      = coreFormats;
4202         formatsCount = DE_LENGTH_OF_ARRAY(coreFormats);
4203     }
4204 
4205     for (int inputFormatIndex = 0; inputFormatIndex < formatsCount; inputFormatIndex++)
4206     {
4207         testCaseGroup->addChild(new T(m_context, name, internalFormat, formats[inputFormatIndex]));
4208     }
4209 }
4210 
addRectangleTest(TestCaseGroup * testCaseGroup,std::string & name,const InternalFormat & internalFormat)4211 void PackedPixelsTests::addRectangleTest(TestCaseGroup *testCaseGroup, std::string &name,
4212                                          const InternalFormat &internalFormat)
4213 {
4214     if (testCaseGroup == nullptr)
4215         return;
4216 
4217     addRectangleTests<RectangleTest>(testCaseGroup, name, internalFormat);
4218 }
4219 
addPBORectangleTest(TestCaseGroup * testCaseGroup,std::string & name,const InternalFormat & internalFormat)4220 void PackedPixelsTests::addPBORectangleTest(TestCaseGroup *testCaseGroup, std::string &name,
4221                                             const InternalFormat &internalFormat)
4222 {
4223     if (testCaseGroup == nullptr)
4224         return;
4225 
4226     addRectangleTests<PBORectangleTest>(testCaseGroup, name, internalFormat);
4227 }
4228 
addVariedRectangleTest(TestCaseGroup * testCaseGroup,std::string & name,const InternalFormat & internalFormat)4229 void PackedPixelsTests::addVariedRectangleTest(TestCaseGroup *testCaseGroup, std::string &name,
4230                                                const InternalFormat &internalFormat)
4231 {
4232     if (testCaseGroup == nullptr)
4233         return;
4234 
4235     addRectangleTests<VariedRectangleTest>(testCaseGroup, name, internalFormat);
4236 }
4237 
4238 } // namespace glcts
4239