From 3116416fcb8dfd54ef2807e9e75959429c946d79 Mon Sep 17 00:00:00 2001
From: "Graham Linden graham@lindenlab.com"
 <Graham Linden graham@lindenlab.com>
Date: Tue, 1 May 2018 00:10:11 +0100
Subject: [PATCH] Le Merge

---
 indra/llrender/llatmosphere.cpp               |  17 +++
 indra/llrender/llatmosphere.h                 |   2 +
 indra/llrender/llglslshader.cpp               |   7 +-
 indra/llrender/llglslshader.h                 |   2 +
 indra/llrender/llrender.cpp                   | 129 ++++++++++++++++--
 indra/llrender/llrender.h                     |  20 +++
 indra/llrender/llshadermgr.cpp                |   3 +
 indra/llrender/llshadermgr.h                  |   1 +
 indra/llrender/llvertexbuffer.cpp             |   2 +-
 indra/newview/app_settings/logcontrol.xml     |   1 +
 indra/newview/app_settings/settings.xml       |   2 +-
 .../shaders/class3/deferred/skyF.glsl         |  28 +++-
 .../shaders/class3/deferred/skyV.glsl         |  14 +-
 .../class3/windlight/advancedAtmoF.glsl       |  17 ++-
 indra/newview/lldrawpoolground.cpp            |   2 +-
 indra/newview/lldrawpoolsky.cpp               |   2 +-
 indra/newview/lldrawpoolwater.cpp             |   2 +-
 indra/newview/lldrawpoolwlsky.cpp             |  87 ++++++------
 indra/newview/lldrawpoolwlsky.h               |   2 +
 indra/newview/llglsandbox.cpp                 |   2 +-
 indra/newview/llpanelprimmediacontrols.cpp    |   6 +-
 indra/newview/llviewercamera.cpp              |  25 ----
 indra/newview/llviewercamera.h                |   8 --
 indra/newview/llviewerdisplay.cpp             |  26 ++--
 indra/newview/llvieweroctree.cpp              |   2 +-
 indra/newview/llviewershadermgr.cpp           |  65 ++++-----
 indra/newview/pipeline.cpp                    | 126 +++++------------
 indra/newview/pipeline.h                      |   9 +-
 28 files changed, 349 insertions(+), 260 deletions(-)

diff --git a/indra/llrender/llatmosphere.cpp b/indra/llrender/llatmosphere.cpp
index 29a5d4be08a..644e102a151 100644
--- a/indra/llrender/llatmosphere.cpp
+++ b/indra/llrender/llatmosphere.cpp
@@ -201,9 +201,11 @@ bool LLAtmosphere::configureAtmosphericModel(AtmosphericModelSettings& settings)
         delete m_model;
     }
     m_model = nullptr;
+
     getTransmittance()->setTexName(0);
     getScattering()->setTexName(0);
     getMieScattering()->setTexName(0);
+    getIlluminance()->setTexName(0);
 
     // Init libatmosphere model
     m_config.num_scattering_orders = 4;
@@ -235,6 +237,7 @@ bool LLAtmosphere::configureAtmosphericModel(AtmosphericModelSettings& settings)
         getTransmittance()->setTexName(m_textures.transmittance_texture);
         getScattering()->setTexName(m_textures.scattering_texture);   
         getMieScattering()->setTexName(m_textures.single_mie_scattering_texture);
+        getIlluminance()->setTexName(m_textures.illuminance_texture);
     }
 
     return m_model != nullptr;
@@ -282,6 +285,20 @@ LLGLTexture* LLAtmosphere::getMieScattering()
     return m_mie_scatter_texture;
 }
 
