From c1856a5e3637652c7aa87fd8e402e2200bedc243 Mon Sep 17 00:00:00 2001
From: Sam <samuel.gateau@gmail.com>
Date: Mon, 2 Jul 2018 10:46:53 +0200
Subject: [PATCH] Make shadows work with one cascade

---
 .../gpu-gl/src/gpu/gl41/GL41BackendTexture.cpp   |  4 ++--
 .../gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp   |  4 ++--
 libraries/gpu/src/gpu/Framebuffer.cpp            |  2 +-
 libraries/render-utils/src/RenderShadowTask.cpp  | 16 ++++++++++------
 libraries/render-utils/src/Shadow.slh            | 14 ++++++++++++++
 libraries/render-utils/src/Shadows_shared.slh    |  2 +-
 libraries/render/src/render/CullTask.cpp         |  5 ++++-
 7 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/libraries/gpu-gl/src/gpu/gl41/GL41BackendTexture.cpp b/libraries/gpu-gl/src/gpu/gl41/GL41BackendTexture.cpp
index a255cc5878..4be82f0158 100644
--- a/libraries/gpu-gl/src/gpu/gl41/GL41BackendTexture.cpp
+++ b/libraries/gpu-gl/src/gpu/gl41/GL41BackendTexture.cpp
@@ -182,7 +182,7 @@ void GL41Texture::syncSampler() const {
     glTexParameteri(_target, GL_TEXTURE_MAG_FILTER, fm.magFilter);
 
     if (sampler.doComparison()) {
-        glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB);
+        glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
         glTexParameteri(_target, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
     } else {
         glTexParameteri(_target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
@@ -197,7 +197,7 @@ void GL41Texture::syncSampler() const {
     glTexParameterf(_target, GL_TEXTURE_MIN_LOD, (float)sampler.getMinMip());
     glTexParameterf(_target, GL_TEXTURE_MAX_LOD, (sampler.getMaxMip() == Sampler::MAX_MIP_LEVEL ? 1000.f : sampler.getMaxMip()));
 
-    glTexParameterf(_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, sampler.getMaxAnisotropy());
+    glTexParameterf(_target, GL_TEXTURE_MAX_ANISOTROPY, sampler.getMaxAnisotropy());
 }
 
 using GL41FixedAllocationTexture = GL41Backend::GL41FixedAllocationTexture;
diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp
index fda7ac22dd..7cc639da5e 100644
--- a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp
+++ b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp
@@ -152,7 +152,7 @@ public:
             glSamplerParameteri(result, GL_TEXTURE_MIN_FILTER, fm.minFilter);
             glSamplerParameteri(result, GL_TEXTURE_MAG_FILTER, fm.magFilter);
             if (sampler.doComparison()) {
-                glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB);
+                glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
                 glSamplerParameteri(result, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
             } else {
                 glSamplerParameteri(result, GL_TEXTURE_COMPARE_MODE, GL_NONE);
@@ -341,7 +341,7 @@ void GL45Texture::syncSampler() const {
     glTextureParameteri(_id, GL_TEXTURE_MAG_FILTER, fm.magFilter);
 
     if (sampler.doComparison()) {
-        glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE_ARB);
+        glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
         glTextureParameteri(_id, GL_TEXTURE_COMPARE_FUNC, COMPARISON_TO_GL[sampler.getComparisonFunction()]);
     } else {
         glTextureParameteri(_id, GL_TEXTURE_COMPARE_MODE, GL_NONE);
diff --git a/libraries/gpu/src/gpu/Framebuffer.cpp b/libraries/gpu/src/gpu/Framebuffer.cpp
index 8bb9be4a76..7e7db1345c 100755
--- a/libraries/gpu/src/gpu/Framebuffer.cpp
+++ b/libraries/gpu/src/gpu/Framebuffer.cpp
@@ -61,7 +61,7 @@ Framebuffer* Framebuffer::createShadowmap(uint16 width) {
     samplerDesc._borderColor = glm::vec4(1.0f);
     samplerDesc._wrapModeU = Sampler::WRAP_BORDER;
     samplerDesc._wrapModeV = Sampler::WRAP_BORDER;
-    samplerDesc._filter = Sampler::FILTER_MIN_MAG_LINEAR;
+    samplerDesc._filter = Sampler::FILTER_MIN_MAG_POINT;
     samplerDesc._comparisonFunc = LESS;
 
     depthTexture->setSampler(Sampler(samplerDesc));
diff --git a/libraries/render-utils/src/RenderShadowTask.cpp b/libraries/render-utils/src/RenderShadowTask.cpp
index fbb4bba263..91eb777199 100644
--- a/libraries/render-utils/src/RenderShadowTask.cpp
+++ b/libraries/render-utils/src/RenderShadowTask.cpp
@@ -227,7 +227,7 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende
     }
 
     const auto setupOutput = task.addJob<RenderShadowSetup>("ShadowSetup");
-    const auto queryResolution = setupOutput.getN<RenderShadowSetup::Outputs>(2);
+    const auto queryResolution = setupOutput.getN<RenderShadowSetup::Outputs>(1);
     // Fetch and cull the items from the scene
 
     static const auto shadowCasterReceiverFilter = ItemFilter::Builder::visibleWorldItems().withTypeShape().withOpaque().withoutLayered().withTagBits(tagBits, tagMask);
@@ -248,10 +248,12 @@ void RenderShadowTask::build(JobModel& task, const render::Varying& input, rende
     const auto sortedShapes = task.addJob<DepthSortShapes>("DepthSortShadow", sortedPipelines, true);
 
     render::Varying cascadeFrustums[SHADOW_CASCADE_MAX_COUNT] = {
-        ViewFrustumPointer(),
-        ViewFrustumPointer(),
+        ViewFrustumPointer()
+#if SHADOW_CASCADE_MAX_COUNT>1
+        ,ViewFrustumPointer(),
         ViewFrustumPointer(),
         ViewFrustumPointer()
+#endif
     };
 
     for (auto i = 0; i < SHADOW_CASCADE_MAX_COUNT; i++) {
@@ -293,13 +295,15 @@ RenderShadowSetup::RenderShadowSetup() :
 
 void RenderShadowSetup::configure(const Config& configuration) {
     setConstantBias(0, configuration.constantBias0);
-    setConstantBias(1, configuration.constantBias1);
-    setConstantBias(2, configuration.constantBias2);
-    setConstantBias(3, configuration.constantBias3);
     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);
+#endif
 }
 
 void RenderShadowSetup::setConstantBias(int cascadeIndex, float value) {
diff --git a/libraries/render-utils/src/Shadow.slh b/libraries/render-utils/src/Shadow.slh
index 36eb35c757..4a63e24082 100644
--- a/libraries/render-utils/src/Shadow.slh
+++ b/libraries/render-utils/src/Shadow.slh
@@ -17,11 +17,19 @@
 #define SHADOW_SCREEN_SPACE_DITHER      1
 
 // the shadow texture
+#if SHADOW_CASCADE_MAX_COUNT>1
 uniform sampler2DShadow shadowMaps[SHADOW_CASCADE_MAX_COUNT];
+#else
+uniform sampler2DShadow shadowMaps;
+#endif
 
 // Sample the shadowMap with PCF (built-in)
 float fetchShadow(int cascadeIndex, vec3 shadowTexcoord) {
+#if SHADOW_CASCADE_MAX_COUNT>1
     return texture(shadowMaps[cascadeIndex], shadowTexcoord);
+#else
+    return texture(shadowMaps, shadowTexcoord);
+#endif
 }
 
 vec2 PCFkernel[4] = vec2[4](
@@ -89,6 +97,7 @@ float evalShadowCascadeAttenuation(int cascadeIndex, ShadowSampleOffsets offsets
 
 float evalShadowAttenuation(vec3 worldLightDir, vec4 worldPosition, float viewDepth, vec3 worldNormal) {
     ShadowSampleOffsets offsets = evalShadowFilterOffsets(worldPosition);
+#if 0
     vec4 cascadeShadowCoords[2];
     cascadeShadowCoords[0] = vec4(0);
     cascadeShadowCoords[1] = vec4(0);
@@ -105,6 +114,11 @@ float evalShadowAttenuation(vec3 worldLightDir, vec4 worldPosition, float viewDe
     float attenuation = mix(cascadeAttenuations.x, cascadeAttenuations.y, cascadeMix);
     // Falloff to max distance
     return mix(1.0, attenuation, evalShadowFalloff(viewDepth));
+#else
+vec4 shadowTexcoord = evalShadowTexcoord(0, worldPosition);
+float attenuation = fetchShadow(0, shadowTexcoord.xyz);
+return attenuation;
+#endif
 }
 
 <@endif@>
diff --git a/libraries/render-utils/src/Shadows_shared.slh b/libraries/render-utils/src/Shadows_shared.slh
index bc8063e018..7dea11cd8b 100644
--- a/libraries/render-utils/src/Shadows_shared.slh
+++ b/libraries/render-utils/src/Shadows_shared.slh
@@ -5,7 +5,7 @@
 #   define MAT4 mat4
 #endif
 
-#define SHADOW_CASCADE_MAX_COUNT    4
+#define SHADOW_CASCADE_MAX_COUNT    1
 
 struct ShadowTransform {
 	MAT4 reprojection;
diff --git a/libraries/render/src/render/CullTask.cpp b/libraries/render/src/render/CullTask.cpp
index 3f55e6dedc..8cfe7683ce 100644
--- a/libraries/render/src/render/CullTask.cpp
+++ b/libraries/render/src/render/CullTask.cpp
@@ -370,10 +370,13 @@ void CullShapeBounds::run(const RenderContextPointer& renderContext, const Input
     const auto& inShapes = inputs.get0();
     const auto& cullFilter = inputs.get1();
     const auto& boundsFilter = inputs.get2();
-    const auto& antiFrustum = inputs.get3();
+    ViewFrustumPointer antiFrustum;
     auto& outShapes = outputs.edit0();
     auto& outBounds = outputs.edit1();
 
+    if (!inputs[3].isNull()) {
+        antiFrustum = inputs.get3();
+    }
     outShapes.clear();
     outBounds = AABox();