diff --git a/indra/llrender/llcubemap.cpp b/indra/llrender/llcubemap.cpp
index d425b8fa2d66326350b4179e758f09b318c85837..5947bca670094cec0f160c7fb767c60fbdbe268f 100644
--- a/indra/llrender/llcubemap.cpp
+++ b/indra/llrender/llcubemap.cpp
@@ -79,9 +79,11 @@ void LLCubeMap::initGL()
 			for (int i = 0; i < 6; i++)
 			{
 				mImages[i] = new LLImageGL(RESOLUTION, RESOLUTION, 4, FALSE);
+            #if USE_SRGB_DECODE
                 if (mIssRGB) {
                     mImages[i]->setExplicitFormat(GL_SRGB8_ALPHA8, GL_RGBA);
                 }
+            #endif
 				mImages[i]->setTarget(mTargets[i], LLTexUnit::TT_CUBE_MAP);
 				mRawImages[i] = new LLImageRaw(RESOLUTION, RESOLUTION, 4);
 				mImages[i]->createGLTexture(0, mRawImages[i], texname);
diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp
index babf649ece81ac7e36a412c615c31ff8dfff2c63..902c08b6ba8b98face63f48601caba7e162ff2fb 100644
--- a/indra/llrender/llglslshader.cpp
+++ b/indra/llrender/llglslshader.cpp
@@ -937,15 +937,15 @@ void LLGLSLShader::bindNoShader(void)
     }
 }
 
-S32 LLGLSLShader::bindTexture(const std::string &uniform, LLTexture *texture, LLTexUnit::eTextureType mode)
+S32 LLGLSLShader::bindTexture(const std::string &uniform, LLTexture *texture, LLTexUnit::eTextureType mode, LLTexUnit::eTextureColorSpace colorspace)
 {
     S32 channel = 0;
     channel = getUniformLocation(uniform);
     
-    return bindTexture(channel, texture, mode);
+    return bindTexture(channel, texture, mode, colorspace);
 }
 
