• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
3  * ----------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Texture utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "tcuTextureUtil.hpp"
25 #include "tcuVectorUtil.hpp"
26 #include "deRandom.hpp"
27 #include "deMath.h"
28 #include "deMemory.h"
29 
30 #include <limits>
31 
32 namespace tcu
33 {
34 
sRGBChannelToLinear(float cs)35 static inline float sRGBChannelToLinear (float cs)
36 {
37 	if (cs <= 0.04045)
38 		return cs / 12.92f;
39 	else
40 		return deFloatPow((cs + 0.055f) / 1.055f, 2.4f);
41 }
42 
43 static const deUint32 s_srgb8Lut[256] =
44 {
45 #include "tcuSRGB8Lut.inl"
46 };
47 
sRGB8ChannelToLinear(deUint32 cs)48 static inline float sRGB8ChannelToLinear (deUint32 cs)
49 {
50 	DE_ASSERT(cs < 256);
51 
52 	// \note This triggers UB, but in practice it doesn't cause any problems
53 	return ((const float*)s_srgb8Lut)[cs];
54 }
55 
linearChannelToSRGB(float cl)56 static inline float linearChannelToSRGB (float cl)
57 {
58 	if (cl <= 0.0f)
59 		return 0.0f;
60 	else if (cl < 0.0031308f)
61 		return 12.92f*cl;
62 	else if (cl < 1.0f)
63 		return 1.055f*deFloatPow(cl, 0.41666f) - 0.055f;
64 	else
65 		return 1.0f;
66 }
67 
68 //! Convert sRGB to linear colorspace
sRGBToLinear(const Vec4 & cs)69 Vec4 sRGBToLinear (const Vec4& cs)
70 {
71 	return Vec4(sRGBChannelToLinear(cs[0]),
72 				sRGBChannelToLinear(cs[1]),
73 				sRGBChannelToLinear(cs[2]),
74 				cs[3]);
75 }
76 
sRGB8ToLinear(const UVec4 & cs)77 Vec4 sRGB8ToLinear (const UVec4& cs)
78 {
79 	return Vec4(sRGB8ChannelToLinear(cs[0]),
80 				sRGB8ChannelToLinear(cs[1]),
81 				sRGB8ChannelToLinear(cs[2]),
82 				1.0f);
83 }
84 
sRGBA8ToLinear(const UVec4 & cs)85 Vec4 sRGBA8ToLinear (const UVec4& cs)
86 {
87 	return Vec4(sRGB8ChannelToLinear(cs[0]),
88 				sRGB8ChannelToLinear(cs[1]),
89 				sRGB8ChannelToLinear(cs[2]),
90 				(float)cs[3] / 255.0f);
91 }
92 
93 //! Convert from linear to sRGB colorspace
linearToSRGB(const Vec4 & cl)94 Vec4 linearToSRGB (const Vec4& cl)
95 {
96 	return Vec4(linearChannelToSRGB(cl[0]),
97 				linearChannelToSRGB(cl[1]),
98 				linearChannelToSRGB(cl[2]),
99 				cl[3]);
100 }
101 
isSRGB(TextureFormat format)102 bool isSRGB (TextureFormat format)
103 {
104 	// make sure to update this if type table is updated
105 	DE_STATIC_ASSERT(TextureFormat::CHANNELORDER_LAST == 21);
106 
107 	return	format.order == TextureFormat::sR		||
108 			format.order == TextureFormat::sRG		||
109 			format.order == TextureFormat::sRGB		||
110 			format.order == TextureFormat::sRGBA	||
111 			format.order == TextureFormat::sBGR		||
112 			format.order == TextureFormat::sBGRA;
113 }
114 
linearToSRGBIfNeeded(const TextureFormat & format,const tcu::Vec4 & color)115 tcu::Vec4 linearToSRGBIfNeeded (const TextureFormat& format, const tcu::Vec4& color)
116 {
117 	return isSRGB(format) ? linearToSRGB(color) : color;
118 }
119 
isCombinedDepthStencilType(TextureFormat::ChannelType type)120 bool isCombinedDepthStencilType (TextureFormat::ChannelType type)
121 {
122 	// make sure to update this if type table is updated
123 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
124 
125 	return	type == TextureFormat::UNSIGNED_INT_16_8_8			||
126 			type == TextureFormat::UNSIGNED_INT_24_8			||
127 			type == TextureFormat::UNSIGNED_INT_24_8_REV		||
128 			type == TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV;
129 }
130 
hasStencilComponent(TextureFormat::ChannelOrder order)131 bool hasStencilComponent (TextureFormat::ChannelOrder order)
132 {
133 	DE_STATIC_ASSERT(TextureFormat::CHANNELORDER_LAST == 21);
134 
135 	switch (order)
136 	{
137 		case TextureFormat::S:
138 		case TextureFormat::DS:
139 			return true;
140 
141 		default:
142 			return false;
143 	}
144 }
145 
hasDepthComponent(TextureFormat::ChannelOrder order)146 bool hasDepthComponent (TextureFormat::ChannelOrder order)
147 {
148 	DE_STATIC_ASSERT(TextureFormat::CHANNELORDER_LAST == 21);
149 
150 	switch (order)
151 	{
152 		case TextureFormat::D:
153 		case TextureFormat::DS:
154 			return true;
155 
156 		default:
157 			return false;
158 	}
159 }
160 
161 //! Get texture channel class for format
getTextureChannelClass(TextureFormat::ChannelType channelType)162 TextureChannelClass getTextureChannelClass (TextureFormat::ChannelType channelType)
163 {
164 	// make sure this table is updated if format table is updated
165 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
166 
167 	switch (channelType)
168 	{
169 		case TextureFormat::SNORM_INT8:						return TEXTURECHANNELCLASS_SIGNED_FIXED_POINT;
170 		case TextureFormat::SNORM_INT16:					return TEXTURECHANNELCLASS_SIGNED_FIXED_POINT;
171 		case TextureFormat::SNORM_INT32:					return TEXTURECHANNELCLASS_SIGNED_FIXED_POINT;
172 		case TextureFormat::UNORM_INT8:						return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
173 		case TextureFormat::UNORM_INT16:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
174 		case TextureFormat::UNORM_INT24:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
175 		case TextureFormat::UNORM_INT32:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
176 		case TextureFormat::UNORM_BYTE_44:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
177 		case TextureFormat::UNORM_SHORT_565:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
178 		case TextureFormat::UNORM_SHORT_555:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
179 		case TextureFormat::UNORM_SHORT_4444:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
180 		case TextureFormat::UNORM_SHORT_5551:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
181 		case TextureFormat::UNORM_SHORT_1555:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
182 		case TextureFormat::UNSIGNED_BYTE_44:				return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
183 		case TextureFormat::UNSIGNED_SHORT_565:				return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
184 		case TextureFormat::UNSIGNED_SHORT_4444:			return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
185 		case TextureFormat::UNSIGNED_SHORT_5551:			return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
186 		case TextureFormat::UNORM_INT_101010:				return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
187 		case TextureFormat::SNORM_INT_1010102_REV:			return TEXTURECHANNELCLASS_SIGNED_FIXED_POINT;
188 		case TextureFormat::UNORM_INT_1010102_REV:			return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
189 		case TextureFormat::SIGNED_INT_1010102_REV:			return TEXTURECHANNELCLASS_SIGNED_INTEGER;
190 		case TextureFormat::UNSIGNED_INT_1010102_REV:		return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
191 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	return TEXTURECHANNELCLASS_FLOATING_POINT;
192 		case TextureFormat::UNSIGNED_INT_999_E5_REV:		return TEXTURECHANNELCLASS_FLOATING_POINT;
193 		case TextureFormat::UNSIGNED_INT_16_8_8:			return TEXTURECHANNELCLASS_LAST;					//!< packed unorm16-x8-uint8
194 		case TextureFormat::UNSIGNED_INT_24_8:				return TEXTURECHANNELCLASS_LAST;					//!< packed unorm24-uint8
195 		case TextureFormat::UNSIGNED_INT_24_8_REV:			return TEXTURECHANNELCLASS_LAST;					//!< packed unorm24-uint8
196 		case TextureFormat::SIGNED_INT8:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
197 		case TextureFormat::SIGNED_INT16:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
198 		case TextureFormat::SIGNED_INT32:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
199 		case TextureFormat::SIGNED_INT64:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
200 		case TextureFormat::UNSIGNED_INT8:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
201 		case TextureFormat::UNSIGNED_INT16:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
202 		case TextureFormat::UNSIGNED_INT24:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
203 		case TextureFormat::UNSIGNED_INT32:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
204 		case TextureFormat::UNSIGNED_INT64:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
205 		case TextureFormat::HALF_FLOAT:						return TEXTURECHANNELCLASS_FLOATING_POINT;
206 		case TextureFormat::FLOAT:							return TEXTURECHANNELCLASS_FLOATING_POINT;
207 		case TextureFormat::FLOAT64:						return TEXTURECHANNELCLASS_FLOATING_POINT;
208 		case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:	return TEXTURECHANNELCLASS_LAST;					//!< packed float32-pad24-uint8
209 		case TextureFormat::UNORM_SHORT_10:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
210 		case TextureFormat::UNORM_SHORT_12:					return TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
211 		case TextureFormat::USCALED_INT8:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
212 		case TextureFormat::USCALED_INT16:					return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
213 		case TextureFormat::SSCALED_INT8:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
214 		case TextureFormat::SSCALED_INT16:					return TEXTURECHANNELCLASS_SIGNED_INTEGER;
215 		case TextureFormat::USCALED_INT_1010102_REV:		return TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
216 		case TextureFormat::SSCALED_INT_1010102_REV:		return TEXTURECHANNELCLASS_SIGNED_INTEGER;
217 		default:
218 			DE_FATAL("Unknown channel type");
219 			return TEXTURECHANNELCLASS_LAST;
220 	}
221 }
222 
isAccessValid(TextureFormat format,TextureAccessType type)223 bool isAccessValid (TextureFormat format, TextureAccessType type)
224 {
225 	DE_ASSERT(isValid(format));
226 
227 	if (format.order == TextureFormat::DS)
228 	{
229 		// It is never allowed to access combined depth-stencil format with getPixel().
230 		// Instead either getPixDepth() or getPixStencil(), or effective depth- or stencil-
231 		// access must be used.
232 		return false;
233 	}
234 	else if (format.order == TextureFormat::D)
235 		return type == TEXTUREACCESSTYPE_FLOAT;
236 	else if (format.order == TextureFormat::S)
237 		return type == TEXTUREACCESSTYPE_UNSIGNED_INT;
238 	else
239 	{
240 		// A few packed color formats have access type restrictions
241 		if (format.type == TextureFormat::UNSIGNED_INT_11F_11F_10F_REV ||
242 			format.type == TextureFormat::UNSIGNED_INT_999_E5_REV)
243 			return type == TEXTUREACCESSTYPE_FLOAT;
244 		else
245 			return true;
246 	}
247 }
248 
249 /*--------------------------------------------------------------------*//*!
250  * \brief Get access to subregion of pixel buffer
251  * \param access	Parent access object
252  * \param x			X offset
253  * \param y			Y offset
254  * \param z			Z offset
255  * \param width		Width
256  * \param height	Height
257  * \param depth		Depth
258  * \return Access object that targets given subregion of parent access object
259  *//*--------------------------------------------------------------------*/
getSubregion(const ConstPixelBufferAccess & access,int x,int y,int z,int width,int height,int depth)260 ConstPixelBufferAccess getSubregion (const ConstPixelBufferAccess& access, int x, int y, int z, int width, int height, int depth)
261 {
262 	DE_ASSERT(de::inBounds(x, 0, access.getWidth()));
263 	DE_ASSERT(de::inRange(x+width, x+1, access.getWidth()));
264 
265 	DE_ASSERT(de::inBounds(y, 0, access.getHeight()));
266 	DE_ASSERT(de::inRange(y+height, y+1, access.getHeight()));
267 
268 	DE_ASSERT(de::inBounds(z, 0, access.getDepth()));
269 	DE_ASSERT(de::inRange(z+depth, z+1, access.getDepth()));
270 
271 	return ConstPixelBufferAccess(access.getFormat(), tcu::IVec3(width, height, depth), access.getPitch(),
272 								  (const deUint8*)access.getDataPtr() + access.getPixelPitch()*x + access.getRowPitch()*y + access.getSlicePitch()*z);
273 }
274 
275 /*--------------------------------------------------------------------*//*!
276  * \brief Get access to subregion of pixel buffer
277  * \param access	Parent access object
278  * \param x			X offset
279  * \param y			Y offset
280  * \param z			Z offset
281  * \param width		Width
282  * \param height	Height
283  * \param depth		Depth
284  * \return Access object that targets given subregion of parent access object
285  *//*--------------------------------------------------------------------*/
getSubregion(const PixelBufferAccess & access,int x,int y,int z,int width,int height,int depth)286 PixelBufferAccess getSubregion (const PixelBufferAccess& access, int x, int y, int z, int width, int height, int depth)
287 {
288 	DE_ASSERT(de::inBounds(x, 0, access.getWidth()));
289 	DE_ASSERT(de::inRange(x+width, x+1, access.getWidth()));
290 
291 	DE_ASSERT(de::inBounds(y, 0, access.getHeight()));
292 	DE_ASSERT(de::inRange(y+height, y+1, access.getHeight()));
293 
294 	DE_ASSERT(de::inBounds(z, 0, access.getDepth()));
295 	DE_ASSERT(de::inRange(z+depth, z+1, access.getDepth()));
296 
297 	return PixelBufferAccess(access.getFormat(), tcu::IVec3(width, height, depth), access.getPitch(),
298 							 (deUint8*)access.getDataPtr() + access.getPixelPitch()*x + access.getRowPitch()*y + access.getSlicePitch()*z);
299 }
300 
301 /*--------------------------------------------------------------------*//*!
302  * \brief Get access to subregion of pixel buffer
303  * \param access	Parent access object
304  * \param x			X offset
305  * \param y			Y offset
306  * \param width		Width
307  * \param height	Height
308  * \return Access object that targets given subregion of parent access object
309  *//*--------------------------------------------------------------------*/
getSubregion(const PixelBufferAccess & access,int x,int y,int width,int height)310 PixelBufferAccess getSubregion (const PixelBufferAccess& access, int x, int y, int width, int height)
311 {
312 	return getSubregion(access, x, y, 0, width, height, 1);
313 }
314 
315 /*--------------------------------------------------------------------*//*!
316  * \brief Get access to subregion of pixel buffer
317  * \param access	Parent access object
318  * \param x			X offset
319  * \param y			Y offset
320  * \param width		Width
321  * \param height	Height
322  * \return Access object that targets given subregion of parent access object
323  *//*--------------------------------------------------------------------*/
getSubregion(const ConstPixelBufferAccess & access,int x,int y,int width,int height)324 ConstPixelBufferAccess getSubregion (const ConstPixelBufferAccess& access, int x, int y, int width, int height)
325 {
326 	return getSubregion(access, x, y, 0, width, height, 1);
327 }
328 
329 /*--------------------------------------------------------------------*//*!
330  * \brief Flip rows in Y direction
331  * \param access Access object
332  * \return Modified access object where Y coordinates are reversed
333  *//*--------------------------------------------------------------------*/
flipYAccess(const PixelBufferAccess & access)334 PixelBufferAccess flipYAccess (const PixelBufferAccess& access)
335 {
336 	const int			rowPitch		= access.getRowPitch();
337 	const int			offsetToLast	= rowPitch*(access.getHeight()-1);
338 	const tcu::IVec3	pitch			(access.getPixelPitch(), -rowPitch, access.getSlicePitch());
339 
340 	return PixelBufferAccess(access.getFormat(), access.getSize(), pitch, (deUint8*)access.getDataPtr() + offsetToLast);
341 }
342 
343 /*--------------------------------------------------------------------*//*!
344  * \brief Flip rows in Y direction
345  * \param access Access object
346  * \return Modified access object where Y coordinates are reversed
347  *//*--------------------------------------------------------------------*/
flipYAccess(const ConstPixelBufferAccess & access)348 ConstPixelBufferAccess flipYAccess (const ConstPixelBufferAccess& access)
349 {
350 	const int			rowPitch		= access.getRowPitch();
351 	const int			offsetToLast	= rowPitch*(access.getHeight()-1);
352 	const tcu::IVec3	pitch			(access.getPixelPitch(), -rowPitch, access.getSlicePitch());
353 
354 	return ConstPixelBufferAccess(access.getFormat(), access.getSize(), pitch, (deUint8*)access.getDataPtr() + offsetToLast);
355 }
356 
getFloatChannelValueRange(TextureFormat::ChannelType channelType)357 static Vec2 getFloatChannelValueRange (TextureFormat::ChannelType channelType)
358 {
359 	// make sure this table is updated if format table is updated
360 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
361 
362 	float cMin = 0.0f;
363 	float cMax = 0.0f;
364 
365 	switch (channelType)
366 	{
367 		// Signed normalized formats.
368 		case TextureFormat::SNORM_INT8:
369 		case TextureFormat::SNORM_INT16:
370 		case TextureFormat::SNORM_INT32:
371 		case TextureFormat::SNORM_INT_1010102_REV:			cMin = -1.0f;			cMax = 1.0f;			break;
372 
373 		// Unsigned normalized formats.
374 		case TextureFormat::UNORM_INT8:
375 		case TextureFormat::UNORM_INT16:
376 		case TextureFormat::UNORM_INT24:
377 		case TextureFormat::UNORM_INT32:
378 		case TextureFormat::UNORM_BYTE_44:
379 		case TextureFormat::UNORM_SHORT_565:
380 		case TextureFormat::UNORM_SHORT_555:
381 		case TextureFormat::UNORM_SHORT_4444:
382 		case TextureFormat::UNORM_SHORT_5551:
383 		case TextureFormat::UNORM_SHORT_1555:
384 		case TextureFormat::UNORM_INT_101010:
385 		case TextureFormat::UNORM_INT_1010102_REV:
386 		case TextureFormat::UNORM_SHORT_10:
387 		case TextureFormat::UNORM_SHORT_12:					cMin = 0.0f;			cMax = 1.0f;			break;
388 
389 		// Misc formats.
390 		case TextureFormat::SIGNED_INT8:					cMin = -128.0f;			cMax = 127.0f;			break;
391 		case TextureFormat::SIGNED_INT16:					cMin = -32768.0f;		cMax = 32767.0f;		break;
392 		case TextureFormat::SIGNED_INT32:					cMin = -2147483520.0f;	cMax = 2147483520.0f;	break; // Maximum exactly representable 31-bit integer: (2^24 - 1) * 2^7
393 		case TextureFormat::UNSIGNED_INT8:					cMin = 0.0f;			cMax = 255.0f;			break;
394 		case TextureFormat::UNSIGNED_INT16:					cMin = 0.0f;			cMax = 65535.0f;		break;
395 		case TextureFormat::UNSIGNED_INT24:					cMin = 0.0f;			cMax = 16777215.0f;		break;
396 		case TextureFormat::UNSIGNED_INT32:					cMin = 0.0f;			cMax = 4294967040.f;	break; // Maximum exactly representable 32-bit integer: (2^24 - 1) * 2^8
397 		case TextureFormat::HALF_FLOAT:						cMin = -1e3f;			cMax = 1e3f;			break;
398 		case TextureFormat::FLOAT:							cMin = -1e5f;			cMax = 1e5f;			break;
399 		case TextureFormat::FLOAT64:						cMin = -1e5f;			cMax = 1e5f;			break;
400 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	cMin = 0.0f;			cMax = 1e4f;			break;
401 		case TextureFormat::UNSIGNED_INT_999_E5_REV:		cMin = 0.0f;			cMax = 1e5f;			break;
402 		case TextureFormat::UNSIGNED_BYTE_44:				cMin = 0.0f;			cMax = 15.f;			break;
403 		case TextureFormat::UNSIGNED_SHORT_4444:			cMin = 0.0f;			cMax = 15.f;			break;
404 		case TextureFormat::USCALED_INT8:					cMin = 0.0f;			cMax = 255.0f;			break;
405 		case TextureFormat::USCALED_INT16:					cMin = 0.0f;			cMax = 65535.0f;		break;
406 		case TextureFormat::SSCALED_INT8:					cMin = -128.0f;			cMax = 127.0f;			break;
407 		case TextureFormat::SSCALED_INT16:					cMin = -32768.0f;		cMax = 32767.0f;		break;
408 		case TextureFormat::USCALED_INT_1010102_REV:		cMin = 0.0f;			cMax = 1023.0f;			break;
409 		case TextureFormat::SSCALED_INT_1010102_REV:		cMin = -512.0f;			cMax = 511.0f;			break;
410 
411 		default:
412 			DE_ASSERT(false);
413 	}
414 
415 	return Vec2(cMin, cMax);
416 }
417 
418 /*--------------------------------------------------------------------*//*!
419  * \brief Get standard parameters for testing texture format
420  *
421  * Returns TextureFormatInfo that describes good parameters for exercising
422  * given TextureFormat. Parameters include value ranges per channel and
423  * suitable lookup scaling and bias in order to reduce result back to
424  * 0..1 range.
425  *//*--------------------------------------------------------------------*/
getTextureFormatInfo(const TextureFormat & format)426 TextureFormatInfo getTextureFormatInfo (const TextureFormat& format)
427 {
428 	// Special cases.
429 	if (format.type == TextureFormat::UNSIGNED_INT_1010102_REV)
430 		return TextureFormatInfo(Vec4(	     0.0f,		    0.0f,		    0.0f,		 0.0f),
431 								 Vec4(	  1023.0f,		 1023.0f,		 1023.0f,		 3.0f),
432 								 Vec4(1.0f/1023.f,	1.0f/1023.0f,	1.0f/1023.0f,	1.0f/3.0f),
433 								 Vec4(	     0.0f,		    0.0f,		    0.0f,		 0.0f));
434 	if (format.type == TextureFormat::SIGNED_INT_1010102_REV)
435 		return TextureFormatInfo(Vec4(	  -512.0f,		 -512.0f,		 -512.0f,		-2.0f),
436 								 Vec4(	   511.0f,		  511.0f,		  511.0f,		 1.0f),
437 								 Vec4(1.0f/1023.f,	1.0f/1023.0f,	1.0f/1023.0f,	1.0f/3.0f),
438 								 Vec4(	     0.5f,		    0.5f,		    0.5f,		 0.5f));
439 	else if (format.order == TextureFormat::D || format.order == TextureFormat::DS)
440 		return TextureFormatInfo(Vec4(0.0f,	0.0f,	0.0f,	0.0f),
441 								 Vec4(1.0f,	1.0f,	1.0f,	0.0f),
442 								 Vec4(1.0f,	1.0f,	1.0f,	1.0f),
443 								 Vec4(0.0f,	0.0f,	0.0f,	0.0f)); // Depth / stencil formats.
444 	else if (format == TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_SHORT_5551))
445 		return TextureFormatInfo(Vec4(0.0f, 0.0f, 0.0f, 0.5f),
446 								 Vec4(1.0f, 1.0f, 1.0f, 1.5f),
447 								 Vec4(1.0f, 1.0f, 1.0f, 1.0f),
448 								 Vec4(0.0f, 0.0f, 0.0f, 0.0f));
449 	else if (format.type == TextureFormat::UNSIGNED_SHORT_5551)
450 		return TextureFormatInfo(Vec4(	   0.0f,		  0.0f,		  0.0f,	0.0f),
451 								 Vec4(	  31.0f,		 31.0f,		 31.0f,	1.0f),
452 								 Vec4(1.0f/31.f,	1.0f/31.0f,	1.0f/31.0f,	1.0f),
453 								 Vec4(	   0.0f,		  0.0f,		  0.0f,	0.0f));
454 	else if (format.type == TextureFormat::UNSIGNED_SHORT_565)
455 		return TextureFormatInfo(Vec4(	   0.0f,		  0.0f,		  0.0f,	0.0f),
456 								 Vec4(	  31.0f,		 63.0f,		 31.0f,	0.0f),
457 								 Vec4(1.0f/31.f,	1.0f/63.0f,	1.0f/31.0f,	1.0f),
458 								 Vec4(	   0.0f,		  0.0f,		  0.0f,	0.0f));
459 
460 	const Vec2						cRange		= getFloatChannelValueRange(format.type);
461 	const TextureSwizzle::Channel*	map			= getChannelReadSwizzle(format.order).components;
462 	const BVec4						chnMask		= BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
463 														deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
464 														deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
465 														deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
466 	const float						scale		= 1.0f / (cRange[1] - cRange[0]);
467 	const float						bias		= -cRange[0] * scale;
468 
469 	return TextureFormatInfo(select(cRange[0],	0.0f, chnMask),
470 							 select(cRange[1],	0.0f, chnMask),
471 							 select(scale,		1.0f, chnMask),
472 							 select(bias,		0.0f, chnMask));
473 }
474 
getFormatMinIntValue(const TextureFormat & format)475 IVec4 getFormatMinIntValue (const TextureFormat& format)
476 {
477 	DE_ASSERT(getTextureChannelClass(format.type) == TEXTURECHANNELCLASS_SIGNED_INTEGER);
478 
479 	switch (format.type)
480 	{
481 		case TextureFormat::SIGNED_INT8:	return IVec4(std::numeric_limits<deInt8>::min());
482 		case TextureFormat::SIGNED_INT16:	return IVec4(std::numeric_limits<deInt16>::min());
483 		case TextureFormat::SIGNED_INT32:	return IVec4(std::numeric_limits<deInt32>::min());
484 
485 		default:
486 			DE_FATAL("Invalid channel type");
487 			return IVec4(0);
488 	}
489 }
490 
getFormatMaxIntValue(const TextureFormat & format)491 IVec4 getFormatMaxIntValue (const TextureFormat& format)
492 {
493 	DE_ASSERT(getTextureChannelClass(format.type) == TEXTURECHANNELCLASS_SIGNED_INTEGER);
494 
495 	if (format == TextureFormat(TextureFormat::RGBA, TextureFormat::SIGNED_INT_1010102_REV)  ||
496 		format == TextureFormat(TextureFormat::BGRA, TextureFormat::SSCALED_INT_1010102_REV) ||
497 		format == TextureFormat(TextureFormat::RGBA, TextureFormat::SSCALED_INT_1010102_REV) ||
498 		format == TextureFormat(TextureFormat::BGRA, TextureFormat::SIGNED_INT_1010102_REV))
499 		return IVec4(511, 511, 511, 1);
500 
501 	switch (format.type)
502 	{
503 		case TextureFormat::SIGNED_INT8:	return IVec4(std::numeric_limits<deInt8>::max());
504 		case TextureFormat::SIGNED_INT16:	return IVec4(std::numeric_limits<deInt16>::max());
505 		case TextureFormat::SIGNED_INT32:	return IVec4(std::numeric_limits<deInt32>::max());
506 
507 		case TextureFormat::SSCALED_INT8:	return IVec4(std::numeric_limits<deInt8>::max());
508 		case TextureFormat::SSCALED_INT16:	return IVec4(std::numeric_limits<deInt16>::max());
509 
510 		default:
511 			DE_FATAL("Invalid channel type");
512 			return IVec4(0);
513 	}
514 }
515 
getFormatMaxUintValue(const TextureFormat & format)516 UVec4 getFormatMaxUintValue (const TextureFormat& format)
517 {
518 	DE_ASSERT(getTextureChannelClass(format.type) == TEXTURECHANNELCLASS_UNSIGNED_INTEGER);
519 
520 	if (format == TextureFormat(TextureFormat::RGBA, TextureFormat::UNSIGNED_INT_1010102_REV) ||
521 		format == TextureFormat(TextureFormat::RGBA, TextureFormat::USCALED_INT_1010102_REV)  ||
522 		format == TextureFormat(TextureFormat::BGRA, TextureFormat::USCALED_INT_1010102_REV)  ||
523 		format == TextureFormat(TextureFormat::BGRA, TextureFormat::UNSIGNED_INT_1010102_REV))
524 		return UVec4(1023u, 1023u, 1023u, 3u);
525 
526 	switch (format.type)
527 	{
528 		case TextureFormat::UNSIGNED_INT8:	return UVec4(std::numeric_limits<deUint8>::max());
529 		case TextureFormat::UNSIGNED_INT16:	return UVec4(std::numeric_limits<deUint16>::max());
530 		case TextureFormat::UNSIGNED_INT24:	return UVec4(0xffffffu);
531 		case TextureFormat::UNSIGNED_INT32:	return UVec4(std::numeric_limits<deUint32>::max());
532 
533 		case TextureFormat::USCALED_INT8:	return UVec4(std::numeric_limits<deUint8>::max());
534 		case TextureFormat::USCALED_INT16:	return UVec4(std::numeric_limits<deUint16>::max());
535 
536 		default:
537 			DE_FATAL("Invalid channel type");
538 			return UVec4(0);
539 	}
540 }
541 
getChannelBitDepth(TextureFormat::ChannelType channelType)542 static IVec4 getChannelBitDepth (TextureFormat::ChannelType channelType)
543 {
544 	// make sure this table is updated if format table is updated
545 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
546 
547 	switch (channelType)
548 	{
549 		case TextureFormat::SNORM_INT8:						return IVec4(8);
550 		case TextureFormat::SNORM_INT16:					return IVec4(16);
551 		case TextureFormat::SNORM_INT32:					return IVec4(32);
552 		case TextureFormat::UNORM_INT8:						return IVec4(8);
553 		case TextureFormat::UNORM_INT16:					return IVec4(16);
554 		case TextureFormat::UNORM_INT24:					return IVec4(24);
555 		case TextureFormat::UNORM_INT32:					return IVec4(32);
556 		case TextureFormat::UNORM_BYTE_44:					return IVec4(4,4,0,0);
557 		case TextureFormat::UNORM_SHORT_565:				return IVec4(5,6,5,0);
558 		case TextureFormat::UNORM_SHORT_4444:				return IVec4(4);
559 		case TextureFormat::UNORM_SHORT_555:				return IVec4(5,5,5,0);
560 		case TextureFormat::UNORM_SHORT_5551:				return IVec4(5,5,5,1);
561 		case TextureFormat::UNORM_SHORT_1555:				return IVec4(1,5,5,5);
562 		case TextureFormat::UNSIGNED_BYTE_44:				return IVec4(4,4,0,0);
563 		case TextureFormat::UNSIGNED_SHORT_565:				return IVec4(5,6,5,0);
564 		case TextureFormat::UNSIGNED_SHORT_4444:			return IVec4(4);
565 		case TextureFormat::UNSIGNED_SHORT_5551:			return IVec4(5,5,5,1);
566 		case TextureFormat::UNORM_INT_101010:				return IVec4(10,10,10,0);
567 		case TextureFormat::SNORM_INT_1010102_REV:			return IVec4(10,10,10,2);
568 		case TextureFormat::UNORM_INT_1010102_REV:			return IVec4(10,10,10,2);
569 		case TextureFormat::SIGNED_INT8:					return IVec4(8);
570 		case TextureFormat::SIGNED_INT16:					return IVec4(16);
571 		case TextureFormat::SIGNED_INT32:					return IVec4(32);
572 		case TextureFormat::UNSIGNED_INT8:					return IVec4(8);
573 		case TextureFormat::UNSIGNED_INT16:					return IVec4(16);
574 		case TextureFormat::UNSIGNED_INT24:					return IVec4(24);
575 		case TextureFormat::UNSIGNED_INT32:					return IVec4(32);
576 		case TextureFormat::SIGNED_INT_1010102_REV:			return IVec4(10,10,10,2);
577 		case TextureFormat::UNSIGNED_INT_1010102_REV:		return IVec4(10,10,10,2);
578 		case TextureFormat::UNSIGNED_INT_16_8_8:			return IVec4(16,8,0,0);
579 		case TextureFormat::UNSIGNED_INT_24_8:				return IVec4(24,8,0,0);
580 		case TextureFormat::UNSIGNED_INT_24_8_REV:			return IVec4(24,8,0,0);
581 		case TextureFormat::HALF_FLOAT:						return IVec4(16);
582 		case TextureFormat::FLOAT:							return IVec4(32);
583 		case TextureFormat::FLOAT64:						return IVec4(64);
584 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	return IVec4(11,11,10,0);
585 		case TextureFormat::UNSIGNED_INT_999_E5_REV:		return IVec4(9,9,9,0);
586 		case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:	return IVec4(32,8,0,0);
587 		case TextureFormat::UNORM_SHORT_10:					return IVec4(10);
588 		case TextureFormat::UNORM_SHORT_12:					return IVec4(12);
589 		case TextureFormat::USCALED_INT8:					return IVec4(8);
590 		case TextureFormat::USCALED_INT16:					return IVec4(16);
591 		case TextureFormat::SSCALED_INT8:					return IVec4(8);
592 		case TextureFormat::SSCALED_INT16:					return IVec4(16);
593 		case TextureFormat::USCALED_INT_1010102_REV:		return IVec4(10,10,10,2);
594 		case TextureFormat::SSCALED_INT_1010102_REV:		return IVec4(10,10,10,2);
595 		default:
596 			DE_ASSERT(false);
597 			return IVec4(0);
598 	}
599 }
600 
getTextureFormatBitDepth(const TextureFormat & format)601 IVec4 getTextureFormatBitDepth (const TextureFormat& format)
602 {
603 	const IVec4						chnBits		= getChannelBitDepth(format.type);
604 	const TextureSwizzle::Channel*	map			= getChannelReadSwizzle(format.order).components;
605 	const BVec4						chnMask		= BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
606 														deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
607 														deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
608 														deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
609 	const IVec4						chnSwz		= IVec4((chnMask[0]) ? ((int)map[0]) : (0),
610 														(chnMask[1]) ? ((int)map[1]) : (0),
611 														(chnMask[2]) ? ((int)map[2]) : (0),
612 														(chnMask[3]) ? ((int)map[3]) : (0));
613 
614 	return select(chnBits.swizzle(chnSwz.x(), chnSwz.y(), chnSwz.z(), chnSwz.w()), IVec4(0), chnMask);
615 }
616 
getChannelMantissaBitDepth(TextureFormat::ChannelType channelType)617 static IVec4 getChannelMantissaBitDepth (TextureFormat::ChannelType channelType)
618 {
619 	// make sure this table is updated if format table is updated
620 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
621 
622 	switch (channelType)
623 	{
624 		case TextureFormat::SNORM_INT8:
625 		case TextureFormat::SNORM_INT16:
626 		case TextureFormat::SNORM_INT32:
627 		case TextureFormat::UNORM_INT8:
628 		case TextureFormat::UNORM_INT16:
629 		case TextureFormat::UNORM_INT24:
630 		case TextureFormat::UNORM_INT32:
631 		case TextureFormat::UNORM_BYTE_44:
632 		case TextureFormat::UNORM_SHORT_565:
633 		case TextureFormat::UNORM_SHORT_4444:
634 		case TextureFormat::UNORM_SHORT_555:
635 		case TextureFormat::UNORM_SHORT_5551:
636 		case TextureFormat::UNORM_SHORT_1555:
637 		case TextureFormat::UNSIGNED_BYTE_44:
638 		case TextureFormat::UNSIGNED_SHORT_565:
639 		case TextureFormat::UNSIGNED_SHORT_4444:
640 		case TextureFormat::UNSIGNED_SHORT_5551:
641 		case TextureFormat::UNORM_INT_101010:
642 		case TextureFormat::SNORM_INT_1010102_REV:
643 		case TextureFormat::UNORM_INT_1010102_REV:
644 		case TextureFormat::SIGNED_INT8:
645 		case TextureFormat::SIGNED_INT16:
646 		case TextureFormat::SIGNED_INT32:
647 		case TextureFormat::UNSIGNED_INT8:
648 		case TextureFormat::UNSIGNED_INT16:
649 		case TextureFormat::UNSIGNED_INT24:
650 		case TextureFormat::UNSIGNED_INT32:
651 		case TextureFormat::SIGNED_INT_1010102_REV:
652 		case TextureFormat::UNSIGNED_INT_1010102_REV:
653 		case TextureFormat::UNSIGNED_INT_16_8_8:
654 		case TextureFormat::UNSIGNED_INT_24_8:
655 		case TextureFormat::UNSIGNED_INT_24_8_REV:
656 		case TextureFormat::UNSIGNED_INT_999_E5_REV:
657 		case TextureFormat::UNORM_SHORT_10:
658 		case TextureFormat::UNORM_SHORT_12:
659 		case TextureFormat::USCALED_INT8:
660 		case TextureFormat::USCALED_INT16:
661 		case TextureFormat::SSCALED_INT8:
662 		case TextureFormat::SSCALED_INT16:
663 		case TextureFormat::USCALED_INT_1010102_REV:
664 		case TextureFormat::SSCALED_INT_1010102_REV:
665 			return getChannelBitDepth(channelType);
666 
667 		case TextureFormat::HALF_FLOAT:						return IVec4(10);
668 		case TextureFormat::FLOAT:							return IVec4(23);
669 		case TextureFormat::FLOAT64:						return IVec4(52);
670 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	return IVec4(6,6,5,0);
671 		case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:	return IVec4(23,8,0,0);
672 		default:
673 			DE_ASSERT(false);
674 			return IVec4(0);
675 	}
676 }
677 
getTextureFormatMantissaBitDepth(const TextureFormat & format)678 IVec4 getTextureFormatMantissaBitDepth (const TextureFormat& format)
679 {
680 	const IVec4						chnBits		= getChannelMantissaBitDepth(format.type);
681 	const TextureSwizzle::Channel*	map			= getChannelReadSwizzle(format.order).components;
682 	const BVec4						chnMask		= BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
683 														deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
684 														deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
685 														deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
686 	const IVec4						chnSwz		= IVec4((chnMask[0]) ? ((int)map[0]) : (0),
687 														(chnMask[1]) ? ((int)map[1]) : (0),
688 														(chnMask[2]) ? ((int)map[2]) : (0),
689 														(chnMask[3]) ? ((int)map[3]) : (0));
690 
691 	return select(chnBits.swizzle(chnSwz.x(), chnSwz.y(), chnSwz.z(), chnSwz.w()), IVec4(0), chnMask);
692 }
693 
getTextureFormatChannelMask(const TextureFormat & format)694 BVec4 getTextureFormatChannelMask (const TextureFormat& format)
695 {
696 	const TextureSwizzle::Channel* const map = getChannelReadSwizzle(format.order).components;
697 	return BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
698 				 deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
699 				 deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
700 				 deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
701 }
702 
linearInterpolate(float t,float minVal,float maxVal)703 static inline float linearInterpolate (float t, float minVal, float maxVal)
704 {
705 	return minVal + (maxVal - minVal) * t;
706 }
707 
linearInterpolate(float t,const Vec4 & a,const Vec4 & b)708 static inline Vec4 linearInterpolate (float t, const Vec4& a, const Vec4& b)
709 {
710 	return a + (b - a) * t;
711 }
712 
713 enum
714 {
715 	CLEAR_OPTIMIZE_THRESHOLD		= 128,
716 	CLEAR_OPTIMIZE_MAX_PIXEL_SIZE	= 8
717 };
718 
fillRow(const PixelBufferAccess & dst,int y,int z,int pixelSize,const deUint8 * pixel)719 inline void fillRow (const PixelBufferAccess& dst, int y, int z, int pixelSize, const deUint8* pixel)
720 {
721 	DE_ASSERT(dst.getPixelPitch() == pixelSize); // only tightly packed
722 
723 	deUint8*	dstPtr	= (deUint8*)dst.getPixelPtr(0, y, z);
724 	int			width	= dst.getWidth();
725 
726 	if (pixelSize == 8 && deIsAlignedPtr(dstPtr, pixelSize))
727 	{
728 		deUint64 val;
729 		memcpy(&val, pixel, sizeof(val));
730 
731 		for (int i = 0; i < width; i++)
732 			((deUint64*)dstPtr)[i] = val;
733 	}
734 	else if (pixelSize == 4 && deIsAlignedPtr(dstPtr, pixelSize))
735 	{
736 		deUint32 val;
737 		memcpy(&val, pixel, sizeof(val));
738 
739 		for (int i = 0; i < width; i++)
740 			((deUint32*)dstPtr)[i] = val;
741 	}
742 	else
743 	{
744 		for (int i = 0; i < width; i++)
745 			for (int j = 0; j < pixelSize; j++)
746 				dstPtr[i*pixelSize+j] = pixel[j];
747 	}
748 }
749 
clear(const PixelBufferAccess & access,const Vec4 & color)750 void clear (const PixelBufferAccess& access, const Vec4& color)
751 {
752 	const int	pixelSize				= access.getFormat().getPixelSize();
753 	const int	pixelPitch				= access.getPixelPitch();
754 	const bool	rowPixelsTightlyPacked	= (pixelSize == pixelPitch);
755 
756 	if (access.getWidth()*access.getHeight()*access.getDepth() >= CLEAR_OPTIMIZE_THRESHOLD &&
757 		pixelSize < CLEAR_OPTIMIZE_MAX_PIXEL_SIZE && rowPixelsTightlyPacked)
758 	{
759 		// Convert to destination format.
760 		union
761 		{
762 			deUint8		u8[CLEAR_OPTIMIZE_MAX_PIXEL_SIZE];
763 			deUint64	u64; // Forces 64-bit alignment.
764 		} pixel;
765 		DE_STATIC_ASSERT(sizeof(pixel) == CLEAR_OPTIMIZE_MAX_PIXEL_SIZE);
766 		PixelBufferAccess(access.getFormat(), 1, 1, 1, 0, 0, &pixel.u8[0]).setPixel(color, 0, 0);
767 
768 		for (int z = 0; z < access.getDepth(); z++)
769 			for (int y = 0; y < access.getHeight(); y++)
770 				fillRow(access, y, z, pixelSize, &pixel.u8[0]);
771 	}
772 	else
773 	{
774 		for (int z = 0; z < access.getDepth(); z++)
775 			for (int y = 0; y < access.getHeight(); y++)
776 				for (int x = 0; x < access.getWidth(); x++)
777 					access.setPixel(color, x, y, z);
778 	}
779 }
780 
clear(const PixelBufferAccess & access,const IVec4 & color)781 void clear (const PixelBufferAccess& access, const IVec4& color)
782 {
783 	const int	pixelSize				= access.getFormat().getPixelSize();
784 	const int	pixelPitch				= access.getPixelPitch();
785 	const bool	rowPixelsTightlyPacked	= (pixelSize == pixelPitch);
786 
787 	if (access.getWidth()*access.getHeight()*access.getDepth() >= CLEAR_OPTIMIZE_THRESHOLD &&
788 		pixelSize < CLEAR_OPTIMIZE_MAX_PIXEL_SIZE && rowPixelsTightlyPacked)
789 	{
790 		// Convert to destination format.
791 		union
792 		{
793 			deUint8		u8[CLEAR_OPTIMIZE_MAX_PIXEL_SIZE];
794 			deUint64	u64; // Forces 64-bit alignment.
795 		} pixel;
796 		DE_STATIC_ASSERT(sizeof(pixel) == CLEAR_OPTIMIZE_MAX_PIXEL_SIZE);
797 		PixelBufferAccess(access.getFormat(), 1, 1, 1, 0, 0, &pixel.u8[0]).setPixel(color, 0, 0);
798 
799 		for (int z = 0; z < access.getDepth(); z++)
800 			for (int y = 0; y < access.getHeight(); y++)
801 				fillRow(access, y, z, pixelSize, &pixel.u8[0]);
802 	}
803 	else
804 	{
805 		for (int z = 0; z < access.getDepth(); z++)
806 			for (int y = 0; y < access.getHeight(); y++)
807 				for (int x = 0; x < access.getWidth(); x++)
808 					access.setPixel(color, x, y, z);
809 	}
810 }
811 
clear(const PixelBufferAccess & access,const UVec4 & color)812 void clear (const PixelBufferAccess& access, const UVec4& color)
813 {
814 	clear(access, color.cast<deInt32>());
815 }
816 
clearDepth(const PixelBufferAccess & access,float depth)817 void clearDepth (const PixelBufferAccess& access, float depth)
818 {
819 	DE_ASSERT(access.getFormat().order == TextureFormat::DS || access.getFormat().order == TextureFormat::D);
820 
821 	clear(getEffectiveDepthStencilAccess(access, Sampler::MODE_DEPTH), tcu::Vec4(depth, 0.0f, 0.0f, 0.0f));
822 }
823 
clearStencil(const PixelBufferAccess & access,int stencil)824 void clearStencil (const PixelBufferAccess& access, int stencil)
825 {
826 	DE_ASSERT(access.getFormat().order == TextureFormat::DS || access.getFormat().order == TextureFormat::S);
827 
828 	clear(getEffectiveDepthStencilAccess(access, Sampler::MODE_STENCIL), tcu::UVec4(stencil, 0u, 0u, 0u));
829 }
830 
fillWithComponentGradients1D(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)831 static void fillWithComponentGradients1D (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
832 {
833 	DE_ASSERT(access.getHeight() == 1);
834 	for (int x = 0; x < access.getWidth(); x++)
835 	{
836 		float s = ((float)x + 0.5f) / (float)access.getWidth();
837 
838 		float r = linearInterpolate(s, minVal.x(), maxVal.x());
839 		float g = linearInterpolate(s, minVal.y(), maxVal.y());
840 		float b = linearInterpolate(s, minVal.z(), maxVal.z());
841 		float a = linearInterpolate(s, minVal.w(), maxVal.w());
842 
843 		access.setPixel(tcu::Vec4(r, g, b, a), x, 0);
844 	}
845 }
846 
fillWithComponentGradients2D(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)847 static void fillWithComponentGradients2D (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
848 {
849 	for (int y = 0; y < access.getHeight(); y++)
850 	{
851 		for (int x = 0; x < access.getWidth(); x++)
852 		{
853 			float s = ((float)x + 0.5f) / (float)access.getWidth();
854 			float t = ((float)y + 0.5f) / (float)access.getHeight();
855 
856 			float r = linearInterpolate((      s  +       t) *0.5f, minVal.x(), maxVal.x());
857 			float g = linearInterpolate((      s  + (1.0f-t))*0.5f, minVal.y(), maxVal.y());
858 			float b = linearInterpolate(((1.0f-s) +       t) *0.5f, minVal.z(), maxVal.z());
859 			float a = linearInterpolate(((1.0f-s) + (1.0f-t))*0.5f, minVal.w(), maxVal.w());
860 
861 			access.setPixel(tcu::Vec4(r, g, b, a), x, y);
862 		}
863 	}
864 }
865 
fillWithComponentGradients3D(const PixelBufferAccess & dst,const Vec4 & minVal,const Vec4 & maxVal)866 static void fillWithComponentGradients3D (const PixelBufferAccess& dst, const Vec4& minVal, const Vec4& maxVal)
867 {
868 	for (int z = 0; z < dst.getDepth(); z++)
869 	{
870 		for (int y = 0; y < dst.getHeight(); y++)
871 		{
872 			for (int x = 0; x < dst.getWidth(); x++)
873 			{
874 				float s = ((float)x + 0.5f) / (float)dst.getWidth();
875 				float t = ((float)y + 0.5f) / (float)dst.getHeight();
876 				float p = ((float)z + 0.5f) / (float)dst.getDepth();
877 
878 				float r = linearInterpolate(s,						minVal.x(), maxVal.x());
879 				float g = linearInterpolate(t,						minVal.y(), maxVal.y());
880 				float b = linearInterpolate(p,						minVal.z(), maxVal.z());
881 				float a = linearInterpolate(1.0f - (s+t+p)/3.0f,	minVal.w(), maxVal.w());
882 
883 				dst.setPixel(tcu::Vec4(r, g, b, a), x, y, z);
884 			}
885 		}
886 	}
887 }
888 
fillWithComponentGradients(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)889 void fillWithComponentGradients (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
890 {
891 	if (isCombinedDepthStencilType(access.getFormat().type))
892 	{
893 		const bool hasDepth		= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::D;
894 		const bool hasStencil	= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::S;
895 
896 		DE_ASSERT(hasDepth || hasStencil);
897 
898 		// For combined formats, treat D and S as separate channels
899 		if (hasDepth)
900 			fillWithComponentGradients(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_DEPTH), minVal, maxVal);
901 		if (hasStencil)
902 			fillWithComponentGradients(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_STENCIL), minVal.swizzle(3,2,1,0), maxVal.swizzle(3,2,1,0));
903 	}
904 	else
905 	{
906 		if (access.getHeight() == 1 && access.getDepth() == 1)
907 			fillWithComponentGradients1D(access, minVal, maxVal);
908 		else if (access.getDepth() == 1)
909 			fillWithComponentGradients2D(access, minVal, maxVal);
910 		else
911 			fillWithComponentGradients3D(access, minVal, maxVal);
912 	}
913 }
914 
fillWithGrid1D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)915 static void fillWithGrid1D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
916 {
917 	for (int x = 0; x < access.getWidth(); x++)
918 	{
919 		int mx = (x / cellSize) % 2;
920 
921 		if (mx)
922 			access.setPixel(colorB, x, 0);
923 		else
924 			access.setPixel(colorA, x, 0);
925 	}
926 }
927 
fillWithGrid2D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)928 static void fillWithGrid2D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
929 {
930 	for (int y = 0; y < access.getHeight(); y++)
931 	{
932 		for (int x = 0; x < access.getWidth(); x++)
933 		{
934 			int mx = (x / cellSize) % 2;
935 			int my = (y / cellSize) % 2;
936 
937 			if (mx ^ my)
938 				access.setPixel(colorB, x, y);
939 			else
940 				access.setPixel(colorA, x, y);
941 		}
942 	}
943 }
944 
fillWithGrid3D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)945 static void fillWithGrid3D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
946 {
947 	for (int z = 0; z < access.getDepth(); z++)
948 	{
949 		for (int y = 0; y < access.getHeight(); y++)
950 		{
951 			for (int x = 0; x < access.getWidth(); x++)
952 			{
953 				int mx = (x / cellSize) % 2;
954 				int my = (y / cellSize) % 2;
955 				int mz = (z / cellSize) % 2;
956 
957 				if (mx ^ my ^ mz)
958 					access.setPixel(colorB, x, y, z);
959 				else
960 					access.setPixel(colorA, x, y, z);
961 			}
962 		}
963 	}
964 }
965 
fillWithGrid(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)966 void fillWithGrid (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
967 {
968 	if (isCombinedDepthStencilType(access.getFormat().type))
969 	{
970 		const bool hasDepth		= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::D;
971 		const bool hasStencil	= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::S;
972 
973 		DE_ASSERT(hasDepth || hasStencil);
974 
975 		// For combined formats, treat D and S as separate channels
976 		if (hasDepth)
977 			fillWithGrid(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_DEPTH), cellSize, colorA, colorB);
978 		if (hasStencil)
979 			fillWithGrid(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_STENCIL), cellSize, colorA.swizzle(3,2,1,0), colorB.swizzle(3,2,1,0));
980 	}
981 	else
982 	{
983 		if (access.getHeight() == 1 && access.getDepth() == 1)
984 			fillWithGrid1D(access, cellSize, colorA, colorB);
985 		else if (access.getDepth() == 1)
986 			fillWithGrid2D(access, cellSize, colorA, colorB);
987 		else
988 			fillWithGrid3D(access, cellSize, colorA, colorB);
989 	}
990 }
991 
fillWithRepeatableGradient(const PixelBufferAccess & access,const Vec4 & colorA,const Vec4 & colorB)992 void fillWithRepeatableGradient (const PixelBufferAccess& access, const Vec4& colorA, const Vec4& colorB)
993 {
994 	for (int y = 0; y < access.getHeight(); y++)
995 	{
996 		for (int x = 0; x < access.getWidth(); x++)
997 		{
998 			float s = ((float)x + 0.5f) / (float)access.getWidth();
999 			float t = ((float)y + 0.5f) / (float)access.getHeight();
1000 
1001 			float a = s > 0.5f ? (2.0f - 2.0f*s) : 2.0f*s;
1002 			float b = t > 0.5f ? (2.0f - 2.0f*t) : 2.0f*t;
1003 
1004 			float p = deFloatClamp(deFloatSqrt(a*a + b*b), 0.0f, 1.0f);
1005 			access.setPixel(linearInterpolate(p, colorA, colorB), x, y);
1006 		}
1007 	}
1008 }
1009 
fillWithRGBAQuads(const PixelBufferAccess & dst)1010 void fillWithRGBAQuads (const PixelBufferAccess& dst)
1011 {
1012 	TCU_CHECK_INTERNAL(dst.getDepth() == 1);
1013 	int width	= dst.getWidth();
1014 	int height	= dst.getHeight();
1015 	int	left	= width/2;
1016 	int top		= height/2;
1017 
1018 	clear(getSubregion(dst, 0,		0,		0, left,		top,		1),	Vec4(1.0f, 0.0f, 0.0f, 1.0f));
1019 	clear(getSubregion(dst, left,	0,		0, width-left,	top,		1),	Vec4(0.0f, 1.0f, 0.0f, 1.0f));
1020 	clear(getSubregion(dst, 0,		top,	0, left,		height-top,	1), Vec4(0.0f, 0.0f, 1.0f, 0.0f));
1021 	clear(getSubregion(dst, left,	top,	0, width-left,	height-top, 1), Vec4(0.5f, 0.5f, 0.5f, 1.0f));
1022 }
1023 
1024 // \todo [2012-11-13 pyry] There is much better metaballs code in CL SIR value generators.
fillWithMetaballs(const PixelBufferAccess & dst,int numBalls,deUint32 seed)1025 void fillWithMetaballs (const PixelBufferAccess& dst, int numBalls, deUint32 seed)
1026 {
1027 	TCU_CHECK_INTERNAL(dst.getDepth() == 1);
1028 	std::vector<Vec2>	points(numBalls);
1029 	de::Random			rnd(seed);
1030 
1031 	for (int i = 0; i < numBalls; i++)
1032 	{
1033 		float x = rnd.getFloat();
1034 		float y = rnd.getFloat();
1035 		points[i] = (Vec2(x, y));
1036 	}
1037 
1038 	for (int y = 0; y < dst.getHeight(); y++)
1039 	for (int x = 0; x < dst.getWidth(); x++)
1040 	{
1041 		Vec2 p((float)x/(float)dst.getWidth(), (float)y/(float)dst.getHeight());
1042 
1043 		float sum = 0.0f;
1044 		for (std::vector<Vec2>::const_iterator i = points.begin(); i != points.end(); i++)
1045 		{
1046 			Vec2	d = p - *i;
1047 			float	f = 0.01f / (d.x()*d.x() + d.y()*d.y());
1048 
1049 			sum += f;
1050 		}
1051 
1052 		dst.setPixel(Vec4(sum), x, y);
1053 	}
1054 }
1055 
copy(const PixelBufferAccess & dst,const ConstPixelBufferAccess & src,const bool clearUnused)1056 void copy (const PixelBufferAccess& dst, const ConstPixelBufferAccess& src, const bool clearUnused)
1057 {
1058 	DE_ASSERT(src.getSize() == dst.getSize());
1059 
1060 	const int	width				= dst.getWidth();
1061 	const int	height				= dst.getHeight();
1062 	const int	depth				= dst.getDepth();
1063 
1064 	const int	srcPixelSize		= src.getFormat().getPixelSize();
1065 	const int	dstPixelSize		= dst.getFormat().getPixelSize();
1066 	const int	srcPixelPitch		= src.getPixelPitch();
1067 	const int	dstPixelPitch		= dst.getPixelPitch();
1068 	const bool	srcTightlyPacked	= (srcPixelSize == srcPixelPitch);
1069 	const bool	dstTightlyPacked	= (dstPixelSize == dstPixelPitch);
1070 
1071 	const bool	srcHasDepth			= (src.getFormat().order == tcu::TextureFormat::DS || src.getFormat().order == tcu::TextureFormat::D);
1072 	const bool	srcHasStencil		= (src.getFormat().order == tcu::TextureFormat::DS || src.getFormat().order == tcu::TextureFormat::S);
1073 	const bool	dstHasDepth			= (dst.getFormat().order == tcu::TextureFormat::DS || dst.getFormat().order == tcu::TextureFormat::D);
1074 	const bool	dstHasStencil		= (dst.getFormat().order == tcu::TextureFormat::DS || dst.getFormat().order == tcu::TextureFormat::S);
1075 
1076 	if (src.getFormat() == dst.getFormat() && srcTightlyPacked && dstTightlyPacked)
1077 	{
1078 		// Fast-path for matching formats.
1079 		for (int z = 0; z < depth; z++)
1080 		for (int y = 0; y < height; y++)
1081 			deMemcpy(dst.getPixelPtr(0, y, z), src.getPixelPtr(0, y, z), srcPixelSize*width);
1082 	}
1083 	else if (src.getFormat() == dst.getFormat())
1084 	{
1085 		// Bit-exact copy for matching formats.
1086 		for (int z = 0; z < depth; z++)
1087 		for (int y = 0; y < height; y++)
1088 		for (int x = 0; x < width; x++)
1089 			deMemcpy(dst.getPixelPtr(x, y, z), src.getPixelPtr(x, y, z), srcPixelSize);
1090 	}
1091 	else if (srcHasDepth || srcHasStencil || dstHasDepth || dstHasStencil)
1092 	{
1093 		DE_ASSERT((srcHasDepth && dstHasDepth) || (srcHasStencil && dstHasStencil)); // must have at least one common channel
1094 
1095 		if (dstHasDepth && srcHasDepth)
1096 		{
1097 			for (int z = 0; z < depth; z++)
1098 			for (int y = 0; y < height; y++)
1099 			for (int x = 0; x < width; x++)
1100 				dst.setPixDepth(src.getPixDepth(x, y, z), x, y, z);
1101 		}
1102 		else if (dstHasDepth && !srcHasDepth && clearUnused)
1103 		{
1104 			// consistency with color copies
1105 			tcu::clearDepth(dst, 0.0f);
1106 		}
1107 
1108 		if (dstHasStencil && srcHasStencil)
1109 		{
1110 			for (int z = 0; z < depth; z++)
1111 			for (int y = 0; y < height; y++)
1112 			for (int x = 0; x < width; x++)
1113 				dst.setPixStencil(src.getPixStencil(x, y, z), x, y, z);
1114 		}
1115 		else if (dstHasStencil && !srcHasStencil && clearUnused)
1116 		{
1117 			// consistency with color copies
1118 			tcu::clearStencil(dst, 0u);
1119 		}
1120 	}
1121 	else
1122 	{
1123 		TextureChannelClass		srcClass	= getTextureChannelClass(src.getFormat().type);
1124 		TextureChannelClass		dstClass	= getTextureChannelClass(dst.getFormat().type);
1125 		bool					srcIsInt	= srcClass == TEXTURECHANNELCLASS_SIGNED_INTEGER || srcClass == TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
1126 		bool					dstIsInt	= dstClass == TEXTURECHANNELCLASS_SIGNED_INTEGER || dstClass == TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
1127 
1128 		if (srcIsInt && dstIsInt)
1129 		{
1130 			for (int z = 0; z < depth; z++)
1131 			for (int y = 0; y < height; y++)
1132 			for (int x = 0; x < width; x++)
1133 				dst.setPixel(src.getPixelInt(x, y, z), x, y, z);
1134 		}
1135 		else
1136 		{
1137 			for (int z = 0; z < depth; z++)
1138 			for (int y = 0; y < height; y++)
1139 			for (int x = 0; x < width; x++)
1140 				dst.setPixel(src.getPixel(x, y, z), x, y, z);
1141 		}
1142 	}
1143 }
1144 
scale(const PixelBufferAccess & dst,const ConstPixelBufferAccess & src,Sampler::FilterMode filter)1145 void scale (const PixelBufferAccess& dst, const ConstPixelBufferAccess& src, Sampler::FilterMode filter)
1146 {
1147 	DE_ASSERT(filter == Sampler::NEAREST || filter == Sampler::LINEAR);
1148 
1149 	Sampler sampler(Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE,
1150 					filter, filter, 0.0f, false);
1151 
1152 	float sX = (float)src.getWidth() / (float)dst.getWidth();
1153 	float sY = (float)src.getHeight() / (float)dst.getHeight();
1154 	float sZ = (float)src.getDepth() / (float)dst.getDepth();
1155 
1156 	if (dst.getDepth() == 1 && src.getDepth() == 1)
1157 	{
1158 		for (int y = 0; y < dst.getHeight(); y++)
1159 		for (int x = 0; x < dst.getWidth(); x++)
1160 			dst.setPixel(linearToSRGBIfNeeded(dst.getFormat(), src.sample2D(sampler, filter, ((float)x+0.5f)*sX, ((float)y+0.5f)*sY, 0)), x, y);
1161 	}
1162 	else
1163 	{
1164 		for (int z = 0; z < dst.getDepth(); z++)
1165 		for (int y = 0; y < dst.getHeight(); y++)
1166 		for (int x = 0; x < dst.getWidth(); x++)
1167 			dst.setPixel(linearToSRGBIfNeeded(dst.getFormat(), src.sample3D(sampler, filter, ((float)x+0.5f)*sX, ((float)y+0.5f)*sY, ((float)z+0.5f)*sZ)), x, y, z);
1168 	}
1169 }
1170 
estimatePixelValueRange(const ConstPixelBufferAccess & access,Vec4 & minVal,Vec4 & maxVal)1171 void estimatePixelValueRange (const ConstPixelBufferAccess& access, Vec4& minVal, Vec4& maxVal)
1172 {
1173 	const TextureFormat& format = access.getFormat();
1174 
1175 	switch (getTextureChannelClass(format.type))
1176 	{
1177 		case TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1178 			// Normalized unsigned formats.
1179 			minVal = Vec4(0.0f);
1180 			maxVal = Vec4(1.0f);
1181 			break;
1182 
1183 		case TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1184 			// Normalized signed formats.
1185 			minVal = Vec4(-1.0f);
1186 			maxVal = Vec4(+1.0f);
1187 			break;
1188 
1189 		default:
1190 			// \note Samples every 4/8th pixel.
1191 			minVal = Vec4(std::numeric_limits<float>::max());
1192 			maxVal = Vec4(std::numeric_limits<float>::min());
1193 
1194 			for (int z = 0; z < access.getDepth(); z += 2)
1195 			{
1196 				for (int y = 0; y < access.getHeight(); y += 2)
1197 				{
1198 					for (int x = 0; x < access.getWidth(); x += 2)
1199 					{
1200 						Vec4 p = access.getPixel(x, y, z);
1201 
1202 						minVal[0] = (deFloatIsNaN(p[0]) ? minVal[0] : de::min(minVal[0], p[0]));
1203 						minVal[1] = (deFloatIsNaN(p[1]) ? minVal[1] : de::min(minVal[1], p[1]));
1204 						minVal[2] = (deFloatIsNaN(p[2]) ? minVal[2] : de::min(minVal[2], p[2]));
1205 						minVal[3] = (deFloatIsNaN(p[3]) ? minVal[3] : de::min(minVal[3], p[3]));
1206 
1207 						maxVal[0] = (deFloatIsNaN(p[0]) ? maxVal[0] : de::max(maxVal[0], p[0]));
1208 						maxVal[1] = (deFloatIsNaN(p[1]) ? maxVal[1] : de::max(maxVal[1], p[1]));
1209 						maxVal[2] = (deFloatIsNaN(p[2]) ? maxVal[2] : de::max(maxVal[2], p[2]));
1210 						maxVal[3] = (deFloatIsNaN(p[3]) ? maxVal[3] : de::max(maxVal[3], p[3]));
1211 					}
1212 				}
1213 			}
1214 			break;
1215 	}
1216 }
1217 
computePixelScaleBias(const ConstPixelBufferAccess & access,Vec4 & scale,Vec4 & bias)1218 void computePixelScaleBias (const ConstPixelBufferAccess& access, Vec4& scale, Vec4& bias)
1219 {
1220 	Vec4 minVal, maxVal;
1221 	estimatePixelValueRange(access, minVal, maxVal);
1222 
1223 	const float eps = 0.0001f;
1224 
1225 	for (int c = 0; c < 4; c++)
1226 	{
1227 		if (maxVal[c] - minVal[c] < eps)
1228 		{
1229 			scale[c]	= (maxVal[c] < eps) ? 1.0f : (1.0f / maxVal[c]);
1230 			bias[c]		= (c == 3) ? (1.0f - maxVal[c]*scale[c]) : (0.0f - minVal[c]*scale[c]);
1231 		}
1232 		else
1233 		{
1234 			scale[c]	= 1.0f / (maxVal[c] - minVal[c]);
1235 			bias[c]		= 0.0f - minVal[c]*scale[c];
1236 		}
1237 	}
1238 }
1239 
getCubeArrayFaceIndex(CubeFace face)1240 int getCubeArrayFaceIndex (CubeFace face)
1241 {
1242 	DE_ASSERT((int)face >= 0 && face < CUBEFACE_LAST);
1243 
1244 	switch (face)
1245 	{
1246 		case CUBEFACE_POSITIVE_X:	return 0;
1247 		case CUBEFACE_NEGATIVE_X:	return 1;
1248 		case CUBEFACE_POSITIVE_Y:	return 2;
1249 		case CUBEFACE_NEGATIVE_Y:	return 3;
1250 		case CUBEFACE_POSITIVE_Z:	return 4;
1251 		case CUBEFACE_NEGATIVE_Z:	return 5;
1252 
1253 		default:
1254 			return -1;
1255 	}
1256 }
1257 
packRGB999E5(const tcu::Vec4 & color)1258 deUint32 packRGB999E5 (const tcu::Vec4& color)
1259 {
1260 	const int	mBits	= 9;
1261 	const int	eBits	= 5;
1262 	const int	eBias	= 15;
1263 	const int	eMax	= (1<<eBits)-1;
1264 	const float	maxVal	= (float)(((1<<mBits) - 1) * (1<<(eMax-eBias))) / (float)(1<<mBits);
1265 
1266 	float	rc		= deFloatClamp(color[0], 0.0f, maxVal);
1267 	float	gc		= deFloatClamp(color[1], 0.0f, maxVal);
1268 	float	bc		= deFloatClamp(color[2], 0.0f, maxVal);
1269 	float	maxc	= de::max(rc, de::max(gc, bc));
1270 	int		exps	= de::max(-eBias - 1, deFloorFloatToInt32(deFloatLog2(maxc))) + 1 + eBias;
1271 	float	e		= deFloatPow(2.0f, (float)(exps-eBias-mBits));
1272 	int		maxs	= deFloorFloatToInt32(maxc / e + 0.5f);
1273 
1274 	if (maxs == (1<<mBits))
1275 	{
1276 		exps++;
1277 		e *= 2.0f;
1278 	}
1279 
1280 	deUint32 rs = (deUint32)deFloorFloatToInt32(rc / e + 0.5f);
1281 	deUint32 gs = (deUint32)deFloorFloatToInt32(gc / e + 0.5f);
1282 	deUint32 bs = (deUint32)deFloorFloatToInt32(bc / e + 0.5f);
1283 
1284 	DE_ASSERT((exps & ~((1<<5)-1)) == 0);
1285 	DE_ASSERT((rs & ~((1<<9)-1)) == 0);
1286 	DE_ASSERT((gs & ~((1<<9)-1)) == 0);
1287 	DE_ASSERT((bs & ~((1<<9)-1)) == 0);
1288 
1289 	return rs | (gs << 9) | (bs << 18) | (exps << 27);
1290 }
1291 
1292 // Sampler utils
1293 
addOffset(const void * ptr,int numBytes)1294 static const void* addOffset (const void* ptr, int numBytes)
1295 {
1296 	return (const deUint8*)ptr + numBytes;
1297 }
1298 
addOffset(void * ptr,int numBytes)1299 static void* addOffset (void* ptr, int numBytes)
1300 {
1301 	return (deUint8*)ptr + numBytes;
1302 }
1303 
1304 template <typename AccessType>
toSamplerAccess(const AccessType & baseAccess,Sampler::DepthStencilMode mode)1305 static AccessType toSamplerAccess (const AccessType& baseAccess, Sampler::DepthStencilMode mode)
1306 {
1307 	// make sure to update this if type table is updated
1308 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
1309 
1310 	if (!isCombinedDepthStencilType(baseAccess.getFormat().type))
1311 		return baseAccess;
1312 	else
1313 	{
1314 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
1315 		const deUint32 uint32ByteOffsetBits0To8		= 0; //!< least significant byte in the lowest address
1316 		const deUint32 uint32ByteOffsetBits0To24	= 0;
1317 		const deUint32 uint32ByteOffsetBits8To32	= 1;
1318 		const deUint32 uint32ByteOffsetBits16To32	= 2;
1319 		const deUint32 uint32ByteOffsetBits24To32	= 3;
1320 #else
1321 		const deUint32 uint32ByteOffsetBits0To8		= 3; //!< least significant byte in the highest address
1322 		const deUint32 uint32ByteOffsetBits0To24	= 1;
1323 		const deUint32 uint32ByteOffsetBits8To32	= 0;
1324 		const deUint32 uint32ByteOffsetBits16To32	= 0;
1325 		const deUint32 uint32ByteOffsetBits24To32	= 0;
1326 #endif
1327 
1328 		// Sampled channel must exist
1329 		DE_ASSERT(baseAccess.getFormat().order == TextureFormat::DS ||
1330 				  (mode == Sampler::MODE_DEPTH && baseAccess.getFormat().order == TextureFormat::D) ||
1331 				  (mode == Sampler::MODE_STENCIL && baseAccess.getFormat().order == TextureFormat::S));
1332 
1333 		// combined formats have multiple channel classes, detect on sampler settings
1334 		switch (baseAccess.getFormat().type)
1335 		{
1336 			case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:
1337 			{
1338 				if (mode == Sampler::MODE_DEPTH)
1339 				{
1340 					// select the float component
1341 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::FLOAT),
1342 									  baseAccess.getSize(),
1343 									  baseAccess.getPitch(),
1344 									  baseAccess.getDataPtr());
1345 				}
1346 				else if (mode == Sampler::MODE_STENCIL)
1347 				{
1348 					// select the uint 8 component
1349 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1350 									  baseAccess.getSize(),
1351 									  baseAccess.getPitch(),
1352 									  addOffset(baseAccess.getDataPtr(), 4 + uint32ByteOffsetBits0To8));
1353 				}
1354 				else
1355 				{
1356 					// unknown sampler mode
1357 					DE_ASSERT(false);
1358 					return AccessType();
1359 				}
1360 			}
1361 
1362 			case TextureFormat::UNSIGNED_INT_16_8_8:
1363 			{
1364 				if (mode == Sampler::MODE_DEPTH)
1365 				{
1366 					// select the unorm16 component
1367 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT16),
1368 									  baseAccess.getSize(),
1369 									  baseAccess.getPitch(),
1370 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits16To32));
1371 				}
1372 				else if (mode == Sampler::MODE_STENCIL)
1373 				{
1374 					// select the uint 8 component
1375 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1376 									  baseAccess.getSize(),
1377 									  baseAccess.getPitch(),
1378 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To8));
1379 				}
1380 				else
1381 				{
1382 					// unknown sampler mode
1383 					DE_ASSERT(false);
1384 					return AccessType();
1385 				}
1386 			}
1387 
1388 			case TextureFormat::UNSIGNED_INT_24_8:
1389 			{
1390 				if (mode == Sampler::MODE_DEPTH)
1391 				{
1392 					// select the unorm24 component
1393 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT24),
1394 									  baseAccess.getSize(),
1395 									  baseAccess.getPitch(),
1396 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits8To32));
1397 				}
1398 				else if (mode == Sampler::MODE_STENCIL)
1399 				{
1400 					// select the uint 8 component
1401 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1402 									  baseAccess.getSize(),
1403 									  baseAccess.getPitch(),
1404 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To8));
1405 				}
1406 				else
1407 				{
1408 					// unknown sampler mode
1409 					DE_ASSERT(false);
1410 					return AccessType();
1411 				}
1412 			}
1413 
1414 			case TextureFormat::UNSIGNED_INT_24_8_REV:
1415 			{
1416 				if (mode == Sampler::MODE_DEPTH)
1417 				{
1418 					// select the unorm24 component
1419 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT24),
1420 									  baseAccess.getSize(),
1421 									  baseAccess.getPitch(),
1422 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To24));
1423 				}
1424 				else if (mode == Sampler::MODE_STENCIL)
1425 				{
1426 					// select the uint 8 component
1427 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1428 									  baseAccess.getSize(),
1429 									  baseAccess.getPitch(),
1430 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits24To32));
1431 				}
1432 				else
1433 				{
1434 					// unknown sampler mode
1435 					DE_ASSERT(false);
1436 					return AccessType();
1437 				}
1438 			}
1439 
1440 			default:
1441 			{
1442 				// unknown combined format
1443 				DE_ASSERT(false);
1444 				return AccessType();
1445 			}
1446 		}
1447 	}
1448 }
1449 
getEffectiveDepthStencilAccess(const PixelBufferAccess & baseAccess,Sampler::DepthStencilMode mode)1450 PixelBufferAccess getEffectiveDepthStencilAccess (const PixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode)
1451 {
1452 	return toSamplerAccess<PixelBufferAccess>(baseAccess, mode);
1453 }
1454 
getEffectiveDepthStencilAccess(const ConstPixelBufferAccess & baseAccess,Sampler::DepthStencilMode mode)1455 ConstPixelBufferAccess getEffectiveDepthStencilAccess (const ConstPixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode)
1456 {
1457 	return toSamplerAccess<ConstPixelBufferAccess>(baseAccess, mode);
1458 }
1459 
getEffectiveDepthStencilTextureFormat(const TextureFormat & baseFormat,Sampler::DepthStencilMode mode)1460 TextureFormat getEffectiveDepthStencilTextureFormat (const TextureFormat& baseFormat, Sampler::DepthStencilMode mode)
1461 {
1462 	return toSamplerAccess(ConstPixelBufferAccess(baseFormat, IVec3(0, 0, 0), DE_NULL), mode).getFormat();
1463 }
1464 
1465 template <typename ViewType>
getEffectiveTView(const ViewType & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1466 ViewType getEffectiveTView (const ViewType& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1467 {
1468 	storage.resize(src.getNumLevels());
1469 
1470 	ViewType view = ViewType(src.getNumLevels(), &storage[0]);
1471 
1472 	for (int levelNdx = 0; levelNdx < src.getNumLevels(); ++levelNdx)
1473 		storage[levelNdx] = tcu::getEffectiveDepthStencilAccess(src.getLevel(levelNdx), sampler.depthStencilMode);
1474 
1475 	return view;
1476 }
1477 
getEffectiveTView(const tcu::TextureCubeView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1478 tcu::TextureCubeView getEffectiveTView (const tcu::TextureCubeView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1479 {
1480 	storage.resize(tcu::CUBEFACE_LAST * src.getNumLevels());
1481 
1482 	const tcu::ConstPixelBufferAccess* storagePtrs[tcu::CUBEFACE_LAST] =
1483 	{
1484 		&storage[0 * src.getNumLevels()],
1485 		&storage[1 * src.getNumLevels()],
1486 		&storage[2 * src.getNumLevels()],
1487 		&storage[3 * src.getNumLevels()],
1488 		&storage[4 * src.getNumLevels()],
1489 		&storage[5 * src.getNumLevels()],
1490 	};
1491 
1492 	tcu::TextureCubeView view = tcu::TextureCubeView(src.getNumLevels(), storagePtrs);
1493 
1494 	for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; ++faceNdx)
1495 	for (int levelNdx = 0; levelNdx < src.getNumLevels(); ++levelNdx)
1496 		storage[faceNdx * src.getNumLevels() + levelNdx] = tcu::getEffectiveDepthStencilAccess(src.getLevelFace(levelNdx, (tcu::CubeFace)faceNdx), sampler.depthStencilMode);
1497 
1498 	return view;
1499 }
1500 
getEffectiveTextureView(const tcu::Texture1DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1501 tcu::Texture1DView getEffectiveTextureView (const tcu::Texture1DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1502 {
1503 	return getEffectiveTView(src, storage, sampler);
1504 }
1505 
getEffectiveTextureView(const tcu::Texture2DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1506 tcu::Texture2DView getEffectiveTextureView (const tcu::Texture2DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1507 {
1508 	return getEffectiveTView(src, storage, sampler);
1509 }
1510 
getEffectiveTextureView(const tcu::Texture3DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1511 tcu::Texture3DView getEffectiveTextureView (const tcu::Texture3DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1512 {
1513 	return getEffectiveTView(src, storage, sampler);
1514 }
1515 
getEffectiveTextureView(const tcu::Texture1DArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1516 tcu::Texture1DArrayView getEffectiveTextureView (const tcu::Texture1DArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1517 {
1518 	return getEffectiveTView(src, storage, sampler);
1519 }
1520 
getEffectiveTextureView(const tcu::Texture2DArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1521 tcu::Texture2DArrayView getEffectiveTextureView (const tcu::Texture2DArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1522 {
1523 	return getEffectiveTView(src, storage, sampler);
1524 }
1525 
getEffectiveTextureView(const tcu::TextureCubeView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1526 tcu::TextureCubeView getEffectiveTextureView (const tcu::TextureCubeView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1527 {
1528 	return getEffectiveTView(src, storage, sampler);
1529 }
1530 
getEffectiveTextureView(const tcu::TextureCubeArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1531 tcu::TextureCubeArrayView getEffectiveTextureView (const tcu::TextureCubeArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1532 {
1533 	return getEffectiveTView(src, storage, sampler);
1534 }
1535 
1536 //! Returns the effective swizzle of a border color. The effective swizzle is the
1537 //! equal to first writing an RGBA color with a write swizzle and then reading
1538 //! it back using a read swizzle, i.e. BorderSwizzle(c) == readSwizzle(writeSwizzle(C))
getBorderColorReadSwizzle(TextureFormat::ChannelOrder order)1539 static const TextureSwizzle& getBorderColorReadSwizzle (TextureFormat::ChannelOrder order)
1540 {
1541 	// make sure to update these tables when channel orders are updated
1542 	DE_STATIC_ASSERT(TextureFormat::CHANNELORDER_LAST == 21);
1543 
1544 	static const TextureSwizzle INV		= {{ TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1545 	static const TextureSwizzle R		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1546 	static const TextureSwizzle A		= {{ TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_3	}};
1547 	static const TextureSwizzle I		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0	}};
1548 	static const TextureSwizzle L		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ONE	}};
1549 	static const TextureSwizzle LA		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_3	}};
1550 	static const TextureSwizzle RG		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1551 	static const TextureSwizzle RA		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_3	}};
1552 	static const TextureSwizzle RGB		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_2,		TextureSwizzle::CHANNEL_ONE	}};
1553 	static const TextureSwizzle RGBA	= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_2,		TextureSwizzle::CHANNEL_3	}};
1554 	static const TextureSwizzle D		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1555 	static const TextureSwizzle S		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1556 
1557 	const TextureSwizzle* swizzle;
1558 
1559 	switch (order)
1560 	{
1561 		case TextureFormat::R:			swizzle = &R;		break;
1562 		case TextureFormat::A:			swizzle = &A;		break;
1563 		case TextureFormat::I:			swizzle = &I;		break;
1564 		case TextureFormat::L:			swizzle = &L;		break;
1565 		case TextureFormat::LA:			swizzle = &LA;		break;
1566 		case TextureFormat::RG:			swizzle = &RG;		break;
1567 		case TextureFormat::RA:			swizzle = &RA;		break;
1568 		case TextureFormat::RGB:		swizzle = &RGB;		break;
1569 		case TextureFormat::RGBA:		swizzle = &RGBA;	break;
1570 		case TextureFormat::ARGB:		swizzle = &RGBA;	break;
1571 		case TextureFormat::BGR:		swizzle = &RGB;		break;
1572 		case TextureFormat::BGRA:		swizzle = &RGBA;	break;
1573 		case TextureFormat::sR:			swizzle = &R;		break;
1574 		case TextureFormat::sRG:		swizzle = &RG;		break;
1575 		case TextureFormat::sRGB:		swizzle = &RGB;		break;
1576 		case TextureFormat::sRGBA:		swizzle = &RGBA;	break;
1577 		case TextureFormat::sBGR:		swizzle = &RGB;		break;
1578 		case TextureFormat::sBGRA:		swizzle = &RGBA;	break;
1579 		case TextureFormat::D:			swizzle = &D;		break;
1580 		case TextureFormat::S:			swizzle = &S;		break;
1581 
1582 		case TextureFormat::DS:
1583 			DE_ASSERT(false); // combined depth-stencil border color?
1584 			swizzle = &INV;
1585 			break;
1586 
1587 		default:
1588 			DE_ASSERT(false);
1589 			swizzle = &INV;
1590 			break;
1591 	}
1592 
1593 #ifdef DE_DEBUG
1594 
1595 	{
1596 		// check that BorderSwizzle(c) == readSwizzle(writeSwizzle(C))
1597 		const TextureSwizzle& readSwizzle	= getChannelReadSwizzle(order);
1598 		const TextureSwizzle& writeSwizzle	= getChannelWriteSwizzle(order);
1599 
1600 		for (int ndx = 0; ndx < 4; ++ndx)
1601 		{
1602 			TextureSwizzle::Channel writeRead = readSwizzle.components[ndx];
1603 			if (deInRange32(writeRead, TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE)
1604 				writeRead = writeSwizzle.components[(int)writeRead];
1605 			DE_ASSERT(writeRead == swizzle->components[ndx]);
1606 		}
1607 	}
1608 
1609 #endif
1610 
1611 	return *swizzle;
1612 }
1613 
getNBitUnsignedIntegerVec4MaxValue(const tcu::IVec4 & numBits)1614 static tcu::UVec4 getNBitUnsignedIntegerVec4MaxValue (const tcu::IVec4& numBits)
1615 {
1616 	return tcu::UVec4((numBits[0] > 0) ? (deUintMaxValue32(numBits[0])) : (0),
1617 					  (numBits[1] > 0) ? (deUintMaxValue32(numBits[1])) : (0),
1618 					  (numBits[2] > 0) ? (deUintMaxValue32(numBits[2])) : (0),
1619 					  (numBits[3] > 0) ? (deUintMaxValue32(numBits[3])) : (0));
1620 }
1621 
getNBitSignedIntegerVec4MaxValue(const tcu::IVec4 & numBits)1622 static tcu::IVec4 getNBitSignedIntegerVec4MaxValue (const tcu::IVec4& numBits)
1623 {
1624 	return tcu::IVec4((numBits[0] > 0) ? (deIntMaxValue32(numBits[0])) : (0),
1625 					  (numBits[1] > 0) ? (deIntMaxValue32(numBits[1])) : (0),
1626 					  (numBits[2] > 0) ? (deIntMaxValue32(numBits[2])) : (0),
1627 					  (numBits[3] > 0) ? (deIntMaxValue32(numBits[3])) : (0));
1628 }
1629 
getNBitSignedIntegerVec4MinValue(const tcu::IVec4 & numBits)1630 static tcu::IVec4 getNBitSignedIntegerVec4MinValue (const tcu::IVec4& numBits)
1631 {
1632 	return tcu::IVec4((numBits[0] > 0) ? (deIntMinValue32(numBits[0])) : (0),
1633 					  (numBits[1] > 0) ? (deIntMinValue32(numBits[1])) : (0),
1634 					  (numBits[2] > 0) ? (deIntMinValue32(numBits[2])) : (0),
1635 					  (numBits[3] > 0) ? (deIntMinValue32(numBits[3])) : (0));
1636 }
1637 
getTextureBorderColorFloat(const TextureFormat & format,const Sampler & sampler)1638 static tcu::Vec4 getTextureBorderColorFloat (const TextureFormat& format, const Sampler& sampler)
1639 {
1640 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1641 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1642 	const bool						isFloat			= channelClass == tcu::TEXTURECHANNELCLASS_FLOATING_POINT;
1643 	const bool						isSigned		= channelClass != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
1644 	const float						valueMin		= (isSigned) ? (-1.0f) : (0.0f);
1645 	const float						valueMax		= 1.0f;
1646 	Vec4							result;
1647 
1648 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_FLOATING_POINT ||
1649 			  channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT ||
1650 			  channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT);
1651 
1652 	for (int c = 0; c < 4; c++)
1653 	{
1654 		const TextureSwizzle::Channel map = channelMap[c];
1655 		if (map == TextureSwizzle::CHANNEL_ZERO)
1656 			result[c] = 0.0f;
1657 		else if (map == TextureSwizzle::CHANNEL_ONE)
1658 			result[c] = 1.0f;
1659 		else if (isFloat)
1660 		{
1661 			// floating point values are not clamped
1662 			result[c] = sampler.borderColor.getAccess<float>()[(int)map];
1663 		}
1664 		else
1665 		{
1666 			// fixed point values are clamped to a representable range
1667 			result[c] = de::clamp(sampler.borderColor.getAccess<float>()[(int)map], valueMin, valueMax);
1668 		}
1669 	}
1670 
1671 	return result;
1672 }
1673 
getTextureBorderColorInt(const TextureFormat & format,const Sampler & sampler)1674 static tcu::IVec4 getTextureBorderColorInt (const TextureFormat& format, const Sampler& sampler)
1675 {
1676 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1677 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1678 	const IVec4						channelBits		= getChannelBitDepth(format.type);
1679 	const IVec4						valueMin		= getNBitSignedIntegerVec4MinValue(channelBits);
1680 	const IVec4						valueMax		= getNBitSignedIntegerVec4MaxValue(channelBits);
1681 	IVec4							result;
1682 
1683 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER);
1684 	DE_UNREF(channelClass);
1685 
1686 	for (int c = 0; c < 4; c++)
1687 	{
1688 		const TextureSwizzle::Channel map = channelMap[c];
1689 		if (map == TextureSwizzle::CHANNEL_ZERO)
1690 			result[c] = 0;
1691 		else if (map == TextureSwizzle::CHANNEL_ONE)
1692 			result[c] = 1;
1693 		else
1694 		{
1695 			// integer values are clamped to a representable range
1696 			result[c] = de::clamp(sampler.borderColor.getAccess<deInt32>()[(int)map], valueMin[(int)map], valueMax[(int)map]);
1697 		}
1698 	}
1699 
1700 	return result;
1701 }
1702 
getTextureBorderColorUint(const TextureFormat & format,const Sampler & sampler)1703 static tcu::UVec4 getTextureBorderColorUint (const TextureFormat& format, const Sampler& sampler)
1704 {
1705 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1706 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1707 	const IVec4						channelBits		= getChannelBitDepth(format.type);
1708 	const UVec4						valueMax		= getNBitUnsignedIntegerVec4MaxValue(channelBits);
1709 	UVec4							result;
1710 
1711 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER);
1712 	DE_UNREF(channelClass);
1713 
1714 	for (int c = 0; c < 4; c++)
1715 	{
1716 		const TextureSwizzle::Channel map = channelMap[c];
1717 		if (map == TextureSwizzle::CHANNEL_ZERO)
1718 			result[c] = 0;
1719 		else if (map == TextureSwizzle::CHANNEL_ONE)
1720 			result[c] = 1;
1721 		else
1722 		{
1723 			// integer values are clamped to a representable range
1724 			result[c] = de::min(sampler.borderColor.getAccess<deUint32>()[(int)map], valueMax[(int)map]);
1725 		}
1726 	}
1727 
1728 	return result;
1729 }
1730 
1731 template <typename ScalarType>
sampleTextureBorder(const TextureFormat & format,const Sampler & sampler)1732 tcu::Vector<ScalarType, 4> sampleTextureBorder (const TextureFormat& format, const Sampler& sampler)
1733 {
1734 	const tcu::TextureChannelClass channelClass = getTextureChannelClass(format.type);
1735 
1736 	switch (channelClass)
1737 	{
1738 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
1739 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1740 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1741 			return getTextureBorderColorFloat(format, sampler).cast<ScalarType>();
1742 
1743 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
1744 			return getTextureBorderColorInt(format, sampler).cast<ScalarType>();
1745 
1746 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
1747 			return getTextureBorderColorUint(format, sampler).cast<ScalarType>();
1748 
1749 		default:
1750 			DE_ASSERT(false);
1751 			return tcu::Vector<ScalarType, 4>();
1752 	}
1753 }
1754 
1755 // instantiation
1756 template tcu::Vector<float, 4>		sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1757 template tcu::Vector<deInt32, 4>	sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1758 template tcu::Vector<deUint32, 4>	sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1759 
1760 } // tcu
1761