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