-S32 LLGLSLShader::bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode)
+S32 LLGLSLShader::bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode, LLTexUnit::eTextureColorSpace colorspace)
 {
     if (uniform < 0 || uniform >= (S32)mTexture.size())
     {
@@ -958,6 +958,7 @@ S32 LLGLSLShader::bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextu
     if (uniform > -1)
     {
         gGL.getTexUnit(uniform)->bind(texture, mode);
+        gGL.getTexUnit(uniform)->setTextureColorSpace(colorspace);
     }
     
     return uniform;
diff --git a/indra/llrender/llglslshader.h b/indra/llrender/llglslshader.h
index 38dc980a97429ad834da5d7283a48fafa0fe52f6..7cf6d3c941f294da9fadfbd014f8f3e04f890ef8 100644
--- a/indra/llrender/llglslshader.h
+++ b/indra/llrender/llglslshader.h
@@ -159,8 +159,8 @@ class LLGLSLShader
 	
 	// bindTexture returns the texture unit we've bound the texture to.
 	// You can reuse the return value to unbind a texture when required.
-	S32 bindTexture(const std::string& uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
-	S32 bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
+	S32 bindTexture(const std::string& uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE, LLTexUnit::eTextureColorSpace space = LLTexUnit::TCS_LINEAR);
+	S32 bindTexture(S32 uniform, LLTexture *texture, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE, LLTexUnit::eTextureColorSpace space = LLTexUnit::TCS_LINEAR);
 	S32 unbindTexture(const std::string& uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
 	S32 unbindTexture(S32 uniform, LLTexUnit::eTextureType mode = LLTexUnit::TT_TEXTURE);
 	
diff --git a/indra/llrender/llimagegl.cpp b/indra/llrender/llimagegl.cpp
index 2f1f702d2c5979af8ad699dcf273e93322cd51e1..790ba25112be746599e5864003c2068716c69a0e 100644
--- a/indra/llrender/llimagegl.cpp
+++ b/indra/llrender/llimagegl.cpp
@@ -1385,22 +1385,28 @@ BOOL LLImageGL::createGLTexture(S32 discard_level, const LLImageRaw* imageraw, S
             mFormatType = GL_UNSIGNED_BYTE;
             break;
         case 3:
+        #if USE_SRGB_DECODE
             if (gGLManager.mHasTexturesRGBDecode)
             {
                 mFormatInternal = GL_SRGB8;
             }
-            else {
+            else
+        #endif
+            {
                 mFormatInternal = GL_RGB8;
             }
             mFormatPrimary = GL_RGB;
             mFormatType = GL_UNSIGNED_BYTE;
             break;
         case 4:
+        #if USE_SRGB_DECODE
             if (gGLManager.mHasTexturesRGBDecode)
             {
                 mFormatInternal = GL_SRGB8_ALPHA8;
             }
-            else {
+            else
+        #endif
+            {
                 mFormatInternal = GL_RGBA8;
             }
             mFormatPrimary = GL_RGBA;
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index 5dc61d0e8f997bfed1e707b2252b23c262b9f33e..f3771f3bf25b55caa02ed735cf4e59839b33d45a 100644
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -193,6 +193,7 @@ void LLTexUnit::enable(eTextureType type)
 			stop_glerror();
 		}
 		mCurrTexType = type;
+
 		gGL.flush();
 		if (!LLGLSLShader::sNoFixedFunction && 
 			type != LLTexUnit::TT_MULTISAMPLE_TEXTURE &&
@@ -850,6 +851,8 @@ void LLTexUnit::debugTextureUnit(void)
 
 void LLTexUnit::setTextureColorSpace(eTextureColorSpace space) {
     mTexColorSpace = space;
+
+#if USE_SRGB_DECODE
     if (gGLManager.mHasTexturesRGBDecode) {
 
         if (space == TCS_SRGB) {
@@ -863,6 +866,9 @@ void LLTexUnit::setTextureColorSpace(eTextureColorSpace space) {
             assert_glerror();
         }
     }
+#endif
+    glTexParameteri(sGLTextureType[mCurrTexType], GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
+
 }
 
 LLLightState::LLLightState(S32 index)
diff --git a/indra/llrender/llrendertarget.cpp b/indra/llrender/llrendertarget.cpp
index cd484b4fe901cdc7053a7bacddf1240e26fdf12e..9fb4f7f2b07d18aed7bf79ea6bf7d69cad19a89d 100644
--- a/indra/llrender/llrendertarget.cpp
+++ b/indra/llrender/llrendertarget.cpp
@@ -502,9 +502,26 @@ U32 LLRenderTarget::getNumTextures() const
 }
 
 
-void LLRenderTarget::bindTexture(U32 index, S32 channel)
+void LLRenderTarget::bindTexture(U32 index, S32 channel, LLTexUnit::eTextureFilterOptions filter_options)
 {
 	gGL.getTexUnit(channel)->bindManual(mUsage, getTexture(index));
+
+    bool isSRGB = false;
+    llassert(mInternalFormat.size() > index);
+    switch (mInternalFormat[index])
+    {
+        case GL_SRGB_ALPHA:
+        case GL_SRGB:
+        case GL_SRGB8_ALPHA8:
+            isSRGB = true;
+        break;
+
+        default:
+        break;
+    }
+
+    gGL.getTexUnit(channel)->setTextureFilteringOption(filter_options);
+    gGL.getTexUnit(channel)->setTextureColorSpace(isSRGB ? LLTexUnit::TCS_SRGB : LLTexUnit::TCS_LINEAR);
 }
 
 void LLRenderTarget::flush(bool fetch_depth)
diff --git a/indra/llrender/llrendertarget.h b/indra/llrender/llrendertarget.h
index 6dc84d978da0b9869e12b2d3987a1017c37e4dff..6c07ac5b1c7c8645af7e04c45fc731bdc97f3dc7 100644
--- a/indra/llrender/llrendertarget.h
+++ b/indra/llrender/llrendertarget.h
@@ -120,7 +120,7 @@ class LLRenderTarget
 	U32 getDepth(void) const { return mDepth; }
 	bool hasStencil() const { return mStencil; }
 
-	void bindTexture(U32 index, S32 channel);
+	void bindTexture(U32 index, S32 channel, LLTexUnit::eTextureFilterOptions filter_options = LLTexUnit::TFO_BILINEAR);
 
 	//flush rendering operations
 	//must be called when rendering is complete
diff --git a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
index ae579d7f85ed35a036de18372d28a8b347354224..ecb1d599deed695c8a37b3251e4dc2b02236fded 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/alphaF.glsl
@@ -121,7 +121,7 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 diffuse, vec3 v, vec3 n, vec
         fa += 1.0f;
         float dist_atten = (fa > 0) ? clamp(1.0-(dist-1.0*(1.0-fa))/fa, 0.0, 1.0) : 1.0f;
         dist_atten *= dist_atten;
-        dist_atten *= 2.0f;
+        dist_atten *= 2.0;
 
         // spotlight coefficient.
         float spot = max(dot(-ln, lv), is_pointlight);
@@ -140,9 +140,7 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 diffuse, vec3 v, vec3 n, vec
         amb_da *= dist_atten;
         amb_da = min(amb_da, 1.0f - lit);
 
-#ifndef NO_AMBIANCE
         col.rgb += amb_da * 0.5 * light_col * diffuse;
-#endif
 
         // no spec for alpha shader...
     }
@@ -164,22 +162,26 @@ void main()
     shadow = sampleDirectionalShadow(pos.xyz, norm.xyz, frag);
 #endif
 
+#ifdef USE_DIFFUSE_TEX
+    vec4 diffuse_linear = texture2D(diffuseMap,vary_texcoord0.xy);
+#endif
+
 #ifdef USE_INDEXED_TEX
-    vec4 diff = diffuseLookup(vary_texcoord0.xy);
-#else
-    vec4 diff = texture2D(diffuseMap,vary_texcoord0.xy);
+    vec4 diffuse_linear = diffuseLookup(vary_texcoord0.xy);
 #endif
 
+    vec4 diffuse_srgb = vec4(linear_to_srgb(diffuse_linear.rgb), diffuse_linear.a);
+
 #ifdef FOR_IMPOSTOR
     vec4 color;
-    color.rgb = diff.rgb;
+    color.rgb = diffuse_srgb.rgb;
     color.a = 1.0;
 
 #ifdef USE_VERTEX_COLOR
-    float final_alpha = diff.a * vertex_color.a;
-    diff.rgb *= vertex_color.rgb;
+    float final_alpha = diffuse_srgb.a * vertex_color.a;
+    diffuse_srgb.rgb *= vertex_color.rgb;
 #else
-    float final_alpha = diff.a;
+    float final_alpha = diffuse_srgb.a;
 #endif
     
     // Insure we don't pollute depth with invis pixels in impostor rendering
@@ -191,16 +193,12 @@ void main()
 #else
     
 #ifdef USE_VERTEX_COLOR
-    float final_alpha = diff.a * vertex_color.a;
-    diff.rgb *= vertex_color.rgb;
+    float final_alpha = diffuse_linear.a * vertex_color.a;
+    diffuse_srgb.rgb *= vertex_color.rgb;
 #else
-    float final_alpha = diff.a;
+    float final_alpha = diffuse_linear.a;
 #endif
 
-    vec3 gamma_diff = diff.rgb;
-
-    diff.rgb = srgb_to_linear(diff.rgb);
-
     vec3 sunlit;
     vec3 amblit;
     vec3 additive;
@@ -217,7 +215,7 @@ void main()
     float final_da = da;
           final_da = clamp(final_da, 0.0f, 1.0f);
 
-    vec4 color = vec4(0,0,0,0);
+    vec4 color = vec4(0.0);
 
     color.rgb = amblit;
     color.a   = final_alpha;
@@ -225,6 +223,7 @@ void main()
     float ambient = da;
     ambient *= 0.5;
     ambient *= ambient;
+    ambient = max(getAmbientClamp(), ambient); // keeps shadows dark
     ambient = 1.0 - ambient;
 
     vec3 sun_contrib = min(final_da, shadow) * sunlit;
@@ -237,14 +236,12 @@ vec3 post_ambient = color.rgb;
 
 vec3 post_sunlight = color.rgb;
 
-    color.rgb *= diff.rgb;
+    color.rgb *= diffuse_linear.rgb;
 
 vec3 post_diffuse = color.rgb;
 
-    color.rgb = mix(diff.rgb, color.rgb, final_alpha);
-
-    //color.rgb = srgb_to_linear(color.rgb);
-
+    //color.rgb = mix(diffuse_srgb.rgb, color.rgb, final_alpha);
+    
     color.rgb = atmosFragLighting(color.rgb, additive, atten);
     color.rgb = scaleSoftClipFrag(color.rgb);
 
@@ -252,7 +249,10 @@ vec3 post_atmo = color.rgb;
 
     vec4 light = vec4(0,0,0,0);
 
-   #define LIGHT_LOOP(i) light.rgb += calcPointLightOrSpotLight(light_diffuse[i].rgb, diff.rgb, pos.xyz, norm, light_position[i], light_direction[i].xyz, light_attenuation[i].x, light_attenuation[i].y, light_attenuation[i].z, light_attenuation[i].w * 0.5);
+    // to linear!
+    color.rgb = srgb_to_linear(color.rgb);
+
+   #define LIGHT_LOOP(i) light.rgb += calcPointLightOrSpotLight(light_diffuse[i].rgb, diffuse_linear.rgb, pos.xyz, norm, light_position[i], light_direction[i].xyz, light_attenuation[i].x, light_attenuation[i].y, light_attenuation[i].z, light_attenuation[i].w * 0.5);
 
     LIGHT_LOOP(1)
     LIGHT_LOOP(2)
@@ -262,15 +262,29 @@ vec3 post_atmo = color.rgb;
     LIGHT_LOOP(6)
     LIGHT_LOOP(7)
 
+    // sum local light contrib in linear colorspace
+    color.rgb += light.rgb;
+
+    // back to sRGB as we're going directly to the final RT post-deferred gamma correction
     color.rgb = linear_to_srgb(color.rgb);
 
-    color.rgb += light.rgb;
-#endif
+
+//color.rgb = amblit;
+//color.rgb = vec3(ambient);
+//color.rgb = sunlit;
+//color.rgb = vec3(final_da);
+//color.rgb = post_ambient;
+//color.rgb = post_sunlight;
+//color.rgb = sun_contrib;
+//color.rgb = diffuse_srgb.rgb;
+//color.rgb = post_diffuse;
+//color.rgb = post_atmo;
 
 #ifdef WATER_FOG
     color = applyWaterFogView(pos.xyz, color);
-#endif
+#endif // WATER_FOG
 
+#endif
 
     frag_color = color;
 }
diff --git a/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl b/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
index 9535cd6870508a09fb45f230ff78050cd651d88d..c5eaacbd0bb42698d6296d8b34459a8aaf954338 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl
@@ -86,14 +86,14 @@ void main()
     vec2 disturbance2 = vec2(cloudNoise((uv1 + uv3) / 4.0f).x, cloudNoise((uv4 + uv2) / 8.0f).x) * cloud_variance * (1.0f - cloud_scale * 0.25f);
 
     // Offset texture coords
-    uv1 += cloud_pos_density1.xy + (disturbance * 0.02);    //large texture, visible density
+    uv1 += cloud_pos_density1.xy;// + (disturbance * 0.02);    //large texture, visible density
     uv2 += cloud_pos_density1.xy;   //large texture, self shadow
     uv3 += cloud_pos_density2.xy;   //small texture, visible density
     uv4 += cloud_pos_density2.xy;   //small texture, self shadow
 
     float density_variance = min(1.0, (disturbance.x* 2.0 + disturbance.y* 2.0 + disturbance2.x + disturbance2.y));
 
-    cloudDensity *= 1.0 - (density_variance * density_variance);
+    //cloudDensity *= 1.0 - (density_variance * density_variance);
 
     // Compute alpha1, the main cloud opacity
 
@@ -104,8 +104,8 @@ void main()
     alpha1 = 1. - alpha1 * alpha1;
     alpha1 = 1. - alpha1 * alpha1;  
 
-    alpha1 *= altitude_blend_factor;
-    alpha1 = clamp(alpha1, 0.0, 1.0);
+    //alpha1 *= altitude_blend_factor;
+    //alpha1 = clamp(alpha1, 0.0, 1.0);
 
     // Compute alpha2, for self shadowing effect
     // (1 - alpha2) will later be used as percentage of incoming sunlight
@@ -122,6 +122,10 @@ void main()
     color.rgb= max(vec3(0), color.rgb);
     color.rgb = linear_to_srgb(color.rgb);
 
+//alpha1 = 1.0;
+//color.rgb = cloudColorAmbient.rgb;
+
+
     /// Gamma correct for WL (soft clip effect).
     frag_data[0] = vec4(scaleSoftClip(color.rgb), alpha1);
     frag_data[1] = vec4(0.0,0.0,0.0,0.0);
diff --git a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
index 61fd43f1eebf265f2845ed96063b6ec539ba2f91..10425bb91f757fca8bd80658c4ad14b395c255ce 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
@@ -78,7 +78,7 @@ void main()
 	// Get relative position
 	vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 
-        altitude_blend_factor = (P.y > -4096.0) ? 1.0 : 1.0 - clamp(abs(P.y) / max_y, 0.0, 1.0);
+    altitude_blend_factor = (P.y > -4096.0) ? 1.0 : 1.0 - clamp(abs(P.y) / max_y, 0.0, 1.0);
 
 	// Set altitude
 	if (P.y > 0.)
@@ -86,7 +86,6 @@ void main()
 		P *= (max_y / P.y);
 	}
 	else
-        if (P.y <= 0.0)
 	{
 		P *= (-32000. / P.y);
 	}
@@ -100,14 +99,14 @@ void main()
 	vec4 temp2 = vec4(0.);
 	vec4 blue_weight;
 	vec4 haze_weight;
-	vec4 sunlight = (sun_up_factor == 1) ? sunlight_color : moonlight_color;
+	//vec4 sunlight = (sun_up_factor == 1) ? sunlight_color : moonlight_color;
+    vec4 sunlight = sunlight_color;
 	vec4 light_atten;
 
-    float dens_mul = density_multiplier;
 
 	// Sunlight attenuation effect (hue and brightness) due to atmosphere
 	// this is used later for sunlight modulation at various altitudes
-	light_atten = (blue_density + vec4(haze_density * 0.25)) * (dens_mul * max_y);
+	light_atten = (blue_density + vec4(haze_density * 0.25)) * (density_multiplier * max_y);
 
 	// Calculate relative weights
 	temp1 = blue_density + haze_density;
@@ -120,13 +119,14 @@ void main()
 	sunlight *= exp( - light_atten * temp2.y);
 
 	// Distance
-	temp2.z = Plen * dens_mul;
+	temp2.z = Plen * density_multiplier;
 
 	// Transparency (-> temp1)
 	// ATI Bugfix -- can't store temp1*temp2.z in a variable because the ati
 	// compiler gets confused.
 	temp1 = exp(-temp1 * temp2.z);
 
+
 	// Compute haze glow
 	temp2.x = dot(Pn, lightnorm.xyz);
 	temp2.x = 1. - temp2.x;
@@ -138,7 +138,7 @@ void main()
 	temp2.x = pow(temp2.x, glow.z);
 		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
 
-        temp2.x *= sun_moon_glow_factor;
+    //temp2.x *= sun_moon_glow_factor;
 
 	// Add "minimum anti-solar illumination"
 	temp2.x += .25;
@@ -179,7 +179,7 @@ void main()
 	// Texture coords
 	vary_texcoord0 = texcoord0;
 	vary_texcoord0.xy -= 0.5;
-	vary_texcoord0.xy /= max(0.001, cloud_scale);
+	vary_texcoord0.xy /= cloud_scale;
 	vary_texcoord0.xy += 0.5;
 
 	vary_texcoord1 = vary_texcoord0;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/diffuseIndexedF.glsl b/indra/newview/app_settings/shaders/class1/deferred/diffuseIndexedF.glsl
index 828c325c9df2f2b9448ee770f01503bed37877ed..ccd1df84f9559313a33be66c67ff1d086b96085a 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/diffuseIndexedF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/diffuseIndexedF.glsl
@@ -36,11 +36,12 @@ VARYING vec4 vertex_color;
 VARYING vec2 vary_texcoord0;
 
 vec2 encode_normal(vec3 n);
+vec3 linear_to_srgb(vec3 c);
 
 void main() 
 {
 	vec3 col = vertex_color.rgb * diffuseLookup(vary_texcoord0.xy).rgb;
-	
+
 	vec3 spec;
 	spec.rgb = vec3(vertex_color.a);
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
index f7af3647a1e0a5c0ae25ae13dbbdfaf1fa568574..f640dba59b862c783196dbc97b2dc878f72d7e1a 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
@@ -120,6 +120,7 @@ vec3 calcPointLightOrSpotLight(vec3 light_col, vec3 npos, vec3 diffuse, vec4 spe
         fa += 1.0f;
         float dist_atten = ( fa > 0) ? clamp(1.0-(dist-1.0*(1.0-fa))/fa, 0.0, 1.0) : 1.0f;
         dist_atten *= dist_atten;
+        dist_atten *= 2.0;
 
         if (dist_atten <= 0)
         {
@@ -226,19 +227,17 @@ void main()
 {
     vec2 pos_screen = vary_texcoord0.xy;
 
-    vec4 diffcol = texture2D(diffuseMap, vary_texcoord0.xy);
-    diffcol.rgb *= vertex_color.rgb;
+    vec4 diffuse_linear = texture2D(diffuseMap, vary_texcoord0.xy);
+    vec4 diffuse_srgb   = vec4(linear_to_srgb(diffuse_linear.rgb), diffuse_linear.a);
 
 #if (DIFFUSE_ALPHA_MODE == DIFFUSE_ALPHA_MODE_MASK)
-    if (diffcol.a < minimum_alpha)
+    if (diffuse_linear.a < minimum_alpha)
     {
         discard;
     }
 #endif
 
-#if (DIFFUSE_ALPHA_MODE == DIFFUSE_ALPHA_MODE_BLEND)
-    vec3 gamma_diff = diffcol.rgb;
-#endif
+    diffuse_linear.rgb *= vertex_color.rgb;
 
 #ifdef HAS_SPECULAR_MAP
     vec4 spec = texture2D(specularMap, vary_texcoord2.xy);
@@ -247,17 +246,19 @@ void main()
     vec4 spec = vec4(specular_color.rgb, 1.0);
 #endif
 
-#ifdef HAS_NORMAL_MAP
-    vec4 norm = texture2D(bumpMap, vary_texcoord1.xy);
+    vec4 norm = vec4(0,0,0,1.0);
+    vec3 tnorm;
 
+#ifdef HAS_NORMAL_MAP
+    norm = texture2D(bumpMap, vary_texcoord1.xy);
     norm.xyz = norm.xyz * 2 - 1;
 
-    vec3 tnorm = vec3(dot(norm.xyz,vary_mat0),
-              dot(norm.xyz,vary_mat1),
-              dot(norm.xyz,vary_mat2));
+    // tangent space norm
+    tnorm = vec3(dot(norm.xyz,vary_mat0),
+                 dot(norm.xyz,vary_mat1),
+                 dot(norm.xyz,vary_mat2));
 #else
-    vec4 norm = vec4(0,0,0,1.0);
-    vec3 tnorm = vary_normal;
+    tnorm = vary_normal;
 #endif
 
     norm.xyz = tnorm;
@@ -265,7 +266,7 @@ void main()
 
     vec2 abnormal   = encode_normal(norm.xyz);
 
-    vec4 final_color = diffcol;
+    vec4 final_color = diffuse_linear;
     
 #if (DIFFUSE_ALPHA_MODE != DIFFUSE_ALPHA_MODE_EMISSIVE)
     final_color.a = emissive_brightness;
@@ -274,14 +275,15 @@ void main()
 #endif
 
     vec4 final_specular = spec;
+    final_specular.a = specular_color.a;
 #ifdef HAS_SPECULAR_MAP
-    vec4 final_normal = vec4(encode_normal(normalize(tnorm)), env_intensity * spec.a, 0.0);
-    final_specular.a = specular_color.a * norm.a;
-#else
+    final_specular.a *= norm.a;
+#endif
+
     vec4 final_normal = vec4(encode_normal(normalize(tnorm)), env_intensity, 0.0);
-    final_specular.a = specular_color.a;
+#ifdef HAS_SPECULAR_MAP
+    final_normal.z *= spec.a;
 #endif
-    
 
 #if (DIFFUSE_ALPHA_MODE == DIFFUSE_ALPHA_MODE_BLEND)
         //forward rendering, output just lit RGBA
@@ -294,13 +296,10 @@ void main()
 #endif
     
     spec = final_specular;
-    vec4 diffuse = final_color;
-
-    diffuse.rgb = srgb_to_linear(diffuse.rgb);
 
     float envIntensity = final_normal.z;
 
-    vec3 col = vec3(0.0f,0.0f,0.0f);
+    vec3 color = vec3(0.0);
 
     float bloom = 0.0;
     vec3 sunlit;
@@ -314,7 +313,7 @@ void main()
 
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir : moon_dir;
 
-    float da = dot(norm.xyz, light_dir.xyz);
+    float da = dot(normalize(norm.xyz), normalize(light_dir.xyz));
           da = clamp(da, -1.0, 1.0);
 
     float final_da = da;
@@ -323,22 +322,23 @@ void main()
     float ambient = da;
     ambient *= 0.5;
     ambient *= ambient;
+    ambient = max(getAmbientClamp(), ambient);
     ambient = 1.0 - ambient;
 
     vec3 sun_contrib = min(final_da, shadow) * sunlit;
    
-    col.rgb = amblit;
-    col.rgb *= ambient;
+    color.rgb = amblit;
+    color.rgb *= ambient;
 
-vec3 post_ambient = col.rgb;
+vec3 post_ambient = color.rgb;
 
-    col.rgb += sun_contrib;
+    color.rgb += sun_contrib;
 
-vec3 post_sunlight = col.rgb;
+vec3 post_sunlight = color.rgb;
 
-    col.rgb *= diffuse.rgb;
+    color.rgb *= diffuse_linear.rgb;
  
-vec3 post_diffuse = col.rgb;
+vec3 post_diffuse = color.rgb;
 
     float glare = 0.0;
 
@@ -363,44 +363,44 @@ vec3 post_diffuse = col.rgb;
             vec3 speccol = sun_contrib*scol*spec.rgb;
             speccol = clamp(speccol, vec3(0), vec3(1));
             bloom = dot(speccol, speccol) / 6;
-            col += speccol;
+            color += speccol;
         }
     }
 
-vec3 post_spec = col.rgb;
+vec3 post_spec = color.rgb;
 
-    col = mix(col.rgb, diffuse.rgb, diffuse.a);
+    //color = mix(color.rgb, diffuse_srgb.rgb, diffuse_srgb.a);
 
     if (envIntensity > 0.0)
     {
         //add environmentmap
         vec3 env_vec = env_mat * refnormpersp;
         
-        vec3 refcol = textureCube(environmentMap, env_vec).rgb;
+        vec3 reflected_color = textureCube(environmentMap, env_vec).rgb;
 
-        col = mix(col.rgb, refcol, 
-            envIntensity);  
+        color = mix(color.rgb, reflected_color, envIntensity); 
 
-        float cur_glare = max(refcol.r, refcol.g);
-        cur_glare = max(cur_glare, refcol.b);
+        float cur_glare = max(reflected_color.r, reflected_color.g);
+        cur_glare = max(cur_glare, reflected_color.b);
         cur_glare *= envIntensity*4.0;
         glare += cur_glare;
     }
 
-vec3 post_env = col.rgb;
+vec3 post_env = color.rgb;
+
+    color = atmosFragLighting(color, additive, atten);
+    color = scaleSoftClipFrag(color);
+
+vec3 post_atmo = color.rgb;
 
-    col = atmosFragLighting(col, additive, atten);
-    col = scaleSoftClipFrag(col);
+    //convert to linear space before adding local lights
+	color = srgb_to_linear(color);
 
     vec3 npos = normalize(-pos.xyz);
             
     vec3 light = vec3(0,0,0);
 
-vec3 post_atmo = col.rgb;
-
-    //col.rgb = srgb_to_linear(col.rgb);
-
- #define LIGHT_LOOP(i) light.rgb += calcPointLightOrSpotLight(light_diffuse[i].rgb, npos, diffuse.rgb, final_specular, pos.xyz, norm.xyz, light_position[i], light_direction[i].xyz, light_attenuation[i].x, light_attenuation[i].y, light_attenuation[i].z, glare, light_attenuation[i].w * 0.5);
+ #define LIGHT_LOOP(i) light.rgb += calcPointLightOrSpotLight(light_diffuse[i].rgb, npos, diffuse_linear.rgb, final_specular, pos.xyz, norm.xyz, light_position[i], light_direction[i].xyz, light_attenuation[i].x, light_attenuation[i].y, light_attenuation[i].z, glare, light_attenuation[i].w * 0.5);
 
         LIGHT_LOOP(1)
         LIGHT_LOOP(2)
@@ -410,27 +410,39 @@ vec3 post_atmo = col.rgb;
         LIGHT_LOOP(6)
         LIGHT_LOOP(7)
 
-vec3 postlight_linear = col.rgb;
 
     glare = min(glare, 1.0);
-    float al = max(diffcol.a,glare)*vertex_color.a;
+    float al = max(diffuse_linear.a,glare)*vertex_color.a;
+
+    color.rgb += light.rgb;
+
+    // (only) post-deferred needs inline gamma correction
+    color.rgb = linear_to_srgb(color.rgb);
+
+//color.rgb = amblit;
+//color.rgb = vec3(ambient);
+//color.rgb = sunlit;
+//color.rgb = post_ambient;
+//color.rgb = vec3(final_da);
+//color.rgb = sun_contrib;
+//color.rgb = post_sunlight;
+//color.rgb = diffuse_srgb.rgb;
+//color.rgb = post_diffuse;
+//color.rgb = post_spec;
+//color.rgb = post_env;
+//color.rgb = post_atmo;
 
 #ifdef WATER_FOG
-    vec4 temp = applyWaterFogView(pos, vec4(col.rgb, al));
-    col.rgb = temp.rgb;
+    vec4 temp = applyWaterFogView(pos, vec4(color.rgb, al));
+    color.rgb = temp.rgb;
     al = temp.a;
 #endif
 
-//col.rgb = post_atmo;
-
-    col.rgb = linear_to_srgb(col.rgb);
-
-    col.rgb += light.rgb;
-
-    frag_color.rgb = col.rgb;
+    frag_color.rgb = color.rgb;
     frag_color.a   = al;
 
 #else
+    // deferred path
     frag_data[0] = final_color;
     frag_data[1] = final_specular; // XYZ = Specular color. W = Specular exponent.
     frag_data[2] = final_normal; // XY = Normal.  Z = Env. intensity.
diff --git a/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl b/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
index 187b5a62dd1ba3e29c48c9b6954eefeb2134e8a9..8256d5f106192ae63358c56367af02a30e262270 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
@@ -44,12 +44,15 @@ uniform sampler2D altDiffuseMap;
 uniform float blend_factor; // interp factor between moon A/B
 VARYING vec2 vary_texcoord0;
 
+vec3 srgb_to_linear(vec3 c);
 void main() 
 {
     vec4 moonA = texture2D(diffuseMap, vary_texcoord0.xy);
     vec4 moonB = texture2D(altDiffuseMap, vary_texcoord0.xy);
     vec4 c     = mix(moonA, moonB, blend_factor);
 
+    c.rgb = srgb_to_linear(c.rgb);
+
     // mix factor which blends when sunlight is brighter
     // and shows true moon color at night
     vec3 luma_weights = vec3(0.3, 0.5, 0.3);
diff --git a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
index 6489508c00726219b296d6ab9b6ff20415d3c68a..2569e49743fda3f2c992ef18861b7b5c16347353 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
@@ -72,8 +72,6 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 vec3 getNorm(vec2 pos_screen);
-vec3 srgb_to_linear(vec3 cs);
-vec3 linear_to_srgb(vec3 cl);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
diff --git a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
index bcd93d9a2d20f30ba9d16dd0d2984b0b6779038d..84642b2f981257586c46f5bbb720c43a4989ff3c 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
@@ -36,7 +36,6 @@ out vec4 frag_color;
 
 uniform sampler2DRect diffuseRect;
 uniform sampler2DRect specularRect;
-uniform sampler2DRect positionMap;
 uniform sampler2DRect normalMap;
 uniform sampler2DRect lightMap;
 uniform sampler2DRect depthMap;
@@ -47,13 +46,7 @@ uniform float blur_size;
 uniform float blur_fidelity;
 
 // Inputs
-uniform vec4 morphFactor;
-uniform vec3 camPosLocal;
-//uniform vec4 camPosWorld;
-uniform vec4 gamma;
-uniform float global_gamma;
 uniform mat3 env_mat;
-uniform mat3 ssao_effect_mat;
 
 uniform vec3 sun_dir;
 uniform vec3 moon_dir;
@@ -63,21 +56,20 @@ VARYING vec2 vary_fragcoord;
 uniform mat4 inv_proj;
 uniform vec2 screen_res;
 
-#ifdef WATER_FOG
-vec4 applyWaterFogView(vec3 pos, vec4 color);
-#endif
-
 vec3 getNorm(vec2 pos_screen);
-vec3 atmosFragLighting(vec3 l, vec3 additive, vec3 atten);
+vec4 getPositionWithDepth(vec2 pos_screen, float depth);
 
-float getAmbientClamp();
 void calcAtmosphericVars(vec3 inPositionEye, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive, out vec3 atten);
-
+float getAmbientClamp();
+vec3 atmosFragLighting(vec3 l, vec3 additive, vec3 atten);
 vec3 scaleSoftClipFrag(vec3 l);
-vec3 srgb_to_linear(vec3 c);
+
 vec3 linear_to_srgb(vec3 c);
+vec3 srgb_to_linear(vec3 c);
 
-vec4 getPositionWithDepth(vec2 pos_screen, float depth);
+#ifdef WATER_FOG
+vec4 applyWaterFogView(vec3 pos, vec4 color);
+#endif
 
 void main() 
 {
@@ -88,50 +80,49 @@ void main()
     float envIntensity = norm.z;
     norm.xyz = getNorm(tc);
     
-    float light_gamma = 1.0/1.3;
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir : moon_dir;
-    
     float da = dot(normalize(norm.xyz), light_dir.xyz);
           da = clamp(da, -1.0, 1.0);
 
     float final_da = da;
           final_da = clamp(final_da, 0.0, 1.0);
-	      //final_da = pow(final_da, light_gamma);
 
-    vec4 gamma_diff = texture2DRect(diffuseRect, tc);
-    vec4 diffuse = gamma_diff;
-         diffuse.rgb = srgb_to_linear(gamma_diff.rgb);
+    vec4 diffuse_linear = texture2DRect(diffuseRect, tc);
+    vec4 diffuse_srgb   = vec4(linear_to_srgb(diffuse_linear.rgb), diffuse_linear.a);
 
     vec4 spec = texture2DRect(specularRect, vary_fragcoord.xy);
-    vec3 col;
+    vec3 color = vec3(0);
     float bloom = 0.0;
     {
+        float ambocc = 1.0; // no AO...
+
         vec3 sunlit;
         vec3 amblit;
         vec3 additive;
         vec3 atten;
-
-        calcAtmosphericVars(pos.xyz, 1.0, sunlit, amblit, additive, atten);
+    
+        calcAtmosphericVars(pos.xyz, ambocc, sunlit, amblit, additive, atten);
 
         float ambient = da;
         ambient *= 0.5;
         ambient *= ambient;
+        ambient = max(getAmbientClamp(), ambient);
         ambient = 1.0 - ambient;
 
         vec3 sun_contrib = final_da * sunlit;
 
-        col.rgb = amblit;
-        col.rgb *= ambient;
+        color.rgb = amblit;
+        color.rgb *= ambient;
 
-vec3 post_ambient = col.rgb;
+vec3 post_ambient = color.rgb;
 
-        col.rgb += sun_contrib;
+        color.rgb += sun_contrib;
 
-vec3 post_sunlight = col.rgb;
+vec3 post_sunlight = color.rgb;
 
-        col.rgb *= gamma_diff.rgb;
+        color.rgb *= diffuse_linear.rgb;
 
-vec3 post_diffuse = col.rgb;
+vec3 post_diffuse = color.rgb;
 
         vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
 
@@ -152,40 +143,59 @@ vec3 post_diffuse = col.rgb;
                                     
             if (nh > 0.0)
             {
-                float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
-                vec3 speccol = sun_contrib*scol*spec.rgb;
+                float scontrib = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
+                vec3 speccol = sun_contrib*scontrib*spec.rgb;
                 speccol = clamp(speccol, vec3(0), vec3(1));
-                bloom = dot(speccol, speccol) / 6;
-                col += speccol;
+                bloom += dot (speccol, speccol) / 6;
+                color += speccol;
             }
         }
        
- vec3 post_spec = col.rgb;
+ vec3 post_spec = color.rgb;
  
-        col.rgb += diffuse.a * diffuse.rgb;
+        color.rgb += diffuse_srgb.a * diffuse_srgb.rgb;
 
         if (envIntensity > 0.0)
         { //add environmentmap
             vec3 env_vec = env_mat * refnormpersp;
-            vec3 refcol = textureCube(environmentMap, env_vec).rgb;
-            col = mix(col.rgb, refcol, envIntensity); 
+            vec3 reflected_color = textureCube(environmentMap, env_vec).rgb;
+            color = mix(color.rgb, reflected_color, envIntensity); 
         }
-                
+        
+vec3 post_env = color.rgb;
+
         if (norm.w < 1)
         {
-            col = atmosFragLighting(col, additive, atten);
-            col = scaleSoftClipFrag(col);
+            color = atmosFragLighting(color, additive, atten);
+            color = scaleSoftClipFrag(color);
         }
 
-vec3 post_atmo = col.rgb;
+vec3 post_atmo = color.rgb;
 
         #ifdef WATER_FOG
-            vec4 fogged = applyWaterFogView(pos.xyz,vec4(col, bloom));
-            col = fogged.rgb;
+            vec4 fogged = applyWaterFogView(pos.xyz,vec4(color, bloom));
+            color = fogged.rgb;
             bloom = fogged.a;
         #endif
+
+//color.rgb = amblit;
+//color.rgb = vec3(ambient);
+//color.rgb = sunlit;
+//color.rgb = post_ambient;
+//color.rgb = vec3(final_da);
+//color.rgb = sun_contrib;
+//color.rgb = post_sunlight;
+//color.rgb = diffuse_srgb.rgb;
+//color.rgb = post_diffuse;
+//color.rgb = post_spec;
+//color.rgb = post_env;
+//color.rgb = post_atmo;
+
+// convert to linear as fullscreen lights need to sum in linear colorspace
+// and will be gamma (re)corrected downstream...
+        color.rgb = srgb_to_linear(color.rgb);
     }
 
-    frag_color.rgb = col.rgb;
+    frag_color.rgb = color.rgb;
     frag_color.a = bloom;
 }
diff --git a/indra/newview/app_settings/shaders/class1/deferred/sunDiscF.glsl b/indra/newview/app_settings/shaders/class1/deferred/sunDiscF.glsl
index 571e9d9326b7994c1ae2e2db8f5b4225fbefdb25..454af2a9bcee478fbe107402a564d52a32a5328c 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/sunDiscF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/sunDiscF.glsl
@@ -33,6 +33,7 @@ out vec4 frag_data[3];
 #define frag_data gl_FragData
 #endif
 
+vec3 srgb_to_linear(vec3 c);
 vec3 fullbrightAtmosTransport(vec3 light);
 vec3 fullbrightScaleSoftClip(vec3 light);
 
@@ -47,6 +48,8 @@ void main()
     vec4 sunDiscA = texture2D(diffuseMap, vary_texcoord0.xy);
     vec4 sunDiscB = texture2D(altDiffuseMap, vary_texcoord0.xy);
     vec4 c     = mix(sunDiscA, sunDiscB, blend_factor);
+
+    c.rgb = srgb_to_linear(c.rgb);
     c.rgb = clamp(c.rgb, vec3(0), vec3(1));
     c.rgb = pow(c.rgb, vec3(0.7f));
 
diff --git a/indra/newview/app_settings/shaders/class1/windlight/atmosphericsFuncs.glsl b/indra/newview/app_settings/shaders/class1/windlight/atmosphericsFuncs.glsl
index cd1e2d6e3299e0f8149d8ea97a3478184a773bca..a3041fccdc60fdb5f3f0f9129548c89d3be5c78e 100644
--- a/indra/newview/app_settings/shaders/class1/windlight/atmosphericsFuncs.glsl
+++ b/indra/newview/app_settings/shaders/class1/windlight/atmosphericsFuncs.glsl
@@ -137,7 +137,7 @@ void calcAtmosphericVars(vec3 inPositionEye, float ambFactor, out vec3 sunlit, o
           + tmpAmbient));
 
     //brightness of surface both sunlight and ambient
-    sunlit = sunlight.rgb;
+    sunlit = sunlight.rgb * 0.5;
     amblit = tmpAmbient.rgb * .25;
     additive *= vec3(1.0 - temp1);
 }
diff --git a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
index 9cd5592d7c1ddb9cdea4cb406b4a1a4fa2bd66b1..b05d544938f45bc941c6d900f8f59547a8104e46 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
@@ -71,12 +71,21 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
+vec3 srgb_to_linear(vec3 cs);
+vec3 linear_to_srgb(vec3 cl);
+
 vec3 getNorm(vec2 pos_screen);
-vec3 scaleDownLight(vec3 light);
+
+
+vec4 correctWithGamma(vec4 col)
+{
+	return vec4(srgb_to_linear(col.rgb), col.a);
+}
+
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    
+    ret = correctWithGamma(ret);
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
     float det = min(lod/(proj_lod*0.5), 1.0);
@@ -95,6 +104,7 @@ vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
+    ret = correctWithGamma(ret);
 
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
@@ -112,6 +122,7 @@ vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
+    ret = correctWithGamma(ret);
 
     vec2 dist = tc-vec2(0.5);
     
@@ -224,10 +235,7 @@ void main()
         amb_da *= dist_atten * noise;
         amb_da = min(amb_da, 1.0-lit);
 
-#ifndef NO_AMBIANCE
         col += amb_da*color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
-#endif
-
     }
     
 
@@ -288,8 +296,6 @@ void main()
     //not sure why, but this line prevents MATBUG-194
     col = max(col, vec3(0.0));
 
-    col = scaleDownLight(col);
-
     frag_color.rgb = col;   
     frag_color.a = 0.0;
 }
diff --git a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
index 7368b8d80cedbe9df387c816f6be77e209a5bc89..a2fbec524e5ebeb06109e32ca8b5c5b482676f04 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
@@ -45,40 +45,27 @@ uniform float blur_size;
 uniform float blur_fidelity;
 
 // Inputs
-uniform vec4 morphFactor;
-uniform vec3 camPosLocal;
-//uniform vec4 camPosWorld;
-uniform float cloud_shadow;
-uniform float max_y;
-uniform float global_gamma;
-uniform float display_gamma;
 uniform mat3 env_mat;
-uniform vec4 shadow_clip;
-uniform mat3 ssao_effect_mat;
 
 uniform vec3 sun_dir;
 uniform vec3 moon_dir;
 uniform int sun_up_factor;
-
 VARYING vec2 vary_fragcoord;
 
 uniform mat4 inv_proj;
 uniform vec2 screen_res;
 
 vec3 getNorm(vec2 pos_screen);
+vec4 getPositionWithDepth(vec2 pos_screen, float depth);
 
+void calcAtmosphericVars(vec3 inPositionEye, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive, out vec3 atten);
+float getAmbientClamp();
 vec3 atmosFragLighting(vec3 l, vec3 additive, vec3 atten);
 vec3 scaleSoftClipFrag(vec3 l);
 
-void calcAtmosphericVars(vec3 inPositionEye, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive, out vec3 atten);
-float getAmbientClamp();
-vec3 atmosTransportFrag(vec3 light, vec3 additive, vec3 atten);
 vec3 linear_to_srgb(vec3 c);
 vec3 srgb_to_linear(vec3 c);
 
-vec4 getPositionWithDepth(vec2 pos_screen, float depth);
-vec4 getPosition(vec2 pos_screen);
-
 #ifdef WATER_FOG
 vec4 applyWaterFogView(vec3 pos, vec4 color);
 #endif
@@ -94,24 +81,24 @@ void main()
 
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir : moon_dir;        
 
-    float light_gamma = 1.0/1.3;
     float scol = 1.0;
     vec2 scol_ambocc = texture2DRect(lightMap, vary_fragcoord.xy).rg;
 
     float da = dot(normalize(norm.xyz), light_dir.xyz);
           da = clamp(da, -1.0, 1.0);
 
-    vec4 gamma_diff = texture2DRect(diffuseRect, tc);
-    vec4 diffuse = gamma_diff;
-         diffuse.rgb = srgb_to_linear(diffuse.rgb);
- 
-    scol = max(scol_ambocc.r, diffuse.a);
+    
 
     float final_da = da;
           final_da = clamp(final_da, 0.0, 1.0);
 
+    vec4 diffuse_srgb   = texture2DRect(diffuseRect, tc);
+    vec4 diffuse_linear = vec4(srgb_to_linear(diffuse_srgb.rgb), diffuse_srgb.a);
+ 
+    scol = max(scol_ambocc.r, diffuse_linear.a);
+
     vec4 spec = texture2DRect(specularRect, vary_fragcoord.xy);
-    vec3 col;
+    vec3 color = vec3(0);
     float bloom = 0.0;
     {
         float ambocc = scol_ambocc.g;
@@ -126,22 +113,23 @@ void main()
         float ambient = da;
         ambient *= 0.5;
         ambient *= ambient;
+        ambient = max(getAmbientClamp(), ambient);
         ambient = 1.0 - ambient;
 
-        vec3 sun_contrib = scol * final_da * sunlit;
+        vec3 sun_contrib = min(scol, final_da) * sunlit;
 
-        col.rgb = amblit;
-        col.rgb *= ambient;
+        color.rgb = amblit;
+        color.rgb *= ambient;
 
-vec3 post_ambient = col.rgb;
+vec3 post_ambient = color.rgb;
 
-        col.rgb += sun_contrib;
+        color.rgb += sun_contrib;
 
-vec3 post_sunlight = col.rgb;
+vec3 post_sunlight = color.rgb;
 
-        col.rgb *= gamma_diff.rgb;
+        color.rgb *= diffuse_srgb.rgb;
 
-vec3 post_diffuse = col.rgb;
+vec3 post_diffuse = color.rgb;
 
         vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
 
@@ -166,31 +154,55 @@ vec3 post_diffuse = col.rgb;
                 vec3 speccol = sun_contrib*scontrib*spec.rgb;
                 speccol = clamp(speccol, vec3(0), vec3(1));
                 bloom += dot (speccol, speccol) / 6;
-                col += speccol;
+                color += speccol;
             }
         }
-        
-        col.rgb += diffuse.a * gamma_diff.rgb;
+       
+ vec3 post_spec = color.rgb;
+ 
+        color.rgb += diffuse_srgb.a * diffuse_srgb.rgb;
 
         if (envIntensity > 0.0)
         { //add environmentmap
             vec3 env_vec = env_mat * refnormpersp;
-            vec3 refcol = textureCube(environmentMap, env_vec).rgb;
-            col = mix(col.rgb, refcol, envIntensity); 
+            vec3 reflected_color = textureCube(environmentMap, env_vec).rgb;
+            color = mix(color.rgb, reflected_color, envIntensity); 
         }
         
+vec3 post_env = color.rgb;
+
         if (norm.w < 1)
         {
-            col = atmosFragLighting(col, additive, atten);
-            col = scaleSoftClipFrag(col);
+            color = atmosFragLighting(color, additive, atten);
+            color = scaleSoftClipFrag(color);
         }
 
+vec3 post_atmo = color.rgb;
+
         #ifdef WATER_FOG
-            vec4 fogged = applyWaterFogView(pos.xyz,vec4(col, bloom));
-            col = fogged.rgb;
+            vec4 fogged = applyWaterFogView(pos.xyz,vec4(color, bloom));
+            color = fogged.rgb;
             bloom = fogged.a;
         #endif
+
+//color.rgb = amblit;
+//color.rgb = vec3(ambient);
+//color.rgb = sunlit;
+//color.rgb = post_ambient;
+//color.rgb = vec3(final_da);
+//color.rgb = sun_contrib;
+//color.rgb = post_sunlight;
+//color.rgb = diffuse_srgb.rgb;
+//color.rgb = post_diffuse;
+//color.rgb = post_spec;
+//color.rgb = post_env;
+//color.rgb = post_atmo;
+
+// convert to linear as fullscreen lights need to sum in linear colorspace
+// and will be gamma (re)corrected downstream...
+        color.rgb = srgb_to_linear(color.rgb);
     }
-    frag_color.rgb = col.rgb;
+
+    frag_color.rgb = color.rgb;
     frag_color.a = bloom;
 }
