PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llrender/llgl.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 493 lines | 280 code | 86 blank | 127 comment | 3 complexity | bbe3b8f823122326fdb0327a81e28be8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llgl.h
  3. * @brief LLGL definition
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLGL_H
  27. #define LL_LLGL_H
  28. // This file contains various stuff for handling gl extensions and other gl related stuff.
  29. #include <string>
  30. #include <boost/unordered_map.hpp>
  31. #include <list>
  32. #include "llerror.h"
  33. #include "v4color.h"
  34. #include "llstring.h"
  35. #include "stdtypes.h"
  36. #include "v4math.h"
  37. #include "llplane.h"
  38. #include "llgltypes.h"
  39. #include "llinstancetracker.h"
  40. #include "llglheaders.h"
  41. #include "glh/glh_linear.h"
  42. extern BOOL gDebugGL;
  43. extern BOOL gDebugSession;
  44. extern std::ofstream gFailLog;
  45. #define LL_GL_ERRS LL_ERRS("RenderState")
  46. void ll_init_fail_log(std::string filename);
  47. void ll_fail(std::string msg);
  48. void ll_close_fail_log();
  49. class LLSD;
  50. // Manage GL extensions...
  51. class LLGLManager
  52. {
  53. public:
  54. LLGLManager();
  55. bool initGL();
  56. void shutdownGL();
  57. void initWGL(); // Initializes stupid WGL extensions
  58. std::string getRawGLString(); // For sending to simulator
  59. BOOL mInited;
  60. BOOL mIsDisabled;
  61. // Extensions used by everyone
  62. BOOL mHasMultitexture;
  63. BOOL mHasATIMemInfo;
  64. BOOL mHasNVXMemInfo;
  65. S32 mNumTextureUnits;
  66. BOOL mHasMipMapGeneration;
  67. BOOL mHasCompressedTextures;
  68. BOOL mHasFramebufferObject;
  69. S32 mMaxSamples;
  70. BOOL mHasBlendFuncSeparate;
  71. // ARB Extensions
  72. BOOL mHasVertexBufferObject;
  73. BOOL mHasVertexArrayObject;
  74. BOOL mHasSync;
  75. BOOL mHasMapBufferRange;
  76. BOOL mHasFlushBufferRange;
  77. BOOL mHasPBuffer;
  78. BOOL mHasShaderObjects;
  79. BOOL mHasVertexShader;
  80. BOOL mHasFragmentShader;
  81. S32 mNumTextureImageUnits;
  82. BOOL mHasOcclusionQuery;
  83. BOOL mHasOcclusionQuery2;
  84. BOOL mHasPointParameters;
  85. BOOL mHasDrawBuffers;
  86. BOOL mHasDepthClamp;
  87. BOOL mHasTextureRectangle;
  88. BOOL mHasTextureMultisample;
  89. S32 mMaxSampleMaskWords;
  90. S32 mMaxColorTextureSamples;
  91. S32 mMaxDepthTextureSamples;
  92. S32 mMaxIntegerSamples;
  93. // Other extensions.
  94. BOOL mHasAnisotropic;
  95. BOOL mHasARBEnvCombine;
  96. BOOL mHasCubeMap;
  97. BOOL mHasDebugOutput;
  98. // Vendor-specific extensions
  99. BOOL mIsATI;
  100. BOOL mIsNVIDIA;
  101. BOOL mIsIntel;
  102. BOOL mIsGF2or4MX;
  103. BOOL mIsGF3;
  104. BOOL mIsGFFX;
  105. BOOL mATIOffsetVerticalLines;
  106. BOOL mATIOldDriver;
  107. // Whether this version of GL is good enough for SL to use
  108. BOOL mHasRequirements;
  109. // Misc extensions
  110. BOOL mHasSeparateSpecularColor;
  111. //whether this GPU is in the debug list.
  112. BOOL mDebugGPU;
  113. S32 mDriverVersionMajor;
  114. S32 mDriverVersionMinor;
  115. S32 mDriverVersionRelease;
  116. F32 mGLVersion; // e.g = 1.4
  117. std::string mDriverVersionVendorString;
  118. S32 mVRAM; // VRAM in MB
  119. S32 mGLMaxVertexRange;
  120. S32 mGLMaxIndexRange;
  121. void getPixelFormat(); // Get the best pixel format
  122. std::string getGLInfoString();
  123. void printGLInfoString();
  124. void getGLInfo(LLSD& info);
  125. // In ALL CAPS
  126. std::string mGLVendor;
  127. std::string mGLVendorShort;
  128. // In ALL CAPS
  129. std::string mGLRenderer;
  130. private:
  131. void initExtensions();
  132. void initGLStates();
  133. void initGLImages();
  134. void setToDebugGPU();
  135. };
  136. extern LLGLManager gGLManager;
  137. class LLQuaternion;
  138. class LLMatrix4;
  139. void rotate_quat(LLQuaternion& rotation);
  140. void flush_glerror(); // Flush GL errors when we know we're handling them correctly.
  141. void log_glerror();
  142. void assert_glerror();
  143. void clear_glerror();
  144. //#if LL_DEBUG
  145. # define stop_glerror() assert_glerror()
  146. # define llglassertok() assert_glerror()
  147. //#else
  148. //# define stop_glerror()
  149. //# define llglassertok()
  150. //#endif
  151. #define llglassertok_always() assert_glerror()
  152. ////////////////////////
  153. //
  154. // Note: U32's are GLEnum's...
  155. //
  156. // This is a class for GL state management
  157. /*
  158. GL STATE MANAGEMENT DESCRIPTION
  159. LLGLState and its two subclasses, LLGLEnable and LLGLDisable, manage the current
  160. enable/disable states of the GL to prevent redundant setting of state within a
  161. render path or the accidental corruption of what state the next path expects.
  162. Essentially, wherever you would call glEnable set a state and then
  163. subsequently reset it by calling glDisable (or vice versa), make an instance of
  164. LLGLEnable with the state you want to set, and assume it will be restored to its
  165. original state when that instance of LLGLEnable is destroyed. It is good practice
  166. to exploit stack frame controls for optimal setting/unsetting and readability of
  167. code. In llglstates.h, there are a collection of helper classes that define groups
  168. of enables/disables that can cause multiple states to be set with the creation of
  169. one instance.
  170. Sample usage:
  171. //disable lighting for rendering hud objects
  172. //INCORRECT USAGE
  173. LLGLEnable lighting(GL_LIGHTING);
  174. renderHUD();
  175. LLGLDisable lighting(GL_LIGHTING);
  176. //CORRECT USAGE
  177. {
  178. LLGLEnable lighting(GL_LIGHTING);
  179. renderHUD();
  180. }
  181. If a state is to be set on a conditional, the following mechanism
  182. is useful:
  183. {
  184. LLGLEnable lighting(light_hud ? GL_LIGHTING : 0);
  185. renderHUD();
  186. }
  187. A LLGLState initialized with a parameter of 0 does nothing.
  188. LLGLState works by maintaining a map of the current GL states, and ignoring redundant
  189. enables/disables. If a redundant call is attempted, it becomes a noop, otherwise,
  190. it is set in the constructor and reset in the destructor.
  191. For debugging GL state corruption, running with debug enabled will trigger asserts
  192. if the existing GL state does not match the expected GL state.
  193. */
  194. class LLGLState
  195. {
  196. public:
  197. static void initClass();
  198. static void restoreGL();
  199. static void resetTextureStates();
  200. static void dumpStates();
  201. static void checkStates(const std::string& msg = "");
  202. static void checkTextureChannels(const std::string& msg = "");
  203. static void checkClientArrays(const std::string& msg = "", U32 data_mask = 0);
  204. protected:
  205. static boost::unordered_map<LLGLenum, LLGLboolean> sStateMap;
  206. public:
  207. enum { CURRENT_STATE = -2 };
  208. LLGLState(LLGLenum state, S32 enabled = CURRENT_STATE);
  209. ~LLGLState();
  210. void setEnabled(S32 enabled);
  211. void enable() { setEnabled(TRUE); }
  212. void disable() { setEnabled(FALSE); }
  213. protected:
  214. LLGLenum mState;
  215. BOOL mWasEnabled;
  216. BOOL mIsEnabled;
  217. };
  218. // New LLGLState class wrappers that don't depend on actual GL flags.
  219. class LLGLEnableBlending : public LLGLState
  220. {
  221. public:
  222. LLGLEnableBlending(bool enable);
  223. };
  224. class LLGLEnableAlphaReject : public LLGLState
  225. {
  226. public:
  227. LLGLEnableAlphaReject(bool enable);
  228. };
  229. /// TODO: Being deprecated.
  230. class LLGLEnable : public LLGLState
  231. {
  232. public:
  233. LLGLEnable(LLGLenum state) : LLGLState(state, TRUE) {}
  234. };
  235. /// TODO: Being deprecated.
  236. class LLGLDisable : public LLGLState
  237. {
  238. public:
  239. LLGLDisable(LLGLenum state) : LLGLState(state, FALSE) {}
  240. };
  241. /*
  242. Store and modify projection matrix to create an oblique
  243. projection that clips to the specified plane. Oblique
  244. projections alter values in the depth buffer, so this
  245. class should not be used mid-renderpass.
  246. Restores projection matrix on destruction.
  247. GL_MODELVIEW_MATRIX is active whenever program execution
  248. leaves this class.
  249. Does not stack.
  250. Caches inverse of projection matrix used in gGLObliqueProjectionInverse
  251. */
  252. class LLGLUserClipPlane
  253. {
  254. public:
  255. LLGLUserClipPlane(const LLPlane& plane, const glh::matrix4f& modelview, const glh::matrix4f& projection, bool apply = true);
  256. ~LLGLUserClipPlane();
  257. void setPlane(F32 a, F32 b, F32 c, F32 d);
  258. private:
  259. bool mApply;
  260. glh::matrix4f mProjection;
  261. glh::matrix4f mModelview;
  262. };
  263. /*
  264. Modify and load projection matrix to push depth values to far clip plane.
  265. Restores projection matrix on destruction.
  266. GL_MODELVIEW_MATRIX is active whenever program execution
  267. leaves this class.
  268. Does not stack.
  269. */
  270. class LLGLSquashToFarClip
  271. {
  272. public:
  273. LLGLSquashToFarClip(glh::matrix4f projection, U32 layer = 0);
  274. ~LLGLSquashToFarClip();
  275. };
  276. /*
  277. Generic pooling scheme for things which use GL names (used for occlusion queries and vertex buffer objects).
  278. Prevents thrashing of GL name caches by avoiding calls to glGenFoo and glDeleteFoo.
  279. */
  280. class LLGLNamePool : public LLInstanceTracker<LLGLNamePool>
  281. {
  282. public:
  283. typedef LLInstanceTracker<LLGLNamePool> tracker_t;
  284. struct NameEntry
  285. {
  286. GLuint name;
  287. BOOL used;
  288. };
  289. struct CompareUsed
  290. {
  291. bool operator()(const NameEntry& lhs, const NameEntry& rhs)
  292. {
  293. return lhs.used < rhs.used; //FALSE entries first
  294. }
  295. };
  296. typedef std::vector<NameEntry> name_list_t;
  297. name_list_t mNameList;
  298. LLGLNamePool();
  299. virtual ~LLGLNamePool();
  300. void upkeep();
  301. void cleanup();
  302. GLuint allocate();
  303. void release(GLuint name);
  304. static void upkeepPools();
  305. static void cleanupPools();
  306. protected:
  307. typedef std::vector<LLGLNamePool*> pool_list_t;
  308. virtual GLuint allocateName() = 0;
  309. virtual void releaseName(GLuint name) = 0;
  310. };
  311. /*
  312. Interface for objects that need periodic GL updates applied to them.
  313. Used to synchronize GL updates with GL thread.
  314. */
  315. class LLGLUpdate
  316. {
  317. public:
  318. static std::list<LLGLUpdate*> sGLQ;
  319. BOOL mInQ;
  320. LLGLUpdate()
  321. : mInQ(FALSE)
  322. {
  323. }
  324. virtual ~LLGLUpdate()
  325. {
  326. if (mInQ)
  327. {
  328. std::list<LLGLUpdate*>::iterator iter = std::find(sGLQ.begin(), sGLQ.end(), this);
  329. if (iter != sGLQ.end())
  330. {
  331. sGLQ.erase(iter);
  332. }
  333. }
  334. }
  335. virtual void updateGL() = 0;
  336. };
  337. extern LLMatrix4 gGLObliqueProjectionInverse;
  338. #include "llglstates.h"
  339. void init_glstates();
  340. void parse_gl_version( S32* major, S32* minor, S32* release, std::string* vendor_specific );
  341. extern BOOL gClothRipple;
  342. extern BOOL gHeadlessClient;
  343. extern BOOL gGLActive;
  344. // Deal with changing glext.h definitions for newer SDK versions, specifically
  345. // with MAC OSX 10.5 -> 10.6
  346. #ifndef GL_DEPTH_ATTACHMENT
  347. #define GL_DEPTH_ATTACHMENT GL_DEPTH_ATTACHMENT_EXT
  348. #endif
  349. #ifndef GL_STENCIL_ATTACHMENT
  350. #define GL_STENCIL_ATTACHMENT GL_STENCIL_ATTACHMENT_EXT
  351. #endif
  352. #ifndef GL_FRAMEBUFFER
  353. #define GL_FRAMEBUFFER GL_FRAMEBUFFER_EXT
  354. #define GL_DRAW_FRAMEBUFFER GL_DRAW_FRAMEBUFFER_EXT
  355. #define GL_READ_FRAMEBUFFER GL_READ_FRAMEBUFFER_EXT
  356. #define GL_FRAMEBUFFER_COMPLETE GL_FRAMEBUFFER_COMPLETE_EXT
  357. #define GL_FRAMEBUFFER_UNSUPPORTED GL_FRAMEBUFFER_UNSUPPORTED_EXT
  358. #define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
  359. #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
  360. #define glGenFramebuffers glGenFramebuffersEXT
  361. #define glBindFramebuffer glBindFramebufferEXT
  362. #define glCheckFramebufferStatus glCheckFramebufferStatusEXT
  363. #define glBlitFramebuffer glBlitFramebufferEXT
  364. #define glDeleteFramebuffers glDeleteFramebuffersEXT
  365. #define glFramebufferRenderbuffer glFramebufferRenderbufferEXT
  366. #define glFramebufferTexture2D glFramebufferTexture2DEXT
  367. #endif
  368. #ifndef GL_RENDERBUFFER
  369. #define GL_RENDERBUFFER GL_RENDERBUFFER_EXT
  370. #define glGenRenderbuffers glGenRenderbuffersEXT
  371. #define glBindRenderbuffer glBindRenderbufferEXT
  372. #define glRenderbufferStorage glRenderbufferStorageEXT
  373. #define glRenderbufferStorageMultisample glRenderbufferStorageMultisampleEXT
  374. #define glDeleteRenderbuffers glDeleteRenderbuffersEXT
  375. #endif
  376. #ifndef GL_COLOR_ATTACHMENT
  377. #define GL_COLOR_ATTACHMENT GL_COLOR_ATTACHMENT_EXT
  378. #endif
  379. #ifndef GL_COLOR_ATTACHMENT0
  380. #define GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0_EXT
  381. #endif
  382. #ifndef GL_COLOR_ATTACHMENT1
  383. #define GL_COLOR_ATTACHMENT1 GL_COLOR_ATTACHMENT1_EXT
  384. #endif
  385. #ifndef GL_COLOR_ATTACHMENT2
  386. #define GL_COLOR_ATTACHMENT2 GL_COLOR_ATTACHMENT2_EXT
  387. #endif
  388. #ifndef GL_COLOR_ATTACHMENT3
  389. #define GL_COLOR_ATTACHMENT3 GL_COLOR_ATTACHMENT3_EXT
  390. #endif
  391. #ifndef GL_DEPTH24_STENCIL8
  392. #define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_EXT
  393. #endif
  394. #endif // LL_LLGL_H