+LLGLTexture* LLAtmosphere::getIlluminance()
+{
+    if (!m_illuminance)
+    {
+        m_illuminance = new LLGLTexture;
+        m_illuminance->generateGLTexture();
+        m_illuminance->setAddressMode(LLTexUnit::eTextureAddressMode::TAM_CLAMP);
+        m_illuminance->setFilteringOption(LLTexUnit::eTextureFilterOptions::TFO_BILINEAR);
+        m_illuminance->setExplicitFormat(GL_RGB16F_ARB, GL_RGB, GL_FLOAT);
+        m_illuminance->setTarget(GL_TEXTURE_2D, LLTexUnit::TT_TEXTURE);
+    }
+    return m_illuminance;
+}
+
 GLhandleARB LLAtmosphere::getAtmosphericShaderForLink() const
 {
     return m_model ? m_model->GetShader() : 0;
diff --git a/indra/llrender/llatmosphere.h b/indra/llrender/llatmosphere.h
index 53dc1dd3547..0d62a069acc 100644
--- a/indra/llrender/llatmosphere.h
+++ b/indra/llrender/llatmosphere.h
@@ -79,6 +79,7 @@ class LLAtmosphere
     LLGLTexture* getTransmittance();
     LLGLTexture* getScattering();
     LLGLTexture* getMieScattering();
+    LLGLTexture* getIlluminance();
 
     GLhandleARB getAtmosphericShaderForLink() const;
 
@@ -97,6 +98,7 @@ class LLAtmosphere
     LLPointer<LLGLTexture> m_transmittance;
     LLPointer<LLGLTexture> m_scattering;
     LLPointer<LLGLTexture> m_mie_scatter_texture;
+    LLPointer<LLGLTexture> m_illuminance;
 
     std::vector<double> m_wavelengths;
     std::vector<double> m_solar_irradiance;
diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp
index 73ab95cf3b5..95e1274c166 100644
--- a/indra/llrender/llglslshader.cpp
+++ b/indra/llrender/llglslshader.cpp
@@ -33,6 +33,8 @@
 #include "llrender.h"
 #include "llvertexbuffer.h"
 
+#pragma optimize("", off)
+
 #if LL_DARWIN
 #include "OpenGL/OpenGL.h"
 #endif
@@ -659,7 +661,7 @@ void LLGLSLShader::mapUniform(GLint index, const vector<LLStaticHashedString> *
         mUniformMap[hashedName] = location;
 
         LL_DEBUGS("ShaderLoading") << "Uniform " << name << " is at location " << location << LL_ENDL;
-    
+  
         //find the index of this uniform
         for (S32 i = 0; i < (S32) LLShaderMgr::instance()->mReservedUniforms.size(); i++)
         {
@@ -683,7 +685,7 @@ void LLGLSLShader::mapUniform(GLint index, const vector<LLStaticHashedString> *
                 std::pair<uniforms_index_t::iterator, bool> result;
                 S32 index = i + LLShaderMgr::instance()->mReservedUniforms.size();
 
-                if ((*uniforms)[i].String() == name)
+                if ((*uniforms)[i] == hashedName)
                 {
                     result = mUniform.insert(uniforms_index_t::value_type(index, location));
                     if (result.second)
@@ -1218,7 +1220,6 @@ void LLGLSLShader::uniformMatrix4fv(U32 index, U32 count, GLboolean transpose, c
     if (mProgramObject)
     {   
         GLint location = getLocationForIndex(index);
-
         if (location >= 0)
         {
             glUniformMatrix4fvARB(location, count, transpose, v);
diff --git a/indra/llrender/llglslshader.h b/indra/llrender/llglslshader.h
index 562cbdcba96..3857dc0519b 100644
--- a/indra/llrender/llglslshader.h
+++ b/indra/llrender/llglslshader.h
@@ -31,6 +31,8 @@
 #include "llrender.h"
 #include "llstaticstringtable.h"
 
+#pragma optimize("", off)
+
 class LLShaderFeatures
 {
 public:
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index a8f622d3ff3..9067a17bafc 100644
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -1191,6 +1191,7 @@ void LLRender::syncMatrices()
 	LLGLSLShader* shader = LLGLSLShader::sCurBoundShaderPtr;
 
 	static glh::matrix4f cached_mvp;
+    static glh::matrix4f cached_inv_mdv;
 	static U32 cached_mvp_mdv_hash = 0xFFFFFFFF;
 	static U32 cached_mvp_proj_hash = 0xFFFFFFFF;
 	
@@ -1204,12 +1205,18 @@ void LLRender::syncMatrices()
 		bool mvp_done = false;
 
 		U32 i = MM_MODELVIEW;
-		if (mMatHash[i] != shader->mMatHash[i])
+		if (mMatHash[MM_MODELVIEW] != shader->mMatHash[MM_MODELVIEW])
 		{ //update modelview, normal, and MVP
-			glh::matrix4f& mat = mMatrix[i][mMatIdx[i]];
+			glh::matrix4f& mat = mMatrix[MM_MODELVIEW][mMatIdx[MM_MODELVIEW]];
 
-			shader->uniformMatrix4fv(name[i], 1, GL_FALSE, mat.m);
-			shader->mMatHash[i] = mMatHash[i];
+            // if MDV has changed, update the cached inverse as well
+            if (cached_mvp_mdv_hash != mMatHash[MM_MODELVIEW])
+            {
+                cached_inv_mdv = mat.inverse();
+            }
+
+			shader->uniformMatrix4fv(name[MM_MODELVIEW], 1, GL_FALSE, mat.m);
+			shader->mMatHash[MM_MODELVIEW] = mMatHash[MM_MODELVIEW];
 
 			//update normal matrix
 			S32 loc = shader->getUniformLocation(LLShaderMgr::NORMAL_MATRIX);
@@ -1217,7 +1224,7 @@ void LLRender::syncMatrices()
 			{
 				if (cached_normal_hash != mMatHash[i])
 				{
-					cached_normal = mat.inverse().transpose();
+					cached_normal = cached_inv_mdv.transpose();
 					cached_normal_hash = mMatHash[i];
 				}
 
@@ -1233,6 +1240,17 @@ void LLRender::syncMatrices()
 				shader->uniformMatrix3fv(LLShaderMgr::NORMAL_MATRIX, 1, GL_FALSE, norm_mat);
 			}
 
+            if (shader->getUniformLocation(LLShaderMgr::INVERSE_MODELVIEW_MATRIX))
+            {
+                glh::matrix4f ogl_to_cfr = copy_matrix((F32*)OGL_TO_CFR_ROTATION);
+                glh::matrix4f modelview  = ogl_to_cfr.inverse() * get_current_modelview();
+
+	            glh::matrix4f inv_modelview = modelview.inverse();
+	            shader->uniformMatrix4fv(LLShaderMgr::INVERSE_MODELVIEW_MATRIX, 1, FALSE, inv_modelview.m);
+            }
+
+            shader->uniformMatrix4fv(LLShaderMgr::INVERSE_MODELVIEW_MATRIX, 1, GL_FALSE, cached_inv_mdv.m);
+
 			//update MVP matrix
 			mvp_done = true;
 			loc = shader->getUniformLocation(LLShaderMgr::MODELVIEW_PROJECTION_MATRIX);
@@ -1249,17 +1267,22 @@ void LLRender::syncMatrices()
 				}
 
 				shader->uniformMatrix4fv(LLShaderMgr::MODELVIEW_PROJECTION_MATRIX, 1, GL_FALSE, cached_mvp.m);
-			}
+			}            
 		}
 
-
 		i = MM_PROJECTION;
-		if (mMatHash[i] != shader->mMatHash[i])
+		if (mMatHash[MM_PROJECTION] != shader->mMatHash[MM_PROJECTION])
 		{ //update projection matrix, normal, and MVP
-			glh::matrix4f& mat = mMatrix[i][mMatIdx[i]];
+			glh::matrix4f& mat = mMatrix[MM_PROJECTION][mMatIdx[MM_PROJECTION]];
 
-			shader->uniformMatrix4fv(name[i], 1, GL_FALSE, mat.m);
-			shader->mMatHash[i] = mMatHash[i];
+            if (shader->getUniformLocation(LLShaderMgr::INVERSE_PROJECTION_MATRIX))
+            {
+	            glh::matrix4f inv_proj = mat.inverse();
+	            shader->uniformMatrix4fv(LLShaderMgr::INVERSE_PROJECTION_MATRIX, 1, FALSE, inv_proj.m);
+            }
+
+			shader->uniformMatrix4fv(name[MM_PROJECTION], 1, GL_FALSE, mat.m);
+			shader->mMatHash[MM_PROJECTION] = mMatHash[MM_PROJECTION];
 
 			if (!mvp_done)
 			{
@@ -1267,7 +1290,7 @@ void LLRender::syncMatrices()
 				S32 loc = shader->getUniformLocation(LLShaderMgr::MODELVIEW_PROJECTION_MATRIX);
 				if (loc > -1)
 				{
-					if (cached_mvp_mdv_hash != mMatHash[i] || cached_mvp_proj_hash != mMatHash[MM_PROJECTION])
+					if (cached_mvp_mdv_hash != mMatHash[MM_PROJECTION] || cached_mvp_proj_hash != mMatHash[MM_PROJECTION])
 					{
 						U32 mdv = MM_MODELVIEW;
 						cached_mvp = mat;
@@ -2332,3 +2355,85 @@ void LLRender::debugTexUnits(void)
 	LL_INFOS("TextureUnit") << "Active TexUnit Enabled : " << active_enabled << LL_ENDL;
 }
 
+
+
+glh::matrix4f copy_matrix(F32* src)
+{
+	glh::matrix4f ret;
+	ret.set_value(src);
+	return ret;
+}
+
+glh::matrix4f get_current_modelview()
+{
+	return copy_matrix(gGLModelView);
+}
+
+glh::matrix4f get_current_projection()
+{
+	return copy_matrix(gGLProjection);
+}
+
+glh::matrix4f get_last_modelview()
+{
+	return copy_matrix(gGLLastModelView);
+}
+
+glh::matrix4f get_last_projection()
+{
+	return copy_matrix(gGLLastProjection);
+}
+
+void copy_matrix(const glh::matrix4f& src, F32* dst)
+{
+	for (U32 i = 0; i < 16; i++)
+	{
+		dst[i] = src.m[i];
+	}
+}
+
+void set_current_modelview(const glh::matrix4f& mat)
+{
+	copy_matrix(mat, gGLModelView);
+}
+
+void set_current_projection(glh::matrix4f& mat)
+{
+	copy_matrix(mat, gGLProjection);
+}
+
+glh::matrix4f gl_ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar)
+{
+	glh::matrix4f ret(
+		2.f/(right-left), 0.f, 0.f, -(right+left)/(right-left),
+		0.f, 2.f/(top-bottom), 0.f, -(top+bottom)/(top-bottom),
+		0.f, 0.f, -2.f/(zfar-znear),  -(zfar+znear)/(zfar-znear),
+		0.f, 0.f, 0.f, 1.f);
+
+	return ret;
+}
+
+glh::matrix4f gl_perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
+{
+	GLfloat f = 1.f/tanf(DEG_TO_RAD*fovy/2.f);
+
+	return glh::matrix4f(f/aspect, 0, 0, 0,
+						 0, f, 0, 0,
+						 0, 0, (zFar+zNear)/(zNear-zFar), (2.f*zFar*zNear)/(zNear-zFar),
+						 0, 0, -1.f, 0);
+}
+
+glh::matrix4f gl_lookat(LLVector3 eye, LLVector3 center, LLVector3 up)
+{
+	LLVector3 f = center-eye;
+	f.normVec();
+	up.normVec();
+	LLVector3 s = f % up;
+	LLVector3 u = s % f;
+
+	return glh::matrix4f(s[0], s[1], s[2], 0,
+					  u[0], u[1], u[2], 0,
+					  -f[0], -f[1], -f[2], 0,
+					  0, 0, 0, 1);
+	
+}
\ No newline at end of file
diff --git a/indra/llrender/llrender.h b/indra/llrender/llrender.h
index 9ad3a6e5931..d6be5bd0d05 100644
--- a/indra/llrender/llrender.h
+++ b/indra/llrender/llrender.h
@@ -489,4 +489,24 @@ extern S32 gGLViewport[4];
 
 extern LLRender gGL;
 
+// This rotation matrix moves the default OpenGL reference frame 
+// (-Z at, Y up) to Cory's favorite reference frame (X at, Z up)
+const F32 OGL_TO_CFR_ROTATION[16] = {  0.f,  0.f, -1.f,  0.f, 	// -Z becomes X
+									  -1.f,  0.f,  0.f,  0.f, 	// -X becomes Y
+									   0.f,  1.f,  0.f,  0.f,	//  Y becomes Z
+									   0.f,  0.f,  0.f,  1.f };
+
+glh::matrix4f copy_matrix(F32* src);
+glh::matrix4f get_current_modelview();
+glh::matrix4f get_current_projection();
+glh::matrix4f get_last_modelview();
+glh::matrix4f get_last_projection();
+
+void copy_matrix(const glh::matrix4f& src, F32* dst);
+void set_current_modelview(const glh::matrix4f& mat);
+void set_current_projection(glh::matrix4f& mat);
+
+glh::matrix4f gl_ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar);
+glh::matrix4f gl_perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar);
+glh::matrix4f gl_lookat(LLVector3 eye, LLVector3 center, LLVector3 up);
 #endif
diff --git a/indra/llrender/llshadermgr.cpp b/indra/llrender/llshadermgr.cpp
index 603f26afd3d..f1ddbed9565 100644
--- a/indra/llrender/llshadermgr.cpp
+++ b/indra/llrender/llshadermgr.cpp
@@ -31,6 +31,8 @@
 #include "llfile.h"
 #include "llrender.h"
 
+#pragma optimize("", off)
+
 #if LL_DARWIN
 #include "OpenGL/OpenGL.h"
 #endif
@@ -1101,6 +1103,7 @@ void LLShaderMgr::initAttribsAndUniforms()
 	mReservedUniforms.push_back("projection_matrix");
 	mReservedUniforms.push_back("inv_proj");
 	mReservedUniforms.push_back("modelview_projection_matrix");
+    mReservedUniforms.push_back("inv_modelview");
 	mReservedUniforms.push_back("normal_matrix");
 	mReservedUniforms.push_back("texture_matrix0");
 	mReservedUniforms.push_back("texture_matrix1");
diff --git a/indra/llrender/llshadermgr.h b/indra/llrender/llshadermgr.h
index 9418849f320..fa2a9f03be4 100644
--- a/indra/llrender/llshadermgr.h
+++ b/indra/llrender/llshadermgr.h
@@ -47,6 +47,7 @@ class LLShaderMgr
 		PROJECTION_MATRIX,
 		INVERSE_PROJECTION_MATRIX,
 		MODELVIEW_PROJECTION_MATRIX,
+        INVERSE_MODELVIEW_MATRIX,
 		NORMAL_MATRIX,
 		TEXTURE_MATRIX0,
 		TEXTURE_MATRIX1,
diff --git a/indra/llrender/llvertexbuffer.cpp b/indra/llrender/llvertexbuffer.cpp
index f10301b42d0..637dedd9f6d 100644
--- a/indra/llrender/llvertexbuffer.cpp
+++ b/indra/llrender/llvertexbuffer.cpp
@@ -195,7 +195,7 @@ volatile U8* LLVBOPool::allocate(U32& name, U32 size, bool for_seed)
 		}
 		else
 		{ //always use a true hint of static draw when allocating non-client-backed buffers
-			glBufferDataARB(mType, size, 0, GL_STATIC_DRAW_ARB);
+            glBufferDataARB(mType, size, 0, GL_STATIC_DRAW_ARB);
 		}
 
 		glBindBufferARB(mType, 0);
diff --git a/indra/newview/app_settings/logcontrol.xml b/indra/newview/app_settings/logcontrol.xml
index 380d7762d6a..951c8edcc76 100644
--- a/indra/newview/app_settings/logcontrol.xml
+++ b/indra/newview/app_settings/logcontrol.xml
@@ -51,6 +51,7 @@
 					<key>tags</key>
 						<array>
 						<!-- sample entry for debugging specific items	
+						     <string>ShaderLoading</string>
 						     <string>Avatar</string>
 						     <string>Inventory</string>
 						     <string>SceneLoadTiming</string>
diff --git a/indra/newview/app_settings/settings.xml b/indra/newview/app_settings/settings.xml
index c3097589ea8..0d9042a3013 100644
--- a/indra/newview/app_settings/settings.xml
+++ b/indra/newview/app_settings/settings.xml
@@ -10143,7 +10143,7 @@
     <key>Type</key>
     <string>Boolean</string>
     <key>Value</key>
-    <integer>0</integer>
+    <integer>1</integer>
   </map>
   <key>RenderUseTriStrips</key>
   <map>
diff --git a/indra/newview/app_settings/shaders/class3/deferred/skyF.glsl b/indra/newview/app_settings/shaders/class3/deferred/skyF.glsl
index 0223f94e55f..5185a9f8f4f 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/skyF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/skyF.glsl
@@ -29,11 +29,12 @@ out vec4 frag_color;
 #define frag_color gl_FragColor
 #endif
 
+in vec3 view_pos;
 in vec3 view_dir;
 
 uniform vec3 cameraPosLocal;
-uniform vec3 sun_direction;
-uniform vec2 sun_size;
+uniform vec3 sun_dir;
+uniform float sun_size;
 
 uniform sampler2D cloud_noise_texture;
 uniform sampler2D transmittance_texture;
@@ -50,20 +51,35 @@ void main()
 {
     vec3 view_direction = normalize(view_dir);
 
+    vec3 sun_direction = sun_dir;
+
     vec3 camPos = cameraPosLocal + vec3(0, 0, 6360.0f);
     vec3 transmittance;
+    vec3 sky_illum;
     vec3 radiance = GetSkyLuminance(camPos, view_direction, 0.0f, sun_direction, transmittance);
+    vec3 radiance2 = GetSunAndSkyIlluminance(camPos, view_direction, sun_direction, sky_illum);
+
+    radiance *= transmittance;
+
+    vec3 solar_luminance = transmittance * GetSolarLuminance();
 
     // If the view ray intersects the Sun, add the Sun radiance.
-    if (dot(view_direction, sun_direction) >= sun_size.y)
+    if (dot(view_direction, sun_direction) >= sun_size)
     {
-        radiance = radiance + (transmittance * GetSolarLuminance());
+        radiance = radiance + solar_luminance;
     }
 
-    vec3 color = vec3(1.0) - exp(-radiance);
-    color = pow(color, vec3(1.0 / 2.2));
+    vec3 color = radiance;
+    
+    color = vec3(1.0) - exp(-color * 0.0001);
+
+    //float d = dot(view_direction, sun_direction);
+    //frag_color.rgb = vec3(d, d >= sun_size ? 1.0f : 0.0f, 0.0f);
 
     frag_color.rgb = color;
+    //frag_color.rgb = vec3(dot(view_direction, sun_direction) > 0.95f ? 1.0 : 0.0, 0,0);
+    frag_color.rgb = normalize(view_pos);
+
     frag_color.a = 1.0;
 }
 
diff --git a/indra/newview/app_settings/shaders/class3/deferred/skyV.glsl b/indra/newview/app_settings/shaders/class3/deferred/skyV.glsl
index 52a75953799..cf3eb658fcd 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/skyV.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/skyV.glsl
@@ -23,21 +23,29 @@
  * $/LicenseInfo$
  */
  
+uniform vec3 cameraPosLocal;
 uniform mat4 modelview_projection_matrix;
+uniform mat4 modelview_matrix;
+uniform mat4 inv_proj;
+uniform mat4 inv_modelview;
 
 ATTRIBUTE vec3 position;
 
 // Inputs
 uniform vec3 camPosLocal;
 
+out vec3 view_pos;
 out vec3 view_dir;
 
 void main()
 {
-	// World / view / projection
-	gl_Position = modelview_projection_matrix * vec4(position.xyz, 1.0);
+    // pass through untransformed fullscreen pos (clipspace)
+	gl_Position = vec4(position.xyz, 1.0);
+
+    view_pos = (inv_proj * vec4(position, 1.0f)).xyz;
 
 	// this will be normalized in the frag shader...
-	view_dir = position.xyz - camPosLocal.xyz; 
+	//view_dir = (inv_modelview * view_pos).xyz;
+    view_dir = view_pos - camPosLocal;
 }
 
diff --git a/indra/newview/app_settings/shaders/class3/windlight/advancedAtmoF.glsl b/indra/newview/app_settings/shaders/class3/windlight/advancedAtmoF.glsl
index 014ab9d11b5..fed3edf7de1 100644
--- a/indra/newview/app_settings/shaders/class3/windlight/advancedAtmoF.glsl
+++ b/indra/newview/app_settings/shaders/class3/windlight/advancedAtmoF.glsl
@@ -32,12 +32,13 @@ out vec4 frag_color;
 in vec3 view_dir;
 
 uniform vec3 cameraPosLocal;
-uniform vec3 sun_direction;
+uniform vec3 sun_dir;
 uniform float sun_size;
 
 uniform sampler2D transmittance_texture;
 uniform sampler3D scattering_texture;
 uniform sampler3D mie_scattering_texture;
+uniform sampler2D irradiance_texture;
 
 vec3 GetSolarLuminance();
 vec3 GetSkyLuminance(vec3 camPos, vec3 view_dir, float shadow_length, vec3 sun_dir, out vec3 transmittance);
@@ -50,20 +51,22 @@ void main()
 
     vec3 camPos = cameraPosLocal;
     vec3 transmittance;
-    vec3 radiance = GetSkyLuminance(camPos, view_direction, 0.0f, sun_direction, transmittance);
+    vec3 sky_illum;
+    vec3 radiance = GetSkyLuminance(camPos, view_direction, 0.0f, sun_dir, transmittance);
+    vec3 radiance2 = GetSunAndSkyIlluminance(camPos, view_direction, sun_dir, sky_illum);
 
-    radiance *= transmittance;
+    //radiance *= transmittance;
 
     // If the view ray intersects the Sun, add the Sun radiance.
-    if (dot(view_direction, sun_direction) >= sun_size)
+    if (dot(view_direction, sun_dir) >= sun_size)
     {
         radiance = radiance + transmittance * GetSolarLuminance();
     }
 
-    vec3 color = vec3(1.0) - exp(-radiance);
-    color = pow(color, vec3(1.0 / 2.2));
+    //vec3 color = vec3(1.0) - exp(-radiance);
+    //color = pow(color, vec3(1.0 / 2.2));
 
-    frag_color.rgb = color;
+    frag_color.rgb = radiance;
  
     frag_color.a = 1.0;
 }
diff --git a/indra/newview/lldrawpoolground.cpp b/indra/newview/lldrawpoolground.cpp
index 59c3fbf7a12..e5ce0413b17 100644
--- a/indra/newview/lldrawpoolground.cpp
+++ b/indra/newview/lldrawpoolground.cpp
@@ -68,7 +68,7 @@ void LLDrawPoolGround::render(S32 pass)
 
 	LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
 
-	LLGLSquashToFarClip far_clip(glh_get_current_projection());
+	LLGLSquashToFarClip far_clip(get_current_projection());
 
 	F32 water_height = gAgent.getRegion()->getWaterHeight();
 	gGL.pushMatrix();
diff --git a/indra/newview/lldrawpoolsky.cpp b/indra/newview/lldrawpoolsky.cpp
index bdb16abc78c..2e4506ce47a 100644
--- a/indra/newview/lldrawpoolsky.cpp
+++ b/indra/newview/lldrawpoolsky.cpp
@@ -102,7 +102,7 @@ void LLDrawPoolSky::render(S32 pass)
 
 	LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);
 
-	LLGLSquashToFarClip far_clip(glh_get_current_projection());
+	LLGLSquashToFarClip far_clip(get_current_projection());
 
 	LLGLEnable fog_enable( (mVertexShaderLevel < 1 && LLViewerCamera::getInstance()->cameraUnderWater()) ? GL_FOG : 0);
 
diff --git a/indra/newview/lldrawpoolwater.cpp b/indra/newview/lldrawpoolwater.cpp
index 493c619a990..bdf04bd4364 100644
--- a/indra/newview/lldrawpoolwater.cpp
+++ b/indra/newview/lldrawpoolwater.cpp
@@ -705,7 +705,7 @@ void LLDrawPoolWater::shade()
 			}
 			else
 			{
-				LLGLSquashToFarClip far_clip(glh_get_current_projection());
+				LLGLSquashToFarClip far_clip(get_current_projection());
 				face->renderIndexed();
 			}
 		}
diff --git a/indra/newview/lldrawpoolwlsky.cpp b/indra/newview/lldrawpoolwlsky.cpp
index 01d10502a1a..789e79bb313 100644
--- a/indra/newview/lldrawpoolwlsky.cpp
+++ b/indra/newview/lldrawpoolwlsky.cpp
@@ -128,17 +128,10 @@ void LLDrawPoolWLSky::endDeferredPass(S32 pass)
 
 void LLDrawPoolWLSky::renderFsSky(const LLVector3& camPosLocal, F32 camHeightLocal, LLGLSLShader * shader) const
 {
-    gGL.pushMatrix();
-    gGL.matrixMode(LLRender::MM_PROJECTION);	
-	gGL.loadIdentity();
-	gGL.matrixMode(LLRender::MM_MODELVIEW);
-	gGL.loadIdentity();
-
     // Draw WL Sky	w/ normal cam pos (where you are) for adv atmo sky
+    //gGL.syncMatrices();
     sky_shader->uniform3f(sCamPosLocal, camPosLocal.mV[0], camPosLocal.mV[1], camPosLocal.mV[2]);
     gSky.mVOWLSkyp->drawFsSky();
-
-    gGL.popMatrix();
 }
 
 void LLDrawPoolWLSky::renderDome(const LLVector3& camPosLocal, F32 camHeightLocal, LLGLSLShader * shader) const
@@ -174,54 +167,63 @@ void LLDrawPoolWLSky::renderDome(const LLVector3& camPosLocal, F32 camHeightLoca
 	gGL.popMatrix();
 }
 
-void LLDrawPoolWLSky::renderSkyHaze(const LLVector3& camPosLocal, F32 camHeightLocal) const
+void LLDrawPoolWLSky::renderSkyHazeDeferred(const LLVector3& camPosLocal, F32 camHeightLocal) const
 {
-	if (gPipeline.canUseWindLightShaders() && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
-	{
-		LLGLDisable blend(GL_BLEND);
-
+    if (gPipeline.useAdvancedAtmospherics() && gPipeline.canUseWindLightShaders() && gAtmosphere)
+    {
 		sky_shader->bind();
 
-        LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
+        // bind precomputed textures necessary for calculating sun and sky luminance
+        sky_shader->bindTexture(LLShaderMgr::TRANSMITTANCE_TEX, gAtmosphere->getTransmittance());
+        sky_shader->bindTexture(LLShaderMgr::SCATTER_TEX, gAtmosphere->getScattering());
+        sky_shader->bindTexture(LLShaderMgr::SINGLE_MIE_SCATTER_TEX, gAtmosphere->getMieScattering());
+        sky_shader->bindTexture(LLShaderMgr::ILLUMINANCE_TEX, gAtmosphere->getIlluminance());
 
-        if (gPipeline.useAdvancedAtmospherics() && gPipeline.canUseWindLightShaders() && gAtmosphere)
-        {
-            // bind precomputed textures necessary for calculating sun and sky luminance
-            sky_shader->bindTexture(LLShaderMgr::TRANSMITTANCE_TEX, gAtmosphere->getTransmittance());
-            sky_shader->bindTexture(LLShaderMgr::SCATTER_TEX, gAtmosphere->getScattering());
-            sky_shader->bindTexture(LLShaderMgr::SINGLE_MIE_SCATTER_TEX, gAtmosphere->getMieScattering());
+        static float sunSize = (float)cos(0.0005);
 
-            static float sunSize = (float)cos(0.0005);
+        sky_shader->uniform1f(LLShaderMgr::SUN_SIZE, sunSize);
 
-            sky_shader->uniform1f(LLShaderMgr::SUN_SIZE, sunSize);
+        static LLVector3 solDir(-0.935f, 0.23f, 0.27f);
 
-            static LLVector3 solDir(0.7f, 0.2f, 0.2f);
+        static bool fooA = false;
+        static bool fooB = false;
 
-            //neither of these appear to track with the env settings, would the real sun please stand up.
-            //sky_shader->uniform3fv(LLShaderMgr::DEFERRED_SUN_DIR, 1, gPipeline.mTransformedSunDir.mV);
-            //sky_shader->uniform3fv(LLShaderMgr::DEFERRED_SUN_DIR, 1, gSky.mVOSkyp->getSun().getDirection().mV);
-            solDir.normalize();
+        //neither of these appear to track with the env settings, would the real sun please stand up.
+        if (fooA) solDir = gPipeline.mTransformedSunDir;
+        if (fooB) solDir = gSky.mVOSkyp->getSun().getDirection();
 
-            sky_shader->uniform3fv(LLShaderMgr::DEFERRED_SUN_DIR, 1, solDir.mV);
+        solDir.normalize();
 
-            // clouds are rendered along with sky in adv atmo
-            if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS) && sCloudNoiseTexture.notNull())
-            {
-                sky_shader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP, sCloudNoiseTexture);
-            }
+        sky_shader->uniform3fv(LLShaderMgr::DEFERRED_SUN_DIR, 1, solDir.mV);
 
-            renderFsSky(origin, camHeightLocal, sky_shader);
-        }
-        else
+        // clouds are rendered along with sky in adv atmo
+        if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_CLOUDS) && sCloudNoiseTexture.notNull())
         {
-		    /// Render the skydome
-		    renderDome(origin, camHeightLocal, sky_shader);	
+            sky_shader->bindTexture(LLShaderMgr::CLOUD_NOISE_MAP, sCloudNoiseTexture);
         }
 
