From 44caf67527da4a48edbf8499c7f94232efffdc41 Mon Sep 17 00:00:00 2001
From: raveenajain <jain.rav@husky.neu.edu>
Date: Tue, 18 Jun 2019 10:02:22 -0700
Subject: [PATCH] init change, bias triggered via script

---
 libraries/graphics/src/graphics/Light.cpp     |  8 +++
 libraries/graphics/src/graphics/Light.h       |  4 ++
 libraries/render-utils/src/LightStage.cpp     |  1 +
 libraries/render-utils/src/LightStage.h       |  3 ++
 .../render-utils/src/RenderShadowTask.cpp     | 52 +++++++++++++++----
 libraries/render-utils/src/RenderShadowTask.h | 10 ++++
 libraries/render-utils/src/Shadow.slh         |  2 +-
 7 files changed, 68 insertions(+), 12 deletions(-)
 mode change 100644 => 100755 libraries/render-utils/src/LightStage.cpp
 mode change 100644 => 100755 libraries/render-utils/src/LightStage.h
 mode change 100644 => 100755 libraries/render-utils/src/RenderShadowTask.cpp
 mode change 100644 => 100755 libraries/render-utils/src/RenderShadowTask.h
 mode change 100644 => 100755 libraries/render-utils/src/Shadow.slh

diff --git a/libraries/graphics/src/graphics/Light.cpp b/libraries/graphics/src/graphics/Light.cpp
index 8a7281880e..3d2ddb4908 100755
--- a/libraries/graphics/src/graphics/Light.cpp
+++ b/libraries/graphics/src/graphics/Light.cpp
@@ -89,6 +89,14 @@ float Light::getShadowsBiasScale() const {
     return _shadowsBiasScale;
 }
 
+void Light::setBiasInput(const float bias) {
+    _biasInput = bias;
+}
+
+float Light::getBiasInput() const {
+    return _biasInput;
+}
+
 void Light::setColor(const Color& color) {
     _lightSchemaBuffer.edit().irradiance.color = color;
     updateLightRadius();
diff --git a/libraries/graphics/src/graphics/Light.h b/libraries/graphics/src/graphics/Light.h
index 824a9138c0..3ef1571032 100755
--- a/libraries/graphics/src/graphics/Light.h
+++ b/libraries/graphics/src/graphics/Light.h
@@ -112,6 +112,9 @@ public:
     void setShadowsBiasScale(const float scale);
     float getShadowsBiasScale() const;
 
+    void setBiasInput(const float bias);
+    float getBiasInput() const;
+
     void setOrientation(const Quat& orientation);
     const glm::quat& getOrientation() const { return _transform.getRotation(); }
 
@@ -200,6 +203,7 @@ protected:
 
     float _shadowsMaxDistance{ 40.0f };
     float _shadowsBiasScale{ 1.0f };
+    float _biasInput{ 0.3f }; // gives default constant and slope values
     bool _castShadows{ false };
 
     void updateLightRadius();
diff --git a/libraries/render-utils/src/LightStage.cpp b/libraries/render-utils/src/LightStage.cpp
old mode 100644
new mode 100755
index 524deaaad2..e22428148a
--- a/libraries/render-utils/src/LightStage.cpp
+++ b/libraries/render-utils/src/LightStage.cpp
@@ -151,6 +151,7 @@ LightStage::Shadow::Shadow(graphics::LightPointer light, unsigned int cascadeCou
 
     if (light) {
         setMaxDistance(light->getShadowsMaxDistance());
+        setBiasInput(light->getBiasInput());
     }
 }
 
diff --git a/libraries/render-utils/src/LightStage.h b/libraries/render-utils/src/LightStage.h
old mode 100644
new mode 100755
index 4da66843cc..75dd14871f
--- a/libraries/render-utils/src/LightStage.h
+++ b/libraries/render-utils/src/LightStage.h
@@ -92,6 +92,8 @@ public:
         float getMaxDistance() const { return _maxDistance; }
         void setMaxDistance(float value);
 
+        void setBiasInput(float value) { _biasInput = value; }
+
         const graphics::LightPointer& getLight() const { return _light; }
 
         gpu::TexturePointer map;
@@ -110,6 +112,7 @@ public:
 
         graphics::LightPointer _light;
         float _maxDistance{ 0.0f };
+        float _biasInput;
         Cascades _cascades;
 
         UniformBufferView _schemaBuffer = nullptr;
diff --git a/libraries/render-utils/src/RenderShadowTask.cpp b/libraries/render-utils/src/RenderShadowTask.cpp
old mode 100644
new mode 100755
index 5de89a11b5..80a7c5d7e2
--- a/libraries/render-utils/src/RenderShadowTask.cpp
+++ b/libraries/render-utils/src/RenderShadowTask.cpp
@@ -56,7 +56,6 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende
         auto fadeEffect = DependencyManager::get<FadeEffect>();
         initZPassPipelines(*shapePlumber, state, fadeEffect->getBatchSetter(), fadeEffect->getItemUniformSetter());
     }
