gfxGLDevice.cpp 36 KB

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