+        renderFsSky(camPosLocal, camHeightLocal, sky_shader);
+
 		sky_shader->unbind();
 	}
 }
 
+void LLDrawPoolWLSky::renderSkyHaze(const LLVector3& camPosLocal, F32 camHeightLocal) const
+{
+    LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
+
+	if (gPipeline.canUseWindLightShaders() && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
+	{
+        LLGLDisable blend(GL_BLEND);
+        sky_shader->bind();
+
+        /// Render the skydome
+        renderDome(origin, camHeightLocal, sky_shader);	
+
+		sky_shader->unbind();
+    }
+}
+
 void LLDrawPoolWLSky::renderStars(void) const
 {
 	LLGLSPipelineSkyBox gls_sky;
@@ -367,18 +369,17 @@ void LLDrawPoolWLSky::renderDeferred(S32 pass)
 
 	gGL.setColorMask(true, false);
 
-	LLGLSquashToFarClip far_clip(glh_get_current_projection());
+	LLGLSquashToFarClip far_clip(get_current_projection());
 
     LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
 
-	renderSkyHaze(origin, camHeightLocal);
+	renderSkyHazeDeferred(origin, camHeightLocal);
 
-    if (!gPipeline.useAdvancedAtmospherics() && gPipeline.canUseWindLightShaders())
+    if (gPipeline.canUseWindLightShaders())
     {
 	    LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
 	    gGL.pushMatrix();
 
-		
 		gGL.translatef(origin.mV[0], origin.mV[1], origin.mV[2]);
 
 		gDeferredStarProgram.bind();
@@ -415,7 +416,7 @@ void LLDrawPoolWLSky::render(S32 pass)
 	LLGLDepthTest depth(GL_TRUE, GL_FALSE);
 	LLGLDisable clip(GL_CLIP_PLANE0);
 
-	LLGLSquashToFarClip far_clip(glh_get_current_projection());
+	LLGLSquashToFarClip far_clip(get_current_projection());
 
     LLVector3 const & origin = LLViewerCamera::getInstance()->getOrigin();
 
diff --git a/indra/newview/lldrawpoolwlsky.h b/indra/newview/lldrawpoolwlsky.h
index 8d4a749a9c8..2beb2867db4 100644
--- a/indra/newview/lldrawpoolwlsky.h
+++ b/indra/newview/lldrawpoolwlsky.h
@@ -76,6 +76,8 @@ class LLDrawPoolWLSky : public LLDrawPool {
     void renderFsSky(const LLVector3& camPosLocal, F32 camHeightLocal, LLGLSLShader * shader) const;
 	void renderDome(const LLVector3& camPosLocal, F32 camHeightLocal, LLGLSLShader * shader) const;
 	void renderSkyHaze(const LLVector3& camPosLocal, F32 camHeightLocal) const;
+    void renderSkyHazeDeferred(const LLVector3& camPosLocal, F32 camHeightLocal) const;
+
 	void renderStars(void) const;
 	void renderSkyClouds(const LLVector3& camPosLocal, F32 camHeightLocal) const;
 	void renderHeavenlyBodies();
diff --git a/indra/newview/llglsandbox.cpp b/indra/newview/llglsandbox.cpp
index 63270e13fe5..5111bbb0663 100644
--- a/indra/newview/llglsandbox.cpp
+++ b/indra/newview/llglsandbox.cpp
@@ -1060,7 +1060,7 @@ F32 gpu_benchmark()
     delete [] pixels;
 
 	//make a dummy triangle to draw with
-	LLPointer<LLVertexBuffer> buff = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD0, GL_STATIC_DRAW_ARB);
+	LLPointer<LLVertexBuffer> buff = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_TEXCOORD0, GL_STREAM_DRAW_ARB);
 
 	if (!buff->allocateBuffer(3, 0, true))
 	{
diff --git a/indra/newview/llpanelprimmediacontrols.cpp b/indra/newview/llpanelprimmediacontrols.cpp
index 5f413fc3c0d..3d3dccee0bf 100644
--- a/indra/newview/llpanelprimmediacontrols.cpp
+++ b/indra/newview/llpanelprimmediacontrols.cpp
@@ -64,8 +64,8 @@
 #include "llvector4a.h"
 
 // Functions pulled from pipeline.cpp
-glh::matrix4f glh_get_current_modelview();
-glh::matrix4f glh_get_current_projection();
+glh::matrix4f get_current_modelview();
+glh::matrix4f get_current_projection();
 // Functions pulled from llviewerdisplay.cpp
 bool get_hud_matrices(glh::matrix4f &proj, glh::matrix4f &model);
 
@@ -642,7 +642,7 @@ void LLPanelPrimMediaControls::updateShape()
 		glh::matrix4f mat;
 		if (!is_hud) 
 		{
-			mat = glh_get_current_projection() * glh_get_current_modelview();
+			mat = get_current_projection() * get_current_modelview();
 		}
 		else {
 			glh::matrix4f proj, modelview;
diff --git a/indra/newview/llviewercamera.cpp b/indra/newview/llviewercamera.cpp
index 57a0195d23e..de92e68032b 100644
--- a/indra/newview/llviewercamera.cpp
+++ b/indra/newview/llviewercamera.cpp
@@ -81,31 +81,6 @@ glh::matrix4f gl_pick_matrix(GLfloat x, GLfloat y, GLfloat width, GLfloat height
 	return glh::matrix4f(m);
 }
 
-glh::matrix4f gl_perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
-{
-	GLfloat f = 1.f/tanf(DEG_TO_RAD*fovy/2.f);
-
-	return glh::matrix4f(f/aspect, 0, 0, 0,
-						 0, f, 0, 0,
-						 0, 0, (zFar+zNear)/(zNear-zFar), (2.f*zFar*zNear)/(zNear-zFar),
-						 0, 0, -1.f, 0);
-}
-
-glh::matrix4f gl_lookat(LLVector3 eye, LLVector3 center, LLVector3 up)
-{
-	LLVector3 f = center-eye;
-	f.normVec();
-	up.normVec();
-	LLVector3 s = f % up;
-	LLVector3 u = s % f;
-
-	return glh::matrix4f(s[0], s[1], s[2], 0,
-					  u[0], u[1], u[2], 0,
-					  -f[0], -f[1], -f[2], 0,
-					  0, 0, 0, 1);
-	
-}
-
 // Build time optimization, generate this once in .cpp file
 template class LLViewerCamera* LLSingleton<class LLViewerCamera>::getInstance();
 
diff --git a/indra/newview/llviewercamera.h b/indra/newview/llviewercamera.h
index f8c973690a0..a5216b5275c 100644
--- a/indra/newview/llviewercamera.h
+++ b/indra/newview/llviewercamera.h
@@ -35,14 +35,6 @@
 #include "lltrace.h"
 
 class LLViewerObject;
-
-// This rotation matrix moves the default OpenGL reference frame 
-// (-Z at, Y up) to Cory's favorite reference frame (X at, Z up)
-const F32 OGL_TO_CFR_ROTATION[16] = {  0.f,  0.f, -1.f,  0.f, 	// -Z becomes X
-									  -1.f,  0.f,  0.f,  0.f, 	// -X becomes Y
-									   0.f,  1.f,  0.f,  0.f,	//  Y becomes Z
-									   0.f,  0.f,  0.f,  1.f };
-
 const BOOL FOR_SELECTION = TRUE;
 const BOOL NOT_FOR_SELECTION = FALSE;
 
diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp
index 950243949f0..d1b6decb0d2 100644
--- a/indra/newview/llviewerdisplay.cpp
+++ b/indra/newview/llviewerdisplay.cpp
@@ -729,8 +729,8 @@ void display(BOOL rebuild, F32 zoom_factor, int subfield, BOOL for_snapshot)
 				LLGLState::checkTextureChannels();
 				LLGLState::checkClientArrays();
 
-				glh::matrix4f proj = glh_get_current_projection();
-				glh::matrix4f mod = glh_get_current_modelview();
+				glh::matrix4f proj = get_current_projection();
+				glh::matrix4f mod = get_current_modelview();
 				glViewport(0,0,512,512);
 				LLVOAvatar::updateFreezeCounter() ;
 
@@ -739,8 +739,8 @@ void display(BOOL rebuild, F32 zoom_factor, int subfield, BOOL for_snapshot)
 					LLVOAvatar::updateImpostors();
 				}
 
-				glh_set_current_projection(proj);
-				glh_set_current_modelview(mod);
+				set_current_projection(proj);
+				set_current_modelview(mod);
 				gGL.matrixMode(LLRender::MM_PROJECTION);
 				gGL.loadMatrix(proj.m);
 				gGL.matrixMode(LLRender::MM_MODELVIEW);
@@ -1078,8 +1078,8 @@ void render_hud_attachments()
 	gGL.matrixMode(LLRender::MM_MODELVIEW);
 	gGL.pushMatrix();
 		
-	glh::matrix4f current_proj = glh_get_current_projection();
-	glh::matrix4f current_mod = glh_get_current_modelview();
+	glh::matrix4f current_proj = get_current_projection();
+	glh::matrix4f current_mod = get_current_modelview();
 
 	// clamp target zoom level to reasonable values
 	gAgentCamera.mHUDTargetZoom = llclamp(gAgentCamera.mHUDTargetZoom, 0.1f, 1.f);
@@ -1171,8 +1171,8 @@ void render_hud_attachments()
 	gGL.matrixMode(LLRender::MM_MODELVIEW);
 	gGL.popMatrix();
 	
-	glh_set_current_projection(current_proj);
-	glh_set_current_modelview(current_mod);
+	set_current_projection(current_proj);
+	set_current_modelview(current_mod);
 }
 
 LLRect get_whole_screen_region()
@@ -1254,11 +1254,11 @@ bool setup_hud_matrices(const LLRect& screen_region)
 	// set up transform to keep HUD objects in front of camera
 	gGL.matrixMode(LLRender::MM_PROJECTION);
 	gGL.loadMatrix(proj.m);
-	glh_set_current_projection(proj);
+	set_current_projection(proj);
 	
 	gGL.matrixMode(LLRender::MM_MODELVIEW);
 	gGL.loadMatrix(model.m);
-	glh_set_current_modelview(model);
+	set_current_modelview(model);
 	return TRUE;
 }
 
@@ -1266,13 +1266,13 @@ void render_ui(F32 zoom_factor, int subfield)
 {
 	LLGLState::checkStates();
 	
-	glh::matrix4f saved_view = glh_get_current_modelview();
+	glh::matrix4f saved_view = get_current_modelview();
 
 	if (!gSnapshot)
 	{
 		gGL.pushMatrix();
 		gGL.loadMatrix(gGLLastModelView);
-		glh_set_current_modelview(glh_copy_matrix(gGLLastModelView));
+		set_current_modelview(copy_matrix(gGLLastModelView));
 	}
 	
 	if(LLSceneMonitor::getInstance()->needsUpdate())
@@ -1335,7 +1335,7 @@ void render_ui(F32 zoom_factor, int subfield)
 
 	if (!gSnapshot)
 	{
-		glh_set_current_modelview(saved_view);
+		set_current_modelview(saved_view);
 		gGL.popMatrix();
 	}
 }
diff --git a/indra/newview/llvieweroctree.cpp b/indra/newview/llvieweroctree.cpp
index 023f1b92ba5..56e6103f673 100644
--- a/indra/newview/llvieweroctree.cpp
+++ b/indra/newview/llvieweroctree.cpp
@@ -1272,7 +1272,7 @@ void LLOcclusionCullingGroup::doOcclusion(LLCamera* camera, const LLVector4a* sh
 						{
 							LL_RECORD_BLOCK_TIME(FTM_OCCLUSION_DRAW_WATER);
 
-							LLGLSquashToFarClip squash(glh_get_current_projection(), 1);
+							LLGLSquashToFarClip squash(get_current_projection(), 1);
 							if (camera->getOrigin().isExactlyZero())
 							{ //origin is invalid, draw entire box
 								gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8, 0);
diff --git a/indra/newview/llviewershadermgr.cpp b/indra/newview/llviewershadermgr.cpp
index 4db039ace32..95e4f27a394 100644
--- a/indra/newview/llviewershadermgr.cpp
+++ b/indra/newview/llviewershadermgr.cpp
@@ -2091,6 +2091,31 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
 		success = gDeferredPostNoDoFProgram.createShader(NULL, NULL);
 	}
 
+    // this shader uses gather so it can't live with the other basic shaders safely
+	/*if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] >= 3))
+	{
+		gDownsampleMinMaxDepthRectProgram.mName = "DownsampleMinMaxDepthRect Shader";
+		gDownsampleMinMaxDepthRectProgram.mShaderFiles.clear();
+		gDownsampleMinMaxDepthRectProgram.mShaderFiles.push_back(make_pair("windlight/downsampleMinMaxDepthV.glsl", GL_VERTEX_SHADER_ARB));
+		gDownsampleMinMaxDepthRectProgram.mShaderFiles.push_back(make_pair("windlight/downsampleMinMaxDepthRectF.glsl", GL_FRAGMENT_SHADER_ARB));
+		gDownsampleMinMaxDepthRectProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];
+		success = gDownsampleMinMaxDepthRectProgram.createShader(NULL, NULL);
+	}*/
+
+    // this shader uses gather so it can't live with the other basic shaders safely
+    /*if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] >= 3))
+    {
+        gInscatterRectProgram.mName = "Inscatter Shader";
+        gInscatterRectProgram.mShaderFiles.clear();
+        gInscatterRectProgram.mShaderFiles.push_back(make_pair("windlight/advancedAtmoV.glsl", GL_VERTEX_SHADER_ARB));
+        gInscatterRectProgram.mShaderFiles.push_back(make_pair("windlight/advancedAtmoF.glsl", GL_FRAGMENT_SHADER_ARB));
+        gInscatterRectProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];       
+        llassert(gAtmosphere != nullptr);
+        gInscatterRectProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
+        success = gInscatterRectProgram.createShader(NULL, NULL);
+        llassert(success);
+    }*/
+
 	if (success)
 	{
 		gDeferredWLSkyProgram.mName = "Deferred Windlight Sky Shader";
@@ -2105,9 +2130,10 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
             gDeferredWLSkyProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
         }
 		success = gDeferredWLSkyProgram.createShader(NULL, NULL);
+        llassert(success);
 	}
 
-    if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] < 3))
+    if (success)
 	{
 		gDeferredWLCloudProgram.mName = "Deferred Windlight Cloud Program";
 		gDeferredWLCloudProgram.mShaderFiles.clear();
@@ -2115,7 +2141,12 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
 		gDeferredWLCloudProgram.mShaderFiles.push_back(make_pair("deferred/cloudsF.glsl", GL_FRAGMENT_SHADER_ARB));
 		gDeferredWLCloudProgram.mShaderLevel = mVertexShaderLevel[SHADER_DEFERRED];
 		gDeferredWLCloudProgram.mShaderGroup = LLGLSLShader::SG_SKY;
+        if (mVertexShaderLevel[SHADER_WINDLIGHT] >= 3)
+        {
+            gDeferredWLSkyProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
+        }
 		success = gDeferredWLCloudProgram.createShader(NULL, NULL);
+        llassert(success);
 	}
 
 	if (success)
