gfxGLDevice.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 "gfxGLTextureArray.h"
  25. #include "platform/platformGL.h"
  26. #include "gfx/gfxCubemap.h"
  27. #include "gfx/gl/screenshotGL.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "gfx/gl/gfxGLEnumTranslate.h"
  30. #include "gfx/gl/gfxGLVertexBuffer.h"
  31. #include "gfx/gl/gfxGLPrimitiveBuffer.h"
  32. #include "gfx/gl/gfxGLTextureTarget.h"
  33. #include "gfx/gl/gfxGLTextureManager.h"
  34. #include "gfx/gl/gfxGLTextureObject.h"
  35. #include "gfx/gl/gfxGLCubemap.h"
  36. #include "gfx/gl/gfxGLCardProfiler.h"
  37. #include "gfx/gl/gfxGLWindowTarget.h"
  38. #include "platform/platformDlibrary.h"
  39. #include "gfx/gl/gfxGLShader.h"
  40. #include "gfx/primBuilder.h"
  41. #include "console/console.h"
  42. #include "gfx/gl/gfxGLOcclusionQuery.h"
  43. #include "materials/shaderData.h"
  44. #include "gfx/gl/gfxGLStateCache.h"
  45. #include "gfx/gl/gfxGLVertexAttribLocation.h"
  46. #include "gfx/gl/gfxGLVertexDecl.h"
  47. #include "shaderGen/shaderGen.h"
  48. #include "gfxGLUtils.h"
  49. GFXAdapter::CreateDeviceInstanceDelegate GFXGLDevice::mCreateDeviceInstance(GFXGLDevice::createInstance);
  50. GFXDevice *GFXGLDevice::createInstance( U32 adapterIndex )
  51. {
  52. return new GFXGLDevice(adapterIndex);
  53. }
  54. namespace GL
  55. {
  56. extern void gglPerformBinds();
  57. extern void gglPerformExtensionBinds(void *context);
  58. }
  59. void loadGLCore()
  60. {
  61. static bool coreLoaded = false; // Guess what this is for.
  62. if(coreLoaded)
  63. return;
  64. coreLoaded = true;
  65. // Make sure we've got our GL bindings.
  66. GL::gglPerformBinds();
  67. }
  68. void loadGLExtensions(void *context)
  69. {
  70. static bool extensionsLoaded = false;
  71. if(extensionsLoaded)
  72. return;
  73. extensionsLoaded = true;
  74. GL::gglPerformExtensionBinds(context);
  75. }
  76. void STDCALL glDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
  77. const GLchar *message, const void *userParam)
  78. {
  79. // JTH [11/24/2016]: This is a temporary fix so that we do not get spammed for redundant fbo changes.
  80. // This only happens on Intel cards. This should be looked into sometime in the near future.
  81. if (dStrStartsWith(message, "API_ID_REDUNDANT_FBO"))
  82. return;
  83. if (severity == GL_DEBUG_SEVERITY_HIGH)
  84. Con::errorf("OPENGL: %s", message);
  85. else if (severity == GL_DEBUG_SEVERITY_MEDIUM)
  86. Con::warnf("OPENGL: %s", message);
  87. else if (severity == GL_DEBUG_SEVERITY_LOW)
  88. Con::printf("OPENGL: %s", message);
  89. }
  90. void STDCALL glAmdDebugCallback(GLuint id, GLenum category, GLenum severity, GLsizei length,
  91. const GLchar* message, GLvoid* userParam)
  92. {
  93. if (severity == GL_DEBUG_SEVERITY_HIGH)
  94. Con::errorf("AMDOPENGL: %s", message);
  95. else if (severity == GL_DEBUG_SEVERITY_MEDIUM)
  96. Con::warnf("AMDOPENGL: %s", message);
  97. else if (severity == GL_DEBUG_SEVERITY_LOW)
  98. Con::printf("AMDOPENGL: %s", message);
  99. }
  100. void GFXGLDevice::initGLState()
  101. {
  102. // We don't currently need to sync device state with a known good place because we are
  103. // going to set everything in GFXGLStateBlock, but if we change our GFXGLStateBlock strategy, this may
  104. // need to happen.
  105. // Deal with the card profiler here when we know we have a valid context.
  106. mCardProfiler = new GFXGLCardProfiler();
  107. mCardProfiler->init();
  108. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, (GLint*)&mMaxShaderTextures);
  109. // JTH: Needs removed, ffp
  110. //glGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&mMaxFFTextures);
  111. glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, (GLint*)&mMaxTRColors);
  112. mMaxTRColors = getMin( mMaxTRColors, (U32)(GFXTextureTarget::MaxRenderSlotId-1) );
  113. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  114. // [JTH 5/6/2016] GLSL 1.50 is really SM 4.0
  115. // Setting mPixelShaderVersion to 3.0 will allow Advanced Lighting to run.
  116. mPixelShaderVersion = 3.0;
  117. // Set capability extensions.
  118. mCapabilities.anisotropicFiltering = mCardProfiler->queryProfile("GL_EXT_texture_filter_anisotropic");
  119. mCapabilities.bufferStorage = mCardProfiler->queryProfile("GL_ARB_buffer_storage");
  120. mCapabilities.textureStorage = mCardProfiler->queryProfile("GL_ARB_texture_storage");
  121. mCapabilities.copyImage = mCardProfiler->queryProfile("GL_ARB_copy_image");
  122. mCapabilities.vertexAttributeBinding = mCardProfiler->queryProfile("GL_ARB_vertex_attrib_binding");
  123. mCapabilities.khrDebug = mCardProfiler->queryProfile("GL_KHR_debug");
  124. mCapabilities.extDebugMarker = mCardProfiler->queryProfile("GL_EXT_debug_marker");
  125. String vendorStr = (const char*)glGetString( GL_VENDOR );
  126. if( vendorStr.find("NVIDIA", 0, String::NoCase | String::Left) != String::NPos)
  127. mUseGlMap = false;
  128. // Workaround for all Mac's, has a problem using glMap* with volatile buffers
  129. #ifdef TORQUE_OS_MAC
  130. mUseGlMap = false;
  131. #endif
  132. #if TORQUE_DEBUG
  133. if( gglHasExtension(ARB_debug_output) )
  134. {
  135. glEnable(GL_DEBUG_OUTPUT);
  136. glDebugMessageCallbackARB(glDebugCallback, NULL);
  137. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  138. GLuint unusedIds = 0;
  139. glDebugMessageControlARB(GL_DONT_CARE,
  140. GL_DONT_CARE,
  141. GL_DONT_CARE,
  142. 0,
  143. &unusedIds,
  144. GL_TRUE);
  145. }
  146. else if(gglHasExtension(AMD_debug_output))
  147. {
  148. glEnable(GL_DEBUG_OUTPUT);
  149. glDebugMessageCallbackAMD(glAmdDebugCallback, NULL);
  150. //glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  151. GLuint unusedIds = 0;
  152. glDebugMessageEnableAMD(GL_DONT_CARE, GL_DONT_CARE, 0,&unusedIds, GL_TRUE);
  153. }
  154. #endif
  155. PlatformGL::setVSync(smDisableVSync ? 0 : 1);
  156. //install vsync callback
  157. Con::NotifyDelegate clbk(this, &GFXGLDevice::vsyncCallback);
  158. Con::addVariableNotify("$pref::Video::disableVerticalSync", clbk);
  159. //OpenGL 3 need a binded VAO for render
  160. GLuint vao;
  161. glGenVertexArrays(1, &vao);
  162. glBindVertexArray(vao);
  163. //enable sRGB
  164. glEnable(GL_FRAMEBUFFER_SRGB);
  165. }
  166. void GFXGLDevice::vsyncCallback()
  167. {
  168. PlatformGL::setVSync(smDisableVSync ? 0 : 1);
  169. }
  170. GFXGLDevice::GFXGLDevice(U32 adapterIndex) :
  171. mAdapterIndex(adapterIndex),
  172. mNeedUpdateVertexAttrib(false),
  173. mCurrentPB(NULL),
  174. mDrawInstancesCount(0),
  175. mCurrentShader( NULL ),
  176. m_mCurrentWorld(true),
  177. m_mCurrentView(true),
  178. mContext(NULL),
  179. mPixelFormat(NULL),
  180. mPixelShaderVersion(0.0f),
  181. mMaxShaderTextures(2),
  182. mMaxFFTextures(2),
  183. mMaxTRColors(1),
  184. mClip(0, 0, 0, 0),
  185. mWindowRT(NULL),
  186. mUseGlMap(true)
  187. {
  188. for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
  189. {
  190. mCurrentVB[i] = NULL;
  191. mCurrentVB_Divisor[i] = 0;
  192. }
  193. // Initiailize capabilities to false.
  194. memset(&mCapabilities, 0, sizeof(GLCapabilities));
  195. loadGLCore();
  196. GFXGLEnumTranslate::init();
  197. GFXVertexColor::setSwizzle( &Swizzles::rgba );
  198. // OpenGL have native RGB, no need swizzle
  199. mDeviceSwizzle32 = &Swizzles::rgba;
  200. mDeviceSwizzle24 = &Swizzles::rgb;
  201. mTextureManager = new GFXGLTextureManager();
  202. gScreenShot = new ScreenShotGL();
  203. for(U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++)
  204. mActiveTextureType[i] = GL_ZERO;
  205. mNumVertexStream = 2;
  206. mSupportsAnisotropic = false;
  207. for(int i = 0; i < GS_COUNT; ++i)
  208. mModelViewProjSC[i] = NULL;
  209. mOpenglStateCache = new GFXGLStateCache;
  210. }
  211. GFXGLDevice::~GFXGLDevice()
  212. {
  213. mCurrentStateBlock = NULL;
  214. for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
  215. mCurrentVB[i] = NULL;
  216. mCurrentPB = NULL;
  217. for(U32 i = 0; i < mVolatileVBs.size(); i++)
  218. mVolatileVBs[i] = NULL;
  219. for(U32 i = 0; i < mVolatilePBs.size(); i++)
  220. mVolatilePBs[i] = NULL;
  221. // Clear out our current texture references
  222. for (U32 i = 0; i < GFX_TEXTURE_STAGE_COUNT; i++)
  223. {
  224. mCurrentTexture[i] = NULL;
  225. mNewTexture[i] = NULL;
  226. mCurrentCubemap[i] = NULL;
  227. mNewCubemap[i] = NULL;
  228. }
  229. mRTStack.clear();
  230. mCurrentRT = NULL;
  231. if( mTextureManager )
  232. {
  233. mTextureManager->zombify();
  234. mTextureManager->kill();
  235. }
  236. GFXResource* walk = mResourceListHead;
  237. while(walk)
  238. {
  239. walk->zombify();
  240. walk = walk->getNextResource();
  241. }
  242. if( mCardProfiler )
  243. SAFE_DELETE( mCardProfiler );
  244. SAFE_DELETE( gScreenShot );
  245. SAFE_DELETE( mOpenglStateCache );
  246. }
  247. void GFXGLDevice::zombify()
  248. {
  249. mTextureManager->zombify();
  250. for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
  251. if(mCurrentVB[i])
  252. mCurrentVB[i]->finish();
  253. if(mCurrentPB)
  254. mCurrentPB->finish();
  255. //mVolatileVBs.clear();
  256. //mVolatilePBs.clear();
  257. GFXResource* walk = mResourceListHead;
  258. while(walk)
  259. {
  260. walk->zombify();
  261. walk = walk->getNextResource();
  262. }
  263. }
  264. void GFXGLDevice::resurrect()
  265. {
  266. GFXResource* walk = mResourceListHead;
  267. while(walk)
  268. {
  269. walk->resurrect();
  270. walk = walk->getNextResource();
  271. }
  272. for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
  273. if(mCurrentVB[i])
  274. mCurrentVB[i]->prepare();
  275. if(mCurrentPB)
  276. mCurrentPB->prepare();
  277. mTextureManager->resurrect();
  278. }
  279. GFXVertexBuffer* GFXGLDevice::findVolatileVBO(U32 numVerts, const GFXVertexFormat *vertexFormat, U32 vertSize)
  280. {
  281. PROFILE_SCOPE(GFXGLDevice_findVBPool);
  282. for(U32 i = 0; i < mVolatileVBs.size(); i++)
  283. if ( mVolatileVBs[i]->mNumVerts >= numVerts &&
  284. mVolatileVBs[i]->mVertexFormat.isEqual( *vertexFormat ) &&
  285. mVolatileVBs[i]->mVertexSize == vertSize &&
  286. mVolatileVBs[i]->getRefCount() == 1 )
  287. return mVolatileVBs[i];
  288. // No existing VB, so create one
  289. PROFILE_SCOPE(GFXGLDevice_createVBPool);
  290. StrongRefPtr<GFXGLVertexBuffer> buf(new GFXGLVertexBuffer(GFX, numVerts, vertexFormat, vertSize, GFXBufferTypeVolatile));
  291. buf->registerResourceWithDevice(this);
  292. mVolatileVBs.push_back(buf);
  293. return buf.getPointer();
  294. }
  295. GFXPrimitiveBuffer* GFXGLDevice::findVolatilePBO(U32 numIndices, U32 numPrimitives)
  296. {
  297. for(U32 i = 0; i < mVolatilePBs.size(); i++)
  298. if((mVolatilePBs[i]->mIndexCount >= numIndices) && (mVolatilePBs[i]->getRefCount() == 1))
  299. return mVolatilePBs[i];
  300. // No existing PB, so create one
  301. StrongRefPtr<GFXGLPrimitiveBuffer> buf(new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, GFXBufferTypeVolatile));
  302. buf->registerResourceWithDevice(this);
  303. mVolatilePBs.push_back(buf);
  304. return buf.getPointer();
  305. }
  306. GFXVertexBuffer *GFXGLDevice::allocVertexBuffer( U32 numVerts,
  307. const GFXVertexFormat *vertexFormat,
  308. U32 vertSize,
  309. GFXBufferType bufferType,
  310. void* data )
  311. {
  312. PROFILE_SCOPE(GFXGLDevice_allocVertexBuffer);
  313. if(bufferType == GFXBufferTypeVolatile)
  314. return findVolatileVBO(numVerts, vertexFormat, vertSize);
  315. GFXGLVertexBuffer* buf = new GFXGLVertexBuffer( GFX, numVerts, vertexFormat, vertSize, bufferType );
  316. buf->registerResourceWithDevice(this);
  317. if(data)
  318. {
  319. void* dest;
  320. buf->lock(0, numVerts, &dest);
  321. dMemcpy(dest, data, vertSize * numVerts);
  322. buf->unlock();
  323. }
  324. return buf;
  325. }
  326. GFXPrimitiveBuffer *GFXGLDevice::allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data )
  327. {
  328. GFXPrimitiveBuffer* buf;
  329. if(bufferType == GFXBufferTypeVolatile)
  330. {
  331. buf = findVolatilePBO(numIndices, numPrimitives);
  332. }
  333. else
  334. {
  335. buf = new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
  336. buf->registerResourceWithDevice(this);
  337. }
  338. if(data)
  339. {
  340. void* dest;
  341. buf->lock(0, numIndices, &dest);
  342. dMemcpy(dest, data, sizeof(U16) * numIndices);
  343. buf->unlock();
  344. }
  345. return buf;
  346. }
  347. void GFXGLDevice::setVertexStream( U32 stream, GFXVertexBuffer *buffer )
  348. {
  349. AssertFatal(stream <= 1, "GFXGLDevice::setVertexStream only support 2 stream (0: data, 1: instancing)");
  350. //if(mCurrentVB[stream] != buffer)
  351. {
  352. // Reset the state the old VB required, then set the state the new VB requires.
  353. if( mCurrentVB[stream] )
  354. {
  355. mCurrentVB[stream]->finish();
  356. }
  357. mCurrentVB[stream] = static_cast<GFXGLVertexBuffer*>( buffer );
  358. mNeedUpdateVertexAttrib = true;
  359. }
  360. }
  361. void GFXGLDevice::setVertexStreamFrequency( U32 stream, U32 frequency )
  362. {
  363. if( stream == 0 )
  364. {
  365. mCurrentVB_Divisor[stream] = 0; // non instanced, is vertex buffer
  366. mDrawInstancesCount = frequency; // instances count
  367. }
  368. else
  369. {
  370. AssertFatal(frequency <= 1, "GFXGLDevice::setVertexStreamFrequency only support 0/1 for this stream" );
  371. if( stream == 1 && frequency == 1 )
  372. mCurrentVB_Divisor[stream] = 1; // instances data need a frequency of 1
  373. else
  374. mCurrentVB_Divisor[stream] = 0;
  375. }
  376. mNeedUpdateVertexAttrib = true;
  377. }
  378. GFXCubemap* GFXGLDevice::createCubemap()
  379. {
  380. GFXGLCubemap* cube = new GFXGLCubemap();
  381. cube->registerResourceWithDevice(this);
  382. return cube;
  383. };
  384. GFXCubemapArray *GFXGLDevice::createCubemapArray()
  385. {
  386. GFXGLCubemapArray* cubeArray = new GFXGLCubemapArray();
  387. cubeArray->registerResourceWithDevice(this);
  388. return cubeArray;
  389. }
  390. GFXTextureArray* GFXGLDevice::createTextureArray()
  391. {
  392. GFXGLTextureArray* textureArray = new GFXGLTextureArray();
  393. textureArray->registerResourceWithDevice(this);
  394. return textureArray;
  395. }
  396. void GFXGLDevice::endSceneInternal()
  397. {
  398. // nothing to do for opengl
  399. mCanCurrentlyRender = false;
  400. }
  401. void GFXGLDevice::copyResource(GFXTextureObject* pDst, GFXCubemap* pSrc, const U32 face)
  402. {
  403. AssertFatal(pDst, "GFXGLDevice::copyResource: Destination texture is null");
  404. AssertFatal(pSrc, "GFXGLDevice::copyResource: Source cubemap is null");
  405. GFXGLTextureObject* gGLDst = static_cast<GFXGLTextureObject*>(pDst);
  406. GFXGLCubemap* pGLSrc = static_cast<GFXGLCubemap*>(pSrc);
  407. const GFXFormat format = pGLSrc->getFormat();
  408. const bool isCompressed = ImageUtil::isCompressedFormat(format);
  409. const U32 mipLevels = pGLSrc->getMipMapLevels();
  410. const U32 texSize = pGLSrc->getSize();
  411. //set up pbo if we don't have copyImage support
  412. if (!GFXGL->mCapabilities.copyImage)
  413. {
  414. const GLuint pbo = gGLDst->getBuffer();
  415. glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
  416. //allocate data
  417. glBufferData(GL_PIXEL_PACK_BUFFER, texSize * texSize * GFXFormat_getByteSize(format), NULL, GL_STREAM_COPY);
  418. }
  419. for (U32 mip = 0; mip < mipLevels; mip++)
  420. {
  421. const U32 mipSize = texSize >> mip;
  422. if (GFXGL->mCapabilities.copyImage)
  423. {
  424. glCopyImageSubData(pGLSrc->mCubemap, GL_TEXTURE_CUBE_MAP, mip, 0, 0, face, gGLDst->getHandle(), GL_TEXTURE_2D, mip, 0, 0, 0, mipSize, mipSize, 1);
  425. }
  426. else
  427. {
  428. //pbo id
  429. const GLuint pbo = gGLDst->getBuffer();
  430. //copy source texture data to pbo
  431. glBindTexture(GL_TEXTURE_CUBE_MAP, pGLSrc->mCubemap);
  432. glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
  433. if (isCompressed)
  434. glGetCompressedTexImage(GFXGLFaceType[face], mip, NULL);
  435. else
  436. glGetTexImage(GFXGLFaceType[face], mip, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  437. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  438. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  439. //copy data from pbo to destination
  440. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
  441. glBindTexture(gGLDst->getBinding(), gGLDst->getHandle());
  442. if (isCompressed)
  443. {
  444. const U32 mipDataSize = getCompressedSurfaceSize(format, pGLSrc->getSize(), pGLSrc->getSize(), 0);
  445. glCompressedTexSubImage2D(gGLDst->getBinding(), mip, 0, 0, mipSize, mipSize, GFXGLTextureFormat[format], mipDataSize, NULL);
  446. }
  447. else
  448. {
  449. glTexSubImage2D(gGLDst->getBinding(), mip, 0, 0, mipSize, mipSize, GFXGLTextureFormat[format], GFXGLTextureType[format], NULL);
  450. }
  451. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  452. glBindTexture(gGLDst->getBinding(), 0);
  453. }
  454. }
  455. }
  456. void GFXGLDevice::clear(U32 flags, const LinearColorF& color, F32 z, U32 stencil)
  457. {
  458. // Make sure we have flushed our render target state.
  459. _updateRenderTargets();
  460. bool writeAllColors = true;
  461. bool zwrite = true;
  462. bool writeAllStencil = true;
  463. const GFXStateBlockDesc *desc = NULL;
  464. if (mCurrentGLStateBlock)
  465. {
  466. desc = &mCurrentGLStateBlock->getDesc();
  467. zwrite = desc->zWriteEnable;
  468. writeAllColors = desc->colorWriteRed && desc->colorWriteGreen && desc->colorWriteBlue && desc->colorWriteAlpha;
  469. writeAllStencil = desc->stencilWriteMask == 0xFFFFFFFF;
  470. }
  471. glColorMask(true, true, true, true);
  472. glDepthMask(true);
  473. glStencilMask(0xFFFFFFFF);
  474. glClearColor(color.red, color.green, color.blue, color.alpha);
  475. glClearDepth(z);
  476. glClearStencil(stencil);
  477. GLbitfield clearflags = 0;
  478. clearflags |= (flags & GFXClearTarget) ? GL_COLOR_BUFFER_BIT : 0;
  479. clearflags |= (flags & GFXClearZBuffer) ? GL_DEPTH_BUFFER_BIT : 0;
  480. clearflags |= (flags & GFXClearStencil) ? GL_STENCIL_BUFFER_BIT : 0;
  481. glClear(clearflags);
  482. if(!writeAllColors)
  483. glColorMask(desc->colorWriteRed, desc->colorWriteGreen, desc->colorWriteBlue, desc->colorWriteAlpha);
  484. if(!zwrite)
  485. glDepthMask(false);
  486. if(!writeAllStencil)
  487. glStencilMask(desc->stencilWriteMask);
  488. }
  489. void GFXGLDevice::clearColorAttachment(const U32 attachment, const LinearColorF& color)
  490. {
  491. const GLfloat clearColor[4] = { color.red, color.green, color.blue, color.alpha };
  492. glClearBufferfv(GL_COLOR, attachment, clearColor);
  493. }
  494. // Given a primitive type and a number of primitives, return the number of indexes/vertexes used.
  495. inline GLsizei GFXGLDevice::primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount)
  496. {
  497. switch (primType)
  498. {
  499. case GFXPointList :
  500. return primitiveCount;
  501. break;
  502. case GFXLineList :
  503. return primitiveCount * 2;
  504. break;
  505. case GFXLineStrip :
  506. return primitiveCount + 1;
  507. break;
  508. case GFXTriangleList :
  509. return primitiveCount * 3;
  510. break;
  511. case GFXTriangleStrip :
  512. return 2 + primitiveCount;
  513. break;
  514. default:
  515. AssertFatal(false, "GFXGLDevice::primCountToIndexCount - unrecognized prim type");
  516. break;
  517. }
  518. return 0;
  519. }
  520. GFXVertexDecl* GFXGLDevice::allocVertexDecl( const GFXVertexFormat *vertexFormat )
  521. {
  522. PROFILE_SCOPE(GFXGLDevice_allocVertexDecl);
  523. typedef Map<void*, GFXGLVertexDecl> GFXGLVertexDeclMap;
  524. static GFXGLVertexDeclMap declMap;
  525. GFXGLVertexDeclMap::Iterator itr = declMap.find( (void*)vertexFormat->getDescription().c_str() ); // description string are interned, safe to use c_str()
  526. if(itr != declMap.end())
  527. return &itr->value;
  528. GFXGLVertexDecl &decl = declMap[(void*)vertexFormat->getDescription().c_str()];
  529. decl.init(vertexFormat);
  530. return &decl;
  531. }
  532. void GFXGLDevice::setVertexDecl( const GFXVertexDecl *decl )
  533. {
  534. static_cast<const GFXGLVertexDecl*>(decl)->prepareVertexFormat();
  535. }
  536. inline void GFXGLDevice::preDrawPrimitive()
  537. {
  538. if( mStateDirty )
  539. {
  540. updateStates();
  541. }
  542. if(mCurrentShaderConstBuffer)
  543. setShaderConstBufferInternal(mCurrentShaderConstBuffer);
  544. if( mNeedUpdateVertexAttrib )
  545. {
  546. AssertFatal(mCurrVertexDecl, "");
  547. const GFXGLVertexDecl* decl = static_cast<const GFXGLVertexDecl*>(mCurrVertexDecl);
  548. for(int i = 0; i < getNumVertexStreams(); ++i)
  549. {
  550. if(mCurrentVB[i])
  551. {
  552. mCurrentVB[i]->prepare(i, mCurrentVB_Divisor[i]); // GL_ARB_vertex_attrib_binding
  553. decl->prepareBuffer_old( i, mCurrentVB[i]->mBuffer, mCurrentVB_Divisor[i] ); // old vertex buffer/format
  554. }
  555. }
  556. decl->updateActiveVertexAttrib( GFXGL->getOpenglCache()->getCacheVertexAttribActive() );
  557. }
  558. mNeedUpdateVertexAttrib = false;
  559. }
  560. inline void GFXGLDevice::postDrawPrimitive(U32 primitiveCount)
  561. {
  562. mDeviceStatistics.mDrawCalls++;
  563. mDeviceStatistics.mPolyCount += primitiveCount;
  564. }
  565. void GFXGLDevice::drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount )
  566. {
  567. preDrawPrimitive();
  568. if(mCurrentVB[0])
  569. vertexStart += mCurrentVB[0]->mBufferVertexOffset;
  570. if(mDrawInstancesCount)
  571. glDrawArraysInstanced(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount), mDrawInstancesCount);
  572. else
  573. glDrawArrays(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount));
  574. postDrawPrimitive(primitiveCount);
  575. }
  576. void GFXGLDevice::drawIndexedPrimitive( GFXPrimitiveType primType,
  577. U32 startVertex,
  578. U32 minIndex,
  579. U32 numVerts,
  580. U32 startIndex,
  581. U32 primitiveCount )
  582. {
  583. preDrawPrimitive();
  584. U16* buf = (U16*)static_cast<GFXGLPrimitiveBuffer*>(mCurrentPrimitiveBuffer.getPointer())->getBuffer() + startIndex + mCurrentPrimitiveBuffer->mVolatileStart;
  585. const U32 baseVertex = mCurrentVB[0]->mBufferVertexOffset + startVertex;
  586. if(mDrawInstancesCount)
  587. glDrawElementsInstancedBaseVertex(GFXGLPrimType[primType], primCountToIndexCount(primType, primitiveCount), GL_UNSIGNED_SHORT, buf, mDrawInstancesCount, baseVertex);
  588. else
  589. glDrawElementsBaseVertex(GFXGLPrimType[primType], primCountToIndexCount(primType, primitiveCount), GL_UNSIGNED_SHORT, buf, baseVertex);
  590. postDrawPrimitive(primitiveCount);
  591. }
  592. void GFXGLDevice::setPB(GFXGLPrimitiveBuffer* pb)
  593. {
  594. if(mCurrentPB)
  595. mCurrentPB->finish();
  596. mCurrentPB = pb;
  597. }
  598. void GFXGLDevice::setTextureInternal(U32 textureUnit, const GFXTextureObject*texture)
  599. {
  600. GFXGLTextureObject *tex = static_cast<GFXGLTextureObject*>(const_cast<GFXTextureObject*>(texture));
  601. if (tex)
  602. {
  603. mActiveTextureType[textureUnit] = tex->getBinding();
  604. tex->bind(textureUnit);
  605. }
  606. else if(mActiveTextureType[textureUnit] != GL_ZERO)
  607. {
  608. glActiveTexture(GL_TEXTURE0 + textureUnit);
  609. glBindTexture(mActiveTextureType[textureUnit], 0);
  610. getOpenglCache()->setCacheBindedTex(textureUnit, mActiveTextureType[textureUnit], 0);
  611. mActiveTextureType[textureUnit] = GL_ZERO;
  612. }
  613. }
  614. void GFXGLDevice::setCubemapInternal(U32 textureUnit, const GFXGLCubemap* texture)
  615. {
  616. if(texture)
  617. {
  618. mActiveTextureType[textureUnit] = GL_TEXTURE_CUBE_MAP;
  619. texture->bind(textureUnit);
  620. }
  621. else if(mActiveTextureType[textureUnit] != GL_ZERO)
  622. {
  623. glActiveTexture(GL_TEXTURE0 + textureUnit);
  624. glBindTexture(mActiveTextureType[textureUnit], 0);
  625. getOpenglCache()->setCacheBindedTex(textureUnit, mActiveTextureType[textureUnit], 0);
  626. mActiveTextureType[textureUnit] = GL_ZERO;
  627. }
  628. }
  629. void GFXGLDevice::setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArray* texture)
  630. {
  631. if (texture)
  632. {
  633. mActiveTextureType[textureUnit] = GL_TEXTURE_CUBE_MAP_ARRAY_ARB;
  634. texture->bind(textureUnit);
  635. }
  636. else if (mActiveTextureType[textureUnit] != GL_ZERO)
  637. {
  638. glActiveTexture(GL_TEXTURE0 + textureUnit);
  639. glBindTexture(mActiveTextureType[textureUnit], 0);
  640. getOpenglCache()->setCacheBindedTex(textureUnit, mActiveTextureType[textureUnit], 0);
  641. mActiveTextureType[textureUnit] = GL_ZERO;
  642. }
  643. }
  644. void GFXGLDevice::setTextureArrayInternal(U32 textureUnit, const GFXGLTextureArray* texture)
  645. {
  646. if (texture)
  647. {
  648. mActiveTextureType[textureUnit] = GL_TEXTURE_2D_ARRAY;
  649. texture->bind(textureUnit);
  650. }
  651. else if (mActiveTextureType[textureUnit] != GL_ZERO)
  652. {
  653. glActiveTexture(GL_TEXTURE0 + textureUnit);
  654. glBindTexture(mActiveTextureType[textureUnit], 0);
  655. getOpenglCache()->setCacheBindedTex(textureUnit, mActiveTextureType[textureUnit], 0);
  656. mActiveTextureType[textureUnit] = GL_ZERO;
  657. }
  658. }
  659. void GFXGLDevice::setClipRect( const RectI &inRect )
  660. {
  661. AssertFatal(mCurrentRT.isValid(), "GFXGLDevice::setClipRect - must have a render target set to do any rendering operations!");
  662. // Clip the rect against the renderable size.
  663. Point2I size = mCurrentRT->getSize();
  664. RectI maxRect(Point2I(0,0), size);
  665. mClip = inRect;
  666. mClip.intersect(maxRect);
  667. // Create projection matrix. See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/ortho.html
  668. const F32 left = mClip.point.x;
  669. const F32 right = mClip.point.x + mClip.extent.x;
  670. const F32 bottom = mClip.extent.y;
  671. const F32 top = 0.0f;
  672. const F32 nearPlane = 0.0f;
  673. const F32 farPlane = 1.0f;
  674. const F32 tx = -(right + left)/(right - left);
  675. const F32 ty = -(top + bottom)/(top - bottom);
  676. const F32 tz = -(farPlane + nearPlane)/(farPlane - nearPlane);
  677. static Point4F pt;
  678. pt.set(2.0f / (right - left), 0.0f, 0.0f, 0.0f);
  679. mProjectionMatrix.setColumn(0, pt);
  680. pt.set(0.0f, 2.0f/(top - bottom), 0.0f, 0.0f);
  681. mProjectionMatrix.setColumn(1, pt);
  682. pt.set(0.0f, 0.0f, -2.0f/(farPlane - nearPlane), 0.0f);
  683. mProjectionMatrix.setColumn(2, pt);
  684. pt.set(tx, ty, tz, 1.0f);
  685. mProjectionMatrix.setColumn(3, pt);
  686. // Translate projection matrix.
  687. static MatrixF translate(true);
  688. pt.set(0.0f, -mClip.point.y, 0.0f, 1.0f);
  689. translate.setColumn(3, pt);
  690. mProjectionMatrix *= translate;
  691. MatrixF mTempMatrix(true);
  692. setViewMatrix( mTempMatrix );
  693. setWorldMatrix( mTempMatrix );
  694. // Set the viewport to the clip rect
  695. RectI viewport(mClip.point.x, mClip.point.y, mClip.extent.x, mClip.extent.y);
  696. setViewport(viewport);
  697. }
  698. /// Creates a state block object based on the desc passed in. This object
  699. /// represents an immutable state.
  700. GFXStateBlockRef GFXGLDevice::createStateBlockInternal(const GFXStateBlockDesc& desc)
  701. {
  702. return GFXStateBlockRef(new GFXGLStateBlock(desc));
  703. }
  704. /// Activates a stateblock
  705. void GFXGLDevice::setStateBlockInternal(GFXStateBlock* block, bool force)
  706. {
  707. AssertFatal(dynamic_cast<GFXGLStateBlock*>(block), "GFXGLDevice::setStateBlockInternal - Incorrect stateblock type for this device!");
  708. GFXGLStateBlock* glBlock = static_cast<GFXGLStateBlock*>(block);
  709. GFXGLStateBlock* glCurrent = static_cast<GFXGLStateBlock*>(mCurrentStateBlock.getPointer());
  710. if (force)
  711. glCurrent = NULL;
  712. glBlock->activate(glCurrent); // Doesn't use current yet.
  713. mCurrentGLStateBlock = glBlock;
  714. }
  715. //------------------------------------------------------------------------------
  716. GFXTextureTarget * GFXGLDevice::allocRenderToTextureTarget(bool genMips)
  717. {
  718. GFXGLTextureTarget *targ = new GFXGLTextureTarget(genMips);
  719. targ->registerResourceWithDevice(this);
  720. return targ;
  721. }
  722. GFXFence * GFXGLDevice::createFence()
  723. {
  724. GFXFence* fence = _createPlatformSpecificFence();
  725. if(!fence)
  726. fence = new GFXGeneralFence( this );
  727. fence->registerResourceWithDevice(this);
  728. return fence;
  729. }
  730. GFXOcclusionQuery* GFXGLDevice::createOcclusionQuery()
  731. {
  732. GFXOcclusionQuery *query = new GFXGLOcclusionQuery( this );
  733. query->registerResourceWithDevice(this);
  734. return query;
  735. }
  736. void GFXGLDevice::setupGenericShaders( GenericShaderType type )
  737. {
  738. AssertFatal(type != GSTargetRestore, "");
  739. if( mGenericShader[GSColor] == NULL )
  740. {
  741. ShaderData *shaderData;
  742. shaderData = new ShaderData();
  743. shaderData->setField("OGLVertexShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/colorV.glsl"));
  744. shaderData->setField("OGLPixelShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/colorP.glsl"));
  745. shaderData->setField("pixVersion", "2.0");
  746. shaderData->registerObject();
  747. mGenericShader[GSColor] = shaderData->getShader();
  748. mGenericShaderBuffer[GSColor] = mGenericShader[GSColor]->allocConstBuffer();
  749. mModelViewProjSC[GSColor] = mGenericShader[GSColor]->getShaderConstHandle( "$modelView" );
  750. Sim::getRootGroup()->addObject(shaderData);
  751. shaderData = new ShaderData();
  752. shaderData->setField("OGLVertexShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/modColorTextureV.glsl"));
  753. shaderData->setField("OGLPixelShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/modColorTextureP.glsl"));
  754. shaderData->setSamplerName("$diffuseMap", 0);
  755. shaderData->setField("pixVersion", "2.0");
  756. shaderData->registerObject();
  757. mGenericShader[GSModColorTexture] = shaderData->getShader();
  758. mGenericShaderBuffer[GSModColorTexture] = mGenericShader[GSModColorTexture]->allocConstBuffer();
  759. mModelViewProjSC[GSModColorTexture] = mGenericShader[GSModColorTexture]->getShaderConstHandle( "$modelView" );
  760. Sim::getRootGroup()->addObject(shaderData);
  761. shaderData = new ShaderData();
  762. shaderData->setField("OGLVertexShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/addColorTextureV.glsl"));
  763. shaderData->setField("OGLPixelShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/addColorTextureP.glsl"));
  764. shaderData->setSamplerName("$diffuseMap", 0);
  765. shaderData->setField("pixVersion", "2.0");
  766. shaderData->registerObject();
  767. mGenericShader[GSAddColorTexture] = shaderData->getShader();
  768. mGenericShaderBuffer[GSAddColorTexture] = mGenericShader[GSAddColorTexture]->allocConstBuffer();
  769. mModelViewProjSC[GSAddColorTexture] = mGenericShader[GSAddColorTexture]->getShaderConstHandle( "$modelView" );
  770. Sim::getRootGroup()->addObject(shaderData);
  771. shaderData = new ShaderData();
  772. shaderData->setField("OGLVertexShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/textureV.glsl"));
  773. shaderData->setField("OGLPixelShaderFile", ShaderGen::smCommonShaderPath + String("/fixedFunction/gl/textureP.glsl"));
  774. shaderData->setSamplerName("$diffuseMap", 0);
  775. shaderData->setField("pixVersion", "2.0");
  776. shaderData->registerObject();
  777. mGenericShader[GSTexture] = shaderData->getShader();
  778. mGenericShaderBuffer[GSTexture] = mGenericShader[GSTexture]->allocConstBuffer();
  779. mModelViewProjSC[GSTexture] = mGenericShader[GSTexture]->getShaderConstHandle( "$modelView" );
  780. Sim::getRootGroup()->addObject(shaderData);
  781. }
  782. MatrixF tempMatrix = mProjectionMatrix * mViewMatrix * mWorldMatrix[mWorldStackSize];
  783. mGenericShaderBuffer[type]->setSafe(mModelViewProjSC[type], tempMatrix);
  784. setShader( mGenericShader[type] );
  785. setShaderConstBuffer( mGenericShaderBuffer[type] );
  786. }
  787. GFXShader* GFXGLDevice::createShader()
  788. {
  789. GFXGLShader* shader = new GFXGLShader();
  790. shader->registerResourceWithDevice( this );
  791. return shader;
  792. }
  793. void GFXGLDevice::setShader(GFXShader *shader, bool force)
  794. {
  795. if(mCurrentShader == shader && !force)
  796. return;
  797. if ( shader )
  798. {
  799. GFXGLShader *glShader = static_cast<GFXGLShader*>( shader );
  800. glShader->useProgram();
  801. mCurrentShader = shader;
  802. }
  803. else
  804. {
  805. setupGenericShaders();
  806. }
  807. }
  808. void GFXGLDevice::setShaderConstBufferInternal(GFXShaderConstBuffer* buffer)
  809. {
  810. PROFILE_SCOPE(GFXGLDevice_setShaderConstBufferInternal);
  811. static_cast<GFXGLShaderConstBuffer*>(buffer)->activate();
  812. }
  813. U32 GFXGLDevice::getNumSamplers() const
  814. {
  815. return getMin((U32)GFX_TEXTURE_STAGE_COUNT,mPixelShaderVersion > 0.001f ? mMaxShaderTextures : mMaxFFTextures);
  816. }
  817. GFXTextureObject* GFXGLDevice::getDefaultDepthTex() const
  818. {
  819. if(mWindowRT && mWindowRT->getPointer())
  820. return static_cast<GFXGLWindowTarget*>( mWindowRT->getPointer() )->mBackBufferDepthTex.getPointer();
  821. return NULL;
  822. }
  823. U32 GFXGLDevice::getNumRenderTargets() const
  824. {
  825. return mMaxTRColors;
  826. }
  827. void GFXGLDevice::_updateRenderTargets()
  828. {
  829. if ( mRTDirty || mCurrentRT->isPendingState() )
  830. {
  831. if ( mRTDeactivate )
  832. {
  833. mRTDeactivate->deactivate();
  834. mRTDeactivate = NULL;
  835. }
  836. // NOTE: The render target changes is not really accurate
  837. // as the GFXTextureTarget supports MRT internally. So when
  838. // we activate a GFXTarget it could result in multiple calls
  839. // to SetRenderTarget on the actual device.
  840. mDeviceStatistics.mRenderTargetChanges++;
  841. GFXGLTextureTarget *tex = dynamic_cast<GFXGLTextureTarget*>( mCurrentRT.getPointer() );
  842. if ( tex )
  843. {
  844. tex->applyState();
  845. tex->makeActive();
  846. }
  847. else
  848. {
  849. GFXGLWindowTarget *win = dynamic_cast<GFXGLWindowTarget*>( mCurrentRT.getPointer() );
  850. AssertFatal( win != NULL,
  851. "GFXGLDevice::_updateRenderTargets() - invalid target subclass passed!" );
  852. win->makeActive();
  853. if( win->mContext != static_cast<GFXGLDevice*>(GFX)->mContext )
  854. {
  855. mRTDirty = false;
  856. GFX->updateStates(true);
  857. }
  858. }
  859. mRTDirty = false;
  860. }
  861. if ( mViewportDirty )
  862. {
  863. glViewport( mViewport.point.x, mViewport.point.y, mViewport.extent.x, mViewport.extent.y );
  864. mViewportDirty = false;
  865. }
  866. }
  867. GFXFormat GFXGLDevice::selectSupportedFormat( GFXTextureProfile* profile,
  868. const Vector<GFXFormat>& formats,
  869. bool texture,
  870. bool mustblend,
  871. bool mustfilter )
  872. {
  873. for(U32 i = 0; i < formats.size(); i++)
  874. {
  875. // Single channel textures are not supported by FBOs.
  876. if(profile->testFlag(GFXTextureProfile::RenderTarget) && (formats[i] == GFXFormatA8 || formats[i] == GFXFormatL8 || formats[i] == GFXFormatL16))
  877. continue;
  878. if(GFXGLTextureInternalFormat[formats[i]] == GL_ZERO)
  879. continue;
  880. return formats[i];
  881. }
  882. return GFXFormatR8G8B8A8;
  883. }
  884. U32 GFXGLDevice::getTotalVideoMemory_GL_EXT()
  885. {
  886. // Source: http://www.opengl.org/registry/specs/ATI/meminfo.txt
  887. if( gglHasExtension(ATI_meminfo) )
  888. {
  889. GLint mem[4] = {0};
  890. glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, mem); // Retrieve the texture pool
  891. /* With mem[0] i get only the total memory free in the pool in KB
  892. *
  893. * mem[0] - total memory free in the pool
  894. * mem[1] - largest available free block in the pool
  895. * mem[2] - total auxiliary memory free
  896. * mem[3] - largest auxiliary free block
  897. */
  898. return mem[0] / 1024;
  899. }
  900. //source http://www.opengl.org/registry/specs/NVX/gpu_memory_info.txt
  901. else if( gglHasExtension(NVX_gpu_memory_info) )
  902. {
  903. GLint mem = 0;
  904. glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &mem);
  905. return mem / 1024;
  906. }
  907. // TODO OPENGL, add supprt for INTEL cards.
  908. return 0;
  909. }
  910. //
  911. // Register this device with GFXInit
  912. //
  913. class GFXGLRegisterDevice
  914. {
  915. public:
  916. GFXGLRegisterDevice()
  917. {
  918. GFXInit::getRegisterDeviceSignal().notify(&GFXGLDevice::enumerateAdapters);
  919. }
  920. };
  921. static GFXGLRegisterDevice pGLRegisterDevice;
  922. DefineEngineFunction(cycleResources, void, (),, "")
  923. {
  924. static_cast<GFXGLDevice*>(GFX)->zombify();
  925. static_cast<GFXGLDevice*>(GFX)->resurrect();
  926. }