-
     const auto setupOutput = task.addJob<RenderShadowSetup>("ShadowSetup", input);
     const auto queryResolution = setupOutput.getN<RenderShadowSetup::Output>(1);
     const auto shadowFrame = setupOutput.getN<RenderShadowSetup::Output>(3);
@@ -318,15 +317,43 @@ RenderShadowSetup::RenderShadowSetup() :
 }
 
 void RenderShadowSetup::configure(const Config& configuration) {
-    setConstantBias(0, configuration.constantBias0);
-    setSlopeBias(0, configuration.slopeBias0);
-#if SHADOW_CASCADE_MAX_COUNT>1
-    setConstantBias(1, configuration.constantBias1);
-    setSlopeBias(1, configuration.slopeBias1);
-    setConstantBias(2, configuration.constantBias2);
-    setSlopeBias(2, configuration.slopeBias2);
-    setConstantBias(3, configuration.constantBias3);
-    setSlopeBias(3, configuration.slopeBias3);
+
+    // change shadow map based on distinct constant and slope values
+    float constant0 = configuration.constantBias0;
+    float constant1 = configuration.constantBias1;
+    float constant2 = configuration.constantBias2;
+    float constant3 = configuration.constantBias3;
+    float slope0 = configuration.slopeBias0;
+    float slope1 = configuration.slopeBias1;
+    float slope2 = configuration.slopeBias2;
+    float slope3 = configuration.slopeBias3;
+    
+    // based on external bias input
+    if (prevBiasInput != configuration.biasInput) {
+        prevBiasInput = configuration.biasInput;
+        _biasInput = configuration.biasInput;
+
+        constant0 = (cacasdeDepths[0] / resolution) * _biasInput;
+        constant1 = (cacasdeDepths[1] / resolution) * _biasInput;
+        constant2 = (cacasdeDepths[2] / resolution) * _biasInput * 1.1f;
+        constant3 = (cacasdeDepths[3] / resolution) * _biasInput * 1.3f;
+        slope0 = constant0 * 2.7f;
+        slope1 = constant1 * 3.0f;
+        slope2 = constant2 * 3.7f;
+        slope3 = constant3 * 3.3f;
+    }
+    
+    setConstantBias(0, constant0);
+    setSlopeBias(0, slope0);
+
+#if SHADOW_CASCADE_MAX_COUNT > 1
+    setConstantBias(1, constant1);
+    setConstantBias(2, constant2);
+    setConstantBias(3, constant3);
+
+    setSlopeBias(1, slope1);
+    setSlopeBias(2, slope2);
+    setSlopeBias(3, slope3);
 #endif
 }
 
