gfxDevice.cpp 41 KB

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