llgl.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /**
  2. * @file llgl.cpp
  3. * @brief LLGL implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. // This file sets some global GL parameters, and implements some
  33. // useful functions for GL operations.
  34. #define GLH_EXT_SINGLE_FILE
  35. #include "linden_common.h"
  36. #include "boost/tokenizer.hpp"
  37. #include "llgl.h"
  38. #include "llglslshader.h"
  39. #include "llimagegl.h"
  40. #include "llmath.h"
  41. #include "llquaternion.h"
  42. #include "llrender.h"
  43. #include "llsys.h"
  44. #include "llmatrix4.h"
  45. #if LL_WINDOWS
  46. # include "lldxhardware.h"
  47. #endif
  48. bool gDebugGL = false;
  49. // Global flag for dual-renderer support (EE/WL and PBR). HB
  50. bool gUsePBRShaders = false;
  51. std::list<LLGLUpdate*> LLGLUpdate::sGLQ;
  52. // Utility functions
  53. void log_glerror(const char* file, U32 line, bool crash)
  54. {
  55. // Do not call glGetError() while GL is stopped or not yet initialized. HB
  56. if (!gGL.isValid())
  57. {
  58. return;
  59. }
  60. static std::string filename;
  61. GLenum error = glGetError();
  62. if (LL_UNLIKELY(error))
  63. {
  64. filename.assign(file);
  65. size_t i = filename.find("indra");
  66. if (i != std::string::npos)
  67. {
  68. filename = filename.substr(i);
  69. }
  70. }
  71. while (LL_UNLIKELY(error))
  72. {
  73. std::string gl_error_msg = getGLErrorString(error);
  74. if (crash)
  75. {
  76. llerrs << "GL Error: " << gl_error_msg << " (" << error
  77. << ") - in file: " << filename << " - at line: "
  78. << line << llendl;
  79. }
  80. else
  81. {
  82. llwarns << "GL Error: " << gl_error_msg << " (" << error
  83. << ") - in file: " << filename << " - at line: "
  84. << line << llendl;
  85. }
  86. error = glGetError();
  87. }
  88. }
  89. // There are 7 non-zero error flags, one of them being cleared on each call to
  90. // glGetError(). Normally, all error flags should therefore get cleared after
  91. // at most 7 calls to glGetError() and the 8th call should always return 0...
  92. // See: http://www.opengl.org/sdk/docs/man/xhtml/glGetError.xml
  93. #define MAX_LOOPS 8U
  94. void clear_glerror()
  95. {
  96. // Do not call glGetError() while GL is stopped or not yet initialized. HB
  97. if (!gGL.isValid())
  98. {
  99. return;
  100. }
  101. U32 counter = MAX_LOOPS;
  102. if (LL_UNLIKELY(gDebugGL))
  103. {
  104. GLenum error;
  105. while ((error = glGetError()))
  106. {
  107. if (--counter == 0)
  108. {
  109. llwarns << "glGetError() still returning errors ("
  110. << getGLErrorString(error) <<") after "
  111. << MAX_LOOPS << " consecutive calls." << llendl;
  112. break;
  113. }
  114. else
  115. {
  116. llwarns << "glGetError() returned error: "
  117. << getGLErrorString(error) << llendl;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. // Fast code, for when gDebugGL is false
  124. while (glGetError() && --counter != 0) ;
  125. }
  126. }
  127. const std::string getGLErrorString(U32 error)
  128. {
  129. switch (error)
  130. {
  131. case GL_NO_ERROR:
  132. return "GL_NO_ERROR";
  133. break;
  134. case GL_INVALID_ENUM:
  135. return "GL_INVALID_ENUM";
  136. break;
  137. case GL_INVALID_VALUE:
  138. return "GL_INVALID_VALUE";
  139. break;
  140. case GL_INVALID_OPERATION:
  141. return "GL_INVALID_OPERATION";
  142. break;
  143. case GL_INVALID_FRAMEBUFFER_OPERATION:
  144. return "GL_INVALID_FRAMEBUFFER_OPERATION";
  145. break;
  146. case GL_OUT_OF_MEMORY:
  147. return "GL_OUT_OF_MEMORY";
  148. break;
  149. case GL_STACK_UNDERFLOW:
  150. return "GL_STACK_UNDERFLOW";
  151. break;
  152. case GL_STACK_OVERFLOW:
  153. return "GL_STACK_OVERFLOW";
  154. break;
  155. default:
  156. return llformat("Unknown GL error #%d", error);
  157. }
  158. }
  159. static std::string parse_gl_version(S32& major, S32& minor, S32& release,
  160. std::string& vendor_specific)
  161. {
  162. major = minor = release = 0;
  163. std::string version_string;
  164. // GL_VERSION returns a nul-terminated string with the format:
  165. // <major>.<minor>[.<release>] [<vendor specific>]
  166. const char* version = (const char*)glGetString(GL_VERSION);
  167. if (!version || !*version)
  168. {
  169. vendor_specific.clear();
  170. return version_string;
  171. }
  172. version_string.assign(version);
  173. std::string ver_copy(version);
  174. size_t len = strlen(version);
  175. size_t i = 0;
  176. size_t start;
  177. // Find the major version
  178. start = i;
  179. for ( ; i < len; ++i)
  180. {
  181. if (version[i] == '.')
  182. {
  183. break;
  184. }
  185. }
  186. std::string major_str = ver_copy.substr(start, i - start);
  187. LLStringUtil::convertToS32(major_str, major);
  188. if (version[i] == '.')
  189. {
  190. ++i;
  191. }
  192. // Find the minor version
  193. start = i;
  194. for ( ; i < len; ++i)
  195. {
  196. if (version[i] == '.' || isspace(version[i]))
  197. {
  198. break;
  199. }
  200. }
  201. std::string minor_str = ver_copy.substr(start, i - start);
  202. LLStringUtil::convertToS32(minor_str, minor);
  203. // Find the release number (optional)
  204. if (version[i] == '.')
  205. {
  206. ++i;
  207. start = i;
  208. for ( ; i < len; ++i)
  209. {
  210. if (isspace(version[i]))
  211. {
  212. break;
  213. }
  214. }
  215. std::string release_str = ver_copy.substr(start, i - start);
  216. LLStringUtil::convertToS32(release_str, release);
  217. }
  218. // Skip over any white space
  219. while (version[i] && isspace(version[i]))
  220. {
  221. ++i;
  222. }
  223. // Copy the vendor-specific string (optional)
  224. if (version[i])
  225. {
  226. vendor_specific.assign(version + i);
  227. }
  228. return version_string;
  229. }
  230. static void parse_glsl_version(S32& major, S32& minor)
  231. {
  232. major = minor = 0;
  233. // GL_SHADING_LANGUAGE_VERSION returns a nul-terminated string with the
  234. // format: <major>.<minor>[.<release>] [<vendor specific>]
  235. const char* version =
  236. (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
  237. if (!version)
  238. {
  239. return;
  240. }
  241. std::string ver_copy(version);
  242. size_t len = strlen(version);
  243. size_t i = 0;
  244. size_t start;
  245. // Find the major version
  246. start = i;
  247. for ( ; i < len; ++i)
  248. {
  249. if (version[i] == '.')
  250. {
  251. break;
  252. }
  253. }
  254. std::string major_str = ver_copy.substr(start, i - start);
  255. LLStringUtil::convertToS32(major_str, major);
  256. if (version[i] == '.')
  257. {
  258. i++;
  259. }
  260. // Find the minor version
  261. start = i;
  262. for ( ; i < len; ++i)
  263. {
  264. if (version[i] == '.' || isspace(version[i]))
  265. {
  266. break;
  267. }
  268. }
  269. std::string minor_str = ver_copy.substr(start, i - start);
  270. LLStringUtil::convertToS32(minor_str, minor);
  271. }
  272. ///////////////////////////////////////////////////////////////////////////////
  273. // LLGLManager class
  274. ///////////////////////////////////////////////////////////////////////////////
  275. LLGLManager gGLManager;
  276. LLGLManager::LLGLManager()
  277. : mInited(false),
  278. mIsDisabled(false),
  279. #if LL_WINDOWS
  280. mHasAMDAssociations(false),
  281. #endif
  282. mHasATIMemInfo(false),
  283. mHasNVXMemInfo(false),
  284. mMaxSamples(0),
  285. mHasSync(false),
  286. mHasVertexArrayObject(false),
  287. mNumTextureImageUnits(1),
  288. mMaxUniformBlockSize(16384),
  289. mMaxAnisotropy(1.f),
  290. mHasOcclusionQuery2(false),
  291. mHasTimerQuery(false),
  292. mHasDepthClamp(false),
  293. mHasAnisotropic(false),
  294. mHasCubeMapArray(false),
  295. mHasDebugOutput(false),
  296. mHasTextureSwizzle(false),
  297. mHasGpuShader4(false),
  298. mHasGpuShader5(false),
  299. mUseDepthClamp(false),
  300. mIsAMD(false),
  301. mIsNVIDIA(false),
  302. mIsIntel(false),
  303. mHasVertexAttribIPointer(false),
  304. mHasRequirements(true),
  305. mDriverVersionMajor(1),
  306. mDriverVersionMinor(0),
  307. mDriverVersionRelease(0),
  308. mGLVersion(1.f),
  309. mGLSLVersionMajor(0),
  310. mGLSLVersionMinor(0),
  311. mVRAM(0),
  312. mGLMaxVertexRange(0),
  313. mGLMaxIndexRange(0)
  314. {
  315. }
  316. //---------------------------------------------------------------------
  317. // Global initialization for GL
  318. //---------------------------------------------------------------------
  319. #if LL_WINDOWS
  320. void LLGLManager::initWGL(HDC dc)
  321. {
  322. if (!epoxy_has_wgl_extension(dc, "WGL_ARB_pixel_format"))
  323. {
  324. llwarns << "No ARB pixel format extensions" << llendl;
  325. }
  326. if (!epoxy_has_wgl_extension(dc, "WGL_ARB_create_context"))
  327. {
  328. llwarns << "No ARB create context extensions" << llendl;
  329. }
  330. mHasAMDAssociations =
  331. epoxy_has_wgl_extension(dc, "WGL_AMD_gpu_association");
  332. }
  333. #endif
  334. // Return false if unable (or unwilling due to old drivers) to init GL
  335. bool LLGLManager::initGL()
  336. {
  337. if (mInited) // Should never happen.
  338. {
  339. llerrs << "GL manager already initialized !" << llendl;
  340. }
  341. // Extract video card strings and convert to upper case to work around
  342. // driver-to-driver variation in capitalization.
  343. mGLVendor = ll_safe_string((const char*)glGetString(GL_VENDOR));
  344. LLStringUtil::toUpper(mGLVendor);
  345. mGLRenderer = ll_safe_string((const char*)glGetString(GL_RENDERER));
  346. LLStringUtil::toUpper(mGLRenderer);
  347. mGLVersionString = parse_gl_version(mDriverVersionMajor,
  348. mDriverVersionMinor,
  349. mDriverVersionRelease,
  350. mDriverVersionVendorString);
  351. mGLVersion = mDriverVersionMajor + mDriverVersionMinor * 0.1f;
  352. llinfos << "Advertised OpenGL version: " << mDriverVersionMajor << "."
  353. << mDriverVersionMinor << llendl;
  354. // We do not support OpenGL below v2.0 any more.
  355. if (mGLVersion < 2.f)
  356. {
  357. mHasRequirements = false;
  358. llwarns << "Graphics driver is too old: OpenGL v2.0 minimum is required"
  359. << llendl;
  360. return false;
  361. }
  362. parse_glsl_version(mGLSLVersionMajor, mGLSLVersionMinor);
  363. llinfos << "Advertised GLSL version: " << mGLSLVersionMajor << "."
  364. << mGLSLVersionMinor << llendl;
  365. #if LL_DARWIN
  366. // Limit the GLSL version to something compatible under macOS
  367. if (LLRender::sGLCoreProfile)
  368. {
  369. if (mGLVersion < 3.3f &&
  370. (mGLSLVersionMajor > 1 || mGLSLVersionMinor > 40))
  371. {
  372. mGLSLVersionMajor = 1;
  373. mGLSLVersionMinor = 40;
  374. llinfos << "Capped to GLSL v1.40." << llendl;
  375. }
  376. }
  377. else if (mGLSLVersionMajor > 1 || mGLSLVersionMinor > 20)
  378. {
  379. mGLSLVersionMajor = 1;
  380. mGLSLVersionMinor = 20;
  381. llinfos << "Capped to GLSL v1.20." << llendl;
  382. }
  383. #endif
  384. // We do not use fixed GL functions any more so we need at the minimum
  385. // support for GLSL v1.10 so to load our basic shaders.
  386. if (mGLSLVersionMajor < 2 && mGLSLVersionMinor < 10)
  387. {
  388. mHasRequirements = false;
  389. llwarns << "Graphics driver is too old: GLSL v1.10 minimum is required"
  390. << llendl;
  391. return false;
  392. }
  393. if (mGLVersion >= 2.1f && LLImageGL::sCompressTextures)
  394. {
  395. // Use texture compression
  396. glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
  397. }
  398. else
  399. {
  400. // Always disable texture compression
  401. LLImageGL::sCompressTextures = false;
  402. }
  403. if (mGLVendor.find("NVIDIA ") != std::string::npos)
  404. {
  405. mGLVendorShort = "NVIDIA";
  406. mIsNVIDIA = true;
  407. }
  408. else if (mGLVendor.find("INTEL") != std::string::npos
  409. #if LL_LINUX
  410. // The Mesa-based drivers put this in the Renderer string, not the
  411. // Vendor string.
  412. || mGLRenderer.find("INTEL") != std::string::npos
  413. #endif
  414. )
  415. {
  416. mGLVendorShort = "INTEL";
  417. mIsIntel = true;
  418. }
  419. // AMD is tested last, since there is more risks than with other vendors to
  420. // see the three letters composing the name appearing in another vendor's
  421. // GL driver name... HB
  422. // Trailing space necessary to keep "nVidia Corpor_ati_on" cards from being
  423. // recognized as ATI/AMD.
  424. // Note: AMD has been pretty good about not breaking this check, do not
  425. // rename without a good reason.
  426. else if (mGLVendor.substr(0, 4) == "ATI ")
  427. {
  428. mGLVendorShort = "AMD";
  429. mIsAMD = true;
  430. }
  431. else
  432. {
  433. mGLVendorShort = "MISC";
  434. }
  435. // This is called here because it may depend on above settings.
  436. initExtensions();
  437. if (!mHasRequirements)
  438. {
  439. // We do not support cards that do not support the
  440. // GL_ARB_framebuffer_object extension
  441. llwarns << "GL driver does not support GL_ARB_framebuffer_object"
  442. << llendl;
  443. return false;
  444. }
  445. if (mHasAnisotropic)
  446. {
  447. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &mMaxAnisotropy);
  448. mMaxAnisotropy = llmax(1.f, mMaxAnisotropy);
  449. llinfos << "Max anisotropy: " << mMaxAnisotropy << llendl;
  450. }
  451. S32 old_vram = mVRAM;
  452. mVRAM = mTexVRAM = 0;
  453. #if LL_WINDOWS
  454. if (mHasAMDAssociations)
  455. {
  456. GLuint gl_gpus_count = wglGetGPUIDsAMD(0, 0);
  457. if (gl_gpus_count > 0)
  458. {
  459. GLuint* ids = new GLuint[gl_gpus_count];
  460. wglGetGPUIDsAMD(gl_gpus_count, ids);
  461. GLuint mem_mb = 0;
  462. for (U32 i = 0; i < gl_gpus_count; ++i)
  463. {
  464. wglGetGPUInfoAMD(ids[i], WGL_GPU_RAM_AMD, GL_UNSIGNED_INT,
  465. sizeof(GLuint), &mem_mb);
  466. if (mVRAM < mem_mb)
  467. {
  468. // Basically pick the best AMD and trust driver/OS to know
  469. // to switch
  470. mVRAM = mem_mb;
  471. }
  472. }
  473. }
  474. if (mVRAM)
  475. {
  476. llinfos << "Detected VRAM via AMDAssociations: " << mVRAM
  477. << llendl;
  478. }
  479. }
  480. #endif
  481. if (mHasATIMemInfo)
  482. {
  483. // Ask GL how much VRAM is free for textures at startup
  484. GLint meminfo[4];
  485. glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, meminfo);
  486. mTexVRAM = meminfo[0] / 1024;
  487. llinfos << "Detected free VRAM for textures via ATIMemInfo: "
  488. << mTexVRAM << " MB." << llendl;
  489. }
  490. else if (mHasNVXMemInfo)
  491. {
  492. GLint meminfo;
  493. glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &meminfo);
  494. mVRAM = meminfo / 1024;
  495. glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX,
  496. &meminfo);
  497. mTexVRAM = meminfo / 1024;
  498. llinfos << "Detected VRAM via NVXMemInfo: Total = " << mVRAM
  499. << " MB - Free for textures: " << mTexVRAM << " MB." << llendl;
  500. }
  501. #if LL_WINDOWS
  502. if (mVRAM < 256)
  503. {
  504. // Something likely went wrong using the above extensions...
  505. // Try via DXGI which will check all GPUs it knows of and will pick up
  506. // the one with most memory (i.e. we assume the most powerful one),
  507. // which will *likely* be the one the OS will pick up to render SL.
  508. S32 mem = LLDXHardware::getMBVideoMemoryViaDXGI();
  509. if (mem > 0)
  510. {
  511. mVRAM = mem;
  512. llinfos << "Detected VRAM via DXGI: " << mVRAM << llendl;
  513. }
  514. }
  515. #endif
  516. if (mVRAM < 256)
  517. {
  518. if (old_vram > mVRAM)
  519. {
  520. // Fall back to old method
  521. mVRAM = old_vram;
  522. }
  523. else if (mTexVRAM > 0)
  524. {
  525. mVRAM = 4 * mTexVRAM / 3;
  526. llinfos << "Estimating total VRAM based on reported free VRAM for textures (this is inaccurate): "
  527. << mVRAM << " MB." << llendl;
  528. }
  529. }
  530. if (mTexVRAM <= 0)
  531. {
  532. mTexVRAM = mVRAM / 2;
  533. llinfos << "Estimating usable VRAM for textures based on reported total VRAM (this is inaccurate): "
  534. << mTexVRAM << " MB." << llendl;
  535. }
  536. stop_glerror();
  537. GLint value;
  538. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &value);
  539. mNumTextureImageUnits = llmin(value, 32);
  540. glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &value);
  541. // Clamp down to 64K (maximum observed for modern hardware/drivers), just
  542. // in case some implementation would report a crazy value.
  543. mMaxUniformBlockSize = llmin(value, 65536);
  544. // Note: min block size should be 16384...
  545. if (mMaxUniformBlockSize < 16384)
  546. {
  547. llwarns << "GL_MAX_UNIFORM_BLOCK_SIZE is very small on this GPU: "
  548. << mMaxUniformBlockSize << llendl;
  549. }
  550. stop_glerror();
  551. glGetIntegerv(GL_MAX_SAMPLES, &mMaxSamples);
  552. stop_glerror();
  553. initGLStates();
  554. return true;
  555. }
  556. void LLGLManager::initGLStates()
  557. {
  558. LLGLState::initClass();
  559. stop_glerror();
  560. }
  561. void LLGLManager::getGLInfo(LLSD& info)
  562. {
  563. info["GLInfo"]["GLVendor"] =
  564. ll_safe_string((const char*)glGetString(GL_VENDOR));
  565. info["GLInfo"]["GLRenderer"] =
  566. ll_safe_string((const char*)glGetString(GL_RENDERER));
  567. info["GLInfo"]["GLVersion"] =
  568. ll_safe_string((const char*)glGetString(GL_VERSION));
  569. std::string all_exts =
  570. ll_safe_string((const char*)glGetString(GL_EXTENSIONS));
  571. boost::char_separator<char> sep(" ");
  572. boost::tokenizer<boost::char_separator<char> > tok(all_exts, sep);
  573. for (boost::tokenizer<boost::char_separator<char> >::iterator
  574. i = tok.begin(); i != tok.end(); ++i)
  575. {
  576. info["GLInfo"]["GLExtensions"].append(*i);
  577. }
  578. }
  579. void LLGLManager::printGLInfoString()
  580. {
  581. llinfos << "GL_VENDOR : "
  582. << ll_safe_string((const char*)glGetString(GL_VENDOR))
  583. << llendl;
  584. llinfos << "GL_RENDERER: "
  585. << ll_safe_string((const char*)glGetString(GL_RENDERER))
  586. << llendl;
  587. llinfos << "GL_VERSION : "
  588. << ll_safe_string((const char*)glGetString(GL_VERSION))
  589. << llendl;
  590. std::string all_exts =
  591. ll_safe_string((const char*)glGetString(GL_EXTENSIONS));
  592. LLStringUtil::replaceChar(all_exts, ' ', '\n');
  593. LL_DEBUGS("RenderInit") << "GL_EXTENSIONS:\n" << all_exts << LL_ENDL;
  594. }
  595. std::string LLGLManager::getRawGLString()
  596. {
  597. return ll_safe_string((char*)glGetString(GL_VENDOR)) + " " +
  598. ll_safe_string((char*)glGetString(GL_RENDERER));
  599. }
  600. void LLGLManager::asLLSD(LLSD& info)
  601. {
  602. // Currently these are duplicates of fields in LLViewerStats "system" info
  603. info["gpu_vendor"] = mGLVendorShort;
  604. info["gpu_version"] = mDriverVersionVendorString;
  605. info["opengl_version"] = mGLVersionString;
  606. info["gl_renderer"] = mGLRenderer;
  607. // Vendor
  608. info["is_ati"] = mIsAMD;
  609. info["is_intel"] = mIsIntel;
  610. info["is_nvidia"] = mIsNVIDIA;
  611. // Limits
  612. info["vram"] = mVRAM;
  613. info["num_texture_image_units"] = mNumTextureImageUnits;
  614. info["max_samples"] = mMaxSamples;
  615. info["max_vertex_range"] = mGLMaxVertexRange;
  616. info["max_index_range"] = mGLMaxIndexRange;
  617. info["max_texture_size"] = mGLMaxTextureSize;
  618. // Extensions
  619. info["has_vertex_array_object"] = mHasVertexArrayObject;
  620. info["has_sync"] = mHasSync;
  621. info["has_timer_query"] = mHasTimerQuery;
  622. info["has_occlusion_query2"] = mHasOcclusionQuery2;
  623. info["has_depth_clamp"] = mHasDepthClamp;
  624. info["has_anisotropic"] = mHasAnisotropic;
  625. info["has_cubemap_array"] = mHasCubeMapArray;
  626. info["has_debug_output"] = mHasDebugOutput;
  627. info["has_nvx_mem_info"] = mHasNVXMemInfo;
  628. info["has_ati_mem_info"] = mHasATIMemInfo;
  629. // Got requirements for our renderer ?
  630. info["has_requirements"] = mHasRequirements;
  631. }
  632. void LLGLManager::shutdownGL()
  633. {
  634. if (mInited)
  635. {
  636. glFinish();
  637. stop_glerror();
  638. mInited = false;
  639. }
  640. }
  641. void LLGLManager::initExtensions()
  642. {
  643. mHasATIMemInfo = epoxy_has_gl_extension("GL_ATI_meminfo");
  644. mHasNVXMemInfo = epoxy_has_gl_extension("GL_NVX_gpu_memory_info");
  645. mHasAnisotropic = mGLVersion >= 4.6f ||
  646. epoxy_has_gl_extension("GL_EXT_texture_filter_anisotropic");
  647. mHasOcclusionQuery2 = mGLVersion >= 3.3f ||
  648. epoxy_has_gl_extension("GL_ARB_occlusion_query2");
  649. mHasTimerQuery = mGLVersion >= 3.3f ||
  650. epoxy_has_gl_extension("GL_ARB_timer_query");
  651. mHasVertexArrayObject = mGLVersion >= 3.f ||
  652. epoxy_has_gl_extension("GL_ARB_vertex_array_object");
  653. mHasSync = mGLVersion >= 3.2f || epoxy_has_gl_extension("GL_ARB_sync");
  654. mHasDepthClamp = mGLVersion >= 3.2f ||
  655. epoxy_has_gl_extension("GL_ARB_depth_clamp") ||
  656. epoxy_has_gl_extension("GL_NV_depth_clamp");
  657. if (!mHasDepthClamp)
  658. {
  659. mUseDepthClamp = false;
  660. }
  661. // Mask out FBO support when packed_depth_stencil is not there because we
  662. // need it for LLRenderTarget. Brad
  663. #if GL_ARB_framebuffer_object
  664. mHasRequirements =
  665. mGLVersion >= 3.f ||
  666. epoxy_has_gl_extension("GL_ARB_framebuffer_object");
  667. #else
  668. mHasRequirements =
  669. mGLVersion >= 3.f ||
  670. (epoxy_has_gl_extension("GL_EXT_framebuffer_object") &&
  671. epoxy_has_gl_extension("GL_EXT_framebuffer_blit") &&
  672. epoxy_has_gl_extension("GL_EXT_framebuffer_multisample") &&
  673. epoxy_has_gl_extension("GL_EXT_packed_depth_stencil"));
  674. #endif
  675. mHasCubeMapArray = mGLVersion >= 4.f;
  676. mHasDebugOutput = mGLVersion >= 4.3f ||
  677. epoxy_has_gl_extension("GL_ARB_debug_output");
  678. mHasVertexAttribIPointer = mGLSLVersionMajor > 1 ||
  679. mGLSLVersionMinor >= 30;
  680. mHasGpuShader4 = mGLVersion >= 3.f &&
  681. epoxy_has_gl_extension("GL_ARB_gpu_shader4");
  682. #if GL_ARB_gpu_shader5
  683. mHasGpuShader5 = epoxy_has_gl_extension("GL_ARB_gpu_shader5");
  684. #endif
  685. #if GL_ARB_texture_swizzle
  686. mHasTextureSwizzle = mGLVersion >= 3.3f ||
  687. epoxy_has_gl_extension("GL_ARB_texture_swizzle");
  688. #endif
  689. if (!mHasSync)
  690. {
  691. llinfos << "This GL implementation lacks GL_ARB_sync" << llendl;
  692. }
  693. if (!mHasAnisotropic)
  694. {
  695. llinfos << "Could not initialize anisotropic filtering" << llendl;
  696. }
  697. if (!mHasOcclusionQuery2)
  698. {
  699. llinfos << "Could not initialize GL_ARB_occlusion_query2" << llendl;
  700. }
  701. // Note: GL_ARB_vertex_array_object should exist in core GL profile v3.0+
  702. if (!mHasVertexArrayObject && LLRender::sGLCoreProfile)
  703. {
  704. llinfos << "Could not initialize GL_ARB_vertex_array_object" << llendl;
  705. }
  706. // Misc
  707. glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, (GLint*)&mGLMaxVertexRange);
  708. glGetIntegerv(GL_MAX_ELEMENTS_INDICES, (GLint*)&mGLMaxIndexRange);
  709. glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&mGLMaxTextureSize);
  710. mInited = true;
  711. clear_glerror();
  712. }
  713. ///////////////////////////////////////////////////////////////////////////////
  714. // LLGLState class
  715. ///////////////////////////////////////////////////////////////////////////////
  716. // Static members
  717. LLGLState::state_map_t LLGLState::sStateMap;
  718. GLboolean LLGLDepthTest::sDepthEnabled = GL_FALSE; // OpenGL default
  719. U32 LLGLDepthTest::sDepthFunc = GL_LESS; // OpenGL default
  720. GLboolean LLGLDepthTest::sWriteEnabled = GL_TRUE; // OpenGL default
  721. //static
  722. void LLGLState::initClass()
  723. {
  724. sStateMap[GL_DITHER] = GL_TRUE;
  725. // Make sure multisample defaults to disabled
  726. sStateMap[GL_MULTISAMPLE] = GL_FALSE;
  727. glDisable(GL_MULTISAMPLE);
  728. }
  729. //static
  730. void LLGLState::restoreGL()
  731. {
  732. sStateMap.clear();
  733. initClass();
  734. }
  735. #if 0 // Not used, but kept in source, just in case... HB
  736. //static
  737. void LLGLState::resetTextureStates()
  738. {
  739. gGL.flush();
  740. GLint max_tex_units;
  741. glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_tex_units);
  742. for (S32 j = max_tex_units - 1; j >= 0; --j)
  743. {
  744. LLTexUnit* unitp = gGL.getTexUnit(j);
  745. unitp->activate();
  746. glClientActiveTexture(GL_TEXTURE0 + j);
  747. if (j == 0)
  748. {
  749. unitp->enable(LLTexUnit::TT_TEXTURE);
  750. }
  751. else
  752. {
  753. unitp->disable();
  754. }
  755. }
  756. }
  757. #endif
  758. void LLGLState::dumpStates()
  759. {
  760. llinfos << "GL States:";
  761. for (state_map_t::iterator iter = sStateMap.begin(), end = sStateMap.end();
  762. iter != end; ++iter)
  763. {
  764. llcont << llformat("\n 0x%04x : %s", (S32)iter->first,
  765. iter->second ? "true" : "false");
  766. }
  767. llcont << llendl;
  768. }
  769. //static
  770. void LLGLState::checkStates(const std::string& msg, S32 line)
  771. {
  772. if (!gDebugGL)
  773. {
  774. return;
  775. }
  776. stop_glerror();
  777. static std::string errors;
  778. if (glIsEnabled(GL_BLEND))
  779. {
  780. GLint src;
  781. glGetIntegerv(GL_BLEND_SRC, &src);
  782. GLint dst;
  783. glGetIntegerv(GL_BLEND_DST, &dst);
  784. if (src != GL_SRC_ALPHA || dst != GL_ONE_MINUS_SRC_ALPHA)
  785. {
  786. errors =
  787. llformat("Blend function corrupted: source: 0x%04x, destination: 0x%04x",
  788. src, dst);
  789. }
  790. }
  791. bool has_state_error = false;
  792. for (state_map_t::iterator iter = sStateMap.begin(), end = sStateMap.end();
  793. iter != end; ++iter)
  794. {
  795. U32 state = iter->first;
  796. GLboolean cur_state = iter->second;
  797. GLboolean gl_state = glIsEnabled(state);
  798. if (cur_state != gl_state)
  799. {
  800. has_state_error = true;
  801. if (!errors.empty())
  802. {
  803. errors.append(" - ");
  804. }
  805. errors += llformat("Incoherent state: 0x%04x", state);
  806. }
  807. }
  808. if (has_state_error)
  809. {
  810. dumpStates();
  811. }
  812. if (!errors.empty())
  813. {
  814. llwarns << errors;
  815. if (!msg.empty())
  816. {
  817. llcont << " - " << msg;
  818. }
  819. if (line > 0)
  820. {
  821. llcont << " - line " << line;
  822. }
  823. llcont << llendl;
  824. errors.clear();
  825. }
  826. }
  827. LLGLState::LLGLState(U32 state, S32 enabled)
  828. : mState(state),
  829. mWasEnabled(GL_FALSE),
  830. mIsEnabled(GL_FALSE)
  831. {
  832. // Always ignore any state deprecated post GL 3.0
  833. switch (state)
  834. {
  835. case GL_STENCIL_TEST:
  836. if (gUsePBRShaders)
  837. {
  838. llerrs << "GL_STENCIL_TEST used in PBR rendering mode !"
  839. << llendl;
  840. }
  841. break;
  842. case GL_ALPHA_TEST:
  843. case GL_NORMALIZE:
  844. case GL_TEXTURE_GEN_R:
  845. case GL_TEXTURE_GEN_S:
  846. case GL_TEXTURE_GEN_T:
  847. case GL_TEXTURE_GEN_Q:
  848. case GL_LIGHTING:
  849. case GL_COLOR_MATERIAL:
  850. case GL_FOG:
  851. case GL_LINE_STIPPLE:
  852. case GL_POLYGON_STIPPLE:
  853. mState = 0;
  854. llwarns_once << "Asked for a deprecated GL state: " << state
  855. << llendl;
  856. llassert(false);
  857. }
  858. if (mState)
  859. {
  860. mWasEnabled = sStateMap[state];
  861. setEnabled(enabled);
  862. stop_glerror();
  863. }
  864. }
  865. void LLGLState::setEnabled(S32 enabled)
  866. {
  867. stop_glerror();
  868. if (!mState)
  869. {
  870. return;
  871. }
  872. if (enabled == CURRENT_STATE)
  873. {
  874. enabled = sStateMap[mState] == GL_TRUE ? GL_TRUE : GL_FALSE;
  875. }
  876. else if (enabled == GL_TRUE && sStateMap[mState] != GL_TRUE)
  877. {
  878. gGL.flush();
  879. glEnable(mState);
  880. sStateMap[mState] = GL_TRUE;
  881. stop_glerror();
  882. }
  883. else if (enabled == GL_FALSE && sStateMap[mState] != GL_FALSE)
  884. {
  885. gGL.flush();
  886. glDisable(mState);
  887. sStateMap[mState] = GL_FALSE;
  888. stop_glerror();
  889. }
  890. mIsEnabled = enabled;
  891. }
  892. //virtual
  893. LLGLState::~LLGLState()
  894. {
  895. if (mState)
  896. {
  897. if (gDebugGL)
  898. {
  899. GLboolean state = glIsEnabled(mState);
  900. if (sStateMap[mState] != state)
  901. {
  902. llwarns_once << "Mismatch for state: " << std::hex << mState
  903. << std::dec << " - Actual status: " << state
  904. << " (should be " << sStateMap[mState] << ")."
  905. << llendl;
  906. }
  907. }
  908. if (mIsEnabled != mWasEnabled)
  909. {
  910. gGL.flush();
  911. if (mWasEnabled)
  912. {
  913. glEnable(mState);
  914. sStateMap[mState] = GL_TRUE;
  915. }
  916. else
  917. {
  918. glDisable(mState);
  919. sStateMap[mState] = GL_FALSE;
  920. }
  921. stop_glerror();
  922. }
  923. }
  924. }
  925. ///////////////////////////////////////////////////////////////////////////////
  926. // LLGLUserClipPlane class
  927. ///////////////////////////////////////////////////////////////////////////////
  928. LLGLUserClipPlane::LLGLUserClipPlane(const LLPlane& p, const LLMatrix4a& mdlv,
  929. const LLMatrix4a& proj, bool apply)
  930. : mApply(apply)
  931. {
  932. if (apply)
  933. {
  934. mModelview = mdlv;
  935. mProjection = proj;
  936. // Flip incoming LLPlane to get consistent behavior compared to frustum
  937. // culling
  938. setPlane(-p[0], -p[1], -p[2], -p[3]);
  939. }
  940. }
  941. LLGLUserClipPlane::~LLGLUserClipPlane()
  942. {
  943. disable();
  944. }
  945. void LLGLUserClipPlane::disable()
  946. {
  947. if (mApply)
  948. {
  949. mApply = false;
  950. gGL.matrixMode(LLRender::MM_PROJECTION);
  951. gGL.popMatrix();
  952. gGL.matrixMode(LLRender::MM_MODELVIEW);
  953. }
  954. }
  955. void LLGLUserClipPlane::setPlane(F32 a, F32 b, F32 c, F32 d)
  956. {
  957. LLMatrix4a& p = mProjection;
  958. LLMatrix4a& m = mModelview;
  959. LLMatrix4a invtrans_mdlv;
  960. invtrans_mdlv.setMul(p, m);
  961. invtrans_mdlv.invert();
  962. invtrans_mdlv.transpose();
  963. LLVector4a oplane(a, b, c, d);
  964. LLVector4a cplane, cplane_splat, cplane_neg;
  965. invtrans_mdlv.rotate4(oplane, cplane);
  966. cplane_splat.splat<2>(cplane);
  967. cplane_splat.setAbs(cplane_splat);
  968. cplane.div(cplane_splat);
  969. cplane.sub(LLVector4a(0.f, 0.f, 0.f, 1.f));
  970. cplane_splat.splat<2>(cplane);
  971. cplane_neg = cplane;
  972. cplane_neg.negate();
  973. cplane.setSelectWithMask(cplane_splat.lessThan(_mm_setzero_ps()),
  974. cplane_neg, cplane);
  975. LLMatrix4a suffix;
  976. suffix.setIdentity();
  977. suffix.setColumn<2>(cplane);
  978. LLMatrix4a new_proj;
  979. new_proj.setMul(suffix, p);
  980. gGL.matrixMode(LLRender::MM_PROJECTION);
  981. gGL.pushMatrix();
  982. gGL.loadMatrix(new_proj);
  983. gGL.matrixMode(LLRender::MM_MODELVIEW);
  984. }
  985. ///////////////////////////////////////////////////////////////////////////////
  986. // LLGLDepthTest class
  987. ///////////////////////////////////////////////////////////////////////////////
  988. LLGLDepthTest::LLGLDepthTest(GLboolean depth_enabled, GLboolean write_enabled,
  989. U32 depth_func, bool ignored)
  990. : mPrevDepthEnabled(sDepthEnabled),
  991. mPrevDepthFunc(sDepthFunc),
  992. mPrevWriteEnabled(sWriteEnabled),
  993. mIgnored(ignored)
  994. {
  995. if (ignored)
  996. {
  997. // Do nothing.
  998. return;
  999. }
  1000. stop_glerror();
  1001. checkState();
  1002. if (!depth_enabled)
  1003. {
  1004. // Always disable depth writes if depth testing is disabled. GL spec
  1005. // defines this as a requirement, but some implementations allow depth
  1006. // writes with testing disabled. The proper way to write to depth
  1007. // buffer with testing disabled is to enable testing and use a
  1008. // depth_func of GL_ALWAYS
  1009. write_enabled = GL_FALSE;
  1010. }
  1011. if (depth_enabled != sDepthEnabled)
  1012. {
  1013. gGL.flush();
  1014. if (depth_enabled)
  1015. {
  1016. glEnable(GL_DEPTH_TEST);
  1017. }
  1018. else
  1019. {
  1020. glDisable(GL_DEPTH_TEST);
  1021. }
  1022. sDepthEnabled = depth_enabled;
  1023. }
  1024. if (depth_func != sDepthFunc)
  1025. {
  1026. gGL.flush();
  1027. glDepthFunc(depth_func);
  1028. sDepthFunc = depth_func;
  1029. }
  1030. if (write_enabled != sWriteEnabled)
  1031. {
  1032. gGL.flush();
  1033. glDepthMask(write_enabled);
  1034. sWriteEnabled = write_enabled;
  1035. }
  1036. stop_glerror();
  1037. }
  1038. LLGLDepthTest::~LLGLDepthTest()
  1039. {
  1040. if (mIgnored)
  1041. {
  1042. // Nothing to do.
  1043. return;
  1044. }
  1045. checkState();
  1046. if (sDepthEnabled != mPrevDepthEnabled)
  1047. {
  1048. gGL.flush();
  1049. if (mPrevDepthEnabled)
  1050. {
  1051. glEnable(GL_DEPTH_TEST);
  1052. }
  1053. else
  1054. {
  1055. glDisable(GL_DEPTH_TEST);
  1056. }
  1057. sDepthEnabled = mPrevDepthEnabled;
  1058. }
  1059. if (sDepthFunc != mPrevDepthFunc)
  1060. {
  1061. gGL.flush();
  1062. glDepthFunc(mPrevDepthFunc);
  1063. sDepthFunc = mPrevDepthFunc;
  1064. }
  1065. if (sWriteEnabled != mPrevWriteEnabled)
  1066. {
  1067. gGL.flush();
  1068. glDepthMask(mPrevWriteEnabled);
  1069. sWriteEnabled = mPrevWriteEnabled;
  1070. }
  1071. stop_glerror();
  1072. }
  1073. void LLGLDepthTest::checkState()
  1074. {
  1075. if (gDebugGL && !mIgnored)
  1076. {
  1077. GLint func = 0;
  1078. GLboolean mask = GL_FALSE;
  1079. glGetIntegerv(GL_DEPTH_FUNC, &func);
  1080. glGetBooleanv(GL_DEPTH_WRITEMASK, &mask);
  1081. if (glIsEnabled(GL_DEPTH_TEST) != sDepthEnabled ||
  1082. sWriteEnabled != mask || (GLint)sDepthFunc != func)
  1083. {
  1084. llwarns << "Unexpected depth testing state." << llendl;
  1085. }
  1086. }
  1087. }
  1088. ///////////////////////////////////////////////////////////////////////////////
  1089. // LLGLSquashToFarClip class
  1090. ///////////////////////////////////////////////////////////////////////////////
  1091. LLGLSquashToFarClip::LLGLSquashToFarClip(U32 layer)
  1092. {
  1093. F32 depth = 0.99999f - 0.0001f * layer;
  1094. LLMatrix4a proj = gGLProjection;
  1095. LLVector4a col = proj.getColumn<3>();
  1096. col.mul(depth);
  1097. proj.setColumn<2>(col);
  1098. U32 last_matrix_mode = gGL.getMatrixMode();
  1099. gGL.matrixMode(LLRender::MM_PROJECTION);
  1100. gGL.pushMatrix();
  1101. gGL.loadMatrix(proj);
  1102. gGL.matrixMode(last_matrix_mode);
  1103. }
  1104. LLGLSquashToFarClip::~LLGLSquashToFarClip()
  1105. {
  1106. U32 last_matrix_mode = gGL.getMatrixMode();
  1107. gGL.matrixMode(LLRender::MM_PROJECTION);
  1108. gGL.popMatrix();
  1109. gGL.matrixMode(last_matrix_mode);
  1110. }
  1111. ///////////////////////////////////////////////////////////////////////////////
  1112. // LLGLSPipeline*SkyBox classes
  1113. ///////////////////////////////////////////////////////////////////////////////
  1114. LLGLSPipelineSkyBox::LLGLSPipelineSkyBox()
  1115. : mCullFace(GL_CULL_FACE),
  1116. mSquashClip()
  1117. {
  1118. }
  1119. LLGLSPipelineDepthTestSkyBox::LLGLSPipelineDepthTestSkyBox(GLboolean depth_test,
  1120. GLboolean depth_write)
  1121. : LLGLSPipelineSkyBox(),
  1122. mDepth(depth_test, depth_write, GL_LEQUAL)
  1123. {
  1124. }
  1125. LLGLSPipelineBlendSkyBox::LLGLSPipelineBlendSkyBox(GLboolean depth_test,
  1126. GLboolean depth_write)
  1127. : LLGLSPipelineDepthTestSkyBox(depth_test, depth_write),
  1128. mBlend(GL_BLEND)
  1129. {
  1130. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1131. }