00001
00032 #ifndef LL_LLGLSLSHADER_H
00033 #define LL_LLGLSLSHADER_H
00034
00035 #include "llgl.h"
00036
00037 class LLShaderFeatures
00038 {
00039 public:
00040 bool calculatesLighting;
00041 bool calculatesAtmospherics;
00042 bool hasLighting;
00043 bool isShiny;
00044 bool isFullbright;
00045 bool isSpecular;
00046 bool hasWaterFog;
00047 bool hasTransport;
00048 bool hasSkinning;
00049 bool hasAtmospherics;
00050 bool hasGamma;
00051
00052
00053
00054 LLShaderFeatures();
00055 };
00056
00057 class LLGLSLShader
00058 {
00059 public:
00060
00061 enum
00062 {
00063 SG_DEFAULT = 0,
00064 SG_SKY,
00065 SG_WATER
00066 };
00067
00068 LLGLSLShader();
00069
00070 void unload();
00071 BOOL createShader(std::vector<std::string> * attributes,
00072 std::vector<std::string> * uniforms);
00073 BOOL attachObject(std::string object);
00074 void attachObject(GLhandleARB object);
00075 void attachObjects(GLhandleARB* objects = NULL, S32 count = 0);
00076 BOOL mapAttributes(const std::vector<std::string> * attributes);
00077 BOOL mapUniforms(const std::vector<std::string> * uniforms);
00078 void mapUniform(GLint index, const std::vector<std::string> * uniforms);
00079 void uniform1f(U32 index, GLfloat v);
00080 void uniform2f(U32 index, GLfloat x, GLfloat y);
00081 void uniform3f(U32 index, GLfloat x, GLfloat y, GLfloat z);
00082 void uniform4f(U32 index, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00083 void uniform1fv(U32 index, U32 count, const GLfloat* v);
00084 void uniform2fv(U32 index, U32 count, const GLfloat* v);
00085 void uniform3fv(U32 index, U32 count, const GLfloat* v);
00086 void uniform4fv(U32 index, U32 count, const GLfloat* v);
00087 void uniform1f(const std::string& uniform, GLfloat v);
00088 void uniform2f(const std::string& uniform, GLfloat x, GLfloat y);
00089 void uniform3f(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z);
00090 void uniform4f(const std::string& uniform, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00091 void uniform1fv(const std::string& uniform, U32 count, const GLfloat* v);
00092 void uniform2fv(const std::string& uniform, U32 count, const GLfloat* v);
00093 void uniform3fv(const std::string& uniform, U32 count, const GLfloat* v);
00094 void uniform4fv(const std::string& uniform, U32 count, const GLfloat* v);
00095 void uniformMatrix2fv(U32 index, U32 count, GLboolean transpose, const GLfloat *v);
00096 void uniformMatrix3fv(U32 index, U32 count, GLboolean transpose, const GLfloat *v);
00097 void uniformMatrix4fv(U32 index, U32 count, GLboolean transpose, const GLfloat *v);
00098 void uniformMatrix2fv(const std::string& uniform, U32 count, GLboolean transpose, const GLfloat *v);
00099 void uniformMatrix3fv(const std::string& uniform, U32 count, GLboolean transpose, const GLfloat *v);
00100 void uniformMatrix4fv(const std::string& uniform, U32 count, GLboolean transpose, const GLfloat *v);
00101
00102 void vertexAttrib4f(U32 index, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
00103 void vertexAttrib4fv(U32 index, GLfloat* v);
00104
00105 GLint getUniformLocation(const std::string& uniform);
00106
00107 GLint mapUniformTextureChannel(GLint location, GLenum type);
00108
00109
00110
00111
00112
00113
00114 S32 enableTexture(S32 uniform, S32 mode = GL_TEXTURE_2D);
00115 S32 disableTexture(S32 uniform, S32 mode = GL_TEXTURE_2D);
00116
00117 BOOL link(BOOL suppress_errors = FALSE);
00118 void bind();
00119 void unbind();
00120
00121 GLhandleARB mProgramObject;
00122 std::vector<GLint> mAttribute;
00123 std::vector<GLint> mUniform;
00124 std::map<std::string, GLint> mUniformMap;
00125 std::map<GLint, LLVector4> mValue;
00126 std::vector<GLint> mTexture;
00127 S32 mActiveTextureChannels;
00128 S32 mShaderLevel;
00129 S32 mShaderGroup;
00130 BOOL mUniformsDirty;
00131 LLShaderFeatures mFeatures;
00132 std::vector< std::pair< std::string, GLenum > > mShaderFiles;
00133 std::string mName;
00134 };
00135
00136 class LLShaderMgr
00137 {
00138 friend class LLGLSLShader;
00139
00140 public:
00141 static void initAttribsAndUniforms(void);
00142 static BOOL attachShaderFeatures(LLGLSLShader * shader);
00143 static void setShaders();
00144 static void unloadShaders();
00145 static void dumpObjectLog(GLhandleARB ret, BOOL warns = TRUE);
00146 static BOOL linkProgramObject(GLhandleARB obj, BOOL suppress_errors = FALSE);
00147 static BOOL validateProgramObject(GLhandleARB obj);
00148 static GLhandleARB loadShaderFile(const LLString& filename, S32 & shader_level, GLenum type);
00149 static S32 getVertexShaderLevel(S32 type);
00150 static BOOL loadBasicShaders();
00151 static BOOL loadShadersEffects();
00152 static BOOL loadShadersDeferred();
00153 static BOOL loadShadersObject();
00154 static BOOL loadShadersAvatar();
00155 static BOOL loadShadersEnvironment();
00156 static BOOL loadShadersWater();
00157 static BOOL loadShadersInterface();
00158 static BOOL loadShadersWindLight();
00159
00160
00161
00162 class shader_iter
00163 {
00164 friend bool operator == (shader_iter const & a, shader_iter const & b);
00165 friend bool operator != (shader_iter const & a, shader_iter const & b);
00166 public:
00167 shader_iter() : mPtr(NULL)
00168 {
00169 }
00170
00171 shader_iter(LLGLSLShader * const * ptr) : mPtr(ptr)
00172 {
00173 }
00174
00175 LLGLSLShader & operator * () const
00176 {
00177 return **mPtr;
00178 }
00179
00180 LLGLSLShader * operator -> () const
00181 {
00182 return *mPtr;
00183 }
00184
00185 shader_iter & operator++ ()
00186 {
00187 ++mPtr;
00188 return *this;
00189 }
00190
00191 shader_iter operator++ (int)
00192 {
00193 return mPtr++;
00194 }
00195
00196 private:
00197 LLGLSLShader * const * mPtr;
00198 };
00199
00200 static shader_iter beginShaders()
00201 {
00202 return sShaderList;
00203 }
00204
00205 static shader_iter endShaders()
00206 {
00207 return sShaderList + sNumShaders;
00208 }
00209
00210 static S32 sVertexShaderLevel[];
00211 static S32 sMaxAvatarShaderLevel;
00212
00213 enum EShaderClass
00214 {
00215 SHADER_LIGHTING,
00216 SHADER_OBJECT,
00217 SHADER_AVATAR,
00218 SHADER_ENVIRONMENT,
00219 SHADER_INTERFACE,
00220 SHADER_EFFECT,
00221 SHADER_WINDLIGHT,
00222 SHADER_WATER,
00223 SHADER_DEFERRED,
00224 SHADER_COUNT
00225 };
00226
00227 typedef enum
00228 {
00229 MATERIAL_COLOR = 0,
00230 SPECULAR_COLOR,
00231 BINORMAL,
00232 END_RESERVED_ATTRIBS
00233 } eGLSLReservedAttribs;
00234
00235 typedef enum
00236 {
00237 DIFFUSE_MAP = 0,
00238 SPECULAR_MAP,
00239 BUMP_MAP,
00240 ENVIRONMENT_MAP,
00241 CLOUD_NOISE_MAP,
00242 FULLBRIGHT,
00243 LIGHTNORM,
00244 SUNLIGHT_COLOR,
00245 AMBIENT,
00246 BLUE_HORIZON,
00247 BLUE_DENSITY,
00248 HAZE_HORIZON,
00249 HAZE_DENSITY,
00250 CLOUD_SHADOW,
00251 DENSITY_MULTIPLIER,
00252 DISTANCE_MULTIPLIER,
00253 MAX_Y,
00254 GLOW,
00255 CLOUD_COLOR,
00256 CLOUD_POS_DENSITY1,
00257 CLOUD_POS_DENSITY2,
00258 CLOUD_SCALE,
00259 GAMMA,
00260 SCENE_LIGHT_STRENGTH,
00261 END_RESERVED_UNIFORMS
00262 } eGLSLReservedUniforms;
00263
00264 typedef enum
00265 {
00266 SHINY_ORIGIN = END_RESERVED_UNIFORMS
00267 } eShinyUniforms;
00268
00269 typedef enum
00270 {
00271 WATER_SCREENTEX = END_RESERVED_UNIFORMS,
00272 WATER_SCREENDEPTH,
00273 WATER_REFTEX,
00274 WATER_EYEVEC,
00275 WATER_TIME,
00276 WATER_WAVE_DIR1,
00277 WATER_WAVE_DIR2,
00278 WATER_LIGHT_DIR,
00279 WATER_SPECULAR,
00280 WATER_SPECULAR_EXP,
00281 WATER_FOGCOLOR,
00282 WATER_FOGDENSITY,
00283 WATER_REFSCALE,
00284 WATER_WATERHEIGHT,
00285 } eWaterUniforms;
00286
00287 typedef enum
00288 {
00289 WL_CAMPOSLOCAL = END_RESERVED_UNIFORMS,
00290 WL_WATERHEIGHT
00291 } eWLUniforms;
00292
00293 typedef enum
00294 {
00295 TERRAIN_DETAIL0 = END_RESERVED_UNIFORMS,
00296 TERRAIN_DETAIL1,
00297 TERRAIN_DETAIL2,
00298 TERRAIN_DETAIL3,
00299 TERRAIN_ALPHARAMP
00300 } eTerrainUniforms;
00301
00302 typedef enum
00303 {
00304 GLOW_DELTA = END_RESERVED_UNIFORMS
00305 } eGlowUniforms;
00306
00307 typedef enum
00308 {
00309 AVATAR_WEIGHT = END_RESERVED_ATTRIBS,
00310 AVATAR_CLOTHING,
00311 AVATAR_WIND,
00312 AVATAR_SINWAVE,
00313 AVATAR_GRAVITY
00314 } eAvatarAttribs;
00315
00316 typedef enum
00317 {
00318 AVATAR_MATRIX = END_RESERVED_UNIFORMS
00319 } eAvatarUniforms;
00320
00321 private:
00322
00323
00324 static std::map<std::string, GLhandleARB> sShaderObjects;
00325
00326
00327 static std::vector<std::string> sReservedAttribs;
00328
00329 static std::vector<std::string> sReservedUniforms;
00330
00331 static std::vector<std::string> sShinyUniforms;
00332
00333
00334 static std::vector<std::string> sWaterUniforms;
00335
00336 static std::vector<std::string> sWLUniforms;
00337
00338
00339 static std::vector<std::string> sTerrainUniforms;
00340
00341
00342 static std::vector<std::string> sGlowUniforms;
00343
00344 static std::vector<std::string> sGlowExtractUniforms;
00345
00346
00347 static std::vector<std::string> sAvatarAttribs;
00348
00349 static std::vector<std::string> sAvatarUniforms;
00350
00351
00352
00353 static LLGLSLShader * const sShaderList[];
00354
00355
00356 static const size_t sNumShaders;
00357
00358 };
00359
00360 inline bool operator == (LLShaderMgr::shader_iter const & a, LLShaderMgr::shader_iter const & b)
00361 {
00362 return a.mPtr == b.mPtr;
00363 }
00364
00365 inline bool operator != (LLShaderMgr::shader_iter const & a, LLShaderMgr::shader_iter const & b)
00366 {
00367 return a.mPtr != b.mPtr;
00368 }
00369
00370
00371 extern LLVector4 gShinyOrigin;
00372
00373
00374 extern LLGLSLShader gObjectSimpleProgram;
00375 extern LLGLSLShader gObjectSimpleWaterProgram;
00376 extern LLGLSLShader gObjectFullbrightProgram;
00377 extern LLGLSLShader gObjectFullbrightWaterProgram;
00378
00379 extern LLGLSLShader gObjectSimpleLODProgram;
00380 extern LLGLSLShader gObjectFullbrightLODProgram;
00381
00382 extern LLGLSLShader gObjectFullbrightShinyProgram;
00383 extern LLGLSLShader gObjectShinyProgram;
00384 extern LLGLSLShader gObjectShinyWaterProgram;
00385
00386
00387 extern LLGLSLShader gTerrainProgram;
00388 extern LLGLSLShader gTerrainWaterProgram;
00389 extern LLGLSLShader gWaterProgram;
00390 extern LLGLSLShader gUnderWaterProgram;
00391 extern LLGLSLShader gGlowProgram;
00392 extern LLGLSLShader gGlowExtractProgram;
00393
00394
00395 extern LLGLSLShader gHighlightProgram;
00396
00397
00398 extern LLGLSLShader gAvatarProgram;
00399 extern LLGLSLShader gAvatarWaterProgram;
00400 extern LLGLSLShader gAvatarEyeballProgram;
00401 extern LLGLSLShader gAvatarPickProgram;
00402
00403
00404 extern LLGLSLShader gWLSkyProgram;
00405 extern LLGLSLShader gWLCloudProgram;
00406
00407
00408 extern LLGLSLShader gPostColorFilterProgram;
00409 extern LLGLSLShader gPostNightVisionProgram;
00410
00411
00412 extern LLGLSLShader gDeferredDiffuseProgram;
00413
00414
00415 extern GLint gAvatarMatrixParam;
00416
00417 #endif