amd/addrlib: Add support for ETC2 and ASTC formats.
[mesa.git] / src / amd / addrlib / core / addrelemlib.h
1 /*
2 * Copyright © 2014 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 /**
28 ****************************************************************************************************
29 * @file addrelemlib.h
30 * @brief Contains the class for element/pixel related functions.
31 ****************************************************************************************************
32 */
33
34 #ifndef __ELEM_LIB_H__
35 #define __ELEM_LIB_H__
36
37 #include "addrinterface.h"
38 #include "addrobject.h"
39 #include "addrcommon.h"
40
41 namespace Addr
42 {
43
44 class Lib;
45
46 // The masks for property bits within the Properties INT_32
47 union ComponentFlags
48 {
49 struct
50 {
51 UINT_32 byteAligned : 1; ///< all components are byte aligned
52 UINT_32 exportNorm : 1; ///< components support R6xx NORM compression
53 UINT_32 floatComp : 1; ///< there is at least one floating point component
54 };
55
56 UINT_32 value;
57 };
58
59 // Copy from legacy lib's NumberType
60 enum NumberType
61 {
62 // The following number types have the range [-1..1]
63 ADDR_NO_NUMBER, // This component doesn't exist and has no default value
64 ADDR_EPSILON, // Force component value to integer 0x00000001
65 ADDR_ZERO, // Force component value to integer 0x00000000
66 ADDR_ONE, // Force component value to floating point 1.0
67 // Above values don't have any bits per component (keep ADDR_ONE the last of these)
68
69 ADDR_UNORM, // Unsigned normalized (repeating fraction) full precision
70 ADDR_SNORM, // Signed normalized (repeating fraction) full precision
71 ADDR_GAMMA, // Gamma-corrected, full precision
72
73 ADDR_UNORM_R5XXRB, // Unsigned normalized (repeating fraction) for r5xx RB
74 ADDR_SNORM_R5XXRB, // Signed normalized (repeating fraction) for r5xx RB
75 ADDR_GAMMA_R5XXRB, // Gamma-corrected for r5xx RB (note: unnormalized value)
76 ADDR_UNORM_R5XXBC, // Unsigned normalized (repeating fraction) for r5xx BC
77 ADDR_SNORM_R5XXBC, // Signed normalized (repeating fraction) for r5xx BC
78 ADDR_GAMMA_R5XXBC, // Gamma-corrected for r5xx BC (note: unnormalized value)
79
80 ADDR_UNORM_R6XX, // Unsigned normalized (repeating fraction) for R6xx
81 ADDR_UNORM_R6XXDB, // Unorms for 24-bit depth: one value differs from ADDR_UNORM_R6XX
82 ADDR_SNORM_R6XX, // Signed normalized (repeating fraction) for R6xx
83 ADDR_GAMMA8_R6XX, // Gamma-corrected for r6xx
84 ADDR_GAMMA8_R7XX_TP, // Gamma-corrected for r7xx TP 12bit unorm 8.4.
85
86 ADDR_U4FLOATC, // Unsigned float: 4-bit exponent, bias=15, no NaN, clamp [0..1]
87 ADDR_GAMMA_4SEG, // Gamma-corrected, four segment approximation
88 ADDR_U0FIXED, // Unsigned 0.N-bit fixed point
89
90 // The following number types have large ranges (LEAVE ADDR_USCALED first or fix Finish routine)
91 ADDR_USCALED, // Unsigned integer converted to/from floating point
92 ADDR_SSCALED, // Signed integer converted to/from floating point
93 ADDR_USCALED_R5XXRB, // Unsigned integer to/from floating point for r5xx RB
94 ADDR_SSCALED_R5XXRB, // Signed integer to/from floating point for r5xx RB
95 ADDR_UINT_BITS, // Keep in unsigned integer form, clamped to specified range
96 ADDR_SINT_BITS, // Keep in signed integer form, clamped to specified range
97 ADDR_UINTBITS, // @@ remove Keep in unsigned integer form, use modulus to reduce bits
98 ADDR_SINTBITS, // @@ remove Keep in signed integer form, use modulus to reduce bits
99
100 // The following number types and ADDR_U4FLOATC have exponents
101 // (LEAVE ADDR_S8FLOAT first or fix Finish routine)
102 ADDR_S8FLOAT, // Signed floating point with 8-bit exponent, bias=127
103 ADDR_S8FLOAT32, // 32-bit IEEE float, passes through NaN values
104 ADDR_S5FLOAT, // Signed floating point with 5-bit exponent, bias=15
105 ADDR_S5FLOATM, // Signed floating point with 5-bit exponent, bias=15, no NaN/Inf
106 ADDR_U5FLOAT, // Signed floating point with 5-bit exponent, bias=15
107 ADDR_U3FLOATM, // Unsigned floating point with 3-bit exponent, bias=3
108
109 ADDR_S5FIXED, // Signed 5.N-bit fixed point, with rounding
110
111 ADDR_END_NUMBER // Used for range comparisons
112 };
113
114 // Copy from legacy lib's AddrElement
115 enum ElemMode
116 {
117 // These formats allow both packing an unpacking
118 ADDR_ROUND_BY_HALF, // add 1/2 and truncate when packing this element
119 ADDR_ROUND_TRUNCATE, // truncate toward 0 for sign/mag, else toward neg
120 ADDR_ROUND_DITHER, // Pack by dithering -- requires (x,y) position
121
122 // These formats only allow unpacking, no packing
123 ADDR_UNCOMPRESSED, // Elements are not compressed: one data element per pixel/texel
124 ADDR_EXPANDED, // Elements are split up and stored in multiple data elements
125 ADDR_PACKED_STD, // Elements are compressed into ExpandX by ExpandY data elements
126 ADDR_PACKED_REV, // Like ADDR_PACKED, but X order of pixels is reverved
127 ADDR_PACKED_GBGR, // Elements are compressed 4:2:2 in G1B_G0R order (high to low)
128 ADDR_PACKED_BGRG, // Elements are compressed 4:2:2 in BG1_RG0 order (high to low)
129 ADDR_PACKED_BC1, // Each data element is uncompressed to a 4x4 pixel/texel array
130 ADDR_PACKED_BC2, // Each data element is uncompressed to a 4x4 pixel/texel array
131 ADDR_PACKED_BC3, // Each data element is uncompressed to a 4x4 pixel/texel array
132 ADDR_PACKED_BC4, // Each data element is uncompressed to a 4x4 pixel/texel array
133 ADDR_PACKED_BC5, // Each data element is uncompressed to a 4x4 pixel/texel array
134 ADDR_PACKED_ASTC,
135
136 // These formats provide various kinds of compression
137 ADDR_ZPLANE_R5XX, // Compressed Zplane using r5xx architecture format
138 ADDR_ZPLANE_R6XX, // Compressed Zplane using r6xx architecture format
139 //@@ Fill in the compression modes
140
141 ADDR_END_ELEMENT // Used for range comparisons
142 };
143
144 enum DepthPlanarType
145 {
146 ADDR_DEPTH_PLANAR_NONE = 0, // No plane z/stencl
147 ADDR_DEPTH_PLANAR_R600 = 1, // R600 z and stencil planes are store within a tile
148 ADDR_DEPTH_PLANAR_R800 = 2, // R800 has separate z and stencil planes
149 };
150
151 /**
152 ****************************************************************************************************
153 * PixelFormatInfo
154 *
155 * @brief
156 * Per component info
157 *
158 ****************************************************************************************************
159 */
160 struct PixelFormatInfo
161 {
162 UINT_32 compBit[4];
163 NumberType numType[4];
164 UINT_32 compStart[4];
165 ElemMode elemMode;
166 UINT_32 comps; ///< Number of components
167 };
168
169 /**
170 ****************************************************************************************************
171 * @brief This class contains asic indepentent element related attributes and operations
172 ****************************************************************************************************
173 */
174 class ElemLib : public Object
175 {
176 protected:
177 ElemLib(Lib* pAddrLib);
178
179 public:
180
181 /// Makes this class virtual
182 virtual ~ElemLib();
183
184 static ElemLib* Create(
185 const Lib* pAddrLib);
186
187 /// The implementation is only for R6xx/R7xx, so make it virtual in case we need for R8xx
188 BOOL_32 PixGetExportNorm(
189 AddrColorFormat colorFmt,
190 AddrSurfaceNumber numberFmt, AddrSurfaceSwap swap) const;
191
192 /// Below method are asic independent, so make them just static.
193 /// Remove static if we need different operation in hwl.
194
195 VOID Flt32ToDepthPixel(
196 AddrDepthFormat format, const ADDR_FLT_32 comps[2], UINT_8 *pPixel) const;
197
198 VOID Flt32ToColorPixel(
199 AddrColorFormat format, AddrSurfaceNumber surfNum, AddrSurfaceSwap surfSwap,
200 const ADDR_FLT_32 comps[4], UINT_8 *pPixel) const;
201
202 static VOID Flt32sToInt32s(
203 ADDR_FLT_32 value, UINT_32 bits, NumberType numberType, UINT_32* pResult);
204
205 static VOID Int32sToPixel(
206 UINT_32 numComps, UINT_32* pComps, UINT_32* pCompBits, UINT_32* pCompStart,
207 ComponentFlags properties, UINT_32 resultBits, UINT_8* pPixel);
208
209 VOID PixGetColorCompInfo(
210 AddrColorFormat format, AddrSurfaceNumber number, AddrSurfaceSwap swap,
211 PixelFormatInfo* pInfo) const;
212
213 VOID PixGetDepthCompInfo(
214 AddrDepthFormat format, PixelFormatInfo* pInfo) const;
215
216 UINT_32 GetBitsPerPixel(
217 AddrFormat format, ElemMode* pElemMode,
218 UINT_32* pExpandX = NULL, UINT_32* pExpandY = NULL, UINT_32* pBitsUnused = NULL);
219
220 static VOID SetClearComps(
221 ADDR_FLT_32 comps[4], BOOL_32 clearColor, BOOL_32 float32);
222
223 VOID AdjustSurfaceInfo(
224 ElemMode elemMode, UINT_32 expandX, UINT_32 expandY,
225 UINT_32* pBpp, UINT_32* pBasePitch, UINT_32* pWidth, UINT_32* pHeight);
226
227 VOID RestoreSurfaceInfo(
228 ElemMode elemMode, UINT_32 expandX, UINT_32 expandY,
229 UINT_32* pBpp, UINT_32* pWidth, UINT_32* pHeight);
230
231 /// Checks if depth and stencil are planar inside a tile
232 BOOL_32 IsDepthStencilTilePlanar()
233 {
234 return (m_depthPlanarType == ADDR_DEPTH_PLANAR_R600) ? TRUE : FALSE;
235 }
236
237 /// Sets m_configFlags, copied from AddrLib
238 VOID SetConfigFlags(ConfigFlags flags)
239 {
240 m_configFlags = flags;
241 }
242
243 static BOOL_32 IsCompressed(AddrFormat format);
244 static BOOL_32 IsBlockCompressed(AddrFormat format);
245 static BOOL_32 IsExpand3x(AddrFormat format);
246
247 protected:
248
249 static VOID GetCompBits(
250 UINT_32 c0, UINT_32 c1, UINT_32 c2, UINT_32 c3,
251 PixelFormatInfo* pInfo,
252 ElemMode elemMode = ADDR_ROUND_BY_HALF);
253
254 static VOID GetCompType(
255 AddrColorFormat format, AddrSurfaceNumber numType,
256 PixelFormatInfo* pInfo);
257
258 static VOID GetCompSwap(
259 AddrSurfaceSwap swap, PixelFormatInfo* pInfo);
260
261 static VOID SwapComps(
262 UINT_32 c0, UINT_32 c1, PixelFormatInfo* pInfo);
263
264 private:
265
266 UINT_32 m_fp16ExportNorm; ///< If allow FP16 to be reported as EXPORT_NORM
267 DepthPlanarType m_depthPlanarType;
268
269 ConfigFlags m_configFlags; ///< Copy of AddrLib's configFlags
270 Addr::Lib* const m_pAddrLib; ///< Pointer to parent addrlib instance
271 };
272
273 } //Addr
274
275 #endif
276