2 * Copyright © 2007-2018 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); } }
102 /// @brief Macro for reporting error warning messages
105 /// This macro optionally prints an error warning message to stdout,
106 /// followed by the file name and line number where the macro was called.
107 /// The first parameter is a condition -- if it is true, nothing is done.
108 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
109 /// starting with a string. This is passed to printf() or an equivalent
110 /// in order to format the informational message. For example,
111 /// ADDR_WARN(0, ("test %d",3) ); prints out "test 3" followed by
112 /// a second line with the file name and line number.
114 #define ADDR_WARN(cond, a) \
117 ADDR_PRNT((" WARNING in file %s, line %d\n", __FILE__, __LINE__)); \
120 /// @brief Macro for reporting fatal error conditions
123 /// This macro optionally stops execution of the current routine
124 /// after printing an error warning message to stdout,
125 /// followed by the file name and line number where the macro was called.
126 /// The first parameter is a condition -- if it is true, nothing is done.
127 /// The second pararmeter MUST be a parenthesis-enclosed list of arguments,
128 /// starting with a string. This is passed to printf() or an equivalent
129 /// in order to format the informational message. For example,
130 /// ADDR_EXIT(0, ("test %d",3) ); prints out "test 3" followed by
131 /// a second line with the file name and line number, then stops execution.
133 #define ADDR_EXIT(cond, a) \
135 { ADDR_PRNT(a); ADDR_DBG_BREAK();\
140 #define ADDRDPF 1 ? (void)0 : (void)
144 #define ADDR_DBG_BREAK()
146 #define ADDR_INFO(cond, a)
148 #define ADDR_WARN(cond, a)
150 #define ADDR_EXIT(cond, a)
153 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 #define ADDR_C_ASSERT(__e) typedef char __ADDR_C_ASSERT__[(__e) ? 1 : -1]
162 ////////////////////////////////////////////////////////////////////////////////////////////////////
164 ////////////////////////////////////////////////////////////////////////////////////////////////////
165 static const UINT_32 MicroTileWidth
= 8; ///< Micro tile width, for 1D and 2D tiling
166 static const UINT_32 MicroTileHeight
= 8; ///< Micro tile height, for 1D and 2D tiling
167 static const UINT_32 ThickTileThickness
= 4; ///< Micro tile thickness, for THICK modes
168 static const UINT_32 XThickTileThickness
= 8; ///< Extra thick tiling thickness
169 static const UINT_32 PowerSaveTileBytes
= 64; ///< Nuber of bytes per tile for power save 64
170 static const UINT_32 CmaskCacheBits
= 1024; ///< Number of bits for CMASK cache
171 static const UINT_32 CmaskElemBits
= 4; ///< Number of bits for CMASK element
172 static const UINT_32 HtileCacheBits
= 16384; ///< Number of bits for HTILE cache 512*32
174 static const UINT_32 MicroTilePixels
= MicroTileWidth
* MicroTileHeight
;
176 static const INT_32 TileIndexInvalid
= TILEINDEX_INVALID
;
177 static const INT_32 TileIndexLinearGeneral
= TILEINDEX_LINEAR_GENERAL
;
178 static const INT_32 TileIndexNoMacroIndex
= -3;
184 ////////////////////////////////////////////////////////////////////////////////////////////////////
186 ////////////////////////////////////////////////////////////////////////////////////////////////////
187 static const UINT_32 MaxSurfaceHeight
= 16384;
191 ////////////////////////////////////////////////////////////////////////////////////////////////////
193 ////////////////////////////////////////////////////////////////////////////////////////////////////
194 #define BITS_PER_BYTE 8
195 #define BITS_TO_BYTES(x) ( ((x) + (BITS_PER_BYTE-1)) / BITS_PER_BYTE )
196 #define BYTES_TO_BITS(x) ( (x) * BITS_PER_BYTE )
198 /// Helper macros to select a single bit from an int (undefined later in section)
199 #define _BIT(v,b) (((v) >> (b) ) & 1)
202 ****************************************************************************************************
203 * @brief Enums to identify AddrLib type
204 ****************************************************************************************************
217 ****************************************************************************************************
221 * Neutral enums that specifies chip family.
223 ****************************************************************************************************
227 ADDR_CHIP_FAMILY_IVLD
, ///< Invalid family
228 ADDR_CHIP_FAMILY_R6XX
,
229 ADDR_CHIP_FAMILY_R7XX
,
230 ADDR_CHIP_FAMILY_R8XX
,
239 ****************************************************************************************************
243 * This structure is used to set configuration flags.
244 ****************************************************************************************************
250 /// These flags are set up internally thru AddrLib::Create() based on ADDR_CREATE_FLAGS
251 UINT_32 optimalBankSwap
: 1; ///< New bank tiling for RV770 only
252 UINT_32 noCubeMipSlicesPad
: 1; ///< Disables faces padding for cubemap mipmaps
253 UINT_32 fillSizeFields
: 1; ///< If clients fill size fields in all input and
255 UINT_32 ignoreTileInfo
: 1; ///< Don't use tile info structure
256 UINT_32 useTileIndex
: 1; ///< Make tileIndex field in input valid
257 UINT_32 useCombinedSwizzle
: 1; ///< Use combined swizzle
258 UINT_32 checkLast2DLevel
: 1; ///< Check the last 2D mip sub level
259 UINT_32 useHtileSliceAlign
: 1; ///< Do htile single slice alignment
260 UINT_32 allowLargeThickTile
: 1; ///< Allow 64*thickness*bytesPerPixel > rowSize
261 UINT_32 disableLinearOpt
: 1; ///< Disallow tile modes to be optimized to linear
262 UINT_32 use32bppFor422Fmt
: 1; ///< View 422 formats as 32 bits per pixel element
263 UINT_32 reserved
: 21; ///< Reserved bits for future use
269 ////////////////////////////////////////////////////////////////////////////////////////////////////
270 // Misc helper functions
271 ////////////////////////////////////////////////////////////////////////////////////////////////////
274 ****************************************************************************************************
278 * Xor the right-side numberOfBits bits of x.
279 ****************************************************************************************************
281 static inline UINT_32
XorReduce(
283 UINT_32 numberOfBits
)
286 UINT_32 result
= x
& 1;
288 for (i
=1; i
<numberOfBits
; i
++)
290 result
^= ((x
>>i
) & 1);
297 ****************************************************************************************************
301 * Check if the size (UINT_32) is pow 2
302 ****************************************************************************************************
304 static inline UINT_32
IsPow2(
305 UINT_32 dim
) ///< [in] dimension of miplevel
307 ADDR_ASSERT(dim
> 0);
308 return !(dim
& (dim
- 1));
312 ****************************************************************************************************
316 * Check if the size (UINT_64) is pow 2
317 ****************************************************************************************************
319 static inline UINT_64
IsPow2(
320 UINT_64 dim
) ///< [in] dimension of miplevel
322 ADDR_ASSERT(dim
> 0);
323 return !(dim
& (dim
- 1));
327 ****************************************************************************************************
331 * Align UINT_32 "x" to "align" alignment, "align" should be power of 2
332 ****************************************************************************************************
334 static inline UINT_32
PowTwoAlign(
339 // Assert that x is a power of two.
341 ADDR_ASSERT(IsPow2(align
));
342 return (x
+ (align
- 1)) & (~(align
- 1));
346 ****************************************************************************************************
350 * Align UINT_64 "x" to "align" alignment, "align" should be power of 2
351 ****************************************************************************************************
353 static inline UINT_64
PowTwoAlign(
358 // Assert that x is a power of two.
360 ADDR_ASSERT(IsPow2(align
));
361 return (x
+ (align
- 1)) & (~(align
- 1));
365 ****************************************************************************************************
369 * Get the min value between two unsigned values
370 ****************************************************************************************************
372 static inline UINT_32
Min(
376 return ((value1
< (value2
)) ? (value1
) : value2
);
380 ****************************************************************************************************
384 * Get the min value between two signed values
385 ****************************************************************************************************
387 static inline INT_32
Min(
391 return ((value1
< (value2
)) ? (value1
) : value2
);
395 ****************************************************************************************************
399 * Get the max value between two unsigned values
400 ****************************************************************************************************
402 static inline UINT_32
Max(
406 return ((value1
> (value2
)) ? (value1
) : value2
);
410 ****************************************************************************************************
414 * Get the max value between two signed values
415 ****************************************************************************************************
417 static inline INT_32
Max(
421 return ((value1
> (value2
)) ? (value1
) : value2
);
425 ****************************************************************************************************
429 * Compute the mipmap's next level dim size
430 ****************************************************************************************************
432 static inline UINT_32
NextPow2(
433 UINT_32 dim
) ///< [in] dimension of miplevel
437 if (dim
> 0x7fffffff)
439 ADDR_ASSERT_ALWAYS();
454 ****************************************************************************************************
458 * Compute log of base 2 no matter the target is power of 2 or not
459 ****************************************************************************************************
461 static inline UINT_32
Log2NonPow2(
462 UINT_32 x
) ///< [in] the value should calculate log based 2
477 ****************************************************************************************************
481 * Compute log of base 2
482 ****************************************************************************************************
484 static inline UINT_32
Log2(
485 UINT_32 x
) ///< [in] the value should calculate log based 2
487 // Assert that x is a power of two.
488 ADDR_ASSERT(IsPow2(x
));
490 return Log2NonPow2(x
);
494 ****************************************************************************************************
498 * Compute log of base 2 quickly (<= 16)
499 ****************************************************************************************************
501 static inline UINT_32
QLog2(
502 UINT_32 x
) ///< [in] the value should calculate log based 2
504 ADDR_ASSERT(x
<= 16);
526 ADDR_ASSERT_ALWAYS();
533 ****************************************************************************************************
537 * NULL pointer safe assignment
538 ****************************************************************************************************
540 static inline VOID
SafeAssign(
541 UINT_32
* pLVal
, ///< [in] Pointer to left val
542 UINT_32 rVal
) ///< [in] Right value
551 ****************************************************************************************************
555 * NULL pointer safe assignment for 64bit values
556 ****************************************************************************************************
558 static inline VOID
SafeAssign(
559 UINT_64
* pLVal
, ///< [in] Pointer to left val
560 UINT_64 rVal
) ///< [in] Right value
569 ****************************************************************************************************
573 * NULL pointer safe assignment for AddrTileMode
574 ****************************************************************************************************
576 static inline VOID
SafeAssign(
577 AddrTileMode
* pLVal
, ///< [in] Pointer to left val
578 AddrTileMode rVal
) ///< [in] Right value
587 ****************************************************************************************************
592 ****************************************************************************************************
594 static inline UINT_32
RoundHalf(
595 UINT_32 x
) ///< [in] input value
600 return (x
>> 1) + (x
& 1);
607 ****************************************************************************************************
611 * Calculate sum of a geometric progression whose ratio is 1/2
612 ****************************************************************************************************
614 static inline UINT_32
SumGeo(
615 UINT_32 base
, ///< [in] First term in the geometric progression
616 UINT_32 num
) ///< [in] Number of terms to be added into sum
618 ADDR_ASSERT(base
> 0);
622 for (; (i
< num
) && (base
> 1); i
++)
625 base
= RoundHalf(base
);
633 ****************************************************************************************************
637 * Extract bit N value (0 or 1) of a UINT32 value.
638 ****************************************************************************************************
640 static inline UINT_32
GetBit(
641 UINT_32 u32
, ///< [in] UINT32 value
642 UINT_32 pos
) ///< [in] bit position from LSB, valid range is [0..31]
644 ADDR_ASSERT(pos
<= 31);
646 return (u32
>> pos
) & 0x1;
650 ****************************************************************************************************
654 * Copy 'bitsNum' bits from src start from srcStartPos into destination from dstStartPos
655 * srcStartPos: 0~31 for UINT_32
656 * bitsNum : 1~32 for UINT_32
657 * srcStartPos: 0~31 for UINT_32
660 * src : b[31] b[30] b[29] ... ... ... ... ... ... ... ... b[end]..b[beg] ... b[1] b[0]
661 * || Bits num || copy length || Bits num ||
662 * dst : b[31] b[30] b[29] ... b[end]..b[beg] ... ... ... ... ... ... ... ... b[1] b[0]
665 ****************************************************************************************************
667 static inline UINT_32
GetBits(
673 ADDR_ASSERT((srcStartPos
< 32) && (dstStartPos
< 32) && (bitsNum
> 0));
674 ADDR_ASSERT((bitsNum
+ dstStartPos
<= 32) && (bitsNum
+ srcStartPos
<= 32));
676 return ((src
>> srcStartPos
) << (32 - bitsNum
)) >> (32 - bitsNum
- dstStartPos
);
680 ****************************************************************************************************
684 * Generate 2D Morton interleave code with num lowest bits in each channel
685 ****************************************************************************************************
687 static inline UINT_32
MortonGen2d(
688 UINT_32 x
, ///< [in] First channel
689 UINT_32 y
, ///< [in] Second channel
690 UINT_32 num
) ///< [in] Number of bits extracted from each channel
694 for (UINT_32 i
= 0; i
< num
; i
++)
696 mort
|= (GetBit(y
, i
) << (2 * i
));
697 mort
|= (GetBit(x
, i
) << (2 * i
+ 1));
704 ****************************************************************************************************
708 * Generate 3D Morton interleave code with num lowest bits in each channel
709 ****************************************************************************************************
711 static inline UINT_32
MortonGen3d(
712 UINT_32 x
, ///< [in] First channel
713 UINT_32 y
, ///< [in] Second channel
714 UINT_32 z
, ///< [in] Third channel
715 UINT_32 num
) ///< [in] Number of bits extracted from each channel
719 for (UINT_32 i
= 0; i
< num
; i
++)
721 mort
|= (GetBit(z
, i
) << (3 * i
));
722 mort
|= (GetBit(y
, i
) << (3 * i
+ 1));
723 mort
|= (GetBit(x
, i
) << (3 * i
+ 2));
730 ****************************************************************************************************
734 * Return reversed lowest num bits of v: v[0]v[1]...v[num-2]v[num-1]
735 ****************************************************************************************************
737 static inline UINT_32
ReverseBitVector(
738 UINT_32 v
, ///< [in] Reverse operation base value
739 UINT_32 num
) ///< [in] Number of bits used in reverse operation
743 for (UINT_32 i
= 0; i
< num
; i
++)
745 reverse
|= (GetBit(v
, num
- 1 - i
) << i
);
752 ****************************************************************************************************
756 * 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]
757 ****************************************************************************************************
759 static inline UINT_32
FoldXor2d(
760 UINT_32 v
, ///< [in] Xor operation base value
761 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
763 return (v
& ((1 << num
) - 1)) ^ ReverseBitVector(v
>> num
, num
);
767 ****************************************************************************************************
771 * Return v[0] | v[2] | v[4] | v[6]... | v[2*num - 2]
772 ****************************************************************************************************
774 static inline UINT_32
DeMort(
775 UINT_32 v
, ///< [in] DeMort operation base value
776 UINT_32 num
) ///< [in] Number of bits used in fold DeMort operation
780 for (UINT_32 i
= 0; i
< num
; i
++)
782 d
|= ((v
& (1 << (i
<< 1))) >> i
);
789 ****************************************************************************************************
793 * 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]
794 ****************************************************************************************************
796 static inline UINT_32
FoldXor3d(
797 UINT_32 v
, ///< [in] Xor operation base value
798 UINT_32 num
) ///< [in] Number of bits used in fold xor operation
800 UINT_32 t
= v
& ((1 << num
) - 1);
801 t
^= ReverseBitVector(DeMort(v
>> num
, num
), num
);
802 t
^= ReverseBitVector(DeMort(v
>> (num
+ 1), num
), num
);
808 ****************************************************************************************************
812 * Set channel initialization value via a return value
813 ****************************************************************************************************
815 static inline ADDR_CHANNEL_SETTING
InitChannel(
816 UINT_32 valid
, ///< [in] valid setting
817 UINT_32 channel
, ///< [in] channel setting
818 UINT_32 index
) ///< [in] index setting
820 ADDR_CHANNEL_SETTING t
;
829 ****************************************************************************************************
833 * Set channel initialization value via channel pointer
834 ****************************************************************************************************
836 static inline VOID
InitChannel(
837 UINT_32 valid
, ///< [in] valid setting
838 UINT_32 channel
, ///< [in] channel setting
839 UINT_32 index
, ///< [in] index setting
840 ADDR_CHANNEL_SETTING
*pChanSet
) ///< [out] channel setting to be initialized
842 pChanSet
->valid
= valid
;
843 pChanSet
->channel
= channel
;
844 pChanSet
->index
= index
;
848 ****************************************************************************************************
852 * Set channel initialization value via another channel
853 ****************************************************************************************************
855 static inline VOID
InitChannel(
856 ADDR_CHANNEL_SETTING
*pChanDst
, ///< [in] channel setting to be copied from
857 ADDR_CHANNEL_SETTING
*pChanSrc
) ///< [out] channel setting to be initialized
859 pChanDst
->valid
= pChanSrc
->valid
;
860 pChanDst
->channel
= pChanSrc
->channel
;
861 pChanDst
->index
= pChanSrc
->index
;
865 ****************************************************************************************************
866 * GetMaxValidChannelIndex
869 * Get max valid index for a specific channel
870 ****************************************************************************************************
872 static inline UINT_32
GetMaxValidChannelIndex(
873 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
874 UINT_32 searchCount
,///< [in] number of channel setting to be searched
875 UINT_32 channel
) ///< [in] channel to be searched
879 for (UINT_32 i
= 0; i
< searchCount
; i
++)
881 if (pChanSet
[i
].valid
&& (pChanSet
[i
].channel
== channel
))
883 index
= Max(index
, static_cast<UINT_32
>(pChanSet
[i
].index
));
891 ****************************************************************************************************
895 * Get bit mask which indicates which positions in the equation match the target coord
896 ****************************************************************************************************
898 static inline UINT_32
GetCoordActiveMask(
899 const ADDR_CHANNEL_SETTING
*pChanSet
, ///< [in] channel setting to be initialized
900 UINT_32 searchCount
,///< [in] number of channel setting to be searched
901 UINT_32 channel
, ///< [in] channel to be searched
902 UINT_32 index
) ///< [in] index to be searched
906 for (UINT_32 i
= 0; i
< searchCount
; i
++)
908 if ((pChanSet
[i
].valid
== TRUE
) &&
909 (pChanSet
[i
].channel
== channel
) &&
910 (pChanSet
[i
].index
== index
))
921 #endif // __ADDR_COMMON_H__