Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // rrutil.cpp
  5. //
  6. // Direct3D Reference Rasterizer - Utilities
  7. //
  8. //
  9. //
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "pch.cpp"
  13. #pragma hdrstop
  14. //////////////////////////////////////////////////////////////////////////////////
  15. // //
  16. // DPF support //
  17. // //
  18. //////////////////////////////////////////////////////////////////////////////////
  19. // control globals
  20. int g_iDPFLevel = 0;
  21. unsigned long g_uDPFMask = 0x0;
  22. //-----------------------------------------------------------------------------
  23. //
  24. // RRDebugPrintf(L) - Utilities to print varargs-formatted strings of debugging
  25. // info. The 'L' version takes a level into account in deciding to print or
  26. // not.
  27. //
  28. //-----------------------------------------------------------------------------
  29. void
  30. RRDebugPrintf( const char* pszFormat, ... )
  31. {
  32. char tmp[1024] = "D3DRR: ";
  33. va_list marker;
  34. va_start(marker, pszFormat);
  35. _vsnprintf(tmp+lstrlen(tmp), 1024-lstrlen(tmp), pszFormat, marker);
  36. OutputDebugString(tmp);
  37. printf(tmp);
  38. }
  39. void
  40. RRDebugPrintfL( int iLevel, const char* pszFormat, ... )
  41. {
  42. if ( (iLevel <= g_iDPFLevel) )
  43. {
  44. char tmp[1024] = "D3DRR: ";
  45. va_list marker;
  46. va_start(marker, pszFormat);
  47. _vsnprintf(tmp+lstrlen(tmp), 1024-lstrlen(tmp), pszFormat, marker);
  48. OutputDebugString(tmp);
  49. printf(tmp);
  50. }
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Assert Reporting
  55. //
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // little-bit-o-state to track file and line number reporting - this is makes
  58. // this code non-reentrant and non-threadsafe... oh well...
  59. static const char* _pszLastReportFile = NULL;
  60. static int _iLastReportLine = -1;
  61. //-----------------------------------------------------------------------------
  62. void
  63. RRAssertReport( const char* pszString, const char* pszFile, int iLine )
  64. {
  65. char szNum[33];
  66. _itoa( iLine, szNum, 10 );
  67. char szTmp[ 1024 ] = "D3DRR ASSERT: <";
  68. strcat( szTmp, szNum );
  69. char* pCur = szTmp + strlen( szTmp );
  70. char* const pEnd = szTmp + sizeof( szTmp ) / sizeof( szTmp[ 0 ] ) - 1;
  71. if( pCur < pEnd )
  72. {
  73. const char szNext[] = ",";
  74. strncpy( pCur, szNext, pEnd - pCur );
  75. pCur += sizeof( szNext ) / sizeof( szNext[ 0 ] ) - 1;
  76. pCur = min( pCur, pEnd );
  77. }
  78. if( pCur < pEnd )
  79. {
  80. const size_t uiFileLen = strlen( pszFile );
  81. strncpy( pCur, pszFile, pEnd - pCur );
  82. pCur += uiFileLen;
  83. pCur = min( pCur, pEnd );
  84. }
  85. if( pCur < pEnd )
  86. {
  87. const char szNext[] = "> ";
  88. strncpy( pCur, szNext, pEnd - pCur );
  89. pCur += sizeof( szNext ) / sizeof( szNext[ 0 ] ) - 1;
  90. pCur = min( pCur, pEnd );
  91. }
  92. if( pCur < pEnd )
  93. {
  94. const size_t uiStringLen = strlen( pszString );
  95. strncpy( pCur, pszString, pEnd - pCur );
  96. pCur += uiStringLen;
  97. pCur = min( pCur, pEnd );
  98. }
  99. if( pCur < pEnd )
  100. {
  101. const char szNext[] = "\n";
  102. strncpy( pCur, szNext, pEnd - pCur );
  103. pCur += sizeof( szNext ) / sizeof( szNext[ 0 ] ) - 1;
  104. pCur = min( pCur, pEnd );
  105. }
  106. *pEnd = '\0';
  107. OutputDebugString( szTmp );
  108. }
  109. //-----------------------------------------------------------------------------
  110. void
  111. RRAssertReportPrefix( const char* pszFile, int iLine )
  112. {
  113. _pszLastReportFile = pszFile;
  114. _iLastReportLine = iLine;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void
  118. RRAssertReportMessage( const char* pszFormat, ... )
  119. {
  120. char szTmp[1024];
  121. va_list marker;
  122. va_start( marker, pszFormat );
  123. _vsnprintf( szTmp, 1024, pszFormat, marker );
  124. RRAssertReport( szTmp, _pszLastReportFile, _iLastReportLine );
  125. }
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // //
  128. // Generic bit twiddling utilities //
  129. // //
  130. ///////////////////////////////////////////////////////////////////////////////
  131. //-----------------------------------------------------------------------------
  132. //
  133. // CountSetBits - Returns number of set bits in a multibit value (up to
  134. // 32 bits).
  135. //
  136. //-----------------------------------------------------------------------------
  137. INT32
  138. CountSetBits( UINT32 uVal, INT32 nBits )
  139. {
  140. INT32 iRet = 0;
  141. for (INT32 i=0; i<nBits; i++) {
  142. if (uVal & (0x1<<i)) { iRet++; }
  143. }
  144. return iRet;
  145. }
  146. //-----------------------------------------------------------------------------
  147. //
  148. // FindFirstSetBit - Returns index of first set bit in a multibit value
  149. // (up to 32 bits) or -1 if no bits are set.
  150. //
  151. //-----------------------------------------------------------------------------
  152. INT32
  153. FindFirstSetBit( UINT32 uVal, INT32 nBits )
  154. {
  155. for (INT32 i=0; i<nBits; i++) {
  156. if (uVal & (0x1<<i)) { return i; }
  157. }
  158. return -1;
  159. }
  160. //-----------------------------------------------------------------------------
  161. //
  162. // FindMostSignificantSetBit - Returns index of first set bit in a
  163. // multibit value (up to 32 bits) or 0 if no bits are set.
  164. //
  165. //-----------------------------------------------------------------------------
  166. INT32
  167. FindMostSignificantSetBit( UINT32 uVal, INT32 nBits )
  168. {
  169. for (INT32 i=nBits; i>=0; i--) {
  170. if (uVal & (0x1<<i)) { return i+1; }
  171. }
  172. return 0;
  173. }
  174. //-----------------------------------------------------------------------------
  175. //
  176. // FindLastSetBit - Returns index of last set bit in a multibit value
  177. // (up to 32 bits) or -1 if no bits are set.
  178. //
  179. //-----------------------------------------------------------------------------
  180. INT32
  181. FindLastSetBit( UINT32 uVal, INT32 nBits )
  182. {
  183. for (INT32 i=0; i<nBits; i++) {
  184. if (uVal & (0x1<<(nBits-i-1))) { return (nBits-i-1); }
  185. }
  186. return -1;
  187. }
  188. ///////////////////////////////////////////////////////////////////////////////
  189. // //
  190. // Arithmetic utilities //
  191. // //
  192. ///////////////////////////////////////////////////////////////////////////////
  193. //-----------------------------------------------------------------------------
  194. //
  195. // LerpColor - Performs a linear interpolation between two RRColors
  196. //
  197. // uT is in 1.5 format (1<<5 represents a unit value)
  198. //
  199. //-----------------------------------------------------------------------------
  200. void
  201. LerpColor(
  202. RRColor& Color,
  203. const RRColor& Color0, const RRColor& Color1, UINT8 uT )
  204. {
  205. FLOAT fT = (1./(FLOAT)(1<<5))*(FLOAT)uT;
  206. Color.A = Color0.A + (Color1.A - Color0.A)*fT;
  207. Color.R = Color0.R + (Color1.R - Color0.R)*fT;
  208. Color.G = Color0.G + (Color1.G - Color0.G)*fT;
  209. Color.B = Color0.B + (Color1.B - Color0.B)*fT;
  210. }
  211. //-----------------------------------------------------------------------------
  212. //
  213. // Bilerp - Performs bilinear interpolation of 4 RRColors returning one RRColor.
  214. //
  215. //-----------------------------------------------------------------------------
  216. void
  217. BiLerpColor(
  218. RRColor& OutColor,
  219. const RRColor& Color00, const RRColor& Color01,
  220. const RRColor& Color10, const RRColor& Color11,
  221. UINT8 uA, UINT8 uB )
  222. {
  223. RRColor Color0, Color1;
  224. LerpColor( Color0, Color00, Color01, uA);
  225. LerpColor( Color1, Color10, Color11, uA);
  226. LerpColor( OutColor, Color0, Color1, uB);
  227. }
  228. ///////////////////////////////////////////////////////////////////////////////
  229. //
  230. // RRAlloc method implementation
  231. //
  232. ///////////////////////////////////////////////////////////////////////////////
  233. void *
  234. RRAlloc::operator new(size_t s)
  235. {
  236. void* pMem = (void*)MEMALLOC( s );
  237. _ASSERTa( NULL != pMem, "malloc failure", return NULL; );
  238. return pMem;
  239. }
  240. void
  241. RRAlloc::operator delete(void* p, size_t)
  242. {
  243. MEMFREE( p );
  244. };
  245. //////////////////////////////////////////////////////////////////////////////////
  246. // end