PageRenderTime 204ms CodeModel.GetById 17ms app.highlight 165ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/newview/llviewershadermgr.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1574 lines | 1311 code | 178 blank | 85 comment | 114 complexity | d1740344ba146731b8c35e035ab60a18 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/** 
   2 * @file llviewershadermgr.cpp
   3 * @brief Viewer shader manager implementation.
   4 *
   5 * $LicenseInfo:firstyear=2005&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
  28#include "llviewerprecompiledheaders.h"
  29
  30#include "llfeaturemanager.h"
  31#include "llviewershadermgr.h"
  32
  33#include "llfile.h"
  34#include "llviewerwindow.h"
  35#include "llwindow.h"
  36#include "llviewercontrol.h"
  37#include "pipeline.h"
  38#include "llworld.h"
  39#include "llwlparammanager.h"
  40#include "llwaterparammanager.h"
  41#include "llsky.h"
  42#include "llvosky.h"
  43#include "llrender.h"
  44
  45#if LL_DARWIN
  46#include "OpenGL/OpenGL.h"
  47#endif
  48
  49#ifdef LL_RELEASE_FOR_DOWNLOAD
  50#define UNIFORM_ERRS LL_WARNS_ONCE("Shader")
  51#else
  52#define UNIFORM_ERRS LL_ERRS("Shader")
  53#endif
  54
  55// Lots of STL stuff in here, using namespace std to keep things more readable
  56using std::vector;
  57using std::pair;
  58using std::make_pair;
  59using std::string;
  60
  61BOOL				LLViewerShaderMgr::sInitialized = FALSE;
  62bool				LLViewerShaderMgr::sSkipReload = false;
  63
  64LLVector4			gShinyOrigin;
  65
  66//utility shaders
  67LLGLSLShader	gOcclusionProgram;
  68LLGLSLShader	gCustomAlphaProgram;
  69LLGLSLShader	gGlowCombineProgram;
  70LLGLSLShader	gSplatTextureRectProgram;
  71LLGLSLShader	gGlowCombineFXAAProgram;
  72LLGLSLShader	gTwoTextureAddProgram;
  73LLGLSLShader	gOneTextureNoColorProgram;
  74LLGLSLShader	gDebugProgram;
  75LLGLSLShader	gAlphaMaskProgram;
  76
  77//object shaders
  78LLGLSLShader		gObjectSimpleProgram;
  79LLGLSLShader		gObjectPreviewProgram;
  80LLGLSLShader		gObjectSimpleWaterProgram;
  81LLGLSLShader		gObjectSimpleAlphaMaskProgram;
  82LLGLSLShader		gObjectSimpleWaterAlphaMaskProgram;
  83LLGLSLShader		gObjectFullbrightProgram;
  84LLGLSLShader		gObjectFullbrightWaterProgram;
  85LLGLSLShader		gObjectEmissiveProgram;
  86LLGLSLShader		gObjectEmissiveWaterProgram;
  87LLGLSLShader		gObjectFullbrightAlphaMaskProgram;
  88LLGLSLShader		gObjectFullbrightWaterAlphaMaskProgram;
  89LLGLSLShader		gObjectFullbrightShinyProgram;
  90LLGLSLShader		gObjectFullbrightShinyWaterProgram;
  91LLGLSLShader		gObjectShinyProgram;
  92LLGLSLShader		gObjectShinyWaterProgram;
  93LLGLSLShader		gObjectBumpProgram;
  94LLGLSLShader		gTreeProgram;
  95LLGLSLShader		gTreeWaterProgram;
  96LLGLSLShader		gObjectFullbrightNoColorProgram;
  97LLGLSLShader		gObjectFullbrightNoColorWaterProgram;
  98
  99LLGLSLShader		gObjectSimpleNonIndexedProgram;
 100LLGLSLShader		gObjectSimpleNonIndexedTexGenProgram;
 101LLGLSLShader		gObjectSimpleNonIndexedTexGenWaterProgram;
 102LLGLSLShader		gObjectSimpleNonIndexedWaterProgram;
 103LLGLSLShader		gObjectAlphaMaskNonIndexedProgram;
 104LLGLSLShader		gObjectAlphaMaskNonIndexedWaterProgram;
 105LLGLSLShader		gObjectAlphaMaskNoColorProgram;
 106LLGLSLShader		gObjectAlphaMaskNoColorWaterProgram;
 107LLGLSLShader		gObjectFullbrightNonIndexedProgram;
 108LLGLSLShader		gObjectFullbrightNonIndexedWaterProgram;
 109LLGLSLShader		gObjectEmissiveNonIndexedProgram;
 110LLGLSLShader		gObjectEmissiveNonIndexedWaterProgram;
 111LLGLSLShader		gObjectFullbrightShinyNonIndexedProgram;
 112LLGLSLShader		gObjectFullbrightShinyNonIndexedWaterProgram;
 113LLGLSLShader		gObjectShinyNonIndexedProgram;
 114LLGLSLShader		gObjectShinyNonIndexedWaterProgram;
 115
 116//object hardware skinning shaders
 117LLGLSLShader		gSkinnedObjectSimpleProgram;
 118LLGLSLShader		gSkinnedObjectFullbrightProgram;
 119LLGLSLShader		gSkinnedObjectEmissiveProgram;
 120LLGLSLShader		gSkinnedObjectFullbrightShinyProgram;
 121LLGLSLShader		gSkinnedObjectShinySimpleProgram;
 122
 123LLGLSLShader		gSkinnedObjectSimpleWaterProgram;
 124LLGLSLShader		gSkinnedObjectFullbrightWaterProgram;
 125LLGLSLShader		gSkinnedObjectEmissiveWaterProgram;
 126LLGLSLShader		gSkinnedObjectFullbrightShinyWaterProgram;
 127LLGLSLShader		gSkinnedObjectShinySimpleWaterProgram;
 128
 129//environment shaders
 130LLGLSLShader		gTerrainProgram;
 131LLGLSLShader		gTerrainWaterProgram;
 132LLGLSLShader		gWaterProgram;
 133LLGLSLShader		gUnderWaterProgram;
 134
 135//interface shaders
 136LLGLSLShader		gHighlightProgram;
 137
 138//avatar shader handles
 139LLGLSLShader		gAvatarProgram;
 140LLGLSLShader		gAvatarWaterProgram;
 141LLGLSLShader		gAvatarEyeballProgram;
 142LLGLSLShader		gAvatarPickProgram;
 143LLGLSLShader		gImpostorProgram;
 144
 145// WindLight shader handles
 146LLGLSLShader			gWLSkyProgram;
 147LLGLSLShader			gWLCloudProgram;
 148
 149
 150// Effects Shaders
 151LLGLSLShader			gGlowProgram;
 152LLGLSLShader			gGlowExtractProgram;
 153LLGLSLShader			gPostColorFilterProgram;
 154LLGLSLShader			gPostNightVisionProgram;
 155
 156// Deferred rendering shaders
 157LLGLSLShader			gDeferredImpostorProgram;
 158LLGLSLShader			gDeferredWaterProgram;
 159LLGLSLShader			gDeferredDiffuseProgram;
 160LLGLSLShader			gDeferredDiffuseAlphaMaskProgram;
 161LLGLSLShader			gDeferredNonIndexedDiffuseProgram;
 162LLGLSLShader			gDeferredNonIndexedDiffuseAlphaMaskProgram;
 163LLGLSLShader			gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram;
 164LLGLSLShader			gDeferredSkinnedDiffuseProgram;
 165LLGLSLShader			gDeferredSkinnedBumpProgram;
 166LLGLSLShader			gDeferredSkinnedAlphaProgram;
 167LLGLSLShader			gDeferredBumpProgram;
 168LLGLSLShader			gDeferredTerrainProgram;
 169LLGLSLShader			gDeferredTreeProgram;
 170LLGLSLShader			gDeferredTreeShadowProgram;
 171LLGLSLShader			gDeferredAvatarProgram;
 172LLGLSLShader			gDeferredAvatarAlphaProgram;
 173LLGLSLShader			gDeferredLightProgram;
 174LLGLSLShader			gDeferredMultiLightProgram;
 175LLGLSLShader			gDeferredSpotLightProgram;
 176LLGLSLShader			gDeferredMultiSpotLightProgram;
 177LLGLSLShader			gDeferredSunProgram;
 178LLGLSLShader			gDeferredBlurLightProgram;
 179LLGLSLShader			gDeferredSoftenProgram;
 180LLGLSLShader			gDeferredShadowProgram;
 181LLGLSLShader			gDeferredShadowAlphaMaskProgram;
 182LLGLSLShader			gDeferredAvatarShadowProgram;
 183LLGLSLShader			gDeferredAttachmentShadowProgram;
 184LLGLSLShader			gDeferredAlphaProgram;
 185LLGLSLShader			gDeferredAvatarEyesProgram;
 186LLGLSLShader			gDeferredFullbrightProgram;
 187LLGLSLShader			gDeferredEmissiveProgram;
 188LLGLSLShader			gDeferredPostProgram;
 189LLGLSLShader			gDeferredCoFProgram;
 190LLGLSLShader			gDeferredDoFCombineProgram;
 191LLGLSLShader			gFXAAProgram;
 192LLGLSLShader			gDeferredPostNoDoFProgram;
 193LLGLSLShader			gDeferredWLSkyProgram;
 194LLGLSLShader			gDeferredWLCloudProgram;
 195LLGLSLShader			gDeferredStarProgram;
 196LLGLSLShader			gNormalMapGenProgram;
 197
 198LLViewerShaderMgr::LLViewerShaderMgr() :
 199	mVertexShaderLevel(SHADER_COUNT, 0),
 200	mMaxAvatarShaderLevel(0)
 201{	
 202	/// Make sure WL Sky is the first program
 203	//ONLY shaders that need WL Param management should be added here
 204	mShaderList.push_back(&gWLSkyProgram);
 205	mShaderList.push_back(&gWLCloudProgram);
 206	mShaderList.push_back(&gAvatarProgram);
 207	mShaderList.push_back(&gObjectShinyProgram);
 208	mShaderList.push_back(&gObjectShinyNonIndexedProgram);
 209	mShaderList.push_back(&gWaterProgram);
 210	mShaderList.push_back(&gAvatarEyeballProgram); 
 211	mShaderList.push_back(&gObjectSimpleProgram);
 212	mShaderList.push_back(&gObjectPreviewProgram);
 213	mShaderList.push_back(&gImpostorProgram);
 214	mShaderList.push_back(&gObjectFullbrightNoColorProgram);
 215	mShaderList.push_back(&gObjectFullbrightNoColorWaterProgram);
 216	mShaderList.push_back(&gObjectSimpleAlphaMaskProgram);
 217	mShaderList.push_back(&gObjectBumpProgram);
 218	mShaderList.push_back(&gObjectEmissiveProgram);
 219	mShaderList.push_back(&gObjectEmissiveWaterProgram);
 220	mShaderList.push_back(&gObjectFullbrightProgram);
 221	mShaderList.push_back(&gObjectFullbrightAlphaMaskProgram);
 222	mShaderList.push_back(&gObjectFullbrightShinyProgram);
 223	mShaderList.push_back(&gObjectFullbrightShinyWaterProgram);
 224	mShaderList.push_back(&gObjectSimpleNonIndexedProgram);
 225	mShaderList.push_back(&gObjectSimpleNonIndexedTexGenProgram);
 226	mShaderList.push_back(&gObjectSimpleNonIndexedTexGenWaterProgram);
 227	mShaderList.push_back(&gObjectSimpleNonIndexedWaterProgram);
 228	mShaderList.push_back(&gObjectAlphaMaskNonIndexedProgram);
 229	mShaderList.push_back(&gObjectAlphaMaskNonIndexedWaterProgram);
 230	mShaderList.push_back(&gObjectAlphaMaskNoColorProgram);
 231	mShaderList.push_back(&gObjectAlphaMaskNoColorWaterProgram);
 232	mShaderList.push_back(&gTreeProgram);
 233	mShaderList.push_back(&gTreeWaterProgram);
 234	mShaderList.push_back(&gObjectFullbrightNonIndexedProgram);
 235	mShaderList.push_back(&gObjectFullbrightNonIndexedWaterProgram);
 236	mShaderList.push_back(&gObjectEmissiveNonIndexedProgram);
 237	mShaderList.push_back(&gObjectEmissiveNonIndexedWaterProgram);
 238	mShaderList.push_back(&gObjectFullbrightShinyNonIndexedProgram);
 239	mShaderList.push_back(&gObjectFullbrightShinyNonIndexedWaterProgram);
 240	mShaderList.push_back(&gSkinnedObjectSimpleProgram);
 241	mShaderList.push_back(&gSkinnedObjectFullbrightProgram);
 242	mShaderList.push_back(&gSkinnedObjectEmissiveProgram);
 243	mShaderList.push_back(&gSkinnedObjectFullbrightShinyProgram);
 244	mShaderList.push_back(&gSkinnedObjectShinySimpleProgram);
 245	mShaderList.push_back(&gSkinnedObjectSimpleWaterProgram);
 246	mShaderList.push_back(&gSkinnedObjectFullbrightWaterProgram);
 247	mShaderList.push_back(&gSkinnedObjectEmissiveWaterProgram);
 248	mShaderList.push_back(&gSkinnedObjectFullbrightShinyWaterProgram);
 249	mShaderList.push_back(&gSkinnedObjectShinySimpleWaterProgram);
 250	mShaderList.push_back(&gTerrainProgram);
 251	mShaderList.push_back(&gTerrainWaterProgram);
 252	mShaderList.push_back(&gObjectSimpleWaterProgram);
 253	mShaderList.push_back(&gObjectFullbrightWaterProgram);
 254	mShaderList.push_back(&gObjectSimpleWaterAlphaMaskProgram);
 255	mShaderList.push_back(&gObjectFullbrightWaterAlphaMaskProgram);
 256	mShaderList.push_back(&gAvatarWaterProgram);
 257	mShaderList.push_back(&gObjectShinyWaterProgram);
 258	mShaderList.push_back(&gObjectShinyNonIndexedWaterProgram);
 259	mShaderList.push_back(&gUnderWaterProgram);
 260	mShaderList.push_back(&gDeferredSunProgram);
 261	mShaderList.push_back(&gDeferredSoftenProgram);
 262	mShaderList.push_back(&gDeferredAlphaProgram);
 263	mShaderList.push_back(&gDeferredSkinnedAlphaProgram);
 264	mShaderList.push_back(&gDeferredFullbrightProgram);
 265	mShaderList.push_back(&gDeferredEmissiveProgram);
 266	mShaderList.push_back(&gDeferredAvatarEyesProgram);
 267	mShaderList.push_back(&gDeferredWaterProgram);
 268	mShaderList.push_back(&gDeferredAvatarAlphaProgram);
 269	mShaderList.push_back(&gDeferredWLSkyProgram);
 270	mShaderList.push_back(&gDeferredWLCloudProgram);
 271}
 272
 273LLViewerShaderMgr::~LLViewerShaderMgr()
 274{
 275	mVertexShaderLevel.clear();
 276	mShaderList.clear();
 277}
 278
 279// static
 280LLViewerShaderMgr * LLViewerShaderMgr::instance()
 281{
 282	if(NULL == sInstance)
 283	{
 284		sInstance = new LLViewerShaderMgr();
 285	}
 286
 287	return static_cast<LLViewerShaderMgr*>(sInstance);
 288}
 289
 290void LLViewerShaderMgr::initAttribsAndUniforms(void)
 291{
 292	if (mReservedAttribs.empty())
 293	{
 294		LLShaderMgr::initAttribsAndUniforms();
 295
 296		mAvatarUniforms.push_back("matrixPalette");
 297		mAvatarUniforms.push_back("gWindDir");
 298		mAvatarUniforms.push_back("gSinWaveParams");
 299		mAvatarUniforms.push_back("gGravity");
 300
 301		mWLUniforms.push_back("camPosLocal");
 302
 303		mTerrainUniforms.reserve(5);
 304		mTerrainUniforms.push_back("detail_0");
 305		mTerrainUniforms.push_back("detail_1");
 306		mTerrainUniforms.push_back("detail_2");
 307		mTerrainUniforms.push_back("detail_3");
 308		mTerrainUniforms.push_back("alpha_ramp");
 309
 310		mGlowUniforms.push_back("glowDelta");
 311		mGlowUniforms.push_back("glowStrength");
 312
 313		mGlowExtractUniforms.push_back("minLuminance");
 314		mGlowExtractUniforms.push_back("maxExtractAlpha");
 315		mGlowExtractUniforms.push_back("lumWeights");
 316		mGlowExtractUniforms.push_back("warmthWeights");
 317		mGlowExtractUniforms.push_back("warmthAmount");
 318
 319		mShinyUniforms.push_back("origin");
 320
 321		mWaterUniforms.reserve(12);
 322		mWaterUniforms.push_back("screenTex");
 323		mWaterUniforms.push_back("screenDepth");
 324		mWaterUniforms.push_back("refTex");
 325		mWaterUniforms.push_back("eyeVec");
 326		mWaterUniforms.push_back("time");
 327		mWaterUniforms.push_back("d1");
 328		mWaterUniforms.push_back("d2");
 329		mWaterUniforms.push_back("lightDir");
 330		mWaterUniforms.push_back("specular");
 331		mWaterUniforms.push_back("lightExp");
 332		mWaterUniforms.push_back("fogCol");
 333		mWaterUniforms.push_back("kd");
 334		mWaterUniforms.push_back("refScale");
 335		mWaterUniforms.push_back("waterHeight");
 336	}	
 337}
 338	
 339
 340//============================================================================
 341// Set Levels
 342
 343S32 LLViewerShaderMgr::getVertexShaderLevel(S32 type)
 344{
 345	return LLPipeline::sDisableShaders ? 0 : mVertexShaderLevel[type];
 346}
 347
 348//============================================================================
 349// Shader Management
 350
 351void LLViewerShaderMgr::setShaders()
 352{
 353	//setShaders might be called redundantly by gSavedSettings, so return on reentrance
 354	static bool reentrance = false;
 355	
 356	if (!gPipeline.mInitialized || !sInitialized || reentrance || sSkipReload)
 357	{
 358		return;
 359	}
 360
 361	LLGLSLShader::sIndexedTextureChannels = llmax(llmin(gGLManager.mNumTextureImageUnits, (S32) gSavedSettings.getU32("RenderMaxTextureIndex")), 1);
 362
 363	//NEVER use more than 16 texture channels (work around for prevalent driver bug)
 364	LLGLSLShader::sIndexedTextureChannels = llmin(LLGLSLShader::sIndexedTextureChannels, 16);
 365
 366	reentrance = true;
 367
 368	if (LLRender::sGLCoreProfile)
 369	{  
 370		if (!gSavedSettings.getBOOL("VertexShaderEnable"))
 371		{ //vertex shaders MUST be enabled to use core profile
 372			gSavedSettings.setBOOL("VertexShaderEnable", TRUE);
 373		}
 374	}
 375	
 376	//setup preprocessor definitions
 377	LLShaderMgr::instance()->mDefinitions["NUM_TEX_UNITS"] = llformat("%d", gGLManager.mNumTextureImageUnits);
 378	
 379	// Make sure the compiled shader map is cleared before we recompile shaders.
 380	mShaderObjects.clear();
 381	
 382	initAttribsAndUniforms();
 383	gPipeline.releaseGLBuffers();
 384
 385	if (gSavedSettings.getBOOL("VertexShaderEnable"))
 386	{
 387		LLPipeline::sWaterReflections = gGLManager.mHasCubeMap;
 388		LLPipeline::sRenderGlow = gSavedSettings.getBOOL("RenderGlow"); 
 389		LLPipeline::updateRenderDeferred();
 390	}
 391	else
 392	{
 393		LLPipeline::sRenderGlow = FALSE;
 394		LLPipeline::sWaterReflections = FALSE;
 395	}
 396	
 397	//hack to reset buffers that change behavior with shaders
 398	gPipeline.resetVertexBuffers();
 399
 400	if (gViewerWindow)
 401	{
 402		gViewerWindow->setCursor(UI_CURSOR_WAIT);
 403	}
 404
 405	// Lighting
 406	gPipeline.setLightingDetail(-1);
 407
 408	// Shaders
 409	LL_INFOS("ShaderLoading") << "\n~~~~~~~~~~~~~~~~~~\n Loading Shaders:\n~~~~~~~~~~~~~~~~~~" << LL_ENDL;
 410	for (S32 i = 0; i < SHADER_COUNT; i++)
 411	{
 412		mVertexShaderLevel[i] = 0;
 413	}
 414	mMaxAvatarShaderLevel = 0;
 415
 416	LLGLSLShader::sNoFixedFunction = false;
 417	LLVertexBuffer::unbind();
 418	if (LLFeatureManager::getInstance()->isFeatureAvailable("VertexShaderEnable") 
 419		&& gSavedSettings.getBOOL("VertexShaderEnable"))
 420	{
 421		//using shaders, disable fixed function
 422		LLGLSLShader::sNoFixedFunction = true;
 423
 424		S32 light_class = 2;
 425		S32 env_class = 2;
 426		S32 obj_class = 2;
 427		S32 effect_class = 2;
 428		S32 wl_class = 2;
 429		S32 water_class = 2;
 430		S32 deferred_class = 0;
 431		
 432		if (LLFeatureManager::getInstance()->isFeatureAvailable("RenderDeferred") &&
 433		    gSavedSettings.getBOOL("RenderDeferred") &&
 434			gSavedSettings.getBOOL("RenderAvatarVP") &&
 435			gSavedSettings.getBOOL("WindLightUseAtmosShaders"))
 436		{
 437			if (gSavedSettings.getS32("RenderShadowDetail") > 0)
 438			{ //shadows
 439				deferred_class = 2;
 440			}
 441			else
 442			{ //no shadows
 443				deferred_class = 1;
 444			}
 445
 446			//make sure hardware skinning is enabled
 447			//gSavedSettings.setBOOL("RenderAvatarVP", TRUE);
 448			
 449			//make sure atmospheric shaders are enabled
 450			//gSavedSettings.setBOOL("WindLightUseAtmosShaders", TRUE);
 451		}
 452
 453
 454		if (!(LLFeatureManager::getInstance()->isFeatureAvailable("WindLightUseAtmosShaders")
 455			  && gSavedSettings.getBOOL("WindLightUseAtmosShaders")))
 456		{
 457			// user has disabled WindLight in their settings, downgrade
 458			// windlight shaders to stub versions.
 459			wl_class = 1;
 460		}
 461
 462		
 463		// Trigger a full rebuild of the fallback skybox / cubemap if we've toggled windlight shaders
 464		if (mVertexShaderLevel[SHADER_WINDLIGHT] != wl_class && gSky.mVOSkyp.notNull())
 465		{
 466			gSky.mVOSkyp->forceSkyUpdate();
 467		}
 468
 469		// Load lighting shaders
 470		mVertexShaderLevel[SHADER_LIGHTING] = light_class;
 471		mVertexShaderLevel[SHADER_INTERFACE] = light_class;
 472		mVertexShaderLevel[SHADER_ENVIRONMENT] = env_class;
 473		mVertexShaderLevel[SHADER_WATER] = water_class;
 474		mVertexShaderLevel[SHADER_OBJECT] = obj_class;
 475		mVertexShaderLevel[SHADER_EFFECT] = effect_class;
 476		mVertexShaderLevel[SHADER_WINDLIGHT] = wl_class;
 477		mVertexShaderLevel[SHADER_DEFERRED] = deferred_class;
 478
 479		BOOL loaded = loadBasicShaders();
 480
 481		if (loaded)
 482		{
 483			gPipeline.mVertexShadersEnabled = TRUE;
 484			gPipeline.mVertexShadersLoaded = 1;
 485
 486			// Load all shaders to set max levels
 487			loadShadersEnvironment();
 488			loadShadersWater();
 489			loadShadersWindLight();
 490			loadShadersEffects();
 491			loadShadersInterface();
 492			
 493			// Load max avatar shaders to set the max level
 494			mVertexShaderLevel[SHADER_AVATAR] = 3;
 495			mMaxAvatarShaderLevel = 3;
 496						
 497			if (gSavedSettings.getBOOL("RenderAvatarVP") && loadShadersObject())
 498			{ //hardware skinning is enabled and rigged attachment shaders loaded correctly
 499				BOOL avatar_cloth = gSavedSettings.getBOOL("RenderAvatarCloth");
 500				S32 avatar_class = 1;
 501				
 502				// cloth is a class3 shader
 503				if(avatar_cloth)
 504				{
 505					avatar_class = 3;
 506				}
 507
 508				// Set the actual level
 509				mVertexShaderLevel[SHADER_AVATAR] = avatar_class;
 510				loadShadersAvatar();
 511				if (mVertexShaderLevel[SHADER_AVATAR] != avatar_class)
 512				{
 513					if (mVertexShaderLevel[SHADER_AVATAR] == 0)
 514					{
 515						gSavedSettings.setBOOL("RenderAvatarVP", FALSE);
 516					}
 517					if(llmax(mVertexShaderLevel[SHADER_AVATAR]-1,0) >= 3)
 518					{
 519						avatar_cloth = true;
 520					}
 521					else
 522					{
 523						avatar_cloth = false;
 524					}
 525					gSavedSettings.setBOOL("RenderAvatarCloth", avatar_cloth);
 526				}
 527			}
 528			else
 529			{ //hardware skinning not possible, neither is deferred rendering
 530				mVertexShaderLevel[SHADER_AVATAR] = 0;
 531				mVertexShaderLevel[SHADER_DEFERRED] = 0;
 532
 533				if (gSavedSettings.getBOOL("RenderAvatarVP"))
 534				{
 535					gSavedSettings.setBOOL("RenderDeferred", FALSE);
 536					gSavedSettings.setBOOL("RenderAvatarCloth", FALSE);
 537					gSavedSettings.setBOOL("RenderAvatarVP", FALSE);
 538				}
 539
 540				loadShadersAvatar(); // unloads
 541				loadShadersObject();
 542			}
 543
 544			if (!loadShadersDeferred())
 545			{
 546				gSavedSettings.setBOOL("RenderDeferred", FALSE);
 547				reentrance = false;
 548				setShaders();
 549				return;
 550			}
 551		}
 552		else
 553		{
 554			LLGLSLShader::sNoFixedFunction = false;
 555			gPipeline.mVertexShadersEnabled = FALSE;
 556			gPipeline.mVertexShadersLoaded = 0;
 557			mVertexShaderLevel[SHADER_LIGHTING] = 0;
 558			mVertexShaderLevel[SHADER_INTERFACE] = 0;
 559			mVertexShaderLevel[SHADER_ENVIRONMENT] = 0;
 560			mVertexShaderLevel[SHADER_WATER] = 0;
 561			mVertexShaderLevel[SHADER_OBJECT] = 0;
 562			mVertexShaderLevel[SHADER_EFFECT] = 0;
 563			mVertexShaderLevel[SHADER_WINDLIGHT] = 0;
 564			mVertexShaderLevel[SHADER_AVATAR] = 0;
 565		}
 566	}
 567	else
 568	{
 569		LLGLSLShader::sNoFixedFunction = false;
 570		gPipeline.mVertexShadersEnabled = FALSE;
 571		gPipeline.mVertexShadersLoaded = 0;
 572		mVertexShaderLevel[SHADER_LIGHTING] = 0;
 573		mVertexShaderLevel[SHADER_INTERFACE] = 0;
 574		mVertexShaderLevel[SHADER_ENVIRONMENT] = 0;
 575		mVertexShaderLevel[SHADER_WATER] = 0;
 576		mVertexShaderLevel[SHADER_OBJECT] = 0;
 577		mVertexShaderLevel[SHADER_EFFECT] = 0;
 578		mVertexShaderLevel[SHADER_WINDLIGHT] = 0;
 579		mVertexShaderLevel[SHADER_AVATAR] = 0;
 580	}
 581	
 582	if (gViewerWindow)
 583	{
 584		gViewerWindow->setCursor(UI_CURSOR_ARROW);
 585	}
 586	gPipeline.createGLBuffers();
 587
 588	reentrance = false;
 589}
 590
 591void LLViewerShaderMgr::unloadShaders()
 592{
 593	gOcclusionProgram.unload();
 594	gDebugProgram.unload();
 595	gAlphaMaskProgram.unload();
 596	gUIProgram.unload();
 597	gCustomAlphaProgram.unload();
 598	gGlowCombineProgram.unload();
 599	gSplatTextureRectProgram.unload();
 600	gGlowCombineFXAAProgram.unload();
 601	gTwoTextureAddProgram.unload();
 602	gOneTextureNoColorProgram.unload();
 603	gSolidColorProgram.unload();
 604
 605	gObjectFullbrightNoColorProgram.unload();
 606	gObjectFullbrightNoColorWaterProgram.unload();
 607	gObjectSimpleProgram.unload();
 608	gObjectPreviewProgram.unload();
 609	gImpostorProgram.unload();
 610	gObjectSimpleAlphaMaskProgram.unload();
 611	gObjectBumpProgram.unload();
 612	gObjectSimpleWaterProgram.unload();
 613	gObjectSimpleWaterAlphaMaskProgram.unload();
 614	gObjectFullbrightProgram.unload();
 615	gObjectFullbrightWaterProgram.unload();
 616	gObjectEmissiveProgram.unload();
 617	gObjectEmissiveWaterProgram.unload();
 618	gObjectFullbrightAlphaMaskProgram.unload();
 619	gObjectFullbrightWaterAlphaMaskProgram.unload();
 620
 621	gObjectShinyProgram.unload();
 622	gObjectFullbrightShinyProgram.unload();
 623	gObjectFullbrightShinyWaterProgram.unload();
 624	gObjectShinyWaterProgram.unload();
 625
 626	gObjectSimpleNonIndexedProgram.unload();
 627	gObjectSimpleNonIndexedTexGenProgram.unload();
 628	gObjectSimpleNonIndexedTexGenWaterProgram.unload();
 629	gObjectSimpleNonIndexedWaterProgram.unload();
 630	gObjectAlphaMaskNonIndexedProgram.unload();
 631	gObjectAlphaMaskNonIndexedWaterProgram.unload();
 632	gObjectAlphaMaskNoColorProgram.unload();
 633	gObjectAlphaMaskNoColorWaterProgram.unload();
 634	gObjectFullbrightNonIndexedProgram.unload();
 635	gObjectFullbrightNonIndexedWaterProgram.unload();
 636	gObjectEmissiveNonIndexedProgram.unload();
 637	gObjectEmissiveNonIndexedWaterProgram.unload();
 638	gTreeProgram.unload();
 639	gTreeWaterProgram.unload();
 640
 641	gObjectShinyNonIndexedProgram.unload();
 642	gObjectFullbrightShinyNonIndexedProgram.unload();
 643	gObjectFullbrightShinyNonIndexedWaterProgram.unload();
 644	gObjectShinyNonIndexedWaterProgram.unload();
 645
 646	gSkinnedObjectSimpleProgram.unload();
 647	gSkinnedObjectFullbrightProgram.unload();
 648	gSkinnedObjectEmissiveProgram.unload();
 649	gSkinnedObjectFullbrightShinyProgram.unload();
 650	gSkinnedObjectShinySimpleProgram.unload();
 651	
 652	gSkinnedObjectSimpleWaterProgram.unload();
 653	gSkinnedObjectFullbrightWaterProgram.unload();
 654	gSkinnedObjectEmissiveWaterProgram.unload();
 655	gSkinnedObjectFullbrightShinyWaterProgram.unload();
 656	gSkinnedObjectShinySimpleWaterProgram.unload();
 657	
 658
 659	gWaterProgram.unload();
 660	gUnderWaterProgram.unload();
 661	gTerrainProgram.unload();
 662	gTerrainWaterProgram.unload();
 663	gGlowProgram.unload();
 664	gGlowExtractProgram.unload();
 665	gAvatarProgram.unload();
 666	gAvatarWaterProgram.unload();
 667	gAvatarEyeballProgram.unload();
 668	gAvatarPickProgram.unload();
 669	gHighlightProgram.unload();
 670
 671	gWLSkyProgram.unload();
 672	gWLCloudProgram.unload();
 673
 674	gPostColorFilterProgram.unload();
 675	gPostNightVisionProgram.unload();
 676
 677	gDeferredDiffuseProgram.unload();
 678	gDeferredDiffuseAlphaMaskProgram.unload();
 679	gDeferredNonIndexedDiffuseAlphaMaskProgram.unload();
 680	gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.unload();
 681	gDeferredNonIndexedDiffuseProgram.unload();
 682	gDeferredSkinnedDiffuseProgram.unload();
 683	gDeferredSkinnedBumpProgram.unload();
 684	gDeferredSkinnedAlphaProgram.unload();
 685
 686	mVertexShaderLevel[SHADER_LIGHTING] = 0;
 687	mVertexShaderLevel[SHADER_OBJECT] = 0;
 688	mVertexShaderLevel[SHADER_AVATAR] = 0;
 689	mVertexShaderLevel[SHADER_ENVIRONMENT] = 0;
 690	mVertexShaderLevel[SHADER_WATER] = 0;
 691	mVertexShaderLevel[SHADER_INTERFACE] = 0;
 692	mVertexShaderLevel[SHADER_EFFECT] = 0;
 693	mVertexShaderLevel[SHADER_WINDLIGHT] = 0;
 694
 695	gPipeline.mVertexShadersLoaded = 0;
 696}
 697
 698BOOL LLViewerShaderMgr::loadBasicShaders()
 699{
 700	// Load basic dependency shaders first
 701	// All of these have to load for any shaders to function
 702	
 703#if LL_DARWIN // Mac can't currently handle all 8 lights, 
 704	S32 sum_lights_class = 2;
 705#else 
 706	S32 sum_lights_class = 3;
 707
 708	// class one cards will get the lower sum lights
 709	// class zero we're not going to think about
 710	// since a class zero card COULD be a ridiculous new card
 711	// and old cards should have the features masked
 712	if(LLFeatureManager::getInstance()->getGPUClass() == GPU_CLASS_1)
 713	{
 714		sum_lights_class = 2;
 715	}
 716#endif
 717
 718	// If we have sun and moon only checked, then only sum those lights.
 719	if (gPipeline.getLightingDetail() == 0)
 720	{
 721		sum_lights_class = 1;
 722	}
 723
 724	// Use the feature table to mask out the max light level to use.  Also make sure it's at least 1.
 725	S32 max_light_class = gSavedSettings.getS32("RenderShaderLightingMaxLevel");
 726	sum_lights_class = llclamp(sum_lights_class, 1, max_light_class);
 727
 728	// Load the Basic Vertex Shaders at the appropriate level. 
 729	// (in order of shader function call depth for reference purposes, deepest level first)
 730
 731	vector< pair<string, S32> > shaders;
 732	shaders.push_back( make_pair( "windlight/atmosphericsVarsV.glsl",		mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 733	shaders.push_back( make_pair( "windlight/atmosphericsVarsWaterV.glsl",		mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 734	shaders.push_back( make_pair( "windlight/atmosphericsHelpersV.glsl",	mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 735	shaders.push_back( make_pair( "lighting/lightFuncV.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 736	shaders.push_back( make_pair( "lighting/sumLightsV.glsl",				sum_lights_class ) );
 737	shaders.push_back( make_pair( "lighting/lightV.glsl",					mVertexShaderLevel[SHADER_LIGHTING] ) );
 738	shaders.push_back( make_pair( "lighting/lightFuncSpecularV.glsl",		mVertexShaderLevel[SHADER_LIGHTING] ) );
 739	shaders.push_back( make_pair( "lighting/sumLightsSpecularV.glsl",		sum_lights_class ) );
 740	shaders.push_back( make_pair( "lighting/lightSpecularV.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 741	shaders.push_back( make_pair( "windlight/atmosphericsV.glsl",			mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 742	shaders.push_back( make_pair( "avatar/avatarSkinV.glsl",				1 ) );
 743	shaders.push_back( make_pair( "avatar/objectSkinV.glsl",				1 ) );
 744	shaders.push_back( make_pair( "objects/indexedTextureV.glsl",			1 ) );
 745	shaders.push_back( make_pair( "objects/nonindexedTextureV.glsl",		1 ) );
 746
 747	// We no longer have to bind the shaders to global glhandles, they are automatically added to a map now.
 748	for (U32 i = 0; i < shaders.size(); i++)
 749	{
 750		// Note usage of GL_VERTEX_SHADER_ARB
 751		if (loadShaderFile(shaders[i].first, shaders[i].second, GL_VERTEX_SHADER_ARB) == 0)
 752		{
 753			return FALSE;
 754		}
 755	}
 756
 757	// Load the Basic Fragment Shaders at the appropriate level. 
 758	// (in order of shader function call depth for reference purposes, deepest level first)
 759
 760	shaders.clear();
 761	S32 ch = llmax(LLGLSLShader::sIndexedTextureChannels-1, 1);
 762
 763	if (gGLManager.mGLVersion < 3.1f)
 764	{ //force to 1 texture index channel for old drivers
 765		ch = 1;
 766	}
 767
 768	std::vector<S32> index_channels;
 769	index_channels.push_back(-1);	 shaders.push_back( make_pair( "windlight/atmosphericsVarsF.glsl",		mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 770	index_channels.push_back(-1);	 shaders.push_back( make_pair( "windlight/atmosphericsVarsWaterF.glsl",		mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 771	index_channels.push_back(-1);	 shaders.push_back( make_pair( "windlight/gammaF.glsl",					mVertexShaderLevel[SHADER_WINDLIGHT]) );
 772	index_channels.push_back(-1);	 shaders.push_back( make_pair( "windlight/atmosphericsF.glsl",			mVertexShaderLevel[SHADER_WINDLIGHT] ) );
 773	index_channels.push_back(-1);	 shaders.push_back( make_pair( "windlight/transportF.glsl",				mVertexShaderLevel[SHADER_WINDLIGHT] ) );	
 774	index_channels.push_back(-1);	 shaders.push_back( make_pair( "environment/waterFogF.glsl",				mVertexShaderLevel[SHADER_WATER] ) );
 775	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightNonIndexedF.glsl",					mVertexShaderLevel[SHADER_LIGHTING] ) );
 776	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightAlphaMaskNonIndexedF.glsl",					mVertexShaderLevel[SHADER_LIGHTING] ) );
 777	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightNonIndexedF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 778	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightNonIndexedAlphaMaskF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 779	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightWaterNonIndexedF.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 780	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightWaterAlphaMaskNonIndexedF.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 781	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightWaterNonIndexedF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 782	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightWaterNonIndexedAlphaMaskF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 783	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightShinyNonIndexedF.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 784	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightShinyNonIndexedF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 785	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightShinyWaterNonIndexedF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 786	index_channels.push_back(-1);	 shaders.push_back( make_pair( "lighting/lightFullbrightShinyWaterNonIndexedF.glsl", mVertexShaderLevel[SHADER_LIGHTING] ) );
 787	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightF.glsl",					mVertexShaderLevel[SHADER_LIGHTING] ) );
 788	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightAlphaMaskF.glsl",					mVertexShaderLevel[SHADER_LIGHTING] ) );
 789	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 790	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightAlphaMaskF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 791	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightWaterF.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 792	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightWaterAlphaMaskF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 793	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightWaterF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 794	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightWaterAlphaMaskF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 795	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightShinyF.glsl",				mVertexShaderLevel[SHADER_LIGHTING] ) );
 796	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightShinyF.glsl",	mVertexShaderLevel[SHADER_LIGHTING] ) );
 797	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightShinyWaterF.glsl",			mVertexShaderLevel[SHADER_LIGHTING] ) );
 798	index_channels.push_back(ch);	 shaders.push_back( make_pair( "lighting/lightFullbrightShinyWaterF.glsl", mVertexShaderLevel[SHADER_LIGHTING] ) );
 799	
 800	for (U32 i = 0; i < shaders.size(); i++)
 801	{
 802		// Note usage of GL_FRAGMENT_SHADER_ARB
 803		if (loadShaderFile(shaders[i].first, shaders[i].second, GL_FRAGMENT_SHADER_ARB, index_channels[i]) == 0)
 804		{
 805			return FALSE;
 806		}
 807	}
 808
 809	return TRUE;
 810}
 811
 812BOOL LLViewerShaderMgr::loadShadersEnvironment()
 813{
 814	BOOL success = TRUE;
 815
 816	if (mVertexShaderLevel[SHADER_ENVIRONMENT] == 0)
 817	{
 818		gTerrainProgram.unload();
 819		return FALSE;
 820	}
 821
 822	if (success)
 823	{
 824		gTerrainProgram.mName = "Terrain Shader";
 825		gTerrainProgram.mFeatures.calculatesLighting = true;
 826		gTerrainProgram.mFeatures.calculatesAtmospherics = true;
 827		gTerrainProgram.mFeatures.hasAtmospherics = true;
 828		gTerrainProgram.mFeatures.mIndexedTextureChannels = 0;
 829		gTerrainProgram.mFeatures.disableTextureIndex = true;
 830		gTerrainProgram.mFeatures.hasGamma = true;
 831		gTerrainProgram.mShaderFiles.clear();
 832		gTerrainProgram.mShaderFiles.push_back(make_pair("environment/terrainV.glsl", GL_VERTEX_SHADER_ARB));
 833		gTerrainProgram.mShaderFiles.push_back(make_pair("environment/terrainF.glsl", GL_FRAGMENT_SHADER_ARB));
 834		gTerrainProgram.mShaderLevel = mVertexShaderLevel[SHADER_ENVIRONMENT];
 835		success = gTerrainProgram.createShader(NULL, &mTerrainUniforms);
 836	}
 837
 838	if (!success)
 839	{
 840		mVertexShaderLevel[SHADER_ENVIRONMENT] = 0;
 841		return FALSE;
 842	}
 843	
 844	LLWorld::getInstance()->updateWaterObjects();
 845	
 846	return TRUE;
 847}
 848
 849BOOL LLViewerShaderMgr::loadShadersWater()
 850{
 851	BOOL success = TRUE;
 852	BOOL terrainWaterSuccess = TRUE;
 853
 854	if (mVertexShaderLevel[SHADER_WATER] == 0)
 855	{
 856		gWaterProgram.unload();
 857		gUnderWaterProgram.unload();
 858		gTerrainWaterProgram.unload();
 859		return FALSE;
 860	}
 861
 862	if (success)
 863	{
 864		// load water shader
 865		gWaterProgram.mName = "Water Shader";
 866		gWaterProgram.mFeatures.calculatesAtmospherics = true;
 867		gWaterProgram.mFeatures.hasGamma = true;
 868		gWaterProgram.mFeatures.hasTransport = true;
 869		gWaterProgram.mShaderFiles.clear();
 870		gWaterProgram.mShaderFiles.push_back(make_pair("environment/waterV.glsl", GL_VERTEX_SHADER_ARB));
 871		gWaterProgram.mShaderFiles.push_back(make_pair("environment/waterF.glsl", GL_FRAGMENT_SHADER_ARB));
 872		gWaterProgram.mShaderLevel = mVertexShaderLevel[SHADER_WATER];
 873		success = gWaterProgram.createShader(NULL, &mWaterUniforms);
 874	}
 875
 876	if (success)
 877	{
 878		//load under water vertex shader
 879		gUnderWaterProgram.mName = "Underwater Shader";
 880		gUnderWaterProgram.mFeatures.calculatesAtmospherics = true;
 881		gUnderWaterProgram.mShaderFiles.clear();
 882		gUnderWaterProgram.mShaderFiles.push_back(make_pair("environment/waterV.glsl", GL_VERTEX_SHADER_ARB));
 883		gUnderWaterProgram.mShaderFiles.push_back(make_pair("environment/underWaterF.glsl", GL_FRAGMENT_SHADER_ARB));
 884		gUnderWaterProgram.mShaderLevel = mVertexShaderLevel[SHADER_WATER];
 885		gUnderWaterProgram.mShaderGroup = LLGLSLShader::SG_WATER;
 886		
 887		success = gUnderWaterProgram.createShader(NULL, &mWaterUniforms);
 888	}
 889
 890	if (success)
 891	{
 892		//load terrain water shader
 893		gTerrainWaterProgram.mName = "Terrain Water Shader";
 894		gTerrainWaterProgram.mFeatures.calculatesLighting = true;
 895		gTerrainWaterProgram.mFeatures.calculatesAtmospherics = true;
 896		gTerrainWaterProgram.mFeatures.hasAtmospherics = true;
 897		gTerrainWaterProgram.mFeatures.hasWaterFog = true;
 898		gTerrainWaterProgram.mFeatures.mIndexedTextureChannels = 0;
 899		gTerrainWaterProgram.mFeatures.disableTextureIndex = true;
 900		gTerrainWaterProgram.mShaderFiles.clear();
 901		gTerrainWaterProgram.mShaderFiles.push_back(make_pair("environment/terrainV.glsl", GL_VERTEX_SHADER_ARB));
 902		gTerrainWaterProgram.mShaderFiles.push_back(make_pair("environment/terrainWaterF.glsl", GL_FRAGMENT_SHADER_ARB));
 903		gTerrainWaterProgram.mShaderLevel = mVertexShaderLevel[SHADER_ENVIRONMENT];
 904		gTerrainWaterProgram.mShaderGroup = LLGLSLShader::SG_WATER;
 905		terrainWaterSuccess = gTerrainWaterProgram.createShader(NULL, &mTerrainUniforms);
 906	}	
 907
 908	/// Keep track of water shader levels
 909	if (gWaterProgram.mShaderLevel != mVertexShaderLevel[SHADER_WATER]
 910		|| gUnderWaterProgram.mShaderLevel != mVertexShaderLevel[SHADER_WATER])
 911	{
 912		mVertexShaderLevel[SHADER_WATER] = llmin(gWaterProgram.mShaderLevel, gUnderWaterProgram.mShaderLevel);
 913	}
 914
 915	if (!success)
 916	{
 917		mVertexShaderLevel[SHADER_WATER] = 0;
 918		return FALSE;
 919	}
 920
 921	// if we failed to load the terrain water shaders and we need them (using class2 water),
 922	// then drop down to class1 water.
 923	if (mVertexShaderLevel[SHADER_WATER] > 1 && !terrainWaterSuccess)
 924	{
 925		mVertexShaderLevel[SHADER_WATER]--;
 926		return loadShadersWater();
 927	}
 928	
 929	LLWorld::getInstance()->updateWaterObjects();
 930
 931	return TRUE;
 932}
 933
 934BOOL LLViewerShaderMgr::loadShadersEffects()
 935{
 936	BOOL success = TRUE;
 937
 938	if (mVertexShaderLevel[SHADER_EFFECT] == 0)
 939	{
 940		gGlowProgram.unload();
 941		gGlowExtractProgram.unload();
 942		gPostColorFilterProgram.unload();	
 943		gPostNightVisionProgram.unload();
 944		return FALSE;
 945	}
 946
 947	if (success)
 948	{
 949		gGlowProgram.mName = "Glow Shader (Post)";
 950		gGlowProgram.mShaderFiles.clear();
 951		gGlowProgram.mShaderFiles.push_back(make_pair("effects/glowV.glsl", GL_VERTEX_SHADER_ARB));
 952		gGlowProgram.mShaderFiles.push_back(make_pair("effects/glowF.glsl", GL_FRAGMENT_SHADER_ARB));
 953		gGlowProgram.mShaderLevel = mVertexShaderLevel[SHADER_EFFECT];
 954		success = gGlowProgram.createShader(NULL, &mGlowUniforms);
 955		if (!success)
 956		{
 957			LLPipeline::sRenderGlow = FALSE;
 958		}
 959	}
 960	
 961	if (success)
 962	{
 963		gGlowExtractProgram.mName = "Glow Extract Shader (Post)";
 964		gGlowExtractProgram.mShaderFiles.clear();
 965		gGlowExtractProgram.mShaderFiles.push_back(make_pair("effects/glowExtractV.glsl", GL_VERTEX_SHADER_ARB));
 966		gGlowExtractProgram.mShaderFiles.push_back(make_pair("effects/glowExtractF.glsl", GL_FRAGMENT_SHADER_ARB));
 967		gGlowExtractProgram.mShaderLevel = mVertexShaderLevel[SHADER_EFFECT];
 968		success = gGlowExtractProgram.createShader(NULL, &mGlowExtractUniforms);
 969		if (!success)
 970		{
 971			LLPipeline::sRenderGlow = FALSE;
 972		}
 973	}
 974	
 975	return success;
 976
 977}
 978
 979BOOL LLViewerShaderMgr::loadShadersDeferred()
 980{
 981	if (mVertexShaderLevel[SHADER_DEFERRED] == 0)
 982	{
 983		gDeferredTreeProgram.unload();
 984		gDeferredTreeShadowProgram.unload();
 985		gDeferredDiffuseProgram.unload();
 986		gDeferredDiffuseAlphaMaskProgram.unload();
 987		gDeferredNonIndexedDiffuseAlphaMaskProgram.unload();
 988		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.unload();
 989		gDeferredNonIndexedDiffuseProgram.unload();
 990		gDeferredSkinnedDiffuseProgram.unload();
 991		gDeferredSkinnedBumpProgram.unload();
 992		gDeferredSkinnedAlphaProgram.unload();
 993		gDeferredBumpProgram.unload();
 994		gDeferredImpostorProgram.unload();
 995		gDeferredTerrainProgram.unload();
 996		gDeferredLightProgram.unload();
 997		gDeferredMultiLightProgram.unload();
 998		gDeferredSpotLightProgram.unload();
 999		gDeferredMultiSpotLightProgram.unload();
1000		gDeferredSunProgram.unload();
1001		gDeferredBlurLightProgram.unload();
1002		gDeferredSoftenProgram.unload();
1003		gDeferredShadowProgram.unload();
1004		gDeferredShadowAlphaMaskProgram.unload();
1005		gDeferredAvatarShadowProgram.unload();
1006		gDeferredAttachmentShadowProgram.unload();
1007		gDeferredAvatarProgram.unload();
1008		gDeferredAvatarAlphaProgram.unload();
1009		gDeferredAlphaProgram.unload();
1010		gDeferredFullbrightProgram.unload();
1011		gDeferredEmissiveProgram.unload();
1012		gDeferredAvatarEyesProgram.unload();
1013		gDeferredPostProgram.unload();		
1014		gDeferredCoFProgram.unload();		
1015		gDeferredDoFCombineProgram.unload();
1016		gFXAAProgram.unload();
1017		gDeferredWaterProgram.unload();
1018		gDeferredWLSkyProgram.unload();
1019		gDeferredWLCloudProgram.unload();
1020		gDeferredStarProgram.unload();
1021		gNormalMapGenProgram.unload();
1022		return TRUE;
1023	}
1024
1025	BOOL success = TRUE;
1026
1027	if (success)
1028	{
1029		gDeferredDiffuseProgram.mName = "Deferred Diffuse Shader";
1030		gDeferredDiffuseProgram.mShaderFiles.clear();
1031		gDeferredDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
1032		gDeferredDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseIndexedF.glsl", GL_FRAGMENT_SHADER_ARB));
1033		gDeferredDiffuseProgram.mFeatures.mIndexedTextureChannels = LLGLSLShader::sIndexedTextureChannels;
1034		gDeferredDiffuseProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1035		success = gDeferredDiffuseProgram.createShader(NULL, NULL);
1036	}
1037
1038	if (success)
1039	{
1040		gDeferredDiffuseAlphaMaskProgram.mName = "Deferred Diffuse Alpha Mask Shader";
1041		gDeferredDiffuseAlphaMaskProgram.mShaderFiles.clear();
1042		gDeferredDiffuseAlphaMaskProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
1043		gDeferredDiffuseAlphaMaskProgram.mShaderFiles.push_back(make_pair("deferred/diffuseAlphaMaskIndexedF.glsl", GL_FRAGMENT_SHADER_ARB));
1044		gDeferredDiffuseAlphaMaskProgram.mFeatures.mIndexedTextureChannels = LLGLSLShader::sIndexedTextureChannels;
1045		gDeferredDiffuseAlphaMaskProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1046		success = gDeferredDiffuseAlphaMaskProgram.createShader(NULL, NULL);
1047	}
1048
1049	if (success)
1050	{
1051		gDeferredNonIndexedDiffuseAlphaMaskProgram.mName = "Deferred Diffuse Non-Indexed Alpha Mask Shader";
1052		gDeferredNonIndexedDiffuseAlphaMaskProgram.mShaderFiles.clear();
1053		gDeferredNonIndexedDiffuseAlphaMaskProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
1054		gDeferredNonIndexedDiffuseAlphaMaskProgram.mShaderFiles.push_back(make_pair("deferred/diffuseAlphaMaskF.glsl", GL_FRAGMENT_SHADER_ARB));
1055		gDeferredNonIndexedDiffuseAlphaMaskProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1056		success = gDeferredNonIndexedDiffuseAlphaMaskProgram.createShader(NULL, NULL);
1057	}
1058	
1059	if (success)
1060	{
1061		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.mName = "Deferred Diffuse Non-Indexed Alpha Mask Shader";
1062		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.mShaderFiles.clear();
1063		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.mShaderFiles.push_back(make_pair("deferred/diffuseNoColorV.glsl", GL_VERTEX_SHADER_ARB));
1064		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.mShaderFiles.push_back(make_pair("deferred/diffuseAlphaMaskNoColorF.glsl", GL_FRAGMENT_SHADER_ARB));
1065		gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1066		success = gDeferredNonIndexedDiffuseAlphaMaskNoColorProgram.createShader(NULL, NULL);
1067	}
1068
1069	if (success)
1070	{
1071		gDeferredNonIndexedDiffuseProgram.mName = "Non Indexed Deferred Diffuse Shader";
1072		gDeferredNonIndexedDiffuseProgram.mShaderFiles.clear();
1073		gDeferredNonIndexedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
1074		gDeferredNonIndexedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseF.glsl", GL_FRAGMENT_SHADER_ARB));
1075		gDeferredNonIndexedDiffuseProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1076		success = gDeferredNonIndexedDiffuseProgram.createShader(NULL, NULL);
1077	}
1078		
1079
1080	if (success)
1081	{
1082		gDeferredSkinnedDiffuseProgram.mName = "Deferred Skinned Diffuse Shader";
1083		gDeferredSkinnedDiffuseProgram.mFeatures.hasObjectSkinning = true;
1084		gDeferredSkinnedDiffuseProgram.mShaderFiles.clear();
1085		gDeferredSkinnedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseSkinnedV.glsl", GL_VERTEX_SHADER_ARB));
1086		gDeferredSkinnedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseF.glsl", GL_FRAGMENT_SHADER_ARB));
1087		gDeferredSkinnedDiffuseProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1088		success = gDeferredSkinnedDiffuseProgram.createShader(NULL, NULL);
1089	}
1090
1091	if (success)
1092	{
1093		gDeferredSkinnedBumpProgram.mName = "Deferred Skinned Bump Shader";
1094		gDeferredSkinnedBumpProgram.mFeatures.hasObjectSkinning = true;
1095		gDeferredSkinnedBumpProgram.mShaderFiles.clear();
1096		gDeferredSkinnedBumpProgram.mShaderFiles.push_back(make_pair("deferred/bumpSkinnedV.glsl", GL_VERTEX_SHADER_ARB));
1097		gDeferredSkinnedBumpProgram.mShaderFiles.push_back(make_pair("deferred/bumpF.glsl", GL_FRAGMENT_SHADER_ARB));
1098		gDeferredSkinnedBumpProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1099		success = gDeferredSkinnedBumpProgram.createShader(NULL, NULL);
1100	}
1101
1102	if (success)
1103	{
1104		gDeferredSkinnedAlphaProgram.mName = "Deferred Skinned Alpha Shader";
1105		gDeferredSkinnedAlphaProgram.mFeatures.hasObjectSkinning = true;
1106		gDeferredSkinnedAlphaProgram.mFeatures.calculatesAtmospherics = true;
1107		gDeferredSkinnedAlphaProgram.mFeatures.hasGamma = true;
1108		gDeferredSkinnedAlphaProgram.mFeatures.hasAtmospherics = true;
1109		gDeferredSkinnedAlphaProgram.mFeatures.calculatesLighting = true;
1110		gDeferredSkinnedAlphaProgram.mFeatures.hasLighting = true;
1111		gDeferredSkinnedAlphaProgram.mFeatures.isAlphaLighting = true;
1112		gDeferredSkinnedAlphaProgram.mFeatures.disableTextureIndex = true;
1113		gDeferredSkinnedAlphaProgram.mShaderFiles.clear();
1114		gDeferredSkinnedAlphaProgram.mShaderFiles.push_back(make_pair("deferred/alphaSkinnedV.glsl", GL_VERTEX_SHADER_ARB));
1115		gDeferredSkinnedAlphaProgram.mShaderFiles.push_back(make_pair("deferred/alphaNonIndexedF.glsl", GL_FRAGMENT_SHADER_ARB));
1116		gDeferredSkinnedAlphaProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1117		success = gDeferredSkinnedAlphaProgram.createShader(NULL, NULL);
1118	}
1119
1120	if (success)
1121	{
1122		gDeferredBumpProgram.mName = "Deferred Bump Shader";
1123		gDeferredBumpProgram.mShaderFiles.clear();
1124		gDeferredBumpProgram.mShaderFiles.push_back(make_pair("deferred/bumpV.glsl", GL_VERTEX_SHADER_ARB));
1125		gDeferredBumpProgram.mShaderFiles.push_back(make_pair("deferred/bumpF.glsl", GL_FRAGMENT_SHADER_ARB));
1126		gDeferredBumpProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1127		success = gDeferredBumpProgram.createShader(NULL, NULL);
1128	}
1129
1130	if (success)
1131	{
1132		gDeferredTreeProgram.mName = "Deferred Tree Shader";
1133		gDeferredTreeProgram.mShaderFiles.clear();
1134		gDeferredTreeProgram.mShaderFiles.push_back(make_pair("deferred/treeV.glsl", GL_VERTEX_SHADER_ARB));
1135		gDeferredTreeProgram.mShaderFiles.push_back(make_pair("deferred/treeF.glsl", GL_FRAGMENT_SHADER_ARB));
1136		gDeferredTreeProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1137		success = gDeferredTreeProgram.createShader(NULL, NULL);
1138	}
1139
1140	if (success)
1141	{
1142		gDeferredTreeShadowProgram.mName = "Deferred Tree Shadow Shader";
1143		gDeferredTreeShadowProgram.mShaderFiles.clear();
1144		gDeferredTreeShadowProgram.mShaderFiles.push_back(make_pair("deferred/treeShadowV.glsl", GL_VERTEX_SHADER_ARB));
1145		gDeferredTreeShadowProgram.mShaderFiles.push_back(make_pair("deferred/treeShadowF.glsl", GL_FRAGMENT_SHADER_ARB));
1146		gDeferredTreeShadowProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1147		success = gDeferredTreeShadowProgram.createShader(NULL, NULL);
1148	}
1149
1150	if (success)
1151	{
1152		gDeferredImpostorProgram.mName = "Deferred Impostor Shader";
1153		gDeferredImpostorProgram.mShaderFiles.clear();
1154		gDeferredImpostorProgram.mShaderFiles.push_back(make_pair("deferred/impostorV.glsl", GL_VERTEX_SHADER_ARB));
1155		gDeferredImpostorProgram.mShaderFiles.push_back(make_pair("deferred/impostorF.glsl", GL_FRAGMENT_SHADER_ARB));
1156		gDeferredImpostorProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1157		success = gDeferredImpostorProgram.createShader(NULL, NULL);
1158	}
1159
1160	if (success)
1161	{		
1162		gDeferredLightProgram.mName = "Deferred Light Shader";
1163		gDeferredLightProgram.mShaderFiles.clear();
1164		gDeferredLightProgram.mShaderFiles.push_back(make_pair("deferred/pointLightV.glsl", GL_VERTEX_SHADER_ARB));
1165		gDeferredLightProgram.mShaderFiles.push_back(make_pair("deferred/pointLightF.glsl", GL_FRAGMENT_SHADER_ARB));
1166		gDeferredLightProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1167		success = gDeferredLightProgram.createShader(NULL, NULL);
1168	}
1169
1170	if (success)
1171	{
1172		gDeferredMultiLightProgram.mName = "Deferred MultiLight Shader";
1173		gDeferredMultiLightProgram.mShaderFiles.clear();
1174		gDeferredMultiLightProgram.mShaderFiles.push_back(make_pair("deferred/multiPointLightV.glsl", GL_VERTEX_SHADER_ARB));
1175		gDeferredMultiLightProgram.mShaderFiles.push_back(make_pair("deferred/multiPointLightF.glsl", GL_FRAGMENT_SHADER_ARB));
1176		gDeferredMultiLightProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1177		success = gDeferredMultiLightProgram.createShader(NULL, NULL);
1178	}
1179
1180	if (success)
1181	{
1182		gDeferredSpotLightProgram.mName = "Deferred SpotLight Shader";
1183		gDeferredSpotLightProgram.mShaderFiles.clear();
1184		gDeferredSpotLightProgram.mShaderFiles.push_back(make_pair("deferred/pointLightV.glsl", GL_VERTEX_SHADER_ARB));
1185		gDeferredSpotLightProgram.mShaderFiles.push_back(make_pair("deferred/multiSpotLightF.glsl", GL_FRAGMENT_SHADER_ARB));
1186		gDeferredSpotLightProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1187		success = gDeferredSpotLightProgram.createShader(NULL, NULL);
1188	}
1189
1190	if (success)
1191	{
1192		gDeferredMultiSpotLightProgram.mName = "Deferred MultiSpotLight Shader";
1193		gDeferredMultiSpotLightProgram.mShaderFiles.clear();
1194		gDeferredMultiSpotLightProgram.mShaderFiles.push_back(make_pair("deferred/pointLightV.glsl", GL_VERTEX_SHADER_ARB));
1195		gDeferredMultiSpotLightProgram.mShaderFiles.push_back(make_pair("deferred/multiSpotLightF.glsl", GL_FRAGMENT_SHADER_ARB));
1196		gDeferredMultiSpotLightProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1197		success = gDeferredMultiSpotLightProgram.createShader(NULL, NULL);
1198	}
1199
1200	if (success)
1201	{
1202		std::string fragment;
1203
1204		if (gSavedSettings.getBOOL("RenderDeferredSSAO"))
1205		{
1206			fragment = "deferred/sunLightSSAOF.glsl";
1207		}
1208		else
1209		{
1210			fragment = "deferred/sunLightF.glsl";
1211		}
1212
1213		gDeferredSunProgram.mName = "Deferred Sun Shader";
1214		gDeferredSunProgram.mShaderFiles.clear();
1215		gDeferredSunProgram.mShaderFiles.push_back(make_pair("deferred/sunLightV.glsl", GL_VERTEX_SHADER_ARB));
1216		gDeferredSunProgram.mShaderFiles.push_back(make_pair(fragment, GL_FRAGMENT_SHADER_ARB));
1217		gDeferredSunProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1218		success = gDeferredSunProgram.createShader(NULL, NULL);
1219	}
1220
1221	if (success)
1222	{
1223		gDeferredBlurLightProgram.mName = "Deferred Blur Light Shader";
1224		gDeferredBlurLightProgram.mShaderFiles.clear();
1225		gDeferredBlurLightProgram.mShaderFiles.push_back(make_pair("deferred/blurLightV.glsl", GL_VERTEX_SHADER_ARB));
1226		gDeferredBlurLightProgram.mShaderFiles.push_back(make_pair("deferred/blurLightF.glsl", GL_FRAGMENT_SHADER_ARB));
1227		gDeferredBlurLightProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1228		success = gDeferredBlurLightProgram.createShader(NULL, NULL);
1229	}
1230
1231	if (success)
1232	{
1233		gDeferredAlphaProgram.mName = "Deferred Alpha Shader";
1234		gDeferredAlphaProgram.mFeatures.calculatesLighting = true;
1235		gDeferredAlphaProgram.mFeatures.calculatesAtmospherics = true;
1236		gDeferredAlphaProgram.mFeatures.hasGamma = true;
1237		gDeferredAlphaProgram.mFeatures.hasAtmospherics = true;
1238		gDeferredAlphaProgram.mFeatures.hasLighting = true;
1239		gDeferredAlphaProgram.mFeatures.isAlphaLighting = true;
1240		gDeferredAlphaProgram.mFeatures.disableTextureIndex = true; //hack to disable auto-setup of texture channels
1241		if (mVertexShaderLevel[SHADER_DEFERRED] < 1)
1242		{
1243			gDeferredAlphaProgram.mFeatures.mIndexedTextureChannels = LLGLSLShader::sIndexedTextureChannels;
1244		}
1245		else
1246		{ //shave off some texture units for shadow maps
1247			gDeferredAlphaProgram.mFeatures.mIndexedTextureChannels = llmax(LLGLSLShader::sIndexedTextureChannels - 6, 1);
1248		}
1249			
1250		gDeferredAlphaProgram.mShaderFiles.clear();
1251		gDeferredAlphaProgram.mShaderFiles.push_back(make_pair("deferred/alphaV.glsl", GL_VERTEX_SHADER_ARB));
1252		gDeferredAlphaProgram.mShaderFiles.push_back(make_pair("deferred/alphaF.glsl", GL_FRAGMENT_SHADER_ARB));
1253		gDeferredAlphaProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
1254		success = gDef

Large files files are truncated, but you can click here to view the full file