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