llgl.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLGL_H
00033 #define LL_LLGL_H
00034 
00035 // This file contains various stuff for handling gl extensions and other gl related stuff.
00036 
00037 #include <string>
00038 #include <map>
00039 
00040 #include "llerror.h"
00041 #include "v4color.h"
00042 #include "llstring.h"
00043 #include "stdtypes.h"
00044 #include "v4math.h"
00045 #include "llplane.h"
00046 #include "llgltypes.h"
00047 
00048 #include "llglheaders.h"
00049 #include "glh/glh_linear.h"
00050 
00051 extern BOOL gDebugGL;
00052 
00053 #define LL_GL_ERRS LL_ERRS("RenderState")
00054 
00055 class LLSD;
00056 
00057 // Manage GL extensions...
00058 class LLGLManager
00059 {
00060 public:
00061         LLGLManager();
00062 
00063         bool initGL();
00064         void shutdownGL();
00065 
00066         void initWGL(); // Initializes stupid WGL extensions
00067 
00068         LLString getRawGLString(); // For sending to simulator
00069 
00070         BOOL mInited;
00071         BOOL mIsDisabled;
00072 
00073         // Extensions used by everyone
00074         BOOL mHasMultitexture;
00075         S32      mNumTextureUnits;
00076         BOOL mHasMipMapGeneration;
00077         BOOL mHasPalettedTextures;
00078         BOOL mHasCompressedTextures;
00079         BOOL mHasFramebufferObject;
00080 
00081         // ARB Extensions
00082         BOOL mHasVertexBufferObject;
00083         BOOL mHasPBuffer;
00084         BOOL mHasShaderObjects;
00085         BOOL mHasVertexShader;
00086         BOOL mHasFragmentShader;
00087         BOOL mHasOcclusionQuery;
00088         BOOL mHasPointParameters;
00089 
00090         // Other extensions.
00091         BOOL mHasAnisotropic;
00092         BOOL mHasARBEnvCombine;
00093         BOOL mHasCubeMap;
00094 
00095         // Vendor-specific extensions
00096         BOOL mIsATI;
00097         BOOL mIsNVIDIA;
00098         BOOL mIsIntel;
00099         BOOL mIsGF2or4MX;
00100         BOOL mIsGF3;
00101         BOOL mIsGFFX;
00102         BOOL mATIOffsetVerticalLines;
00103 
00104         // Whether this version of GL is good enough for SL to use
00105         BOOL mHasRequirements;
00106 
00107         // Misc extensions
00108         BOOL mHasSeparateSpecularColor;
00109         
00110         S32 mDriverVersionMajor;
00111         S32 mDriverVersionMinor;
00112         S32 mDriverVersionRelease;
00113         F32 mGLVersion; // e.g = 1.4
00114         LLString mDriverVersionVendorString;
00115 
00116         S32 mVRAM; // VRAM in MB
00117         S32 mGLMaxVertexRange;
00118         S32 mGLMaxIndexRange;
00119         
00120         void getPixelFormat(); // Get the best pixel format
00121 
00122         LLString getGLInfoString();
00123         void printGLInfoString();
00124         void getGLInfo(LLSD& info);
00125 
00126         // In ALL CAPS
00127         LLString mGLVendor;
00128         LLString mGLVendorShort;
00129 
00130         // In ALL CAPS
00131         LLString mGLRenderer;
00132 
00133 private:
00134         void initExtensions();
00135         void initGLStates();
00136         void initGLImages();
00137 };
00138 
00139 extern LLGLManager gGLManager;
00140 
00141 class LLQuaternion;
00142 class LLMatrix4;
00143 
00144 void rotate_quat(LLQuaternion& rotation);
00145 
00146 void flush_glerror(); // Flush GL errors when we know we're handling them correctly.
00147 
00148 void assert_glerror();
00149 
00150 void clear_glerror();
00151 
00152 //#if LL_DEBUG
00153 # define stop_glerror() assert_glerror()
00154 # define llglassertok() assert_glerror()
00155 //#else
00156 //# define stop_glerror()
00157 //# define llglassertok()
00158 //#endif
00159 
00160 #define llglassertok_always() assert_glerror()
00161 
00163 //
00164 // Note: U32's are GLEnum's...
00165 //
00166 
00167 // This is a class for GL state management
00168 
00169 /*
00170         GL STATE MANAGEMENT DESCRIPTION
00171 
00172         LLGLState and its two subclasses, LLGLEnable and LLGLDisable, manage the current 
00173         enable/disable states of the GL to prevent redundant setting of state within a 
00174         render path or the accidental corruption of what state the next path expects.
00175 
00176         Essentially, wherever you would call glEnable set a state and then
00177         subsequently reset it by calling glDisable (or vice versa), make an instance of 
00178         LLGLEnable with the state you want to set, and assume it will be restored to its
00179         original state when that instance of LLGLEnable is destroyed.  It is good practice
00180         to exploit stack frame controls for optimal setting/unsetting and readability of 
00181         code.  In llglstates.h, there are a collection of helper classes that define groups
00182         of enables/disables that can cause multiple states to be set with the creation of
00183         one instance.  
00184 
00185         Sample usage:
00186 
00187         //disable lighting for rendering hud objects
00188         //INCORRECT USAGE
00189         LLGLEnable lighting(GL_LIGHTING);
00190         renderHUD();
00191         LLGLDisable lighting(GL_LIGHTING);
00192 
00193         //CORRECT USAGE
00194         {
00195                 LLGLEnable lighting(GL_LIGHTING);
00196                 renderHUD();
00197         }
00198 
00199         If a state is to be set on a conditional, the following mechanism
00200         is useful:
00201 
00202         {
00203                 LLGLEnable lighting(light_hud ? GL_LIGHTING : 0);
00204                 renderHUD();
00205         }
00206 
00207         A LLGLState initialized with a parameter of 0 does nothing.
00208 
00209         LLGLState works by maintaining a map of the current GL states, and ignoring redundant
00210         enables/disables.  If a redundant call is attempted, it becomes a noop, otherwise,
00211         it is set in the constructor and reset in the destructor.  
00212 
00213         For debugging GL state corruption, running with debug enabled will trigger asserts
00214         if the existing GL state does not match the expected GL state.
00215 
00216 */
00217 class LLGLState
00218 {
00219 public:
00220         static void initClass();
00221         static void restoreGL();
00222 
00223         static void resetTextureStates();
00224         static void dumpStates();
00225         static void checkStates();
00226         static void checkTextureChannels();
00227         static void checkClientArrays(U32 data_mask = 0x0001);
00228         
00229 protected:
00230         static std::map<LLGLenum, LLGLboolean> sStateMap;
00231         
00232 public:
00233         enum { CURRENT_STATE = -2 };
00234         LLGLState(LLGLenum state, S32 enabled = CURRENT_STATE);
00235         ~LLGLState();
00236         void setEnabled(S32 enabled);
00237         void enable() { setEnabled(TRUE); }
00238         void disable() { setEnabled(FALSE); }
00239 protected:
00240         LLGLenum mState;
00241         BOOL mWasEnabled;
00242         BOOL mIsEnabled;
00243 };
00244 
00245 // New LLGLState class wrappers that don't depend on actual GL flags.
00246 class LLGLEnableBlending : public LLGLState
00247 {
00248 public:
00249         LLGLEnableBlending(bool enable);
00250 };
00251 
00252 class LLGLEnableAlphaReject : public LLGLState
00253 {
00254 public:
00255         LLGLEnableAlphaReject(bool enable);
00256 };
00257 
00259 class LLGLEnable : public LLGLState
00260 {
00261 public:
00262         LLGLEnable(LLGLenum state) : LLGLState(state, TRUE) {}
00263 };
00264 
00266 class LLGLDisable : public LLGLState
00267 {
00268 public:
00269         LLGLDisable(LLGLenum state) : LLGLState(state, FALSE) {}
00270 };
00271 
00272 /*
00273   Store and modify projection matrix to create an oblique
00274   projection that clips to the specified plane.  Oblique
00275   projections alter values in the depth buffer, so this
00276   class should not be used mid-renderpass.  
00277 
00278   Restores projection matrix on destruction.
00279   GL_MODELVIEW_MATRIX is active whenever program execution
00280   leaves this class.
00281   Does not stack.
00282   Caches inverse of projection matrix used in gGLObliqueProjectionInverse
00283 */
00284 class LLGLUserClipPlane 
00285 {
00286 public:
00287         
00288         LLGLUserClipPlane(const LLPlane& plane, const glh::matrix4f& modelview, const glh::matrix4f& projection);
00289         ~LLGLUserClipPlane();
00290 
00291         void setPlane(F32 a, F32 b, F32 c, F32 d);
00292 
00293 private:
00294         glh::matrix4f mProjection;
00295         glh::matrix4f mModelview;
00296 };
00297 
00298 /*
00299   Modify and load projection matrix to push depth values to far clip plane.
00300 
00301   Restores projection matrix on destruction.
00302   GL_MODELVIEW_MATRIX is active whenever program execution
00303   leaves this class.
00304   Does not stack.
00305 */
00306 class LLGLClampToFarClip
00307 {
00308 public:
00309         LLGLClampToFarClip(glh::matrix4f projection);
00310         ~LLGLClampToFarClip();
00311 };
00312 
00313 /*
00314         Generic pooling scheme for things which use GL names (used for occlusion queries and vertex buffer objects).
00315         Prevents thrashing of GL name caches by avoiding calls to glGenFoo and glDeleteFoo.
00316 */
00317 class LLGLNamePool
00318 {
00319 public:
00320         typedef struct
00321         {
00322                 GLuint name;
00323                 BOOL used;
00324         } NameEntry;
00325 
00326         struct CompareUsed
00327         {
00328                 bool operator()(const NameEntry& lhs, const NameEntry& rhs)
00329                 {
00330                         return lhs.used < rhs.used;  //FALSE entries first
00331                 }
00332         };
00333 
00334         typedef std::vector<NameEntry> name_list_t;
00335         name_list_t mNameList;
00336 
00337         LLGLNamePool();
00338         virtual ~LLGLNamePool();
00339         
00340         void upkeep();
00341         void cleanup();
00342         
00343         GLuint allocate();
00344         void release(GLuint name);
00345         
00346         static void registerPool(LLGLNamePool* pool);
00347         static void upkeepPools();
00348         static void cleanupPools();
00349 
00350 protected:
00351         typedef std::vector<LLGLNamePool*> pool_list_t;
00352         static pool_list_t sInstances;
00353         
00354         virtual GLuint allocateName() = 0;
00355         virtual void releaseName(GLuint name) = 0;
00356 };
00357 
00358 extern LLMatrix4 gGLObliqueProjectionInverse;
00359 
00360 #include "llglstates.h"
00361 
00362 void init_glstates();
00363 void enable_vertex_weighting(const S32 index);
00364 void disable_vertex_weighting(const S32 index);
00365 void enable_binormals(const S32 index);
00366 void disable_binormals(const S32 index);
00367 void enable_cloth_weights(const S32 index);
00368 void disable_cloth_weights(const S32 index);
00369 void set_vertex_weights(const S32 index, const U32 stride, const F32 *weights);
00370 void set_vertex_clothing_weights(const S32 index, const U32 stride, const LLVector4 *weights);
00371 void set_binormals(const S32 index, const U32 stride, const LLVector3 *binormals);
00372 void set_palette(U8* palette_data);
00373 void parse_gl_version( S32* major, S32* minor, S32* release, LLString* vendor_specific );
00374 
00375 extern BOOL gClothRipple;
00376 extern BOOL gNoRender;
00377 #endif // LL_LLGL_H

Generated on Fri May 16 08:33:02 2008 for SecondLife by  doxygen 1.5.5