1#ifndef _TCUTEXTUREUTIL_HPP
2#define _TCUTEXTUREUTIL_HPP
3/*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Texture utilities.
24 *//*--------------------------------------------------------------------*/
25
26#include "tcuDefs.hpp"
27#include "tcuTexture.hpp"
28
29namespace tcu
30{
31
32// PixelBufferAccess utilities.
33PixelBufferAccess		getSubregion				(const PixelBufferAccess& access, int x, int y, int z, int width, int height, int depth);
34ConstPixelBufferAccess	getSubregion				(const ConstPixelBufferAccess& access, int x, int y, int z, int width, int height, int depth);
35
36PixelBufferAccess		getSubregion				(const PixelBufferAccess& access, int x, int y, int width, int height);
37ConstPixelBufferAccess	getSubregion				(const ConstPixelBufferAccess& access, int x, int y, int width, int height);
38
39PixelBufferAccess		flipYAccess					(const PixelBufferAccess& access);
40ConstPixelBufferAccess	flipYAccess					(const ConstPixelBufferAccess& access);
41
42bool					isCombinedDepthStencilType	(TextureFormat::ChannelType type);
43bool					hasStencilComponent			(TextureFormat::ChannelOrder order);
44bool					hasDepthComponent			(TextureFormat::ChannelOrder order);
45
46// sRGB - linear conversion.
47Vec4					sRGBToLinear				(const Vec4& cs);
48Vec4					sRGB8ToLinear				(const UVec4& cs);
49Vec4					sRGBA8ToLinear				(const UVec4& cs);
50Vec4					linearToSRGB				(const Vec4& cl);
51bool					isSRGB						(TextureFormat format);
52
53/*--------------------------------------------------------------------*//*!
54 * \brief Color channel storage type
55 *//*--------------------------------------------------------------------*/
56enum TextureChannelClass
57{
58	TEXTURECHANNELCLASS_SIGNED_FIXED_POINT = 0,
59	TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT,
60	TEXTURECHANNELCLASS_SIGNED_INTEGER,
61	TEXTURECHANNELCLASS_UNSIGNED_INTEGER,
62	TEXTURECHANNELCLASS_FLOATING_POINT,
63
64	TEXTURECHANNELCLASS_LAST
65};
66
67TextureChannelClass		getTextureChannelClass		(TextureFormat::ChannelType channelType);
68
69/*--------------------------------------------------------------------*//*!
70 * \brief Texture access type
71 *//*--------------------------------------------------------------------*/
72enum TextureAccessType
73{
74	TEXTUREACCESSTYPE_FLOAT	= 0,		//!< Read (getPixel) or write as floating-point data
75	TEXTUREACCESSTYPE_SIGNED_INT,		//!< Read (getPixelInt) or write as signed integer data
76	TEXTUREACCESSTYPE_UNSIGNED_INT,		//!< Read (getPixelUint) or write as unsigned integer data
77
78	TEXTUREACCESSTYPE_LAST
79};
80
81bool					isAccessValid				(TextureFormat format, TextureAccessType type);
82
83/*--------------------------------------------------------------------*//*!
84 * \brief Standard parameters for texture format testing
85 *//*--------------------------------------------------------------------*/
86struct TextureFormatInfo
87{
88	Vec4	valueMin;
89	Vec4	valueMax;
90	Vec4	lookupScale;
91	Vec4	lookupBias;
92
93	TextureFormatInfo (const Vec4& valueMin_, const Vec4& valueMax_, const Vec4& lookupScale_, const Vec4& lookupBias_)
94		: valueMin		(valueMin_)
95		, valueMax		(valueMax_)
96		, lookupScale	(lookupScale_)
97		, lookupBias	(lookupBias_)
98	{
99	}
100} DE_WARN_UNUSED_TYPE;
101
102TextureFormatInfo	getTextureFormatInfo				(const TextureFormat& format);
103IVec4				getTextureFormatBitDepth			(const TextureFormat& format);
104IVec4				getTextureFormatMantissaBitDepth	(const TextureFormat& format);
105BVec4				getTextureFormatChannelMask			(const TextureFormat& format);
106
107IVec4				getFormatMinIntValue				(const TextureFormat& format);
108IVec4				getFormatMaxIntValue				(const TextureFormat& format);
109
110UVec4				getFormatMaxUintValue				(const TextureFormat& format);
111
112// Texture fill.
113void	clear							(const PixelBufferAccess& access, const Vec4& color);
114void	clear							(const PixelBufferAccess& access, const IVec4& color);
115void	clear							(const PixelBufferAccess& access, const UVec4& color);
116void	clearDepth						(const PixelBufferAccess& access, float depth);
117void	clearStencil					(const PixelBufferAccess& access, int stencil);
118void	fillWithComponentGradients		(const PixelBufferAccess& access, const Vec4& minVal, const Vec4& maxVal);
119void	fillWithGrid					(const PixelBufferAccess& access, int cellSize, const Vec4& colorA, const Vec4& colorB);
120void	fillWithRepeatableGradient		(const PixelBufferAccess& access, const Vec4& colorA, const Vec4& colorB);
121void	fillWithMetaballs				(const PixelBufferAccess& access, int numMetaballs, deUint32 seed);
122void	fillWithRGBAQuads				(const PixelBufferAccess& access);
123
124//! Copies contents of src to dst. If formats of dst and src are equal, a bit-exact copy is made.
125void	copy							(const PixelBufferAccess& dst, const ConstPixelBufferAccess& src);
126
127void	scale							(const PixelBufferAccess& dst, const ConstPixelBufferAccess& src, Sampler::FilterMode filter);
128
129void	estimatePixelValueRange			(const ConstPixelBufferAccess& access, Vec4& minVal, Vec4& maxVal);
130void	computePixelScaleBias			(const ConstPixelBufferAccess& access, Vec4& scale, Vec4& bias);
131
132int		getCubeArrayFaceIndex			(CubeFace face);
133
134//! FP32->U8 with RTE rounding (extremely fast, always accurate).
135inline deUint8 floatToU8 (float fv)
136{
137	union { float fv; deUint32 uv; deInt32 iv; } v;
138	v.fv = fv;
139
140	const deUint32	e	= (deUint32)(126-(v.iv>>23));
141	deUint32		m	= v.uv;
142
143	m &= 0x00ffffffu;
144	m |= 0x00800000u;
145	m  = (m << 8) - m;
146	m  = 0x00800000u + (m >> e);
147
148	if (e > 8)
149		m = e;
150
151	return (deUint8)(m>>24);
152}
153
154deUint32 packRGB999E5 (const tcu::Vec4& color);
155
156/*--------------------------------------------------------------------*//*!
157 * \brief Depth-stencil utilities
158 *//*--------------------------------------------------------------------*/
159
160TextureFormat				getEffectiveDepthStencilTextureFormat	(const TextureFormat& baseFormat, Sampler::DepthStencilMode mode);
161
162//! returns the currently effective access to an access with a given sampler mode, e.g.
163//! for combined depth stencil accesses and for sampler set to sample stencil returns
164//! stencil access. Identity for non-combined formats.
165PixelBufferAccess			getEffectiveDepthStencilAccess			(const PixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode);
166ConstPixelBufferAccess		getEffectiveDepthStencilAccess			(const ConstPixelBufferAccess& baseAccess, Sampler::DepthStencilMode mode);
167
168//! returns the currently effective view to an texture with a given sampler mode. Uses
169//! storage for access storage storage
170
171tcu::Texture1DView			getEffectiveTextureView					(const tcu::Texture1DView&			src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
172tcu::Texture2DView			getEffectiveTextureView					(const tcu::Texture2DView&			src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
173tcu::Texture3DView			getEffectiveTextureView					(const tcu::Texture3DView&			src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
174tcu::Texture1DArrayView		getEffectiveTextureView					(const tcu::Texture1DArrayView&		src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
175tcu::Texture2DArrayView		getEffectiveTextureView					(const tcu::Texture2DArrayView&		src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
176tcu::TextureCubeView		getEffectiveTextureView					(const tcu::TextureCubeView&		src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
177tcu::TextureCubeArrayView	getEffectiveTextureView					(const tcu::TextureCubeArrayView&	src, std::vector<tcu::ConstPixelBufferAccess>& storage, const tcu::Sampler& sampler);
178
179template <typename ScalarType>
180tcu::Vector<ScalarType, 4>	sampleTextureBorder						(const TextureFormat& format, const Sampler& sampler);
181
182} // tcu
183
184#endif // _TCUTEXTUREUTIL_HPP
185