• 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 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 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 == 22);
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 == 22);
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 == 22);
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 - how the values are stored (not how they are sampled)
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 = 0.5e5f;			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::SIGNED_INT64:					return IVec4(64);
573 		case TextureFormat::UNSIGNED_INT8:					return IVec4(8);
574 		case TextureFormat::UNSIGNED_INT16:					return IVec4(16);
575 		case TextureFormat::UNSIGNED_INT24:					return IVec4(24);
576 		case TextureFormat::UNSIGNED_INT32:					return IVec4(32);
577 		case TextureFormat::UNSIGNED_INT64:					return IVec4(64);
578 		case TextureFormat::SIGNED_INT_1010102_REV:			return IVec4(10,10,10,2);
579 		case TextureFormat::UNSIGNED_INT_1010102_REV:		return IVec4(10,10,10,2);
580 		case TextureFormat::UNSIGNED_INT_16_8_8:			return IVec4(16,8,0,0);
581 		case TextureFormat::UNSIGNED_INT_24_8:				return IVec4(24,8,0,0);
582 		case TextureFormat::UNSIGNED_INT_24_8_REV:			return IVec4(24,8,0,0);
583 		case TextureFormat::HALF_FLOAT:						return IVec4(16);
584 		case TextureFormat::FLOAT:							return IVec4(32);
585 		case TextureFormat::FLOAT64:						return IVec4(64);
586 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	return IVec4(11,11,10,0);
587 		case TextureFormat::UNSIGNED_INT_999_E5_REV:		return IVec4(9,9,9,0);
588 		case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:	return IVec4(32,8,0,0);
589 		case TextureFormat::UNORM_SHORT_10:					return IVec4(10);
590 		case TextureFormat::UNORM_SHORT_12:					return IVec4(12);
591 		case TextureFormat::USCALED_INT8:					return IVec4(8);
592 		case TextureFormat::USCALED_INT16:					return IVec4(16);
593 		case TextureFormat::SSCALED_INT8:					return IVec4(8);
594 		case TextureFormat::SSCALED_INT16:					return IVec4(16);
595 		case TextureFormat::USCALED_INT_1010102_REV:		return IVec4(10,10,10,2);
596 		case TextureFormat::SSCALED_INT_1010102_REV:		return IVec4(10,10,10,2);
597 		default:
598 			DE_ASSERT(false);
599 			return IVec4(0);
600 	}
601 }
602 
getTextureFormatBitDepth(const TextureFormat & format)603 IVec4 getTextureFormatBitDepth (const TextureFormat& format)
604 {
605 	const IVec4						chnBits		= getChannelBitDepth(format.type);
606 	const TextureSwizzle::Channel*	map			= getChannelReadSwizzle(format.order).components;
607 	const BVec4						chnMask		= BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
608 														deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
609 														deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
610 														deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
611 	const IVec4						chnSwz		= IVec4((chnMask[0]) ? ((int)map[0]) : (0),
612 														(chnMask[1]) ? ((int)map[1]) : (0),
613 														(chnMask[2]) ? ((int)map[2]) : (0),
614 														(chnMask[3]) ? ((int)map[3]) : (0));
615 
616 	return select(chnBits.swizzle(chnSwz.x(), chnSwz.y(), chnSwz.z(), chnSwz.w()), IVec4(0), chnMask);
617 }
618 
getChannelMantissaBitDepth(TextureFormat::ChannelType channelType)619 static IVec4 getChannelMantissaBitDepth (TextureFormat::ChannelType channelType)
620 {
621 	// make sure this table is updated if format table is updated
622 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
623 
624 	switch (channelType)
625 	{
626 		case TextureFormat::SNORM_INT8:
627 		case TextureFormat::SNORM_INT16:
628 		case TextureFormat::SNORM_INT32:
629 		case TextureFormat::UNORM_INT8:
630 		case TextureFormat::UNORM_INT16:
631 		case TextureFormat::UNORM_INT24:
632 		case TextureFormat::UNORM_INT32:
633 		case TextureFormat::UNORM_BYTE_44:
634 		case TextureFormat::UNORM_SHORT_565:
635 		case TextureFormat::UNORM_SHORT_4444:
636 		case TextureFormat::UNORM_SHORT_555:
637 		case TextureFormat::UNORM_SHORT_5551:
638 		case TextureFormat::UNORM_SHORT_1555:
639 		case TextureFormat::UNSIGNED_BYTE_44:
640 		case TextureFormat::UNSIGNED_SHORT_565:
641 		case TextureFormat::UNSIGNED_SHORT_4444:
642 		case TextureFormat::UNSIGNED_SHORT_5551:
643 		case TextureFormat::UNORM_INT_101010:
644 		case TextureFormat::SNORM_INT_1010102_REV:
645 		case TextureFormat::UNORM_INT_1010102_REV:
646 		case TextureFormat::SIGNED_INT8:
647 		case TextureFormat::SIGNED_INT16:
648 		case TextureFormat::SIGNED_INT32:
649 		case TextureFormat::UNSIGNED_INT8:
650 		case TextureFormat::UNSIGNED_INT16:
651 		case TextureFormat::UNSIGNED_INT24:
652 		case TextureFormat::UNSIGNED_INT32:
653 		case TextureFormat::SIGNED_INT_1010102_REV:
654 		case TextureFormat::UNSIGNED_INT_1010102_REV:
655 		case TextureFormat::UNSIGNED_INT_16_8_8:
656 		case TextureFormat::UNSIGNED_INT_24_8:
657 		case TextureFormat::UNSIGNED_INT_24_8_REV:
658 		case TextureFormat::UNSIGNED_INT_999_E5_REV:
659 		case TextureFormat::UNORM_SHORT_10:
660 		case TextureFormat::UNORM_SHORT_12:
661 		case TextureFormat::USCALED_INT8:
662 		case TextureFormat::USCALED_INT16:
663 		case TextureFormat::SSCALED_INT8:
664 		case TextureFormat::SSCALED_INT16:
665 		case TextureFormat::USCALED_INT_1010102_REV:
666 		case TextureFormat::SSCALED_INT_1010102_REV:
667 			return getChannelBitDepth(channelType);
668 
669 		case TextureFormat::HALF_FLOAT:						return IVec4(10);
670 		case TextureFormat::FLOAT:							return IVec4(23);
671 		case TextureFormat::FLOAT64:						return IVec4(52);
672 		case TextureFormat::UNSIGNED_INT_11F_11F_10F_REV:	return IVec4(6,6,5,0);
673 		case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:	return IVec4(23,8,0,0);
674 		default:
675 			DE_ASSERT(false);
676 			return IVec4(0);
677 	}
678 }
679 
getTextureFormatMantissaBitDepth(const TextureFormat & format)680 IVec4 getTextureFormatMantissaBitDepth (const TextureFormat& format)
681 {
682 	const IVec4						chnBits		= getChannelMantissaBitDepth(format.type);
683 	const TextureSwizzle::Channel*	map			= getChannelReadSwizzle(format.order).components;
684 	const BVec4						chnMask		= BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
685 														deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
686 														deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
687 														deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
688 	const IVec4						chnSwz		= IVec4((chnMask[0]) ? ((int)map[0]) : (0),
689 														(chnMask[1]) ? ((int)map[1]) : (0),
690 														(chnMask[2]) ? ((int)map[2]) : (0),
691 														(chnMask[3]) ? ((int)map[3]) : (0));
692 
693 	return select(chnBits.swizzle(chnSwz.x(), chnSwz.y(), chnSwz.z(), chnSwz.w()), IVec4(0), chnMask);
694 }
695 
getTextureFormatChannelMask(const TextureFormat & format)696 BVec4 getTextureFormatChannelMask (const TextureFormat& format)
697 {
698 	const TextureSwizzle::Channel* const map = getChannelReadSwizzle(format.order).components;
699 	return BVec4(deInRange32(map[0], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
700 				 deInRange32(map[1], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
701 				 deInRange32(map[2], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE,
702 				 deInRange32(map[3], TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE);
703 }
704 
linearInterpolate(float t,float minVal,float maxVal)705 static inline float linearInterpolate (float t, float minVal, float maxVal)
706 {
707 	return minVal + (maxVal - minVal) * t;
708 }
709 
linearInterpolate(float t,const Vec4 & a,const Vec4 & b)710 static inline Vec4 linearInterpolate (float t, const Vec4& a, const Vec4& b)
711 {
712 	return a + (b - a) * t;
713 }
714 
715 enum
716 {
717 	CLEAR_OPTIMIZE_THRESHOLD		= 128,
718 	CLEAR_OPTIMIZE_MAX_PIXEL_SIZE	= 8
719 };
720 
fillRow(const PixelBufferAccess & dst,int y,int z,int pixelSize,const deUint8 * pixel)721 inline void fillRow (const PixelBufferAccess& dst, int y, int z, int pixelSize, const deUint8* pixel)
722 {
723 	DE_ASSERT(dst.getPixelPitch() == pixelSize); // only tightly packed
724 
725 	deUint8*	dstPtr	= (deUint8*)dst.getPixelPtr(0, y, z);
726 	int			width	= dst.getWidth();
727 
728 	if (pixelSize == 8 && deIsAlignedPtr(dstPtr, pixelSize))
729 	{
730 		deUint64 val;
731 		memcpy(&val, pixel, sizeof(val));
732 
733 		for (int i = 0; i < width; i++)
734 			((deUint64*)dstPtr)[i] = val;
735 	}
736 	else if (pixelSize == 4 && deIsAlignedPtr(dstPtr, pixelSize))
737 	{
738 		deUint32 val;
739 		memcpy(&val, pixel, sizeof(val));
740 
741 		for (int i = 0; i < width; i++)
742 			((deUint32*)dstPtr)[i] = val;
743 	}
744 	else
745 	{
746 		for (int i = 0; i < width; i++)
747 			for (int j = 0; j < pixelSize; j++)
748 				dstPtr[i*pixelSize+j] = pixel[j];
749 	}
750 }
751 
clear(const PixelBufferAccess & access,const Vec4 & color)752 void clear (const PixelBufferAccess& access, const Vec4& color)
753 {
754 	const int	pixelSize				= access.getFormat().getPixelSize();
755 	const int	pixelPitch				= access.getPixelPitch();
756 	const bool	rowPixelsTightlyPacked	= (pixelSize == pixelPitch);
757 
758 	if (access.getWidth()*access.getHeight()*access.getDepth() >= CLEAR_OPTIMIZE_THRESHOLD &&
759 		pixelSize < CLEAR_OPTIMIZE_MAX_PIXEL_SIZE && rowPixelsTightlyPacked)
760 	{
761 		// Convert to destination format.
762 		union
763 		{
764 			deUint8		u8[CLEAR_OPTIMIZE_MAX_PIXEL_SIZE];
765 			deUint64	u64; // Forces 64-bit alignment.
766 		} pixel;
767 		DE_STATIC_ASSERT(sizeof(pixel) == CLEAR_OPTIMIZE_MAX_PIXEL_SIZE);
768 		PixelBufferAccess(access.getFormat(), 1, 1, 1, 0, 0, &pixel.u8[0]).setPixel(color, 0, 0);
769 
770 		for (int z = 0; z < access.getDepth(); z++)
771 			for (int y = 0; y < access.getHeight(); y++)
772 				fillRow(access, y, z, pixelSize, &pixel.u8[0]);
773 	}
774 	else
775 	{
776 		for (int z = 0; z < access.getDepth(); z++)
777 			for (int y = 0; y < access.getHeight(); y++)
778 				for (int x = 0; x < access.getWidth(); x++)
779 					access.setPixel(color, x, y, z);
780 	}
781 }
782 
clear(const PixelBufferAccess & access,const IVec4 & color)783 void clear (const PixelBufferAccess& access, const IVec4& color)
784 {
785 	const int	pixelSize				= access.getFormat().getPixelSize();
786 	const int	pixelPitch				= access.getPixelPitch();
787 	const bool	rowPixelsTightlyPacked	= (pixelSize == pixelPitch);
788 
789 	if (access.getWidth()*access.getHeight()*access.getDepth() >= CLEAR_OPTIMIZE_THRESHOLD &&
790 		pixelSize < CLEAR_OPTIMIZE_MAX_PIXEL_SIZE && rowPixelsTightlyPacked)
791 	{
792 		// Convert to destination format.
793 		union
794 		{
795 			deUint8		u8[CLEAR_OPTIMIZE_MAX_PIXEL_SIZE];
796 			deUint64	u64; // Forces 64-bit alignment.
797 		} pixel;
798 		DE_STATIC_ASSERT(sizeof(pixel) == CLEAR_OPTIMIZE_MAX_PIXEL_SIZE);
799 		PixelBufferAccess(access.getFormat(), 1, 1, 1, 0, 0, &pixel.u8[0]).setPixel(color, 0, 0);
800 
801 		for (int z = 0; z < access.getDepth(); z++)
802 			for (int y = 0; y < access.getHeight(); y++)
803 				fillRow(access, y, z, pixelSize, &pixel.u8[0]);
804 	}
805 	else
806 	{
807 		for (int z = 0; z < access.getDepth(); z++)
808 			for (int y = 0; y < access.getHeight(); y++)
809 				for (int x = 0; x < access.getWidth(); x++)
810 					access.setPixel(color, x, y, z);
811 	}
812 }
813 
clear(const PixelBufferAccess & access,const UVec4 & color)814 void clear (const PixelBufferAccess& access, const UVec4& color)
815 {
816 	clear(access, color.cast<deInt32>());
817 }
818 
clearDepth(const PixelBufferAccess & access,float depth)819 void clearDepth (const PixelBufferAccess& access, float depth)
820 {
821 	DE_ASSERT(access.getFormat().order == TextureFormat::DS || access.getFormat().order == TextureFormat::D);
822 
823 	clear(getEffectiveDepthStencilAccess(access, Sampler::MODE_DEPTH), tcu::Vec4(depth, 0.0f, 0.0f, 0.0f));
824 }
825 
clearStencil(const PixelBufferAccess & access,int stencil)826 void clearStencil (const PixelBufferAccess& access, int stencil)
827 {
828 	DE_ASSERT(access.getFormat().order == TextureFormat::DS || access.getFormat().order == TextureFormat::S);
829 
830 	clear(getEffectiveDepthStencilAccess(access, Sampler::MODE_STENCIL), tcu::UVec4(stencil, 0u, 0u, 0u));
831 }
832 
833 enum GradientStyle
834 {
835 	GRADIENT_STYLE_OLD = 0,
836 	GRADIENT_STYLE_NEW = 1,
837 	GRADIENT_STYLE_PYRAMID = 2
838 };
839 
fillWithComponentGradients1D(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal,GradientStyle)840 static void fillWithComponentGradients1D (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal, GradientStyle)
841 {
842 	DE_ASSERT(access.getHeight() == 1);
843 	for (int x = 0; x < access.getWidth(); x++)
844 	{
845 		float s = ((float)x + 0.5f) / (float)access.getWidth();
846 
847 		float r = linearInterpolate(s, minVal.x(), maxVal.x());
848 		float g = linearInterpolate(s, minVal.y(), maxVal.y());
849 		float b = linearInterpolate(s, minVal.z(), maxVal.z());
850 		float a = linearInterpolate(s, minVal.w(), maxVal.w());
851 
852 		access.setPixel(tcu::Vec4(r, g, b, a), x, 0);
853 	}
854 }
855 
fillWithComponentGradients2D(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal,GradientStyle style)856 static void fillWithComponentGradients2D (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal, GradientStyle style)
857 {
858 	if (style == GRADIENT_STYLE_PYRAMID)
859 	{
860 		int xedge = deFloorFloatToInt32(float(access.getWidth()) * 0.6f);
861 		int yedge = deFloorFloatToInt32(float(access.getHeight()) * 0.6f);
862 
863 		for (int y = 0; y < access.getHeight(); y++)
864 		{
865 			for (int x = 0; x < access.getWidth(); x++)
866 			{
867 				float s = ((float)x + 0.5f) / (float)access.getWidth();
868 				float t = ((float)y + 0.5f) / (float)access.getHeight();
869 				float coefR = 0.0f;
870 				float coefG = 0.0f;
871 				float coefB = 0.0f;
872 				float coefA = 0.0f;
873 
874 				coefR = (x < xedge) ? s * 0.4f : (1 - s) * 0.6f;
875 				coefG = (x < xedge) ? s * 0.4f : (1 - s) * 0.6f;
876 				coefB = (x < xedge) ? (1.0f - s) * 0.4f : s * 0.6f - 0.2f;
877 				coefA = (x < xedge) ? (1.0f - s) * 0.4f : s * 0.6f - 0.2f;
878 
879 				coefR += (y < yedge) ? t * 0.4f : (1 - t) * 0.6f;
880 				coefG += (y < yedge) ? (1.0f - t) * 0.4f : t * 0.6f - 0.2f;
881 				coefB += (y < yedge) ? t * 0.4f : (1 - t) * 0.6f;
882 				coefA += (y < yedge) ? (1.0f - t) * 0.4f : t * 0.6f - 0.2f;
883 
884 				float r = linearInterpolate(coefR, minVal.x(), maxVal.x());
885 				float g = linearInterpolate(coefG, minVal.y(), maxVal.y());
886 				float b = linearInterpolate(coefB, minVal.z(), maxVal.z());
887 				float a = linearInterpolate(coefA, minVal.w(), maxVal.w());
888 
889 				access.setPixel(tcu::Vec4(r, g, b, a), x, y);
890 			}
891 		}
892 	}
893 	else
894 	{
895 		for (int y = 0; y < access.getHeight(); y++)
896 		{
897 			for (int x = 0; x < access.getWidth(); x++)
898 			{
899 				float s = ((float)x + 0.5f) / (float)access.getWidth();
900 				float t = ((float)y + 0.5f) / (float)access.getHeight();
901 
902 				float r = linearInterpolate((s + t) *0.5f, minVal.x(), maxVal.x());
903 				float g = linearInterpolate((s + (1.0f - t))*0.5f, minVal.y(), maxVal.y());
904 				float b = linearInterpolate(((1.0f - s) + t) *0.5f, minVal.z(), maxVal.z());
905 				float a = linearInterpolate(((1.0f - s) + (1.0f - t))*0.5f, minVal.w(), maxVal.w());
906 
907 				access.setPixel(tcu::Vec4(r, g, b, a), x, y);
908 			}
909 		}
910 	}
911 }
912 
fillWithComponentGradients3D(const PixelBufferAccess & dst,const Vec4 & minVal,const Vec4 & maxVal,GradientStyle style)913 static void fillWithComponentGradients3D (const PixelBufferAccess& dst, const Vec4& minVal, const Vec4& maxVal, GradientStyle style)
914 {
915 	for (int z = 0; z < dst.getDepth(); z++)
916 	{
917 		for (int y = 0; y < dst.getHeight(); y++)
918 		{
919 			for (int x = 0; x < dst.getWidth(); x++)
920 			{
921 				float s = ((float)x + 0.5f) / (float)dst.getWidth();
922 				float t = ((float)y + 0.5f) / (float)dst.getHeight();
923 				float p = ((float)z + 0.5f) / (float)dst.getDepth();
924 
925 				float r, g, b, a;
926 
927 				if (style == GRADIENT_STYLE_NEW)
928 				{
929 					// R, G, B and A all depend on every coordinate.
930 					r = linearInterpolate((s+t+p)/3.0f,							minVal.x(), maxVal.x());
931 					g = linearInterpolate((s + (1.0f - (t+p)*0.5f)*2.0f)/3.0f,	minVal.y(), maxVal.y());
932 					b = linearInterpolate(((1.0f - (s+t)*0.5f)*2.0f + p)/3.0f,	minVal.z(), maxVal.z());
933 					a = linearInterpolate(1.0f - (s+t+p)/3.0f,					minVal.w(), maxVal.w());
934 				}
935 				else // GRADIENT_STYLE_OLD
936 				{
937 					// Each of R, G and B only depend on X, Y and Z, respectively.
938 					r = linearInterpolate(s,					minVal.x(), maxVal.x());
939 					g = linearInterpolate(t,					minVal.y(), maxVal.y());
940 					b = linearInterpolate(p,					minVal.z(), maxVal.z());
941 					a = linearInterpolate(1.0f - (s+t+p)/3.0f,	minVal.w(), maxVal.w());
942 				}
943 
944 				dst.setPixel(tcu::Vec4(r, g, b, a), x, y, z);
945 			}
946 		}
947 	}
948 }
949 
fillWithComponentGradientsStyled(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal,GradientStyle style)950 void fillWithComponentGradientsStyled (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal, GradientStyle style)
951 {
952 	if (isCombinedDepthStencilType(access.getFormat().type))
953 	{
954 		const bool hasDepth		= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::D;
955 		const bool hasStencil	= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::S;
956 
957 		DE_ASSERT(hasDepth || hasStencil);
958 
959 		// For combined formats, treat D and S as separate channels
960 		if (hasDepth)
961 			fillWithComponentGradientsStyled(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_DEPTH), minVal, maxVal, style);
962 		if (hasStencil)
963 			fillWithComponentGradientsStyled(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_STENCIL), minVal.swizzle(3,2,1,0), maxVal.swizzle(3,2,1,0), style);
964 	}
965 	else
966 	{
967 		if (access.getHeight() == 1 && access.getDepth() == 1)
968 			fillWithComponentGradients1D(access, minVal, maxVal, style);
969 		else if (access.getDepth() == 1)
970 			fillWithComponentGradients2D(access, minVal, maxVal, style);
971 		else
972 			fillWithComponentGradients3D(access, minVal, maxVal, style);
973 	}
974 }
975 
fillWithComponentGradients(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)976 void fillWithComponentGradients (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
977 {
978 	fillWithComponentGradientsStyled(access, minVal, maxVal, GRADIENT_STYLE_OLD);
979 }
980 
fillWithComponentGradients2(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)981 void fillWithComponentGradients2 (const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
982 {
983 	fillWithComponentGradientsStyled(access, minVal, maxVal, GRADIENT_STYLE_NEW);
984 }
985 
fillWithComponentGradients3(const PixelBufferAccess & access,const Vec4 & minVal,const Vec4 & maxVal)986 void fillWithComponentGradients3(const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal)
987 {
988 	fillWithComponentGradientsStyled(access, minVal, maxVal, GRADIENT_STYLE_PYRAMID);
989 }
990 
fillWithGrid1D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)991 static void fillWithGrid1D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
992 {
993 	for (int x = 0; x < access.getWidth(); x++)
994 	{
995 		int mx = (x / cellSize) % 2;
996 
997 		if (mx)
998 			access.setPixel(colorB, x, 0);
999 		else
1000 			access.setPixel(colorA, x, 0);
1001 	}
1002 }
1003 
fillWithGrid2D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)1004 static void fillWithGrid2D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
1005 {
1006 	for (int y = 0; y < access.getHeight(); y++)
1007 	{
1008 		for (int x = 0; x < access.getWidth(); x++)
1009 		{
1010 			int mx = (x / cellSize) % 2;
1011 			int my = (y / cellSize) % 2;
1012 
1013 			if (mx ^ my)
1014 				access.setPixel(colorB, x, y);
1015 			else
1016 				access.setPixel(colorA, x, y);
1017 		}
1018 	}
1019 }
1020 
fillWithGrid3D(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)1021 static void fillWithGrid3D (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
1022 {
1023 	for (int z = 0; z < access.getDepth(); z++)
1024 	{
1025 		for (int y = 0; y < access.getHeight(); y++)
1026 		{
1027 			for (int x = 0; x < access.getWidth(); x++)
1028 			{
1029 				int mx = (x / cellSize) % 2;
1030 				int my = (y / cellSize) % 2;
1031 				int mz = (z / cellSize) % 2;
1032 
1033 				if (mx ^ my ^ mz)
1034 					access.setPixel(colorB, x, y, z);
1035 				else
1036 					access.setPixel(colorA, x, y, z);
1037 			}
1038 		}
1039 	}
1040 }
1041 
fillWithGrid(const PixelBufferAccess & access,int cellSize,const Vec4 & colorA,const Vec4 & colorB)1042 void fillWithGrid (const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB)
1043 {
1044 	if (isCombinedDepthStencilType(access.getFormat().type))
1045 	{
1046 		const bool hasDepth		= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::D;
1047 		const bool hasStencil	= access.getFormat().order == tcu::TextureFormat::DS || access.getFormat().order == tcu::TextureFormat::S;
1048 
1049 		DE_ASSERT(hasDepth || hasStencil);
1050 
1051 		// For combined formats, treat D and S as separate channels
1052 		if (hasDepth)
1053 			fillWithGrid(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_DEPTH), cellSize, colorA, colorB);
1054 		if (hasStencil)
1055 			fillWithGrid(getEffectiveDepthStencilAccess(access, tcu::Sampler::MODE_STENCIL), cellSize, colorA.swizzle(3,2,1,0), colorB.swizzle(3,2,1,0));
1056 	}
1057 	else
1058 	{
1059 		if (access.getHeight() == 1 && access.getDepth() == 1)
1060 			fillWithGrid1D(access, cellSize, colorA, colorB);
1061 		else if (access.getDepth() == 1)
1062 			fillWithGrid2D(access, cellSize, colorA, colorB);
1063 		else
1064 			fillWithGrid3D(access, cellSize, colorA, colorB);
1065 	}
1066 }
1067 
fillWithRepeatableGradient(const PixelBufferAccess & access,const Vec4 & colorA,const Vec4 & colorB)1068 void fillWithRepeatableGradient (const PixelBufferAccess& access, const Vec4& colorA, const Vec4& colorB)
1069 {
1070 	for (int y = 0; y < access.getHeight(); y++)
1071 	{
1072 		for (int x = 0; x < access.getWidth(); x++)
1073 		{
1074 			float s = ((float)x + 0.5f) / (float)access.getWidth();
1075 			float t = ((float)y + 0.5f) / (float)access.getHeight();
1076 
1077 			float a = s > 0.5f ? (2.0f - 2.0f*s) : 2.0f*s;
1078 			float b = t > 0.5f ? (2.0f - 2.0f*t) : 2.0f*t;
1079 
1080 			float p = deFloatClamp(deFloatSqrt(a*a + b*b), 0.0f, 1.0f);
1081 			access.setPixel(linearInterpolate(p, colorA, colorB), x, y);
1082 		}
1083 	}
1084 }
1085 
fillWithRGBAQuads(const PixelBufferAccess & dst)1086 void fillWithRGBAQuads (const PixelBufferAccess& dst)
1087 {
1088 	TCU_CHECK_INTERNAL(dst.getDepth() == 1);
1089 	int width	= dst.getWidth();
1090 	int height	= dst.getHeight();
1091 	int	left	= width/2;
1092 	int top		= height/2;
1093 
1094 	clear(getSubregion(dst, 0,		0,		0, left,		top,		1),	Vec4(1.0f, 0.0f, 0.0f, 1.0f));
1095 	clear(getSubregion(dst, left,	0,		0, width-left,	top,		1),	Vec4(0.0f, 1.0f, 0.0f, 1.0f));
1096 	clear(getSubregion(dst, 0,		top,	0, left,		height-top,	1), Vec4(0.0f, 0.0f, 1.0f, 0.0f));
1097 	clear(getSubregion(dst, left,	top,	0, width-left,	height-top, 1), Vec4(0.5f, 0.5f, 0.5f, 1.0f));
1098 }
1099 
1100 // \todo [2012-11-13 pyry] There is much better metaballs code in CL SIR value generators.
fillWithMetaballs(const PixelBufferAccess & dst,int numBalls,deUint32 seed)1101 void fillWithMetaballs (const PixelBufferAccess& dst, int numBalls, deUint32 seed)
1102 {
1103 	TCU_CHECK_INTERNAL(dst.getDepth() == 1);
1104 	std::vector<Vec2>	points(numBalls);
1105 	de::Random			rnd(seed);
1106 
1107 	for (int i = 0; i < numBalls; i++)
1108 	{
1109 		float x = rnd.getFloat();
1110 		float y = rnd.getFloat();
1111 		points[i] = (Vec2(x, y));
1112 	}
1113 
1114 	for (int y = 0; y < dst.getHeight(); y++)
1115 	for (int x = 0; x < dst.getWidth(); x++)
1116 	{
1117 		Vec2 p((float)x/(float)dst.getWidth(), (float)y/(float)dst.getHeight());
1118 
1119 		float sum = 0.0f;
1120 		for (std::vector<Vec2>::const_iterator i = points.begin(); i != points.end(); i++)
1121 		{
1122 			Vec2	d = p - *i;
1123 			float	f = 0.01f / (d.x()*d.x() + d.y()*d.y());
1124 
1125 			sum += f;
1126 		}
1127 
1128 		dst.setPixel(Vec4(sum), x, y);
1129 	}
1130 }
1131 
copy(const PixelBufferAccess & dst,const ConstPixelBufferAccess & src,const bool clearUnused)1132 void copy (const PixelBufferAccess& dst, const ConstPixelBufferAccess& src, const bool clearUnused)
1133 {
1134 	DE_ASSERT(src.getSize() == dst.getSize());
1135 
1136 	const int	width				= dst.getWidth();
1137 	const int	height				= dst.getHeight();
1138 	const int	depth				= dst.getDepth();
1139 
1140 	const int	srcPixelSize		= src.getFormat().getPixelSize();
1141 	const int	dstPixelSize		= dst.getFormat().getPixelSize();
1142 	const int	srcPixelPitch		= src.getPixelPitch();
1143 	const int	dstPixelPitch		= dst.getPixelPitch();
1144 	const bool	srcTightlyPacked	= (srcPixelSize == srcPixelPitch);
1145 	const bool	dstTightlyPacked	= (dstPixelSize == dstPixelPitch);
1146 
1147 	const bool	srcHasDepth			= (src.getFormat().order == tcu::TextureFormat::DS || src.getFormat().order == tcu::TextureFormat::D);
1148 	const bool	srcHasStencil		= (src.getFormat().order == tcu::TextureFormat::DS || src.getFormat().order == tcu::TextureFormat::S);
1149 	const bool	dstHasDepth			= (dst.getFormat().order == tcu::TextureFormat::DS || dst.getFormat().order == tcu::TextureFormat::D);
1150 	const bool	dstHasStencil		= (dst.getFormat().order == tcu::TextureFormat::DS || dst.getFormat().order == tcu::TextureFormat::S);
1151 
1152 	if (src.getFormat() == dst.getFormat() && srcTightlyPacked && dstTightlyPacked)
1153 	{
1154 		// Fast-path for matching formats.
1155 		for (int z = 0; z < depth; z++)
1156 		for (int y = 0; y < height; y++)
1157 			deMemcpy(dst.getPixelPtr(0, y, z), src.getPixelPtr(0, y, z), srcPixelSize*width);
1158 	}
1159 	else if (src.getFormat() == dst.getFormat())
1160 	{
1161 		// Bit-exact copy for matching formats.
1162 		for (int z = 0; z < depth; z++)
1163 		for (int y = 0; y < height; y++)
1164 		for (int x = 0; x < width; x++)
1165 			deMemcpy(dst.getPixelPtr(x, y, z), src.getPixelPtr(x, y, z), srcPixelSize);
1166 	}
1167 	else if (srcHasDepth || srcHasStencil || dstHasDepth || dstHasStencil)
1168 	{
1169 		DE_ASSERT((srcHasDepth && dstHasDepth) || (srcHasStencil && dstHasStencil)); // must have at least one common channel
1170 
1171 		if (dstHasDepth && srcHasDepth)
1172 		{
1173 			for (int z = 0; z < depth; z++)
1174 			for (int y = 0; y < height; y++)
1175 			for (int x = 0; x < width; x++)
1176 				dst.setPixDepth(src.getPixDepth(x, y, z), x, y, z);
1177 		}
1178 		else if (dstHasDepth && !srcHasDepth && clearUnused)
1179 		{
1180 			// consistency with color copies
1181 			tcu::clearDepth(dst, 0.0f);
1182 		}
1183 
1184 		if (dstHasStencil && srcHasStencil)
1185 		{
1186 			for (int z = 0; z < depth; z++)
1187 			for (int y = 0; y < height; y++)
1188 			for (int x = 0; x < width; x++)
1189 				dst.setPixStencil(src.getPixStencil(x, y, z), x, y, z);
1190 		}
1191 		else if (dstHasStencil && !srcHasStencil && clearUnused)
1192 		{
1193 			// consistency with color copies
1194 			tcu::clearStencil(dst, 0u);
1195 		}
1196 	}
1197 	else
1198 	{
1199 		TextureChannelClass		srcClass	= getTextureChannelClass(src.getFormat().type);
1200 		TextureChannelClass		dstClass	= getTextureChannelClass(dst.getFormat().type);
1201 		bool					srcIsInt	= srcClass == TEXTURECHANNELCLASS_SIGNED_INTEGER || srcClass == TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
1202 		bool					dstIsInt	= dstClass == TEXTURECHANNELCLASS_SIGNED_INTEGER || dstClass == TEXTURECHANNELCLASS_UNSIGNED_INTEGER;
1203 
1204 		if (srcIsInt && dstIsInt)
1205 		{
1206 			for (int z = 0; z < depth; z++)
1207 			for (int y = 0; y < height; y++)
1208 			for (int x = 0; x < width; x++)
1209 				dst.setPixel(src.getPixelInt(x, y, z), x, y, z);
1210 		}
1211 		else
1212 		{
1213 			for (int z = 0; z < depth; z++)
1214 			for (int y = 0; y < height; y++)
1215 			for (int x = 0; x < width; x++)
1216 				dst.setPixel(src.getPixel(x, y, z), x, y, z);
1217 		}
1218 	}
1219 }
1220 
scale(const PixelBufferAccess & dst,const ConstPixelBufferAccess & src,Sampler::FilterMode filter)1221 void scale (const PixelBufferAccess& dst, const ConstPixelBufferAccess& src, Sampler::FilterMode filter)
1222 {
1223 	DE_ASSERT(filter == Sampler::NEAREST || filter == Sampler::LINEAR);
1224 
1225 	Sampler sampler(Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE,
1226 					filter, filter, 0.0f, false);
1227 
1228 	float sX = (float)src.getWidth() / (float)dst.getWidth();
1229 	float sY = (float)src.getHeight() / (float)dst.getHeight();
1230 	float sZ = (float)src.getDepth() / (float)dst.getDepth();
1231 
1232 	if (dst.getDepth() == 1 && src.getDepth() == 1)
1233 	{
1234 		for (int y = 0; y < dst.getHeight(); y++)
1235 		for (int x = 0; x < dst.getWidth(); x++)
1236 			dst.setPixel(linearToSRGBIfNeeded(dst.getFormat(), src.sample2D(sampler, filter, ((float)x+0.5f)*sX, ((float)y+0.5f)*sY, 0)), x, y);
1237 	}
1238 	else
1239 	{
1240 		for (int z = 0; z < dst.getDepth(); z++)
1241 		for (int y = 0; y < dst.getHeight(); y++)
1242 		for (int x = 0; x < dst.getWidth(); x++)
1243 			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);
1244 	}
1245 }
1246 
estimatePixelValueRange(const ConstPixelBufferAccess & access,Vec4 & minVal,Vec4 & maxVal)1247 void estimatePixelValueRange (const ConstPixelBufferAccess& access, Vec4& minVal, Vec4& maxVal)
1248 {
1249 	const TextureFormat& format = access.getFormat();
1250 
1251 	switch (getTextureChannelClass(format.type))
1252 	{
1253 		case TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1254 			// Normalized unsigned formats.
1255 			minVal = Vec4(0.0f);
1256 			maxVal = Vec4(1.0f);
1257 			break;
1258 
1259 		case TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1260 			// Normalized signed formats.
1261 			minVal = Vec4(-1.0f);
1262 			maxVal = Vec4(+1.0f);
1263 			break;
1264 
1265 		default:
1266 			// \note Samples every 4/8th pixel.
1267 			minVal = Vec4(std::numeric_limits<float>::max());
1268 			maxVal = Vec4(std::numeric_limits<float>::min());
1269 
1270 			for (int z = 0; z < access.getDepth(); z += 2)
1271 			{
1272 				for (int y = 0; y < access.getHeight(); y += 2)
1273 				{
1274 					for (int x = 0; x < access.getWidth(); x += 2)
1275 					{
1276 						Vec4 p = access.getPixel(x, y, z);
1277 
1278 						minVal[0] = (deFloatIsNaN(p[0]) ? minVal[0] : de::min(minVal[0], p[0]));
1279 						minVal[1] = (deFloatIsNaN(p[1]) ? minVal[1] : de::min(minVal[1], p[1]));
1280 						minVal[2] = (deFloatIsNaN(p[2]) ? minVal[2] : de::min(minVal[2], p[2]));
1281 						minVal[3] = (deFloatIsNaN(p[3]) ? minVal[3] : de::min(minVal[3], p[3]));
1282 
1283 						maxVal[0] = (deFloatIsNaN(p[0]) ? maxVal[0] : de::max(maxVal[0], p[0]));
1284 						maxVal[1] = (deFloatIsNaN(p[1]) ? maxVal[1] : de::max(maxVal[1], p[1]));
1285 						maxVal[2] = (deFloatIsNaN(p[2]) ? maxVal[2] : de::max(maxVal[2], p[2]));
1286 						maxVal[3] = (deFloatIsNaN(p[3]) ? maxVal[3] : de::max(maxVal[3], p[3]));
1287 					}
1288 				}
1289 			}
1290 			break;
1291 	}
1292 }
1293 
computePixelScaleBias(const ConstPixelBufferAccess & access,Vec4 & scale,Vec4 & bias)1294 void computePixelScaleBias (const ConstPixelBufferAccess& access, Vec4& scale, Vec4& bias)
1295 {
1296 	Vec4 minVal, maxVal;
1297 	estimatePixelValueRange(access, minVal, maxVal);
1298 
1299 	const float eps = 0.0001f;
1300 
1301 	for (int c = 0; c < 4; c++)
1302 	{
1303 		if (maxVal[c] - minVal[c] < eps)
1304 		{
1305 			scale[c]	= (maxVal[c] < eps) ? 1.0f : (1.0f / maxVal[c]);
1306 			bias[c]		= (c == 3) ? (1.0f - maxVal[c]*scale[c]) : (0.0f - minVal[c]*scale[c]);
1307 		}
1308 		else
1309 		{
1310 			scale[c]	= 1.0f / (maxVal[c] - minVal[c]);
1311 			bias[c]		= 0.0f - minVal[c]*scale[c];
1312 		}
1313 	}
1314 }
1315 
getCubeArrayFaceIndex(CubeFace face)1316 int getCubeArrayFaceIndex (CubeFace face)
1317 {
1318 	DE_ASSERT((int)face >= 0 && face < CUBEFACE_LAST);
1319 
1320 	switch (face)
1321 	{
1322 		case CUBEFACE_POSITIVE_X:	return 0;
1323 		case CUBEFACE_NEGATIVE_X:	return 1;
1324 		case CUBEFACE_POSITIVE_Y:	return 2;
1325 		case CUBEFACE_NEGATIVE_Y:	return 3;
1326 		case CUBEFACE_POSITIVE_Z:	return 4;
1327 		case CUBEFACE_NEGATIVE_Z:	return 5;
1328 
1329 		default:
1330 			return -1;
1331 	}
1332 }
1333 
packRGB999E5(const tcu::Vec4 & color)1334 deUint32 packRGB999E5 (const tcu::Vec4& color)
1335 {
1336 	const int	mBits	= 9;
1337 	const int	eBits	= 5;
1338 	const int	eBias	= 15;
1339 	const int	eMax	= (1<<eBits)-1;
1340 	const float	maxVal	= (float)(((1<<mBits) - 1) * (1<<(eMax-eBias))) / (float)(1<<mBits);
1341 
1342 	float	rc		= deFloatClamp(color[0], 0.0f, maxVal);
1343 	float	gc		= deFloatClamp(color[1], 0.0f, maxVal);
1344 	float	bc		= deFloatClamp(color[2], 0.0f, maxVal);
1345 	float	maxc	= de::max(rc, de::max(gc, bc));
1346 	float	log2c	= deFloatLog2(maxc);
1347 	deInt32	floorc	= deIsInf(log2c) ? std::numeric_limits<deInt32>::min() : deFloorFloatToInt32(log2c);
1348 	int		exps	= de::max(-eBias - 1, floorc) + 1 + eBias;
1349 	float	e		= deFloatPow(2.0f, (float)(exps-eBias-mBits));
1350 	int		maxs	= deFloorFloatToInt32(maxc / e + 0.5f);
1351 
1352 	if (maxs == (1<<mBits))
1353 	{
1354 		exps++;
1355 		e *= 2.0f;
1356 	}
1357 
1358 	deUint32 rs = (deUint32)deFloorFloatToInt32(rc / e + 0.5f);
1359 	deUint32 gs = (deUint32)deFloorFloatToInt32(gc / e + 0.5f);
1360 	deUint32 bs = (deUint32)deFloorFloatToInt32(bc / e + 0.5f);
1361 
1362 	DE_ASSERT((exps & ~((1<<5)-1)) == 0);
1363 	DE_ASSERT((rs & ~((1<<9)-1)) == 0);
1364 	DE_ASSERT((gs & ~((1<<9)-1)) == 0);
1365 	DE_ASSERT((bs & ~((1<<9)-1)) == 0);
1366 
1367 	return rs | (gs << 9) | (bs << 18) | (exps << 27);
1368 }
1369 
1370 // Sampler utils
1371 
addOffset(const void * ptr,int numBytes)1372 static const void* addOffset (const void* ptr, int numBytes)
1373 {
1374 	return (const deUint8*)ptr + numBytes;
1375 }
1376 
addOffset(void * ptr,int numBytes)1377 static void* addOffset (void* ptr, int numBytes)
1378 {
1379 	return (deUint8*)ptr + numBytes;
1380 }
1381 
1382 template <typename AccessType>
toSamplerAccess(const AccessType & baseAccess,Sampler::DepthStencilMode mode)1383 static AccessType toSamplerAccess (const AccessType& baseAccess, Sampler::DepthStencilMode mode)
1384 {
1385 	// make sure to update this if type table is updated
1386 	DE_STATIC_ASSERT(TextureFormat::CHANNELTYPE_LAST == 48);
1387 
1388 	if (!isCombinedDepthStencilType(baseAccess.getFormat().type))
1389 		return baseAccess;
1390 	else
1391 	{
1392 #if (DE_ENDIANNESS == DE_LITTLE_ENDIAN)
1393 		const deUint32 uint32ByteOffsetBits0To8		= 0; //!< least significant byte in the lowest address
1394 		const deUint32 uint32ByteOffsetBits0To24	= 0;
1395 		const deUint32 uint32ByteOffsetBits8To32	= 1;
1396 		const deUint32 uint32ByteOffsetBits16To32	= 2;
1397 		const deUint32 uint32ByteOffsetBits24To32	= 3;
1398 #else
1399 		const deUint32 uint32ByteOffsetBits0To8		= 3; //!< least significant byte in the highest address
1400 		const deUint32 uint32ByteOffsetBits0To24	= 1;
1401 		const deUint32 uint32ByteOffsetBits8To32	= 0;
1402 		const deUint32 uint32ByteOffsetBits16To32	= 0;
1403 		const deUint32 uint32ByteOffsetBits24To32	= 0;
1404 #endif
1405 
1406 		// Sampled channel must exist
1407 		DE_ASSERT(baseAccess.getFormat().order == TextureFormat::DS ||
1408 				  (mode == Sampler::MODE_DEPTH && baseAccess.getFormat().order == TextureFormat::D) ||
1409 				  (mode == Sampler::MODE_STENCIL && baseAccess.getFormat().order == TextureFormat::S));
1410 
1411 		// combined formats have multiple channel classes, detect on sampler settings
1412 		switch (baseAccess.getFormat().type)
1413 		{
1414 			case TextureFormat::FLOAT_UNSIGNED_INT_24_8_REV:
1415 			{
1416 				if (mode == Sampler::MODE_DEPTH)
1417 				{
1418 					// select the float component
1419 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::FLOAT),
1420 									  baseAccess.getSize(),
1421 									  baseAccess.getPitch(),
1422 									  baseAccess.getDataPtr());
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(), 4 + uint32ByteOffsetBits0To8));
1431 				}
1432 				else
1433 				{
1434 					// unknown sampler mode
1435 					DE_ASSERT(false);
1436 					return AccessType();
1437 				}
1438 			}
1439 
1440 			case TextureFormat::UNSIGNED_INT_16_8_8:
1441 			{
1442 				if (mode == Sampler::MODE_DEPTH)
1443 				{
1444 					// select the unorm16 component
1445 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT16),
1446 									  baseAccess.getSize(),
1447 									  baseAccess.getPitch(),
1448 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits16To32));
1449 				}
1450 				else if (mode == Sampler::MODE_STENCIL)
1451 				{
1452 					// select the uint 8 component
1453 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1454 									  baseAccess.getSize(),
1455 									  baseAccess.getPitch(),
1456 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To8));
1457 				}
1458 				else
1459 				{
1460 					// unknown sampler mode
1461 					DE_ASSERT(false);
1462 					return AccessType();
1463 				}
1464 			}
1465 
1466 			case TextureFormat::UNSIGNED_INT_24_8:
1467 			{
1468 				if (mode == Sampler::MODE_DEPTH)
1469 				{
1470 					// select the unorm24 component
1471 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT24),
1472 									  baseAccess.getSize(),
1473 									  baseAccess.getPitch(),
1474 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits8To32));
1475 				}
1476 				else if (mode == Sampler::MODE_STENCIL)
1477 				{
1478 					// select the uint 8 component
1479 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1480 									  baseAccess.getSize(),
1481 									  baseAccess.getPitch(),
1482 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To8));
1483 				}
1484 				else
1485 				{
1486 					// unknown sampler mode
1487 					DE_ASSERT(false);
1488 					return AccessType();
1489 				}
1490 			}
1491 
1492 			case TextureFormat::UNSIGNED_INT_24_8_REV:
1493 			{
1494 				if (mode == Sampler::MODE_DEPTH)
1495 				{
1496 					// select the unorm24 component
1497 					return AccessType(TextureFormat(TextureFormat::D, TextureFormat::UNORM_INT24),
1498 									  baseAccess.getSize(),
1499 									  baseAccess.getPitch(),
1500 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits0To24));
1501 				}
1502 				else if (mode == Sampler::MODE_STENCIL)
1503 				{
1504 					// select the uint 8 component
1505 					return AccessType(TextureFormat(TextureFormat::S, TextureFormat::UNSIGNED_INT8),
1506 									  baseAccess.getSize(),
1507 									  baseAccess.getPitch(),
1508 									  addOffset(baseAccess.getDataPtr(), uint32ByteOffsetBits24To32));
1509 				}
1510 				else
1511 				{
1512 					// unknown sampler mode
1513 					DE_ASSERT(false);
1514 					return AccessType();
1515 				}
1516 			}
1517 
1518 			default:
1519 			{
1520 				// unknown combined format
1521 				DE_ASSERT(false);
1522 				return AccessType();
1523 			}
1524 		}
1525 	}
1526 }
1527 
getEffectiveDepthStencilAccess(const PixelBufferAccess & baseAccess,Sampler::DepthStencilMode mode)1528 PixelBufferAccess getEffectiveDepthStencilAccess (const PixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode)
1529 {
1530 	return toSamplerAccess<PixelBufferAccess>(baseAccess, mode);
1531 }
1532 
getEffectiveDepthStencilAccess(const ConstPixelBufferAccess & baseAccess,Sampler::DepthStencilMode mode)1533 ConstPixelBufferAccess getEffectiveDepthStencilAccess (const ConstPixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode)
1534 {
1535 	return toSamplerAccess<ConstPixelBufferAccess>(baseAccess, mode);
1536 }
1537 
getEffectiveDepthStencilTextureFormat(const TextureFormat & baseFormat,Sampler::DepthStencilMode mode)1538 TextureFormat getEffectiveDepthStencilTextureFormat (const TextureFormat& baseFormat, Sampler::DepthStencilMode mode)
1539 {
1540 	return toSamplerAccess(ConstPixelBufferAccess(baseFormat, IVec3(0, 0, 0), DE_NULL), mode).getFormat();
1541 }
1542 
1543 template <typename ViewType>
getEffectiveTView(const ViewType & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1544 ViewType getEffectiveTView (const ViewType& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1545 {
1546 	storage.resize(src.getNumLevels());
1547 
1548 	ViewType view = ViewType(src.getNumLevels(), &storage[0], src.isES2());
1549 
1550 	for (int levelNdx = 0; levelNdx < src.getNumLevels(); ++levelNdx)
1551 		storage[levelNdx] = tcu::getEffectiveDepthStencilAccess(src.getLevel(levelNdx), sampler.depthStencilMode);
1552 
1553 	return view;
1554 }
1555 
getEffectiveTView(const tcu::TextureCubeView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1556 tcu::TextureCubeView getEffectiveTView (const tcu::TextureCubeView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1557 {
1558 	storage.resize(tcu::CUBEFACE_LAST * src.getNumLevels());
1559 
1560 	const tcu::ConstPixelBufferAccess* storagePtrs[tcu::CUBEFACE_LAST] =
1561 	{
1562 		&storage[0 * src.getNumLevels()],
1563 		&storage[1 * src.getNumLevels()],
1564 		&storage[2 * src.getNumLevels()],
1565 		&storage[3 * src.getNumLevels()],
1566 		&storage[4 * src.getNumLevels()],
1567 		&storage[5 * src.getNumLevels()],
1568 	};
1569 
1570 	tcu::TextureCubeView view = tcu::TextureCubeView(src.getNumLevels(), storagePtrs, false);
1571 
1572 	for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; ++faceNdx)
1573 	for (int levelNdx = 0; levelNdx < src.getNumLevels(); ++levelNdx)
1574 		storage[faceNdx * src.getNumLevels() + levelNdx] = tcu::getEffectiveDepthStencilAccess(src.getLevelFace(levelNdx, (tcu::CubeFace)faceNdx), sampler.depthStencilMode);
1575 
1576 	return view;
1577 }
1578 
getEffectiveTextureView(const tcu::Texture1DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1579 tcu::Texture1DView getEffectiveTextureView (const tcu::Texture1DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1580 {
1581 	return getEffectiveTView(src, storage, sampler);
1582 }
1583 
getEffectiveTextureView(const tcu::Texture2DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1584 tcu::Texture2DView getEffectiveTextureView (const tcu::Texture2DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1585 {
1586 	return getEffectiveTView(src, storage, sampler);
1587 }
1588 
getEffectiveTextureView(const tcu::Texture3DView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1589 tcu::Texture3DView getEffectiveTextureView (const tcu::Texture3DView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1590 {
1591 	return getEffectiveTView(src, storage, sampler);
1592 }
1593 
getEffectiveTextureView(const tcu::Texture1DArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1594 tcu::Texture1DArrayView getEffectiveTextureView (const tcu::Texture1DArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1595 {
1596 	return getEffectiveTView(src, storage, sampler);
1597 }
1598 
getEffectiveTextureView(const tcu::Texture2DArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1599 tcu::Texture2DArrayView getEffectiveTextureView (const tcu::Texture2DArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1600 {
1601 	return getEffectiveTView(src, storage, sampler);
1602 }
1603 
getEffectiveTextureView(const tcu::TextureCubeView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1604 tcu::TextureCubeView getEffectiveTextureView (const tcu::TextureCubeView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1605 {
1606 	return getEffectiveTView(src, storage, sampler);
1607 }
1608 
getEffectiveTextureView(const tcu::TextureCubeArrayView & src,std::vector<tcu::ConstPixelBufferAccess> & storage,const tcu::Sampler & sampler)1609 tcu::TextureCubeArrayView getEffectiveTextureView (const tcu::TextureCubeArrayView& src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler)
1610 {
1611 	return getEffectiveTView(src, storage, sampler);
1612 }
1613 
1614 //! Returns the effective swizzle of a border color. The effective swizzle is the
1615 //! equal to first writing an RGBA color with a write swizzle and then reading
1616 //! it back using a read swizzle, i.e. BorderSwizzle(c) == readSwizzle(writeSwizzle(C))
getBorderColorReadSwizzle(TextureFormat::ChannelOrder order)1617 static const TextureSwizzle& getBorderColorReadSwizzle (TextureFormat::ChannelOrder order)
1618 {
1619 	// make sure to update these tables when channel orders are updated
1620 	DE_STATIC_ASSERT(TextureFormat::CHANNELORDER_LAST == 22);
1621 
1622 	static const TextureSwizzle INV		= {{ TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1623 	static const TextureSwizzle R		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1624 	static const TextureSwizzle A		= {{ TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_3	}};
1625 	static const TextureSwizzle I		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0	}};
1626 	static const TextureSwizzle L		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ONE	}};
1627 	static const TextureSwizzle LA		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_3	}};
1628 	static const TextureSwizzle RG		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1629 	static const TextureSwizzle RA		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_3	}};
1630 	static const TextureSwizzle RGB		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_2,		TextureSwizzle::CHANNEL_ONE	}};
1631 	static const TextureSwizzle RGBA	= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_1,		TextureSwizzle::CHANNEL_2,		TextureSwizzle::CHANNEL_3	}};
1632 	static const TextureSwizzle D		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1633 	static const TextureSwizzle S		= {{ TextureSwizzle::CHANNEL_0,		TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ZERO,	TextureSwizzle::CHANNEL_ONE	}};
1634 
1635 	const TextureSwizzle* swizzle;
1636 
1637 	switch (order)
1638 	{
1639 		case TextureFormat::R:			swizzle = &R;		break;
1640 		case TextureFormat::A:			swizzle = &A;		break;
1641 		case TextureFormat::I:			swizzle = &I;		break;
1642 		case TextureFormat::L:			swizzle = &L;		break;
1643 		case TextureFormat::LA:			swizzle = &LA;		break;
1644 		case TextureFormat::RG:			swizzle = &RG;		break;
1645 		case TextureFormat::RA:			swizzle = &RA;		break;
1646 		case TextureFormat::RGB:		swizzle = &RGB;		break;
1647 		case TextureFormat::RGBA:		swizzle = &RGBA;	break;
1648 		case TextureFormat::ARGB:		swizzle = &RGBA;	break;
1649 		case TextureFormat::ABGR:		swizzle = &RGBA;	break;
1650 		case TextureFormat::BGR:		swizzle = &RGB;		break;
1651 		case TextureFormat::BGRA:		swizzle = &RGBA;	break;
1652 		case TextureFormat::sR:			swizzle = &R;		break;
1653 		case TextureFormat::sRG:		swizzle = &RG;		break;
1654 		case TextureFormat::sRGB:		swizzle = &RGB;		break;
1655 		case TextureFormat::sRGBA:		swizzle = &RGBA;	break;
1656 		case TextureFormat::sBGR:		swizzle = &RGB;		break;
1657 		case TextureFormat::sBGRA:		swizzle = &RGBA;	break;
1658 		case TextureFormat::D:			swizzle = &D;		break;
1659 		case TextureFormat::S:			swizzle = &S;		break;
1660 
1661 		case TextureFormat::DS:
1662 			DE_ASSERT(false); // combined depth-stencil border color?
1663 			swizzle = &INV;
1664 			break;
1665 
1666 		default:
1667 			DE_ASSERT(false);
1668 			swizzle = &INV;
1669 			break;
1670 	}
1671 
1672 #ifdef DE_DEBUG
1673 
1674 	{
1675 		// check that BorderSwizzle(c) == readSwizzle(writeSwizzle(C))
1676 		const TextureSwizzle& readSwizzle	= getChannelReadSwizzle(order);
1677 		const TextureSwizzle& writeSwizzle	= getChannelWriteSwizzle(order);
1678 
1679 		for (int ndx = 0; ndx < 4; ++ndx)
1680 		{
1681 			TextureSwizzle::Channel writeRead = readSwizzle.components[ndx];
1682 			if (deInRange32(writeRead, TextureSwizzle::CHANNEL_0, TextureSwizzle::CHANNEL_3) == DE_TRUE)
1683 				writeRead = writeSwizzle.components[(int)writeRead];
1684 			DE_ASSERT(writeRead == swizzle->components[ndx]);
1685 		}
1686 	}
1687 
1688 #endif
1689 
1690 	return *swizzle;
1691 }
1692 
getNBitUnsignedIntegerVec4MaxValue(const tcu::IVec4 & numBits)1693 static tcu::UVec4 getNBitUnsignedIntegerVec4MaxValue (const tcu::IVec4& numBits)
1694 {
1695 	return tcu::UVec4((numBits[0] > 0) ? (deUintMaxValue32(numBits[0])) : (0),
1696 					  (numBits[1] > 0) ? (deUintMaxValue32(numBits[1])) : (0),
1697 					  (numBits[2] > 0) ? (deUintMaxValue32(numBits[2])) : (0),
1698 					  (numBits[3] > 0) ? (deUintMaxValue32(numBits[3])) : (0));
1699 }
1700 
getNBitSignedIntegerVec4MaxValue(const tcu::IVec4 & numBits)1701 static tcu::IVec4 getNBitSignedIntegerVec4MaxValue (const tcu::IVec4& numBits)
1702 {
1703 	return tcu::IVec4((numBits[0] > 0) ? (deIntMaxValue32(numBits[0])) : (0),
1704 					  (numBits[1] > 0) ? (deIntMaxValue32(numBits[1])) : (0),
1705 					  (numBits[2] > 0) ? (deIntMaxValue32(numBits[2])) : (0),
1706 					  (numBits[3] > 0) ? (deIntMaxValue32(numBits[3])) : (0));
1707 }
1708 
getNBitSignedIntegerVec4MinValue(const tcu::IVec4 & numBits)1709 static tcu::IVec4 getNBitSignedIntegerVec4MinValue (const tcu::IVec4& numBits)
1710 {
1711 	return tcu::IVec4((numBits[0] > 0) ? (deIntMinValue32(numBits[0])) : (0),
1712 					  (numBits[1] > 0) ? (deIntMinValue32(numBits[1])) : (0),
1713 					  (numBits[2] > 0) ? (deIntMinValue32(numBits[2])) : (0),
1714 					  (numBits[3] > 0) ? (deIntMinValue32(numBits[3])) : (0));
1715 }
1716 
getTextureBorderColorFloat(const TextureFormat & format,const Sampler & sampler)1717 static tcu::Vec4 getTextureBorderColorFloat (const TextureFormat& format, const Sampler& sampler)
1718 {
1719 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1720 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1721 	const bool						isFloat			= channelClass == tcu::TEXTURECHANNELCLASS_FLOATING_POINT;
1722 	const bool						isSigned		= channelClass != tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT;
1723 	const float						valueMin		= (isSigned) ? (-1.0f) : (0.0f);
1724 	const float						valueMax		= 1.0f;
1725 	Vec4							result;
1726 
1727 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_FLOATING_POINT ||
1728 			  channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT ||
1729 			  channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT);
1730 
1731 	for (int c = 0; c < 4; c++)
1732 	{
1733 		const TextureSwizzle::Channel map = channelMap[c];
1734 		if (map == TextureSwizzle::CHANNEL_ZERO)
1735 			result[c] = 0.0f;
1736 		else if (map == TextureSwizzle::CHANNEL_ONE)
1737 			result[c] = 1.0f;
1738 		else if (isFloat)
1739 		{
1740 			// floating point values are not clamped
1741 			result[c] = sampler.borderColor.getAccess<float>()[(int)map];
1742 		}
1743 		else
1744 		{
1745 			// fixed point values are clamped to a representable range
1746 			result[c] = de::clamp(sampler.borderColor.getAccess<float>()[(int)map], valueMin, valueMax);
1747 		}
1748 	}
1749 
1750 	return result;
1751 }
1752 
getTextureBorderColorInt(const TextureFormat & format,const Sampler & sampler)1753 static tcu::IVec4 getTextureBorderColorInt (const TextureFormat& format, const Sampler& sampler)
1754 {
1755 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1756 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1757 	const IVec4						channelBits		= getChannelBitDepth(format.type);
1758 	const IVec4						valueMin		= getNBitSignedIntegerVec4MinValue(channelBits);
1759 	const IVec4						valueMax		= getNBitSignedIntegerVec4MaxValue(channelBits);
1760 	IVec4							result;
1761 
1762 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER);
1763 	DE_UNREF(channelClass);
1764 
1765 	for (int c = 0; c < 4; c++)
1766 	{
1767 		const TextureSwizzle::Channel map = channelMap[c];
1768 		if (map == TextureSwizzle::CHANNEL_ZERO)
1769 			result[c] = 0;
1770 		else if (map == TextureSwizzle::CHANNEL_ONE)
1771 			result[c] = 1;
1772 		else
1773 		{
1774 			// integer values are clamped to a representable range
1775 			result[c] = de::clamp(sampler.borderColor.getAccess<deInt32>()[(int)map], valueMin[(int)map], valueMax[(int)map]);
1776 		}
1777 	}
1778 
1779 	return result;
1780 }
1781 
getTextureBorderColorUint(const TextureFormat & format,const Sampler & sampler)1782 static tcu::UVec4 getTextureBorderColorUint (const TextureFormat& format, const Sampler& sampler)
1783 {
1784 	const tcu::TextureChannelClass	channelClass	= getTextureChannelClass(format.type);
1785 	const TextureSwizzle::Channel*	channelMap		= getBorderColorReadSwizzle(format.order).components;
1786 	const IVec4						channelBits		= getChannelBitDepth(format.type);
1787 	const UVec4						valueMax		= getNBitUnsignedIntegerVec4MaxValue(channelBits);
1788 	UVec4							result;
1789 
1790 	DE_ASSERT(channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER);
1791 	DE_UNREF(channelClass);
1792 
1793 	for (int c = 0; c < 4; c++)
1794 	{
1795 		const TextureSwizzle::Channel map = channelMap[c];
1796 		if (map == TextureSwizzle::CHANNEL_ZERO)
1797 			result[c] = 0;
1798 		else if (map == TextureSwizzle::CHANNEL_ONE)
1799 			result[c] = 1;
1800 		else
1801 		{
1802 			// integer values are clamped to a representable range
1803 			result[c] = de::min(sampler.borderColor.getAccess<deUint32>()[(int)map], valueMax[(int)map]);
1804 		}
1805 	}
1806 
1807 	return result;
1808 }
1809 
1810 template <typename ScalarType>
sampleTextureBorder(const TextureFormat & format,const Sampler & sampler)1811 tcu::Vector<ScalarType, 4> sampleTextureBorder (const TextureFormat& format, const Sampler& sampler)
1812 {
1813 	const tcu::TextureChannelClass channelClass = getTextureChannelClass(format.type);
1814 
1815 	switch (channelClass)
1816 	{
1817 		case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
1818 		case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
1819 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
1820 			return getTextureBorderColorFloat(format, sampler).cast<ScalarType>();
1821 
1822 		case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
1823 			return getTextureBorderColorInt(format, sampler).cast<ScalarType>();
1824 
1825 		case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
1826 			return getTextureBorderColorUint(format, sampler).cast<ScalarType>();
1827 
1828 		default:
1829 			DE_ASSERT(false);
1830 			return tcu::Vector<ScalarType, 4>();
1831 	}
1832 }
1833 
1834 // instantiation
1835 template tcu::Vector<float, 4>		sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1836 template tcu::Vector<deInt32, 4>	sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1837 template tcu::Vector<deUint32, 4>	sampleTextureBorder (const TextureFormat& format, const Sampler& sampler);
1838 
1839 } // tcu
1840