gfxDevice.cpp 40 KB

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