gfxDevice.cpp 39 KB

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