@@ -3459,31 +3490,7 @@ BOOL LLViewerShaderMgr::loadShadersWindLight()
         LLAtmosphere::initClass();
     }
 
-	// this shader uses gather so it can't live with the other basic shaders safely
-	/*if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] >= 3))
-	{
-		gDownsampleMinMaxDepthRectProgram.mName = "DownsampleMinMaxDepthRect Shader";
-		gDownsampleMinMaxDepthRectProgram.mShaderFiles.clear();
-		gDownsampleMinMaxDepthRectProgram.mShaderFiles.push_back(make_pair("windlight/downsampleMinMaxDepthV.glsl", GL_VERTEX_SHADER_ARB));
-		gDownsampleMinMaxDepthRectProgram.mShaderFiles.push_back(make_pair("windlight/downsampleMinMaxDepthRectF.glsl", GL_FRAGMENT_SHADER_ARB));
-		gDownsampleMinMaxDepthRectProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];
-		success = gDownsampleMinMaxDepthRectProgram.createShader(NULL, NULL);
-	}*/
-
-    // this shader uses gather so it can't live with the other basic shaders safely
-    if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] >= 3))
-    {
-        gInscatterRectProgram.mName = "Inscatter Shader";
-        gInscatterRectProgram.mShaderFiles.clear();
-        gInscatterRectProgram.mShaderFiles.push_back(make_pair("windlight/advancedAtmoV.glsl", GL_VERTEX_SHADER_ARB));
-        gInscatterRectProgram.mShaderFiles.push_back(make_pair("windlight/advancedAtmoF.glsl", GL_FRAGMENT_SHADER_ARB));
-        gInscatterRectProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];       
-        llassert(gAtmosphere != nullptr);
-        gInscatterRectProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
-        success = gInscatterRectProgram.createShader(NULL, NULL);
-    }
-
-	if (success)
+	if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] < 3))
 	{
 		gWLSkyProgram.mName = "Windlight Sky Shader";
 		//gWLSkyProgram.mFeatures.hasGamma = true;
@@ -3492,12 +3499,10 @@ BOOL LLViewerShaderMgr::loadShadersWindLight()
 		gWLSkyProgram.mShaderFiles.push_back(make_pair("windlight/skyF.glsl", GL_FRAGMENT_SHADER_ARB));
 		gWLSkyProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];
 		gWLSkyProgram.mShaderGroup = LLGLSLShader::SG_SKY;