@@ -368,7 +395,7 @@ void RenderShadowSetup::run(const render::RenderContextPointer& renderContext, c
     if (!_globalShadowObject) {
         _globalShadowObject = std::make_shared<LightStage::Shadow>(currentKeyLight, SHADOW_CASCADE_COUNT);
     }
-
+    resolution = _globalShadowObject->MAP_SIZE;
     _globalShadowObject->setLight(currentKeyLight);
     _globalShadowObject->setKeylightFrustum(args->getViewFrustum(), SHADOW_FRUSTUM_NEAR, SHADOW_FRUSTUM_FAR);
 
@@ -378,6 +405,7 @@ void RenderShadowSetup::run(const render::RenderContextPointer& renderContext, c
 
     // Adjust each cascade frustum
     const auto biasScale = currentKeyLight->getShadowsBiasScale();
+    setBiasInput(currentKeyLight->getBiasInput());
     for (cascadeIndex = 0; cascadeIndex < _globalShadowObject->getCascadeCount(); ++cascadeIndex) {
         auto& bias = _bias[cascadeIndex];
         _globalShadowObject->setKeylightCascadeFrustum(cascadeIndex, args->getViewFrustum(),
@@ -398,6 +426,7 @@ void RenderShadowSetup::run(const render::RenderContextPointer& renderContext, c
     auto bottom = glm::dot(farBottomRight, firstCascadeFrustum->getUp());
     auto near = firstCascadeFrustum->getNearClip();
     auto far = firstCascadeFrustum->getFarClip();
+    cacasdeDepths[0] = far;
 
     for (cascadeIndex = 1; cascadeIndex < _globalShadowObject->getCascadeCount(); ++cascadeIndex) {
         auto& cascadeFrustum = _globalShadowObject->getCascade(cascadeIndex).getFrustum();
@@ -417,6 +446,7 @@ void RenderShadowSetup::run(const render::RenderContextPointer& renderContext, c
         top = glm::max(top, cascadeTop);
         near = glm::min(near, cascadeNear);
         far = glm::max(far, cascadeFar);
+        cacasdeDepths[cascadeIndex] = far;
     }
 
     _coarseShadowFrustum->setPosition(firstCascadeFrustum->getPosition());
diff --git a/libraries/render-utils/src/RenderShadowTask.h b/libraries/render-utils/src/RenderShadowTask.h
old mode 100644
new mode 100755
index ef469a7247..14178f0505
--- a/libraries/render-utils/src/RenderShadowTask.h
+++ b/libraries/render-utils/src/RenderShadowTask.h
@@ -87,8 +87,12 @@ class RenderShadowSetupConfig : public render::Job::Config {
         Q_PROPERTY(float slopeBias1 MEMBER slopeBias1 NOTIFY dirty)
         Q_PROPERTY(float slopeBias2 MEMBER slopeBias2 NOTIFY dirty)
         Q_PROPERTY(float slopeBias3 MEMBER slopeBias3 NOTIFY dirty)
+        Q_PROPERTY(float biasInput MEMBER biasInput NOTIFY dirty)
+
 public:
 
+    float biasInput { 0.3f };
+
     float constantBias0{ 0.15f };
     float constantBias1{ 0.15f };
     float constantBias2{ 0.175f };
@@ -112,6 +116,11 @@ public:
     RenderShadowSetup();
     void configure(const Config& configuration);
     void run(const render::RenderContextPointer& renderContext, const Input& input, Output& output);
+    
+    float _biasInput;
+    float prevBiasInput { _biasInput };
+    int resolution { 1024 };
+    QVector<float> cacasdeDepths = QVector<float>(4);
 
 private:
 
@@ -127,6 +136,7 @@ private:
 
     void setConstantBias(int cascadeIndex, float value);
     void setSlopeBias(int cascadeIndex, float value);
+    void setBiasInput(float input) { _biasInput = input; }
 };
 
 class RenderShadowCascadeSetup {
diff --git a/libraries/render-utils/src/Shadow.slh b/libraries/render-utils/src/Shadow.slh
old mode 100644
new mode 100755
index 94bec86b34..ac55bc95e5
--- a/libraries/render-utils/src/Shadow.slh
+++ b/libraries/render-utils/src/Shadow.slh
@@ -63,7 +63,7 @@ ShadowSampleOffsets evalShadowFilterOffsets(vec4 position) {
     coords.y += (index & 2) >> 1;
 #endif
 
-    // Offset for efficient PCF, see http://http.developer.nvidia.com/GPUGems/gpugems_ch11.html
+    // Offset for efficient PCF, see https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch11.html
     ivec2 offset = coords & ivec2(1,1);
     offset.y = (offset.x+offset.y) & 1;