gfxDevice.cpp 40 KB

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