-		if (gAtmosphere != nullptr)
-		gWLSkyProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
 		success = gWLSkyProgram.createShader(NULL, NULL);
 	}
 
-	if (success)
+	if (success && (mVertexShaderLevel[SHADER_WINDLIGHT] < 3))
 	{
 		gWLCloudProgram.mName = "Windlight Cloud Program";
 		//gWLCloudProgram.mFeatures.hasGamma = true;
@@ -3506,8 +3511,6 @@ BOOL LLViewerShaderMgr::loadShadersWindLight()
 		gWLCloudProgram.mShaderFiles.push_back(make_pair("windlight/cloudsF.glsl", GL_FRAGMENT_SHADER_ARB));
 		gWLCloudProgram.mShaderLevel = mVertexShaderLevel[SHADER_WINDLIGHT];
 		gWLCloudProgram.mShaderGroup = LLGLSLShader::SG_SKY;
-		if (gAtmosphere != nullptr)
-		gWLCloudProgram.mExtraLinkObject = gAtmosphere->getAtmosphericShaderForLink();
 		success = gWLCloudProgram.createShader(NULL, NULL);
 	}
 
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 4f34f58b760..6ad47386c77 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -298,62 +298,6 @@ void drawBoxOutline(const LLVector3& pos, const LLVector3& size);
 U32 nhpo2(U32 v);
 LLVertexBuffer* ll_create_cube_vb(U32 type_mask, U32 usage);
 
