gfxDevice.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 "platform/platform.h"
  23. #include "gfx/gfxDevice.h"
  24. #include "gfx/gfxInit.h"
  25. #include "gfx/gfxCubemap.h"
  26. #include "gfx/primBuilder.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gfx/gfxFence.h"
  29. #include "gfx/gfxFontRenderBatcher.h"
  30. #include "gfx/gfxPrimitiveBuffer.h"
  31. #include "gfx/gfxShader.h"
  32. #include "gfx/gfxStateBlock.h"
  33. #include "gfx/screenshot.h"
  34. #include "gfx/gfxStringEnumTranslate.h"
  35. #include "gfx/gfxTextureManager.h"
  36. #include "core/frameAllocator.h"
  37. #include "core/stream/fileStream.h"
  38. #include "core/strings/unicode.h"
  39. #include "core/util/journal/process.h"
  40. #include "core/util/safeDelete.h"
  41. #include "math/util/frustum.h"
  42. #include "console/consoleTypes.h"
  43. #include "console/engineAPI.h"
  44. GFXDevice * GFXDevice::smGFXDevice = NULL;
  45. bool GFXDevice::smWireframe = false;
  46. bool GFXDevice::smDisableVSync = true;
  47. F32 GFXDevice::smForcedPixVersion = -1.0f;
  48. bool GFXDevice::smDisableOcclusionQuery = false;
  49. bool gDisassembleAllShaders = false;
  50. void GFXDevice::initConsole()
  51. {
  52. GFXStringEnumTranslate::init();
  53. Con::addVariable( "$gfx::wireframe", TypeBool, &smWireframe,
  54. "Used to toggle wireframe rendering at runtime.\n"
  55. "@ingroup GFX\n" );
  56. Con::addVariable( "$gfx::disassembleAllShaders", TypeBool, &gDisassembleAllShaders,
  57. "On supported devices this will dump shader disassembly to the "
  58. "procedural shader folder.\n"
  59. "@ingroup GFX\n" );
  60. Con::addVariable( "$gfx::disableOcclusionQuery", TypeBool, &smDisableOcclusionQuery,
  61. "Debug helper that disables all hardware occlusion queries causing "
  62. "them to return only the visibile state.\n"
  63. "@ingroup GFX\n" );
  64. Con::addVariable( "$pref::Video::disableVerticalSync", TypeBool, &smDisableVSync,
  65. "Disables vertical sync on the active device.\n"
  66. "@note The video mode must be reset for the change to take affect.\n"
  67. "@ingroup GFX\n" );
  68. Con::addVariable( "$pref::Video::forcedPixVersion", TypeF32, &smForcedPixVersion,
  69. "Will force the shader model if the value is positive and less than the "
  70. "shader model supported by the active device. Use 0 for fixed function.\n"
  71. "@note The graphics device must be reset for the change to take affect.\n"
  72. "@ingroup GFX\n" );
  73. }
  74. GFXDevice::DeviceEventSignal& GFXDevice::getDeviceEventSignal()
  75. {
  76. static DeviceEventSignal theSignal;
  77. return theSignal;
  78. }
  79. GFXDevice::GFXDevice()
  80. {
  81. VECTOR_SET_ASSOCIATION( mVideoModes );
  82. VECTOR_SET_ASSOCIATION( mRTStack );
  83. mWorldMatrixDirty = false;
  84. mWorldStackSize = 0;
  85. mProjectionMatrixDirty = false;
  86. mViewMatrixDirty = false;
  87. mTextureMatrixCheckDirty = false;
  88. mViewMatrix.identity();
  89. mProjectionMatrix.identity();
  90. for( S32 i = 0; i < WORLD_STACK_MAX; i++ )
  91. mWorldMatrix[i].identity();
  92. AssertFatal(smGFXDevice == NULL, "Already a GFXDevice created! Bad!");
  93. smGFXDevice = this;
  94. // Vertex buffer cache
  95. mCurrVertexDecl = NULL;
  96. mVertexDeclDirty = false;
  97. for ( U32 i=0; i < VERTEX_STREAM_COUNT; i++ )
  98. {
  99. mVertexBufferDirty[i] = false;
  100. mVertexBufferFrequency[i] = 0;
  101. mVertexBufferFrequencyDirty[i] = false;
  102. }
  103. // Primitive buffer cache
  104. mPrimitiveBufferDirty = false;
  105. mTexturesDirty = false;
  106. // Use of TEXTURE_STAGE_COUNT in initialization is okay [7/2/2007 Pat]
  107. for(U32 i = 0; i < TEXTURE_STAGE_COUNT; i++)
  108. {
  109. mTextureDirty[i] = false;
  110. mCurrentTexture[i] = NULL;
  111. mNewTexture[i] = NULL;
  112. mCurrentCubemap[i] = NULL;
  113. mNewCubemap[i] = NULL;
  114. mTexType[i] = GFXTDT_Normal;
  115. mTextureMatrix[i].identity();
  116. mTextureMatrixDirty[i] = false;
  117. }
  118. mLightsDirty = false;
  119. for(U32 i = 0; i < LIGHT_STAGE_COUNT; i++)
  120. {
  121. mLightDirty[i] = false;
  122. mCurrentLightEnable[i] = false;
  123. }
  124. mGlobalAmbientColorDirty = false;
  125. mGlobalAmbientColor = ColorF(0.0f, 0.0f, 0.0f, 1.0f);
  126. mLightMaterialDirty = false;
  127. dMemset(&mCurrentLightMaterial, 0, sizeof(GFXLightMaterial));
  128. // State block
  129. mStateBlockDirty = false;
  130. mCurrentStateBlock = NULL;
  131. mNewStateBlock = NULL;
  132. mCurrentShaderConstBuffer = NULL;
  133. // misc
  134. mAllowRender = true;
  135. mCurrentRenderStyle = RS_Standard;
  136. mCurrentStereoTarget = -1;
  137. mStereoHeadTransform = MatrixF(1);
  138. mCanCurrentlyRender = false;
  139. mInitialized = false;
  140. mRTDirty = false;
  141. mViewport = RectI::Zero;
  142. mViewportDirty = false;
  143. mCurrentFrontBufferIdx = 0;
  144. mDeviceSwizzle32 = NULL;
  145. mDeviceSwizzle24 = NULL;
  146. mResourceListHead = NULL;
  147. mCardProfiler = NULL;
  148. // Initialize our drawing utility.
  149. mDrawer = NULL;
  150. mFrameTime = PlatformTimer::create();
  151. // Add a few system wide shader macros.
  152. GFXShader::addGlobalMacro( "TORQUE", "1" );
  153. GFXShader::addGlobalMacro( "TORQUE_VERSION", String::ToString(getVersionNumber()) );
  154. #if defined TORQUE_OS_WIN
  155. GFXShader::addGlobalMacro( "TORQUE_OS_WIN" );
  156. #elif defined TORQUE_OS_MAC
  157. GFXShader::addGlobalMacro( "TORQUE_OS_MAC" );
  158. #elif defined TORQUE_OS_LINUX
  159. GFXShader::addGlobalMacro( "TORQUE_OS_LINUX" );
  160. #endif
  161. mStereoTargets[0] = NULL;
  162. mStereoTargets[1] = NULL;
  163. }
  164. GFXDrawUtil* GFXDevice::getDrawUtil()
  165. {
  166. if (!mDrawer)
  167. {
  168. mDrawer = new GFXDrawUtil(this);
  169. }
  170. return mDrawer;
  171. }
  172. void GFXDevice::deviceInited()
  173. {
  174. getDeviceEventSignal().trigger(deInit);
  175. mDeviceStatistics.setPrefix("$GFXDeviceStatistics::");
  176. // Initialize the static helper textures.
  177. GBitmap temp( 2, 2, false, GFXFormatR8G8B8A8 );
  178. temp.fill( ColorI::ONE );
  179. GFXTexHandle::ONE.set( &temp, &GFXDefaultStaticDiffuseProfile, false, "GFXTexHandle::ONE" );
  180. temp.fill( ColorI::ZERO );
  181. GFXTexHandle::ZERO.set( &temp, &GFXDefaultStaticDiffuseProfile, false, "GFXTexHandle::ZERO" );
  182. temp.fill( ColorI( 128, 128, 255 ) );
  183. GFXTexHandle::ZUP.set( &temp, &GFXDefaultStaticNormalMapProfile, false, "GFXTexHandle::ZUP" );
  184. }
  185. bool GFXDevice::destroy()
  186. {
  187. // Cleanup the static helper textures.
  188. GFXTexHandle::ONE.free();
  189. GFXTexHandle::ZERO.free();
  190. GFXTexHandle::ZUP.free();
  191. // Make this release its buffer.
  192. PrimBuild::shutdown();
  193. // Let people know we are shutting down
  194. getDeviceEventSignal().trigger(deDestroy);
  195. if(smGFXDevice)
  196. smGFXDevice->preDestroy();
  197. SAFE_DELETE(smGFXDevice);
  198. return true;
  199. }
  200. void GFXDevice::preDestroy()
  201. {
  202. // Delete draw util
  203. SAFE_DELETE( mDrawer );
  204. }
  205. GFXDevice::~GFXDevice()
  206. {
  207. smGFXDevice = NULL;
  208. // Clean up our current buffers.
  209. mCurrentPrimitiveBuffer = NULL;
  210. for ( U32 i=0; i < VERTEX_STREAM_COUNT; i++ )
  211. mCurrentVertexBuffer[i] = NULL;
  212. // Clear out our current texture references
  213. for (U32 i = 0; i < TEXTURE_STAGE_COUNT; i++)
  214. {
  215. mCurrentTexture[i] = NULL;
  216. mNewTexture[i] = NULL;
  217. mCurrentCubemap[i] = NULL;
  218. mNewCubemap[i] = NULL;
  219. }
  220. mCurrentRT = NULL;
  221. // Release all the unreferenced textures in the cache.
  222. mTextureManager->cleanupCache();
  223. // Check for resource leaks
  224. #ifdef TORQUE_DEBUG
  225. AssertFatal( GFXTextureObject::dumpActiveTOs() == 0, "There is a texture object leak, check the log for more details." );
  226. GFXPrimitiveBuffer::dumpActivePBs();
  227. #endif
  228. SAFE_DELETE( mTextureManager );
  229. SAFE_DELETE( mFrameTime );
  230. // Clear out our state block references
  231. mCurrentStateBlocks.clear();
  232. mNewStateBlock = NULL;
  233. mCurrentStateBlock = NULL;
  234. mCurrentShaderConstBuffer = NULL;
  235. /// End Block above BTR
  236. // -- Clear out resource list
  237. // Note: our derived class destructor will have already released resources.
  238. // Clearing this list saves us from having our resources (which are not deleted
  239. // just released) turn around and try to remove themselves from this list.
  240. while (mResourceListHead)
  241. {
  242. GFXResource * head = mResourceListHead;
  243. mResourceListHead = head->mNextResource;
  244. head->mPrevResource = NULL;
  245. head->mNextResource = NULL;
  246. head->mOwningDevice = NULL;
  247. }
  248. }
  249. GFXStateBlockRef GFXDevice::createStateBlock(const GFXStateBlockDesc& desc)
  250. {
  251. PROFILE_SCOPE( GFXDevice_CreateStateBlock );
  252. U32 hashValue = desc.getHashValue();
  253. if (mCurrentStateBlocks[hashValue])
  254. return mCurrentStateBlocks[hashValue];
  255. GFXStateBlockRef result = createStateBlockInternal(desc);
  256. result->registerResourceWithDevice(this);
  257. mCurrentStateBlocks[hashValue] = result;
  258. return result;
  259. }
  260. void GFXDevice::setStateBlock(GFXStateBlock* block)
  261. {
  262. AssertFatal(block, "NULL state block!");
  263. AssertFatal(block->getOwningDevice() == this, "This state doesn't apply to this device!");
  264. if (block != mCurrentStateBlock)
  265. {
  266. mStateDirty = true;
  267. mStateBlockDirty = true;
  268. mNewStateBlock = block;
  269. } else {
  270. mStateBlockDirty = false;
  271. mNewStateBlock = mCurrentStateBlock;
  272. }
  273. }
  274. void GFXDevice::setStateBlockByDesc( const GFXStateBlockDesc &desc )
  275. {
  276. PROFILE_SCOPE( GFXDevice_SetStateBlockByDesc );
  277. GFXStateBlock *block = createStateBlock( desc );
  278. setStateBlock( block );
  279. }
  280. void GFXDevice::setShaderConstBuffer(GFXShaderConstBuffer* buffer)
  281. {
  282. mCurrentShaderConstBuffer = buffer;
  283. }
  284. void GFXDevice::updateStates(bool forceSetAll /*=false*/)
  285. {
  286. PROFILE_SCOPE(GFXDevice_updateStates);
  287. if(forceSetAll)
  288. {
  289. bool rememberToEndScene = false;
  290. if(!canCurrentlyRender())
  291. {
  292. if (!beginScene())
  293. {
  294. AssertFatal(false, "GFXDevice::updateStates: Unable to beginScene!");
  295. }
  296. rememberToEndScene = true;
  297. }
  298. setMatrix( GFXMatrixProjection, mProjectionMatrix );
  299. setMatrix( GFXMatrixWorld, mWorldMatrix[mWorldStackSize] );
  300. setMatrix( GFXMatrixView, mViewMatrix );
  301. setVertexDecl( mCurrVertexDecl );
  302. for ( U32 i=0; i < VERTEX_STREAM_COUNT; i++ )
  303. {
  304. setVertexStream( i, mCurrentVertexBuffer[i] );
  305. setVertexStreamFrequency( i, mVertexBufferFrequency[i] );
  306. }
  307. if( mCurrentPrimitiveBuffer.isValid() ) // This could be NULL when the device is initalizing
  308. mCurrentPrimitiveBuffer->prepare();
  309. /// Stateblocks
  310. if ( mNewStateBlock )
  311. setStateBlockInternal(mNewStateBlock, true);
  312. mCurrentStateBlock = mNewStateBlock;
  313. for(U32 i = 0; i < getNumSamplers(); i++)
  314. {
  315. switch (mTexType[i])
  316. {
  317. case GFXTDT_Normal :
  318. {
  319. mCurrentTexture[i] = mNewTexture[i];
  320. setTextureInternal(i, mCurrentTexture[i]);
  321. }
  322. break;
  323. case GFXTDT_Cube :
  324. {
  325. mCurrentCubemap[i] = mNewCubemap[i];
  326. if (mCurrentCubemap[i])
  327. mCurrentCubemap[i]->setToTexUnit(i);
  328. else
  329. setTextureInternal(i, NULL);
  330. }
  331. break;
  332. default:
  333. AssertFatal(false, "Unknown texture type!");
  334. break;
  335. }
  336. }
  337. // Set our material
  338. setLightMaterialInternal(mCurrentLightMaterial);
  339. // Set our lights
  340. for(U32 i = 0; i < LIGHT_STAGE_COUNT; i++)
  341. {
  342. setLightInternal(i, mCurrentLight[i], mCurrentLightEnable[i]);
  343. }
  344. _updateRenderTargets();
  345. if(rememberToEndScene)
  346. endScene();
  347. return;
  348. }
  349. if (!mStateDirty)
  350. return;
  351. // Normal update logic begins here.
  352. mStateDirty = false;
  353. // Update Projection Matrix
  354. if( mProjectionMatrixDirty )
  355. {
  356. setMatrix( GFXMatrixProjection, mProjectionMatrix );
  357. mProjectionMatrixDirty = false;
  358. }
  359. // Update World Matrix
  360. if( mWorldMatrixDirty )
  361. {
  362. setMatrix( GFXMatrixWorld, mWorldMatrix[mWorldStackSize] );
  363. mWorldMatrixDirty = false;
  364. }
  365. // Update View Matrix
  366. if( mViewMatrixDirty )
  367. {
  368. setMatrix( GFXMatrixView, mViewMatrix );
  369. mViewMatrixDirty = false;
  370. }
  371. if( mTextureMatrixCheckDirty )
  372. {
  373. for( S32 i = 0; i < getNumSamplers(); i++ )
  374. {
  375. if( mTextureMatrixDirty[i] )
  376. {
  377. mTextureMatrixDirty[i] = false;
  378. setMatrix( (GFXMatrixType)(GFXMatrixTexture + i), mTextureMatrix[i] );
  379. }
  380. }
  381. mTextureMatrixCheckDirty = false;
  382. }
  383. // Update the vertex declaration.
  384. if ( mVertexDeclDirty )
  385. {
  386. setVertexDecl( mCurrVertexDecl );
  387. mVertexDeclDirty = false;
  388. }
  389. // Update the vertex buffers.
  390. for ( U32 i=0; i < VERTEX_STREAM_COUNT; i++ )
  391. {
  392. if ( mVertexBufferDirty[i] )
  393. {
  394. setVertexStream( i, mCurrentVertexBuffer[i] );
  395. mVertexBufferDirty[i] = false;
  396. }
  397. if ( mVertexBufferFrequencyDirty[i] )
  398. {
  399. setVertexStreamFrequency( i, mVertexBufferFrequency[i] );
  400. mVertexBufferFrequencyDirty[i] = false;
  401. }
  402. }
  403. // Update primitive buffer
  404. //
  405. // NOTE: It is very important to set the primitive buffer AFTER the vertex buffer
  406. // because in order to draw indexed primitives in DX8, the call to SetIndicies
  407. // needs to include the base vertex offset, and the DX8 GFXDevice relies on
  408. // having mCurrentVB properly assigned before the call to setIndices -patw
  409. if( mPrimitiveBufferDirty )
  410. {
  411. if( mCurrentPrimitiveBuffer.isValid() ) // This could be NULL when the device is initalizing
  412. mCurrentPrimitiveBuffer->prepare();
  413. mPrimitiveBufferDirty = false;
  414. }
  415. // NOTE: With state blocks, it's now important to update state before setting textures
  416. // some devices (e.g. OpenGL) set states on the texture and we need that information before
  417. // the texture is activated.
  418. if (mStateBlockDirty)
  419. {
  420. setStateBlockInternal(mNewStateBlock, false);
  421. mCurrentStateBlock = mNewStateBlock;
  422. mStateBlockDirty = false;
  423. }
  424. _updateRenderTargets();
  425. if( mTexturesDirty )
  426. {
  427. mTexturesDirty = false;
  428. for(U32 i = 0; i < getNumSamplers(); i++)
  429. {
  430. if(!mTextureDirty[i])
  431. continue;
  432. mTextureDirty[i] = false;
  433. switch (mTexType[i])
  434. {
  435. case GFXTDT_Normal :
  436. {
  437. mCurrentTexture[i] = mNewTexture[i];
  438. setTextureInternal(i, mCurrentTexture[i]);
  439. }
  440. break;
  441. case GFXTDT_Cube :
  442. {
  443. mCurrentCubemap[i] = mNewCubemap[i];
  444. if (mCurrentCubemap[i])
  445. mCurrentCubemap[i]->setToTexUnit(i);
  446. else
  447. setTextureInternal(i, NULL);
  448. }
  449. break;
  450. default:
  451. AssertFatal(false, "Unknown texture type!");
  452. break;
  453. }
  454. }
  455. }
  456. // Set light material
  457. if(mLightMaterialDirty)
  458. {
  459. setLightMaterialInternal(mCurrentLightMaterial);
  460. mLightMaterialDirty = false;
  461. }
  462. // Set our lights
  463. if(mLightsDirty)
  464. {
  465. mLightsDirty = false;
  466. for(U32 i = 0; i < LIGHT_STAGE_COUNT; i++)
  467. {
  468. if(!mLightDirty[i])
  469. continue;
  470. mLightDirty[i] = false;
  471. setLightInternal(i, mCurrentLight[i], mCurrentLightEnable[i]);
  472. }
  473. }
  474. _updateRenderTargets();
  475. #ifdef TORQUE_DEBUG_RENDER
  476. doParanoidStateCheck();
  477. #endif
  478. }
  479. void GFXDevice::setPrimitiveBuffer( GFXPrimitiveBuffer *buffer )
  480. {
  481. if( buffer == mCurrentPrimitiveBuffer )
  482. return;
  483. mCurrentPrimitiveBuffer = buffer;
  484. mPrimitiveBufferDirty = true;
  485. mStateDirty = true;
  486. }
  487. void GFXDevice::drawPrimitive( U32 primitiveIndex )
  488. {
  489. AssertFatal( mCurrentPrimitiveBuffer.isValid(), "Trying to call drawPrimitive with no current primitive buffer, call setPrimitiveBuffer()" );
  490. AssertFatal( primitiveIndex < mCurrentPrimitiveBuffer->mPrimitiveCount, "Out of range primitive index.");
  491. drawPrimitive( mCurrentPrimitiveBuffer->mPrimitiveArray[primitiveIndex] );
  492. }
  493. void GFXDevice::drawPrimitive( const GFXPrimitive &prim )
  494. {
  495. // Do NOT add index buffer offset to this call, it will be added by drawIndexedPrimitive
  496. drawIndexedPrimitive( prim.type,
  497. prim.startVertex,
  498. prim.minIndex,
  499. prim.numVertices,
  500. prim.startIndex,
  501. prim.numPrimitives );
  502. }
  503. void GFXDevice::drawPrimitives()
  504. {
  505. AssertFatal( mCurrentPrimitiveBuffer.isValid(), "Trying to call drawPrimitive with no current primitive buffer, call setPrimitiveBuffer()" );
  506. GFXPrimitive *info = NULL;
  507. for( U32 i = 0; i < mCurrentPrimitiveBuffer->mPrimitiveCount; i++ ) {
  508. info = &mCurrentPrimitiveBuffer->mPrimitiveArray[i];
  509. // Do NOT add index buffer offset to this call, it will be added by drawIndexedPrimitive
  510. drawIndexedPrimitive( info->type,
  511. info->startVertex,
  512. info->minIndex,
  513. info->numVertices,
  514. info->startIndex,
  515. info->numPrimitives );
  516. }
  517. }
  518. DefineEngineFunction( getDisplayDeviceList, String, (),,
  519. "Returns a tab-seperated string of the detected devices across all adapters.\n"
  520. "@ingroup GFX\n" )
  521. {
  522. Vector<GFXAdapter*> adapters;
  523. GFXInit::getAdapters(&adapters);
  524. StringBuilder str;
  525. for (S32 i=0; i<adapters.size(); i++)
  526. {
  527. if (i)
  528. str.append( '\t' );
  529. str.append(adapters[i]->mName);
  530. }
  531. return str.end();
  532. }
  533. void GFXDevice::setFrustum( F32 left,
  534. F32 right,
  535. F32 bottom,
  536. F32 top,
  537. F32 nearPlane,
  538. F32 farPlane,
  539. bool bRotate )
  540. {
  541. // store values
  542. mFrustum.set(false, left, right, top, bottom, nearPlane, farPlane);
  543. // compute matrix
  544. MatrixF projection;
  545. mFrustum.getProjectionMatrix(&projection, bRotate);
  546. setProjectionMatrix( projection );
  547. }
  548. void GFXDevice::setFrustum( const Frustum& frust, bool bRotate )
  549. {
  550. // store values
  551. mFrustum = frust;
  552. // compute matrix
  553. MatrixF projection;
  554. mFrustum.getProjectionMatrix(&projection, bRotate);
  555. setProjectionMatrix( projection );
  556. }
  557. void GFXDevice::getFrustum( F32 *left, F32 *right, F32 *bottom, F32 *top, F32 *nearPlane, F32 *farPlane, bool *isOrtho ) const
  558. {
  559. if ( left ) *left = mFrustum.getNearLeft();
  560. if ( right ) *right = mFrustum.getNearRight();
  561. if ( bottom ) *bottom = mFrustum.getNearBottom();
  562. if ( top ) *top = mFrustum.getNearTop();
  563. if ( nearPlane ) *nearPlane = mFrustum.getNearDist();
  564. if ( farPlane ) *farPlane = mFrustum.getFarDist();
  565. if ( isOrtho ) *isOrtho = mFrustum.isOrtho();
  566. }
  567. void GFXDevice::setOrtho( F32 left,
  568. F32 right,
  569. F32 bottom,
  570. F32 top,
  571. F32 nearPlane,
  572. F32 farPlane,
  573. bool doRotate )
  574. {
  575. // store values
  576. mFrustum.set(true, left, right, top, bottom, nearPlane, farPlane);
  577. // compute matrix
  578. MatrixF projection;
  579. mFrustum.getProjectionMatrix(&projection, doRotate);
  580. setProjectionMatrix( projection );
  581. }
  582. Point2F GFXDevice::getWorldToScreenScale() const
  583. {
  584. Point2F scale;
  585. const RectI &viewport = getViewport();
  586. if ( mFrustum.isOrtho() )
  587. scale.set( viewport.extent.x / mFrustum.getWidth(),
  588. viewport.extent.y / mFrustum.getHeight() );
  589. else
  590. scale.set( ( mFrustum.getNearDist() * viewport.extent.x ) / mFrustum.getWidth(),
  591. ( mFrustum.getNearDist() * viewport.extent.y ) / mFrustum.getHeight() );
  592. return scale;
  593. }
  594. //-----------------------------------------------------------------------------
  595. // Set Light
  596. //-----------------------------------------------------------------------------
  597. void GFXDevice::setLight(U32 stage, GFXLightInfo* light)
  598. {
  599. AssertFatal(stage < LIGHT_STAGE_COUNT, "GFXDevice::setLight - out of range stage!");
  600. if(!mLightDirty[stage])
  601. {
  602. mStateDirty = true;
  603. mLightsDirty = true;
  604. mLightDirty[stage] = true;
  605. }
  606. mCurrentLightEnable[stage] = (light != NULL);
  607. if(mCurrentLightEnable[stage])
  608. mCurrentLight[stage] = *light;
  609. }
  610. //-----------------------------------------------------------------------------
  611. // Set Light Material
  612. //-----------------------------------------------------------------------------
  613. void GFXDevice::setLightMaterial(const GFXLightMaterial& mat)
  614. {
  615. mCurrentLightMaterial = mat;
  616. mLightMaterialDirty = true;
  617. mStateDirty = true;
  618. }
  619. void GFXDevice::setGlobalAmbientColor(const ColorF& color)
  620. {
  621. if(mGlobalAmbientColor != color)
  622. {
  623. mGlobalAmbientColor = color;
  624. mGlobalAmbientColorDirty = true;
  625. }
  626. }
  627. //-----------------------------------------------------------------------------
  628. // Set texture
  629. //-----------------------------------------------------------------------------
  630. void GFXDevice::setTexture( U32 stage, GFXTextureObject *texture )
  631. {
  632. AssertFatal(stage < getNumSamplers(), "GFXDevice::setTexture - out of range stage!");
  633. if ( mTexType[stage] == GFXTDT_Normal &&
  634. ( ( mTextureDirty[stage] && mNewTexture[stage].getPointer() == texture ) ||
  635. ( !mTextureDirty[stage] && mCurrentTexture[stage].getPointer() == texture ) ) )
  636. return;
  637. mStateDirty = true;
  638. mTexturesDirty = true;
  639. mTextureDirty[stage] = true;
  640. mNewTexture[stage] = texture;
  641. mTexType[stage] = GFXTDT_Normal;
  642. // Clear out the cubemaps
  643. mNewCubemap[stage] = NULL;
  644. mCurrentCubemap[stage] = NULL;
  645. }
  646. //-----------------------------------------------------------------------------
  647. // Set cube texture
  648. //-----------------------------------------------------------------------------
  649. void GFXDevice::setCubeTexture( U32 stage, GFXCubemap *texture )
  650. {
  651. AssertFatal(stage < getNumSamplers(), "GFXDevice::setTexture - out of range stage!");
  652. if ( mTexType[stage] == GFXTDT_Cube &&
  653. ( ( mTextureDirty[stage] && mNewCubemap[stage].getPointer() == texture ) ||
  654. ( !mTextureDirty[stage] && mCurrentCubemap[stage].getPointer() == texture ) ) )
  655. return;
  656. mStateDirty = true;
  657. mTexturesDirty = true;
  658. mTextureDirty[stage] = true;
  659. mNewCubemap[stage] = texture;
  660. mTexType[stage] = GFXTDT_Cube;
  661. // Clear out the normal textures
  662. mNewTexture[stage] = NULL;
  663. mCurrentTexture[stage] = NULL;
  664. }
  665. //------------------------------------------------------------------------------
  666. inline bool GFXDevice::beginScene()
  667. {
  668. AssertFatal( mCanCurrentlyRender == false, "GFXDevice::beginScene() - The scene has already begun!" );
  669. mDeviceStatistics.clear();
  670. // Send the start of frame signal.
  671. getDeviceEventSignal().trigger( GFXDevice::deStartOfFrame );
  672. mFrameTime->reset();
  673. return beginSceneInternal();
  674. }
  675. inline void GFXDevice::endScene()
  676. {
  677. AssertFatal( mCanCurrentlyRender == true, "GFXDevice::endScene() - The scene has already ended!" );
  678. // End frame signal
  679. getDeviceEventSignal().trigger( GFXDevice::deEndOfFrame );
  680. endSceneInternal();
  681. mDeviceStatistics.exportToConsole();
  682. }
  683. inline void GFXDevice::beginField()
  684. {
  685. AssertFatal( mCanCurrentlyRender == true, "GFXDevice::beginField() - The scene has not yet begun!" );
  686. // Send the start of field signal.
  687. getDeviceEventSignal().trigger( GFXDevice::deStartOfField );
  688. }
  689. inline void GFXDevice::endField()
  690. {
  691. AssertFatal( mCanCurrentlyRender == true, "GFXDevice::endField() - The scene has not yet begun!" );
  692. // Send the end of field signal.
  693. getDeviceEventSignal().trigger( GFXDevice::deEndOfField );
  694. }
  695. void GFXDevice::setViewport( const RectI &inRect )
  696. {
  697. // Clip the rect against the renderable size.
  698. Point2I size = mCurrentRT->getSize();
  699. RectI maxRect(Point2I(0,0), size);
  700. RectI rect = inRect;
  701. rect.intersect(maxRect);
  702. if ( mViewport != rect )
  703. {
  704. mViewport = rect;
  705. mViewportDirty = true;
  706. }
  707. }
  708. void GFXDevice::pushActiveRenderTarget()
  709. {
  710. // Push the current target on to the stack.
  711. mRTStack.push_back( mCurrentRT );
  712. }
  713. void GFXDevice::popActiveRenderTarget()
  714. {
  715. AssertFatal( mRTStack.size() > 0, "GFXDevice::popActiveRenderTarget() - stack is empty!" );
  716. // Restore the last item on the stack and pop.
  717. setActiveRenderTarget( mRTStack.last() );
  718. mRTStack.pop_back();
  719. }
  720. void GFXDevice::setActiveRenderTarget( GFXTarget *target, bool updateViewport )
  721. {
  722. AssertFatal( target,
  723. "GFXDevice::setActiveRenderTarget - must specify a render target!" );
  724. if ( target == mCurrentRT )
  725. return;
  726. // If we're not dirty then store the
  727. // current RT for deactivation later.
  728. if ( !mRTDirty )
  729. {
  730. // Deactivate the target queued for deactivation
  731. if(mRTDeactivate)
  732. mRTDeactivate->deactivate();
  733. mRTDeactivate = mCurrentRT;
  734. }
  735. mRTDirty = true;
  736. mCurrentRT = target;
  737. // When a target changes we also change the viewport
  738. // to match it. This causes problems when the viewport
  739. // has been modified for clipping to a GUI bounds.
  740. //
  741. // We should consider removing this and making it the
  742. // responsibility of the caller to set a proper viewport
  743. // when the target is changed.
  744. if ( updateViewport )
  745. {
  746. setViewport( RectI( Point2I::Zero, mCurrentRT->getSize() ) );
  747. }
  748. }
  749. /// Helper class for GFXDevice::describeResources.
  750. class DescriptionOutputter
  751. {
  752. /// Are we writing to a file?
  753. bool mWriteToFile;
  754. /// File if we are writing to a file
  755. FileStream mFile;
  756. public:
  757. DescriptionOutputter(const char* file)
  758. {
  759. mWriteToFile = false;
  760. // If we've been given what could be a valid file path, open it.
  761. if(file && file[0] != '\0')
  762. {
  763. mWriteToFile = mFile.open(file, Torque::FS::File::Write);
  764. // Note that it is safe to retry. If this is hit, we'll just write to the console instead of to the file.
  765. AssertFatal(mWriteToFile, avar("DescriptionOutputter::DescriptionOutputter - could not open file %s", file));
  766. }
  767. }
  768. ~DescriptionOutputter()
  769. {
  770. // Close the file
  771. if(mWriteToFile)
  772. mFile.close();
  773. }
  774. /// Writes line to the file or to the console, depending on what we want.
  775. void write(const char* line)
  776. {
  777. if(mWriteToFile)
  778. mFile.writeLine((const U8*)line);
  779. else
  780. Con::printf(line);
  781. }
  782. };
  783. #ifndef TORQUE_SHIPPING
  784. void GFXDevice::dumpStates( const char *fileName ) const
  785. {
  786. DescriptionOutputter output(fileName);
  787. output.write("Current state");
  788. if (!mCurrentStateBlock.isNull())
  789. output.write(mCurrentStateBlock->getDesc().describeSelf().c_str());
  790. else
  791. output.write("No state!");
  792. output.write("\nAll states:\n");
  793. GFXResource* walk = mResourceListHead;
  794. while(walk)
  795. {
  796. const GFXStateBlock* sb = dynamic_cast<const GFXStateBlock*>(walk);
  797. if (sb)
  798. {
  799. output.write(sb->getDesc().describeSelf().c_str());
  800. }
  801. walk = walk->getNextResource();
  802. }
  803. }
  804. #endif
  805. void GFXDevice::listResources(bool unflaggedOnly)
  806. {
  807. U32 numTextures = 0, numShaders = 0, numRenderToTextureTargs = 0, numWindowTargs = 0;
  808. U32 numCubemaps = 0, numVertexBuffers = 0, numPrimitiveBuffers = 0, numFences = 0;
  809. U32 numStateBlocks = 0;
  810. GFXResource* walk = mResourceListHead;
  811. while(walk)
  812. {
  813. if(unflaggedOnly && walk->isFlagged())
  814. {
  815. walk = walk->getNextResource();
  816. continue;
  817. }
  818. if(dynamic_cast<GFXTextureObject*>(walk))
  819. numTextures++;
  820. else if(dynamic_cast<GFXShader*>(walk))
  821. numShaders++;
  822. else if(dynamic_cast<GFXTextureTarget*>(walk))
  823. numRenderToTextureTargs++;
  824. else if(dynamic_cast<GFXWindowTarget*>(walk))
  825. numWindowTargs++;
  826. else if(dynamic_cast<GFXCubemap*>(walk))
  827. numCubemaps++;
  828. else if(dynamic_cast<GFXVertexBuffer*>(walk))
  829. numVertexBuffers++;
  830. else if(dynamic_cast<GFXPrimitiveBuffer*>(walk))
  831. numPrimitiveBuffers++;
  832. else if(dynamic_cast<GFXFence*>(walk))
  833. numFences++;
  834. else if (dynamic_cast<GFXStateBlock*>(walk))
  835. numStateBlocks++;
  836. else
  837. Con::warnf("Unknown resource: %x", walk);
  838. walk = walk->getNextResource();
  839. }
  840. const char* flag = unflaggedOnly ? "unflagged" : "allocated";
  841. Con::printf("GFX currently has:");
  842. Con::printf(" %i %s textures", numTextures, flag);
  843. Con::printf(" %i %s shaders", numShaders, flag);
  844. Con::printf(" %i %s texture targets", numRenderToTextureTargs, flag);
  845. Con::printf(" %i %s window targets", numWindowTargs, flag);
  846. Con::printf(" %i %s cubemaps", numCubemaps, flag);
  847. Con::printf(" %i %s vertex buffers", numVertexBuffers, flag);
  848. Con::printf(" %i %s primitive buffers", numPrimitiveBuffers, flag);
  849. Con::printf(" %i %s fences", numFences, flag);
  850. Con::printf(" %i %s state blocks", numStateBlocks, flag);
  851. }
  852. void GFXDevice::fillResourceVectors(const char* resNames, bool unflaggedOnly, Vector<GFXResource*> &textureObjects,
  853. Vector<GFXResource*> &textureTargets, Vector<GFXResource*> &windowTargets, Vector<GFXResource*> &vertexBuffers,
  854. Vector<GFXResource*> &primitiveBuffers, Vector<GFXResource*> &fences, Vector<GFXResource*> &cubemaps,
  855. Vector<GFXResource*> &shaders, Vector<GFXResource*> &stateblocks)
  856. {
  857. bool describeTexture = true, describeTextureTarget = true, describeWindowTarget = true, describeVertexBuffer = true,
  858. describePrimitiveBuffer = true, describeFence = true, describeCubemap = true, describeShader = true,
  859. describeStateBlock = true;
  860. // If we didn't specify a string of names, we'll print all of them
  861. if(resNames && resNames[0] != '\0')
  862. {
  863. // If we did specify a string of names, determine which names
  864. describeTexture = (dStrstr(resNames, "GFXTextureObject") != NULL);
  865. describeTextureTarget = (dStrstr(resNames, "GFXTextureTarget") != NULL);
  866. describeWindowTarget = (dStrstr(resNames, "GFXWindowTarget") != NULL);
  867. describeVertexBuffer = (dStrstr(resNames, "GFXVertexBuffer") != NULL);
  868. describePrimitiveBuffer = (dStrstr(resNames, "GFXPrimitiveBuffer") != NULL);
  869. describeFence = (dStrstr(resNames, "GFXFence") != NULL);
  870. describeCubemap = (dStrstr(resNames, "GFXCubemap") != NULL);
  871. describeShader = (dStrstr(resNames, "GFXShader") != NULL);
  872. describeStateBlock = (dStrstr(resNames, "GFXStateBlock") != NULL);
  873. }
  874. // Start going through the list
  875. GFXResource* walk = mResourceListHead;
  876. while(walk)
  877. {
  878. // If we only want unflagged resources, skip all flagged resources
  879. if(unflaggedOnly && walk->isFlagged())
  880. {
  881. walk = walk->getNextResource();
  882. continue;
  883. }
  884. // All of the following checks go through the same logic.
  885. // if(describingThisResource)
  886. // {
  887. // ResourceType* type = dynamic_cast<ResourceType*>(walk)
  888. // if(type)
  889. // {
  890. // typeVector.push_back(type);
  891. // walk = walk->getNextResource();
  892. // continue;
  893. // }
  894. // }
  895. if(describeTexture)
  896. {
  897. GFXTextureObject* tex = dynamic_cast<GFXTextureObject*>(walk);
  898. {
  899. if(tex)
  900. {
  901. textureObjects.push_back(tex);
  902. walk = walk->getNextResource();
  903. continue;
  904. }
  905. }
  906. }
  907. if(describeShader)
  908. {
  909. GFXShader* shd = dynamic_cast<GFXShader*>(walk);
  910. if(shd)
  911. {
  912. shaders.push_back(shd);
  913. walk = walk->getNextResource();
  914. continue;
  915. }
  916. }
  917. if(describeVertexBuffer)
  918. {
  919. GFXVertexBuffer* buf = dynamic_cast<GFXVertexBuffer*>(walk);
  920. if(buf)
  921. {
  922. vertexBuffers.push_back(buf);
  923. walk = walk->getNextResource();
  924. continue;
  925. }
  926. }
  927. if(describePrimitiveBuffer)
  928. {
  929. GFXPrimitiveBuffer* buf = dynamic_cast<GFXPrimitiveBuffer*>(walk);
  930. if(buf)
  931. {
  932. primitiveBuffers.push_back(buf);
  933. walk = walk->getNextResource();
  934. continue;
  935. }
  936. }
  937. if(describeTextureTarget)
  938. {
  939. GFXTextureTarget* targ = dynamic_cast<GFXTextureTarget*>(walk);
  940. if(targ)
  941. {
  942. textureTargets.push_back(targ);
  943. walk = walk->getNextResource();
  944. continue;
  945. }
  946. }
  947. if(describeWindowTarget)
  948. {
  949. GFXWindowTarget* targ = dynamic_cast<GFXWindowTarget*>(walk);
  950. if(targ)
  951. {
  952. windowTargets.push_back(targ);
  953. walk = walk->getNextResource();
  954. continue;
  955. }
  956. }
  957. if(describeCubemap)
  958. {
  959. GFXCubemap* cube = dynamic_cast<GFXCubemap*>(walk);
  960. if(cube)
  961. {
  962. cubemaps.push_back(cube);
  963. walk = walk->getNextResource();
  964. continue;
  965. }
  966. }
  967. if(describeFence)
  968. {
  969. GFXFence* fence = dynamic_cast<GFXFence*>(walk);
  970. if(fence)
  971. {
  972. fences.push_back(fence);
  973. walk = walk->getNextResource();
  974. continue;
  975. }
  976. }
  977. if (describeStateBlock)
  978. {
  979. GFXStateBlock* sb = dynamic_cast<GFXStateBlock*>(walk);
  980. if (sb)
  981. {
  982. stateblocks.push_back(sb);
  983. walk = walk->getNextResource();
  984. continue;
  985. }
  986. }
  987. // Wasn't something we were looking for
  988. walk = walk->getNextResource();
  989. }
  990. }
  991. void GFXDevice::describeResources(const char* resNames, const char* filePath, bool unflaggedOnly)
  992. {
  993. const U32 numResourceTypes = 9;
  994. Vector<GFXResource*> resVectors[numResourceTypes];
  995. const char* reslabels[numResourceTypes] = { "texture", "texture target", "window target", "vertex buffers", "primitive buffers", "fences", "cubemaps", "shaders", "stateblocks" };
  996. // Fill the vectors with the right resources
  997. fillResourceVectors(resNames, unflaggedOnly, resVectors[0], resVectors[1], resVectors[2], resVectors[3],
  998. resVectors[4], resVectors[5], resVectors[6], resVectors[7], resVectors[8]);
  999. // Helper object
  1000. DescriptionOutputter output(filePath);
  1001. // Print the info to the file
  1002. // Note that we check if we have any objects of that type.
  1003. for (U32 i = 0; i < numResourceTypes; i++)
  1004. {
  1005. if (resVectors[i].size())
  1006. {
  1007. // Header
  1008. String header = String::ToString("--------Dumping GFX %s descriptions...----------", reslabels[i]);
  1009. output.write(header);
  1010. // Data
  1011. for (U32 j = 0; j < resVectors[i].size(); j++)
  1012. {
  1013. GFXResource* resource = resVectors[i][j];
  1014. String dataline = String::ToString("Addr: %x %s", resource, resource->describeSelf().c_str());
  1015. output.write(dataline.c_str());
  1016. }
  1017. // Footer
  1018. output.write("--------------------Done---------------------");
  1019. output.write("");
  1020. }
  1021. }
  1022. }
  1023. void GFXDevice::flagCurrentResources()
  1024. {
  1025. GFXResource* walk = mResourceListHead;
  1026. while(walk)
  1027. {
  1028. walk->setFlag();
  1029. walk = walk->getNextResource();
  1030. }
  1031. }
  1032. void GFXDevice::clearResourceFlags()
  1033. {
  1034. GFXResource* walk = mResourceListHead;
  1035. while(walk)
  1036. {
  1037. walk->clearFlag();
  1038. walk = walk->getNextResource();
  1039. }
  1040. }
  1041. DefineEngineFunction( listGFXResources, void, ( bool unflaggedOnly ), ( false ),
  1042. "Returns a list of the unflagged GFX resources. See flagCurrentGFXResources for usage details.\n"
  1043. "@ingroup GFX\n"
  1044. "@see flagCurrentGFXResources, clearGFXResourceFlags, describeGFXResources" )
  1045. {
  1046. GFX->listResources(unflaggedOnly);
  1047. }
  1048. DefineEngineFunction( flagCurrentGFXResources, void, (),,
  1049. "@brief Flags all currently allocated GFX resources.\n"
  1050. "Used for resource allocation and leak tracking by flagging "
  1051. "current resources then dumping a list of unflagged resources "
  1052. "at some later point in execution.\n"
  1053. "@ingroup GFX\n"
  1054. "@see listGFXResources, clearGFXResourceFlags, describeGFXResources" )
  1055. {
  1056. GFX->flagCurrentResources();
  1057. }
  1058. DefineEngineFunction( clearGFXResourceFlags, void, (),,
  1059. "Clears the flagged state on all allocated GFX resources. "
  1060. "See flagCurrentGFXResources for usage details.\n"
  1061. "@ingroup GFX\n"
  1062. "@see flagCurrentGFXResources, listGFXResources, describeGFXResources" )
  1063. {
  1064. GFX->clearResourceFlags();
  1065. }
  1066. DefineEngineFunction( describeGFXResources, void, ( const char *resourceTypes, const char *filePath, bool unflaggedOnly ), ( false ),
  1067. "@brief Dumps a description of GFX resources to a file or the console.\n"
  1068. "@param resourceTypes A space seperated list of resource types or an empty string for all resources.\n"
  1069. "@param filePath A file to dump the list to or an empty string to write to the console.\n"
  1070. "@param unflaggedOnly If true only unflagged resources are dumped. See flagCurrentGFXResources.\n"
  1071. "@note The resource types can be one or more of the following:\n\n"
  1072. " - texture\n"
  1073. " - texture target\n"
  1074. " - window target\n"
  1075. " - vertex buffers\n"
  1076. " - primitive buffers\n"
  1077. " - fences\n"
  1078. " - cubemaps\n"
  1079. " - shaders\n"
  1080. " - stateblocks\n\n"
  1081. "@ingroup GFX\n" )
  1082. {
  1083. GFX->describeResources( resourceTypes, filePath, unflaggedOnly );
  1084. }
  1085. DefineEngineFunction( describeGFXStateBlocks, void, ( const char *filePath ),,
  1086. "Dumps a description of all state blocks.\n"
  1087. "@param filePath A file to dump the state blocks to or an empty string to write to the console.\n"
  1088. "@ingroup GFX\n" )
  1089. {
  1090. GFX->dumpStates( filePath );
  1091. }
  1092. DefineEngineFunction( getPixelShaderVersion, F32, (),,
  1093. "Returns the pixel shader version for the active device.\n"
  1094. "@ingroup GFX\n" )
  1095. {
  1096. return GFX->getPixelShaderVersion();
  1097. }
  1098. DefineEngineFunction( setPixelShaderVersion, void, ( F32 version ),,
  1099. "@brief Sets the pixel shader version for the active device.\n"
  1100. "This can be used to force a lower pixel shader version than is supported by "
  1101. "the device for testing or performance optimization.\n"
  1102. "@param version The floating point shader version number.\n"
  1103. "@note This will only affect shaders/materials created after the call "
  1104. "and should be used before the game begins.\n"
  1105. "@see $pref::Video::forcedPixVersion\n"
  1106. "@ingroup GFX\n" )
  1107. {
  1108. GFX->setPixelShaderVersion( version );
  1109. }
  1110. DefineEngineFunction( getDisplayDeviceInformation, const char*, (),,
  1111. "Get the string describing the active GFX device.\n"
  1112. "@ingroup GFX\n" )
  1113. {
  1114. if (!GFXDevice::devicePresent())
  1115. return "(no device)";
  1116. const GFXAdapter& adapter = GFX->getAdapter();
  1117. return adapter.getName();
  1118. }
  1119. DefineEngineFunction( getBestHDRFormat, GFXFormat, (),,
  1120. "Returns the best texture format for storage of HDR data for the active device.\n"
  1121. "@ingroup GFX\n" )
  1122. {
  1123. // TODO: Maybe expose GFX::selectSupportedFormat() so that this
  1124. // specialized method can be moved to script.
  1125. // Figure out the best HDR format. This is the smallest
  1126. // format which supports blending and filtering.
  1127. Vector<GFXFormat> formats;
  1128. //formats.push_back( GFXFormatR10G10B10A2 ); TODO: replace with SRGB format once DX9 is gone - BJR
  1129. formats.push_back( GFXFormatR16G16B16A16F );
  1130. formats.push_back( GFXFormatR16G16B16A16 );
  1131. GFXFormat format = GFX->selectSupportedFormat( &GFXDefaultRenderTargetProfile,
  1132. formats,
  1133. true,
  1134. true,
  1135. true );
  1136. return format;
  1137. }
  1138. DefineConsoleFunction(ResetGFX, void, (), , "forces the gbuffer to be reinitialized in cases of improper/lack of buffer clears.")
  1139. {
  1140. GFX->beginReset();
  1141. }