gfxD3D9Device.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/D3D9/gfxD3D9Device.h"
  23. #include "console/console.h"
  24. #include "core/stream/fileStream.h"
  25. #include "core/strings/unicode.h"
  26. #include "gfx/D3D9/gfxD3D9CardProfiler.h"
  27. #include "gfx/D3D9/gfxD3D9VertexBuffer.h"
  28. #include "gfx/D3D9/screenShotD3D9.h"
  29. #include "gfx/D3D9/gfxD3D9EnumTranslate.h"
  30. #include "gfx/D3D9/gfxD3D9QueryFence.h"
  31. #include "gfx/D3D9/gfxD3D9OcclusionQuery.h"
  32. #include "gfx/D3D9/gfxD3D9Shader.h"
  33. #include "windowManager/platformWindow.h"
  34. #include "materials/shaderData.h"
  35. #ifndef TORQUE_OS_XENON
  36. # include "windowManager/win32/win32Window.h"
  37. #endif
  38. D3DXFNTable GFXD3D9Device::smD3DX;
  39. GFXD3D9Device::GFXD3D9Device( LPDIRECT3D9 d3d, U32 index )
  40. {
  41. mDeviceSwizzle32 = &Swizzles::bgra;
  42. GFXVertexColor::setSwizzle( mDeviceSwizzle32 );
  43. mDeviceSwizzle24 = &Swizzles::bgr;
  44. mD3D = d3d;
  45. mAdapterIndex = index;
  46. mD3DDevice = NULL;
  47. mVolatileVB = NULL;
  48. mCurrentPB = NULL;
  49. mDynamicPB = NULL;
  50. mLastVertShader = NULL;
  51. mLastPixShader = NULL;
  52. mCanCurrentlyRender = false;
  53. mTextureManager = NULL;
  54. mCurrentStateBlock = NULL;
  55. mResourceListHead = NULL;
  56. #ifdef TORQUE_DEBUG
  57. mVBListHead = NULL;
  58. mNumAllocatedVertexBuffers = 0;
  59. #endif
  60. mPixVersion = 0.0;
  61. mNumSamplers = 0;
  62. mNumRenderTargets = 0;
  63. mCardProfiler = NULL;
  64. mDeviceDepthStencil = NULL;
  65. mDeviceBackbuffer = NULL;
  66. mDeviceColor = NULL;
  67. mCreateFenceType = -1; // Unknown, test on first allocate
  68. mCurrentConstBuffer = NULL;
  69. mOcclusionQuerySupported = false;
  70. for (U32 i = 0; i < GS_COUNT; ++i)
  71. mModelViewProjSC[i] = NULL;
  72. // Set up the Enum translation tables
  73. GFXD3D9EnumTranslate::init();
  74. #if !defined(TORQUE_OS_XENON)
  75. mD3DEx = NULL;
  76. mD3DDeviceEx = NULL;
  77. #endif
  78. }
  79. //-----------------------------------------------------------------------------
  80. GFXD3D9Device::~GFXD3D9Device()
  81. {
  82. // Release our refcount on the current stateblock object
  83. mCurrentStateBlock = NULL;
  84. releaseDefaultPoolResources();
  85. // Free the vertex declarations.
  86. VertexDeclMap::Iterator iter = mVertexDecls.begin();
  87. for ( ; iter != mVertexDecls.end(); iter++ )
  88. delete iter->value;
  89. // Check up on things
  90. Con::printf("Cur. D3DDevice ref count=%d", mD3DDevice->AddRef() - 1);
  91. mD3DDevice->Release();
  92. // Forcibly clean up the pools
  93. mVolatileVBList.setSize(0);
  94. mDynamicPB = NULL;
  95. // And release our D3D resources.
  96. SAFE_RELEASE( mDeviceDepthStencil );
  97. SAFE_RELEASE( mDeviceBackbuffer )
  98. SAFE_RELEASE( mDeviceColor );
  99. SAFE_RELEASE( mD3D );
  100. SAFE_RELEASE( mD3DDevice );
  101. #ifdef TORQUE_DEBUG
  102. logVertexBuffers();
  103. #endif
  104. if( mCardProfiler )
  105. {
  106. delete mCardProfiler;
  107. mCardProfiler = NULL;
  108. }
  109. if( gScreenShot )
  110. {
  111. delete gScreenShot;
  112. gScreenShot = NULL;
  113. }
  114. }
  115. //------------------------------------------------------------------------------
  116. // setupGenericShaders
  117. //------------------------------------------------------------------------------
  118. inline void GFXD3D9Device::setupGenericShaders( GenericShaderType type /* = GSColor */ )
  119. {
  120. AssertFatal(type != GSTargetRestore, ""); //not used
  121. if (mGenericShader[GSColor] == NULL)
  122. {
  123. ShaderData *shaderData;
  124. shaderData = new ShaderData();
  125. shaderData->setField("DXVertexShaderFile", "shaders/common/fixedFunction/colorV.hlsl");
  126. shaderData->setField("DXPixelShaderFile", "shaders/common/fixedFunction/colorP.hlsl");
  127. shaderData->setField("pixVersion", "3.0");
  128. shaderData->registerObject();
  129. mGenericShader[GSColor] = shaderData->getShader();
  130. mGenericShaderBuffer[GSColor] = mGenericShader[GSColor]->allocConstBuffer();
  131. mModelViewProjSC[GSColor] = mGenericShader[GSColor]->getShaderConstHandle("$modelView");
  132. Sim::getRootGroup()->addObject(shaderData);
  133. shaderData = new ShaderData();
  134. shaderData->setField("DXVertexShaderFile", "shaders/common/fixedFunction/modColorTextureV.hlsl");
  135. shaderData->setField("DXPixelShaderFile", "shaders/common/fixedFunction/modColorTextureP.hlsl");
  136. shaderData->setSamplerName("$diffuseMap", 0);
  137. shaderData->setField("pixVersion", "3.0");
  138. shaderData->registerObject();
  139. mGenericShader[GSModColorTexture] = shaderData->getShader();
  140. mGenericShaderBuffer[GSModColorTexture] = mGenericShader[GSModColorTexture]->allocConstBuffer();
  141. mModelViewProjSC[GSModColorTexture] = mGenericShader[GSModColorTexture]->getShaderConstHandle("$modelView");
  142. Sim::getRootGroup()->addObject(shaderData);
  143. shaderData = new ShaderData();
  144. shaderData->setField("DXVertexShaderFile", "shaders/common/fixedFunction/addColorTextureV.hlsl");
  145. shaderData->setField("DXPixelShaderFile", "shaders/common/fixedFunction/addColorTextureP.hlsl");
  146. shaderData->setSamplerName("$diffuseMap", 0);
  147. shaderData->setField("pixVersion", "3.0");
  148. shaderData->registerObject();
  149. mGenericShader[GSAddColorTexture] = shaderData->getShader();
  150. mGenericShaderBuffer[GSAddColorTexture] = mGenericShader[GSAddColorTexture]->allocConstBuffer();
  151. mModelViewProjSC[GSAddColorTexture] = mGenericShader[GSAddColorTexture]->getShaderConstHandle("$modelView");
  152. Sim::getRootGroup()->addObject(shaderData);
  153. shaderData = new ShaderData();
  154. shaderData->setField("DXVertexShaderFile", "shaders/common/fixedFunction/textureV.hlsl");
  155. shaderData->setField("DXPixelShaderFile", "shaders/common/fixedFunction/textureP.hlsl");
  156. shaderData->setSamplerName("$diffuseMap", 0);
  157. shaderData->setField("pixVersion", "3.0");
  158. shaderData->registerObject();
  159. mGenericShader[GSTexture] = shaderData->getShader();
  160. mGenericShaderBuffer[GSTexture] = mGenericShader[GSTexture]->allocConstBuffer();
  161. mModelViewProjSC[GSTexture] = mGenericShader[GSTexture]->getShaderConstHandle("$modelView");
  162. Sim::getRootGroup()->addObject(shaderData);
  163. //Force an update
  164. mViewportDirty = true;
  165. }
  166. MatrixF tempMatrix = mProjectionMatrix * mViewMatrix * mWorldMatrix[mWorldStackSize];
  167. mGenericShaderBuffer[type]->setSafe(mModelViewProjSC[type], tempMatrix);
  168. setShader(mGenericShader[type]);
  169. setShaderConstBuffer(mGenericShaderBuffer[type]);
  170. }
  171. //-----------------------------------------------------------------------------
  172. /// Creates a state block object based on the desc passed in. This object
  173. /// represents an immutable state.
  174. GFXStateBlockRef GFXD3D9Device::createStateBlockInternal(const GFXStateBlockDesc& desc)
  175. {
  176. return GFXStateBlockRef(new GFXD3D9StateBlock(desc, mD3DDevice));
  177. }
  178. /// Activates a stateblock
  179. void GFXD3D9Device::setStateBlockInternal(GFXStateBlock* block, bool force)
  180. {
  181. AssertFatal(dynamic_cast<GFXD3D9StateBlock*>(block), "Incorrect stateblock type for this device!");
  182. GFXD3D9StateBlock* d3dBlock = static_cast<GFXD3D9StateBlock*>(block);
  183. GFXD3D9StateBlock* d3dCurrent = static_cast<GFXD3D9StateBlock*>(mCurrentStateBlock.getPointer());
  184. if (force)
  185. d3dCurrent = NULL;
  186. d3dBlock->activate(d3dCurrent);
  187. }
  188. /// Called by base GFXDevice to actually set a const buffer
  189. void GFXD3D9Device::setShaderConstBufferInternal(GFXShaderConstBuffer* buffer)
  190. {
  191. if (buffer)
  192. {
  193. PROFILE_SCOPE(GFXD3D9Device_setShaderConstBufferInternal);
  194. AssertFatal(dynamic_cast<GFXD3D9ShaderConstBuffer*>(buffer), "Incorrect shader const buffer type for this device!");
  195. GFXD3D9ShaderConstBuffer* d3dBuffer = static_cast<GFXD3D9ShaderConstBuffer*>(buffer);
  196. d3dBuffer->activate(mCurrentConstBuffer);
  197. mCurrentConstBuffer = d3dBuffer;
  198. } else {
  199. mCurrentConstBuffer = NULL;
  200. }
  201. }
  202. //-----------------------------------------------------------------------------
  203. void GFXD3D9Device::clear( U32 flags, ColorI color, F32 z, U32 stencil )
  204. {
  205. // Make sure we have flushed our render target state.
  206. _updateRenderTargets();
  207. // Kind of a bummer we have to do this, there should be a better way made
  208. DWORD realflags = 0;
  209. if( flags & GFXClearTarget )
  210. realflags |= D3DCLEAR_TARGET;
  211. if( flags & GFXClearZBuffer )
  212. realflags |= D3DCLEAR_ZBUFFER;
  213. if( flags & GFXClearStencil )
  214. realflags |= D3DCLEAR_STENCIL;
  215. mD3DDevice->Clear( 0, NULL, realflags,
  216. D3DCOLOR_ARGB( color.alpha, color.red, color.green, color.blue ),
  217. z, stencil );
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool GFXD3D9Device::beginSceneInternal()
  221. {
  222. HRESULT hr = mD3DDevice->BeginScene();
  223. D3D9Assert(hr, "GFXD3D9Device::beginSceneInternal - failed to BeginScene");
  224. mCanCurrentlyRender = SUCCEEDED(hr);
  225. return mCanCurrentlyRender;
  226. }
  227. //-----------------------------------------------------------------------------
  228. void GFXD3D9Device::endSceneInternal()
  229. {
  230. mD3DDevice->EndScene();
  231. mCanCurrentlyRender = false;
  232. }
  233. void GFXD3D9Device::_updateRenderTargets()
  234. {
  235. if ( mRTDirty || ( mCurrentRT && mCurrentRT->isPendingState() ) )
  236. {
  237. if ( mRTDeactivate )
  238. {
  239. mRTDeactivate->deactivate();
  240. mRTDeactivate = NULL;
  241. }
  242. // NOTE: The render target changes are not really accurate
  243. // as the GFXTextureTarget supports MRT internally. So when
  244. // we activate a GFXTarget it could result in multiple calls
  245. // to SetRenderTarget on the actual device.
  246. mDeviceStatistics.mRenderTargetChanges++;
  247. mCurrentRT->activate();
  248. mRTDirty = false;
  249. }
  250. if ( mViewportDirty )
  251. {
  252. D3DVIEWPORT9 viewport;
  253. viewport.X = mViewport.point.x;
  254. viewport.Y = mViewport.point.y;
  255. viewport.Width = mViewport.extent.x;
  256. viewport.Height = mViewport.extent.y;
  257. viewport.MinZ = 0.0;
  258. viewport.MaxZ = 1.0;
  259. D3D9Assert( mD3DDevice->SetViewport( &viewport ),
  260. "GFXD3D9Device::_updateRenderTargets() - Error setting viewport!" );
  261. mViewportDirty = false;
  262. }
  263. }
  264. #ifdef TORQUE_DEBUG
  265. void GFXD3D9Device::logVertexBuffers()
  266. {
  267. // NOTE: This function should be called on the destructor of this class and ONLY then
  268. // otherwise it'll produce the wrong output
  269. if( mNumAllocatedVertexBuffers == 0 )
  270. return;
  271. FileStream fs;
  272. fs.open( "vertexbuffer.log", Torque::FS::File::Write );
  273. char buff[256];
  274. fs.writeLine( (U8 *)avar("-- Vertex buffer memory leak report -- time = %d", Platform::getRealMilliseconds()) );
  275. dSprintf( (char *)&buff, sizeof( buff ), "%d un-freed vertex buffers", mNumAllocatedVertexBuffers );
  276. fs.writeLine( (U8 *)buff );
  277. GFXD3D9VertexBuffer *walk = mVBListHead;
  278. while( walk != NULL )
  279. {
  280. dSprintf( (char *)&buff, sizeof( buff ), "[Name: %s] Size: %d", walk->name, walk->mNumVerts );
  281. fs.writeLine( (U8 *)buff );
  282. walk = walk->next;
  283. }
  284. fs.writeLine( (U8 *)"-- End report --" );
  285. fs.close();
  286. }
  287. //-----------------------------------------------------------------------------
  288. void GFXD3D9Device::addVertexBuffer( GFXD3D9VertexBuffer *buffer )
  289. {
  290. mNumAllocatedVertexBuffers++;
  291. if( mVBListHead == NULL )
  292. {
  293. mVBListHead = buffer;
  294. }
  295. else
  296. {
  297. GFXD3D9VertexBuffer *walk = mVBListHead;
  298. while( walk->next != NULL )
  299. {
  300. walk = walk->next;
  301. }
  302. walk->next = buffer;
  303. }
  304. buffer->next = NULL;
  305. }
  306. //-----------------------------------------------------------------------------
  307. void GFXD3D9Device::removeVertexBuffer( GFXD3D9VertexBuffer *buffer )
  308. {
  309. mNumAllocatedVertexBuffers--;
  310. // Quick check to see if this is head of list
  311. if( mVBListHead == buffer )
  312. {
  313. mVBListHead = mVBListHead->next;
  314. return;
  315. }
  316. GFXD3D9VertexBuffer *walk = mVBListHead;
  317. while( walk->next != NULL )
  318. {
  319. if( walk->next == buffer )
  320. {
  321. walk->next = walk->next->next;
  322. return;
  323. }
  324. walk = walk->next;
  325. }
  326. AssertFatal( false, "Vertex buffer not found in list." );
  327. }
  328. #endif
  329. //-----------------------------------------------------------------------------
  330. void GFXD3D9Device::releaseDefaultPoolResources()
  331. {
  332. // Release all the dynamic vertex buffer arrays
  333. // Forcibly clean up the pools
  334. for( U32 i=0; i<mVolatileVBList.size(); i++ )
  335. {
  336. // Con::printf("Trying to release volatile vb with COM refcount of %d and internal refcount of %d", mVolatileVBList[i]->vb->AddRef() - 1, mVolatileVBList[i]->mRefCount);
  337. // mVolatileVBList[i]->vb->Release();
  338. mVolatileVBList[i]->vb->Release();
  339. mVolatileVBList[i]->vb = NULL;
  340. mVolatileVBList[i] = NULL;
  341. }
  342. mVolatileVBList.setSize(0);
  343. // We gotta clear the current const buffer else the next
  344. // activate may erroneously think the device is still holding
  345. // this state and fail to set it.
  346. mCurrentConstBuffer = NULL;
  347. // Set current VB to NULL and set state dirty
  348. for ( U32 i=0; i < VERTEX_STREAM_COUNT; i++ )
  349. {
  350. mCurrentVertexBuffer[i] = NULL;
  351. mVertexBufferDirty[i] = true;
  352. mVertexBufferFrequency[i] = 0;
  353. mVertexBufferFrequencyDirty[i] = true;
  354. }
  355. // Release dynamic index buffer
  356. if( mDynamicPB != NULL )
  357. {
  358. SAFE_RELEASE( mDynamicPB->ib );
  359. }
  360. // Set current PB/IB to NULL and set state dirty
  361. mCurrentPrimitiveBuffer = NULL;
  362. mCurrentPB = NULL;
  363. mPrimitiveBufferDirty = true;
  364. // Zombify texture manager (for D3D this only modifies default pool textures)
  365. if( mTextureManager )
  366. mTextureManager->zombify();
  367. // Kill off other potentially dangling references...
  368. SAFE_RELEASE( mDeviceDepthStencil );
  369. SAFE_RELEASE( mDeviceBackbuffer );
  370. mD3DDevice->SetDepthStencilSurface(NULL);
  371. // Set global dirty state so the IB/PB and VB get reset
  372. mStateDirty = true;
  373. // Walk the resource list and zombify everything.
  374. GFXResource *walk = mResourceListHead;
  375. while(walk)
  376. {
  377. walk->zombify();
  378. walk = walk->getNextResource();
  379. }
  380. }
  381. //-----------------------------------------------------------------------------
  382. void GFXD3D9Device::reacquireDefaultPoolResources()
  383. {
  384. // Now do the dynamic index buffers
  385. if( mDynamicPB == NULL )
  386. mDynamicPB = new GFXD3D9PrimitiveBuffer(this, 0, 0, GFXBufferTypeDynamic);
  387. D3D9Assert( mD3DDevice->CreateIndexBuffer( sizeof( U16 ) * MAX_DYNAMIC_INDICES,
  388. #ifdef TORQUE_OS_XENON
  389. D3DUSAGE_WRITEONLY,
  390. #else
  391. D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
  392. #endif
  393. GFXD3D9IndexFormat[GFXIndexFormat16], D3DPOOL_DEFAULT, &mDynamicPB->ib, NULL ), "Failed to allocate dynamic IB" );
  394. // Grab the depth-stencil...
  395. SAFE_RELEASE(mDeviceDepthStencil);
  396. D3D9Assert(mD3DDevice->GetDepthStencilSurface(&mDeviceDepthStencil),
  397. "GFXD3D9Device::reacquireDefaultPoolResources - couldn't grab reference to device's depth-stencil surface.");
  398. SAFE_RELEASE(mDeviceBackbuffer);
  399. mD3DDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &mDeviceBackbuffer );
  400. // Store for query later.
  401. mD3DDevice->GetDisplayMode( 0, &mDisplayMode );
  402. // Walk the resource list and zombify everything.
  403. GFXResource *walk = mResourceListHead;
  404. while(walk)
  405. {
  406. walk->resurrect();
  407. walk = walk->getNextResource();
  408. }
  409. if(mTextureManager)
  410. mTextureManager->resurrect();
  411. }
  412. GFXD3D9VertexBuffer* GFXD3D9Device::findVBPool( const GFXVertexFormat *vertexFormat, U32 vertsNeeded )
  413. {
  414. PROFILE_SCOPE( GFXD3D9Device_findVBPool );
  415. // Verts needed is ignored on the base device, 360 is different
  416. for( U32 i=0; i<mVolatileVBList.size(); i++ )
  417. if( mVolatileVBList[i]->mVertexFormat.isEqual( *vertexFormat ) )
  418. return mVolatileVBList[i];
  419. return NULL;
  420. }
  421. GFXD3D9VertexBuffer * GFXD3D9Device::createVBPool( const GFXVertexFormat *vertexFormat, U32 vertSize )
  422. {
  423. PROFILE_SCOPE( GFXD3D9Device_createVBPool );
  424. // this is a bit funky, but it will avoid problems with (lack of) copy constructors
  425. // with a push_back() situation
  426. mVolatileVBList.increment();
  427. StrongRefPtr<GFXD3D9VertexBuffer> newBuff;
  428. mVolatileVBList.last() = new GFXD3D9VertexBuffer();
  429. newBuff = mVolatileVBList.last();
  430. newBuff->mNumVerts = 0;
  431. newBuff->mBufferType = GFXBufferTypeVolatile;
  432. newBuff->mVertexFormat.copy( *vertexFormat );
  433. newBuff->mVertexSize = vertSize;
  434. newBuff->mDevice = this;
  435. // Requesting it will allocate it.
  436. vertexFormat->getDecl();
  437. // Con::printf("Created buff with type %x", vertFlags);
  438. D3D9Assert( mD3DDevice->CreateVertexBuffer( vertSize * MAX_DYNAMIC_VERTS,
  439. #ifdef TORQUE_OS_XENON
  440. D3DUSAGE_WRITEONLY,
  441. #else
  442. D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
  443. #endif
  444. 0,
  445. D3DPOOL_DEFAULT,
  446. &newBuff->vb,
  447. NULL ),
  448. "Failed to allocate dynamic VB" );
  449. return newBuff;
  450. }
  451. //-----------------------------------------------------------------------------
  452. void GFXD3D9Device::setClipRect( const RectI &inRect )
  453. {
  454. // We transform the incoming rect by the view
  455. // matrix first, so that it can be used to pan
  456. // and scale the clip rect.
  457. //
  458. // This is currently used to take tiled screenshots.
  459. Point3F pos( inRect.point.x, inRect.point.y, 0.0f );
  460. Point3F extent( inRect.extent.x, inRect.extent.y, 0.0f );
  461. getViewMatrix().mulP( pos );
  462. getViewMatrix().mulV( extent );
  463. RectI rect( pos.x, pos.y, extent.x, extent.y );
  464. // Clip the rect against the renderable size.
  465. Point2I size = mCurrentRT->getSize();
  466. RectI maxRect(Point2I(0,0), size);
  467. rect.intersect(maxRect);
  468. mClipRect = rect;
  469. F32 l = F32( mClipRect.point.x );
  470. F32 r = F32( mClipRect.point.x + mClipRect.extent.x );
  471. F32 b = F32( mClipRect.point.y + mClipRect.extent.y );
  472. F32 t = F32( mClipRect.point.y );
  473. // Set up projection matrix,
  474. static Point4F pt;
  475. pt.set(2.0f / (r - l), 0.0f, 0.0f, 0.0f);
  476. mTempMatrix.setColumn(0, pt);
  477. pt.set(0.0f, 2.0f/(t - b), 0.0f, 0.0f);
  478. mTempMatrix.setColumn(1, pt);
  479. pt.set(0.0f, 0.0f, 1.0f, 0.0f);
  480. mTempMatrix.setColumn(2, pt);
  481. pt.set((l+r)/(l-r), (t+b)/(b-t), 1.0f, 1.0f);
  482. mTempMatrix.setColumn(3, pt);
  483. setProjectionMatrix( mTempMatrix );
  484. // Set up world/view matrix
  485. mTempMatrix.identity();
  486. setWorldMatrix( mTempMatrix );
  487. setViewport( mClipRect );
  488. }
  489. void GFXD3D9Device::setVertexStream( U32 stream, GFXVertexBuffer *buffer )
  490. {
  491. GFXD3D9VertexBuffer *d3dBuffer = static_cast<GFXD3D9VertexBuffer*>( buffer );
  492. if ( stream == 0 )
  493. {
  494. // Set the volatile buffer which is used to
  495. // offset the start index when doing draw calls.
  496. if ( d3dBuffer && d3dBuffer->mVolatileStart > 0 )
  497. mVolatileVB = d3dBuffer;
  498. else
  499. mVolatileVB = NULL;
  500. }
  501. U32 offset = d3dBuffer && stream != 0 ? d3dBuffer->mVolatileStart * d3dBuffer->mVertexSize : 0;
  502. // NOTE: We do not use the stream offset here for stream 0
  503. // as that feature is *supposedly* not as well supported as
  504. // using the start index in drawPrimitive.
  505. //
  506. // If we can verify that this is not the case then we should
  507. // start using this method exclusively for all streams.
  508. D3D9Assert( mD3DDevice->SetStreamSource( stream,
  509. d3dBuffer ? d3dBuffer->vb : NULL,
  510. offset,
  511. d3dBuffer ? d3dBuffer->mVertexSize : 0 ),
  512. "GFXD3D9Device::setVertexStream - Failed to set stream source." );
  513. }
  514. void GFXD3D9Device::setVertexStreamFrequency( U32 stream, U32 frequency )
  515. {
  516. if ( frequency == 0 )
  517. frequency = 1;
  518. else
  519. {
  520. if ( stream == 0 )
  521. frequency = D3DSTREAMSOURCE_INDEXEDDATA | frequency;
  522. else
  523. frequency = D3DSTREAMSOURCE_INSTANCEDATA | frequency;
  524. }
  525. D3D9Assert( mD3DDevice->SetStreamSourceFreq( stream, frequency ),
  526. "GFXD3D9Device::setVertexStreamFrequency - Failed to set stream frequency." );
  527. }
  528. void GFXD3D9Device::_setPrimitiveBuffer( GFXPrimitiveBuffer *buffer )
  529. {
  530. mCurrentPB = static_cast<GFXD3D9PrimitiveBuffer *>( buffer );
  531. D3D9Assert( mD3DDevice->SetIndices( mCurrentPB->ib ), "Failed to set indices" );
  532. }
  533. void GFXD3D9Device::drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount )
  534. {
  535. // This is done to avoid the function call overhead if possible
  536. if( mStateDirty )
  537. updateStates();
  538. if (mCurrentShaderConstBuffer)
  539. setShaderConstBufferInternal(mCurrentShaderConstBuffer);
  540. if ( mVolatileVB )
  541. vertexStart += mVolatileVB->mVolatileStart;
  542. D3D9Assert( mD3DDevice->DrawPrimitive( GFXD3D9PrimType[primType], vertexStart, primitiveCount ), "Failed to draw primitives" );
  543. mDeviceStatistics.mDrawCalls++;
  544. if ( mVertexBufferFrequency[0] > 1 )
  545. mDeviceStatistics.mPolyCount += primitiveCount * mVertexBufferFrequency[0];
  546. else
  547. mDeviceStatistics.mPolyCount += primitiveCount;
  548. }
  549. //-----------------------------------------------------------------------------
  550. void GFXD3D9Device::drawIndexedPrimitive( GFXPrimitiveType primType,
  551. U32 startVertex,
  552. U32 minIndex,
  553. U32 numVerts,
  554. U32 startIndex,
  555. U32 primitiveCount )
  556. {
  557. // This is done to avoid the function call overhead if possible
  558. if( mStateDirty )
  559. updateStates();
  560. if (mCurrentShaderConstBuffer)
  561. setShaderConstBufferInternal(mCurrentShaderConstBuffer);
  562. AssertFatal( mCurrentPB != NULL, "Trying to call drawIndexedPrimitive with no current index buffer, call setIndexBuffer()" );
  563. if ( mVolatileVB )
  564. startVertex += mVolatileVB->mVolatileStart;
  565. D3D9Assert( mD3DDevice->DrawIndexedPrimitive( GFXD3D9PrimType[primType],
  566. startVertex,
  567. /* mCurrentPB->mVolatileStart + */ minIndex,
  568. numVerts,
  569. mCurrentPB->mVolatileStart + startIndex,
  570. primitiveCount ), "Failed to draw indexed primitive" );
  571. mDeviceStatistics.mDrawCalls++;
  572. if ( mVertexBufferFrequency[0] > 1 )
  573. mDeviceStatistics.mPolyCount += primitiveCount * mVertexBufferFrequency[0];
  574. else
  575. mDeviceStatistics.mPolyCount += primitiveCount;
  576. }
  577. GFXShader* GFXD3D9Device::createShader()
  578. {
  579. GFXD3D9Shader* shader = new GFXD3D9Shader();
  580. shader->registerResourceWithDevice( this );
  581. return shader;
  582. }
  583. void GFXD3D9Device::disableShaders(bool force)
  584. {
  585. setShader( NULL, force );
  586. setShaderConstBuffer( NULL );
  587. }
  588. //-----------------------------------------------------------------------------
  589. // Set shader - this function exists to make sure this is done in one place,
  590. // and to make sure redundant shader states are not being
  591. // sent to the card.
  592. //-----------------------------------------------------------------------------
  593. void GFXD3D9Device::setShader( GFXShader *shader, bool force )
  594. {
  595. GFXD3D9Shader *d3dShader = static_cast<GFXD3D9Shader*>( shader );
  596. IDirect3DPixelShader9 *pixShader = ( d3dShader != NULL ? d3dShader->mPixShader : NULL );
  597. IDirect3DVertexShader9 *vertShader = ( d3dShader ? d3dShader->mVertShader : NULL );
  598. if( pixShader != mLastPixShader || force )
  599. {
  600. mD3DDevice->SetPixelShader( pixShader );
  601. mLastPixShader = pixShader;
  602. }
  603. if( vertShader != mLastVertShader || force )
  604. {
  605. mD3DDevice->SetVertexShader( vertShader );
  606. mLastVertShader = vertShader;
  607. }
  608. }
  609. //-----------------------------------------------------------------------------
  610. // allocPrimitiveBuffer
  611. //-----------------------------------------------------------------------------
  612. GFXPrimitiveBuffer * GFXD3D9Device::allocPrimitiveBuffer( U32 numIndices,
  613. U32 numPrimitives,
  614. GFXBufferType bufferType,
  615. void* data )
  616. {
  617. // Allocate a buffer to return
  618. GFXD3D9PrimitiveBuffer * res = new GFXD3D9PrimitiveBuffer(this, numIndices, numPrimitives, bufferType);
  619. // Determine usage flags
  620. U32 usage = 0;
  621. D3DPOOL pool = D3DPOOL_DEFAULT;
  622. // Assumptions:
  623. // - static buffers are write rarely, use many
  624. // - dynamic buffers are write many, use many
  625. // - volatile buffers are write once, use once
  626. // You may never read from a buffer.
  627. switch(bufferType)
  628. {
  629. case GFXBufferTypeImmutable:
  630. case GFXBufferTypeStatic:
  631. pool = isD3D9Ex() ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  632. break;
  633. case GFXBufferTypeDynamic:
  634. case GFXBufferTypeVolatile:
  635. #ifndef TORQUE_OS_XENON
  636. usage |= D3DUSAGE_DYNAMIC;
  637. #endif
  638. break;
  639. }
  640. // Register resource
  641. res->registerResourceWithDevice(this);
  642. // We never allow reading from a primitive buffer.
  643. usage |= D3DUSAGE_WRITEONLY;
  644. // Create d3d index buffer
  645. if(bufferType == GFXBufferTypeVolatile)
  646. {
  647. // Get it from the pool if it's a volatile...
  648. AssertFatal( numIndices < MAX_DYNAMIC_INDICES, "Cannot allocate that many indices in a volatile buffer, increase MAX_DYNAMIC_INDICES." );
  649. res->ib = mDynamicPB->ib;
  650. // mDynamicPB->ib->AddRef();
  651. res->mVolatileBuffer = mDynamicPB;
  652. }
  653. else
  654. {
  655. // Otherwise, get it as a seperate buffer...
  656. D3D9Assert(mD3DDevice->CreateIndexBuffer( sizeof(U16) * numIndices , usage, GFXD3D9IndexFormat[GFXIndexFormat16], pool, &res->ib, 0),
  657. "Failed to allocate an index buffer.");
  658. }
  659. if(data)
  660. {
  661. void* dest;
  662. res->lock(0, numIndices, &dest);
  663. dMemcpy(dest, data, sizeof(U16) * numIndices);
  664. res->unlock();
  665. }
  666. return res;
  667. }
  668. //-----------------------------------------------------------------------------
  669. // allocVertexBuffer
  670. //-----------------------------------------------------------------------------
  671. GFXVertexBuffer * GFXD3D9Device::allocVertexBuffer( U32 numVerts,
  672. const GFXVertexFormat *vertexFormat,
  673. U32 vertSize,
  674. GFXBufferType bufferType,
  675. void* data)
  676. {
  677. PROFILE_SCOPE( GFXD3D9Device_allocVertexBuffer );
  678. GFXD3D9VertexBuffer *res = new GFXD3D9VertexBuffer( this,
  679. numVerts,
  680. vertexFormat,
  681. vertSize,
  682. bufferType );
  683. // Determine usage flags
  684. U32 usage = 0;
  685. D3DPOOL pool = D3DPOOL_DEFAULT;
  686. res->mNumVerts = 0;
  687. // Assumptions:
  688. // - static buffers are write rarely, use many
  689. // - dynamic buffers are write many, use many
  690. // - volatile buffers are write once, use once
  691. // You may never read from a buffer.
  692. switch(bufferType)
  693. {
  694. case GFXBufferTypeImmutable:
  695. case GFXBufferTypeStatic:
  696. pool = isD3D9Ex() ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  697. break;
  698. case GFXBufferTypeDynamic:
  699. case GFXBufferTypeVolatile:
  700. pool = D3DPOOL_DEFAULT;
  701. usage |= D3DUSAGE_WRITEONLY;
  702. #ifndef TORQUE_OS_XENON
  703. usage |= D3DUSAGE_DYNAMIC;
  704. #endif
  705. break;
  706. }
  707. res->registerResourceWithDevice(this);
  708. // Create vertex buffer
  709. if( bufferType == GFXBufferTypeVolatile )
  710. {
  711. // NOTE: Volatile VBs are pooled and will be allocated at lock time.
  712. AssertFatal( numVerts <= MAX_DYNAMIC_VERTS,
  713. "GFXD3D9Device::allocVertexBuffer - Volatile vertex buffer is too big... see MAX_DYNAMIC_VERTS!" );
  714. }
  715. else
  716. {
  717. // Requesting it will allocate it.
  718. vertexFormat->getDecl();
  719. // Get a new buffer...
  720. D3D9Assert( mD3DDevice->CreateVertexBuffer( vertSize * numVerts, usage, 0, pool, &res->vb, NULL ),
  721. "Failed to allocate VB" );
  722. }
  723. res->mNumVerts = numVerts;
  724. if(data)
  725. {
  726. void* dest;
  727. res->lock(0, numVerts, &dest);
  728. dMemcpy(dest, data, vertSize * numVerts);
  729. res->unlock();
  730. }
  731. return res;
  732. }
  733. //-----------------------------------------------------------------------------
  734. // deallocate vertex buffer
  735. //-----------------------------------------------------------------------------
  736. void GFXD3D9Device::deallocVertexBuffer( GFXD3D9VertexBuffer *vertBuff )
  737. {
  738. SAFE_RELEASE(vertBuff->vb);
  739. }
  740. GFXVertexDecl* GFXD3D9Device::allocVertexDecl( const GFXVertexFormat *vertexFormat )
  741. {
  742. PROFILE_SCOPE( GFXD3D9Device_allocVertexDecl );
  743. // First check the map... you shouldn't allocate VBs very often
  744. // if you want performance. The map lookup should never become
  745. // a performance bottleneck.
  746. D3D9VertexDecl *decl = mVertexDecls[vertexFormat->getDescription()];
  747. if ( decl )
  748. return decl;
  749. // Setup the declaration struct.
  750. U32 elemCount = vertexFormat->getElementCount();
  751. U32 offsets[4] = { 0 };
  752. U32 stream;
  753. S32 i = 0;
  754. S32 elemIdx = 0;
  755. D3DVERTEXELEMENT9 *vd = new D3DVERTEXELEMENT9[ elemCount + 1 ];
  756. for ( i=0; elemIdx < elemCount; i++, elemIdx++ )
  757. {
  758. const GFXVertexElement &element = vertexFormat->getElement( elemIdx );
  759. stream = element.getStreamIndex();
  760. vd[i].Stream = stream;
  761. vd[i].Offset = offsets[stream];
  762. vd[i].Type = GFXD3D9DeclType[element.getType()];
  763. vd[i].Method = D3DDECLMETHOD_DEFAULT;
  764. // We force the usage index of 0 for everything but
  765. // texture coords for now... this may change later.
  766. vd[i].UsageIndex = 0;
  767. if ( element.isSemantic( GFXSemantic::POSITION ) )
  768. vd[i].Usage = D3DDECLUSAGE_POSITION;
  769. else if ( element.isSemantic( GFXSemantic::NORMAL ) )
  770. vd[i].Usage = D3DDECLUSAGE_NORMAL;
  771. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  772. vd[i].Usage = D3DDECLUSAGE_COLOR;
  773. else if ( element.isSemantic( GFXSemantic::TANGENT ) )
  774. vd[i].Usage = D3DDECLUSAGE_TANGENT;
  775. else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
  776. vd[i].Usage = D3DDECLUSAGE_BINORMAL;
  777. else if ( element.isSemantic( GFXSemantic::BLENDINDICES ) )
  778. {
  779. vd[i].Usage = D3DDECLUSAGE_BLENDINDICES;
  780. vd[i].UsageIndex = element.getSemanticIndex();
  781. }
  782. else if ( element.isSemantic( GFXSemantic::BLENDWEIGHT ) )
  783. {
  784. vd[i].Usage = D3DDECLUSAGE_BLENDWEIGHT;
  785. vd[i].UsageIndex = element.getSemanticIndex();
  786. }
  787. else if ( element.isSemantic( GFXSemantic::PADDING ) )
  788. i--;
  789. else
  790. {
  791. // Anything that falls thru to here will be a texture coord.
  792. vd[i].Usage = D3DDECLUSAGE_TEXCOORD;
  793. vd[i].UsageIndex = element.getSemanticIndex();
  794. }
  795. offsets[stream] += element.getSizeInBytes();
  796. }
  797. D3DVERTEXELEMENT9 declEnd = D3DDECL_END();
  798. vd[i] = declEnd;
  799. decl = new D3D9VertexDecl();
  800. D3D9Assert( mD3DDevice->CreateVertexDeclaration( vd, &decl->decl ),
  801. "GFXD3D9Device::allocVertexDecl - Failed to create vertex declaration!" );
  802. delete [] vd;
  803. // Store it in the cache.
  804. mVertexDecls[vertexFormat->getDescription()] = decl;
  805. return decl;
  806. }
  807. void GFXD3D9Device::setVertexDecl( const GFXVertexDecl *decl )
  808. {
  809. IDirect3DVertexDeclaration9 *dx9Decl = NULL;
  810. if ( decl )
  811. dx9Decl = static_cast<const D3D9VertexDecl*>( decl )->decl;
  812. D3D9Assert( mD3DDevice->SetVertexDeclaration( dx9Decl ), "GFXD3D9Device::setVertexDecl - Failed to set vertex declaration." );
  813. }
  814. //-----------------------------------------------------------------------------
  815. // This function should ONLY be called from GFXDevice::updateStates() !!!
  816. //-----------------------------------------------------------------------------
  817. void GFXD3D9Device::setTextureInternal( U32 textureUnit, const GFXTextureObject *texture)
  818. {
  819. if( texture == NULL )
  820. {
  821. D3D9Assert(mD3DDevice->SetTexture( textureUnit, NULL ), "Failed to set texture to null!");
  822. return;
  823. }
  824. GFXD3D9TextureObject *tex = (GFXD3D9TextureObject *) texture;
  825. D3D9Assert(mD3DDevice->SetTexture( textureUnit, tex->getTex()), "Failed to set texture to valid value!");
  826. }
  827. //-----------------------------------------------------------------------------
  828. // This function should ONLY be called from GFXDevice::updateStates() !!!
  829. //-----------------------------------------------------------------------------
  830. void GFXD3D9Device::setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable)
  831. {
  832. #ifndef TORQUE_OS_XENON
  833. if(!lightEnable)
  834. {
  835. mD3DDevice->LightEnable(lightStage, false);
  836. return;
  837. }
  838. D3DLIGHT9 d3dLight;
  839. switch (light.mType)
  840. {
  841. case GFXLightInfo::Ambient:
  842. AssertFatal(false, "Instead of setting an ambient light you should set the global ambient color.");
  843. return;
  844. case GFXLightInfo::Vector:
  845. d3dLight.Type = D3DLIGHT_DIRECTIONAL;
  846. break;
  847. case GFXLightInfo::Point:
  848. d3dLight.Type = D3DLIGHT_POINT;
  849. break;
  850. case GFXLightInfo::Spot:
  851. d3dLight.Type = D3DLIGHT_SPOT;
  852. break;
  853. default :
  854. AssertFatal(false, "Unknown light type!");
  855. };
  856. dMemcpy(&d3dLight.Diffuse, &light.mColor, sizeof(light.mColor));
  857. dMemcpy(&d3dLight.Ambient, &light.mAmbient, sizeof(light.mAmbient));
  858. dMemcpy(&d3dLight.Specular, &light.mColor, sizeof(light.mColor));
  859. dMemcpy(&d3dLight.Position, &light.mPos, sizeof(light.mPos));
  860. dMemcpy(&d3dLight.Direction, &light.mDirection, sizeof(light.mDirection));
  861. d3dLight.Range = light.mRadius;
  862. d3dLight.Falloff = 1.0;
  863. d3dLight.Attenuation0 = 1.0f;
  864. d3dLight.Attenuation1 = 0.1f;
  865. d3dLight.Attenuation2 = 0.0f;
  866. d3dLight.Theta = light.mInnerConeAngle;
  867. d3dLight.Phi = light.mOuterConeAngle;
  868. mD3DDevice->SetLight(lightStage, &d3dLight);
  869. mD3DDevice->LightEnable(lightStage, true);
  870. #endif
  871. }
  872. void GFXD3D9Device::setLightMaterialInternal(const GFXLightMaterial mat)
  873. {
  874. #ifndef TORQUE_OS_XENON
  875. D3DMATERIAL9 d3dmat;
  876. dMemset(&d3dmat, 0, sizeof(D3DMATERIAL9));
  877. D3DCOLORVALUE col;
  878. col.r = mat.ambient.red;
  879. col.g = mat.ambient.green;
  880. col.b = mat.ambient.blue;
  881. col.a = mat.ambient.alpha;
  882. d3dmat.Ambient = col;
  883. col.r = mat.diffuse.red;
  884. col.g = mat.diffuse.green;
  885. col.b = mat.diffuse.blue;
  886. col.a = mat.diffuse.alpha;
  887. d3dmat.Diffuse = col;
  888. col.r = mat.specular.red;
  889. col.g = mat.specular.green;
  890. col.b = mat.specular.blue;
  891. col.a = mat.specular.alpha;
  892. d3dmat.Specular = col;
  893. col.r = mat.emissive.red;
  894. col.g = mat.emissive.green;
  895. col.b = mat.emissive.blue;
  896. col.a = mat.emissive.alpha;
  897. d3dmat.Emissive = col;
  898. d3dmat.Power = mat.shininess;
  899. mD3DDevice->SetMaterial(&d3dmat);
  900. #endif
  901. }
  902. void GFXD3D9Device::setGlobalAmbientInternal(ColorF color)
  903. {
  904. #ifndef TORQUE_OS_XENON
  905. mD3DDevice->SetRenderState(D3DRS_AMBIENT,
  906. D3DCOLOR_COLORVALUE(color.red, color.green, color.blue, color.alpha));
  907. #endif
  908. }
  909. //------------------------------------------------------------------------------
  910. // Check for texture mis-match between GFX internal state and what is on the card
  911. // This function is expensive because of the readbacks from DX, and additionally
  912. // won't work unless it's a non-pure device.
  913. //
  914. // This function can crash or give false positives when the game
  915. // is shutting down or returning to the main menu as some of the textures
  916. // present in the mCurrentTexture array will have been freed.
  917. //
  918. // This function is best used as a quick check for mismatched state when it is
  919. // suspected.
  920. //------------------------------------------------------------------------------
  921. void GFXD3D9Device::doParanoidStateCheck()
  922. {
  923. #ifdef TORQUE_DEBUG
  924. // Read back all states and make sure they match what we think they should be.
  925. // For now just do texture binds.
  926. for(U32 i = 0; i < getNumSamplers(); i++)
  927. {
  928. IDirect3DBaseTexture9 *b=NULL;
  929. getDevice()->GetTexture(i, &b);
  930. if ((mCurrentTexture[i].isNull()) && (mCurrentCubemap[i].isNull()))
  931. {
  932. AssertFatal(b == NULL, "GFXD3D9Device::doParanoidStateCheck - got non-null texture in expected NULL slot!");
  933. getDevice()->SetTexture(i, NULL);
  934. }
  935. else
  936. {
  937. AssertFatal(mCurrentTexture[i] || mCurrentCubemap[i], "GFXD3D9Device::doParanoidStateCheck - got null texture in expected non-null slot!");
  938. if (mCurrentCubemap[i])
  939. {
  940. IDirect3DCubeTexture9 *cur= static_cast<GFXD3D9Cubemap*>(mCurrentCubemap[i].getPointer())->mCubeTex;
  941. AssertFatal(cur == b, "GFXD3D9Device::doParanoidStateCheck - mismatched cubemap!");
  942. }
  943. else
  944. {
  945. IDirect3DBaseTexture9 *cur= static_cast<GFXD3D9TextureObject*>(mCurrentTexture[i].getPointer())->getTex();
  946. AssertFatal(cur == b, "GFXD3D9Device::doParanoidStateCheck - mismatched 2d texture!");
  947. }
  948. }
  949. SAFE_RELEASE(b);
  950. }
  951. #endif
  952. }
  953. GFXFence *GFXD3D9Device::createFence()
  954. {
  955. // Figure out what fence type we should be making if we don't know
  956. if( mCreateFenceType == -1 )
  957. {
  958. IDirect3DQuery9 *testQuery = NULL;
  959. mCreateFenceType = ( mD3DDevice->CreateQuery( D3DQUERYTYPE_EVENT, &testQuery ) == D3DERR_NOTAVAILABLE );
  960. SAFE_RELEASE( testQuery );
  961. }
  962. // Cool, use queries
  963. if( !mCreateFenceType )
  964. {
  965. GFXFence* fence = new GFXD3D9QueryFence( this );
  966. fence->registerResourceWithDevice(this);
  967. return fence;
  968. }
  969. // CodeReview: At some point I would like a specialized D3D9 implementation of
  970. // the method used by the general fence, only without the overhead incurred
  971. // by using the GFX constructs. Primarily the lock() method on texture handles
  972. // will do a data copy, and this method doesn't require a copy, just a lock
  973. // [5/10/2007 Pat]
  974. GFXFence* fence = new GFXGeneralFence( this );
  975. fence->registerResourceWithDevice(this);
  976. return fence;
  977. }
  978. GFXOcclusionQuery* GFXD3D9Device::createOcclusionQuery()
  979. {
  980. GFXOcclusionQuery *query;
  981. if (mOcclusionQuerySupported)
  982. query = new GFXD3D9OcclusionQuery( this );
  983. else
  984. return NULL;
  985. query->registerResourceWithDevice(this);
  986. return query;
  987. }
  988. GFXCubemap * GFXD3D9Device::createCubemap()
  989. {
  990. GFXD3D9Cubemap* cube = new GFXD3D9Cubemap();
  991. cube->registerResourceWithDevice(this);
  992. return cube;
  993. }