-glh::matrix4f glh_copy_matrix(F32* src)
-{
-	glh::matrix4f ret;
-	ret.set_value(src);
-	return ret;
-}
-
-glh::matrix4f glh_get_current_modelview()
-{
-	return glh_copy_matrix(gGLModelView);
-}
-
-glh::matrix4f glh_get_current_projection()
-{
-	return glh_copy_matrix(gGLProjection);
-}
-
-glh::matrix4f glh_get_last_modelview()
-{
-	return glh_copy_matrix(gGLLastModelView);
-}
-
-glh::matrix4f glh_get_last_projection()
-{
-	return glh_copy_matrix(gGLLastProjection);
-}
-
-void glh_copy_matrix(const glh::matrix4f& src, F32* dst)
-{
-	for (U32 i = 0; i < 16; i++)
-	{
-		dst[i] = src.m[i];
-	}
-}
-
-void glh_set_current_modelview(const glh::matrix4f& mat)
-{
-	glh_copy_matrix(mat, gGLModelView);
-}
-
-void glh_set_current_projection(glh::matrix4f& mat)
-{
-	glh_copy_matrix(mat, gGLProjection);
-}
-
-glh::matrix4f gl_ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar)
-{
-	glh::matrix4f ret(
-		2.f/(right-left), 0.f, 0.f, -(right+left)/(right-left),
-		0.f, 2.f/(top-bottom), 0.f, -(top+bottom)/(top-bottom),
-		0.f, 0.f, -2.f/(zfar-znear),  -(zfar+znear)/(zfar-znear),
-		0.f, 0.f, 0.f, 1.f);
-
-	return ret;
-}
-
 void display_update_camera();
 //----------------------------------------
 
