gfxDevice.cpp 40 KB

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