diff --git a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
index 957ce579c0ef6e6436bb6cd7288f20f47f40cdc0..a3ff75484988e894173a1fb57b53395300d2b38b 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
@@ -72,12 +72,17 @@ uniform vec2 screen_res;
 uniform mat4 inv_proj;
 
 vec3 getNorm(vec2 pos_screen);
-vec3 scaleDownLight(vec3 c);
+vec3 srgb_to_linear(vec3 c);
+vec4 correctWithGamma(vec4 col)
+{
+	return vec4(srgb_to_linear(col.rgb), col.a);
+}
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
-	
+	ret = correctWithGamma(ret);
+
 	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
 	float det = min(lod/(proj_lod*0.5), 1.0);
@@ -96,6 +101,7 @@ vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
+	ret = correctWithGamma(ret);
 	
 	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
@@ -113,6 +119,7 @@ vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
+	ret = correctWithGamma(ret);
 	
 	vec2 dist = tc-vec2(0.5);
 	
@@ -223,9 +230,7 @@ void main()
 		amb_da *= dist_atten * noise;
 		amb_da = min(amb_da, 1.0-lit);
 	
-#ifndef NO_AMBIANCE		
 	    col += amb_da*color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
-#endif
 	}
 	
 
@@ -290,8 +295,6 @@ void main()
 	//not sure why, but this line prevents MATBUG-194
 	col = max(col, vec3(0.0));
 