@@ -1445,9 +1389,9 @@ bool LLPipeline::canUseVertexShaders()
 
 bool LLPipeline::canUseWindLightShaders() const
 {
-	return (!LLPipeline::sDisableShaders &&
-			gWLSkyProgram.mProgramObject != 0 &&
-			LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_WINDLIGHT) > 1);
+    bool usingWindlight = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_WINDLIGHT) > 1;
+    bool haveShaders    = ((gWLSkyProgram.mProgramObject != 0) || (gDeferredWLSkyProgram.mProgramObject != 0));
+	return (!LLPipeline::sDisableShaders && haveShaders && usingWindlight);
 }
 
 bool LLPipeline::canUseWindLightShadersOnObjects() const
@@ -2483,8 +2427,8 @@ void LLPipeline::updateCull(LLCamera& camera, LLCullResult& result, S32 water_cl
 		}
 	}
 	
-	glh::matrix4f modelview = glh_get_last_modelview();
-	glh::matrix4f proj = glh_get_last_projection();
+	glh::matrix4f modelview = get_last_modelview();
+	glh::matrix4f proj = get_last_projection();
 	LLGLUserClipPlane clip(plane, modelview, proj, water_clip != 0 && LLPipeline::sReflectionRender);
 
 	LLGLDepthTest depth(GL_TRUE, GL_FALSE);
@@ -8269,7 +8213,7 @@ void LLPipeline::bindDeferredShader(LLGLSLShader& shader, U32 light_index, U32 n
 
 		stop_glerror();
 
-		glh::matrix4f projection = glh_get_current_projection();
+		glh::matrix4f projection = get_current_projection();
 		glh::matrix4f inv_proj = projection.inverse();
 		
 		shader.uniformMatrix4fv(LLShaderMgr::INVERSE_PROJECTION_MATRIX, 1, FALSE, inv_proj.m);
@@ -8443,7 +8387,7 @@ void LLPipeline::bindDeferredShader(LLGLSLShader& shader, U32 light_index, U32 n
 
 	if (shader.getUniformLocation(LLShaderMgr::DEFERRED_NORM_MATRIX) >= 0)
 	{
-		glh::matrix4f norm_mat = glh_get_current_modelview().inverse().transpose();
+		glh::matrix4f norm_mat = get_current_modelview().inverse().transpose();
 		shader.uniformMatrix4fv(LLShaderMgr::DEFERRED_NORM_MATRIX, 1, FALSE, norm_mat.m);
 	}
 }
@@ -8513,7 +8457,7 @@ void LLPipeline::renderDeferredLighting()
 		LLGLEnable cull(GL_CULL_FACE);
 		LLGLEnable blend(GL_BLEND);
 
-		glh::matrix4f mat = glh_copy_matrix(gGLModelView);
+		glh::matrix4f mat = copy_matrix(gGLModelView);
 
 		LLStrider<LLVector3> vert; 
 		mDeferredVB->getVertexStrider(vert);
@@ -8547,7 +8491,7 @@ void LLPipeline::renderDeferredLighting()
 				mDeferredLight.clear(GL_COLOR_BUFFER_BIT);
 				glClearColor(0,0,0,0);
 
-				glh::matrix4f inv_trans = glh_get_current_modelview().inverse().transpose();
+				glh::matrix4f inv_trans = get_current_modelview().inverse().transpose();
 
 				const U32 slice = 32;
 				F32 offset[slice*3];
@@ -9127,7 +9071,7 @@ void LLPipeline::renderDeferredLightingToRT(LLRenderTarget* target)
 		LLGLEnable cull(GL_CULL_FACE);
 		LLGLEnable blend(GL_BLEND);
 
-		glh::matrix4f mat = glh_copy_matrix(gGLModelView);
+		glh::matrix4f mat = copy_matrix(gGLModelView);
 
 		LLStrider<LLVector3> vert; 
 		mDeferredVB->getVertexStrider(vert);
@@ -9161,7 +9105,7 @@ void LLPipeline::renderDeferredLightingToRT(LLRenderTarget* target)
 				mDeferredLight.clear(GL_COLOR_BUFFER_BIT);
 				glClearColor(0,0,0,0);
 
-				glh::matrix4f inv_trans = glh_get_current_modelview().inverse().transpose();
+				glh::matrix4f inv_trans = get_current_modelview().inverse().transpose();
 
 				const U32 slice = 32;
 				F32 offset[slice*3];
@@ -9650,7 +9594,7 @@ void LLPipeline::setupSpotLight(LLGLSLShader& shader, LLDrawable* drawablep)
 	LLMatrix4 light_mat(quat, LLVector4(origin,1.f));
 
 	glh::matrix4f light_to_agent((F32*) light_mat.mMatrix);
-	glh::matrix4f light_to_screen = glh_get_current_modelview() * light_to_agent;
+	glh::matrix4f light_to_screen = get_current_modelview() * light_to_agent;
 
 	glh::matrix4f screen_to_light = light_to_screen.inverse();
 
@@ -9837,7 +9781,7 @@ void LLPipeline::generateWaterReflection(LLCamera& camera_in)
 		
 		gPipeline.pushRenderTypeMask();
 
-		glh::matrix4f projection = glh_get_current_projection();
+		glh::matrix4f projection = get_current_projection();
 		glh::matrix4f mat;
 
 		stop_glerror();
@@ -9898,11 +9842,11 @@ void LLPipeline::generateWaterReflection(LLCamera& camera_in)
 			mat.set_scale(glh::vec3f(1,1,-1));
 			mat.set_translate(glh::vec3f(0,0,height*2.f));
 
-			glh::matrix4f current = glh_get_current_modelview();
+			glh::matrix4f current = get_current_modelview();
 
 			mat = current * mat;
 
-			glh_set_current_modelview(mat);
+			set_current_modelview(mat);
 			gGL.loadMatrix(mat.m);
 
 			LLViewerCamera::updateFrustumPlanes(camera, FALSE, TRUE);
@@ -10013,7 +9957,7 @@ void LLPipeline::generateWaterReflection(LLCamera& camera_in)
 			glCullFace(GL_BACK);
 			gGL.popMatrix();
 			mWaterRef.flush();
-			glh_set_current_modelview(current);
+			set_current_modelview(current);
 			LLPipeline::sUseOcclusion = occlusion;
 		}
 
