diff --git a/indra/llinventory/llsettingssky.cpp b/indra/llinventory/llsettingssky.cpp
index f1bea326c1196054379c8f14aada7e4659559d59..0e597aabb0c9d164d68ab3819caf39ac1cdb7881 100644
--- a/indra/llinventory/llsettingssky.cpp
+++ b/indra/llinventory/llsettingssky.cpp
@@ -1281,7 +1281,7 @@ void LLSettingsSky::calculateLightSettings() const
 
     // and vary_sunlight will work properly with moon light
     F32 lighty = lightnorm[1];
-    if(fabs(lighty) > 0.001f)
+    if(lighty > 0.001f)
     {
         lighty = 1.f / lighty;
     }
diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp
index dc662bf8982fa4bbbaef95f0ef0c9841cfde14e3..2bf7ad9902e7bd31f8823d7206368264b1f50d74 100644
--- a/indra/llrender/llrender.cpp
+++ b/indra/llrender/llrender.cpp
@@ -850,9 +850,12 @@ LLLightState::LLLightState(S32 index)
 	if (mIndex == 0)
 	{
 		mDiffuse.set(1,1,1,1);
+        mDiffuseB.set(0,0,0,0);
 		mSpecular.set(1,1,1,1);
 	}
 
+    mSunIsPrimary = true;
+
 	mAmbient.set(0,0,0,1);
 	mPosition.set(0,0,1,0);
 	mSpotDirection.set(0,0,-1);
@@ -904,6 +907,15 @@ void LLLightState::setDiffuseB(const LLColor4& diffuse)
 	}
 }
 
+void LLLightState::setSunPrimary(bool v)
+{
+    if (mSunIsPrimary != v)
+    {
+        ++gGL.mLightHash;
+		mSunIsPrimary = v;
+    }
+}
+
 void LLLightState::setAmbient(const LLColor4& ambient)
 {
 	if (mAmbient != ambient)
@@ -1162,7 +1174,8 @@ void LLRender::syncLightState()
 		LLVector3 direction[8];
 		LLVector4 attenuation[8];
 		LLVector3 diffuse[8];
-        LLVector3 diffuseB[8];
+        LLVector3 diffuse_b[8];
+        bool      sun_primary[8];
 
 		for (U32 i = 0; i < 8; i++)
 		{
@@ -1172,7 +1185,8 @@ void LLRender::syncLightState()
 			direction[i] = light->mSpotDirection;
             attenuation[i].set(light->mLinearAtten, light->mQuadraticAtten, light->mSpecular.mV[2], light->mSpecular.mV[3]);
 			diffuse[i].set(light->mDiffuse.mV);
-            diffuseB[i].set(light->mDiffuseB.mV);
+            diffuse_b[i].set(light->mDiffuseB.mV);
+            sun_primary[i] = light->mSunIsPrimary;
 		}
 
 		shader->uniform4fv(LLShaderMgr::LIGHT_POSITION, 8, position[0].mV);
@@ -1180,6 +1194,8 @@ void LLRender::syncLightState()
 		shader->uniform4fv(LLShaderMgr::LIGHT_ATTENUATION, 8, attenuation[0].mV);
 		shader->uniform3fv(LLShaderMgr::LIGHT_DIFFUSE, 8, diffuse[0].mV);
 		shader->uniform4fv(LLShaderMgr::LIGHT_AMBIENT, 1, mAmbientLightColor.mV);
+        shader->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_primary[0] ? 1 : 0);
+        shader->uniform4fv(LLShaderMgr::MOONLIGHT_COLOR, 1, diffuse_b[0].mV);
 	}
 }
 
diff --git a/indra/llrender/llrender.h b/indra/llrender/llrender.h
index 3f2d5eb35d0ad593ab525e5140bcc92e92432864..ad9cd11283d1a133bd835f8db9bd945e7f8563f5 100644
--- a/indra/llrender/llrender.h
+++ b/indra/llrender/llrender.h
@@ -239,6 +239,7 @@ class LLLightState
 	void setSpotExponent(const F32& exponent);
 	void setSpotCutoff(const F32& cutoff);
 	void setSpotDirection(const LLVector3& direction);
+    void setSunPrimary(bool v);
 
 protected:
 	friend class LLRender;
@@ -247,6 +248,7 @@ class LLLightState
 	bool mEnabled;
 	LLColor4 mDiffuse;
     LLColor4 mDiffuseB;
+    bool     mSunIsPrimary;
 	LLColor4 mAmbient;
 	LLColor4 mSpecular;
 	LLVector4 mPosition;
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index c4a644c7b68b63698c60b595c8cebc7eb7cc51dd..4c93d16fb1edbcaa80509a3ebe135f5f041e9b5e 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -6240,6 +6240,7 @@ void LLPipeline::setupHWLights(LLDrawPool* pool)
         LLLightState* light = gGL.getLight(0);
         light->setPosition(light_dir);
 
+        light->setSunPrimary(sun_up);
         light->setDiffuse(mSunDiffuse);
         light->setDiffuseB(mMoonDiffuse);
         light->setAmbient(LLColor4::black);
@@ -6361,7 +6362,7 @@ void LLPipeline::setupHWLights(LLDrawPool* pool)
 	{
 		mHWLightColors[cur_light] = LLColor4::black;
 		LLLightState* light = gGL.getLight(cur_light);
-
+        light->setSunPrimary(true);
 		light->setDiffuse(LLColor4::black);
 		light->setAmbient(LLColor4::black);
 		light->setSpecular(LLColor4::black);