2 * Copyright © 2014 Advanced Micro Devices, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
28 ****************************************************************************************************
30 * @brief Contains the helper function and constants.
31 ****************************************************************************************************
34 #ifndef __ADDR_COMMON_H__
35 #define __ADDR_COMMON_H__
37 #include "addrinterface.h"
51 ////////////////////////////////////////////////////////////////////////////////////////////////////
52 // Platform specific debug break defines
53 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 #define ADDR_DBG_BREAK() assert(false)
57 #elif defined(__APPLE__)
58 #define ADDR_DBG_BREAK() { IOPanic("");}
60 #define ADDR_DBG_BREAK() { __debugbreak(); }
63 #define ADDR_DBG_BREAK()
65 ////////////////////////////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////////////////////////////
68 // Debug assertions used in AddrLib
69 ////////////////////////////////////////////////////////////////////////////////////////////////////
70 #if defined(_WIN32) && (_MSC_VER >= 1400)
71 #define ADDR_ANALYSIS_ASSUME(expr) __analysis_assume(expr)
73 #define ADDR_ANALYSIS_ASSUME(expr) do { (void)(expr); } while (0)
76 #define ADDR_ASSERT(__e) assert(__e)
77 #define ADDR_ASSERT_ALWAYS() ADDR_DBG_BREAK()
78 #define ADDR_UNHANDLED_CASE() ADDR_ASSERT(!"Unhandled case")
79 #define ADDR_NOT_IMPLEMENTED() ADDR_ASSERT(!"Not implemented");
80 ////////////////////////////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////////////////////////////
83 // Debug print macro from legacy address library
84 ////////////////////////////////////////////////////////////////////////////////////////////////////
87 #define ADDR_PRNT(a) Object::DebugPrint a
89 /// @brief Macro for reporting informational messages
92 /// This macro optionally prints an informational message to stdout.
93 /// The first parameter is a condition -- if it is true, nothing is done.
94 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
95 /// starting with a string. This is passed to printf() or an equivalent
96 /// in order to format the informational message. For example,
97 /// ADDR_INFO(0, ("test %d",3) ); prints out "test 3".
99 #define ADDR_INFO(cond, a) \
100 { if (!(cond)) { ADDR_PRNT(a); } }
103 /// @brief Macro for reporting error warning messages
106 /// This macro optionally prints an error warning message to stdout,
107 /// followed by the file name and line number where the macro was called.
108 /// The first parameter is a condition -- if it is true, nothing is done.
109 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
110 /// starting with a string. This is passed to printf() or an equivalent
111 /// in order to format the informational message. For example,
112 /// ADDR_WARN(0, ("test %d",3) ); prints out "test 3" followed by
113 /// a second line with the file name and line number.
115 #define ADDR_WARN(cond, a) \
118 ADDR_PRNT((" WARNING in file %s, line %d\n", __FILE__, __LINE__)); \
122 /// @brief Macro for reporting fatal error conditions
125 /// This macro optionally stops execution of the current routine
126 /// after printing an error warning message to stdout,
127 /// followed by the file name and line number where the macro was called.
128 /// The first parameter is a condition -- if it is true, nothing is done.
129 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
130 /// starting with a string. This is passed to printf() or an equivalent
131 /// in order to format the informational message. For example,
132 /// ADDR_EXIT(0, ("test %d",3) ); prints out "test 3" followed by
133 /// a second line with the file name and line number, then stops execution.
135 #define ADDR_EXIT(cond, a) \
137 { ADDR_PRNT(a); ADDR_DBG_BREAK();\
142 #define ADDRDPF 1 ? (void)0 : (void)
146 #define ADDR_DBG_BREAK()
148 #define ADDR_INFO(cond, a)
150 #define ADDR_WARN(cond, a)
152 #define ADDR_EXIT(cond, a)
155 ////////////////////////////////////////////////////////////////////////////////////////////////////
157 #define ADDR_C_ASSERT(__e) typedef char __ADDR_C_ASSERT__[(__e) ? 1 : -1]
164 ////////////////////////////////////////////////////////////////////////////////////////////////////
166 ////////////////////////////////////////////////////////////////////////////////////////////////////
167 static const UINT_32 MicroTileWidth
= 8; ///< Micro tile width, for 1D and 2D tiling
168 static const UINT_32 MicroTileHeight
= 8; ///< Micro tile height, for 1D and 2D tiling
169 static const UINT_32 ThickTileThickness
= 4; ///< Micro tile thickness, for THICK modes
170 static const UINT_32 XThickTileThickness
= 8; ///< Extra thick tiling thickness
171 static const UINT_32 PowerSaveTileBytes
= 64; ///< Nuber of bytes per tile for power save 64
172 static const UINT_32 CmaskCacheBits
= 1024; ///< Number of bits for CMASK cache
173 static const UINT_32 CmaskElemBits
= 4; ///< Number of bits for CMASK element
174 static const UINT_32 HtileCacheBits
= 16384; ///< Number of bits for HTILE cache 512*32
176 static const UINT_32 MicroTilePixels
= MicroTileWidth
* MicroTileHeight
;
178 static const INT_32 TileIndexInvalid
= TILEINDEX_INVALID
;
179 static const INT_32 TileIndexLinearGeneral
= TILEINDEX_LINEAR_GENERAL
;
180 static const INT_32 TileIndexNoMacroIndex
= -3;
186 ////////////////////////////////////////////////////////////////////////////////////////////////////
188 ////////////////////////////////////////////////////////////////////////////////////////////////////
189 static const UINT_32 MaxSurfaceHeight
= 16384;
193 ////////////////////////////////////////////////////////////////////////////////////////////////////
195 ////////////////////////////////////////////////////////////////////////////////////////////////////
196 #define BITS_PER_BYTE 8
197 #define BITS_TO_BYTES(x) ( ((x) + (BITS_PER_BYTE-1)) / BITS_PER_BYTE )
198 #define BYTES_TO_BITS(x) ( (x) * BITS_PER_BYTE )
200 /// Helper macros to select a single bit from an int (undefined later in section)
201 #define _BIT(v,b) (((v) >> (b) ) & 1)
204 ****************************************************************************************************
205 * @brief Enums to identify AddrLib type
206 ****************************************************************************************************
219 ****************************************************************************************************
223 * Neutral enums that specifies chip family.
225 ****************************************************************************************************
229 ADDR_CHIP_FAMILY_IVLD
, ///< Invalid family
230 ADDR_CHIP_FAMILY_R6XX
,
231 ADDR_CHIP_FAMILY_R7XX
,
232 ADDR_CHIP_FAMILY_R8XX
,
241 ****************************************************************************************************
245 * This structure is used to set configuration flags.
246 ****************************************************************************************************
252 /// These flags are set up internally thru AddrLib::Create() based on ADDR_CREATE_FLAGS
253 UINT_32 optimalBankSwap
: 1; ///< New bank tiling for RV770 only
254 UINT_32 noCubeMipSlicesPad
: 1; ///< Disables faces padding for cubemap mipmaps
255 UINT_32 fillSizeFields
: 1; ///< If clients fill size fields in all input and
257 UINT_32 ignoreTileInfo
: 1; ///< Don't use tile info structure
258 UINT_32 useTileIndex
: 1; ///< Make tileIndex field in input valid
259 UINT_32 useCombinedSwizzle
: 1; ///< Use combined swizzle
260 UINT_32 checkLast2DLevel
: 1; ///< Check the last 2D mip sub level
261 UINT_32 useHtileSliceAlign
: 1; ///< Do htile single slice alignment
262 UINT_32 allowLargeThickTile
: 1; ///< Allow 64*thickness*bytesPerPixel > rowSize
263 UINT_32 disableLinearOpt
: 1; ///< Disallow tile modes to be optimized to linear
264 UINT_32 reserved
: 22; ///< Reserved bits for future use
270 ////////////////////////////////////////////////////////////////////////////////////////////////////
271 // Misc helper functions
272 ////////////////////////////////////////////////////////////////////////////////////////////////////
275 ****************************************************************************************************
279 * Xor the right-side numberOfBits bits of x.
280 ****************************************************************************************************
282 static inline UINT_32
XorReduce(
284 UINT_32 numberOfBits
)
287 UINT_32 result
= x
& 1;
289 for (i
=1; i
<numberOfBits
; i
++)
291 result
^= ((x
>>i
) & 1);
298 ****************************************************************************************************
302 * Check if the size (UINT_32) is pow 2
303 ****************************************************************************************************
305 static inline UINT_32
IsPow2(
306 UINT_32 dim
) ///< [in] dimension of miplevel
308 ADDR_ASSERT(dim
> 0);
309 return !(dim
& (dim
- 1));
313 ****************************************************************************************************
317 * Check if the size (UINT_64) is pow 2
318 ****************************************************************************************************
320 static inline UINT_64
IsPow2(
321 UINT_64 dim
) ///< [in] dimension of miplevel
323 ADDR_ASSERT(dim
> 0);
324 return !(dim
& (dim
- 1));
328 ****************************************************************************************************
332 * Align UINT_32 "x" to "align" alignment, "align" should be power of 2
333 ****************************************************************************************************
335 static inline UINT_32
PowTwoAlign(
340 // Assert that x is a power of two.
342 ADDR_ASSERT(IsPow2(align
));
343 return (x
+ (align
- 1)) & (~(align
- 1));
347 ****************************************************************************************************
351 * Align UINT_64 "x" to "align" alignment, "align" should be power of 2
352 ****************************************************************************************************
354 static inline UINT_64
PowTwoAlign(
359 // Assert that x is a power of two.
361 ADDR_ASSERT(IsPow2(align
));
362 return (x
+ (align
- 1)) & (~(align
- 1));
366 ****************************************************************************************************
370 * Get the min value between two unsigned values
371 ****************************************************************************************************
373 static inline UINT_32
Min(
377 return ((value1
< (value2
)) ? (value1
) : value2
);
381 ****************************************************************************************************
385 * Get the min value between two signed values
386 ****************************************************************************************************
388 static inline INT_32
Min(
392 return ((value1
< (value2
)) ? (value1
) : value2
);
396 ****************************************************************************************************
400 * Get the max value between two unsigned values
401 ****************************************************************************************************
403 static inline UINT_32
Max(
407 return ((value1
> (value2
)) ? (value1
) : value2
);
411 ****************************************************************************************************
415 * Get the max value between two signed values
416 ****************************************************************************************************
418 static inline INT_32
Max(
422 return ((value1
> (value2
)) ? (value1
) : value2
);
426 ****************************************************************************************************
430 * Compute the mipmap's next level dim size
431 ****************************************************************************************************
433 static inline UINT_32
NextPow2(
434 UINT_32 dim
) ///< [in] dimension of miplevel
438 if (dim
> 0x7fffffff)
440 ADDR_ASSERT_ALWAYS();
455 ****************************************************************************************************
459 * Compute log of base 2 no matter the target is power of 2 or not
460 ****************************************************************************************************
462 static inline UINT_32
Log2NonPow2(
463 UINT_32 x
) ///< [in] the value should calculate log based 2
478 ****************************************************************************************************
482 * Compute log of base 2
483 ****************************************************************************************************
485 static inline UINT_32
Log2(
486 UINT_32 x
) ///< [in] the value should calculate log based 2
488 // Assert that x is a power of two.
489 ADDR_ASSERT(IsPow2(x
));
491 return Log2NonPow2(x
);
495 ****************************************************************************************************
499 * Compute log of base 2 quickly (<= 16)
500 ****************************************************************************************************
502 static inline UINT_32
QLog2(
503 UINT_32 x
) ///< [in] the value should calculate log based 2
505 ADDR_ASSERT(x
<= 16);
527 ADDR_ASSERT_ALWAYS();
534 ****************************************************************************************************
538 * NULL pointer safe assignment
539 ****************************************************************************************************
541 static inline VOID
SafeAssign(
542 UINT_32
* pLVal
, ///< [in] Pointer to left val
543 UINT_32 rVal
) ///< [in] Right value
552 ****************************************************************************************************
556 * NULL pointer safe assignment for 64bit values
557 ****************************************************************************************************
559 static inline VOID
SafeAssign(
560 UINT_64
* pLVal
, ///< [in] Pointer to left val
561 UINT_64 rVal
) ///< [in] Right value
570 ****************************************************************************************************
574 * NULL pointer safe assignment for AddrTileMode
575 ****************************************************************************************************
577 static inline VOID
SafeAssign(
578 AddrTileMode
* pLVal
, ///< [in] Pointer to left val
579 AddrTileMode rVal
) ///< [in] Right value
588 ****************************************************************************************************
593 ****************************************************************************************************
595 static inline UINT_32
RoundHalf(
596 UINT_32 x
) ///< [in] input value
601 return (x
>> 1) + (x
& 1);
608 ****************************************************************************************************
612 * Calculate sum of a geometric progression whose ratio is 1/2
613 ****************************************************************************************************
615 static inline UINT_32
SumGeo(
616 UINT_32 base
, ///< [in] First term in the geometric progression
617 UINT_32 num
) ///< [in] Number of terms to be added into sum
619 ADDR_ASSERT(base
> 0);
623 for (; (i
< num
) && (base
> 1); i
++)
626 base
= RoundHalf(base
);
634 ****************************************************************************************************
638 * Extract bit N value (0 or 1) of a UINT32 value.
639 ****************************************************************************************************
641 static inline UINT_32
GetBit(
642 UINT_32 u32
, ///< [in] UINT32 value
643 UINT_32 pos
) ///< [in] bit position from LSB, valid range is [0..31]
645 ADDR_ASSERT(pos
<= 31);
647 return (u32
>> pos
) & 0x1;
651 ****************************************************************************************************
655 * Copy 'bitsNum' bits from src start from srcStartPos into destination from dstStartPos
656 * srcStartPos: 0~31 for UINT_32
657 * bitsNum : 1~32 for UINT_32
658 * srcStartPos: 0~31 for UINT_32
661 * src : b[31] b[30] b[29] ... ... ... ... ... ... ... ... b[end]..b[beg] ... b[1] b[0]
662 * || Bits num || copy length || Bits num ||
663 * dst : b[31] b[30] b[29] ... b[end]..b[beg] ... ... ... ... ... ... ... ... b[1] b[0]
666 ****************************************************************************************************
668 static inline UINT_32
GetBits(
674 ADDR_ASSERT((srcStartPos
< 32) && (dstStartPos
< 32) && (bitsNum
> 0));
675 ADDR_ASSERT((bitsNum
+ dstStartPos
<= 32) && (bitsNum
+ srcStartPos
<= 32));
677 return ((src
>> srcStartPos
) << (32 - bitsNum
)) >> (32 - bitsNum
- dstStartPos
);
681 ****************************************************************************************************
685 * Generate 2D Morton interleave code with num lowest bits in each channel
686 ****************************************************************************************************
688 static inline UINT_32
MortonGen2d(
689 UINT_32 x
, ///< [in] First channel
690 UINT_32 y
, ///< [in] Second channel
691 UINT_32 num
) ///< [in] Number of bits extracted from each channel
695 for (UINT_32 i
= 0; i
< num
; i
++)
697 mort
|= (GetBit(y
, i
) << (2 * i
));
698 mort
|= (GetBit(x
, i
) << (2 * i
+ 1));
705 ****************************************************************************************************
709 * Generate 3D Morton interleave code with num lowest bits in each channel
710 ****************************************************************************************************
712 static inline UINT_32
MortonGen3d(
713 UINT_32 x
, ///< [in] First channel
714 UINT_32 y
, ///< [in] Second channel
715 UINT_32 z
, ///< [in] Third channel
716 UINT_32 num
) ///< [in] Number of bits extracted from each channel
720 for (UINT_32 i
= 0; i
< num
; i
++)
722 mort
|= (GetBit(z
, i
) << (3 * i
));
723 mort
|= (GetBit(y
, i
) << (3 * i
+ 1));
724 mort
|= (GetBit(x
, i
) << (3 * i
+ 2));
731 ****************************************************************************************************
735 * Return reversed lowest num bits of v: v[0]v[1]...v[num-2]v[num-1]
736 ****************************************************************************************************
738 static inline UINT_32
ReverseBitVector(
739 UINT_32 v
, ///< [in] Reverse operation base value
740 UINT_32 num
) ///< [in] Number of bits used in reverse operation
744 for (UINT_32 i
= 0; i
< num
; i
++)
746 reverse
|= (GetBit(v
, num
- 1 - i
) << i
);
753 ****************************************************************************************************
757 * Xor bit vector v[num-1]v[num-2]...v[1]v[0] with v[num]v[num+1]...v[2*num-2]v[2*num-1]
758 ****************************************************************************************************
760 static inline UINT_32
FoldXor2d(
761 UINT_32 v
, ///< [in] Xor operation base value
762 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
764 return (v
& ((1 << num
) - 1)) ^ ReverseBitVector(v
>> num
, num
);
768 ****************************************************************************************************
772 * Return v[0] | v[2] | v[4] | v[6]... | v[2*num - 2]
773 ****************************************************************************************************
775 static inline UINT_32
DeMort(
776 UINT_32 v
, ///< [in] DeMort operation base value
777 UINT_32 num
) ///< [in] Number of bits used in fold DeMort operation
781 for (UINT_32 i
= 0; i
< num
; i
++)
783 d
|= ((v
& (1 << (i
<< 1))) >> i
);
790 ****************************************************************************************************
794 * v[0]...v[num-1] ^ v[3*num-1]v[3*num-3]...v[num+2]v[num] ^ v[3*num-2]...v[num+1]v[num-1]
795 ****************************************************************************************************
797 static inline UINT_32
FoldXor3d(
798 UINT_32 v
, ///< [in] Xor operation base value
799 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
801 UINT_32 t
= v
& ((1 << num
) - 1);
802 t
^= ReverseBitVector(DeMort(v
>> num
, num
), num
);
803 t
^= ReverseBitVector(DeMort(v
>> (num
+ 1), num
), num
);
809 ****************************************************************************************************
813 * Set channel initialization value via a return value
814 ****************************************************************************************************
816 static inline ADDR_CHANNEL_SETTING
InitChannel(
817 UINT_32 valid
, ///< [in] valid setting
818 UINT_32 channel
, ///< [in] channel setting
819 UINT_32 index
) ///< [in] index setting
821 ADDR_CHANNEL_SETTING t
;
830 ****************************************************************************************************
834 * Set channel initialization value via channel pointer
835 ****************************************************************************************************
837 static inline VOID
InitChannel(
838 UINT_32 valid
, ///< [in] valid setting
839 UINT_32 channel
, ///< [in] channel setting
840 UINT_32 index
, ///< [in] index setting
841 ADDR_CHANNEL_SETTING
*pChanSet
) ///< [out] channel setting to be initialized
843 pChanSet
->valid
= valid
;
844 pChanSet
->channel
= channel
;
845 pChanSet
->index
= index
;
850 ****************************************************************************************************
854 * Set channel initialization value via another channel
855 ****************************************************************************************************
857 static inline VOID
InitChannel(
858 ADDR_CHANNEL_SETTING
*pChanDst
, ///< [in] channel setting to be copied from
859 ADDR_CHANNEL_SETTING
*pChanSrc
) ///< [out] channel setting to be initialized
861 pChanDst
->valid
= pChanSrc
->valid
;
862 pChanDst
->channel
= pChanSrc
->channel
;
863 pChanDst
->index
= pChanSrc
->index
;
867 ****************************************************************************************************
868 * GetMaxValidChannelIndex
871 * Get max valid index for a specific channel
872 ****************************************************************************************************
874 static inline UINT_32
GetMaxValidChannelIndex(
875 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
876 UINT_32 searchCount
,///< [in] number of channel setting to be searched
877 UINT_32 channel
) ///< [in] channel to be searched
881 for (UINT_32 i
= 0; i
< searchCount
; i
++)
883 if (pChanSet
[i
].valid
&& (pChanSet
[i
].channel
== channel
))
885 index
= Max(index
, static_cast<UINT_32
>(pChanSet
[i
].index
));
893 ****************************************************************************************************
897 * Get bit mask which indicates which positions in the equation match the target coord
898 ****************************************************************************************************
900 static inline UINT_32
GetCoordActiveMask(
901 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
902 UINT_32 searchCount
,///< [in] number of channel setting to be searched
903 UINT_32 channel
, ///< [in] channel to be searched
904 UINT_32 index
) ///< [in] index to be searched
908 for (UINT_32 i
= 0; i
< searchCount
; i
++)
910 if ((pChanSet
[i
].valid
== TRUE
) &&
911 (pChanSet
[i
].channel
== channel
) &&
912 (pChanSet
[i
].index
== index
))
923 #endif // __ADDR_COMMON_H__