gfxDevice.cpp 41 KB

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