gfxDevice.cpp 40 KB

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