@@ -10053,7 +9997,7 @@ void LLPipeline::generateWaterReflection(LLCamera& camera_in)
 			if (!LLPipeline::sUnderWaterRender || LLDrawPoolWater::sNeedsReflectionUpdate)
 			{
 				//clip out geometry on the same side of water as the camera
-				mat = glh_get_current_modelview();
+				mat = get_current_modelview();
 				LLPlane plane(-pnorm, -(pd+pad));
 
 				LLGLUserClipPlane clip_plane(plane, mat, projection);
@@ -10686,8 +10630,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 	//get sun view matrix
 	
 	//store current projection/modelview matrix
-	glh::matrix4f saved_proj = glh_get_current_projection();
-	glh::matrix4f saved_view = glh_get_current_modelview();
+	glh::matrix4f saved_proj = get_current_projection();
+	glh::matrix4f saved_view = get_current_modelview();
 	glh::matrix4f inv_view = saved_view.inverse();
 
 	glh::matrix4f view[6];
@@ -10844,8 +10788,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 			LLViewerCamera::sCurCameraID = (LLViewerCamera::eCameraID)(LLViewerCamera::CAMERA_SHADOW0+j);
 
 			//restore render matrices
-			glh_set_current_modelview(saved_view);
-			glh_set_current_projection(saved_proj);
+			set_current_modelview(saved_view);
+			set_current_projection(saved_proj);
 
 			LLVector3 eye = camera.getOrigin();
 
@@ -11158,8 +11102,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 
 			shadow_cam.setOrigin(0,0,0);
 
-			glh_set_current_modelview(view[j]);
-			glh_set_current_projection(proj[j]);
+			set_current_modelview(view[j]);
+			set_current_projection(proj[j]);
 
 			LLViewerCamera::updateFrustumPlanes(shadow_cam, FALSE, FALSE, TRUE);
 
@@ -11172,8 +11116,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 							0.f, 0.f, 0.5f, 0.5f,
 							0.f, 0.f, 0.f, 1.f);
 
-			glh_set_current_modelview(view[j]);
-			glh_set_current_projection(proj[j]);
+			set_current_modelview(view[j]);
+			set_current_projection(proj[j]);
 
 			for (U32 i = 0; i < 16; i++)
 			{
@@ -11252,8 +11196,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 
 		for (S32 i = 0; i < 2; i++)
 		{
-			glh_set_current_modelview(saved_view);
-			glh_set_current_projection(saved_proj);
+			set_current_modelview(saved_view);
+			set_current_projection(saved_proj);
 
 			if (mShadowSpotLight[i].isNull())
 			{
@@ -11313,8 +11257,8 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 							0.f, 0.f, 0.5f, 0.5f,
 							0.f, 0.f, 0.f, 1.f);
 
-			glh_set_current_modelview(view[i+4]);
-			glh_set_current_projection(proj[i+4]);
+			set_current_modelview(view[i+4]);
+			set_current_projection(proj[i+4]);
 
 			mSunShadowMatrix[i+4] = trans*proj[i+4]*view[i+4]*inv_view;
 			
@@ -11358,13 +11302,13 @@ void LLPipeline::generateSunShadow(LLCamera& camera)
 
 	if (!CameraOffset)
 	{
-		glh_set_current_modelview(saved_view);
-		glh_set_current_projection(saved_proj);
+		set_current_modelview(saved_view);
+		set_current_projection(saved_proj);
 	}
 	else
 	{
-		glh_set_current_modelview(view[1]);
-		glh_set_current_projection(proj[1]);
+		set_current_modelview(view[1]);
+		set_current_projection(proj[1]);
 		gGL.loadMatrix(view[1].m);
 		gGL.matrixMode(LLRender::MM_PROJECTION);
 		gGL.loadMatrix(proj[1].m);
@@ -11542,7 +11486,7 @@ void LLPipeline::generateImpostor(LLVOAvatar* avatar)
 		F32 fov = atanf(tdim.mV[1]/distance)*2.f*RAD_TO_DEG;
 		F32 aspect = tdim.mV[0]/tdim.mV[1];
 		glh::matrix4f persp = gl_perspective(fov, aspect, 1.f, 256.f);
-		glh_set_current_projection(persp);
+		set_current_projection(persp);
 		gGL.loadMatrix(persp.m);
 
 		gGL.matrixMode(LLRender::MM_MODELVIEW);
@@ -11553,7 +11497,7 @@ void LLPipeline::generateImpostor(LLVOAvatar* avatar)
 		mat = glh::matrix4f((GLfloat*) OGL_TO_CFR_ROTATION) * mat;
 
 		gGL.loadMatrix(mat.m);
-		glh_set_current_modelview(mat);
+		set_current_modelview(mat);
 
 		glClearColor(0.0f,0.0f,0.0f,0.0f);
 		gGL.setColorMask(true, true);
diff --git a/indra/newview/pipeline.h b/indra/newview/pipeline.h
index 6023a41ca28..75fed7dd397 100644
--- a/indra/newview/pipeline.h
+++ b/indra/newview/pipeline.h
@@ -61,14 +61,7 @@ bool compute_min_max(LLMatrix4& box, LLVector2& min, LLVector2& max); // Shouldn
 bool LLRayAABB(const LLVector3 &center, const LLVector3 &size, const LLVector3& origin, const LLVector3& dir, LLVector3 &coord, F32 epsilon = 0);
 bool setup_hud_matrices(); // use whole screen to render hud
 bool setup_hud_matrices(const LLRect& screen_region); // specify portion of screen (in pixels) to render hud attachments from (for picking)
-glh::matrix4f glh_copy_matrix(F32* src);
-glh::matrix4f glh_get_current_modelview();
-void glh_set_current_modelview(const glh::matrix4f& mat);
-glh::matrix4f glh_get_current_projection();
-void glh_set_current_projection(glh::matrix4f& mat);
-glh::matrix4f gl_ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar);
-glh::matrix4f gl_perspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar);
-glh::matrix4f gl_lookat(LLVector3 eye, LLVector3 center, LLVector3 up);
+
 
 extern LLTrace::BlockTimerStatHandle FTM_RENDER_GEOMETRY;
 extern LLTrace::BlockTimerStatHandle FTM_RENDER_GRASS;
-- 
GitLab