-    col = scaleDownLight(col);
-
 	frag_color.rgb = col;	
 	frag_color.a = 0.0;
 }
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
index 94e776d51d669240f46f87e7157dd287405a6321..36703ea7425b6767cc9afb314aceaca519e5393c 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
@@ -44,7 +44,6 @@ uniform mat3 ssao_effect_mat;
 uniform int no_atmo;
 uniform float sun_moon_glow_factor;
 
-vec3 srgb_to_linear(vec3 c);
 vec3 scaleSoftClipFrag(vec3 light);
 
 vec3 atmosFragLighting(vec3 light, vec3 additive, vec3 atten)
diff --git a/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
index e31bdf610c777478bda6fe10f980dce8eb6f1af2..1de919bf309b7c58386b92b5c8eec29ee5df0046 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/cloudsF.glsl
@@ -86,14 +86,14 @@ void main()
     vec2 disturbance2 = vec2(cloudNoise((uv1 + uv3) / 4.0f).x, cloudNoise((uv4 + uv2) / 8.0f).x) * cloud_variance * (1.0f - cloud_scale * 0.25f);
 
     // Offset texture coords
-    uv1 += cloud_pos_density1.xy + (disturbance * 0.02);    //large texture, visible density
+    uv1 += cloud_pos_density1.xy;// + (disturbance * 0.02);    //large texture, visible density
     uv2 += cloud_pos_density1.xy;   //large texture, self shadow
     uv3 += cloud_pos_density2.xy;   //small texture, visible density
     uv4 += cloud_pos_density2.xy;   //small texture, self shadow
 
     float density_variance = min(1.0, (disturbance.x* 2.0 + disturbance.y* 2.0 + disturbance2.x + disturbance2.y));
 
