gfxDevice.cpp 40 KB

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