llgl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**
  2. * @file llgl.h
  3. * @brief LLGL definition
  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. #ifndef LL_LLGL_H
  33. #define LL_LLGL_H
  34. // This file contains various stuff for handling gl extensions and other GL
  35. // related stuff.
  36. #include <list>
  37. #include "llcolor4.h"
  38. #include "hbfastmap.h"
  39. #include "llglheaders.h"
  40. #include "llinstancetracker.h"
  41. #include "llmatrix4a.h"
  42. #include "llplane.h"
  43. #include "llstring.h"
  44. extern bool gDebugGL;
  45. // Global flag for dual-renderer support (EE/WL and PBR). HB
  46. extern bool gUsePBRShaders;
  47. class LLSD;
  48. // Manage GL extensions...
  49. class LLGLManager
  50. {
  51. protected:
  52. LOG_CLASS(LLGLManager);
  53. public:
  54. LLGLManager();
  55. bool initGL();
  56. void shutdownGL();
  57. #if LL_WINDOWS
  58. void initWGL(HDC dc); // Initializes stupid WGL extensions
  59. #endif
  60. std::string getRawGLString(); // For sending to simulator
  61. void getPixelFormat(); // Get the best pixel format
  62. void printGLInfoString();
  63. void getGLInfo(LLSD& info);
  64. void asLLSD(LLSD& info);
  65. private:
  66. void initExtensions();
  67. void initGLStates();
  68. void initGLImages();
  69. public:
  70. S32 mDriverVersionMajor;
  71. S32 mDriverVersionMinor;
  72. S32 mDriverVersionRelease;
  73. F32 mGLVersion; // E.g = 3.2
  74. S32 mGLSLVersionMajor;
  75. S32 mGLSLVersionMinor;
  76. S32 mVRAM; // Total VRAM in MB
  77. S32 mTexVRAM; // VRAM reserved for textures in MB
  78. S32 mGLMaxVertexRange;
  79. S32 mGLMaxIndexRange;
  80. S32 mGLMaxTextureSize;
  81. std::string mDriverVersionVendorString;
  82. std::string mGLVersionString;
  83. // In ALL CAPS
  84. std::string mGLVendor;
  85. std::string mGLVendorShort;
  86. // In ALL CAPS
  87. std::string mGLRenderer;
  88. // Limits
  89. S32 mMaxSamples;
  90. S32 mNumTextureImageUnits;
  91. S32 mMaxUniformBlockSize;
  92. F32 mMaxAnisotropy;
  93. // Extensions
  94. #if LL_WINDOWS
  95. bool mHasAMDAssociations;
  96. #endif
  97. bool mHasATIMemInfo;
  98. bool mHasNVXMemInfo;
  99. bool mHasVertexArrayObject;
  100. bool mHasSync;
  101. bool mHasOcclusionQuery2;
  102. bool mHasTimerQuery;
  103. bool mHasDepthClamp;
  104. bool mUseDepthClamp;
  105. bool mHasAnisotropic;
  106. bool mHasCubeMapArray;
  107. bool mHasDebugOutput;
  108. bool mHasTextureSwizzle;
  109. bool mHasGpuShader4;
  110. bool mHasGpuShader5;
  111. // GPU vendor flags
  112. bool mIsAMD;
  113. bool mIsNVIDIA;
  114. bool mIsIntel;
  115. // Whether this version of GL is good enough for SL to use
  116. bool mHasRequirements;
  117. // Misc extensions
  118. bool mHasVertexAttribIPointer;
  119. bool mInited;
  120. bool mIsDisabled;
  121. };
  122. extern LLGLManager gGLManager;
  123. class LLQuaternion;
  124. class LLMatrix4;
  125. LL_NO_INLINE void log_glerror(const char* file, U32 line, bool crash = false);
  126. #if LL_DEBUG
  127. # define stop_glerror() if (LL_UNLIKELY(gDebugGL)) log_glerror(__FILE__, __LINE__, true)
  128. #else
  129. # define stop_glerror() if (LL_UNLIKELY(gDebugGL)) log_glerror(__FILE__, __LINE__)
  130. #endif
  131. void clear_glerror();
  132. // This is a class for GL state management
  133. /*
  134. GL STATE MANAGEMENT DESCRIPTION
  135. LLGLState and its two subclasses, LLGLEnable and LLGLDisable, manage the
  136. current enable/disable states of the GL to prevent redundant setting of
  137. state within a render path or the accidental corruption of what state the
  138. next path expects.
  139. Essentially, wherever you would call glEnable set a state and then
  140. subsequently reset it by calling glDisable (or vice versa), make an
  141. instance of LLGLEnable with the state you want to set, and assume it will
  142. be restored to its original state when that instance of LLGLEnable is
  143. destroyed. It is good practice to exploit stack frame controls for optimal
  144. setting/unsetting and readability of code. There are a collection of helper
  145. classes below that define groups of enables/disables and that can cause
  146. multiple states to be set with the creation of one instance.
  147. Sample usage:
  148. // Disable lighting for rendering hud objects:
  149. // INCORRECT USAGE
  150. LLGLEnable blend(GL_BLEND);
  151. renderHUD();
  152. LLGLDisable blend(GL_BLEND);
  153. // CORRECT USAGE
  154. {
  155. LLGLEnable blend(GL_BLEND);
  156. renderHUD();
  157. }
  158. If a state is to be set on a conditional, the following mechanism
  159. is useful:
  160. {
  161. LLGLEnable blend(blend_hud ? GL_BLEND : 0);
  162. renderHUD();
  163. }
  164. A LLGLState initialized with a parameter of 0 does nothing.
  165. LLGLState works by maintaining a map of the current GL states, and ignoring
  166. redundant enables/disables. If a redundant call is attempted, it becomes a
  167. noop, otherwise, it is set in the constructor and reset in the destructor.
  168. For debugging GL state corruption, running with debug enabled will trigger
  169. asserts if the existing GL state does not match the expected GL state.
  170. */
  171. class LLGLState
  172. {
  173. protected:
  174. LOG_CLASS(LLGLState);
  175. public:
  176. static void initClass();
  177. static void restoreGL();
  178. static void dumpStates();
  179. static void checkStates(const std::string& msg = LLStringUtil::null,
  180. S32 line = -1);
  181. #if 0 // Not used, but kept in source, just in case... HB
  182. // Really should not be needed.
  183. static void resetTextureStates();
  184. #endif
  185. protected:
  186. typedef fast_hmap<U32, GLboolean> state_map_t;
  187. static state_map_t sStateMap;
  188. public:
  189. enum { CURRENT_STATE = -2 };
  190. LLGLState(U32 state, S32 enabled = CURRENT_STATE);
  191. virtual ~LLGLState();
  192. void setEnabled(S32 enabled);
  193. LL_INLINE void enable() { setEnabled(GL_TRUE); }
  194. LL_INLINE void disable() { setEnabled(GL_FALSE); }
  195. protected:
  196. U32 mState;
  197. S32 mWasEnabled;
  198. S32 mIsEnabled;
  199. };
  200. // Helper define, to log the filename and line number of the checkStates()
  201. // call, whenever an error is detected. Unless you got a specific message to
  202. // pass on error, use this macro instead of LLGLState::checkStates(). HB
  203. #define LL_GL_CHECK_STATES LLGLState::checkStates(__FILE__, __LINE__)
  204. class LLGLEnable : public LLGLState
  205. {
  206. public:
  207. LL_INLINE LLGLEnable(U32 state)
  208. : LLGLState(state, GL_TRUE)
  209. {
  210. }
  211. };
  212. class LLGLDisable : public LLGLState
  213. {
  214. public:
  215. LL_INLINE LLGLDisable(U32 state)
  216. : LLGLState(state, GL_FALSE)
  217. {
  218. }
  219. };
  220. /*
  221. Store and modify projection matrix to create an oblique projection that clips
  222. to the specified plane. Oblique projections alter values in the depth buffer,
  223. so this class should not be used mid-renderpass.
  224. Restores projection matrix on destruction.
  225. GL_MODELVIEW_MATRIX is active whenever program execution leaves this class.
  226. Does not stack.
  227. */
  228. class alignas(16) LLGLUserClipPlane
  229. {
  230. public:
  231. LLGLUserClipPlane(const LLPlane& plane, const LLMatrix4a& modelview,
  232. const LLMatrix4a& projection, bool apply = true);
  233. ~LLGLUserClipPlane();
  234. void disable();
  235. void setPlane(F32 a, F32 b, F32 c, F32 d);
  236. private:
  237. LLMatrix4a mProjection;
  238. LLMatrix4a mModelview;
  239. bool mApply;
  240. };
  241. /*
  242. Modify and load projection matrix to push depth values to far clip plane.
  243. Restores projection matrix on destruction.
  244. Saves/restores matrix mode around projection manipulation.
  245. Does not stack.
  246. */
  247. class LLGLSquashToFarClip
  248. {
  249. public:
  250. LLGLSquashToFarClip(U32 layer = 0);
  251. ~LLGLSquashToFarClip();
  252. };
  253. /*
  254. Interface for objects that need periodic GL updates applied to them.
  255. Used to synchronize GL updates with GL thread.
  256. */
  257. class LLGLUpdate
  258. {
  259. public:
  260. LLGLUpdate()
  261. : mInQ(false)
  262. {
  263. }
  264. virtual ~LLGLUpdate()
  265. {
  266. if (mInQ)
  267. {
  268. std::list<LLGLUpdate*>::iterator end = sGLQ.end();
  269. std::list<LLGLUpdate*>::iterator iter = std::find(sGLQ.begin(),
  270. end,
  271. this);
  272. if (iter != end)
  273. {
  274. sGLQ.erase(iter);
  275. }
  276. }
  277. }
  278. virtual void updateGL() = 0;
  279. public:
  280. static std::list<LLGLUpdate*> sGLQ;
  281. bool mInQ;
  282. };
  283. ///////////////////////////////////////////////////////////////////////////////
  284. // Formerly in llglstates.h, but since that header was #included by this one...
  285. ///////////////////////////////////////////////////////////////////////////////
  286. class LLGLDepthTest
  287. {
  288. protected:
  289. LOG_CLASS(LLGLDepthTest);
  290. public:
  291. // Enabled by default
  292. // Note: the new 'ignored' allows (when 'true' to make this class a no-
  293. // operation, so to simplify the dual renderer code. HB
  294. LLGLDepthTest(GLboolean depth_enabled, GLboolean write_enabled = GL_TRUE,
  295. U32 depth_func = GL_LEQUAL, bool ignored = false);
  296. ~LLGLDepthTest();
  297. void checkState();
  298. public:
  299. U32 mPrevDepthFunc;
  300. GLboolean mPrevDepthEnabled;
  301. GLboolean mPrevWriteEnabled;
  302. private:
  303. bool mIgnored;
  304. static U32 sDepthFunc; // defaults to GL_LESS
  305. static GLboolean sDepthEnabled; // defaults to GL_FALSE
  306. static GLboolean sWriteEnabled; // defaults to GL_TRUE
  307. };
  308. class LLGLSDefault
  309. {
  310. public:
  311. LLGLSDefault()
  312. : // Disable
  313. mBlend(GL_BLEND),
  314. mCullFace(GL_CULL_FACE)
  315. {
  316. }
  317. protected:
  318. LLGLDisable mBlend;
  319. LLGLDisable mCullFace;
  320. };
  321. class LLGLSObjectSelect
  322. {
  323. public:
  324. LLGLSObjectSelect()
  325. : mBlend(GL_BLEND),
  326. mCullFace(GL_CULL_FACE)
  327. {
  328. }
  329. protected:
  330. LLGLDisable mBlend;
  331. LLGLEnable mCullFace;
  332. };
  333. class LLGLSUIDefault
  334. {
  335. public:
  336. LLGLSUIDefault()
  337. : mBlend(GL_BLEND),
  338. mCullFace(GL_CULL_FACE),
  339. mDepthTest(GL_FALSE, GL_TRUE, GL_LEQUAL),
  340. mMSAA(GL_MULTISAMPLE)
  341. {
  342. }
  343. protected:
  344. LLGLEnable mBlend;
  345. LLGLDisable mCullFace;
  346. LLGLDepthTest mDepthTest;
  347. LLGLDisable mMSAA;
  348. };
  349. class LLGLSPipeline
  350. {
  351. public:
  352. LLGLSPipeline()
  353. : mCullFace(GL_CULL_FACE),
  354. mDepthTest(GL_TRUE, GL_TRUE, GL_LEQUAL)
  355. {
  356. }
  357. protected:
  358. LLGLEnable mCullFace;
  359. LLGLDepthTest mDepthTest;
  360. };
  361. class LLGLSPipelineAlpha // : public LLGLSPipeline
  362. {
  363. public:
  364. LLGLSPipelineAlpha()
  365. : mBlend(GL_BLEND)
  366. {
  367. }
  368. protected:
  369. LLGLEnable mBlend;
  370. };
  371. class LLGLSPipelineSelection
  372. {
  373. public:
  374. LLGLSPipelineSelection()
  375. : mCullFace(GL_CULL_FACE)
  376. {
  377. }
  378. protected:
  379. LLGLDisable mCullFace;
  380. };
  381. class LLGLSPipelineSkyBox
  382. {
  383. public:
  384. LLGLSPipelineSkyBox();
  385. virtual ~LLGLSPipelineSkyBox() = default;
  386. protected:
  387. LLGLDisable mCullFace;
  388. LLGLSquashToFarClip mSquashClip;
  389. };
  390. class LLGLSPipelineDepthTestSkyBox : public LLGLSPipelineSkyBox
  391. {
  392. public:
  393. LLGLSPipelineDepthTestSkyBox(GLboolean depth_test, GLboolean depth_write);
  394. public:
  395. LLGLDepthTest mDepth;
  396. };
  397. class LLGLSPipelineBlendSkyBox : public LLGLSPipelineDepthTestSkyBox
  398. {
  399. public:
  400. LLGLSPipelineBlendSkyBox(GLboolean depth_test, GLboolean depth_write);
  401. public:
  402. LLGLEnable mBlend;
  403. };
  404. class LLGLSTracker
  405. {
  406. public:
  407. LLGLSTracker()
  408. : mCullFace(GL_CULL_FACE),
  409. mBlend(GL_BLEND)
  410. {
  411. }
  412. protected:
  413. LLGLEnable mCullFace;
  414. LLGLEnable mBlend;
  415. };
  416. class LLGLSSpecular
  417. {
  418. public:
  419. LLGLSSpecular(const LLColor4& color, F32 shininess)
  420. {
  421. mShininess = shininess;
  422. if (mShininess > 0.f)
  423. {
  424. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color.mV);
  425. S32 shiny = (S32)(shininess * 128.f);
  426. shiny = llclamp(shiny, 0, 128);
  427. glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, shiny);
  428. }
  429. }
  430. ~LLGLSSpecular()
  431. {
  432. if (mShininess > 0.f)
  433. {
  434. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
  435. LLColor4::transparent.mV);
  436. glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
  437. }
  438. }
  439. public:
  440. F32 mShininess;
  441. };
  442. ///////////////////////////////////////////////////////////////////////////////
  443. const std::string getGLErrorString(U32 error);
  444. #endif // LL_LLGL_H