-    cloudDensity *= 1.0 - (density_variance * density_variance);
+    //cloudDensity *= 1.0 - (density_variance * density_variance);
 
     // Compute alpha1, the main cloud opacity
 
@@ -104,12 +104,12 @@ void main()
     alpha1 = 1. - alpha1 * alpha1;
     alpha1 = 1. - alpha1 * alpha1;  
 
-    alpha1 *= altitude_blend_factor;
+    //alpha1 *= altitude_blend_factor;
 
-    if (alpha1 < 0.001f)
-    {
-        discard;
-    }
+    //if (alpha1 < 0.001f)
+    //{
+    //    discard;
+    //}
 
     // Compute alpha2, for self shadowing effect
     // (1 - alpha2) will later be used as percentage of incoming sunlight
diff --git a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
index 64156bd3bf979887569b21af394ca6348456e469..5c5cd0294af56f23b9e45da84a0d77ffbb65a076 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
@@ -180,7 +180,8 @@ void main()
     // Texture coords
     vary_texcoord0 = texcoord0;
     vary_texcoord0.xy -= 0.5;
-    vary_texcoord0.xy /= max(0.001, cloud_scale);
+    vary_texcoord0.xy /= cloud_scale;
+    //vary_texcoord0.xy /= max(0.001, cloud_scale);
     vary_texcoord0.xy += 0.5;
 
     vary_texcoord1 = vary_texcoord0;
