gfxDevice.cpp 40 KB

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