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.

1271 lines
49 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: cbhal.cpp
  6. * Content: DrawPrimitive implementation for command buffer HALs
  7. *
  8. ***************************************************************************/
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. #include "drawprim.hpp"
  12. #include "clipfunc.h"
  13. #include "d3dfei.h"
  14. extern const DWORD LOWVERTICESNUMBER;
  15. extern "C" HRESULT WINAPI
  16. DDInternalLock( LPDDRAWI_DDRAWSURFACE_LCL this_lcl, LPVOID* lpBits );
  17. extern "C" FLATPTR GetAliasedVidMem( LPDDRAWI_DIRECTDRAW_LCL pdrv_lcl,
  18. LPDDRAWI_DDRAWSURFACE_LCL surf_lcl,
  19. FLATPTR fpVidMem );
  20. // Each vertex buffer is big enough to hold 256 TL vertices
  21. const DWORD CDirect3DDeviceIDP2::dwD3DDefaultVertexBatchSize = 256; // * 32 = 8K bytes
  22. // Command buffer size tuned to 16K to minimize flushes in Unreal
  23. const DWORD CDirect3DDeviceIDP2::dwD3DDefaultCommandBatchSize = 16384; // * 1 = 16K bytes
  24. inline void CDirect3DDeviceIDP2::ClearBatch()
  25. {
  26. // Reset command buffer
  27. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)lpvDP2Commands;
  28. dwDP2CommandLength = 0;
  29. dp2data.dwCommandOffset = 0;
  30. dp2data.dwCommandLength = 0;
  31. bDP2CurrCmdOP = 0;
  32. // Reset vertex buffer
  33. if (this->dwFlags & D3DPV_WITHINPRIMITIVE)
  34. {
  35. // Do not reset vertex buffer
  36. // or reset to the start of current primitives'
  37. // vertex data to prevent unecessary processing
  38. // of unused vertices by the driver.
  39. /*
  40. dp2data.dwVertexOffset = dwDP2CurrPrimVertexOffset;
  41. dp2data.dwVertexLength -= dwDP2CurrPrimVertexOffset;
  42. */
  43. }
  44. else
  45. {
  46. dp2data.dwVertexOffset = 0;
  47. dp2data.dwVertexLength = 0;
  48. dwVertexBase = 0;
  49. TLVbuf.Base() = 0;
  50. if (dp2data.dwFlags & D3DHALDP2_USERMEMVERTICES)
  51. {
  52. // We are flushing a user mem primitive.
  53. // We need to clear dp2data.lpUMVertices
  54. // since we are done with it. We replace
  55. // it with TLVbuf.
  56. DDASSERT(lpDP2CurrBatchVBI == NULL);
  57. dp2data.lpDDVertex = ((LPDDRAWI_DDRAWSURFACE_INT)TLVbuf.GetDDS())->lpLcl;
  58. lpDP2CurrBatchVBI = TLVbuf.GetVBI();
  59. lpDP2CurrBatchVBI->AddRef();
  60. dp2data.dwFlags &= ~D3DHALDP2_USERMEMVERTICES;
  61. }
  62. }
  63. }
  64. #undef DPF_MODNAME
  65. #define DPF_MODNAME "CDirect3DDeviceIDP2::CheckSurfaces()"
  66. HRESULT CDirect3DDeviceIDP2::CheckSurfaces()
  67. {
  68. HRESULT hr;
  69. if(this->lpDirect3DI->lpTextureManager->CheckIfLost())
  70. {
  71. D3D_ERR("Managed Textures lost");
  72. return DDERR_SURFACELOST;
  73. }
  74. if ( ((LPDDRAWI_DDRAWSURFACE_INT) this->lpDDSTarget)->lpLcl->lpGbl->dwUsageCount ||
  75. (this->lpDDSZBuffer && ((LPDDRAWI_DDRAWSURFACE_INT) this->lpDDSZBuffer)->lpLcl->lpGbl->dwUsageCount) )
  76. {
  77. D3D_ERR("Render target or Z buffer locked");
  78. return DDERR_SURFACEBUSY;
  79. }
  80. if ( ((LPDDRAWI_DDRAWSURFACE_INT) this->lpDDSTarget)->lpLcl->dwFlags & DDRAWISURF_INVALID )\
  81. {
  82. D3D_ERR("Render target buffer lost");
  83. return DDERR_SURFACELOST;
  84. }
  85. if ( this->lpDDSZBuffer && ( ((LPDDRAWI_DDRAWSURFACE_INT) this->lpDDSZBuffer)->lpLcl->dwFlags & DDRAWISURF_INVALID ) )
  86. {
  87. D3D_ERR("Z buffer lost");
  88. return DDERR_SURFACELOST;
  89. }
  90. if (!(this->dp2data.dwFlags & D3DHALDP2_USERMEMVERTICES) && (this->dp2data.lpDDVertex->dwFlags & DDRAWISURF_INVALID))
  91. {
  92. D3D_ERR("Vertex buffer lost");
  93. return DDERR_SURFACELOST;
  94. }
  95. if (this->dp2data.lpDDCommands->dwFlags & DDRAWISURF_INVALID)
  96. {
  97. D3D_ERR("Command buffer lost");
  98. return DDERR_SURFACELOST;
  99. }
  100. return D3D_OK;
  101. }
  102. #undef DPF_MODNAME
  103. #define DPF_MODNAME "CDirect3DDeviceIDP2::FlushStates(void)"
  104. HRESULT CDirect3DDeviceIDP2::FlushStates()
  105. {
  106. HRESULT dwRet=D3D_OK;
  107. FlushTextureFromDevice( this ); // delink all texture surfaces
  108. if (dwDP2CommandLength) // Do we have some instructions to flush ?
  109. {
  110. CLockD3DST lockObject(this, DPF_MODNAME, REMIND("")); // Takes D3D lock (ST only).
  111. // Check if render target and / or z buffer is lost
  112. if ((dwRet = CheckSurfaces()) != D3D_OK)
  113. { // If lost, we'll just chuck all this work into the bit bucket
  114. ClearBatch();
  115. if (dwRet == DDERR_SURFACELOST)
  116. {
  117. this->dwFEFlags |= D3DFE_LOSTSURFACES;
  118. dwRet = D3D_OK;
  119. }
  120. }
  121. else
  122. {
  123. // Save since it will get overwritten by ddrval after DDI call
  124. DWORD dwVertexSize = dp2data.dwVertexSize;
  125. dp2data.dwCommandLength = dwDP2CommandLength;
  126. //we clear this to break re-entering as SW rasterizer needs to lock DDRAWSURFACE
  127. dwDP2CommandLength = 0;
  128. // Try and set these 2 values only once during initialization
  129. dp2data.dwhContext = this->dwhContext;
  130. dp2data.lpdwRStates = this->rstates;
  131. DDASSERT(dp2data.dwVertexSize != 0);
  132. // If we need the same TLVbuf next time do not swap buffers.
  133. if (this->dwFlags & D3DPV_WITHINPRIMITIVE)
  134. {
  135. dp2data.dwFlags &= ~D3DHALDP2_SWAPVERTEXBUFFER;
  136. }
  137. D3D_INFO(6, "dwVertexType passed to the driver = 0x%08x", dp2data.dwVertexType);
  138. #ifndef WIN95
  139. if (!IS_DX7HAL_DEVICE(this))
  140. {
  141. if((dwRet = CheckContextSurface(this)) != D3D_OK)
  142. {
  143. ClearBatch();
  144. return dwRet;
  145. }
  146. }
  147. #else
  148. // Take Win 16 Lock here
  149. LOCK_HAL( dwRet, this );
  150. #endif //WIN95
  151. // Spin waiting on the driver if wait requested
  152. do {
  153. // Need to set this since the driver may have overwrote it by
  154. // setting ddrval = DDERR_WASSTILLDRAWING
  155. dp2data.dwVertexSize = dwVertexSize;
  156. CALL_HAL3ONLY_NOLOCK(dwRet, this, DrawPrimitives2, &dp2data);
  157. if (dwRet != DDHAL_DRIVER_HANDLED)
  158. {
  159. D3D_ERR ( "Driver not handled in DrawPrimitives2" );
  160. // Need sensible return value in this case,
  161. // currently we return whatever the driver stuck in here.
  162. }
  163. } while (dp2data.ddrval == DDERR_WASSTILLDRAWING);
  164. if (dp2data.ddrval == D3DERR_COMMAND_UNPARSED)
  165. { // This should never occur since the driver must understand
  166. // all the instruction we batch.
  167. D3D_ERR("Driver could not parse this batch!");
  168. dwRet = DDERR_GENERIC; // Some thing better here ?
  169. }
  170. else
  171. {
  172. dwRet= dp2data.ddrval;
  173. #ifdef WIN95
  174. // update command buffer pointer
  175. if ((dwRet == D3D_OK) && (dp2data.dwFlags & D3DHALDP2_SWAPCOMMANDBUFFER))
  176. {
  177. // Get Aliased vid mem pointer if it is a vid mem surf.
  178. if (dp2data.dwFlags & D3DHALDP2_VIDMEMCOMMANDBUF)
  179. {
  180. D3D_INFO(7, "Got back new vid mem command buffer");
  181. FLATPTR paliasbits = GetAliasedVidMem( dp2data.lpDDCommands->lpSurfMore->lpDD_lcl,
  182. dp2data.lpDDCommands, (FLATPTR) dp2data.lpDDCommands->lpGbl->fpVidMem );
  183. if (paliasbits == NULL)
  184. {
  185. DPF_ERR("Could not get Aliased pointer for vid mem command buffer");
  186. // Since we can't use this pointer, set it's size to 0
  187. // That way next time around we will try and allocate a new one
  188. dp2data.lpDDCommands->lpGbl->dwLinearSize = 0;
  189. }
  190. lpvDP2Commands = (LPVOID)paliasbits;
  191. }
  192. else
  193. {
  194. D3D_INFO(7, "Got back new sys mem command buffer");
  195. lpvDP2Commands = (LPVOID)dp2data.lpDDCommands->lpGbl->fpVidMem;
  196. }
  197. dwDP2CommandBufSize = dp2data.lpDDCommands->lpGbl->dwLinearSize;
  198. }
  199. // update vertex buffer pointer
  200. if ((dwRet == D3D_OK) && (dp2data.dwFlags & D3DHALDP2_SWAPVERTEXBUFFER))
  201. {
  202. if (dp2data.dwFlags & D3DHALDP2_VIDMEMVERTEXBUF)
  203. {
  204. D3D_INFO(7, "Got back new vid mem vertex buffer");
  205. FLATPTR paliasbits = GetAliasedVidMem( dp2data.lpDDVertex->lpSurfMore->lpDD_lcl,
  206. dp2data.lpDDVertex, (FLATPTR) dp2data.lpDDVertex->lpGbl->fpVidMem );
  207. if (paliasbits == NULL)
  208. {
  209. DPF_ERR("Could not get Aliased pointer for vid mem vertex buffer");
  210. // Since we can't use this pointer, set it's size to 0
  211. // That way next time around we will try and allocate a new one
  212. dp2data.lpDDVertex->lpGbl->dwLinearSize = 0;
  213. }
  214. TLVbuf.alignedBuf = (LPVOID)paliasbits;
  215. }
  216. else
  217. {
  218. D3D_INFO(7, "Got back new sys mem vertex buffer");
  219. TLVbuf.alignedBuf = (LPVOID)dp2data.lpDDVertex->lpGbl->fpVidMem;
  220. }
  221. TLVbuf.size = dp2data.lpDDVertex->lpGbl->dwLinearSize;
  222. }
  223. #endif
  224. }
  225. #ifdef WIN95
  226. // Release Win16 Lock here
  227. UNLOCK_HAL( this );
  228. #endif
  229. // Restore to value before the DDI call
  230. dp2data.dwVertexSize = dwVertexSize;
  231. ClearBatch();
  232. }
  233. }
  234. // There are situations when the command stream has no data, but there is data in
  235. // the vertex pool. This could happen, for instance if every triangle got rejected
  236. // while clipping. In this case we still need to "Flush out" the vertex data.
  237. else if (dp2data.dwCommandLength == 0)
  238. {
  239. ClearBatch();
  240. }
  241. return dwRet;
  242. }
  243. #undef DPF_MODNAME
  244. #define DPF_MODNAME "CDirect3DDeviceIDP2::FlushStates(DWORD)"
  245. HRESULT CDirect3DDeviceIDP2::FlushStates(DWORD dwReqSize)
  246. {
  247. // Request the driver to grow the command buffer upon flush
  248. dp2data.dwReqVertexBufSize = dwReqSize;
  249. dp2data.dwFlags |= D3DHALDP2_SWAPVERTEXBUFFER | D3DHALDP2_REQVERTEXBUFSIZE;
  250. HRESULT ret = FlushStates();
  251. dp2data.dwFlags &= ~D3DHALDP2_REQVERTEXBUFSIZE;
  252. return ret;
  253. }
  254. #undef DPF_MODNAME
  255. #define DPF_MODNAME "CDirect3DDeviceIDP2::GrowCommandBuffer"
  256. // Check and grow command buffer
  257. HRESULT CDirect3DDeviceIDP2::GrowCommandBuffer(LPDIRECT3DI lpD3DI, DWORD dwSize)
  258. {
  259. HRESULT ret;
  260. if (dwSize > dwDP2CommandBufSize)
  261. {
  262. if (lpDDSCB1)
  263. lpDDSCB1->Release();
  264. // Create command buffer through DirectDraw
  265. DDSURFACEDESC2 ddsd;
  266. memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
  267. ddsd.dwSize = sizeof(DDSURFACEDESC2);
  268. ddsd.dwFlags = DDSD_WIDTH | DDSD_CAPS;
  269. ddsd.dwWidth = dwSize;
  270. ddsd.ddsCaps.dwCaps = DDSCAPS_EXECUTEBUFFER;
  271. if (IS_HW_DEVICE(this))
  272. ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
  273. else
  274. ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
  275. ddsd.ddsCaps.dwCaps2 = DDSCAPS2_COMMANDBUFFER;
  276. // Try explicit video memory first
  277. D3D_INFO(7, "Trying to create a vid mem command buffer");
  278. ret = lpD3DI->lpDD4->CreateSurface(&ddsd, &lpDDSCB1, NULL);
  279. if (ret != DD_OK)
  280. {
  281. // If that failed, try explicit system memory
  282. ddsd.ddsCaps.dwCaps &= ~DDSCAPS_VIDEOMEMORY;
  283. ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
  284. D3D_INFO(7, "Trying to create a sys mem command buffer");
  285. ret = lpD3DI->lpDD4->CreateSurface(&ddsd, &lpDDSCB1, NULL);
  286. if (ret != DD_OK)
  287. {
  288. D3D_ERR("failed to allocate Command Buffer 1");
  289. return ret;
  290. }
  291. }
  292. // Lock command buffer
  293. ret = lpDDSCB1->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_NOSYSLOCK, NULL);
  294. if (ret != DD_OK)
  295. {
  296. D3D_ERR("Could not lock command buffer.");
  297. return ret;
  298. }
  299. // update command buffer pointer
  300. lpvDP2Commands = ddsd.lpSurface;
  301. dp2data.lpDDCommands = ((LPDDRAWI_DDRAWSURFACE_INT)lpDDSCB1)->lpLcl;
  302. dwDP2CommandBufSize = dwSize;
  303. }
  304. return D3D_OK;
  305. }
  306. #undef DPF_MODNAME
  307. #define DPF_MODNAME "CDirect3DDeviceIDP2::Init"
  308. HRESULT CDirect3DDeviceIDP2::Init(REFCLSID riid, LPDIRECT3DI lpD3DI, LPDIRECTDRAWSURFACE lpDDS,
  309. IUnknown* pUnkOuter, LPUNKNOWN* lplpD3DDevice, DWORD dwVersion)
  310. {
  311. dwDP2CommandBufSize = 0;
  312. dwDP2Flags =0;
  313. lpDDSCB1 = NULL;
  314. lpvDP2Commands = NULL;
  315. // We do this early in case of DP2 since GrowCommandBuffer depends on this check
  316. if (IsEqualIID(riid, IID_IDirect3DHALDevice))
  317. {
  318. this->dwFEFlags |= D3DFE_REALHAL;
  319. }
  320. HRESULT ret = GrowCommandBuffer(lpD3DI, dwD3DDefaultCommandBatchSize);
  321. if (ret != D3D_OK)
  322. return ret;
  323. // Fill the dp2data structure with initial values
  324. dp2data.dwFlags = D3DHALDP2_SWAPCOMMANDBUFFER;
  325. dp2data.dwVertexType = D3DFVF_TLVERTEX; // Initial assumption
  326. dp2data.dwVertexSize = sizeof(D3DTLVERTEX); // Initial assumption
  327. ClearBatch();
  328. // Initialize the DDI independent part of the device
  329. ret = DIRECT3DDEVICEI::Init(riid, lpD3DI, lpDDS, pUnkOuter, lplpD3DDevice, dwVersion);
  330. if (ret != D3D_OK)
  331. {
  332. return ret;
  333. }
  334. lpDP2CurrBatchVBI = TLVbuf.GetVBI();
  335. lpDP2CurrBatchVBI->AddRef();
  336. dp2data.lpDDVertex = ((LPDDRAWI_DDRAWSURFACE_INT)(lpDP2CurrBatchVBI->GetDDS()))->lpLcl;
  337. return ret;
  338. }
  339. #undef DPF_MODNAME
  340. #define DPF_MODNAME "CDirect3DDeviceIDP2::~CDirect3DDeviceIDP2"
  341. CDirect3DDeviceIDP2::~CDirect3DDeviceIDP2()
  342. {
  343. DestroyDevice();
  344. if (lpDDSCB1)
  345. lpDDSCB1->Release();
  346. if (lpDP2CurrBatchVBI)
  347. lpDP2CurrBatchVBI->Release();
  348. }
  349. #undef DPF_MODNAME
  350. #define DPF_MODNAME "CDirect3DDeviceIDP2::SetRenderStateI"
  351. HRESULT D3DAPI CDirect3DDeviceIDP2::SetRenderStateI(D3DRENDERSTATETYPE dwStateType,
  352. DWORD value)
  353. {
  354. HRESULT ret = D3D_OK;
  355. if (bDP2CurrCmdOP == D3DDP2OP_RENDERSTATE)
  356. { // Last instruction is a renderstate, append this one to it
  357. if (dwDP2CommandLength + sizeof(D3DHAL_DP2RENDERSTATE) <= dwDP2CommandBufSize)
  358. {
  359. LPD3DHAL_DP2RENDERSTATE lpRState = (LPD3DHAL_DP2RENDERSTATE)((LPBYTE)lpvDP2Commands +
  360. dwDP2CommandLength + dp2data.dwCommandOffset);
  361. lpDP2CurrCommand->wStateCount = ++wDP2CurrCmdCnt;
  362. lpRState->RenderState = dwStateType;
  363. lpRState->dwState = value;
  364. dwDP2CommandLength += sizeof(D3DHAL_DP2RENDERSTATE);
  365. D3D_INFO(6, "Modify Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  366. return ret;
  367. }
  368. }
  369. // Check for space
  370. if (dwDP2CommandLength + sizeof(D3DHAL_DP2COMMAND) +
  371. sizeof(D3DHAL_DP2RENDERSTATE) > dwDP2CommandBufSize)
  372. {
  373. ret = FlushStates();
  374. // Since we ran out of space, we were not able to put (dwStateType, value)
  375. // into the batch so rstates will reflect only the last batched
  376. // renderstate (since the driver updates rstates from the batch).
  377. // To fix this, we simply put the current (dwStateType, value) into rstates.
  378. this->rstates[dwStateType]=value;
  379. if (ret != D3D_OK)
  380. {
  381. D3D_ERR("Error trying to render batched commands in SetRenderStateI");
  382. return ret;
  383. }
  384. }
  385. // Add new renderstate instruction
  386. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  387. dwDP2CommandLength + dp2data.dwCommandOffset);
  388. lpDP2CurrCommand->bCommand = D3DDP2OP_RENDERSTATE;
  389. bDP2CurrCmdOP = D3DDP2OP_RENDERSTATE;
  390. lpDP2CurrCommand->bReserved = 0;
  391. lpDP2CurrCommand->wStateCount = 1;
  392. wDP2CurrCmdCnt = 1;
  393. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  394. // Add renderstate data
  395. LPD3DHAL_DP2RENDERSTATE lpRState = (LPD3DHAL_DP2RENDERSTATE)(lpDP2CurrCommand + 1);
  396. lpRState->RenderState = dwStateType;
  397. lpRState->dwState = value;
  398. dwDP2CommandLength += sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2RENDERSTATE);
  399. return ret;
  400. }
  401. // Map D3DPRIMITIVETYPE to D3DHAL_DP2OPERATION
  402. const iprim2cmdop[] = {
  403. 0, // Invalid
  404. 0, // Points are invalid too
  405. D3DDP2OP_INDEXEDLINELIST2,
  406. D3DDP2OP_INDEXEDLINESTRIP,
  407. D3DDP2OP_INDEXEDTRIANGLELIST2,
  408. D3DDP2OP_INDEXEDTRIANGLESTRIP,
  409. D3DDP2OP_INDEXEDTRIANGLEFAN
  410. };
  411. // Map D3DPRIMITIVETYPE to D3DHAL_DP2OPERATION (Execute buffer case)
  412. // Only triangle lists and line lists are valid
  413. const iprim2cmdopEx[] = {
  414. 0,
  415. 0,
  416. D3DDP2OP_INDEXEDLINELIST,
  417. 0,
  418. D3DDP2OP_INDEXEDTRIANGLELIST,
  419. 0,
  420. 0
  421. };
  422. #undef DPF_MODNAME
  423. #define DPF_MODNAME "CDirect3DDeviceIDP2::DrawIndexPrim"
  424. //---------------------------------------------------------------------
  425. //
  426. // The vertices are already in the vertex buffer.
  427. //
  428. HRESULT CDirect3DDeviceIDP2::DrawIndexPrim()
  429. {
  430. HRESULT ret = D3D_OK;
  431. DWORD dwByteCount; // Command length plus indices
  432. DWORD dwIndicesByteCount; // Indices only
  433. if(this->dwFEFlags & D3DFE_NEED_TEXTURE_UPDATE)
  434. {
  435. UpdateTextures();
  436. this->dwFEFlags &= ~D3DFE_NEED_TEXTURE_UPDATE;
  437. }
  438. if (this->dwFlags & D3DPV_INSIDEEXECUTE)
  439. {
  440. if (this->primType == D3DPT_TRIANGLELIST)
  441. // Edge flags WORD presents in every triangle
  442. dwIndicesByteCount = sizeof(WORD) * this->dwNumPrimitives * 4;
  443. else
  444. // This is Line List
  445. dwIndicesByteCount = sizeof(WORD) * this->dwNumIndices;
  446. dwByteCount = dwIndicesByteCount + sizeof(D3DHAL_DP2COMMAND);
  447. }
  448. else
  449. {
  450. dwIndicesByteCount = sizeof(WORD) * this->dwNumIndices;
  451. dwByteCount = dwIndicesByteCount + sizeof(D3DHAL_DP2COMMAND) +
  452. sizeof(D3DHAL_DP2STARTVERTEX);
  453. }
  454. if (dwDP2CommandLength + dwByteCount > dwDP2CommandBufSize)
  455. {
  456. // Request the driver to grow the command buffer upon flush
  457. dp2data.dwReqCommandBufSize = dwByteCount;
  458. dp2data.dwFlags |= D3DHALDP2_REQCOMMANDBUFSIZE;
  459. ret = FlushStates();
  460. dp2data.dwFlags &= ~D3DHALDP2_REQCOMMANDBUFSIZE;
  461. if (ret != D3D_OK)
  462. return ret;
  463. // Check if the driver did give us what we need or do it ourselves
  464. ret = GrowCommandBuffer(this->lpDirect3DI, dwByteCount);
  465. if (ret != D3D_OK)
  466. {
  467. D3D_ERR("Could not grow Command Buffer");
  468. return ret;
  469. }
  470. }
  471. // Insert indexed primitive instruction
  472. LPD3DHAL_DP2COMMAND lpDP2CurrCommand;
  473. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  474. dwDP2CommandLength + dp2data.dwCommandOffset);
  475. lpDP2CurrCommand->bReserved = 0;
  476. lpDP2CurrCommand->wPrimitiveCount = (WORD)this->dwNumPrimitives;
  477. LPBYTE pIndices = (BYTE*)(lpDP2CurrCommand + 1); // Place for indices
  478. if (!(this->dwFlags & D3DPV_INSIDEEXECUTE))
  479. {
  480. lpDP2CurrCommand->bCommand = (BYTE)iprim2cmdop[this->primType];
  481. ((LPD3DHAL_DP2STARTVERTEX)(lpDP2CurrCommand+1))->wVStart =
  482. (WORD)this->dwVertexBase;
  483. pIndices += sizeof(D3DHAL_DP2STARTVERTEX);
  484. }
  485. else
  486. {
  487. // If we are inside Execute, the indexed triangle and line lists
  488. // do not have wVStart inside the command
  489. lpDP2CurrCommand->bCommand = (BYTE)iprim2cmdopEx[this->primType];
  490. }
  491. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  492. D3D_INFO(6, "Vertex Base: %08lx", this->dwVertexBase);
  493. #if DBG
  494. if (lpDP2CurrCommand->bCommand == 0)
  495. {
  496. D3D_ERR("Illegal primitive type");
  497. return DDERR_GENERIC;
  498. }
  499. #endif
  500. bDP2CurrCmdOP = lpDP2CurrCommand->bCommand;
  501. memcpy(pIndices, this->lpwIndices, dwIndicesByteCount);
  502. wDP2CurrCmdCnt = lpDP2CurrCommand->wPrimitiveCount;
  503. dwDP2CommandLength += dwByteCount;
  504. // We have batched some stuff, so we could be within a primitive
  505. // Unless the higher functions clear this flag we need to assume
  506. // we are mid-primitive during our flushes.
  507. this->dwFlags |= D3DPV_WITHINPRIMITIVE;
  508. return ret;
  509. }
  510. // Map D3DPRIMITIVETYPE to D3DHAL_DP2OPERATION
  511. const prim2cmdop[] = {
  512. 0, // Invalid
  513. D3DDP2OP_POINTS,
  514. D3DDP2OP_LINELIST,
  515. D3DDP2OP_LINESTRIP,
  516. D3DDP2OP_TRIANGLELIST,
  517. D3DDP2OP_TRIANGLESTRIP,
  518. D3DDP2OP_TRIANGLEFAN
  519. };
  520. // Map D3DPRIMITIVETYPE to bytes needed in command stream
  521. const prim2cmdsz[] = {
  522. 0, // Invalid
  523. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2POINTS),
  524. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2LINELIST),
  525. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2LINESTRIP),
  526. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TRIANGLELIST),
  527. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TRIANGLESTRIP),
  528. sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TRIANGLEFAN)
  529. };
  530. #undef DPF_MODNAME
  531. #define DPF_MODNAME "CDirect3DDeviceIDP2::DrawPrim"
  532. HRESULT CDirect3DDeviceIDP2::DrawPrim()
  533. {
  534. HRESULT ret = D3D_OK;
  535. DWORD dwVertexPoolSize = this->dwNumVertices * this->dwOutputSize;
  536. if(this->dwFEFlags & D3DFE_NEED_TEXTURE_UPDATE)
  537. {
  538. UpdateTextures();
  539. this->dwFEFlags &= ~D3DFE_NEED_TEXTURE_UPDATE;
  540. }
  541. // This primitive is generated by the clipper.
  542. // The vertices of this primitive are pointed to by the
  543. // lpvOut member, which need to be copied into the
  544. // command stream immediately after the command itself.
  545. if (this->dwFlags & D3DPV_CLIPPERPRIM)
  546. {
  547. DWORD dwExtra = 0;
  548. LPVOID lpvVerticesImm; // Place for vertices
  549. if (this->primType == D3DPT_TRIANGLEFAN)
  550. {
  551. if (rstates[D3DRENDERSTATE_FILLMODE] == D3DFILL_WIREFRAME &&
  552. this->dwFlags & D3DPV_NONCLIPPED)
  553. {
  554. // For unclipped (but pretended to be clipped) tri fans in
  555. // wireframe mode we generate 3-vertex tri fans to enable drawing of
  556. // interior edges
  557. BYTE vertices[__MAX_VERTEX_SIZE*3];
  558. BYTE *pV1 = vertices + this->dwOutputSize;
  559. BYTE *pV2 = pV1 + this->dwOutputSize;
  560. BYTE *pInput = (BYTE*)this->lpvOut;
  561. memcpy(vertices, pInput, this->dwOutputSize);
  562. pInput += this->dwOutputSize;
  563. const DWORD nTriangles = this->dwNumVertices - 2;
  564. this->dwNumVertices = 3;
  565. this->dwNumPrimitives = 1;
  566. this->lpvOut = vertices;
  567. this->dwFlags &= ~D3DPV_NONCLIPPED; // Remove this flag for recursive call
  568. for (DWORD i = nTriangles; i; i--)
  569. {
  570. memcpy(pV1, pInput, this->dwOutputSize);
  571. memcpy(pV2, pInput+this->dwOutputSize, this->dwOutputSize);
  572. pInput += this->dwOutputSize;
  573. // To enable all edge flag we set the fill mode to SOLID.
  574. // This will prevent checking the clip flags in the clipper state.
  575. rstates[D3DRENDERSTATE_FILLMODE] = D3DFILL_SOLID;
  576. ret = DrawPrim();
  577. rstates[D3DRENDERSTATE_FILLMODE] = D3DFILL_WIREFRAME;
  578. if (ret != D3D_OK)
  579. return ret;
  580. }
  581. return D3D_OK;
  582. }
  583. dwExtra = sizeof(D3DHAL_DP2TRIANGLEFAN_IMM);
  584. }
  585. DWORD dwPad = (sizeof(D3DHAL_DP2COMMAND) + dwDP2CommandLength + dwExtra) & 3;
  586. DWORD dwByteCount = sizeof(D3DHAL_DP2COMMAND) + dwPad + dwExtra + dwVertexPoolSize;
  587. // Check for space in the command buffer for commands & vertices
  588. if (dwDP2CommandLength + dwByteCount > dwDP2CommandBufSize)
  589. {
  590. // Flush the current batch but hold on to the vertices
  591. ret = FlushStates();
  592. if (ret != D3D_OK)
  593. return ret;
  594. if (dwByteCount > dwDP2CommandBufSize)
  595. {
  596. ret = GrowCommandBuffer(this->lpDirect3DI, dwByteCount);
  597. if (ret != D3D_OK)
  598. {
  599. D3D_ERR("Could not grow Command Buffer");
  600. return ret;
  601. }
  602. }
  603. dwPad = (sizeof(D3DHAL_DP2COMMAND) + dwExtra) & 3;
  604. dwByteCount = sizeof(D3DHAL_DP2COMMAND) + dwExtra + dwPad + dwVertexPoolSize;
  605. }
  606. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  607. dwDP2CommandLength + dp2data.dwCommandOffset);
  608. lpDP2CurrCommand->wPrimitiveCount = (WORD)this->dwNumPrimitives;
  609. lpDP2CurrCommand->bReserved = 0;
  610. if (this->primType == D3DPT_TRIANGLEFAN)
  611. {
  612. // Insert inline instruction and vertices
  613. bDP2CurrCmdOP = D3DDP2OP_TRIANGLEFAN_IMM;
  614. lpDP2CurrCommand->bCommand = bDP2CurrCmdOP;
  615. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  616. LPD3DHAL_DP2TRIANGLEFAN_IMM lpTriFanImm = (LPD3DHAL_DP2TRIANGLEFAN_IMM)(lpDP2CurrCommand + 1);
  617. if (rstates[D3DRENDERSTATE_FILLMODE] == D3DFILL_WIREFRAME)
  618. {
  619. lpTriFanImm->dwEdgeFlags = 0;
  620. ClipVertex **clip = this->ClipperState.current_vbuf;
  621. // Look at the exterior edges and mark the visible ones
  622. for(DWORD i = 0; i < this->dwNumVertices; ++i)
  623. {
  624. if (clip[i]->clip & CLIPPED_ENABLE)
  625. lpTriFanImm->dwEdgeFlags |= (1 << i);
  626. }
  627. }
  628. else
  629. {
  630. // Mark all exterior edges visible
  631. lpTriFanImm->dwEdgeFlags = 0xFFFFFFFF;
  632. }
  633. lpvVerticesImm = (LPBYTE)(lpTriFanImm + 1) + dwPad;
  634. }
  635. else
  636. {
  637. // Insert inline instruction and vertices
  638. bDP2CurrCmdOP = D3DDP2OP_LINELIST_IMM;
  639. lpDP2CurrCommand->bCommand = bDP2CurrCmdOP;
  640. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  641. lpvVerticesImm = (LPBYTE)(lpDP2CurrCommand + 1) + dwPad;
  642. }
  643. memcpy(lpvVerticesImm, this->lpvOut, dwVertexPoolSize);
  644. dwDP2CommandLength += dwByteCount;
  645. }
  646. else
  647. {
  648. // Check for space in the command buffer for new command.
  649. // The vertices are already in the vertex buffer.
  650. if (dwDP2CommandLength + prim2cmdsz[this->primType] > dwDP2CommandBufSize)
  651. {
  652. ret = FlushStates();
  653. if (ret != D3D_OK)
  654. return ret;
  655. }
  656. // Insert non indexed primitive instruction
  657. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  658. dwDP2CommandLength + dp2data.dwCommandOffset);
  659. bDP2CurrCmdOP = (BYTE)prim2cmdop[this->primType];
  660. lpDP2CurrCommand->bCommand = bDP2CurrCmdOP;
  661. lpDP2CurrCommand->bReserved = 0;
  662. switch(bDP2CurrCmdOP)
  663. {
  664. case D3DDP2OP_POINTS:
  665. {
  666. lpDP2CurrCommand->wPrimitiveCount = 1;
  667. LPD3DHAL_DP2POINTS lpPoints = (LPD3DHAL_DP2POINTS)(lpDP2CurrCommand + 1);
  668. lpPoints->wCount = (WORD)this->dwNumVertices;
  669. lpPoints->wVStart = (WORD)this->dwVertexBase;
  670. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  671. D3D_INFO(6, "Write Data:%08lx", *(LPDWORD)lpPoints);
  672. }
  673. break;
  674. case D3DDP2OP_LINELIST:
  675. case D3DDP2OP_TRIANGLELIST:
  676. {
  677. lpDP2CurrCommand->wPrimitiveCount = (WORD)this->dwNumPrimitives;
  678. // Linelist and trianglelist are identical
  679. LPD3DHAL_DP2LINELIST lpLines = (LPD3DHAL_DP2LINELIST)(lpDP2CurrCommand + 1);
  680. lpLines->wVStart = (WORD)this->dwVertexBase;
  681. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  682. D3D_INFO(6, "Write Data:%08lx", (DWORD)lpLines->wVStart);
  683. }
  684. break;
  685. default: // strips or fans
  686. {
  687. lpDP2CurrCommand->wPrimitiveCount = (WORD)this->dwNumPrimitives;
  688. // Linestrip, trianglestrip and trianglefan are identical
  689. LPD3DHAL_DP2LINESTRIP lpStrip = (LPD3DHAL_DP2LINESTRIP)(lpDP2CurrCommand + 1);
  690. lpStrip->wVStart = (WORD)this->dwVertexBase;
  691. D3D_INFO(6, "Write Ins :%08lx @ %08lx", *(LPDWORD)lpDP2CurrCommand,lpDP2CurrCommand);
  692. D3D_INFO(6, "Write Data:%08lx", (DWORD)lpStrip->wVStart);
  693. }
  694. }
  695. wDP2CurrCmdCnt = lpDP2CurrCommand->wPrimitiveCount;
  696. dwDP2CommandLength += prim2cmdsz[this->primType];
  697. }
  698. // We have batched some stuff, so we could be within a primitive
  699. // Unless the higher functions clear this flag we need to assume
  700. // we are mid-primitive during our flushes.
  701. this->dwFlags |= D3DPV_WITHINPRIMITIVE;
  702. return ret;
  703. }
  704. #undef DPF_MODNAME
  705. #define DPF_MODNAME "CDirect3DDeviceIDP2::SetTextureStageState"
  706. HRESULT D3DAPI
  707. CDirect3DDeviceIDP2::SetTextureStageState(DWORD dwStage,
  708. D3DTEXTURESTAGESTATETYPE dwState,
  709. DWORD dwValue)
  710. {
  711. // Holds D3D lock until exit.
  712. CLockD3DMT ldmLock(this, DPF_MODNAME, REMIND(""));
  713. #if DBG
  714. if (dwStage >= D3DHAL_TSS_MAXSTAGES ||
  715. dwState == 0 || dwState >= D3DTSS_MAX)
  716. {
  717. D3D_ERR( "Invalid texture stage or state index" );
  718. return DDERR_INVALIDPARAMS;
  719. }
  720. #endif //DBG
  721. HRESULT hr;
  722. if (this->tsstates[dwStage][dwState] == dwValue)
  723. {
  724. D3D_WARN(4,"Ignoring redundant SetTextureStageState");
  725. return D3D_OK;
  726. }
  727. // Update runtime copy of state.
  728. DWORD dwOldValue = tsstates[dwStage][dwState];
  729. tsstates[dwStage][dwState] = dwValue;
  730. if (dwState == D3DTSS_TEXCOORDINDEX && TextureStageEnabled(this, dwStage) ||
  731. dwState == D3DTSS_COLOROP &&
  732. ((dwValue == D3DTOP_DISABLE) == !(dwOldValue == D3DTOP_DISABLE)))
  733. {
  734. this->dwFVFLastIn = 0; // Force to recompute output VID
  735. this->dwFEFlags |= D3DFE_TSSINDEX_DIRTY;
  736. }
  737. if (dwStage >= dwMaxTextureBlendStages) return D3D_OK; // Ignore higher stage states
  738. hr = SetTSSI(dwStage, dwState, dwValue);
  739. return hr;
  740. }
  741. #undef DPF_MODNAME
  742. #define DPF_MODNAME "CDirect3DDeviceIDP2::SetTSSI"
  743. HRESULT CDirect3DDeviceIDP2::SetTSSI(DWORD dwStage, D3DTEXTURESTAGESTATETYPE dwState, DWORD dwValue)
  744. {
  745. HRESULT ret = D3D_OK;
  746. if (bDP2CurrCmdOP == D3DDP2OP_TEXTURESTAGESTATE)
  747. { // Last instruction is a texture stage state, append this one to it
  748. if (dwDP2CommandLength + sizeof(D3DHAL_DP2TEXTURESTAGESTATE) <= dwDP2CommandBufSize)
  749. {
  750. LPD3DHAL_DP2TEXTURESTAGESTATE lpRState = (LPD3DHAL_DP2TEXTURESTAGESTATE)((LPBYTE)lpvDP2Commands +
  751. dwDP2CommandLength + dp2data.dwCommandOffset);
  752. lpDP2CurrCommand->wStateCount = ++wDP2CurrCmdCnt;
  753. lpRState->wStage = (WORD)dwStage;
  754. lpRState->TSState = (WORD)dwState;
  755. lpRState->dwValue = dwValue;
  756. dwDP2CommandLength += sizeof(D3DHAL_DP2TEXTURESTAGESTATE);
  757. D3D_INFO(6, "Modify Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  758. return ret;
  759. }
  760. }
  761. // Check for space
  762. if (dwDP2CommandLength + sizeof(D3DHAL_DP2COMMAND) +
  763. sizeof(D3DHAL_DP2TEXTURESTAGESTATE) > dwDP2CommandBufSize)
  764. {
  765. ret = FlushStates();
  766. if (ret != D3D_OK)
  767. {
  768. D3D_ERR("Error trying to render batched commands in SetTSSI");
  769. return ret;
  770. }
  771. }
  772. // Add new renderstate instruction
  773. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  774. dwDP2CommandLength + dp2data.dwCommandOffset);
  775. lpDP2CurrCommand->bCommand = D3DDP2OP_TEXTURESTAGESTATE;
  776. bDP2CurrCmdOP = D3DDP2OP_TEXTURESTAGESTATE;
  777. lpDP2CurrCommand->bReserved = 0;
  778. lpDP2CurrCommand->wStateCount = 1;
  779. wDP2CurrCmdCnt = 1;
  780. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  781. // Add renderstate data
  782. LPD3DHAL_DP2TEXTURESTAGESTATE lpRState = (LPD3DHAL_DP2TEXTURESTAGESTATE)(lpDP2CurrCommand + 1);
  783. lpRState->wStage = (WORD)dwStage;
  784. lpRState->TSState = (WORD)dwState;
  785. lpRState->dwValue = dwValue;
  786. dwDP2CommandLength += sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2TEXTURESTAGESTATE);
  787. return ret;
  788. }
  789. #undef DPF_MODNAME
  790. #define DPF_MODNAME "CDirect3DDeviceIDP2::ValidateDevice"
  791. HRESULT D3DAPI
  792. CDirect3DDeviceIDP2::ValidateDevice(LPDWORD lpdwNumPasses)
  793. {
  794. // Holds D3D lock until exit.
  795. CLockD3DMT ldmLock(this, DPF_MODNAME, REMIND(""));
  796. HRESULT ret;
  797. D3DHAL_VALIDATETEXTURESTAGESTATEDATA vbod;
  798. TRY
  799. {
  800. if (!VALID_DIRECT3DDEVICE3_PTR(this))
  801. {
  802. D3D_ERR( "Invalid Direct3DDevice3 pointer" );
  803. return DDERR_INVALIDOBJECT;
  804. }
  805. if (!VALID_PTR(lpdwNumPasses, sizeof(DWORD)))
  806. {
  807. D3D_ERR( "Invalid lpdwNumPasses pointer" );
  808. return DDERR_INVALIDPARAMS;
  809. }
  810. }
  811. EXCEPT( EXCEPTION_EXECUTE_HANDLER )
  812. {
  813. D3D_ERR( "Exception encountered validating parameters" );
  814. return DDERR_INVALIDPARAMS;
  815. }
  816. // First, Update textures since drivers pass /fail this call based
  817. // on the current texture handles
  818. ret = UpdateTextures();
  819. if (ret != D3D_OK)
  820. {
  821. D3D_ERR("Error trying to update managed textures in ValidateDevice");
  822. return ret;
  823. }
  824. // Second, flush states, so we can validate the current state
  825. ret = FlushStates();
  826. if (ret != D3D_OK)
  827. {
  828. D3D_ERR("Error trying to FlushStates in ValidateDevice");
  829. return ret;
  830. }
  831. // Now ask the driver!
  832. *lpdwNumPasses = 0;
  833. memset(&vbod, 0, sizeof(D3DHAL_VALIDATETEXTURESTAGESTATEDATA));
  834. vbod.dwhContext = this->dwhContext;
  835. if (this->lpD3DHALCallbacks3->ValidateTextureStageState)
  836. {
  837. CALL_HAL3ONLY(ret, this, ValidateTextureStageState, &vbod);
  838. if (ret != DDHAL_DRIVER_HANDLED)
  839. return DDERR_UNSUPPORTED;
  840. *lpdwNumPasses = vbod.dwNumPasses;
  841. return vbod.ddrval;
  842. }
  843. else
  844. {
  845. D3D_ERR("Error: ValidateTextureStageState not supported by the driver.");
  846. }
  847. return DDERR_UNSUPPORTED;
  848. }
  849. #undef DPF_MODNAME
  850. #define DPF_MODNAME "CDirect3DDeviceIDP2::StartPrimVB"
  851. //---------------------------------------------------------------------
  852. // This function prepares the batch for new primitive.
  853. // Called only if vertices from user memory are NOT used for rendering
  854. //
  855. HRESULT CDirect3DDeviceIDP2::StartPrimVB(LPDIRECT3DVERTEXBUFFERI lpVBI,
  856. DWORD dwStartVertex)
  857. {
  858. HRESULT ret = D3D_OK;
  859. // If VID has been changed or new vertex buffer is used we flush the batch
  860. if (this->dwVIDOut != dp2data.dwVertexType ||
  861. lpDP2CurrBatchVBI != lpVBI ||
  862. dp2data.lpDDVertex != ((LPDDRAWI_DDRAWSURFACE_INT)(lpVBI->GetDDS()))->lpLcl)
  863. {
  864. ret = FlushStates();
  865. if (ret != D3D_OK)
  866. return ret;
  867. dp2data.dwVertexType = this->dwVIDOut;
  868. dp2data.dwVertexSize = this->dwOutputSize;
  869. dp2data.lpDDVertex = ((LPDDRAWI_DDRAWSURFACE_INT)(lpVBI->GetDDS()))->lpLcl;
  870. // Release previously used vertex buffer (if any), because we do not
  871. // need it any more. We did AddRef() to TL buffer, so it is safe.
  872. if (lpDP2CurrBatchVBI)
  873. lpDP2CurrBatchVBI->Release();
  874. // If a vertex buffer is used for rendering, make sure that it is not
  875. // released by user. So do AddRef().
  876. lpDP2CurrBatchVBI = lpVBI;
  877. lpDP2CurrBatchVBI->AddRef();
  878. }
  879. if (this->TLVbuf.GetVBI() == lpVBI)
  880. {
  881. this->dwVertexBase = this->dp2data.dwVertexLength;
  882. DDASSERT(this->dwVertexBase < MAX_DX6_VERTICES);
  883. dp2data.dwFlags |= D3DHALDP2_SWAPVERTEXBUFFER;
  884. }
  885. else
  886. {
  887. this->dwVertexBase = dwStartVertex;
  888. dp2data.dwFlags &= ~D3DHALDP2_SWAPVERTEXBUFFER;
  889. }
  890. this->dp2data.dwVertexLength = this->dwVertexBase + this->dwNumVertices;
  891. this->dwFlags |= D3DPV_WITHINPRIMITIVE;
  892. return ret;
  893. }
  894. #undef DPF_MODNAME
  895. #define DPF_MODNAME "CDirect3DDeviceIDP2::StartPrimUserMem"
  896. //---------------------------------------------------------------------
  897. // This function prepares the batch for new primitive.
  898. // Called if vertices from user memory is used for rendering
  899. //
  900. HRESULT CDirect3DDeviceIDP2::StartPrimUserMem(LPVOID lpMem)
  901. {
  902. const DWORD vertexPoolSize = this->dwNumVertices * this->dwOutputSize;
  903. HRESULT ret = D3D_OK;
  904. // If the primitive is small, we copy vertices into the TL buffer
  905. if (this->dwNumVertices < LOWVERTICESNUMBER)
  906. {
  907. if (vertexPoolSize > this->TLVbuf.GetSize())
  908. {
  909. if (this->TLVbuf.Grow(this, vertexPoolSize) != D3D_OK)
  910. {
  911. D3D_ERR( "Could not grow TL vertex buffer" );
  912. ret = DDERR_OUTOFMEMORY;
  913. goto l_exit;
  914. }
  915. }
  916. // So now user memory is not used any more.
  917. ret = StartPrimVB(this->TLVbuf.GetVBI(), 0);
  918. if (ret != D3D_OK)
  919. goto l_exit;
  920. LPVOID tmp = this->TLVbuf.GetAddress();
  921. memcpy(tmp, this->lpvOut, vertexPoolSize);
  922. // We have to update lpvOut, because it was set to user memory
  923. this->lpvOut = tmp;
  924. }
  925. else
  926. {
  927. // We can not mix user memory primitive with other primitives, so
  928. // flush the batch.
  929. // Do not forget to flush the batch after rendering this primitive
  930. ret = this->FlushStates();
  931. if (ret != D3D_OK)
  932. goto l_exit;
  933. // Release previously used vertex buffer (if any), because we do not
  934. // it any more
  935. if (lpDP2CurrBatchVBI)
  936. {
  937. lpDP2CurrBatchVBI->Release();
  938. lpDP2CurrBatchVBI = NULL;
  939. }
  940. dp2data.dwVertexType = this->dwVIDOut;
  941. dp2data.dwVertexSize = this->dwOutputSize;
  942. dp2data.lpVertices = lpMem;
  943. dp2data.dwFlags |= D3DHALDP2_USERMEMVERTICES;
  944. dp2data.dwFlags &= ~D3DHALDP2_SWAPVERTEXBUFFER;
  945. dp2data.dwVertexLength = this->dwNumVertices;
  946. this->dwFlags |= D3DPV_USERMEMVERTICES;
  947. }
  948. l_exit:
  949. this->dwFlags |= D3DPV_WITHINPRIMITIVE;
  950. return ret;
  951. }
  952. #undef DPF_MODNAME
  953. #define DPF_MODNAME "CDirect3DDeviceIDP2::EndPrim"
  954. //---------------------------------------------------------------------
  955. // This function should not be called from DrawVertexBufferVB
  956. //
  957. HRESULT CDirect3DDeviceIDP2::EndPrim(DWORD dwVertexPoolSize)
  958. {
  959. HRESULT ret = D3D_OK;
  960. if (this->dwFlags & D3DPV_USERMEMVERTICES)
  961. // We can not mix user memory primitive, so flush it.
  962. ret = this->FlushStates();
  963. else
  964. {
  965. // If TL buffer was used, we have to move its internal base pointer
  966. this->TLVbuf.Base() += dwVertexPoolSize;
  967. }
  968. this->dwFlags &= ~(D3DPV_USERMEMVERTICES | D3DPV_WITHINPRIMITIVE);
  969. return ret;
  970. }
  971. //---------------------------------------------------------------------
  972. //
  973. //
  974. HRESULT CDirect3DDeviceIDP2::UpdateDrvViewInfo(LPD3DVIEWPORT2 lpVwpData)
  975. {
  976. HRESULT ret = D3D_OK;
  977. // Check to see if there is space to add a new command for space
  978. if (dwDP2CommandLength + sizeof(D3DHAL_DP2COMMAND) +
  979. sizeof(D3DHAL_DP2VIEWPORTINFO) > dwDP2CommandBufSize)
  980. {
  981. ret = FlushStates();
  982. if (ret != D3D_OK)
  983. {
  984. D3D_ERR("Error trying to render batched commands in UpdateDrvViewInfo");
  985. return ret;
  986. }
  987. }
  988. // Add new ViewInfo instruction
  989. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  990. dwDP2CommandLength + dp2data.dwCommandOffset);
  991. lpDP2CurrCommand->bCommand = D3DDP2OP_VIEWPORTINFO;
  992. bDP2CurrCmdOP = D3DDP2OP_VIEWPORTINFO;
  993. lpDP2CurrCommand->bReserved = 0;
  994. lpDP2CurrCommand->wStateCount = 1;
  995. wDP2CurrCmdCnt = 1;
  996. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  997. // Add ViewInfo data
  998. LPD3DHAL_DP2VIEWPORTINFO lpVpInfo = (LPD3DHAL_DP2VIEWPORTINFO)(lpDP2CurrCommand + 1);
  999. lpVpInfo->dwX = lpVwpData->dwX;
  1000. lpVpInfo->dwY = lpVwpData->dwY;
  1001. lpVpInfo->dwWidth = lpVwpData->dwWidth;
  1002. lpVpInfo->dwHeight = lpVwpData->dwHeight;
  1003. dwDP2CommandLength += sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2VIEWPORTINFO);
  1004. return ret;
  1005. }
  1006. //---------------------------------------------------------------------
  1007. //
  1008. //
  1009. HRESULT CDirect3DDeviceIDP2::UpdateDrvWInfo()
  1010. {
  1011. HRESULT ret = D3D_OK;
  1012. // Check to see if there is space to add a new command for space
  1013. if (dwDP2CommandLength + sizeof(D3DHAL_DP2COMMAND) +
  1014. sizeof(D3DHAL_DP2WINFO) > dwDP2CommandBufSize)
  1015. {
  1016. ret = FlushStates();
  1017. if (ret != D3D_OK)
  1018. {
  1019. D3D_ERR("Error trying to render batched commands in UpdateDrvViewInfo");
  1020. return ret;
  1021. }
  1022. }
  1023. // Add new WInfo instruction
  1024. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  1025. dwDP2CommandLength + dp2data.dwCommandOffset);
  1026. lpDP2CurrCommand->bCommand = D3DDP2OP_WINFO;
  1027. bDP2CurrCmdOP = D3DDP2OP_WINFO;
  1028. lpDP2CurrCommand->bReserved = 0;
  1029. lpDP2CurrCommand->wStateCount = 1;
  1030. wDP2CurrCmdCnt = 1;
  1031. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  1032. // Add WInfo data
  1033. D3DMATRIXI &m = transform.proj;
  1034. LPD3DHAL_DP2WINFO lpWInfo = (LPD3DHAL_DP2WINFO)(lpDP2CurrCommand + 1);
  1035. if( (m._33 == m._34) || (m._33 == 0.0f) )
  1036. {
  1037. D3D_ERR( "Cannot compute WNear and WFar from the supplied projection matrix.\n Setting wNear to 0.0 and wFar to 1.0" );
  1038. lpWInfo->dvWNear = 0.0f;
  1039. lpWInfo->dvWFar = 1.0f;
  1040. dwDP2CommandLength += sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2WINFO);
  1041. return ret;
  1042. }
  1043. lpWInfo->dvWNear = m._44 - m._43/m._33*m._34;
  1044. lpWInfo->dvWFar = (m._44 - m._43)/(m._33 - m._34)*m._34 + m._44;
  1045. dwDP2CommandLength += sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2WINFO);
  1046. return ret;
  1047. }
  1048. #undef DPF_MODNAME
  1049. #define DPF_MODNAME "CDirect3DDeviceIDP2::UpdatePalette"
  1050. //---------------------------------------------------------------------
  1051. // This function should be called from PaletteUpdateNotify
  1052. //
  1053. HRESULT CDirect3DDeviceIDP2::UpdatePalette(
  1054. DWORD dwPaletteHandle,
  1055. DWORD dwStartIndex,
  1056. DWORD dwNumberOfIndices,
  1057. LPPALETTEENTRY pFirstIndex)
  1058. {
  1059. HRESULT ret = D3D_OK;
  1060. DWORD dwSizeChange=sizeof(D3DHAL_DP2COMMAND) +
  1061. sizeof(D3DHAL_DP2UPDATEPALETTE) + dwNumberOfIndices*sizeof(PALETTEENTRY);
  1062. if (bDP2CurrCmdOP == D3DDP2OP_UPDATEPALETTE)
  1063. { // Last instruction is a tex blt, append this one to it
  1064. }
  1065. // Check for space
  1066. if (dwDP2CommandLength + dwSizeChange > dwDP2CommandBufSize)
  1067. {
  1068. ret = FlushStates();
  1069. if (ret != D3D_OK)
  1070. {
  1071. D3D_ERR("Error trying to render batched commands in TexBltI");
  1072. return ret;
  1073. }
  1074. }
  1075. // Add new renderstate instruction
  1076. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  1077. dwDP2CommandLength + dp2data.dwCommandOffset);
  1078. lpDP2CurrCommand->bCommand = D3DDP2OP_UPDATEPALETTE;
  1079. bDP2CurrCmdOP = D3DDP2OP_UPDATEPALETTE;
  1080. lpDP2CurrCommand->bReserved = 0;
  1081. lpDP2CurrCommand->wStateCount = 1;
  1082. wDP2CurrCmdCnt = 1;
  1083. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  1084. // Add texture blt data
  1085. LPD3DHAL_DP2UPDATEPALETTE lpUpdatePal = (LPD3DHAL_DP2UPDATEPALETTE)(lpDP2CurrCommand + 1);
  1086. lpUpdatePal->dwPaletteHandle=dwPaletteHandle;
  1087. lpUpdatePal->wStartIndex=(WORD)dwStartIndex;
  1088. lpUpdatePal->wNumEntries=(WORD)dwNumberOfIndices;
  1089. memcpy((LPVOID)(lpUpdatePal+1),(LPVOID)pFirstIndex,
  1090. dwNumberOfIndices*sizeof(PALETTEENTRY));
  1091. dwDP2CommandLength += dwSizeChange;
  1092. return ret;
  1093. }
  1094. #undef DPF_MODNAME
  1095. #define DPF_MODNAME "CDirect3DDeviceIDP2::SetPalette"
  1096. //---------------------------------------------------------------------
  1097. // This function should be called from PaletteAssociateNotify
  1098. //
  1099. HRESULT CDirect3DDeviceIDP2::SetPalette(DWORD dwPaletteHandle, DWORD dwPaletteFlags, DWORD dwSurfaceHandle )
  1100. {
  1101. HRESULT ret = D3D_OK;
  1102. DWORD dwSizeChange;
  1103. if (bDP2CurrCmdOP == D3DDP2OP_SETPALETTE)
  1104. { // Last instruction is a tex blt, append this one to it
  1105. }
  1106. dwSizeChange=sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2SETPALETTE);
  1107. // Check for space
  1108. if (dwDP2CommandLength + dwSizeChange > dwDP2CommandBufSize)
  1109. {
  1110. ret = FlushStates();
  1111. if (ret != D3D_OK)
  1112. {
  1113. D3D_ERR("Error trying to render batched commands in TexBltI");
  1114. return ret;
  1115. }
  1116. }
  1117. // Add new renderstate instruction
  1118. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  1119. dwDP2CommandLength + dp2data.dwCommandOffset);
  1120. bDP2CurrCmdOP = lpDP2CurrCommand->bCommand = D3DDP2OP_SETPALETTE;
  1121. lpDP2CurrCommand->bReserved = 0;
  1122. lpDP2CurrCommand->wStateCount = 1;
  1123. wDP2CurrCmdCnt = 1;
  1124. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  1125. LPD3DHAL_DP2SETPALETTE lpSetPal = (LPD3DHAL_DP2SETPALETTE)(lpDP2CurrCommand + 1);
  1126. lpSetPal->dwPaletteHandle=dwPaletteHandle;
  1127. lpSetPal->dwPaletteFlags=dwPaletteFlags;
  1128. lpSetPal->dwSurfaceHandle=dwSurfaceHandle;
  1129. dwDP2CommandLength += dwSizeChange;
  1130. return ret;
  1131. }
  1132. #undef DPF_MODNAME
  1133. #define DPF_MODNAME "CDirect3DDeviceIDP2::SetRenderTargetI"
  1134. void CDirect3DDeviceIDP2::SetRenderTargetI(LPDIRECTDRAWSURFACE pRenderTarget, LPDIRECTDRAWSURFACE pZBuffer)
  1135. {
  1136. DWORD dwSizeChange;
  1137. dwSizeChange=sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2SETRENDERTARGET);
  1138. // Check for space
  1139. if (dwDP2CommandLength + dwSizeChange > dwDP2CommandBufSize)
  1140. {
  1141. if (FlushStates() != D3D_OK)
  1142. {
  1143. D3D_ERR("Error trying to render batched commands in SetRenderTargetI");
  1144. return;
  1145. }
  1146. }
  1147. // Add new renderstate instruction
  1148. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  1149. dwDP2CommandLength + dp2data.dwCommandOffset);
  1150. bDP2CurrCmdOP = lpDP2CurrCommand->bCommand = D3DDP2OP_SETRENDERTARGET;
  1151. lpDP2CurrCommand->bReserved = 0;
  1152. lpDP2CurrCommand->wStateCount = 1;
  1153. wDP2CurrCmdCnt = 1;
  1154. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  1155. LPD3DHAL_DP2SETRENDERTARGET pData = (LPD3DHAL_DP2SETRENDERTARGET)(lpDP2CurrCommand + 1);
  1156. pData->hRenderTarget = ((LPDDRAWI_DDRAWSURFACE_INT)pRenderTarget)->lpLcl->lpSurfMore->dwSurfaceHandle;
  1157. if (pZBuffer)
  1158. pData->hZBuffer = ((LPDDRAWI_DDRAWSURFACE_INT)pZBuffer)->lpLcl->lpSurfMore->dwSurfaceHandle;
  1159. else
  1160. pData->hZBuffer = 0;
  1161. dwDP2CommandLength += dwSizeChange;
  1162. }
  1163. #undef DPF_MODNAME
  1164. #define DPF_MODNAME "CDirect3DDeviceIDP2::ClearI"
  1165. void CDirect3DDeviceIDP2::ClearI(DWORD dwFlags, DWORD clrCount, LPD3DRECT clrRects, D3DCOLOR dwColor, D3DVALUE dvZ, DWORD dwStencil)
  1166. {
  1167. DWORD dwCommandSize = sizeof(D3DHAL_DP2COMMAND) + sizeof(D3DHAL_DP2CLEAR) + sizeof(RECT) * (clrCount - 1);
  1168. // Check to see if there is space to add a new command for space
  1169. if (dwCommandSize + dwDP2CommandLength > dwDP2CommandBufSize)
  1170. {
  1171. HRESULT ret = FlushStates();
  1172. if (ret != D3D_OK)
  1173. {
  1174. D3D_ERR("Error trying to render batched commands");
  1175. throw ret;
  1176. }
  1177. }
  1178. lpDP2CurrCommand = (LPD3DHAL_DP2COMMAND)((LPBYTE)lpvDP2Commands +
  1179. dwDP2CommandLength + dp2data.dwCommandOffset);
  1180. lpDP2CurrCommand->bCommand = D3DDP2OP_CLEAR;
  1181. bDP2CurrCmdOP = D3DDP2OP_CLEAR;
  1182. lpDP2CurrCommand->bReserved = 0;
  1183. wDP2CurrCmdCnt = (WORD)clrCount;
  1184. lpDP2CurrCommand->wStateCount = wDP2CurrCmdCnt;
  1185. D3D_INFO(6, "Write Ins:%08lx", *(LPDWORD)lpDP2CurrCommand);
  1186. dwDP2CommandLength += dwCommandSize;
  1187. // Write data
  1188. LPD3DHAL_DP2CLEAR pData = (LPD3DHAL_DP2CLEAR)(lpDP2CurrCommand + 1);
  1189. pData->dwFlags = dwFlags;
  1190. pData->dwFillColor = dwColor;
  1191. pData->dvFillDepth = dvZ;
  1192. pData->dwFillStencil = dwStencil;
  1193. memcpy(pData->Rects, clrRects, clrCount * sizeof(D3DRECT));
  1194. }