diff --git a/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
index abb6a3a5d8aaa8d139d3e6c199058fff79865d7f..52de7ceaad5c1e5b3e4b6ac6b9c1692e40f7cbaf 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
@@ -71,19 +71,11 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
-vec3 srgb_to_linear(vec3 cs);
-vec3 linear_to_srgb(vec3 cl);
 vec3 getNorm(vec2 pos_screen);
 
-vec4 correctWithGamma(vec4 col)
-{
-    return vec4(srgb_to_linear(col.rgb), col.a);
-}
-
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret.rgb = srgb_to_linear(ret.rgb);
     
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
@@ -103,7 +95,6 @@ vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret = correctWithGamma(ret);
 
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
@@ -121,7 +112,6 @@ vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret = correctWithGamma(ret);
 
     vec2 dist = tc-vec2(0.5);
     
@@ -260,10 +250,6 @@ void main()
             //col += spec.rgb;
         }
     }   
-    
-    
-    
-    
 
     if (envIntensity > 0.0)
     {
@@ -297,6 +283,8 @@ void main()
     //not sure why, but this line prevents MATBUG-194
     col = max(col, vec3(0.0));
 
+    col = scaleDownLight(col);
+
     frag_color.rgb = col;   
     frag_color.a = 0.0;
 }
diff --git a/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
index afc542b8bc6e25d353e5213bbd0df609753849c9..bccd819a4374c25936f126451a56330d7d765d94 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
@@ -72,18 +72,10 @@ uniform vec2 screen_res;
 uniform mat4 inv_proj;
 
 vec3 getNorm(vec2 pos_screen);
-vec3 srgb_to_linear(vec3 cs);
-vec3 linear_to_srgb(vec3 cl);
-
-vec4 correctWithGamma(vec4 col)
-{
-    return vec4(srgb_to_linear(col.rgb), col.a);
-}
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret.rgb = srgb_to_linear(ret.rgb);
     
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
@@ -103,7 +95,6 @@ vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret = correctWithGamma(ret);
     
     vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
     
@@ -121,7 +112,6 @@ vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
 {
     vec4 ret = texture2DLod(projectionMap, tc, lod);
-    ret = correctWithGamma(ret);
     
     vec2 dist = tc-vec2(0.5);
     
@@ -256,7 +246,6 @@ void main()
         {
             float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da);
             col += dlit*scol*spec.rgb*shadow;
-            //col += spec.rgb;
         }
     }   
     
diff --git a/indra/newview/app_settings/shaders/class3/deferred/underWaterF.glsl b/indra/newview/app_settings/shaders/class3/deferred/underWaterF.glsl
index 9d18d1afd8f300be93e46bc02a985216179b071e..540226e672abd571b963f8fb685b2a5a21641bcb 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/underWaterF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/underWaterF.glsl
@@ -58,9 +58,6 @@ VARYING vec4 refCoord;
 VARYING vec4 littleWave;
 VARYING vec4 view;
 
-vec3 srgb_to_linear(vec3 cs);
-vec3 linear_to_srgb(vec3 cl);
-
 vec2 encode_normal(vec3 n);
 
 vec4 applyWaterFog(vec4 color, vec3 viewVec)
