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.

2807 lines
86 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // primfns.cpp
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998.
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include "pch.cpp"
  9. #pragma hdrstop
  10. //----------------------------------------------------------------------------
  11. //
  12. // Wrap functions
  13. //
  14. //----------------------------------------------------------------------------
  15. HRESULT WrapDp2SetViewport( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  16. {
  17. return pRefDev->Dp2SetViewport(pCmd);
  18. }
  19. HRESULT WrapDp2SetWRange ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  20. {
  21. return pRefDev->Dp2SetWRange(pCmd);
  22. }
  23. HRESULT WrapDp2SetZRange ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  24. {
  25. return pRefDev->Dp2SetZRange(pCmd);
  26. }
  27. HRESULT WrapDp2SetRenderStates( RefDev *pRefDev,
  28. DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd,
  29. LPDWORD lpdwRuntimeRStates )
  30. {
  31. return pRefDev->Dp2SetRenderStates(dwFvf, pCmd, lpdwRuntimeRStates);
  32. }
  33. HRESULT WrapDp2SetTextureStageState( RefDev *pRefDev, DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd )
  34. {
  35. return pRefDev->Dp2SetTextureStageState(dwFvf, pCmd);
  36. }
  37. HRESULT WrapDp2SetMaterial ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  38. {
  39. return pRefDev->Dp2SetMaterial(pCmd);
  40. }
  41. HRESULT WrapDp2SetLight( RefDev *pRefDev,
  42. LPD3DHAL_DP2COMMAND pCmd,
  43. LPDWORD pdwStride )
  44. {
  45. return pRefDev->Dp2SetLight(pCmd, pdwStride);
  46. }
  47. HRESULT WrapDp2CreateLight ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  48. {
  49. return pRefDev->Dp2CreateLight(pCmd);
  50. }
  51. HRESULT WrapDp2SetTransform( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  52. {
  53. return pRefDev->Dp2SetTransform(pCmd);
  54. }
  55. HRESULT WrapDp2MultiplyTransform( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  56. {
  57. return pRefDev->Dp2MultiplyTransform(pCmd);
  58. }
  59. HRESULT WrapDp2SetExtention( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  60. {
  61. return pRefDev->Dp2SetExtention(pCmd);
  62. }
  63. HRESULT WrapDp2SetClipPlane( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  64. {
  65. return pRefDev->Dp2SetClipPlane(pCmd);
  66. }
  67. HRESULT
  68. WrapDp2SetVertexShader( RefDev *pRefDev,
  69. LPD3DHAL_DP2COMMAND pCmd )
  70. {
  71. return pRefDev->Dp2SetVertexShader( pCmd );
  72. }
  73. HRESULT
  74. WrapDp2SetVertexShaderConsts( RefDev *pRefDev,
  75. DWORD StartReg, DWORD dwCount, LPDWORD pData )
  76. {
  77. return pRefDev->Dp2SetVertexShaderConsts( StartReg, dwCount, pData );
  78. }
  79. HRESULT
  80. WrapDp2SetPixelShader( RefDev *pRefDev,
  81. LPD3DHAL_DP2COMMAND pCmd )
  82. {
  83. return pRefDev->Dp2SetPixelShader( pCmd );
  84. }
  85. HRESULT
  86. WrapDp2SetPixelShaderConsts( RefDev *pRefDev,
  87. DWORD StartReg, DWORD dwCount, LPDWORD pData )
  88. {
  89. return pRefDev->Dp2SetPixelShaderConsts( StartReg, dwCount, pData );
  90. }
  91. HRESULT
  92. WrapDp2SetStreamSource( RefDev *pRefDev,
  93. LPD3DHAL_DP2COMMAND pCmd )
  94. {
  95. return pRefDev->Dp2SetStreamSource( pCmd );
  96. }
  97. HRESULT
  98. WrapDp2SetIndices( RefDev *pRefDev,
  99. LPD3DHAL_DP2COMMAND pCmd )
  100. {
  101. return pRefDev->Dp2SetIndices( pCmd );
  102. }
  103. HRESULT WrapDp2RecViewport( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  104. {
  105. return pRefDev->Dp2RecViewport(pCmd);
  106. }
  107. HRESULT WrapDp2RecWRange ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  108. {
  109. return pRefDev->Dp2RecWRange(pCmd);
  110. }
  111. HRESULT WrapDp2RecZRange ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  112. {
  113. return pRefDev->Dp2RecZRange(pCmd);
  114. }
  115. HRESULT WrapDp2RecRenderStates( RefDev *pRefDev,
  116. DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd,
  117. LPDWORD lpdwRuntimeRStates )
  118. {
  119. return pRefDev->Dp2RecRenderStates(dwFvf, pCmd, lpdwRuntimeRStates);
  120. }
  121. HRESULT WrapDp2RecTextureStageState( RefDev *pRefDev, DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd )
  122. {
  123. return pRefDev->Dp2RecTextureStageState(dwFvf, pCmd);
  124. }
  125. HRESULT WrapDp2RecMaterial ( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  126. {
  127. return pRefDev->Dp2RecMaterial(pCmd);
  128. }
  129. HRESULT WrapDp2RecSetLight ( RefDev *pRefDev,
  130. LPD3DHAL_DP2COMMAND pCmd,
  131. LPDWORD pdwStride)
  132. {
  133. return pRefDev->Dp2RecSetLight(pCmd, pdwStride);
  134. }
  135. HRESULT WrapDp2RecCreateLight( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  136. {
  137. return pRefDev->Dp2RecCreateLight(pCmd);
  138. }
  139. HRESULT WrapDp2RecTransform( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  140. {
  141. return pRefDev->Dp2RecTransform(pCmd);
  142. }
  143. HRESULT WrapDp2RecExtention( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  144. {
  145. return pRefDev->Dp2RecExtention(pCmd);
  146. }
  147. HRESULT WrapDp2RecClipPlane( RefDev *pRefDev, LPD3DHAL_DP2COMMAND pCmd )
  148. {
  149. return pRefDev->Dp2RecClipPlane(pCmd);
  150. }
  151. HRESULT
  152. WrapDp2RecSetVertexShader( RefDev* pRefDev,
  153. LPD3DHAL_DP2COMMAND pCmd )
  154. {
  155. return pRefDev->Dp2RecSetVertexShader( pCmd );
  156. }
  157. HRESULT
  158. WrapDp2RecSetVertexShaderConsts( RefDev* pRefDev,
  159. DWORD StartReg, DWORD dwCount, LPDWORD pData )
  160. {
  161. return pRefDev->Dp2RecSetVertexShaderConsts( StartReg, dwCount, pData );
  162. }
  163. HRESULT
  164. WrapDp2RecSetPixelShader( RefDev* pRefDev,
  165. LPD3DHAL_DP2COMMAND pCmd )
  166. {
  167. return pRefDev->Dp2RecSetPixelShader( pCmd );
  168. }
  169. HRESULT
  170. WrapDp2RecSetPixelShaderConsts( RefDev* pRefDev,
  171. DWORD StartReg, DWORD dwCount, LPDWORD pData )
  172. {
  173. return pRefDev->Dp2RecSetPixelShaderConsts( StartReg, dwCount, pData );
  174. }
  175. HRESULT
  176. WrapDp2RecSetStreamSource( RefDev* pRefDev,
  177. LPD3DHAL_DP2COMMAND pCmd )
  178. {
  179. return pRefDev->Dp2RecSetStreamSource( pCmd );
  180. }
  181. HRESULT
  182. WrapDp2RecSetIndices( RefDev* pRefDev,
  183. LPD3DHAL_DP2COMMAND pCmd )
  184. {
  185. return pRefDev->Dp2RecSetIndices( pCmd );
  186. }
  187. static RD_STATESETFUNCTIONTBL StateRecFunctions =
  188. {
  189. sizeof(RD_STATESETFUNCTIONTBL),
  190. WrapDp2RecRenderStates,
  191. WrapDp2RecTextureStageState,
  192. WrapDp2RecViewport,
  193. WrapDp2RecWRange,
  194. WrapDp2RecMaterial,
  195. WrapDp2RecZRange,
  196. WrapDp2RecSetLight,
  197. WrapDp2RecCreateLight,
  198. WrapDp2RecTransform,
  199. WrapDp2RecExtention,
  200. WrapDp2RecClipPlane,
  201. WrapDp2RecSetVertexShader,
  202. WrapDp2RecSetVertexShaderConsts,
  203. WrapDp2RecSetPixelShader,
  204. WrapDp2RecSetPixelShaderConsts,
  205. WrapDp2RecSetStreamSource,
  206. WrapDp2RecSetIndices
  207. };
  208. static RD_STATESETFUNCTIONTBL StateSetFunctions =
  209. {
  210. sizeof(RD_STATESETFUNCTIONTBL),
  211. WrapDp2SetRenderStates,
  212. WrapDp2SetTextureStageState,
  213. WrapDp2SetViewport,
  214. WrapDp2SetWRange,
  215. WrapDp2SetMaterial,
  216. WrapDp2SetZRange,
  217. WrapDp2SetLight,
  218. WrapDp2CreateLight,
  219. WrapDp2SetTransform,
  220. WrapDp2SetExtention,
  221. WrapDp2SetClipPlane,
  222. WrapDp2SetVertexShader,
  223. WrapDp2SetVertexShaderConsts,
  224. WrapDp2SetPixelShader,
  225. WrapDp2SetPixelShaderConsts,
  226. WrapDp2SetStreamSource,
  227. WrapDp2SetIndices,
  228. WrapDp2MultiplyTransform
  229. };
  230. //----------------------------------------------------------------------------
  231. //
  232. // RefDev methods
  233. //
  234. //----------------------------------------------------------------------------
  235. void
  236. RefDev::StoreLastPixelState(BOOL bStore)
  237. {
  238. if( bStore )
  239. {
  240. m_LastState = GetRS()[D3DRENDERSTATE_LASTPIXEL];
  241. SetRenderState(D3DRENDERSTATE_LASTPIXEL, 0);
  242. }
  243. else
  244. {
  245. SetRenderState(D3DRENDERSTATE_LASTPIXEL, m_LastState);
  246. }
  247. }
  248. void
  249. RefDev::SetRecStateFunctions(void)
  250. {
  251. pStateSetFuncTbl = &StateRecFunctions;
  252. }
  253. void
  254. RefDev::SetSetStateFunctions(void)
  255. {
  256. pStateSetFuncTbl = &StateSetFunctions;
  257. }
  258. HRESULT
  259. RefDev::Dp2SetRenderStates(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd,
  260. LPDWORD lpdwRuntimeRStates )
  261. {
  262. WORD wStateCount = pCmd->wStateCount;
  263. INT i;
  264. HRESULT hr = D3D_OK;
  265. D3DHAL_DP2RENDERSTATE *pRenderState =
  266. (D3DHAL_DP2RENDERSTATE *)(pCmd + 1);
  267. for (i = 0; i < (INT)wStateCount; i++, pRenderState++)
  268. {
  269. UINT32 type = (UINT32) pRenderState->RenderState;
  270. // Check for overrides
  271. if( IS_OVERRIDE(type) )
  272. {
  273. UINT32 override = GET_OVERRIDE(type);
  274. if( pRenderState->dwState )
  275. STATESET_SET(m_renderstate_override, override);
  276. else
  277. STATESET_CLEAR(m_renderstate_override, override);
  278. continue;
  279. }
  280. if( STATESET_ISSET(m_renderstate_override, type) )
  281. continue;
  282. // Set the runtime copy (if necessary)
  283. if( NULL != lpdwRuntimeRStates )
  284. {
  285. lpdwRuntimeRStates[pRenderState->RenderState] = pRenderState->dwState;
  286. }
  287. // Set the state
  288. SetRenderState(pRenderState->RenderState, pRenderState->dwState);
  289. }
  290. return hr;
  291. }
  292. HRESULT
  293. RefDev::Dp2SetTextureStageState( DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd )
  294. {
  295. WORD wStateCount = pCmd->wStateCount;
  296. INT i;
  297. HRESULT hr = D3D_OK;
  298. D3DHAL_DP2TEXTURESTAGESTATE *pTexStageState =
  299. (D3DHAL_DP2TEXTURESTAGESTATE *)(pCmd + 1);
  300. for (i = 0; i < (INT)wStateCount; i++, pTexStageState++)
  301. {
  302. SetTextureStageState( pTexStageState->wStage, pTexStageState->TSState,
  303. pTexStageState->dwValue );
  304. }
  305. return hr;
  306. }
  307. HRESULT
  308. RefDev::Dp2SetViewport(LPD3DHAL_DP2COMMAND pCmd)
  309. {
  310. LPD3DHAL_DP2VIEWPORTINFO pVpt;
  311. // Keep only the last viewport notification
  312. pVpt = (D3DHAL_DP2VIEWPORTINFO *)(pCmd + 1) + (pCmd->wStateCount - 1);
  313. // Update T&L viewport state
  314. D3DVIEWPORT7& vp = m_Clipper.m_Viewport;
  315. vp.dwX = pVpt->dwX;
  316. vp.dwY = pVpt->dwY;
  317. vp.dwWidth = pVpt->dwWidth;
  318. vp.dwHeight = pVpt->dwHeight;
  319. m_Clipper.m_dwFlags |= RefClipper::RCLIP_DIRTY_VIEWRECT;
  320. // get render target; update it; put it back
  321. RDRenderTarget *pRendTgt = this->GetRenderTarget();
  322. pRendTgt->m_Clip.left = pVpt->dwX;
  323. pRendTgt->m_Clip.top = pVpt->dwY;
  324. pRendTgt->m_Clip.right = pVpt->dwX + pVpt->dwWidth - 1;
  325. pRendTgt->m_Clip.bottom = pVpt->dwY + pVpt->dwHeight - 1;
  326. SetRenderTarget( pRendTgt );
  327. return D3D_OK;
  328. }
  329. HRESULT
  330. RefDev::Dp2SetWRange(LPD3DHAL_DP2COMMAND pCmd)
  331. {
  332. LPD3DHAL_DP2WINFO pWInfo;
  333. // Keep only the last viewport notification
  334. pWInfo = (D3DHAL_DP2WINFO *)(pCmd + 1) + (pCmd->wStateCount - 1);
  335. // get render target; update it; put it back
  336. RDRenderTarget *pRendTgt = this->GetRenderTarget();
  337. pRendTgt->m_fWRange[0] = pWInfo->dvWNear;
  338. pRendTgt->m_fWRange[1] = pWInfo->dvWFar;
  339. this->SetRenderTarget( pRendTgt );
  340. return D3D_OK;
  341. }
  342. HRESULT
  343. RefDev::Dp2SetZRange(LPD3DHAL_DP2COMMAND pCmd)
  344. {
  345. LPD3DHAL_DP2ZRANGE pZRange;
  346. // Keep only the last viewport notification
  347. pZRange = (D3DHAL_DP2ZRANGE *)(pCmd + 1) + (pCmd->wStateCount - 1);
  348. // Update T&L viewport state
  349. D3DVIEWPORT7& vp = m_Clipper.m_Viewport;
  350. vp.dvMinZ = pZRange->dvMinZ;
  351. vp.dvMaxZ = pZRange->dvMaxZ;
  352. m_Clipper.m_dwFlags |= RefClipper::RCLIP_DIRTY_ZRANGE;
  353. return D3D_OK;
  354. }
  355. HRESULT
  356. RefDev::Dp2SetMaterial(LPD3DHAL_DP2COMMAND pCmd)
  357. {
  358. LPD3DHAL_DP2SETMATERIAL pSetMat;
  359. // Keep only the last material notification
  360. pSetMat = (D3DHAL_DP2SETMATERIAL *)(pCmd + 1) + (pCmd->wStateCount - 1);
  361. m_RefVP.m_Material = *(D3DMATERIAL7 *)pSetMat;
  362. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_MATERIAL;
  363. return D3D_OK;
  364. }
  365. HRESULT
  366. RefDev::Dp2CreateLight(LPD3DHAL_DP2COMMAND pCmd)
  367. {
  368. WORD wNumCreateLight = pCmd->wStateCount;
  369. LPD3DHAL_DP2CREATELIGHT pCreateLight = (LPD3DHAL_DP2CREATELIGHT)(pCmd + 1);
  370. HRESULT hr = D3D_OK;
  371. for (int i = 0; i < wNumCreateLight; i++, pCreateLight++)
  372. {
  373. HR_RET(m_RefVP.GrowLightArray( pCreateLight->dwIndex ) );
  374. }
  375. return hr;
  376. }
  377. HRESULT
  378. RefDev::Dp2SetLight(LPD3DHAL_DP2COMMAND pCmd,
  379. LPDWORD pdwStride)
  380. {
  381. HRESULT hr = D3D_OK;
  382. WORD wNumSetLight = pCmd->wStateCount;
  383. _ASSERT( pdwStride != NULL, "pdwStride is Null" );
  384. *pdwStride = sizeof(D3DHAL_DP2COMMAND);
  385. LPD3DHAL_DP2SETLIGHT pSetLight = (LPD3DHAL_DP2SETLIGHT)(pCmd + 1);
  386. D3DLIGHT7 *pLightData = NULL;
  387. for (int i = 0; i < wNumSetLight; i++)
  388. {
  389. DWORD dwStride = sizeof(D3DHAL_DP2SETLIGHT);
  390. DWORD dwIndex = pSetLight->dwIndex;
  391. // Assert that create was not called here
  392. _ASSERTf( m_RefVP.m_LightArray.IsValidIndex( dwIndex ),
  393. ( "Create was not called prior to the SetLight for light %d",
  394. dwIndex ));
  395. switch (pSetLight->dwDataType)
  396. {
  397. case D3DHAL_SETLIGHT_ENABLE:
  398. m_RefVP.LightEnable( dwIndex, TRUE );
  399. break;
  400. case D3DHAL_SETLIGHT_DISABLE:
  401. m_RefVP.LightEnable( dwIndex, FALSE );
  402. break;
  403. case D3DHAL_SETLIGHT_DATA:
  404. pLightData = (D3DLIGHT7 *)((LPBYTE)pSetLight + dwStride);
  405. dwStride += sizeof(D3DLIGHT7);
  406. HR_RET(m_RefVP.SetLightData( pSetLight->dwIndex, pLightData));
  407. break;
  408. default:
  409. DPFERR( "Unknown SetLight command" );
  410. hr = DDERR_INVALIDPARAMS;
  411. }
  412. *pdwStride += dwStride;
  413. // Update the command buffer pointer
  414. pSetLight = (D3DHAL_DP2SETLIGHT *)((LPBYTE)pSetLight +
  415. dwStride);
  416. }
  417. return hr;
  418. }
  419. static D3DMATRIX matIdent =
  420. {
  421. 1.0f, 0.0f, 0.0f, 0.0f,
  422. 0.0f, 1.0f, 0.0f, 0.0f,
  423. 0.0f, 0.0f, 1.0f, 0.0f,
  424. 0.0f, 0.0f, 0.0f, 1.0f
  425. };
  426. HRESULT
  427. RefDev::Dp2SetTransform(LPD3DHAL_DP2COMMAND pCmd)
  428. {
  429. WORD wNumXfrms = pCmd->wStateCount;
  430. D3DHAL_DP2SETTRANSFORM *pSetXfrm = (D3DHAL_DP2SETTRANSFORM*)(pCmd + 1);
  431. for (int i = 0; i < (int) wNumXfrms; i++, pSetXfrm++)
  432. {
  433. D3DMATRIX* pMat = &pSetXfrm->matrix;
  434. DWORD xfrmType = (DWORD)pSetXfrm->xfrmType;
  435. if ((DWORD)xfrmType >= RD_WORLDMATRIXBASE &&
  436. (DWORD)xfrmType < (RD_WORLDMATRIXBASE + RD_MAX_WORLD_MATRICES))
  437. {
  438. // World matrix is set
  439. UINT index = (DWORD)xfrmType - RD_WORLDMATRIXBASE;
  440. memcpy(&(m_RefVP.m_xfmWorld[index]), pMat, sizeof(D3DMATRIX));
  441. switch (index)
  442. {
  443. case 0:
  444. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDXFM;
  445. break;
  446. case 1:
  447. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD1XFM;
  448. break;
  449. case 2:
  450. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD2XFM;
  451. break;
  452. case 3:
  453. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD3XFM;
  454. break;
  455. default:
  456. // m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDNXFM;
  457. break;
  458. }
  459. }
  460. else
  461. {
  462. switch( xfrmType )
  463. {
  464. case D3DTRANSFORMSTATE_WORLD_DX7:
  465. memcpy(&(m_RefVP.m_xfmWorld[0]), pMat, sizeof(D3DMATRIX));
  466. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDXFM;
  467. break;
  468. case D3DTRANSFORMSTATE_VIEW:
  469. memcpy(&m_RefVP.m_xfmView, pMat, sizeof(D3DMATRIX));
  470. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_VIEWXFM;
  471. break;
  472. case D3DTRANSFORMSTATE_PROJECTION:
  473. memcpy(&m_RefVP.m_xfmProj, pMat, sizeof(D3DMATRIX));
  474. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_PROJXFM;
  475. break;
  476. case D3DTRANSFORMSTATE_WORLD1_DX7:
  477. memcpy(&(m_RefVP.m_xfmWorld[1]), pMat, sizeof(D3DMATRIX));
  478. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD1XFM;
  479. break;
  480. case D3DTRANSFORMSTATE_WORLD2_DX7:
  481. memcpy(&(m_RefVP.m_xfmWorld[2]), pMat, sizeof(D3DMATRIX));
  482. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD2XFM;
  483. break;
  484. case D3DTRANSFORMSTATE_WORLD3_DX7:
  485. memcpy(&(m_RefVP.m_xfmWorld[3]), pMat, sizeof(D3DMATRIX));
  486. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD3XFM;
  487. break;
  488. case D3DTRANSFORMSTATE_TEXTURE0:
  489. case D3DTRANSFORMSTATE_TEXTURE1:
  490. case D3DTRANSFORMSTATE_TEXTURE2:
  491. case D3DTRANSFORMSTATE_TEXTURE3:
  492. case D3DTRANSFORMSTATE_TEXTURE4:
  493. case D3DTRANSFORMSTATE_TEXTURE5:
  494. case D3DTRANSFORMSTATE_TEXTURE6:
  495. case D3DTRANSFORMSTATE_TEXTURE7:
  496. memcpy(
  497. &(m_RefVP.m_xfmTex[xfrmType - D3DTRANSFORMSTATE_TEXTURE0]),
  498. pMat, sizeof(D3DMATRIX)
  499. );
  500. break;
  501. default:
  502. DPFERR( "Ignoring unknown transform type" );
  503. }
  504. }
  505. }
  506. return D3D_OK;
  507. }
  508. extern void MatrixProduct(D3DMATRIX *result, D3DMATRIX *a, D3DMATRIX *b);
  509. HRESULT
  510. RefDev::Dp2MultiplyTransform(LPD3DHAL_DP2COMMAND pCmd)
  511. {
  512. WORD wNumXfrms = pCmd->wStateCount;
  513. D3DHAL_DP2MULTIPLYTRANSFORM *pSetXfrm = (D3DHAL_DP2MULTIPLYTRANSFORM*)(pCmd + 1);
  514. for (int i = 0; i < (int) wNumXfrms; i++, pSetXfrm++)
  515. {
  516. D3DMATRIX* pMat = &pSetXfrm->matrix;
  517. DWORD xfrmType = (DWORD)pSetXfrm->xfrmType;
  518. if ((DWORD)xfrmType >= RD_WORLDMATRIXBASE &&
  519. (DWORD)xfrmType < (RD_WORLDMATRIXBASE + RD_MAX_WORLD_MATRICES))
  520. {
  521. // World matrix is set
  522. UINT index = (DWORD)xfrmType - RD_WORLDMATRIXBASE;
  523. MatrixProduct(&(m_RefVP.m_xfmWorld[index]), pMat,
  524. &(m_RefVP.m_xfmWorld[index]));
  525. switch (index)
  526. {
  527. case 0:
  528. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDXFM;
  529. break;
  530. case 1:
  531. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD1XFM;
  532. break;
  533. case 2:
  534. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD2XFM;
  535. break;
  536. case 3:
  537. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD3XFM;
  538. break;
  539. default:
  540. // m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDNXFM;
  541. break;
  542. }
  543. }
  544. else
  545. {
  546. switch( xfrmType )
  547. {
  548. case D3DTRANSFORMSTATE_WORLD_DX7:
  549. MatrixProduct(&(m_RefVP.m_xfmWorld[0]), pMat,
  550. &(m_RefVP.m_xfmWorld[0]));
  551. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLDXFM;
  552. break;
  553. case D3DTRANSFORMSTATE_VIEW:
  554. MatrixProduct(&m_RefVP.m_xfmView, pMat, &m_RefVP.m_xfmView);
  555. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_VIEWXFM;
  556. break;
  557. case D3DTRANSFORMSTATE_PROJECTION:
  558. MatrixProduct(&m_RefVP.m_xfmProj, pMat, &m_RefVP.m_xfmProj);
  559. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_PROJXFM;
  560. break;
  561. case D3DTRANSFORMSTATE_WORLD1_DX7:
  562. MatrixProduct(&(m_RefVP.m_xfmWorld[1]), pMat,
  563. &(m_RefVP.m_xfmWorld[1]));
  564. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD1XFM;
  565. break;
  566. case D3DTRANSFORMSTATE_WORLD2_DX7:
  567. MatrixProduct(&(m_RefVP.m_xfmWorld[2]), pMat,
  568. &(m_RefVP.m_xfmWorld[2]));
  569. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD2XFM;
  570. break;
  571. case D3DTRANSFORMSTATE_WORLD3_DX7:
  572. MatrixProduct(&(m_RefVP.m_xfmWorld[3]), pMat,
  573. &(m_RefVP.m_xfmWorld[3]));
  574. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_WORLD3XFM;
  575. break;
  576. case D3DTRANSFORMSTATE_TEXTURE0:
  577. case D3DTRANSFORMSTATE_TEXTURE1:
  578. case D3DTRANSFORMSTATE_TEXTURE2:
  579. case D3DTRANSFORMSTATE_TEXTURE3:
  580. case D3DTRANSFORMSTATE_TEXTURE4:
  581. case D3DTRANSFORMSTATE_TEXTURE5:
  582. case D3DTRANSFORMSTATE_TEXTURE6:
  583. case D3DTRANSFORMSTATE_TEXTURE7:
  584. MatrixProduct(
  585. &(m_RefVP.m_xfmTex[xfrmType - D3DTRANSFORMSTATE_TEXTURE0]),
  586. pMat,
  587. &(m_RefVP.m_xfmTex[xfrmType - D3DTRANSFORMSTATE_TEXTURE0])
  588. );
  589. break;
  590. default:
  591. DPFERR( "Ignoring unknown transform type" );
  592. }
  593. }
  594. }
  595. return D3D_OK;
  596. }
  597. HRESULT
  598. RefDev::Dp2SetClipPlane(LPD3DHAL_DP2COMMAND pCmd)
  599. {
  600. WORD wNumClipPlanes = pCmd->wStateCount;
  601. LPD3DHAL_DP2SETCLIPPLANE pSetClipPlane =
  602. (LPD3DHAL_DP2SETCLIPPLANE)(pCmd + 1);
  603. for (int i = 0; i < (int) wNumClipPlanes; i++, pSetClipPlane++)
  604. {
  605. _ASSERTf( pSetClipPlane->dwIndex < RD_MAX_USER_CLIPPLANES,
  606. ("Refrast does not support %d clip planes",
  607. pSetClipPlane->dwIndex ) );
  608. memcpy( &(m_Clipper.m_userClipPlanes[pSetClipPlane->dwIndex]),
  609. pSetClipPlane->plane, sizeof(RDVECTOR4) );
  610. }
  611. return D3D_OK;
  612. }
  613. HRESULT
  614. RefDev::Dp2SetExtention(LPD3DHAL_DP2COMMAND pCmd)
  615. {
  616. return D3D_OK;
  617. }
  618. HRESULT
  619. RefDev::Dp2RecRenderStates(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd,
  620. LPDWORD lpdwRuntimeRStates )
  621. {
  622. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  623. pCmd->wStateCount * sizeof(D3DHAL_DP2RENDERSTATE);
  624. return RecordStates((PUINT8)pCmd, dwSize);
  625. }
  626. HRESULT
  627. RefDev::Dp2RecTextureStageState(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd )
  628. {
  629. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  630. pCmd->wStateCount * sizeof(D3DHAL_DP2TEXTURESTAGESTATE);
  631. return RecordStates((PUINT8)pCmd, dwSize);
  632. }
  633. HRESULT
  634. RefDev::Dp2RecViewport(LPD3DHAL_DP2COMMAND pCmd)
  635. {
  636. return RecordLastState(pCmd, sizeof(D3DHAL_DP2VIEWPORTINFO));
  637. }
  638. HRESULT
  639. RefDev::Dp2RecWRange(LPD3DHAL_DP2COMMAND pCmd)
  640. {
  641. return RecordLastState(pCmd, sizeof(D3DHAL_DP2WINFO));
  642. }
  643. HRESULT
  644. RefDev::Dp2RecZRange(LPD3DHAL_DP2COMMAND pCmd)
  645. {
  646. return RecordLastState(pCmd, sizeof(D3DHAL_DP2ZRANGE));
  647. }
  648. HRESULT
  649. RefDev::Dp2RecMaterial(LPD3DHAL_DP2COMMAND pCmd)
  650. {
  651. return RecordLastState(pCmd, sizeof(D3DHAL_DP2SETMATERIAL));
  652. }
  653. HRESULT
  654. RefDev::Dp2RecCreateLight(LPD3DHAL_DP2COMMAND pCmd)
  655. {
  656. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  657. pCmd->wStateCount * sizeof(D3DHAL_DP2CREATELIGHT);
  658. return RecordStates((PUINT8)pCmd, dwSize);
  659. }
  660. HRESULT
  661. RefDev::Dp2RecSetLight(LPD3DHAL_DP2COMMAND pCmd,
  662. LPDWORD pdwStride)
  663. {
  664. WORD wNumSetLight = pCmd->wStateCount;
  665. _ASSERT(pdwStride != NULL, "pdwStride is NULL" );
  666. *pdwStride = sizeof(D3DHAL_DP2COMMAND);
  667. LPD3DHAL_DP2SETLIGHT pSetLight = (LPD3DHAL_DP2SETLIGHT)(pCmd + 1);
  668. for (int i = 0; i < wNumSetLight; i++)
  669. {
  670. DWORD dwStride = sizeof(D3DHAL_DP2SETLIGHT);
  671. switch (pSetLight->dwDataType)
  672. {
  673. case D3DHAL_SETLIGHT_ENABLE:
  674. break;
  675. case D3DHAL_SETLIGHT_DISABLE:
  676. break;
  677. case D3DHAL_SETLIGHT_DATA:
  678. dwStride += sizeof(D3DLIGHT7);
  679. break;
  680. }
  681. *pdwStride += dwStride;
  682. // Update the command buffer pointer
  683. pSetLight = (D3DHAL_DP2SETLIGHT *)((LPBYTE)pSetLight +
  684. dwStride);
  685. }
  686. return RecordStates((PUINT8)pCmd, *pdwStride);
  687. }
  688. HRESULT
  689. RefDev::Dp2RecTransform(LPD3DHAL_DP2COMMAND pCmd)
  690. {
  691. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  692. pCmd->wStateCount * sizeof(D3DHAL_DP2SETTRANSFORM);
  693. return RecordStates((PUINT8)pCmd, dwSize);
  694. }
  695. HRESULT
  696. RefDev::Dp2RecExtention(LPD3DHAL_DP2COMMAND pCmd)
  697. {
  698. return D3D_OK;
  699. }
  700. HRESULT
  701. RefDev::Dp2RecClipPlane(LPD3DHAL_DP2COMMAND pCmd)
  702. {
  703. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  704. pCmd->wStateCount * sizeof(D3DHAL_DP2SETCLIPPLANE);
  705. return RecordStates((PUINT8)pCmd, dwSize);
  706. }
  707. HRESULT
  708. RefDev::Dp2RecSetVertexShader(LPD3DHAL_DP2COMMAND pCmd)
  709. {
  710. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  711. pCmd->wStateCount * sizeof(D3DHAL_DP2VERTEXSHADER);
  712. return RecordStates((PUINT8)pCmd, dwSize);
  713. }
  714. HRESULT
  715. RefDev::Dp2RecSetVertexShaderConsts( DWORD StartReg,
  716. DWORD dwCount,
  717. LPDWORD pData )
  718. {
  719. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  720. sizeof(D3DHAL_DP2SETVERTEXSHADERCONST) +
  721. dwCount*4*sizeof(float);
  722. LPBYTE pBytes = new BYTE[dwSize];
  723. if( pBytes == NULL ) return DDERR_OUTOFMEMORY;
  724. LPD3DHAL_DP2COMMAND pCmd = (LPD3DHAL_DP2COMMAND)pBytes;
  725. LPD3DHAL_DP2SETVERTEXSHADERCONST pSVC =
  726. (LPD3DHAL_DP2SETVERTEXSHADERCONST)(pCmd + 1);
  727. LPDWORD pStuff = (LPDWORD)(pSVC + 1);
  728. // Set up pCmd
  729. pCmd->bCommand = D3DDP2OP_SETVERTEXSHADERCONST;
  730. pCmd->wStateCount = 1;
  731. // Set up pSVC
  732. pSVC->dwRegister = StartReg;
  733. pSVC->dwCount = dwCount;
  734. // Copy the data
  735. memcpy( pStuff, pData, dwCount*4*sizeof(float));
  736. HRESULT hr = RecordStates(pBytes, dwSize);
  737. delete [] pBytes;
  738. return hr;
  739. }
  740. HRESULT
  741. RefDev::Dp2RecSetPixelShader(LPD3DHAL_DP2COMMAND pCmd)
  742. {
  743. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  744. pCmd->wStateCount * sizeof(D3DHAL_DP2PIXELSHADER);
  745. return RecordStates((PUINT8)pCmd, dwSize);
  746. }
  747. HRESULT
  748. RefDev::Dp2RecSetPixelShaderConsts( DWORD StartReg,
  749. DWORD dwCount,
  750. LPDWORD pData )
  751. {
  752. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  753. sizeof(D3DHAL_DP2SETPIXELSHADERCONST) +
  754. dwCount*4*sizeof(float);
  755. LPBYTE pBytes = new BYTE[dwSize];
  756. if( pBytes == NULL ) return DDERR_OUTOFMEMORY;
  757. LPD3DHAL_DP2COMMAND pCmd = (LPD3DHAL_DP2COMMAND)pBytes;
  758. LPD3DHAL_DP2SETPIXELSHADERCONST pSVC =
  759. (LPD3DHAL_DP2SETPIXELSHADERCONST)(pCmd + 1);
  760. LPDWORD pStuff = (LPDWORD)(pSVC + 1);
  761. // Set up pCmd
  762. pCmd->bCommand = D3DDP2OP_SETPIXELSHADERCONST;
  763. pCmd->wStateCount = 1;
  764. // Set up pSVC
  765. pSVC->dwRegister = StartReg;
  766. pSVC->dwCount = dwCount;
  767. // Copy the data
  768. memcpy( pStuff, pData, dwCount*4*sizeof(float));
  769. HRESULT hr = RecordStates(pBytes, dwSize);
  770. delete [] pBytes;
  771. return hr;
  772. }
  773. HRESULT
  774. RefDev::Dp2RecSetStreamSource(LPD3DHAL_DP2COMMAND pCmd)
  775. {
  776. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  777. pCmd->wStateCount * sizeof(D3DHAL_DP2SETSTREAMSOURCE);
  778. return RecordStates((PUINT8)pCmd, dwSize);
  779. }
  780. HRESULT
  781. RefDev::Dp2RecSetIndices(LPD3DHAL_DP2COMMAND pCmd)
  782. {
  783. DWORD dwSize = sizeof(D3DHAL_DP2COMMAND) +
  784. pCmd->wStateCount * sizeof(D3DHAL_DP2SETINDICES);
  785. return RecordStates((PUINT8)pCmd, dwSize);
  786. }
  787. //-----------------------------------------------------------------------------
  788. //
  789. // RecordStates - This function copies the state data into the internal stateset
  790. // buffer. It assumes that the current state set has already been properly set
  791. // up in BeginStateSet().
  792. //
  793. //-----------------------------------------------------------------------------
  794. HRESULT
  795. RefDev::RecordStates(PUINT8 pData, DWORD dwSize)
  796. {
  797. HRESULT ret;
  798. LPStateSetData pCurStateSets = m_pStateSets.CurrentItem();
  799. DWORD dwCurIdx = pCurStateSets->CurrentIndex();
  800. // Check if the buffer has enough space
  801. if( (ret = pCurStateSets->CheckAndGrow(dwCurIdx + dwSize,
  802. RD_STATESET_GROWDELTA)) != D3D_OK )
  803. {
  804. return ret;
  805. }
  806. // Copy the data and update the ptr.
  807. PUINT8 pDest = (PUINT8)&((*pCurStateSets)[dwCurIdx]);
  808. memcpy(pDest, pData, dwSize);
  809. pCurStateSets->SetCurrentIndex(dwCurIdx + dwSize);
  810. return D3D_OK;
  811. }
  812. HRESULT RefDev::RecordLastState(LPD3DHAL_DP2COMMAND pCmd,
  813. DWORD dwUnitSize)
  814. {
  815. _ASSERT(pCmd->wStateCount != 0, "Number of states to record is zero" );
  816. if( pCmd->wStateCount == 1 )
  817. {
  818. return RecordStates((PUINT8)pCmd, sizeof(D3DHAL_DP2COMMAND) + dwUnitSize);
  819. }
  820. else
  821. {
  822. HRESULT ret;
  823. WORD wCount = pCmd->wStateCount;
  824. pCmd->wStateCount = 1;
  825. ret = RecordStates((PUINT8)pCmd, sizeof(D3DHAL_DP2COMMAND));
  826. if( ret != D3D_OK )
  827. {
  828. return ret;
  829. }
  830. ret = RecordStates((PUINT8)(pCmd + 1) + dwUnitSize * (wCount - 1),
  831. dwUnitSize);
  832. if( ret != D3D_OK )
  833. {
  834. return ret;
  835. }
  836. pCmd->wStateCount = wCount;
  837. return D3D_OK;
  838. }
  839. }
  840. HRESULT
  841. RefDev::BeginStateSet(DWORD dwHandle)
  842. {
  843. HRESULT ret;
  844. // Grow the array if no more space left
  845. if( (ret = m_pStateSets.CheckAndGrow(dwHandle)) != D3D_OK )
  846. {
  847. return ret;
  848. }
  849. _ASSERT(m_pStateSets[dwHandle] == NULL, "pStateSets array is NULL" );
  850. // Create the new StateSet
  851. LPStateSetData pNewStateSet = new StateSetData;
  852. if( pNewStateSet == NULL )
  853. {
  854. return DDERR_OUTOFMEMORY;
  855. }
  856. m_pStateSets.SetCurrentIndex(dwHandle);
  857. m_pStateSets.SetCurrentItem(pNewStateSet);
  858. // Switch to record mode
  859. SetRecStateFunctions();
  860. return D3D_OK;
  861. }
  862. HRESULT
  863. RefDev::EndStateSet(void)
  864. {
  865. // Switch to execute mode
  866. SetSetStateFunctions();
  867. return D3D_OK;
  868. }
  869. HRESULT
  870. RefDev::ExecuteStateSet(DWORD dwHandle)
  871. {
  872. HRESULT ret;
  873. if( (ret = m_pStateSets.CheckRange(dwHandle)) != D3D_OK )
  874. {
  875. return ret;
  876. }
  877. LPStateSetData pStateSet = m_pStateSets[dwHandle];
  878. if( pStateSet == NULL )
  879. {
  880. return DDERR_INVALIDPARAMS;
  881. }
  882. LPD3DHAL_DP2COMMAND pCmd = (LPD3DHAL_DP2COMMAND)&((*pStateSet)[0]);
  883. UINT_PTR CmdBoundary = (UINT_PTR)pCmd + pStateSet->CurrentIndex();
  884. // Loop through the data, update render states
  885. for (;;)
  886. {
  887. ret = DrawPrimitives2( NULL,
  888. (UINT16)0,
  889. (DWORD)0,
  890. 0,
  891. &pCmd,
  892. NULL );
  893. if( ret != D3D_OK )
  894. {
  895. return ret;
  896. }
  897. if( (UINT_PTR)pCmd >= CmdBoundary )
  898. break;
  899. }
  900. return D3D_OK;
  901. }
  902. HRESULT
  903. RefDev::DeleteStateSet(DWORD dwHandle)
  904. {
  905. HRESULT ret;
  906. if( (ret = m_pStateSets.CheckRange(dwHandle)) != D3D_OK )
  907. {
  908. return ret;
  909. }
  910. if( m_pStateSets[dwHandle] != NULL )
  911. {
  912. delete m_pStateSets[dwHandle];
  913. m_pStateSets[dwHandle] = NULL;
  914. }
  915. return D3D_OK;
  916. }
  917. HRESULT
  918. RefDev::CaptureStateSet(DWORD dwHandle)
  919. {
  920. HRESULT ret;
  921. if( (ret = m_pStateSets.CheckRange(dwHandle)) != D3D_OK )
  922. {
  923. return ret;
  924. }
  925. LPStateSetData pStateSet = m_pStateSets[dwHandle];
  926. if( pStateSet == NULL )
  927. {
  928. return DDERR_INVALIDPARAMS;
  929. }
  930. BYTE *p = &((*pStateSet)[0]);
  931. UINT_PTR pEnd = (UINT_PTR)(p + pStateSet->CurrentIndex());
  932. while((UINT_PTR)p < pEnd)
  933. {
  934. LPD3DHAL_DP2COMMAND pCmd = (LPD3DHAL_DP2COMMAND)p;
  935. p += sizeof(D3DHAL_DP2COMMAND);
  936. switch(pCmd->bCommand)
  937. {
  938. case D3DDP2OP_RENDERSTATE:
  939. {
  940. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  941. {
  942. LPD3DHAL_DP2RENDERSTATE pData = (LPD3DHAL_DP2RENDERSTATE)p;
  943. pData->dwState = GetRS()[pData->RenderState];
  944. p += sizeof(D3DHAL_DP2RENDERSTATE);
  945. }
  946. break;
  947. }
  948. case D3DDP2OP_SETLIGHT:
  949. {
  950. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  951. {
  952. LPD3DHAL_DP2SETLIGHT pData = (LPD3DHAL_DP2SETLIGHT)p;
  953. p += sizeof(D3DHAL_DP2SETLIGHT);
  954. if( !m_RefVP.m_LightArray.IsValidIndex( pData->dwIndex ) )
  955. {
  956. DPFERR( "The light index in capture is invalid\n" );
  957. return D3DERR_INVALIDCALL;
  958. }
  959. switch (pData->dwDataType)
  960. {
  961. case D3DHAL_SETLIGHT_ENABLE:
  962. if(!m_RefVP.m_LightArray[pData->dwIndex].IsEnabled())
  963. pData->dwDataType = D3DHAL_SETLIGHT_DISABLE;
  964. break;
  965. case D3DHAL_SETLIGHT_DISABLE:
  966. if(m_RefVP.m_LightArray[pData->dwIndex].IsEnabled())
  967. pData->dwDataType = D3DHAL_SETLIGHT_ENABLE;
  968. break;
  969. case D3DHAL_SETLIGHT_DATA:
  970. m_RefVP.m_LightArray[pData->dwIndex].GetLight((LPD3DLIGHT7)p);
  971. p += sizeof(D3DLIGHT7);
  972. break;
  973. }
  974. }
  975. break;
  976. }
  977. case D3DDP2OP_SETMATERIAL:
  978. {
  979. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  980. {
  981. LPD3DHAL_DP2SETMATERIAL pData = (LPD3DHAL_DP2SETMATERIAL)p;
  982. *pData = m_RefVP.m_Material;
  983. p += sizeof(D3DHAL_DP2SETMATERIAL);
  984. }
  985. break;
  986. }
  987. case D3DDP2OP_SETTRANSFORM:
  988. {
  989. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  990. {
  991. LPD3DHAL_DP2SETTRANSFORM pData = (LPD3DHAL_DP2SETTRANSFORM)p;
  992. switch(pData->xfrmType)
  993. {
  994. case D3DTRANSFORMSTATE_WORLD:
  995. pData->matrix = m_RefVP.m_xfmWorld[0];
  996. break;
  997. case D3DTRANSFORMSTATE_WORLD1:
  998. pData->matrix = m_RefVP.m_xfmWorld[1];
  999. break;
  1000. case D3DTRANSFORMSTATE_WORLD2:
  1001. pData->matrix = m_RefVP.m_xfmWorld[2];
  1002. break;
  1003. case D3DTRANSFORMSTATE_WORLD3:
  1004. pData->matrix = m_RefVP.m_xfmWorld[3];
  1005. break;
  1006. case D3DTRANSFORMSTATE_VIEW:
  1007. pData->matrix = m_RefVP.m_xfmView;
  1008. break;
  1009. case D3DTRANSFORMSTATE_PROJECTION:
  1010. pData->matrix = m_RefVP.m_xfmProj;
  1011. break;
  1012. case D3DTRANSFORMSTATE_TEXTURE0:
  1013. case D3DTRANSFORMSTATE_TEXTURE1:
  1014. case D3DTRANSFORMSTATE_TEXTURE2:
  1015. case D3DTRANSFORMSTATE_TEXTURE3:
  1016. case D3DTRANSFORMSTATE_TEXTURE4:
  1017. case D3DTRANSFORMSTATE_TEXTURE5:
  1018. case D3DTRANSFORMSTATE_TEXTURE6:
  1019. case D3DTRANSFORMSTATE_TEXTURE7:
  1020. pData->matrix = m_RefVP.m_xfmTex[pData->xfrmType - D3DTRANSFORMSTATE_TEXTURE0];
  1021. break;
  1022. default:
  1023. if( ((DWORD)pData->xfrmType >= RD_WORLDMATRIXBASE) &&
  1024. ((DWORD)pData->xfrmType < (RD_WORLDMATRIXBASE +
  1025. RD_MAX_WORLD_MATRICES)) )
  1026. {
  1027. pData->matrix = m_RefVP.m_xfmWorld[
  1028. (DWORD)pData->xfrmType - RD_WORLDMATRIXBASE];
  1029. }
  1030. else
  1031. {
  1032. DPFERR( "Ignoring unknown transform type" );
  1033. return D3DERR_INVALIDCALL;
  1034. }
  1035. break;
  1036. }
  1037. p += sizeof(D3DHAL_DP2SETTRANSFORM);
  1038. }
  1039. break;
  1040. }
  1041. case D3DDP2OP_TEXTURESTAGESTATE:
  1042. {
  1043. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1044. {
  1045. LPD3DHAL_DP2TEXTURESTAGESTATE pData = (LPD3DHAL_DP2TEXTURESTAGESTATE)p;
  1046. pData->dwValue = m_TextureStageState[pData->wStage].m_dwVal[pData->TSState];
  1047. p += sizeof(D3DHAL_DP2TEXTURESTAGESTATE);
  1048. }
  1049. break;
  1050. }
  1051. case D3DDP2OP_VIEWPORTINFO:
  1052. {
  1053. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1054. {
  1055. D3DVIEWPORT7 viewport;
  1056. LPD3DHAL_DP2VIEWPORTINFO lpVwpData = (LPD3DHAL_DP2VIEWPORTINFO)p;
  1057. D3DVIEWPORT7& vp = m_Clipper.m_Viewport;
  1058. lpVwpData->dwX = vp.dwX;
  1059. lpVwpData->dwY = vp.dwY;
  1060. lpVwpData->dwWidth = vp.dwWidth;
  1061. lpVwpData->dwHeight = vp.dwHeight;
  1062. p += sizeof(D3DHAL_DP2VIEWPORTINFO);
  1063. }
  1064. break;
  1065. }
  1066. case D3DDP2OP_ZRANGE:
  1067. {
  1068. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1069. {
  1070. LPD3DHAL_DP2ZRANGE pData = (LPD3DHAL_DP2ZRANGE)p;
  1071. D3DVIEWPORT7& vp = m_Clipper.m_Viewport;
  1072. pData->dvMinZ = vp.dvMinZ;
  1073. pData->dvMaxZ = vp.dvMaxZ;
  1074. p += sizeof(D3DHAL_DP2ZRANGE);
  1075. }
  1076. break;
  1077. }
  1078. case D3DDP2OP_SETCLIPPLANE:
  1079. {
  1080. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1081. {
  1082. LPD3DHAL_DP2SETCLIPPLANE pData = (LPD3DHAL_DP2SETCLIPPLANE)p;
  1083. *((RDVECTOR4 *)pData->plane) =
  1084. m_Clipper.m_userClipPlanes[pData->dwIndex];
  1085. p += sizeof(D3DHAL_DP2SETCLIPPLANE);
  1086. }
  1087. break;
  1088. }
  1089. case D3DDP2OP_SETVERTEXSHADER:
  1090. {
  1091. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1092. {
  1093. LPD3DHAL_DP2VERTEXSHADER pData = (LPD3DHAL_DP2VERTEXSHADER)p;
  1094. pData->dwHandle = m_CurrentVShaderHandle;
  1095. p += sizeof(D3DHAL_DP2VERTEXSHADER);
  1096. }
  1097. break;
  1098. }
  1099. case D3DDP2OP_SETVERTEXSHADERCONST:
  1100. {
  1101. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1102. {
  1103. LPD3DHAL_DP2SETVERTEXSHADERCONST pData =
  1104. (LPD3DHAL_DP2SETVERTEXSHADERCONST)p;
  1105. m_RefVM.GetData( D3DSPR_CONST, pData->dwRegister,
  1106. pData->dwCount, (LPVOID)(pData+1) );
  1107. p += (sizeof(D3DHAL_DP2SETVERTEXSHADERCONST) +
  1108. (pData->dwCount<<4));
  1109. }
  1110. break;
  1111. }
  1112. case D3DDP2OP_SETSTREAMSOURCE:
  1113. {
  1114. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1115. {
  1116. LPD3DHAL_DP2SETSTREAMSOURCE pData =
  1117. (LPD3DHAL_DP2SETSTREAMSOURCE)p;
  1118. pData->dwVBHandle = m_VStream[pData->dwStream].m_dwHandle;
  1119. pData->dwStride = m_VStream[pData->dwStream].m_dwStride;
  1120. p += sizeof(D3DHAL_DP2SETSTREAMSOURCE);
  1121. }
  1122. break;
  1123. }
  1124. case D3DDP2OP_SETINDICES:
  1125. {
  1126. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1127. {
  1128. LPD3DHAL_DP2SETINDICES pData =
  1129. (LPD3DHAL_DP2SETINDICES)p;
  1130. pData->dwVBHandle = m_IndexStream.m_dwHandle;
  1131. pData->dwStride = m_IndexStream.m_dwStride;
  1132. p += sizeof(D3DHAL_DP2SETINDICES);
  1133. }
  1134. break;
  1135. }
  1136. case D3DDP2OP_SETPIXELSHADER:
  1137. {
  1138. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1139. {
  1140. LPD3DHAL_DP2PIXELSHADER pData = (LPD3DHAL_DP2PIXELSHADER)p;
  1141. pData->dwHandle = m_CurrentPShaderHandle;
  1142. p += sizeof(D3DHAL_DP2PIXELSHADER);
  1143. }
  1144. break;
  1145. }
  1146. case D3DDP2OP_SETPIXELSHADERCONST:
  1147. {
  1148. for(DWORD i = 0; i < (DWORD)pCmd->wStateCount; ++i)
  1149. {
  1150. LPD3DHAL_DP2SETPIXELSHADERCONST pData =
  1151. (LPD3DHAL_DP2SETPIXELSHADERCONST)p;
  1152. FLOAT* pfData = (FLOAT*)(pData+1);
  1153. for (UINT iR=pData->dwRegister; iR<pData->dwCount; iR++)
  1154. {
  1155. *(pfData+0) = m_Rast.m_ConstReg[iR][0][0];
  1156. *(pfData+1) = m_Rast.m_ConstReg[iR][0][1];
  1157. *(pfData+2) = m_Rast.m_ConstReg[iR][0][2];
  1158. *(pfData+3) = m_Rast.m_ConstReg[iR][0][3];
  1159. pfData += 4;
  1160. }
  1161. p += (sizeof(D3DHAL_DP2SETPIXELSHADERCONST) +
  1162. (pData->dwCount<<4));
  1163. }
  1164. break;
  1165. }
  1166. default:
  1167. _ASSERT(FALSE, "Ununderstood DP2 command in Capture");
  1168. }
  1169. }
  1170. return D3D_OK;
  1171. }
  1172. HRESULT
  1173. RefDev::CreateStateSet(DWORD dwHandle, D3DSTATEBLOCKTYPE sbType)
  1174. {
  1175. HRESULT hr = S_OK;
  1176. // This DDI should be called only for drivers > DX7
  1177. // and only for those which support TLHals.
  1178. // It is called only when the device created is a pure-device
  1179. // We need to add filtering code in DX9 to make the DDI emulation
  1180. // work.
  1181. _ASSERT( m_dwDDIType > RDDDI_DX8HAL, "This DDI should be called only"
  1182. " for DX8TL\n" );
  1183. // Begin a new stateset
  1184. if( FAILED( hr = BeginStateSet( dwHandle ) ) )
  1185. {
  1186. DPFERR( "CreateStateSet: Begin failed\n" );
  1187. return hr;
  1188. }
  1189. switch( sbType )
  1190. {
  1191. case D3DSBT_VERTEXSTATE:
  1192. hr = RecordVertexState( dwHandle );
  1193. if( FAILED( hr ) )
  1194. {
  1195. DPFERR( "RecordVertexState failed\n" );
  1196. }
  1197. break;
  1198. case D3DSBT_PIXELSTATE:
  1199. hr = RecordPixelState( dwHandle );
  1200. if( FAILED( hr ) )
  1201. {
  1202. DPFERR( "RecordPixelState failed\n" );
  1203. }
  1204. break;
  1205. case D3DSBT_ALL:
  1206. hr = RecordAllState( dwHandle );
  1207. if( FAILED( hr ) )
  1208. {
  1209. DPFERR( "RecordAllState failed\n" );
  1210. }
  1211. break;
  1212. default:
  1213. DPFERR( "Unknown StateBlock type for Creation\n" );
  1214. hr = D3DERR_INVALIDCALL;
  1215. }
  1216. EndStateSet();
  1217. return hr;
  1218. }
  1219. HRESULT
  1220. RefDev::RecordAllState( DWORD dwHandle )
  1221. {
  1222. DWORD data_size = 0;
  1223. DWORD i = 0;
  1224. DWORD j = 0;
  1225. GArrayT<BYTE> data;
  1226. LPD3DHAL_DP2COMMAND pCmd = NULL;
  1227. HRESULT hr = S_OK;
  1228. static D3DRENDERSTATETYPE rstates[] =
  1229. {
  1230. D3DRENDERSTATE_SPECULARENABLE,
  1231. D3DRENDERSTATE_ZENABLE,
  1232. D3DRENDERSTATE_FILLMODE,
  1233. D3DRENDERSTATE_SHADEMODE,
  1234. D3DRENDERSTATE_LINEPATTERN,
  1235. D3DRENDERSTATE_ZWRITEENABLE,
  1236. D3DRENDERSTATE_ALPHATESTENABLE,
  1237. D3DRENDERSTATE_LASTPIXEL,
  1238. D3DRENDERSTATE_SRCBLEND,
  1239. D3DRENDERSTATE_DESTBLEND,
  1240. D3DRENDERSTATE_CULLMODE,
  1241. D3DRENDERSTATE_ZFUNC,
  1242. D3DRENDERSTATE_ALPHAREF,
  1243. D3DRENDERSTATE_ALPHAFUNC,
  1244. D3DRENDERSTATE_DITHERENABLE,
  1245. D3DRENDERSTATE_FOGENABLE,
  1246. D3DRENDERSTATE_STIPPLEDALPHA,
  1247. D3DRENDERSTATE_FOGCOLOR,
  1248. D3DRENDERSTATE_FOGTABLEMODE,
  1249. D3DRENDERSTATE_FOGSTART,
  1250. D3DRENDERSTATE_FOGEND,
  1251. D3DRENDERSTATE_FOGDENSITY,
  1252. D3DRENDERSTATE_EDGEANTIALIAS,
  1253. D3DRENDERSTATE_ALPHABLENDENABLE,
  1254. D3DRENDERSTATE_ZBIAS,
  1255. D3DRENDERSTATE_RANGEFOGENABLE,
  1256. D3DRENDERSTATE_STENCILENABLE,
  1257. D3DRENDERSTATE_STENCILFAIL,
  1258. D3DRENDERSTATE_STENCILZFAIL,
  1259. D3DRENDERSTATE_STENCILPASS,
  1260. D3DRENDERSTATE_STENCILFUNC,
  1261. D3DRENDERSTATE_STENCILREF,
  1262. D3DRENDERSTATE_STENCILMASK,
  1263. D3DRENDERSTATE_STENCILWRITEMASK,
  1264. D3DRENDERSTATE_TEXTUREFACTOR,
  1265. D3DRENDERSTATE_WRAP0,
  1266. D3DRENDERSTATE_WRAP1,
  1267. D3DRENDERSTATE_WRAP2,
  1268. D3DRENDERSTATE_WRAP3,
  1269. D3DRENDERSTATE_WRAP4,
  1270. D3DRENDERSTATE_WRAP5,
  1271. D3DRENDERSTATE_WRAP6,
  1272. D3DRENDERSTATE_WRAP7,
  1273. D3DRENDERSTATE_AMBIENT,
  1274. D3DRENDERSTATE_COLORVERTEX,
  1275. D3DRENDERSTATE_FOGVERTEXMODE,
  1276. D3DRENDERSTATE_CLIPPING,
  1277. D3DRENDERSTATE_LIGHTING,
  1278. D3DRENDERSTATE_NORMALIZENORMALS,
  1279. D3DRENDERSTATE_LOCALVIEWER,
  1280. D3DRENDERSTATE_EMISSIVEMATERIALSOURCE,
  1281. D3DRENDERSTATE_AMBIENTMATERIALSOURCE,
  1282. D3DRENDERSTATE_DIFFUSEMATERIALSOURCE,
  1283. D3DRENDERSTATE_SPECULARMATERIALSOURCE,
  1284. D3DRENDERSTATE_VERTEXBLEND,
  1285. D3DRENDERSTATE_CLIPPLANEENABLE,
  1286. D3DRS_SOFTWAREVERTEXPROCESSING,
  1287. D3DRS_POINTSIZE,
  1288. D3DRS_POINTSIZE_MIN,
  1289. D3DRS_POINTSPRITEENABLE,
  1290. D3DRS_POINTSCALEENABLE,
  1291. D3DRS_POINTSCALE_A,
  1292. D3DRS_POINTSCALE_B,
  1293. D3DRS_POINTSCALE_C,
  1294. D3DRS_MULTISAMPLEANTIALIAS,
  1295. D3DRS_MULTISAMPLEMASK,
  1296. D3DRS_PATCHEDGESTYLE,
  1297. D3DRS_PATCHSEGMENTS,
  1298. D3DRS_POINTSIZE_MAX,
  1299. D3DRS_INDEXEDVERTEXBLENDENABLE,
  1300. D3DRS_COLORWRITEENABLE,
  1301. D3DRS_TWEENFACTOR,
  1302. D3DRS_BLENDOP,
  1303. };
  1304. static D3DTEXTURESTAGESTATETYPE tsstates[] =
  1305. {
  1306. D3DTSS_COLOROP,
  1307. D3DTSS_COLORARG1,
  1308. D3DTSS_COLORARG2,
  1309. D3DTSS_ALPHAOP,
  1310. D3DTSS_ALPHAARG1,
  1311. D3DTSS_ALPHAARG2,
  1312. D3DTSS_BUMPENVMAT00,
  1313. D3DTSS_BUMPENVMAT01,
  1314. D3DTSS_BUMPENVMAT10,
  1315. D3DTSS_BUMPENVMAT11,
  1316. D3DTSS_TEXCOORDINDEX,
  1317. D3DTSS_ADDRESSU,
  1318. D3DTSS_ADDRESSV,
  1319. D3DTSS_BORDERCOLOR,
  1320. D3DTSS_MAGFILTER,
  1321. D3DTSS_MINFILTER,
  1322. D3DTSS_MIPFILTER,
  1323. D3DTSS_MIPMAPLODBIAS,
  1324. D3DTSS_MAXMIPLEVEL,
  1325. D3DTSS_MAXANISOTROPY,
  1326. D3DTSS_BUMPENVLSCALE,
  1327. D3DTSS_BUMPENVLOFFSET,
  1328. D3DTSS_TEXTURETRANSFORMFLAGS,
  1329. D3DTSS_ADDRESSW,
  1330. D3DTSS_COLORARG0,
  1331. D3DTSS_ALPHAARG0,
  1332. D3DTSS_RESULTARG,
  1333. };
  1334. //
  1335. // !!! Dont Capture vertex streams !!!
  1336. // !!! Dont Capture index streams !!!
  1337. // !!! Dont Capture textures !!!
  1338. //
  1339. //
  1340. // Capture render-states
  1341. //
  1342. const UINT16 uiRStates = sizeof(rstates)/sizeof(D3DRENDERSTATETYPE);
  1343. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2RENDERSTATE) *
  1344. uiRStates;
  1345. HR_RET(data.Grow( data_size ));
  1346. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1347. pCmd->wPrimitiveCount = uiRStates;
  1348. pCmd->bCommand = D3DDP2OP_RENDERSTATE;
  1349. D3DHAL_DP2RENDERSTATE* pRS = (D3DHAL_DP2RENDERSTATE*)(pCmd + 1);
  1350. for( i = 0; i < uiRStates; ++i)
  1351. {
  1352. pRS->RenderState = rstates[i];
  1353. pRS->dwState = GetRS()[rstates[i]];
  1354. pRS++;
  1355. }
  1356. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1357. //
  1358. // Capture texture-stage-states
  1359. //
  1360. const UINT16 uiTSStates = sizeof(tsstates)/sizeof(D3DTEXTURESTAGESTATETYPE);
  1361. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1362. sizeof(D3DHAL_DP2TEXTURESTAGESTATE) * uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1363. HR_RET(data.Grow( data_size ));
  1364. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1365. pCmd->wPrimitiveCount = uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1366. pCmd->bCommand = D3DDP2OP_TEXTURESTAGESTATE;
  1367. D3DHAL_DP2TEXTURESTAGESTATE* pTSS = (D3DHAL_DP2TEXTURESTAGESTATE*)(pCmd+1);
  1368. for ( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  1369. {
  1370. for( DWORD j = 0; j < uiTSStates; ++j)
  1371. {
  1372. pTSS->wStage = i;
  1373. pTSS->TSState = tsstates[j];
  1374. pTSS->dwValue = GetTSS( i )[tsstates[j]];
  1375. pTSS++;
  1376. }
  1377. }
  1378. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1379. //
  1380. // Capture viewport
  1381. //
  1382. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2VIEWPORTINFO);
  1383. HR_RET(data.Grow( data_size ));
  1384. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1385. pCmd->wPrimitiveCount = 1;
  1386. pCmd->bCommand = D3DDP2OP_VIEWPORTINFO;
  1387. D3DHAL_DP2VIEWPORTINFO* pVP = (D3DHAL_DP2VIEWPORTINFO*)(pCmd+1);
  1388. pVP->dwX = m_Clipper.m_Viewport.dwX;
  1389. pVP->dwY = m_Clipper.m_Viewport.dwY;
  1390. pVP->dwWidth = m_Clipper.m_Viewport.dwWidth;
  1391. pVP->dwHeight = m_Clipper.m_Viewport.dwHeight;
  1392. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1393. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2ZRANGE);
  1394. HR_RET(data.Grow( data_size ));
  1395. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1396. pCmd->wPrimitiveCount = 1;
  1397. pCmd->bCommand = D3DDP2OP_ZRANGE;
  1398. D3DHAL_DP2ZRANGE* pZR = (D3DHAL_DP2ZRANGE*)(pCmd+1);
  1399. D3DVIEWPORT7& vp = m_Clipper.m_Viewport;
  1400. pZR->dvMinZ = m_Clipper.m_Viewport.dvMinZ;
  1401. pZR->dvMaxZ = m_Clipper.m_Viewport.dvMaxZ;
  1402. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1403. //
  1404. // Capture transforms
  1405. //
  1406. // All the world-matrices, view, projection and the texture matrices
  1407. // (one per stage)
  1408. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2SETTRANSFORM) *
  1409. (RD_MAX_WORLD_MATRICES + D3DHAL_TSS_MAXSTAGES + 2);
  1410. HR_RET(data.Grow( data_size ));
  1411. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1412. pCmd->wPrimitiveCount = RD_MAX_WORLD_MATRICES + D3DHAL_TSS_MAXSTAGES + 2;
  1413. pCmd->bCommand = D3DDP2OP_SETTRANSFORM;
  1414. D3DHAL_DP2SETTRANSFORM* pST = (D3DHAL_DP2SETTRANSFORM*)(pCmd+1);
  1415. for( i = 0; i < RD_MAX_WORLD_MATRICES; i++ )
  1416. {
  1417. pST->xfrmType = (D3DTRANSFORMSTATETYPE)(RD_WORLDMATRIXBASE + i);
  1418. pST->matrix = m_RefVP.m_xfmWorld[i];
  1419. pST++;
  1420. }
  1421. // View Matrix
  1422. pST->xfrmType = D3DTRANSFORMSTATE_VIEW;
  1423. pST->matrix = m_RefVP.m_xfmView;
  1424. pST++;
  1425. // Projection Matrix
  1426. pST->xfrmType = D3DTRANSFORMSTATE_PROJECTION;
  1427. pST->matrix = m_RefVP.m_xfmProj;
  1428. pST++;
  1429. // Texture Matrices
  1430. for( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  1431. {
  1432. pST->xfrmType =
  1433. (D3DTRANSFORMSTATETYPE)(D3DTRANSFORMSTATE_TEXTURE0 + i);
  1434. pST->matrix = m_RefVP.m_xfmTex[i];
  1435. pST++;
  1436. }
  1437. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1438. //
  1439. // Capture clip-planes
  1440. //
  1441. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1442. sizeof(D3DHAL_DP2SETCLIPPLANE) * RD_MAX_USER_CLIPPLANES;
  1443. HR_RET(data.Grow( data_size ));
  1444. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1445. pCmd->wPrimitiveCount = RD_MAX_USER_CLIPPLANES;
  1446. pCmd->bCommand = D3DDP2OP_SETCLIPPLANE;
  1447. D3DHAL_DP2SETCLIPPLANE* pSCP = (D3DHAL_DP2SETCLIPPLANE*)(pCmd+1);
  1448. for( i = 0; i < RD_MAX_USER_CLIPPLANES; i++ )
  1449. {
  1450. pSCP->dwIndex = i;
  1451. for( j=0; j<4; j++ )
  1452. pSCP->plane[j] = m_Clipper.m_userClipPlanes[i].v[j];
  1453. pSCP++;
  1454. }
  1455. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1456. //
  1457. // Capture material
  1458. //
  1459. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2SETMATERIAL);
  1460. HR_RET(data.Grow( data_size ));
  1461. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1462. pCmd->wPrimitiveCount = 1;
  1463. pCmd->bCommand = D3DDP2OP_SETMATERIAL;
  1464. D3DHAL_DP2SETMATERIAL* pSM = (D3DHAL_DP2SETMATERIAL*)(pCmd+1);
  1465. *pSM = m_RefVP.m_Material;
  1466. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1467. //
  1468. // Capture lights
  1469. //
  1470. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DLIGHT7) +
  1471. sizeof(D3DHAL_DP2SETLIGHT)*2;
  1472. HR_RET(data.Grow( data_size ));
  1473. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1474. pCmd->wPrimitiveCount = 2;
  1475. pCmd->bCommand = D3DDP2OP_SETLIGHT;
  1476. D3DHAL_DP2SETLIGHT* pSL = (D3DHAL_DP2SETLIGHT *)(pCmd + 1);
  1477. D3DHAL_DP2SETLIGHT* pSL2 = pSL + 1;
  1478. pSL2->dwDataType = D3DHAL_SETLIGHT_DATA;
  1479. for( i = 0; i < m_RefVP.m_LightArray.GetSize(); i++ )
  1480. {
  1481. if( m_RefVP.m_LightArray[i].IsRefered() )
  1482. {
  1483. pSL2->dwIndex = pSL->dwIndex = i;
  1484. if( m_RefVP.m_LightArray[i].IsEnabled() )
  1485. {
  1486. pSL->dwDataType = D3DHAL_SETLIGHT_ENABLE;
  1487. }
  1488. else
  1489. {
  1490. pSL->dwDataType = D3DHAL_SETLIGHT_DISABLE;
  1491. }
  1492. m_RefVP.m_LightArray[i].GetLight((D3DLIGHT7*)(pSL2 + 1));
  1493. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1494. }
  1495. }
  1496. //
  1497. // Capture current vertex shader
  1498. //
  1499. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2VERTEXSHADER);
  1500. HR_RET(data.Grow( data_size ));
  1501. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1502. pCmd->wPrimitiveCount = 1;
  1503. pCmd->bCommand = D3DDP2OP_SETVERTEXSHADER;
  1504. D3DHAL_DP2VERTEXSHADER* pVS = (D3DHAL_DP2VERTEXSHADER*)(pCmd+1);
  1505. pVS->dwHandle = m_CurrentVShaderHandle;
  1506. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1507. //
  1508. // Capture current pixel shader
  1509. //
  1510. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2PIXELSHADER);
  1511. HR_RET(data.Grow( data_size ));
  1512. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1513. pCmd->wPrimitiveCount = 1;
  1514. pCmd->bCommand = D3DDP2OP_SETPIXELSHADER;
  1515. D3DHAL_DP2PIXELSHADER* pPS = (D3DHAL_DP2PIXELSHADER*)(pCmd+1);
  1516. pPS->dwHandle = m_CurrentPShaderHandle;
  1517. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1518. //
  1519. // Capture vertex shader constants
  1520. //
  1521. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1522. sizeof(D3DHAL_DP2SETVERTEXSHADERCONST) + (RD_MAX_NUMCONSTREG << 4);
  1523. HR_RET(data.Grow( data_size ));
  1524. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1525. pCmd->wPrimitiveCount = 1;
  1526. pCmd->bCommand = D3DDP2OP_SETVERTEXSHADERCONST;
  1527. D3DHAL_DP2SETVERTEXSHADERCONST* pVSC =
  1528. (D3DHAL_DP2SETVERTEXSHADERCONST*)(pCmd+1);
  1529. pVSC->dwRegister = 0;
  1530. pVSC->dwCount = RD_MAX_NUMCONSTREG;
  1531. m_RefVM.GetData( D3DSPR_CONST, pVSC->dwRegister, pVSC->dwCount,
  1532. (LPVOID)(pVSC+1) );
  1533. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1534. //
  1535. // Capture pixel shader constants
  1536. //
  1537. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1538. sizeof(D3DHAL_DP2SETPIXELSHADERCONST) + (RDPS_MAX_NUMCONSTREG << 4);
  1539. HR_RET(data.Grow( data_size ));
  1540. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1541. pCmd->wPrimitiveCount = 1;
  1542. pCmd->bCommand = D3DDP2OP_SETPIXELSHADERCONST;
  1543. D3DHAL_DP2SETPIXELSHADERCONST* pPSC =
  1544. (D3DHAL_DP2SETPIXELSHADERCONST*)(pCmd+1);
  1545. pPSC->dwRegister = 0;
  1546. pPSC->dwCount = RDPS_MAX_NUMCONSTREG;
  1547. FLOAT* pfData = (FLOAT*)(pPSC+1);
  1548. for (UINT iR=pPSC->dwRegister; iR<pPSC->dwCount; iR++)
  1549. {
  1550. *(pfData+0) = m_Rast.m_ConstReg[iR][0][0];
  1551. *(pfData+1) = m_Rast.m_ConstReg[iR][0][1];
  1552. *(pfData+2) = m_Rast.m_ConstReg[iR][0][2];
  1553. *(pfData+3) = m_Rast.m_ConstReg[iR][0][3];
  1554. pfData += 4;
  1555. }
  1556. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1557. return hr;
  1558. }
  1559. HRESULT
  1560. RefDev::RecordVertexState( DWORD dwHandle )
  1561. {
  1562. DWORD data_size = 0;
  1563. DWORD i = 0;
  1564. DWORD j = 0;
  1565. GArrayT<BYTE> data;
  1566. LPD3DHAL_DP2COMMAND pCmd = NULL;
  1567. HRESULT hr = S_OK;
  1568. static D3DRENDERSTATETYPE rstates[] =
  1569. {
  1570. D3DRENDERSTATE_SHADEMODE,
  1571. D3DRENDERSTATE_SPECULARENABLE,
  1572. D3DRENDERSTATE_CULLMODE,
  1573. D3DRENDERSTATE_FOGENABLE,
  1574. D3DRENDERSTATE_FOGCOLOR,
  1575. D3DRENDERSTATE_FOGTABLEMODE,
  1576. D3DRENDERSTATE_FOGSTART,
  1577. D3DRENDERSTATE_FOGEND,
  1578. D3DRENDERSTATE_FOGDENSITY,
  1579. D3DRENDERSTATE_RANGEFOGENABLE,
  1580. D3DRENDERSTATE_AMBIENT,
  1581. D3DRENDERSTATE_COLORVERTEX,
  1582. D3DRENDERSTATE_FOGVERTEXMODE,
  1583. D3DRENDERSTATE_CLIPPING,
  1584. D3DRENDERSTATE_LIGHTING,
  1585. D3DRENDERSTATE_NORMALIZENORMALS,
  1586. D3DRENDERSTATE_LOCALVIEWER,
  1587. D3DRENDERSTATE_EMISSIVEMATERIALSOURCE,
  1588. D3DRENDERSTATE_AMBIENTMATERIALSOURCE,
  1589. D3DRENDERSTATE_DIFFUSEMATERIALSOURCE,
  1590. D3DRENDERSTATE_SPECULARMATERIALSOURCE,
  1591. D3DRENDERSTATE_VERTEXBLEND,
  1592. D3DRENDERSTATE_CLIPPLANEENABLE,
  1593. D3DRS_SOFTWAREVERTEXPROCESSING,
  1594. D3DRS_POINTSIZE,
  1595. D3DRS_POINTSIZE_MIN,
  1596. D3DRS_POINTSPRITEENABLE,
  1597. D3DRS_POINTSCALEENABLE,
  1598. D3DRS_POINTSCALE_A,
  1599. D3DRS_POINTSCALE_B,
  1600. D3DRS_POINTSCALE_C,
  1601. D3DRS_MULTISAMPLEANTIALIAS,
  1602. D3DRS_MULTISAMPLEMASK,
  1603. D3DRS_PATCHEDGESTYLE,
  1604. D3DRS_PATCHSEGMENTS,
  1605. D3DRS_POINTSIZE_MAX,
  1606. D3DRS_INDEXEDVERTEXBLENDENABLE,
  1607. D3DRS_TWEENFACTOR,
  1608. };
  1609. static D3DTEXTURESTAGESTATETYPE tsstates[] =
  1610. {
  1611. D3DTSS_TEXCOORDINDEX,
  1612. D3DTSS_TEXTURETRANSFORMFLAGS
  1613. };
  1614. //
  1615. // Capture render-states
  1616. //
  1617. const UINT16 uiRStates = sizeof(rstates)/sizeof(D3DRENDERSTATETYPE);
  1618. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2RENDERSTATE) *
  1619. uiRStates;
  1620. HR_RET(data.Grow( data_size ));
  1621. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1622. pCmd->wPrimitiveCount = uiRStates;
  1623. pCmd->bCommand = D3DDP2OP_RENDERSTATE;
  1624. D3DHAL_DP2RENDERSTATE* pRS = (D3DHAL_DP2RENDERSTATE*)(pCmd + 1);
  1625. for( i = 0; i < uiRStates; ++i)
  1626. {
  1627. pRS->RenderState = rstates[i];
  1628. pRS->dwState = GetRS()[rstates[i]];
  1629. pRS++;
  1630. }
  1631. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1632. //
  1633. // Capture texture-stage-states
  1634. //
  1635. const UINT16 uiTSStates = sizeof(tsstates)/sizeof(D3DTEXTURESTAGESTATETYPE);
  1636. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TEXTURESTAGESTATE)
  1637. * uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1638. HR_RET(data.Grow( data_size ));
  1639. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1640. pCmd->wPrimitiveCount = uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1641. pCmd->bCommand = D3DDP2OP_TEXTURESTAGESTATE;
  1642. D3DHAL_DP2TEXTURESTAGESTATE* pTSS = (D3DHAL_DP2TEXTURESTAGESTATE*)(pCmd+1);
  1643. for ( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  1644. {
  1645. for( DWORD j = 0; j < uiTSStates; ++j)
  1646. {
  1647. pTSS->wStage = i;
  1648. pTSS->TSState = tsstates[j];
  1649. pTSS->dwValue = GetTSS( i )[tsstates[j]];
  1650. pTSS++;
  1651. }
  1652. }
  1653. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1654. //
  1655. // Capture lights
  1656. //
  1657. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DLIGHT7) +
  1658. sizeof(D3DHAL_DP2SETLIGHT)*2;
  1659. HR_RET(data.Grow( data_size ));
  1660. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1661. pCmd->wPrimitiveCount = 2;
  1662. pCmd->bCommand = D3DDP2OP_SETLIGHT;
  1663. D3DHAL_DP2SETLIGHT* pSL = (D3DHAL_DP2SETLIGHT *)(pCmd + 1);
  1664. D3DHAL_DP2SETLIGHT* pSL2 = pSL + 1;
  1665. pSL2->dwDataType = D3DHAL_SETLIGHT_DATA;
  1666. for( i = 0; i < m_RefVP.m_LightArray.GetSize(); i++ )
  1667. {
  1668. if( m_RefVP.m_LightArray[i].IsRefered() )
  1669. {
  1670. pSL2->dwIndex = pSL->dwIndex = i;
  1671. if( m_RefVP.m_LightArray[i].IsEnabled() )
  1672. {
  1673. pSL->dwDataType = D3DHAL_SETLIGHT_ENABLE;
  1674. }
  1675. else
  1676. {
  1677. pSL->dwDataType = D3DHAL_SETLIGHT_DISABLE;
  1678. }
  1679. m_RefVP.m_LightArray[i].GetLight((D3DLIGHT7*)(pSL2 + 1));
  1680. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1681. }
  1682. }
  1683. //
  1684. // Capture vertex shader constants
  1685. //
  1686. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1687. sizeof(D3DHAL_DP2SETVERTEXSHADERCONST) + (RD_MAX_NUMCONSTREG << 4);
  1688. HR_RET(data.Grow( data_size ));
  1689. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1690. pCmd->wPrimitiveCount = 1;
  1691. pCmd->bCommand = D3DDP2OP_SETVERTEXSHADERCONST;
  1692. D3DHAL_DP2SETVERTEXSHADERCONST* pVSC =
  1693. (D3DHAL_DP2SETVERTEXSHADERCONST*)(pCmd+1);
  1694. pVSC->dwRegister = 0;
  1695. pVSC->dwCount = RD_MAX_NUMCONSTREG;
  1696. m_RefVM.GetData( D3DSPR_CONST, pVSC->dwRegister, pVSC->dwCount,
  1697. (LPVOID)(pVSC+1) );
  1698. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1699. //
  1700. // Capture current vertex shader
  1701. //
  1702. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2VERTEXSHADER);
  1703. HR_RET(data.Grow( data_size ));
  1704. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1705. pCmd->wPrimitiveCount = 1;
  1706. pCmd->bCommand = D3DDP2OP_SETVERTEXSHADER;
  1707. D3DHAL_DP2VERTEXSHADER* pVS =
  1708. (D3DHAL_DP2VERTEXSHADER*)(pCmd+1);
  1709. pVS->dwHandle = m_CurrentVShaderHandle;
  1710. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1711. return hr;
  1712. }
  1713. HRESULT
  1714. RefDev::RecordPixelState( DWORD dwHandle )
  1715. {
  1716. DWORD data_size = 0;
  1717. DWORD i = 0;
  1718. DWORD j = 0;
  1719. GArrayT<BYTE> data;
  1720. LPD3DHAL_DP2COMMAND pCmd = NULL;
  1721. HRESULT hr = S_OK;
  1722. static D3DRENDERSTATETYPE rstates[] =
  1723. {
  1724. D3DRENDERSTATE_ZENABLE,
  1725. D3DRENDERSTATE_FILLMODE,
  1726. D3DRENDERSTATE_SHADEMODE,
  1727. D3DRENDERSTATE_LINEPATTERN,
  1728. D3DRENDERSTATE_ZWRITEENABLE,
  1729. D3DRENDERSTATE_ALPHATESTENABLE,
  1730. D3DRENDERSTATE_LASTPIXEL,
  1731. D3DRENDERSTATE_SRCBLEND,
  1732. D3DRENDERSTATE_DESTBLEND,
  1733. D3DRENDERSTATE_ZFUNC,
  1734. D3DRENDERSTATE_ALPHAREF,
  1735. D3DRENDERSTATE_ALPHAFUNC,
  1736. D3DRENDERSTATE_DITHERENABLE,
  1737. D3DRENDERSTATE_STIPPLEDALPHA,
  1738. D3DRENDERSTATE_FOGSTART,
  1739. D3DRENDERSTATE_FOGEND,
  1740. D3DRENDERSTATE_FOGDENSITY,
  1741. D3DRENDERSTATE_EDGEANTIALIAS,
  1742. D3DRENDERSTATE_ALPHABLENDENABLE,
  1743. D3DRENDERSTATE_ZBIAS,
  1744. D3DRENDERSTATE_STENCILENABLE,
  1745. D3DRENDERSTATE_STENCILFAIL,
  1746. D3DRENDERSTATE_STENCILZFAIL,
  1747. D3DRENDERSTATE_STENCILPASS,
  1748. D3DRENDERSTATE_STENCILFUNC,
  1749. D3DRENDERSTATE_STENCILREF,
  1750. D3DRENDERSTATE_STENCILMASK,
  1751. D3DRENDERSTATE_STENCILWRITEMASK,
  1752. D3DRENDERSTATE_TEXTUREFACTOR,
  1753. D3DRENDERSTATE_WRAP0,
  1754. D3DRENDERSTATE_WRAP1,
  1755. D3DRENDERSTATE_WRAP2,
  1756. D3DRENDERSTATE_WRAP3,
  1757. D3DRENDERSTATE_WRAP4,
  1758. D3DRENDERSTATE_WRAP5,
  1759. D3DRENDERSTATE_WRAP6,
  1760. D3DRENDERSTATE_WRAP7,
  1761. D3DRS_COLORWRITEENABLE,
  1762. D3DRS_BLENDOP,
  1763. };
  1764. static D3DTEXTURESTAGESTATETYPE tsstates[] =
  1765. {
  1766. D3DTSS_COLOROP,
  1767. D3DTSS_COLORARG1,
  1768. D3DTSS_COLORARG2,
  1769. D3DTSS_ALPHAOP,
  1770. D3DTSS_ALPHAARG1,
  1771. D3DTSS_ALPHAARG2,
  1772. D3DTSS_BUMPENVMAT00,
  1773. D3DTSS_BUMPENVMAT01,
  1774. D3DTSS_BUMPENVMAT10,
  1775. D3DTSS_BUMPENVMAT11,
  1776. D3DTSS_TEXCOORDINDEX,
  1777. D3DTSS_ADDRESSU,
  1778. D3DTSS_ADDRESSV,
  1779. D3DTSS_BORDERCOLOR,
  1780. D3DTSS_MAGFILTER,
  1781. D3DTSS_MINFILTER,
  1782. D3DTSS_MIPFILTER,
  1783. D3DTSS_MIPMAPLODBIAS,
  1784. D3DTSS_MAXMIPLEVEL,
  1785. D3DTSS_MAXANISOTROPY,
  1786. D3DTSS_BUMPENVLSCALE,
  1787. D3DTSS_BUMPENVLOFFSET,
  1788. D3DTSS_TEXTURETRANSFORMFLAGS,
  1789. D3DTSS_ADDRESSW,
  1790. D3DTSS_COLORARG0,
  1791. D3DTSS_ALPHAARG0,
  1792. D3DTSS_RESULTARG,
  1793. };
  1794. //
  1795. // Capture render-states
  1796. //
  1797. const UINT16 uiRStates = sizeof(rstates)/sizeof(D3DRENDERSTATETYPE);
  1798. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2RENDERSTATE) *
  1799. uiRStates;
  1800. HR_RET(data.Grow( data_size ));
  1801. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1802. pCmd->wPrimitiveCount = uiRStates;
  1803. pCmd->bCommand = D3DDP2OP_RENDERSTATE;
  1804. D3DHAL_DP2RENDERSTATE* pRS = (D3DHAL_DP2RENDERSTATE*)(pCmd + 1);
  1805. for( i = 0; i < uiRStates; ++i)
  1806. {
  1807. pRS->RenderState = rstates[i];
  1808. pRS->dwState = GetRS()[rstates[i]];
  1809. pRS++;
  1810. }
  1811. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1812. //
  1813. // Capture texture-stage-states
  1814. //
  1815. const UINT16 uiTSStates = sizeof(tsstates)/sizeof(D3DTEXTURESTAGESTATETYPE);
  1816. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TEXTURESTAGESTATE)
  1817. * uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1818. HR_RET(data.Grow( data_size ));
  1819. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1820. pCmd->wPrimitiveCount = uiTSStates * D3DHAL_TSS_MAXSTAGES;
  1821. pCmd->bCommand = D3DDP2OP_TEXTURESTAGESTATE;
  1822. D3DHAL_DP2TEXTURESTAGESTATE* pTSS = (D3DHAL_DP2TEXTURESTAGESTATE*)(pCmd+1);
  1823. for ( i = 0; i < D3DHAL_TSS_MAXSTAGES; i++ )
  1824. {
  1825. for( DWORD j = 0; j < uiTSStates; ++j)
  1826. {
  1827. pTSS->wStage = i;
  1828. pTSS->TSState = tsstates[j];
  1829. pTSS->dwValue = GetTSS( i )[tsstates[j]];
  1830. pTSS++;
  1831. }
  1832. }
  1833. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1834. //
  1835. // Capture pixel shader constants
  1836. //
  1837. data_size = sizeof(D3DHAL_DP2COMMAND) +
  1838. sizeof(D3DHAL_DP2SETPIXELSHADERCONST) + (RDPS_MAX_NUMCONSTREG << 4);
  1839. HR_RET(data.Grow( data_size ));
  1840. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1841. pCmd->wPrimitiveCount = 1;
  1842. pCmd->bCommand = D3DDP2OP_SETPIXELSHADERCONST;
  1843. D3DHAL_DP2SETPIXELSHADERCONST* pPSC =
  1844. (D3DHAL_DP2SETPIXELSHADERCONST*)(pCmd+1);
  1845. pPSC->dwRegister = 0;
  1846. pPSC->dwCount = RDPS_MAX_NUMCONSTREG;
  1847. FLOAT* pfData = (FLOAT*)(pPSC+1);
  1848. for (UINT iR=pPSC->dwRegister; iR<pPSC->dwCount; iR++)
  1849. {
  1850. *(pfData+0) = m_Rast.m_ConstReg[iR][0][0];
  1851. *(pfData+1) = m_Rast.m_ConstReg[iR][0][1];
  1852. *(pfData+2) = m_Rast.m_ConstReg[iR][0][2];
  1853. *(pfData+3) = m_Rast.m_ConstReg[iR][0][3];
  1854. pfData += 4;
  1855. }
  1856. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1857. //
  1858. // Capture current pixel shader
  1859. //
  1860. data_size = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2PIXELSHADER);
  1861. HR_RET(data.Grow( data_size ));
  1862. pCmd = (LPD3DHAL_DP2COMMAND)&(data[0]);
  1863. pCmd->wPrimitiveCount = 1;
  1864. pCmd->bCommand = D3DDP2OP_SETPIXELSHADER;
  1865. D3DHAL_DP2PIXELSHADER* pPS = (D3DHAL_DP2PIXELSHADER*)(pCmd+1);
  1866. pPS->dwHandle = m_CurrentPShaderHandle;
  1867. HR_RET(RecordStates( (PUINT8)pCmd, data_size ));
  1868. return hr;
  1869. }
  1870. //-----------------------------------------------------------------------------
  1871. //
  1872. // SetRenderState -
  1873. //
  1874. //-----------------------------------------------------------------------------
  1875. void
  1876. RefDev::SetRenderState( DWORD dwState, DWORD dwValue )
  1877. {
  1878. // check for range before continuing
  1879. if( dwState >= D3DHAL_MAX_RSTATES )
  1880. {
  1881. return;
  1882. }
  1883. // set value in internal object
  1884. m_dwRenderState[dwState] = dwValue;
  1885. // do special validation work for some render states
  1886. switch ( dwState )
  1887. {
  1888. case D3DRENDERSTATE_ZENABLE:
  1889. if( dwValue )
  1890. m_Clipper.m_dwFlags |= RefClipper::RCLIP_Z_ENABLE;
  1891. else
  1892. m_Clipper.m_dwFlags &= ~RefClipper::RCLIP_Z_ENABLE;
  1893. break;
  1894. case D3DRENDERSTATE_LIGHTING:
  1895. if( dwValue )
  1896. m_RefVP.m_dwTLState |= RDPV_DOLIGHTING;
  1897. else
  1898. m_RefVP.m_dwTLState &= ~RDPV_DOLIGHTING;
  1899. break;
  1900. case D3DRS_INDEXEDVERTEXBLENDENABLE:
  1901. if( dwValue )
  1902. m_RefVP.m_dwTLState |= RDPV_DOINDEXEDVERTEXBLEND;
  1903. else
  1904. m_RefVP.m_dwTLState &= ~RDPV_DOINDEXEDVERTEXBLEND;
  1905. break;
  1906. case D3DRENDERSTATE_CLIPPING:
  1907. if( dwValue )
  1908. m_RefVP.m_dwTLState |= RDPV_DOCLIPPING;
  1909. else
  1910. m_RefVP.m_dwTLState &= ~RDPV_DOCLIPPING;
  1911. break;
  1912. case D3DRENDERSTATE_SHADEMODE:
  1913. {
  1914. if( dwValue == D3DSHADE_FLAT )
  1915. m_Clipper.m_dwFlags |= RefClipper::RCLIP_DO_FLATSHADING;
  1916. else
  1917. m_Clipper.m_dwFlags &= ~RefClipper::RCLIP_DO_FLATSHADING;
  1918. }
  1919. break;
  1920. case D3DRENDERSTATE_FILLMODE:
  1921. {
  1922. if( dwValue == D3DFILL_WIREFRAME )
  1923. m_Clipper.m_dwFlags |= RefClipper::RCLIP_DO_WIREFRAME;
  1924. else
  1925. m_Clipper.m_dwFlags &= ~RefClipper::RCLIP_DO_WIREFRAME;
  1926. }
  1927. break;
  1928. case D3DRENDERSTATE_NORMALIZENORMALS:
  1929. {
  1930. if( dwValue )
  1931. m_RefVP.m_dwTLState |= RDPV_NORMALIZENORMALS;
  1932. else
  1933. m_RefVP.m_dwTLState &= ~RDPV_NORMALIZENORMALS;
  1934. }
  1935. break;
  1936. case D3DRENDERSTATE_LOCALVIEWER:
  1937. {
  1938. if( dwValue )
  1939. m_RefVP.m_dwTLState |= RDPV_LOCALVIEWER;
  1940. else
  1941. m_RefVP.m_dwTLState &= ~RDPV_LOCALVIEWER;
  1942. }
  1943. break;
  1944. case D3DRENDERSTATE_SPECULARENABLE:
  1945. {
  1946. if( dwValue )
  1947. m_RefVP.m_dwTLState |= RDPV_DOSPECULAR;
  1948. else
  1949. m_RefVP.m_dwTLState &= ~RDPV_DOSPECULAR;
  1950. }
  1951. break;
  1952. case D3DRENDERSTATE_COLORVERTEX:
  1953. case D3DRENDERSTATE_AMBIENTMATERIALSOURCE:
  1954. case D3DRENDERSTATE_DIFFUSEMATERIALSOURCE:
  1955. case D3DRENDERSTATE_SPECULARMATERIALSOURCE:
  1956. case D3DRENDERSTATE_EMISSIVEMATERIALSOURCE:
  1957. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_COLORVTX;
  1958. break;
  1959. case D3DRENDERSTATE_FOGCOLOR:
  1960. {
  1961. m_RefVP.m_lighting.fog_color = (D3DCOLOR) dwValue;
  1962. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_FOG;
  1963. }
  1964. break;
  1965. case D3DRENDERSTATE_FOGTABLESTART:
  1966. {
  1967. m_RefVP.m_lighting.fog_start = *(D3DVALUE*)&dwValue;
  1968. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_FOG;
  1969. }
  1970. break;
  1971. case D3DRENDERSTATE_FOGTABLEEND:
  1972. {
  1973. m_RefVP.m_lighting.fog_end = *(D3DVALUE*)&dwValue;
  1974. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_FOG;
  1975. }
  1976. break;
  1977. case D3DRENDERSTATE_FOGTABLEDENSITY:
  1978. {
  1979. m_RefVP.m_lighting.fog_density = *(D3DVALUE*)&dwValue;
  1980. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_FOG;
  1981. }
  1982. break;
  1983. case D3DRENDERSTATE_FOGVERTEXMODE:
  1984. {
  1985. m_RefVP.m_lighting.fog_mode = (int) dwValue;
  1986. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_FOG;
  1987. }
  1988. break;
  1989. case D3DRENDERSTATE_AMBIENT:
  1990. {
  1991. m_RefVP.m_lighting.ambient_red =
  1992. D3DVAL(RGBA_GETRED(dwValue))/D3DVALUE(255);
  1993. m_RefVP.m_lighting.ambient_green =
  1994. D3DVAL(RGBA_GETGREEN(dwValue))/D3DVALUE(255);
  1995. m_RefVP.m_lighting.ambient_blue =
  1996. D3DVAL(RGBA_GETBLUE(dwValue))/D3DVALUE(255);
  1997. m_RefVP.m_lighting.ambient_save = dwValue;
  1998. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_MATERIAL;
  1999. }
  2000. break;
  2001. //
  2002. // map legacy texture to multi-texture stage 0
  2003. //
  2004. case D3DRENDERSTATE_TEXTUREMAPBLEND:
  2005. // map legacy blending state to texture stage 0
  2006. MapLegacyTextureBlend();
  2007. break;
  2008. // map legacy modes with one-to-one mappings to texture stage 0
  2009. case D3DRENDERSTATE_TEXTUREADDRESS:
  2010. // not available in DX8 headers
  2011. // m_TextureStageState[0].m_dwVal[D3DTSS_ADDRESS] =
  2012. m_TextureStageState[0].m_dwVal[D3DTSS_ADDRESSU] =
  2013. m_TextureStageState[0].m_dwVal[D3DTSS_ADDRESSV] = dwValue;
  2014. break;
  2015. case D3DRENDERSTATE_TEXTUREADDRESSU:
  2016. m_TextureStageState[0].m_dwVal[D3DTSS_ADDRESSU] = dwValue;
  2017. break;
  2018. case D3DRENDERSTATE_TEXTUREADDRESSV:
  2019. m_TextureStageState[0].m_dwVal[D3DTSS_ADDRESSV] = dwValue;
  2020. break;
  2021. case D3DRENDERSTATE_MIPMAPLODBIAS:
  2022. m_TextureStageState[0].m_dwVal[D3DTSS_MIPMAPLODBIAS] = dwValue;
  2023. break;
  2024. case D3DRENDERSTATE_BORDERCOLOR:
  2025. m_TextureStageState[0].m_dwVal[D3DTSS_BORDERCOLOR] = dwValue;
  2026. break;
  2027. case D3DRENDERSTATE_ANISOTROPY:
  2028. m_TextureStageState[0].m_dwVal[D3DTSS_MAXANISOTROPY] = dwValue;
  2029. // fall thru to update filter state
  2030. case D3DRENDERSTATE_TEXTUREMAG:
  2031. case D3DRENDERSTATE_TEXTUREMIN:
  2032. // map legacy filtering/sampling state to texture stage 0
  2033. MapLegacyTextureFilter();
  2034. break;
  2035. case D3DRENDERSTATE_TEXTUREHANDLE:
  2036. // map thru to set handle for first stage
  2037. SetTextureStageState( 0, D3DTSS_TEXTUREMAP, dwValue );
  2038. break;
  2039. //
  2040. // map legacy WRAPU/V state through to controls for tex coord 0
  2041. //
  2042. case D3DRENDERSTATE_WRAPU:
  2043. m_dwRenderState[D3DRENDERSTATE_WRAP0] &= ~D3DWRAP_U;
  2044. m_dwRenderState[D3DRENDERSTATE_WRAP0] |= ((dwValue) ? D3DWRAP_U : 0);
  2045. break;
  2046. case D3DRENDERSTATE_WRAPV:
  2047. m_dwRenderState[D3DRENDERSTATE_WRAP0] &= ~D3DWRAP_V;
  2048. m_dwRenderState[D3DRENDERSTATE_WRAP0] |= ((dwValue) ? D3DWRAP_V : 0);
  2049. break;
  2050. case D3DRS_MULTISAMPLEANTIALIAS:
  2051. case D3DRS_MULTISAMPLEMASK:
  2052. m_dwRastFlags |= RDRF_MULTISAMPLE_CHANGED;
  2053. break;
  2054. //
  2055. // Scene Capture
  2056. //
  2057. case D3DRENDERSTATE_SCENECAPTURE:
  2058. if( dwValue )
  2059. SceneCapture(D3DHAL_SCENE_CAPTURE_START);
  2060. else
  2061. SceneCapture(D3DHAL_SCENE_CAPTURE_END);
  2062. break;
  2063. case D3DRS_POINTSIZE:
  2064. m_RefVP.m_fPointSize = m_fRenderState[dwState];
  2065. break;
  2066. case D3DRS_POINTSCALE_A:
  2067. m_RefVP.m_fPointAttA = m_fRenderState[dwState];
  2068. break;
  2069. case D3DRS_POINTSCALE_B:
  2070. m_RefVP.m_fPointAttB = m_fRenderState[dwState];
  2071. break;
  2072. case D3DRS_POINTSCALE_C:
  2073. m_RefVP.m_fPointAttC = m_fRenderState[dwState];
  2074. break;
  2075. case D3DRS_POINTSIZE_MIN:
  2076. m_RefVP.m_fPointSizeMin = m_fRenderState[dwState];
  2077. break;
  2078. case D3DRS_POINTSIZE_MAX:
  2079. m_RefVP.m_fPointSizeMax = min(RD_MAX_POINT_SIZE,
  2080. m_fRenderState[dwState]);
  2081. break;
  2082. case D3DRS_TWEENFACTOR:
  2083. m_RefVP.m_fTweenFactor = m_fRenderState[dwState];
  2084. break;
  2085. //
  2086. // HOSurface DDI only renderstate
  2087. //
  2088. case D3DRS_DELETERTPATCH:
  2089. if(dwValue < m_HOSCoeffs.GetSize())
  2090. {
  2091. RDHOCoeffs &coeffs = m_HOSCoeffs[dwValue];
  2092. delete[] coeffs.m_pNumSegs;
  2093. coeffs.m_pNumSegs = 0;
  2094. for(unsigned i = 0; i < RD_MAX_NUMSTREAMS; ++i)
  2095. {
  2096. delete[] coeffs.m_pData[i];
  2097. coeffs.m_pData[i] = 0;
  2098. }
  2099. }
  2100. }
  2101. }
  2102. HRESULT
  2103. RefDev::Dp2SetRenderTarget(LPD3DHAL_DP2COMMAND pCmd)
  2104. {
  2105. LPD3DHAL_DP2SETRENDERTARGET pSRTData;
  2106. HRESULT hr;
  2107. // Get new data by ignoring all but the last structure
  2108. pSRTData = (D3DHAL_DP2SETRENDERTARGET*)(pCmd + 1) + (pCmd->wStateCount - 1);
  2109. // set 'changed' flags
  2110. m_dwRastFlags =
  2111. RDRF_MULTISAMPLE_CHANGED|
  2112. RDRF_PIXELSHADER_CHANGED|
  2113. RDRF_LEGACYPIXELSHADER_CHANGED|
  2114. RDRF_TEXTURESTAGESTATE_CHANGED;
  2115. return GetRenderTarget()->Initialize( m_pDDLcl,
  2116. pSRTData->hRenderTarget,
  2117. pSRTData->hZBuffer );
  2118. }
  2119. HRESULT
  2120. RefDev::Dp2CreateVertexShader( DWORD handle,
  2121. DWORD dwDeclSize, LPDWORD pDecl,
  2122. DWORD dwCodeSize, LPDWORD pCode )
  2123. {
  2124. HRESULT hr = S_OK;
  2125. HR_RET( m_VShaderHandleArray.Grow( handle ) );
  2126. //
  2127. // Validation sequence
  2128. //
  2129. #if DBG
  2130. _ASSERT( m_VShaderHandleArray[handle].m_tag == 0,
  2131. "A shader exists with the given handle, tag is non-zero" );
  2132. #endif
  2133. _ASSERT( pDecl, "A declaration should exist" );
  2134. _ASSERT( dwDeclSize, "A declaration size should be non-zero" );
  2135. _ASSERT( m_VShaderHandleArray[handle].m_pShader == NULL,
  2136. "A shader exists with the given handle" );
  2137. RDVShader* pShader = m_VShaderHandleArray[handle].m_pShader =
  2138. new RDVShader;
  2139. if( pShader == NULL )
  2140. return E_OUTOFMEMORY;
  2141. //
  2142. // Parse the declaration
  2143. //
  2144. if( FAILED( hr = pShader->m_Declaration.Parse( pDecl,
  2145. !(BOOL)dwCodeSize ) ) )
  2146. {
  2147. DPFERR( "Vertex Shader declaration parsing failed" );
  2148. goto error_ret;
  2149. }
  2150. //
  2151. // Now compile the shader code if any of it is given
  2152. //
  2153. if( dwCodeSize )
  2154. {
  2155. pShader->m_pCode = m_RefVM.CompileCode( dwCodeSize, pCode );
  2156. if( pShader->m_pCode == NULL )
  2157. {
  2158. DPFERR( "Vertex Shader Code compilation failed" );
  2159. hr = E_FAIL;
  2160. goto error_ret;
  2161. }
  2162. }
  2163. #if DBG
  2164. // Everything successful, mark this handle as in use.
  2165. m_VShaderHandleArray[handle].m_tag = 1;
  2166. #endif
  2167. return S_OK;
  2168. error_ret:
  2169. delete pShader;
  2170. m_VShaderHandleArray[handle].m_pShader = NULL;
  2171. #if DBG
  2172. m_VShaderHandleArray[handle].m_tag = 0;
  2173. #endif
  2174. return hr;
  2175. }
  2176. HRESULT
  2177. RefDev::Dp2DeleteVertexShader(LPD3DHAL_DP2COMMAND pCmd)
  2178. {
  2179. HRESULT hr = S_OK;
  2180. LPD3DHAL_DP2VERTEXSHADER pVS =
  2181. (LPD3DHAL_DP2VERTEXSHADER)(pCmd + 1);
  2182. for( int i = 0; i < pCmd->wStateCount; i++ )
  2183. {
  2184. DWORD handle = pVS[i].dwHandle;
  2185. _ASSERT( m_VShaderHandleArray.IsValidIndex( handle ),
  2186. "Such a shader does not exist" );
  2187. _ASSERT( m_VShaderHandleArray[handle].m_pShader,
  2188. "Such a shader does not exist" );
  2189. delete m_VShaderHandleArray[handle].m_pShader;
  2190. m_VShaderHandleArray[handle].m_pShader = NULL;
  2191. #if DBG
  2192. m_VShaderHandleArray[handle].m_tag = 0;
  2193. #endif
  2194. if( handle == m_CurrentVShaderHandle )
  2195. {
  2196. m_CurrentVShaderHandle = 0;
  2197. m_pCurrentVShader = NULL;
  2198. }
  2199. }
  2200. return hr;
  2201. }
  2202. HRESULT
  2203. RefDev::Dp2SetVertexShader(LPD3DHAL_DP2COMMAND pCmd)
  2204. {
  2205. HRESULT hr = S_OK;
  2206. LPD3DHAL_DP2VERTEXSHADER pVS =
  2207. (LPD3DHAL_DP2VERTEXSHADER)(pCmd + 1);
  2208. // Just set the last Vertex Shader in this array
  2209. DWORD handle = pVS[pCmd->wStateCount-1].dwHandle;
  2210. //
  2211. // Zero is a special handle that tells the driver to
  2212. // invalidate the currently set shader.
  2213. //
  2214. if( handle == 0 )
  2215. {
  2216. m_pCurrentVShader = NULL;
  2217. m_CurrentVShaderHandle = handle;
  2218. return hr;
  2219. }
  2220. if( RDVSD_ISLEGACY(handle) )
  2221. {
  2222. // Make it parse the FVF and build the VertexElement array
  2223. hr = m_FVFShader.m_Declaration.MakeVElementArray( handle );
  2224. if( FAILED( hr ) )
  2225. {
  2226. DPFERR( "MakeVElementArray failed" );
  2227. return hr;
  2228. }
  2229. m_pCurrentVShader = &m_FVFShader;
  2230. }
  2231. else
  2232. {
  2233. if( !m_VShaderHandleArray.IsValidIndex( handle ) ||
  2234. (m_VShaderHandleArray[handle].m_pShader == NULL) )
  2235. {
  2236. DPFERR( "Such a Vertex Shader has not been created" );
  2237. return E_INVALIDARG;
  2238. }
  2239. m_pCurrentVShader = m_VShaderHandleArray[handle].m_pShader;
  2240. // Save the tesselator stride computed at parsing time.
  2241. // This feature is only available when using a Declaration.
  2242. m_VStream[RDVSD_STREAMTESS].m_dwStride =
  2243. m_pCurrentVShader->m_Declaration.m_dwStreamTessStride;
  2244. }
  2245. if( m_pCurrentVShader->m_pCode )
  2246. {
  2247. hr = m_RefVM.SetActiveShaderCode( m_pCurrentVShader->m_pCode );
  2248. if( FAILED( hr ) )
  2249. {
  2250. DPFERR( "SetActiveShaderCode failed" );
  2251. return hr;
  2252. }
  2253. RDVConstantData* pConst =
  2254. m_pCurrentVShader->m_Declaration.m_pConstants;
  2255. while( pConst )
  2256. {
  2257. hr = m_RefVM.SetData( D3DSPR_CONST,
  2258. pConst->m_dwAddress,
  2259. pConst->m_dwCount,
  2260. pConst->m_pData );
  2261. if( FAILED( hr ) )
  2262. {
  2263. DPFERR( "SetVMData failed" );
  2264. return hr;
  2265. }
  2266. pConst = static_cast<RDVConstantData *>(pConst->m_pNext);
  2267. }
  2268. }
  2269. m_CurrentVShaderHandle = handle;
  2270. if( m_pCurrentVShader->m_Declaration.m_qwInputFVF != m_RefVP.m_qwFVFIn )
  2271. {
  2272. m_RefVP.m_dwDirtyFlags |= RDPV_DIRTY_COLORVTX;
  2273. }
  2274. m_RefVP.m_qwFVFIn = m_pCurrentVShader->m_Declaration.m_qwInputFVF;
  2275. return hr;
  2276. }
  2277. HRESULT
  2278. RefDev::Dp2SetVertexShaderConsts( DWORD StartReg, DWORD dwCount,
  2279. LPDWORD pData )
  2280. {
  2281. HRESULT hr = m_RefVM.SetData( D3DSPR_CONST, StartReg, dwCount, pData );
  2282. return hr;
  2283. }
  2284. HRESULT
  2285. RefDev::Dp2SetStreamSource(LPD3DHAL_DP2COMMAND pCmd)
  2286. {
  2287. HRESULT hr = S_OK;
  2288. LPD3DHAL_DP2SETSTREAMSOURCE pSSS =
  2289. (LPD3DHAL_DP2SETSTREAMSOURCE)(pCmd + 1);
  2290. for( int i = 0; i < pCmd->wStateCount; i++ )
  2291. {
  2292. RDVStream& Stream = m_VStream[pSSS[i].dwStream];
  2293. // NULL handle means that the StreamSource should be unset.
  2294. if( pSSS[i].dwVBHandle == 0 )
  2295. {
  2296. Stream.m_pData = NULL;
  2297. Stream.m_dwStride = 0;
  2298. Stream.m_dwHandle = 0;
  2299. }
  2300. else
  2301. {
  2302. // Check if the handle has a valid vertexbuffer
  2303. RDSurface* pSurf =
  2304. g_SurfMgr.GetSurfFromList(m_pDDLcl, pSSS[i].dwVBHandle);
  2305. if( (pSurf == NULL) ||
  2306. (pSurf->GetSurfaceType() != RR_ST_VERTEXBUFFER) )
  2307. {
  2308. DPFERR( "Invalid VB Handle passed in SetStreamSource" );
  2309. return E_INVALIDARG;
  2310. }
  2311. RDVertexBuffer* pVB = static_cast<RDVertexBuffer *>(pSurf);
  2312. Stream.m_pData = pVB->GetBits();
  2313. Stream.m_dwStride = pSSS[i].dwStride;
  2314. Stream.m_dwHandle = pSSS[i].dwVBHandle;
  2315. }
  2316. }
  2317. return hr;
  2318. }
  2319. HRESULT
  2320. RefDev::Dp2SetStreamSourceUM( LPD3DHAL_DP2COMMAND pCmd,
  2321. PUINT8 pUMVtx )
  2322. {
  2323. HRESULT hr = S_OK;
  2324. // Get new data by ignoring all but the last structure
  2325. D3DHAL_DP2SETSTREAMSOURCEUM* pSSUM =
  2326. (D3DHAL_DP2SETSTREAMSOURCEUM*)(pCmd + 1) + (pCmd->wStateCount - 1);
  2327. // Access only the Zero'th stream
  2328. m_VStream[pSSUM->dwStream].m_pData = pUMVtx;
  2329. m_VStream[pSSUM->dwStream].m_dwStride = pSSUM->dwStride;
  2330. m_VStream[pSSUM->dwStream].m_dwHandle = 0;
  2331. return hr;
  2332. }
  2333. HRESULT
  2334. RefDev::Dp2SetIndices(LPD3DHAL_DP2COMMAND pCmd)
  2335. {
  2336. HRESULT hr = S_OK;
  2337. // Get new data by ignoring all but the last structure
  2338. D3DHAL_DP2SETINDICES* pSI =
  2339. (D3DHAL_DP2SETINDICES*)(pCmd + 1) + (pCmd->wStateCount - 1);
  2340. // NULL handle means that the StreamSource should be unset.
  2341. if( pSI->dwVBHandle == 0 )
  2342. {
  2343. m_IndexStream.m_pData = NULL;
  2344. m_IndexStream.m_dwStride = 0;
  2345. m_IndexStream.m_dwHandle = 0;
  2346. }
  2347. else
  2348. {
  2349. // Check if the handle has a valid vertexbuffer
  2350. RDSurface* pSurf = g_SurfMgr.GetSurfFromList(m_pDDLcl,
  2351. pSI->dwVBHandle);
  2352. if( (pSurf == NULL) ||
  2353. (pSurf->GetSurfaceType() != RR_ST_VERTEXBUFFER) )
  2354. {
  2355. DPFERR( "Invalid VB Handle passed in SetIndices" );
  2356. return E_INVALIDARG;
  2357. }
  2358. RDVertexBuffer* pVB = static_cast<RDVertexBuffer *>(pSurf);
  2359. m_IndexStream.m_pData = pVB->GetBits();
  2360. m_IndexStream.m_dwStride = pSI->dwStride;
  2361. m_IndexStream.m_dwHandle = pSI->dwVBHandle;
  2362. }
  2363. return hr;
  2364. }
  2365. HRESULT
  2366. RefDev::Dp2DrawPrimitive(LPD3DHAL_DP2COMMAND pCmd)
  2367. {
  2368. HRESULT hr = S_OK;
  2369. //
  2370. // Validation
  2371. //
  2372. _ASSERT( m_CurrentVShaderHandle, "No vertex shader currently bound" );
  2373. LPD3DHAL_DP2DRAWPRIMITIVE pDP = (LPD3DHAL_DP2DRAWPRIMITIVE)(pCmd + 1);
  2374. for( int i = 0; i < pCmd->wStateCount; i++ )
  2375. {
  2376. if( FAILED( hr = DrawDX8Prim( &(pDP[i]) ) ) )
  2377. return hr;
  2378. }
  2379. return hr;
  2380. }
  2381. HRESULT
  2382. RefDev::Dp2DrawPrimitive2(LPD3DHAL_DP2COMMAND pCmd)
  2383. {
  2384. HRESULT hr = S_OK;
  2385. //
  2386. // Validation
  2387. //
  2388. _ASSERT( m_CurrentVShaderHandle, "No vertex shader currently bound" );
  2389. LPD3DHAL_DP2DRAWPRIMITIVE2 pDP = (LPD3DHAL_DP2DRAWPRIMITIVE2)(pCmd + 1);
  2390. for( int i = 0; i < pCmd->wStateCount; i++ )
  2391. {
  2392. if( FAILED( hr = DrawDX8Prim2( &(pDP[i]) ) ) )
  2393. return hr;
  2394. }
  2395. return hr;
  2396. }
  2397. HRESULT
  2398. RefDev::Dp2DrawIndexedPrimitive(LPD3DHAL_DP2COMMAND pCmd)
  2399. {
  2400. HRESULT hr = S_OK;
  2401. //
  2402. // Validation
  2403. //
  2404. _ASSERT( m_CurrentVShaderHandle, "No vertex shader currently bound" );
  2405. LPD3DHAL_DP2DRAWINDEXEDPRIMITIVE pDIP =
  2406. (LPD3DHAL_DP2DRAWINDEXEDPRIMITIVE)(pCmd + 1);
  2407. for( int i = 0; i < pCmd->wStateCount; i++ )
  2408. {
  2409. if( FAILED( hr = DrawDX8IndexedPrim( &(pDIP[i]) ) ) )
  2410. return hr;
  2411. }
  2412. return hr;
  2413. }
  2414. HRESULT
  2415. RefDev::Dp2DrawIndexedPrimitive2(LPD3DHAL_DP2COMMAND pCmd)
  2416. {
  2417. HRESULT hr = S_OK;
  2418. //
  2419. // Validation
  2420. //
  2421. _ASSERT( m_CurrentVShaderHandle, "No vertex shader currently bound" );
  2422. LPD3DHAL_DP2DRAWINDEXEDPRIMITIVE2 pDIP =
  2423. (LPD3DHAL_DP2DRAWINDEXEDPRIMITIVE2)(pCmd + 1);
  2424. for( int i = 0; i < pCmd->wStateCount; i++ )
  2425. {
  2426. if( FAILED( hr = DrawDX8IndexedPrim2( &(pDIP[i]) ) ) )
  2427. return hr;
  2428. }
  2429. return hr;
  2430. }
  2431. HRESULT
  2432. RefDev::Dp2DrawClippedTriFan(LPD3DHAL_DP2COMMAND pCmd)
  2433. {
  2434. HRESULT hr = S_OK;
  2435. //
  2436. // Validation
  2437. //
  2438. _ASSERT( m_CurrentVShaderHandle, "No vertex shader currently bound" );
  2439. LPD3DHAL_CLIPPEDTRIANGLEFAN pDIP =
  2440. (LPD3DHAL_CLIPPEDTRIANGLEFAN)(pCmd + 1);
  2441. for( int i = 0; i < pCmd->wStateCount; i++ )
  2442. {
  2443. if( FAILED( hr = DrawDX8ClippedTriFan( &(pDIP[i]) ) ) )
  2444. return hr;
  2445. }
  2446. return hr;
  2447. }
  2448. HRESULT
  2449. RefDev::Dp2SetPalette(LPD3DHAL_DP2COMMAND pCmd)
  2450. {
  2451. HRESULT hr = S_OK;
  2452. LPD3DHAL_DP2SETPALETTE pSP = (LPD3DHAL_DP2SETPALETTE)(pCmd + 1);
  2453. for( int i = 0; i < pCmd->wStateCount; i++ )
  2454. {
  2455. HR_RET( m_PaletteHandleArray.Grow( pSP->dwPaletteHandle ) );
  2456. if( m_PaletteHandleArray[pSP->dwPaletteHandle].m_pPal == NULL )
  2457. {
  2458. m_PaletteHandleArray[pSP->dwPaletteHandle].m_pPal = new RDPalette;
  2459. }
  2460. RDPalette* pPal = m_PaletteHandleArray[pSP->dwPaletteHandle].m_pPal;
  2461. pPal->m_dwFlags = (pSP->dwPaletteFlags & DDRAWIPAL_ALPHA) ?
  2462. RDPalette::RDPAL_ALPHAINPALETTE : 0;
  2463. RDSurface2D* pSurf = (RDSurface2D *)g_SurfMgr.GetSurfFromList(
  2464. m_pDDLcl, pSP->dwSurfaceHandle );
  2465. if( pSurf == NULL ) return E_FAIL;
  2466. if( (pSurf->GetSurfaceType() & RR_ST_TEXTURE) == 0 )
  2467. {
  2468. DPFERR( "Setting palette to a non-texture\n" );
  2469. return E_FAIL;
  2470. }
  2471. pSurf->SetPalette( pPal );
  2472. }
  2473. return hr;
  2474. }
  2475. HRESULT
  2476. RefDev::Dp2UpdatePalette(LPD3DHAL_DP2UPDATEPALETTE pUP, PALETTEENTRY* pPalData)
  2477. {
  2478. HRESULT hr = S_OK;
  2479. HR_RET( m_PaletteHandleArray.Grow( pUP->dwPaletteHandle ) );
  2480. if( m_PaletteHandleArray[pUP->dwPaletteHandle].m_pPal == NULL )
  2481. {
  2482. m_PaletteHandleArray[pUP->dwPaletteHandle].m_pPal = new RDPalette;
  2483. }
  2484. RDPalette* pPal = m_PaletteHandleArray[pUP->dwPaletteHandle].m_pPal;
  2485. HR_RET( pPal->Update( pUP->wStartIndex, pUP->wNumEntries, pPalData ) );
  2486. return hr;
  2487. }
  2488. HRESULT
  2489. RefDev::Dp2SetTexLod(LPD3DHAL_DP2COMMAND pCmd)
  2490. {
  2491. HRESULT hr = S_OK;
  2492. LPD3DHAL_DP2SETTEXLOD pSTL = (LPD3DHAL_DP2SETTEXLOD)(pCmd + 1);
  2493. for( int i = 0; i < pCmd->wStateCount; i++ )
  2494. {
  2495. RDSurface2D* pSurf = (RDSurface2D *)g_SurfMgr.GetSurfFromList(
  2496. m_pDDLcl, pSTL->dwDDSurface );
  2497. if( pSurf == NULL ) return E_FAIL;
  2498. if( (pSurf->GetSurfaceType() & RR_ST_TEXTURE) == 0 )
  2499. {
  2500. DPFERR( "Setting LOD to a non-texture\n" );
  2501. return E_FAIL;
  2502. }
  2503. HR_RET(pSurf->SetLod( pSTL->dwLOD ));
  2504. }
  2505. return hr;
  2506. }