PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

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