diff --git a/indra/newview/app_settings/shaders/class3/deferred/waterF.glsl b/indra/newview/app_settings/shaders/class3/deferred/waterF.glsl
index 2a144ba23a93cf7821131d2eafb3a802a1b1ad40..c65cf48c673355e681336dc029b85bdec79f8b9c 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/waterF.glsl
@@ -61,7 +61,6 @@ VARYING vec4 view;
 VARYING vec4 vary_position;
 
 vec3 scaleSoftClip(vec3 c);
-vec3 srgb_to_linear(vec3 cs);
 vec2 encode_normal(vec3 n);
 
 vec3 BlendNormal(vec3 bump1, vec3 bump2)
diff --git a/indra/newview/app_settings/shaders/class3/lighting/lightV.glsl b/indra/newview/app_settings/shaders/class3/lighting/lightV.glsl
index f5428077fa501fbc4e8a43e652bb8dbabba2fb64..69267621a3081bed2e891df18a0e04a4cf9b849d 100644
--- a/indra/newview/app_settings/shaders/class3/lighting/lightV.glsl
+++ b/indra/newview/app_settings/shaders/class3/lighting/lightV.glsl
@@ -31,8 +31,8 @@ vec4 sumLights(vec3 pos, vec3 norm, vec4 color);
 
 vec4 calcLighting(vec3 pos, vec3 norm, vec4 color)
 {
-	vec4 c = sumLights(pos, norm, color) * 2.0;
-    c.rgb += atmosAmbient() * color.rgb * 0.5;
-    return c; 
+	vec4 c = sumLights(pos, norm, color * 2.0);
+    c.rgb += atmosAmbient() * color.rgb;
+    return c * 2.0; 
 }
 
diff --git a/indra/newview/lldrawpoolwlsky.cpp b/indra/newview/lldrawpoolwlsky.cpp
index 9039c8cbcb5f102dd8be3d58866012d7132f5203..b9bc7678e6e1bf51071aa1f1d2ac66187b7e592d 100644
--- a/indra/newview/lldrawpoolwlsky.cpp
+++ b/indra/newview/lldrawpoolwlsky.cpp
@@ -484,9 +484,6 @@ void LLDrawPoolWLSky::renderHeavenlyBodies()
             {
                 sun_shader->bind();
 
-                gGL.getTexUnit(0)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
-                gGL.getTexUnit(1)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
-
                 if (tex_a && (!tex_b || (tex_a == tex_b)))
                 {
                     // Bind current and next sun textures
@@ -535,9 +532,6 @@ void LLDrawPoolWLSky::renderHeavenlyBodies()
         {
             moon_shader->bind();
 
-            gGL.getTexUnit(0)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
-            gGL.getTexUnit(1)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
-
             if (tex_a && (!tex_b || (tex_a == tex_b)))
             {
                 // Bind current and next sun textures
diff --git a/indra/newview/llsettingsvo.cpp b/indra/newview/llsettingsvo.cpp
index 628666c3eda475f84a91fdaeede454a8b456cd6b..4bd8fcac0cd0f7200ae9350bcca608a8e2cd8c43 100644
--- a/indra/newview/llsettingsvo.cpp
+++ b/indra/newview/llsettingsvo.cpp
@@ -707,6 +707,7 @@ void LLSettingsVOSky::applySpecial(void *ptarget)
 
         shader->uniform4fv(LLShaderMgr::SUNLIGHT_COLOR, 1, sunDiffuse.mV);
         shader->uniform4fv(LLShaderMgr::MOONLIGHT_COLOR, 1, moonDiffuse.mV);
+        shader->uniform4fv(LLShaderMgr::CLOUD_COLOR, 1, psky->getCloudColor().mV);
 	}
 
     F32 g = getGamma();
@@ -736,14 +737,17 @@ LLSettingsSky::parammapping_t LLSettingsVOSky::getParameterMap() const
 
         // Following values are always present, so we can just zero these ones, but used values from defaults()
         LLSD sky_defaults = LLSettingsSky::defaults();
-        param_map[SETTING_CLOUD_COLOR] = DefaultParam(LLShaderMgr::CLOUD_COLOR, sky_defaults[SETTING_CLOUD_COLOR]);
+        
         param_map[SETTING_CLOUD_POS_DENSITY2] = DefaultParam(LLShaderMgr::CLOUD_POS_DENSITY2, sky_defaults[SETTING_CLOUD_POS_DENSITY2]);
         param_map[SETTING_CLOUD_SCALE] = DefaultParam(LLShaderMgr::CLOUD_SCALE, sky_defaults[SETTING_CLOUD_SCALE]);
         param_map[SETTING_CLOUD_SHADOW] = DefaultParam(LLShaderMgr::CLOUD_SHADOW, sky_defaults[SETTING_CLOUD_SHADOW]);
         param_map[SETTING_CLOUD_VARIANCE] = DefaultParam(LLShaderMgr::CLOUD_VARIANCE, sky_defaults[SETTING_CLOUD_VARIANCE]);
         param_map[SETTING_GLOW] = DefaultParam(LLShaderMgr::GLOW, sky_defaults[SETTING_GLOW]);
         param_map[SETTING_MAX_Y] = DefaultParam(LLShaderMgr::MAX_Y, sky_defaults[SETTING_MAX_Y]);
+
         //param_map[SETTING_SUNLIGHT_COLOR] = DefaultParam(LLShaderMgr::SUNLIGHT_COLOR, sky_defaults[SETTING_SUNLIGHT_COLOR]);
+        //param_map[SETTING_CLOUD_COLOR] = DefaultParam(LLShaderMgr::CLOUD_COLOR, sky_defaults[SETTING_CLOUD_COLOR]);
+
         param_map[SETTING_MOON_BRIGHTNESS] = DefaultParam(LLShaderMgr::MOON_BRIGHTNESS, sky_defaults[SETTING_MOON_BRIGHTNESS]);
         param_map[SETTING_SKY_MOISTURE_LEVEL] = DefaultParam(LLShaderMgr::MOISTURE_LEVEL, sky_defaults[SETTING_SKY_MOISTURE_LEVEL]);
         param_map[SETTING_SKY_DROPLET_RADIUS] = DefaultParam(LLShaderMgr::DROPLET_RADIUS, sky_defaults[SETTING_SKY_DROPLET_RADIUS]);
diff --git a/indra/newview/llviewershadermgr.cpp b/indra/newview/llviewershadermgr.cpp
index 0bd4a5eae076f4ecef0e2620ed43420fad0be8e1..ff7ba6b284a02e93b1ee225e1def510ad5cd536d 100644
--- a/indra/newview/llviewershadermgr.cpp
+++ b/indra/newview/llviewershadermgr.cpp
@@ -1340,6 +1340,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
 	{
 		gDeferredDiffuseProgram.mName = "Deferred Diffuse Shader";
         gDeferredDiffuseProgram.mFeatures.encodesNormal = true;
+        gDeferredDiffuseProgram.mFeatures.hasSrgb = true;
 		gDeferredDiffuseProgram.mShaderFiles.clear();
 		gDeferredDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
 		gDeferredDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseIndexedF.glsl", GL_FRAGMENT_SHADER_ARB));
@@ -1389,6 +1390,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
 		gDeferredNonIndexedDiffuseProgram.mName = "Non Indexed Deferred Diffuse Shader";
 		gDeferredNonIndexedDiffuseProgram.mShaderFiles.clear();
         gDeferredNonIndexedDiffuseProgram.mFeatures.encodesNormal = true;
+        gDeferredNonIndexedDiffuseProgram.mFeatures.hasSrgb = true;
 		gDeferredNonIndexedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseV.glsl", GL_VERTEX_SHADER_ARB));
 		gDeferredNonIndexedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseF.glsl", GL_FRAGMENT_SHADER_ARB));
 		gDeferredNonIndexedDiffuseProgram.mShaderLevel = mShaderLevel[SHADER_DEFERRED];
@@ -1401,6 +1403,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
 		gDeferredSkinnedDiffuseProgram.mName = "Deferred Skinned Diffuse Shader";
 		gDeferredSkinnedDiffuseProgram.mFeatures.hasObjectSkinning = true;
 		gDeferredSkinnedDiffuseProgram.mFeatures.encodesNormal = true;
+        gDeferredSkinnedDiffuseProgram.mFeatures.hasSrgb = true;
 		gDeferredSkinnedDiffuseProgram.mShaderFiles.clear();
 		gDeferredSkinnedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseSkinnedV.glsl", GL_VERTEX_SHADER_ARB));
 		gDeferredSkinnedDiffuseProgram.mShaderFiles.push_back(make_pair("deferred/diffuseF.glsl", GL_FRAGMENT_SHADER_ARB));
@@ -1829,10 +1832,12 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
     {
         gDeferredAlphaImpostorProgram.mName = "Deferred Alpha Impostor Shader";
 
+// Begin Hack
 		gDeferredAlphaImpostorProgram.mFeatures.calculatesLighting = false;
 		gDeferredAlphaImpostorProgram.mFeatures.hasLighting = false;
+
+        gDeferredAlphaImpostorProgram.mFeatures.hasSrgb = true;
 		gDeferredAlphaImpostorProgram.mFeatures.isAlphaLighting = true;
-		gDeferredAlphaImpostorProgram.mFeatures.disableTextureIndex = true; //hack to disable auto-setup of         gDeferredAlphaImpostorProgram.mFeatures.hasSrgb = true;
         gDeferredAlphaImpostorProgram.mFeatures.encodesNormal = true;
         gDeferredAlphaImpostorProgram.mFeatures.hasShadows = use_sun_shadow;
 
@@ -1864,7 +1869,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
         success = gDeferredAlphaImpostorProgram.createShader(NULL, NULL);
         llassert(success);
 
-        // Hack
+// End Hack
         gDeferredAlphaImpostorProgram.mFeatures.calculatesLighting = true;
         gDeferredAlphaImpostorProgram.mFeatures.hasLighting = true;
     }
@@ -2514,6 +2519,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
         gDeferredWLSunProgram.mFeatures.hasAtmospherics = true;
         gDeferredWLSunProgram.mFeatures.isFullbright = true;
         gDeferredWLSunProgram.mFeatures.disableTextureIndex = true;
+        gDeferredWLSunProgram.mFeatures.hasSrgb = true;
         gDeferredWLSunProgram.mShaderFiles.clear();
         gDeferredWLSunProgram.mShaderFiles.push_back(make_pair("deferred/sunDiscV.glsl", GL_VERTEX_SHADER_ARB));
         gDeferredWLSunProgram.mShaderFiles.push_back(make_pair("deferred/sunDiscF.glsl", GL_FRAGMENT_SHADER_ARB));
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 4cd63de70cdcd290608bcb97279462a2f2b8dd3a..f1a75f12770742db85fd34725fee966dbb4243d3 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -7544,7 +7544,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 		
 		gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
 		
-		mScreen.bindTexture(0, 0);
+		mScreen.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
 		
 		gGL.color4f(1,1,1,1);
 		gPipeline.enableLightsFullbright(LLColor4(1,1,1,1));
@@ -7777,7 +7777,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 				S32 channel = shader->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mScreen.getUsage());
 				if (channel > -1)
 				{
-					mScreen.bindTexture(0, channel);
+					mScreen.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 				}
 
 				shader->uniform1f(LLShaderMgr::DOF_FOCAL_DISTANCE, -subject_distance/1000.f);
@@ -7816,7 +7816,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 				S32 channel = shader->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mDeferredLight.getUsage());
 				if (channel > -1)
 				{
-					mDeferredLight.bindTexture(0, channel);
+					mDeferredLight.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 				}
 
 				shader->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
@@ -7860,8 +7860,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 				S32 channel = shader->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mScreen.getUsage());
 				if (channel > -1)
 				{
-					mScreen.bindTexture(0, channel);
-					gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
+					mScreen.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 				}
 
 				if (!LLViewerCamera::getInstance()->cameraUnderWater())
@@ -7909,7 +7908,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 			S32 channel = shader->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mScreen.getUsage());
 			if (channel > -1)
 			{
-				mScreen.bindTexture(0, channel);
+				mScreen.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 			}
 
 			if (!LLViewerCamera::getInstance()->cameraUnderWater())
