Source code of Windows XP (NT5)
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.

401 lines
12 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // rdutil.cpp
  5. //
  6. // Direct3D Reference Device - 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. // RDDebugPrintf(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. RDErrorPrintf( const char* pszFormat, ... )
  31. {
  32. char tmp[1024];
  33. _snprintf( (LPSTR) tmp, 1024, "D3DRefDev:ERROR: ");
  34. va_list marker;
  35. va_start(marker, pszFormat);
  36. _vsnprintf(tmp+lstrlen(tmp), 1024-lstrlen(tmp), pszFormat, marker);
  37. _snprintf( (LPSTR) tmp, 1024, "%s\n", tmp );
  38. OutputDebugString(tmp);
  39. printf(tmp);
  40. }
  41. void
  42. RDDebugPrintf( const char* pszFormat, ... )
  43. {
  44. char tmp[1024];
  45. _snprintf( (LPSTR) tmp, 1024, "D3DRefDev: ");
  46. va_list marker;
  47. va_start(marker, pszFormat);
  48. _vsnprintf(tmp+lstrlen(tmp), 1024-lstrlen(tmp), pszFormat, marker);
  49. _snprintf( (LPSTR) tmp, 1024, "%s\n", tmp );
  50. OutputDebugString(tmp);
  51. printf(tmp);
  52. }
  53. void
  54. RDDebugPrintfL( int iLevel, const char* pszFormat, ... )
  55. {
  56. if ( (iLevel <= g_iDPFLevel) )
  57. {
  58. char tmp[1024];
  59. _snprintf( (LPSTR) tmp, 1024, "D3DRefDev: ");
  60. va_list marker;
  61. va_start(marker, pszFormat);
  62. _vsnprintf(tmp+lstrlen(tmp), 1024-lstrlen(tmp), pszFormat, marker);
  63. _snprintf( (LPSTR) tmp, 1024, "%s\n", tmp );
  64. OutputDebugString(tmp);
  65. printf(tmp);
  66. }
  67. }
  68. ///////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Assert Reporting
  71. //
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // little-bit-o-state to track file and line number reporting - this is makes
  74. // this code non-reentrant and non-threadsafe... oh well...
  75. static const char* _pszLastReportFile = NULL;
  76. static int _iLastReportLine = -1;
  77. //-----------------------------------------------------------------------------
  78. void
  79. RDAssertReport( const char* pszString, const char* pszFile, int iLine )
  80. {
  81. char szTmp[1024];
  82. _snprintf( szTmp, 1024, "D3DRR ASSERT: <%d,%s> %s\n",
  83. iLine, pszFile, pszString );
  84. OutputDebugString( szTmp );
  85. #if DBG
  86. DebugBreak();
  87. #endif
  88. }
  89. //-----------------------------------------------------------------------------
  90. void
  91. RDAssertReportPrefix( const char* pszFile, int iLine )
  92. {
  93. _pszLastReportFile = pszFile;
  94. _iLastReportLine = iLine;
  95. }
  96. //-----------------------------------------------------------------------------
  97. void
  98. RDAssertReportMessage( const char* pszFormat, ... )
  99. {
  100. char szTmp[1024];
  101. va_list marker;
  102. va_start( marker, pszFormat );
  103. _vsnprintf( szTmp, 1024, pszFormat, marker );
  104. RDAssertReport( szTmp, _pszLastReportFile, _iLastReportLine );
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // //
  108. // Generic bit twiddling utilities //
  109. // //
  110. ///////////////////////////////////////////////////////////////////////////////
  111. //-----------------------------------------------------------------------------
  112. //
  113. // CountSetBits - Returns number of set bits in a multibit value (up to
  114. // 32 bits).
  115. //
  116. //-----------------------------------------------------------------------------
  117. INT32
  118. CountSetBits( UINT32 uVal, INT32 nBits )
  119. {
  120. INT32 iRet = 0;
  121. for (INT32 i=0; i<nBits; i++) {
  122. if (uVal & (0x1<<i)) { iRet++; }
  123. }
  124. return iRet;
  125. }
  126. //-----------------------------------------------------------------------------
  127. //
  128. // FindFirstSetBit - Returns index of first set bit in a multibit value
  129. // (up to 32 bits) or -1 if no bits are set.
  130. //
  131. //-----------------------------------------------------------------------------
  132. INT32
  133. FindFirstSetBit( UINT32 uVal, INT32 nBits )
  134. {
  135. for (INT32 i=0; i<nBits; i++) {
  136. if (uVal & (0x1<<i)) { return i; }
  137. }
  138. return -1;
  139. }
  140. //-----------------------------------------------------------------------------
  141. //
  142. // FindMostSignificantSetBit - Returns index of first set bit in a
  143. // multibit value (up to 32 bits) or 0 if no bits are set.
  144. //
  145. //-----------------------------------------------------------------------------
  146. INT32
  147. FindMostSignificantSetBit( UINT32 uVal, INT32 nBits )
  148. {
  149. for (INT32 i=nBits; i>=0; i--) {
  150. if (uVal & (0x1<<i)) { return i+1; }
  151. }
  152. return 0;
  153. }
  154. //-----------------------------------------------------------------------------
  155. //
  156. // FindLastSetBit - Returns index of last set bit in a multibit value
  157. // (up to 32 bits) or -1 if no bits are set.
  158. //
  159. //-----------------------------------------------------------------------------
  160. INT32
  161. FindLastSetBit( UINT32 uVal, INT32 nBits )
  162. {
  163. for (INT32 i=0; i<nBits; i++) {
  164. if (uVal & (0x1<<(nBits-i-1))) { return (nBits-i-1); }
  165. }
  166. return -1;
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////
  169. // //
  170. // Arithmetic utilities //
  171. // //
  172. ///////////////////////////////////////////////////////////////////////////////
  173. //-----------------------------------------------------------------------------
  174. //
  175. // LerpColor - Performs a linear interpolation between two RDColors
  176. //
  177. // uT is in 1.5 format (1<<5 represents a unit value)
  178. //
  179. //-----------------------------------------------------------------------------
  180. void
  181. LerpColor(
  182. RDColor& Color,
  183. const RDColor& Color0, const RDColor& Color1, UINT8 uT )
  184. {
  185. FLOAT fT = (1./(FLOAT)(1<<5))*(FLOAT)uT;
  186. Color.A = Color0.A + (Color1.A - Color0.A)*fT;
  187. Color.R = Color0.R + (Color1.R - Color0.R)*fT;
  188. Color.G = Color0.G + (Color1.G - Color0.G)*fT;
  189. Color.B = Color0.B + (Color1.B - Color0.B)*fT;
  190. }
  191. //-----------------------------------------------------------------------------
  192. //
  193. // Bilerp - Performs bilinear interpolation of 4 RDColors returning one RDColor.
  194. //
  195. //-----------------------------------------------------------------------------
  196. void
  197. BiLerpColor(
  198. RDColor& OutColor,
  199. const RDColor& Color00, const RDColor& Color01,
  200. const RDColor& Color10, const RDColor& Color11,
  201. UINT8 uA, UINT8 uB )
  202. {
  203. RDColor Color0, Color1;
  204. LerpColor( Color0, Color00, Color01, uA);
  205. LerpColor( Color1, Color10, Color11, uA);
  206. LerpColor( OutColor, Color0, Color1, uB);
  207. }
  208. void
  209. BiLerpColor3D(
  210. RDColor& OutColor,
  211. const RDColor& Color000, const RDColor& Color010,
  212. const RDColor& Color100, const RDColor& Color110,
  213. const RDColor& Color001, const RDColor& Color011,
  214. const RDColor& Color101, const RDColor& Color111,
  215. UINT8 uA, UINT8 uB, UINT8 uC)
  216. {
  217. RDColor Color0, Color1, OutColor0, OutColor1;
  218. LerpColor( Color0, Color000, Color010, uA);
  219. LerpColor( Color1, Color100, Color110, uA);
  220. LerpColor( OutColor0, Color0, Color1, uB);
  221. LerpColor( Color0, Color001, Color011, uA);
  222. LerpColor( Color1, Color101, Color111, uA);
  223. LerpColor( OutColor1, Color0, Color1, uB);
  224. LerpColor( OutColor, OutColor0, OutColor1, uC);
  225. }
  226. ///////////////////////////////////////////////////////////////////////////////
  227. //
  228. // DDGetAttachedSurfaceLcl implementation
  229. //
  230. ///////////////////////////////////////////////////////////////////////////////
  231. HRESULT
  232. DDGetAttachedSurfaceLcl(
  233. LPDDRAWI_DDRAWSURFACE_LCL this_lcl,
  234. LPDDSCAPS2 lpDDSCaps,
  235. LPDDRAWI_DDRAWSURFACE_LCL *lplpDDAttachedSurfaceLcl)
  236. {
  237. LPDDRAWI_DIRECTDRAW_GBL pdrv;
  238. LPDDRAWI_DDRAWSURFACE_GBL pgbl;
  239. LPATTACHLIST pal;
  240. DWORD caps;
  241. DWORD testcaps;
  242. DWORD ucaps;
  243. DWORD caps2;
  244. DWORD testcaps2;
  245. DWORD ucaps2;
  246. DWORD caps3;
  247. DWORD testcaps3;
  248. DWORD ucaps3;
  249. DWORD caps4;
  250. DWORD testcaps4;
  251. DWORD ucaps4;
  252. BOOL ok;
  253. pgbl = this_lcl->lpGbl;
  254. *lplpDDAttachedSurfaceLcl = NULL;
  255. pdrv = pgbl->lpDD;
  256. /*
  257. * look for the surface
  258. */
  259. pal = this_lcl->lpAttachList;
  260. testcaps = lpDDSCaps->dwCaps;
  261. testcaps2 = lpDDSCaps->dwCaps2;
  262. testcaps3 = lpDDSCaps->dwCaps3;
  263. testcaps4 = lpDDSCaps->dwCaps4;
  264. while( pal != NULL )
  265. {
  266. ok = TRUE;
  267. caps = pal->lpAttached->ddsCaps.dwCaps;
  268. caps2 = pal->lpAttached->lpSurfMore->ddsCapsEx.dwCaps2;
  269. caps3 = pal->lpAttached->lpSurfMore->ddsCapsEx.dwCaps3;
  270. caps4 = pal->lpAttached->lpSurfMore->ddsCapsEx.dwCaps4;
  271. ucaps = caps & testcaps;
  272. ucaps2 = caps2 & testcaps2;
  273. ucaps3 = caps3 & testcaps3;
  274. ucaps4 = caps4 & testcaps4;
  275. if( ucaps | ucaps2 | ucaps3 | ucaps4 )
  276. {
  277. /*
  278. * there are caps in common, make sure that the caps to test
  279. * were all there
  280. */
  281. if( (ucaps & testcaps) == testcaps &&
  282. (ucaps2 & testcaps2) == testcaps2 &&
  283. (ucaps3 & testcaps3) == testcaps3 &&
  284. (ucaps4 & testcaps4) == testcaps4 )
  285. {
  286. }
  287. else
  288. {
  289. ok = FALSE;
  290. }
  291. }
  292. else
  293. {
  294. ok = FALSE;
  295. }
  296. if( ok )
  297. {
  298. *lplpDDAttachedSurfaceLcl = pal->lpAttached;
  299. return DD_OK;
  300. }
  301. pal = pal->lpLink;
  302. }
  303. return DDERR_NOTFOUND;
  304. }
  305. //---------------------------------------------------------------------
  306. // Gets the value from DIRECT3D registry key
  307. // Returns TRUE if success
  308. // If fails value is not changed
  309. //---------------------------------------------------------------------
  310. BOOL
  311. GetD3DRegValue(DWORD type, char *valueName, LPVOID value, DWORD dwSize)
  312. {
  313. HKEY hKey = (HKEY) NULL;
  314. if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, RESPATH_D3D, &hKey))
  315. {
  316. DWORD dwType;
  317. LONG result;
  318. result = RegQueryValueEx(hKey, valueName, NULL, &dwType,
  319. (LPBYTE)value, &dwSize);
  320. RegCloseKey(hKey);
  321. return result == ERROR_SUCCESS && dwType == type;
  322. }
  323. else
  324. return FALSE;
  325. }
  326. //---------------------------------------------------------------------
  327. // Gets the value from DIRECT3D Reference device registry key
  328. // Returns TRUE if success
  329. // If fails value is not changed
  330. //---------------------------------------------------------------------
  331. BOOL
  332. GetD3DRefRegValue(DWORD type, char *valueName, LPVOID value, DWORD dwSize)
  333. {
  334. HKEY hKey = (HKEY) NULL;
  335. if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, RESPATH_D3DREF, &hKey))
  336. {
  337. DWORD dwType;
  338. LONG result;
  339. result = RegQueryValueEx(hKey, valueName, NULL, &dwType,
  340. (LPBYTE)value, &dwSize);
  341. RegCloseKey(hKey);
  342. return result == ERROR_SUCCESS && dwType == type;
  343. }
  344. else
  345. return FALSE;
  346. }
  347. ///////////////////////////////////////////////////////////////////////////////
  348. // RefAlignedBuffer32
  349. ///////////////////////////////////////////////////////////////////////////////
  350. HRESULT
  351. RefAlignedBuffer32::Grow(DWORD growSize)
  352. {
  353. if (m_allocatedBuf)
  354. free(m_allocatedBuf);
  355. m_size = growSize;
  356. if ((m_allocatedBuf = malloc(m_size + 31)) == NULL)
  357. {
  358. m_allocatedBuf = 0;
  359. m_alignedBuf = 0;
  360. m_size = 0;
  361. return DDERR_OUTOFMEMORY;
  362. }
  363. m_alignedBuf = (LPVOID)(((ULONG_PTR)m_allocatedBuf + 31 ) & ~31);
  364. return S_OK;
  365. }
  366. //////////////////////////////////////////////////////////////////////////////////
  367. // end