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.

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