@@ -7956,7 +7955,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 			S32 channel = shader->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mDeferredLight.getUsage());
 			if (channel > -1)
 			{
-				mDeferredLight.bindTexture(0, channel);
+				mDeferredLight.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 			}
 						
 			gGL.begin(LLRender::TRIANGLE_STRIP);
@@ -7978,8 +7977,7 @@ void LLPipeline::renderBloom(bool for_snapshot, F32 zoom_factor, int subfield)
 			channel = shader->enableTexture(LLShaderMgr::DIFFUSE_MAP, mFXAABuffer.getUsage());
 			if (channel > -1)
 			{
-				mFXAABuffer.bindTexture(0, channel);
-				gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
+				mFXAABuffer.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
 			}
 			
 			gGLViewport[0] = gViewerWindow->getWorldViewRectRaw().mLeft;
@@ -8144,25 +8142,19 @@ void LLPipeline::bindDeferredShader(LLGLSLShader& shader, LLRenderTarget* light_
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, deferred_target->getUsage());
     if (channel > -1)
     {
-        deferred_target->bindTexture(0,channel);
-        gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
-        gGL.getTexUnit(channel)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
+        deferred_target->bindTexture(0,channel, LLTexUnit::TFO_POINT);
     }
 
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_SPECULAR, deferred_target->getUsage());
     if (channel > -1)
     {
-        deferred_target->bindTexture(1, channel);
-        gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
-        gGL.getTexUnit(channel)->setTextureColorSpace(LLTexUnit::TCS_SRGB);
+        deferred_target->bindTexture(1, channel, LLTexUnit::TFO_POINT);
     }
 
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_NORMAL, deferred_target->getUsage());
     if (channel > -1)
     {
-        deferred_target->bindTexture(2, channel);
-        gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
-        gGL.getTexUnit(channel)->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
+        deferred_target->bindTexture(2, channel, LLTexUnit::TFO_POINT);
     }
 
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_DEPTH, deferred_depth_target->getUsage());
@@ -8212,14 +8204,13 @@ void LLPipeline::bindDeferredShader(LLGLSLShader& shader, LLRenderTarget* light_
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_LIGHT, light_target->getUsage());
     if (channel > -1)
     {
-        light_target->bindTexture(0, channel);
-        gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
+        light_target->bindTexture(0, channel, LLTexUnit::TFO_POINT);
     }
 
     channel = shader.enableTexture(LLShaderMgr::DEFERRED_BLOOM);
     if (channel > -1)
     {
-        mGlow[1].bindTexture(0, channel);
+        mGlow[1].bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
     }
 
     stop_glerror();
@@ -8285,7 +8276,7 @@ void LLPipeline::bindDeferredShader(LLGLSLShader& shader, LLRenderTarget* light_
 
 	stop_glerror();
 
-	channel = shader.enableTexture(LLShaderMgr::ENVIRONMENT_MAP, LLTexUnit::TT_CUBE_MAP, LLTexUnit::TCS_SRGB);
+	channel = shader.enableTexture(LLShaderMgr::ENVIRONMENT_MAP, LLTexUnit::TT_CUBE_MAP);
 	if (channel > -1)
 	{
 		LLCubeMap* cube_map = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
@@ -8784,7 +8775,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
 
                 mCubeVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
 
-                gDeferredSpotLightProgram.enableTexture(LLShaderMgr::DEFERRED_PROJECTION, LLTexUnit::TT_TEXTURE, LLTexUnit::TCS_SRGB);
+                gDeferredSpotLightProgram.enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
 
                 for (LLDrawable::drawable_list_t::iterator iter = spot_lights.begin(); iter != spot_lights.end(); ++iter)
                 {
@@ -8812,7 +8803,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                                         
                     mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8, get_box_fan_indices(camera, center));
                 }
-                gDeferredSpotLightProgram.disableTexture(LLShaderMgr::DEFERRED_PROJECTION, LLTexUnit::TT_TEXTURE, LLTexUnit::TCS_SRGB);
+                gDeferredSpotLightProgram.disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
                 unbindDeferredShader(gDeferredSpotLightProgram);
             }
 
@@ -8868,7 +8859,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                 
                 bindDeferredShader(gDeferredMultiSpotLightProgram);
 
-                gDeferredMultiSpotLightProgram.enableTexture(LLShaderMgr::DEFERRED_PROJECTION, LLTexUnit::TT_TEXTURE, LLTexUnit::TCS_SRGB);
+                gDeferredMultiSpotLightProgram.enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
 
                 mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
 
@@ -8899,7 +8890,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
                     mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
                 }
 
-                gDeferredMultiSpotLightProgram.disableTexture(LLShaderMgr::DEFERRED_PROJECTION, LLTexUnit::TT_TEXTURE, LLTexUnit::TCS_SRGB);
+                gDeferredMultiSpotLightProgram.disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
                 unbindDeferredShader(gDeferredMultiSpotLightProgram);
 
                 gGL.popMatrix();
@@ -8936,8 +8927,7 @@ void LLPipeline::renderDeferredLighting(LLRenderTarget* screen_target)
         channel = gDeferredPostGammaCorrectProgram.enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, screen_target->getUsage());
         if (channel > -1)
         {
-            screen_target->bindTexture(0,channel);
-            gGL.getTexUnit(channel)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
+            screen_target->bindTexture(0, channel, LLTexUnit::TFO_POINT);
         }
         
         gDeferredPostGammaCorrectProgram.uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, screen_target->getWidth(), screen_target->getHeight());
@@ -9144,7 +9134,7 @@ void LLPipeline::setupSpotLight(LLGLSLShader& shader, LLDrawable* drawablep)
 		img = LLViewerFetchedTexture::sWhiteImagep;
 	}
 
-	S32 channel = shader.enableTexture(LLShaderMgr::DEFERRED_PROJECTION, LLTexUnit::TT_TEXTURE, LLTexUnit::TCS_SRGB);
+	S32 channel = shader.enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
 
 	if (channel > -1)
 	{
@@ -9196,7 +9186,7 @@ void LLPipeline::unbindDeferredShader(LLGLSLShader &shader)
     shader.disableTexture(LLShaderMgr::DEFERRED_NOISE);
     shader.disableTexture(LLShaderMgr::DEFERRED_LIGHTFUNC);
 
-    S32 channel = shader.disableTexture(LLShaderMgr::ENVIRONMENT_MAP, LLTexUnit::TT_CUBE_MAP, LLTexUnit::TCS_SRGB);
+    S32 channel = shader.disableTexture(LLShaderMgr::ENVIRONMENT_MAP, LLTexUnit::TT_CUBE_MAP);
     if (channel > -1)
     {
         LLCubeMap* cube_map = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap() : NULL;
@@ -10280,7 +10270,6 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 
         //far_clip = llmin(far_clip, 128.f);
         far_clip = llmin(far_clip, camera.getFar());
-        far_clip = llmax(far_clip, 256.0f);
 
         F32 range = far_clip-near_clip;