gfxGLDevice.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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/gl/gfxGLDevice.h"
  24. #include "gfx/gfxCubemap.h"
  25. #include "gfx/screenshot.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "gfx/gl/gfxGLEnumTranslate.h"
  28. #include "gfx/gl/gfxGLVertexBuffer.h"
  29. #include "gfx/gl/gfxGLPrimitiveBuffer.h"
  30. #include "gfx/gl/gfxGLTextureTarget.h"
  31. #include "gfx/gl/gfxGLTextureManager.h"
  32. #include "gfx/gl/gfxGLTextureObject.h"
  33. #include "gfx/gl/gfxGLCubemap.h"
  34. #include "gfx/gl/gfxGLCardProfiler.h"
  35. #include "gfx/gl/gfxGLWindowTarget.h"
  36. #include "gfx/gl/ggl/ggl.h"
  37. #include "platform/platformDlibrary.h"
  38. #include "gfx/gl/gfxGLShader.h"
  39. #include "gfx/primBuilder.h"
  40. #include "console/console.h"
  41. #include "gfx/gl/gfxGLOcclusionQuery.h"
  42. GFXAdapter::CreateDeviceInstanceDelegate GFXGLDevice::mCreateDeviceInstance(GFXGLDevice::createInstance);
  43. GFXDevice *GFXGLDevice::createInstance( U32 adapterIndex )
  44. {
  45. return new GFXGLDevice(adapterIndex);
  46. }
  47. namespace GL
  48. {
  49. extern void gglPerformBinds();
  50. extern void gglPerformExtensionBinds(void *context);
  51. }
  52. void loadGLCore()
  53. {
  54. static bool coreLoaded = false; // Guess what this is for.
  55. if(coreLoaded)
  56. return;
  57. coreLoaded = true;
  58. // Make sure we've got our GL bindings.
  59. GL::gglPerformBinds();
  60. }
  61. void loadGLExtensions(void *context)
  62. {
  63. static bool extensionsLoaded = false;
  64. if(extensionsLoaded)
  65. return;
  66. extensionsLoaded = true;
  67. GL::gglPerformExtensionBinds(context);
  68. }
  69. void GFXGLDevice::initGLState()
  70. {
  71. // We don't currently need to sync device state with a known good place because we are
  72. // going to set everything in GFXGLStateBlock, but if we change our GFXGLStateBlock strategy, this may
  73. // need to happen.
  74. // Deal with the card profiler here when we know we have a valid context.
  75. mCardProfiler = new GFXGLCardProfiler();
  76. mCardProfiler->init();
  77. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, (GLint*)&mMaxShaderTextures);
  78. glGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&mMaxFFTextures);
  79. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  80. // Apple's drivers lie and claim that everything supports fragment shaders. Conveniently they don't lie about the number
  81. // of supported image units. Checking for 16 or more image units ensures that we don't try and use pixel shaders on
  82. // cards which don't support them.
  83. if(mCardProfiler->queryProfile("GL::suppFragmentShader") && mMaxShaderTextures >= 16)
  84. mPixelShaderVersion = 2.0f;
  85. else
  86. mPixelShaderVersion = 0.0f;
  87. // MACHAX - Setting mPixelShaderVersion to 3.0 will allow Advanced Lighting
  88. // to run. At the time of writing (6/18) it doesn't quite work yet.
  89. if(Con::getBoolVariable("$pref::machax::enableAdvancedLighting", false))
  90. mPixelShaderVersion = 3.0f;
  91. mSupportsAnisotropic = mCardProfiler->queryProfile( "GL::suppAnisotropic" );
  92. }
  93. GFXGLDevice::GFXGLDevice(U32 adapterIndex) :
  94. mAdapterIndex(adapterIndex),
  95. mCurrentVB(NULL),
  96. mCurrentPB(NULL),
  97. m_mCurrentWorld(true),
  98. m_mCurrentView(true),
  99. mContext(NULL),
  100. mPixelFormat(NULL),
  101. mPixelShaderVersion(0.0f),
  102. mMaxShaderTextures(2),
  103. mMaxFFTextures(2),
  104. mClip(0, 0, 0, 0)
  105. {
  106. loadGLCore();
  107. GFXGLEnumTranslate::init();
  108. GFXVertexColor::setSwizzle( &Swizzles::rgba );
  109. mDeviceSwizzle32 = &Swizzles::bgra;
  110. mDeviceSwizzle24 = &Swizzles::bgr;
  111. mTextureManager = new GFXGLTextureManager();
  112. gScreenShot = new ScreenShot();
  113. for(U32 i = 0; i < TEXTURE_STAGE_COUNT; i++)
  114. mActiveTextureType[i] = GL_ZERO;
  115. }
  116. GFXGLDevice::~GFXGLDevice()
  117. {
  118. mCurrentStateBlock = NULL;
  119. mCurrentPB = NULL;
  120. mCurrentVB = NULL;
  121. for(U32 i = 0; i < mVolatileVBs.size(); i++)
  122. mVolatileVBs[i] = NULL;
  123. for(U32 i = 0; i < mVolatilePBs.size(); i++)
  124. mVolatilePBs[i] = NULL;
  125. GFXResource* walk = mResourceListHead;
  126. while(walk)
  127. {
  128. walk->zombify();
  129. walk = walk->getNextResource();
  130. }
  131. if( mCardProfiler )
  132. SAFE_DELETE( mCardProfiler );
  133. SAFE_DELETE( gScreenShot );
  134. }
  135. void GFXGLDevice::zombify()
  136. {
  137. mTextureManager->zombify();
  138. if(mCurrentVB)
  139. mCurrentVB->finish();
  140. if(mCurrentPB)
  141. mCurrentPB->finish();
  142. //mVolatileVBs.clear();
  143. //mVolatilePBs.clear();
  144. GFXResource* walk = mResourceListHead;
  145. while(walk)
  146. {
  147. walk->zombify();
  148. walk = walk->getNextResource();
  149. }
  150. }
  151. void GFXGLDevice::resurrect()
  152. {
  153. GFXResource* walk = mResourceListHead;
  154. while(walk)
  155. {
  156. walk->resurrect();
  157. walk = walk->getNextResource();
  158. }
  159. if(mCurrentVB)
  160. mCurrentVB->prepare();
  161. if(mCurrentPB)
  162. mCurrentPB->prepare();
  163. mTextureManager->resurrect();
  164. }
  165. GFXVertexBuffer* GFXGLDevice::findVolatileVBO(U32 numVerts, const GFXVertexFormat *vertexFormat, U32 vertSize)
  166. {
  167. for(U32 i = 0; i < mVolatileVBs.size(); i++)
  168. if ( mVolatileVBs[i]->mNumVerts >= numVerts &&
  169. mVolatileVBs[i]->mVertexFormat.isEqual( *vertexFormat ) &&
  170. mVolatileVBs[i]->mVertexSize == vertSize &&
  171. mVolatileVBs[i]->getRefCount() == 1 )
  172. return mVolatileVBs[i];
  173. // No existing VB, so create one
  174. StrongRefPtr<GFXGLVertexBuffer> buf(new GFXGLVertexBuffer(GFX, numVerts, vertexFormat, vertSize, GFXBufferTypeVolatile));
  175. buf->registerResourceWithDevice(this);
  176. mVolatileVBs.push_back(buf);
  177. return buf.getPointer();
  178. }
  179. GFXPrimitiveBuffer* GFXGLDevice::findVolatilePBO(U32 numIndices, U32 numPrimitives)
  180. {
  181. for(U32 i = 0; i < mVolatilePBs.size(); i++)
  182. if((mVolatilePBs[i]->mIndexCount >= numIndices) && (mVolatilePBs[i]->getRefCount() == 1))
  183. return mVolatilePBs[i];
  184. // No existing PB, so create one
  185. StrongRefPtr<GFXGLPrimitiveBuffer> buf(new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, GFXBufferTypeVolatile));
  186. buf->registerResourceWithDevice(this);
  187. mVolatilePBs.push_back(buf);
  188. return buf.getPointer();
  189. }
  190. GFXVertexBuffer *GFXGLDevice::allocVertexBuffer( U32 numVerts,
  191. const GFXVertexFormat *vertexFormat,
  192. U32 vertSize,
  193. GFXBufferType bufferType )
  194. {
  195. if(bufferType == GFXBufferTypeVolatile)
  196. return findVolatileVBO(numVerts, vertexFormat, vertSize);
  197. GFXGLVertexBuffer* buf = new GFXGLVertexBuffer( GFX, numVerts, vertexFormat, vertSize, bufferType );
  198. buf->registerResourceWithDevice(this);
  199. return buf;
  200. }
  201. GFXPrimitiveBuffer *GFXGLDevice::allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType )
  202. {
  203. if(bufferType == GFXBufferTypeVolatile)
  204. return findVolatilePBO(numIndices, numPrimitives);
  205. GFXGLPrimitiveBuffer* buf = new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
  206. buf->registerResourceWithDevice(this);
  207. return buf;
  208. }
  209. void GFXGLDevice::setVertexStream( U32 stream, GFXVertexBuffer *buffer )
  210. {
  211. AssertFatal( stream == 0, "GFXGLDevice::setVertexStream - We don't support multiple vertex streams!" );
  212. // Reset the state the old VB required, then set the state the new VB requires.
  213. if ( mCurrentVB )
  214. mCurrentVB->finish();
  215. mCurrentVB = static_cast<GFXGLVertexBuffer*>( buffer );
  216. if ( mCurrentVB )
  217. mCurrentVB->prepare();
  218. }
  219. void GFXGLDevice::setVertexStreamFrequency( U32 stream, U32 frequency )
  220. {
  221. // We don't support vertex stream frequency or mesh instancing in OGL yet.
  222. }
  223. GFXCubemap* GFXGLDevice::createCubemap()
  224. {
  225. GFXGLCubemap* cube = new GFXGLCubemap();
  226. cube->registerResourceWithDevice(this);
  227. return cube;
  228. };
  229. void GFXGLDevice::endSceneInternal()
  230. {
  231. // nothing to do for opengl
  232. mCanCurrentlyRender = false;
  233. }
  234. void GFXGLDevice::clear(U32 flags, ColorI color, F32 z, U32 stencil)
  235. {
  236. // Make sure we have flushed our render target state.
  237. _updateRenderTargets();
  238. bool zwrite = true;
  239. if (mCurrentGLStateBlock)
  240. {
  241. zwrite = mCurrentGLStateBlock->getDesc().zWriteEnable;
  242. }
  243. glDepthMask(true);
  244. ColorF c = color;
  245. glClearColor(c.red, c.green, c.blue, c.alpha);
  246. glClearDepth(z);
  247. glClearStencil(stencil);
  248. GLbitfield clearflags = 0;
  249. clearflags |= (flags & GFXClearTarget) ? GL_COLOR_BUFFER_BIT : 0;
  250. clearflags |= (flags & GFXClearZBuffer) ? GL_DEPTH_BUFFER_BIT : 0;
  251. clearflags |= (flags & GFXClearStencil) ? GL_STENCIL_BUFFER_BIT : 0;
  252. glClear(clearflags);
  253. if(!zwrite)
  254. glDepthMask(false);
  255. }
  256. // Given a primitive type and a number of primitives, return the number of indexes/vertexes used.
  257. GLsizei GFXGLDevice::primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount)
  258. {
  259. switch (primType)
  260. {
  261. case GFXPointList :
  262. return primitiveCount;
  263. break;
  264. case GFXLineList :
  265. return primitiveCount * 2;
  266. break;
  267. case GFXLineStrip :
  268. return primitiveCount + 1;
  269. break;
  270. case GFXTriangleList :
  271. return primitiveCount * 3;
  272. break;
  273. case GFXTriangleStrip :
  274. return 2 + primitiveCount;
  275. break;
  276. case GFXTriangleFan :
  277. return 2 + primitiveCount;
  278. break;
  279. default:
  280. AssertFatal(false, "GFXGLDevice::primCountToIndexCount - unrecognized prim type");
  281. break;
  282. }
  283. return 0;
  284. }
  285. inline void GFXGLDevice::preDrawPrimitive()
  286. {
  287. if( mStateDirty )
  288. {
  289. updateStates();
  290. }
  291. if(mCurrentShaderConstBuffer)
  292. setShaderConstBufferInternal(mCurrentShaderConstBuffer);
  293. }
  294. inline void GFXGLDevice::postDrawPrimitive(U32 primitiveCount)
  295. {
  296. mDeviceStatistics.mDrawCalls++;
  297. mDeviceStatistics.mPolyCount += primitiveCount;
  298. }
  299. void GFXGLDevice::drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount )
  300. {
  301. preDrawPrimitive();
  302. // There are some odd performance issues if a buffer is bound to GL_ELEMENT_ARRAY_BUFFER when glDrawArrays is called. Unbinding the buffer
  303. // improves performance by 10%.
  304. if(mCurrentPB)
  305. mCurrentPB->finish();
  306. glDrawArrays(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount));
  307. if(mCurrentPB)
  308. mCurrentPB->prepare();
  309. postDrawPrimitive(primitiveCount);
  310. }
  311. void GFXGLDevice::drawIndexedPrimitive( GFXPrimitiveType primType,
  312. U32 startVertex,
  313. U32 minIndex,
  314. U32 numVerts,
  315. U32 startIndex,
  316. U32 primitiveCount )
  317. {
  318. AssertFatal( startVertex == 0, "GFXGLDevice::drawIndexedPrimitive() - Non-zero startVertex unsupported!" );
  319. preDrawPrimitive();
  320. U16* buf = (U16*)static_cast<GFXGLPrimitiveBuffer*>(mCurrentPrimitiveBuffer.getPointer())->getBuffer() + startIndex;
  321. glDrawElements(GFXGLPrimType[primType], primCountToIndexCount(primType, primitiveCount), GL_UNSIGNED_SHORT, buf);
  322. postDrawPrimitive(primitiveCount);
  323. }
  324. void GFXGLDevice::setPB(GFXGLPrimitiveBuffer* pb)
  325. {
  326. if(mCurrentPB)
  327. mCurrentPB->finish();
  328. mCurrentPB = pb;
  329. }
  330. void GFXGLDevice::setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable)
  331. {
  332. if(!lightEnable)
  333. {
  334. glDisable(GL_LIGHT0 + lightStage);
  335. return;
  336. }
  337. if(light.mType == GFXLightInfo::Ambient)
  338. {
  339. AssertFatal(false, "Instead of setting an ambient light you should set the global ambient color.");
  340. return;
  341. }
  342. GLenum lightEnum = GL_LIGHT0 + lightStage;
  343. glLightfv(lightEnum, GL_AMBIENT, (GLfloat*)&light.mAmbient);
  344. glLightfv(lightEnum, GL_DIFFUSE, (GLfloat*)&light.mColor);
  345. glLightfv(lightEnum, GL_SPECULAR, (GLfloat*)&light.mColor);
  346. F32 pos[4];
  347. if(light.mType != GFXLightInfo::Vector)
  348. {
  349. dMemcpy(pos, &light.mPos, sizeof(light.mPos));
  350. pos[3] = 1.0;
  351. }
  352. else
  353. {
  354. dMemcpy(pos, &light.mDirection, sizeof(light.mDirection));
  355. pos[3] = 0.0;
  356. }
  357. // Harcoded attenuation
  358. glLightf(lightEnum, GL_CONSTANT_ATTENUATION, 1.0f);
  359. glLightf(lightEnum, GL_LINEAR_ATTENUATION, 0.1f);
  360. glLightf(lightEnum, GL_QUADRATIC_ATTENUATION, 0.0f);
  361. glLightfv(lightEnum, GL_POSITION, (GLfloat*)&pos);
  362. glEnable(lightEnum);
  363. }
  364. void GFXGLDevice::setLightMaterialInternal(const GFXLightMaterial mat)
  365. {
  366. // CodeReview - Setting these for front and back is unnecessary. We should consider
  367. // checking what faces we're culling and setting this only for the unculled faces.
  368. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat*)&mat.ambient);
  369. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat*)&mat.diffuse);
  370. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (GLfloat*)&mat.specular);
  371. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, (GLfloat*)&mat.emissive);
  372. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat.shininess);
  373. }
  374. void GFXGLDevice::setGlobalAmbientInternal(ColorF color)
  375. {
  376. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (GLfloat*)&color);
  377. }
  378. void GFXGLDevice::setTextureInternal(U32 textureUnit, const GFXTextureObject*texture)
  379. {
  380. const GFXGLTextureObject *tex = static_cast<const GFXGLTextureObject*>(texture);
  381. glActiveTexture(GL_TEXTURE0 + textureUnit);
  382. if (tex)
  383. {
  384. // GFXGLTextureObject::bind also handles applying the current sampler state.
  385. if(mActiveTextureType[textureUnit] != tex->getBinding() && mActiveTextureType[textureUnit] != GL_ZERO)
  386. {
  387. glBindTexture(mActiveTextureType[textureUnit], 0);
  388. glDisable(mActiveTextureType[textureUnit]);
  389. }
  390. mActiveTextureType[textureUnit] = tex->getBinding();
  391. tex->bind(textureUnit);
  392. }
  393. else if(mActiveTextureType[textureUnit] != GL_ZERO)
  394. {
  395. glBindTexture(mActiveTextureType[textureUnit], 0);
  396. glDisable(mActiveTextureType[textureUnit]);
  397. mActiveTextureType[textureUnit] = GL_ZERO;
  398. }
  399. glActiveTexture(GL_TEXTURE0);
  400. }
  401. void GFXGLDevice::setCubemapInternal(U32 textureUnit, const GFXGLCubemap* texture)
  402. {
  403. glActiveTexture(GL_TEXTURE0 + textureUnit);
  404. if(texture)
  405. {
  406. if(mActiveTextureType[textureUnit] != GL_TEXTURE_CUBE_MAP && mActiveTextureType[textureUnit] != GL_ZERO)
  407. {
  408. glBindTexture(mActiveTextureType[textureUnit], 0);
  409. glDisable(mActiveTextureType[textureUnit]);
  410. }
  411. mActiveTextureType[textureUnit] = GL_TEXTURE_CUBE_MAP;
  412. texture->bind(textureUnit);
  413. }
  414. else if(mActiveTextureType[textureUnit] != GL_ZERO)
  415. {
  416. glBindTexture(mActiveTextureType[textureUnit], 0);
  417. glDisable(mActiveTextureType[textureUnit]);
  418. mActiveTextureType[textureUnit] = GL_ZERO;
  419. }
  420. glActiveTexture(GL_TEXTURE0);
  421. }
  422. void GFXGLDevice::setMatrix( GFXMatrixType mtype, const MatrixF &mat )
  423. {
  424. MatrixF modelview;
  425. switch (mtype)
  426. {
  427. case GFXMatrixWorld :
  428. {
  429. glMatrixMode(GL_MODELVIEW);
  430. m_mCurrentWorld = mat;
  431. modelview = m_mCurrentWorld;
  432. modelview *= m_mCurrentView;
  433. modelview.transpose();
  434. glLoadMatrixf((F32*) modelview);
  435. }
  436. break;
  437. case GFXMatrixView :
  438. {
  439. glMatrixMode(GL_MODELVIEW);
  440. m_mCurrentView = mat;
  441. modelview = m_mCurrentView;
  442. modelview *= m_mCurrentWorld;
  443. modelview.transpose();
  444. glLoadMatrixf((F32*) modelview);
  445. }
  446. break;
  447. case GFXMatrixProjection :
  448. {
  449. glMatrixMode(GL_PROJECTION);
  450. MatrixF t(mat);
  451. t.transpose();
  452. glLoadMatrixf((F32*) t);
  453. glMatrixMode(GL_MODELVIEW);
  454. }
  455. break;
  456. // CodeReview - Add support for texture transform matrix types
  457. default:
  458. AssertFatal(false, "GFXGLDevice::setMatrix - Unknown matrix mode!");
  459. return;
  460. }
  461. }
  462. void GFXGLDevice::setClipRect( const RectI &inRect )
  463. {
  464. AssertFatal(mCurrentRT.isValid(), "GFXGLDevice::setClipRect - must have a render target set to do any rendering operations!");
  465. // Clip the rect against the renderable size.
  466. Point2I size = mCurrentRT->getSize();
  467. RectI maxRect(Point2I(0,0), size);
  468. mClip = inRect;
  469. mClip.intersect(maxRect);
  470. // Create projection matrix. See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/ortho.html
  471. const F32 left = mClip.point.x;
  472. const F32 right = mClip.point.x + mClip.extent.x;
  473. const F32 bottom = mClip.extent.y;
  474. const F32 top = 0.0f;
  475. const F32 near = 0.0f;
  476. const F32 far = 1.0f;
  477. const F32 tx = -(right + left)/(right - left);
  478. const F32 ty = -(top + bottom)/(top - bottom);
  479. const F32 tz = -(far + near)/(far - near);
  480. static Point4F pt;
  481. pt.set(2.0f / (right - left), 0.0f, 0.0f, 0.0f);
  482. mProjectionMatrix.setColumn(0, pt);
  483. pt.set(0.0f, 2.0f/(top - bottom), 0.0f, 0.0f);
  484. mProjectionMatrix.setColumn(1, pt);
  485. pt.set(0.0f, 0.0f, -2.0f/(far - near), 0.0f);
  486. mProjectionMatrix.setColumn(2, pt);
  487. pt.set(tx, ty, tz, 1.0f);
  488. mProjectionMatrix.setColumn(3, pt);
  489. // Translate projection matrix.
  490. static MatrixF translate(true);
  491. pt.set(0.0f, -mClip.point.y, 0.0f, 1.0f);
  492. translate.setColumn(3, pt);
  493. mProjectionMatrix *= translate;
  494. setMatrix(GFXMatrixProjection, mProjectionMatrix);
  495. MatrixF mTempMatrix(true);
  496. setViewMatrix( mTempMatrix );
  497. setWorldMatrix( mTempMatrix );
  498. // Set the viewport to the clip rect (with y flip)
  499. RectI viewport(mClip.point.x, size.y - (mClip.point.y + mClip.extent.y), mClip.extent.x, mClip.extent.y);
  500. setViewport(viewport);
  501. }
  502. /// Creates a state block object based on the desc passed in. This object
  503. /// represents an immutable state.
  504. GFXStateBlockRef GFXGLDevice::createStateBlockInternal(const GFXStateBlockDesc& desc)
  505. {
  506. return GFXStateBlockRef(new GFXGLStateBlock(desc));
  507. }
  508. /// Activates a stateblock
  509. void GFXGLDevice::setStateBlockInternal(GFXStateBlock* block, bool force)
  510. {
  511. AssertFatal(dynamic_cast<GFXGLStateBlock*>(block), "GFXGLDevice::setStateBlockInternal - Incorrect stateblock type for this device!");
  512. GFXGLStateBlock* glBlock = static_cast<GFXGLStateBlock*>(block);
  513. GFXGLStateBlock* glCurrent = static_cast<GFXGLStateBlock*>(mCurrentStateBlock.getPointer());
  514. if (force)
  515. glCurrent = NULL;
  516. glBlock->activate(glCurrent); // Doesn't use current yet.
  517. mCurrentGLStateBlock = glBlock;
  518. }
  519. //------------------------------------------------------------------------------
  520. GFXTextureTarget * GFXGLDevice::allocRenderToTextureTarget()
  521. {
  522. GFXGLTextureTarget *targ = new GFXGLTextureTarget();
  523. targ->registerResourceWithDevice(this);
  524. return targ;
  525. }
  526. GFXFence * GFXGLDevice::createFence()
  527. {
  528. GFXFence* fence = _createPlatformSpecificFence();
  529. if(!fence)
  530. fence = new GFXGeneralFence( this );
  531. fence->registerResourceWithDevice(this);
  532. return fence;
  533. }
  534. GFXOcclusionQuery* GFXGLDevice::createOcclusionQuery()
  535. {
  536. GFXOcclusionQuery *query = new GFXGLOcclusionQuery( this );
  537. query->registerResourceWithDevice(this);
  538. return query;
  539. }
  540. void GFXGLDevice::setupGenericShaders( GenericShaderType type )
  541. {
  542. TORQUE_UNUSED(type);
  543. // We have FF support, use that.
  544. disableShaders();
  545. }
  546. GFXShader* GFXGLDevice::createShader()
  547. {
  548. GFXGLShader* shader = new GFXGLShader();
  549. shader->registerResourceWithDevice( this );
  550. return shader;
  551. }
  552. void GFXGLDevice::setShader( GFXShader *shader )
  553. {
  554. if ( shader )
  555. {
  556. GFXGLShader *glShader = static_cast<GFXGLShader*>( shader );
  557. glShader->useProgram();
  558. }
  559. else
  560. glUseProgram(0);
  561. }
  562. void GFXGLDevice::disableShaders()
  563. {
  564. setShader(NULL);
  565. setShaderConstBuffer( NULL );
  566. }
  567. void GFXGLDevice::setShaderConstBufferInternal(GFXShaderConstBuffer* buffer)
  568. {
  569. static_cast<GFXGLShaderConstBuffer*>(buffer)->activate();
  570. }
  571. U32 GFXGLDevice::getNumSamplers() const
  572. {
  573. return mPixelShaderVersion > 0.001f ? mMaxShaderTextures : mMaxFFTextures;
  574. }
  575. U32 GFXGLDevice::getNumRenderTargets() const
  576. {
  577. return 1;
  578. }
  579. void GFXGLDevice::_updateRenderTargets()
  580. {
  581. if ( mRTDirty || mCurrentRT->isPendingState() )
  582. {
  583. if ( mRTDeactivate )
  584. {
  585. mRTDeactivate->deactivate();
  586. mRTDeactivate = NULL;
  587. }
  588. // NOTE: The render target changes is not really accurate
  589. // as the GFXTextureTarget supports MRT internally. So when
  590. // we activate a GFXTarget it could result in multiple calls
  591. // to SetRenderTarget on the actual device.
  592. mDeviceStatistics.mRenderTargetChanges++;
  593. GFXGLTextureTarget *tex = dynamic_cast<GFXGLTextureTarget*>( mCurrentRT.getPointer() );
  594. if ( tex )
  595. {
  596. tex->applyState();
  597. tex->makeActive();
  598. }
  599. else
  600. {
  601. GFXGLWindowTarget *win = dynamic_cast<GFXGLWindowTarget*>( mCurrentRT.getPointer() );
  602. AssertFatal( win != NULL,
  603. "GFXGLDevice::_updateRenderTargets() - invalid target subclass passed!" );
  604. win->makeActive();
  605. if( win->mContext != static_cast<GFXGLDevice*>(GFX)->mContext )
  606. {
  607. mRTDirty = false;
  608. GFX->updateStates(true);
  609. }
  610. }
  611. mRTDirty = false;
  612. }
  613. if ( mViewportDirty )
  614. {
  615. glViewport( mViewport.point.x, mViewport.point.y, mViewport.extent.x, mViewport.extent.y );
  616. mViewportDirty = false;
  617. }
  618. }
  619. GFXFormat GFXGLDevice::selectSupportedFormat( GFXTextureProfile* profile,
  620. const Vector<GFXFormat>& formats,
  621. bool texture,
  622. bool mustblend,
  623. bool mustfilter )
  624. {
  625. for(U32 i = 0; i < formats.size(); i++)
  626. {
  627. // Single channel textures are not supported by FBOs.
  628. if(profile->testFlag(GFXTextureProfile::RenderTarget) && (formats[i] == GFXFormatA8 || formats[i] == GFXFormatL8 || formats[i] == GFXFormatL16))
  629. continue;
  630. if(GFXGLTextureInternalFormat[formats[i]] == GL_ZERO)
  631. continue;
  632. return formats[i];
  633. }
  634. return GFXFormatR8G8B8A8;
  635. }
  636. //
  637. // Register this device with GFXInit
  638. //
  639. class GFXGLRegisterDevice
  640. {
  641. public:
  642. GFXGLRegisterDevice()
  643. {
  644. GFXInit::getRegisterDeviceSignal().notify(&GFXGLDevice::enumerateAdapters);
  645. }
  646. };
  647. static GFXGLRegisterDevice pGLRegisterDevice;
  648. ConsoleFunction(cycleResources, void, 1, 1, "")
  649. {
  650. static_cast<GFXGLDevice*>(GFX)->zombify();
  651. static_cast<GFXGLDevice*>(GFX)->resurrect();
  652. }