From cbd28775246409df2e70b7f8ab2151d34ad061ba Mon Sep 17 00:00:00 2001 From: Olivier Prat Date: Fri, 10 Nov 2017 11:26:20 +0100 Subject: [PATCH] Optimized shadow shader evaluation by computing concatenated shadow reprojection matrix on CPU --- libraries/render-utils/src/LightStage.cpp | 11 +++++++---- libraries/render-utils/src/LightStage.h | 6 ++++-- libraries/render-utils/src/Shadow.slh | 18 ++++-------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/libraries/render-utils/src/LightStage.cpp b/libraries/render-utils/src/LightStage.cpp index 411f179d49..eb712b075c 100644 --- a/libraries/render-utils/src/LightStage.cpp +++ b/libraries/render-utils/src/LightStage.cpp @@ -14,6 +14,11 @@ #include "LightStage.h" std::string LightStage::_stageName { "LIGHT_STAGE"}; +const glm::mat4 LightStage::Shadow::_biasMatrix{ + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 }; LightStage::LightStage() { } @@ -92,8 +97,7 @@ void LightStage::Shadow::setKeylightFrustum(const ViewFrustum& viewFrustum, _frustum->calculate(); // Update the buffer - _schemaBuffer.edit().projection = ortho; - _schemaBuffer.edit().viewInverse = viewInverse.getMatrix(); + _schemaBuffer.edit().reprojection = _biasMatrix * ortho * viewInverse.getMatrix(); } void LightStage::Shadow::setFrustum(const ViewFrustum& shadowFrustum) { @@ -102,8 +106,7 @@ void LightStage::Shadow::setFrustum(const ViewFrustum& shadowFrustum) { *_frustum = shadowFrustum; // Update the buffer - _schemaBuffer.edit().projection = shadowFrustum.getProjection(); - _schemaBuffer.edit().viewInverse = viewInverse.getMatrix(); + _schemaBuffer.edit().reprojection = _biasMatrix * shadowFrustum.getProjection() * viewInverse.getMatrix(); } const glm::mat4& LightStage::Shadow::getView() const { diff --git a/libraries/render-utils/src/LightStage.h b/libraries/render-utils/src/LightStage.h index 3a2a77055f..c45627c89b 100644 --- a/libraries/render-utils/src/LightStage.h +++ b/libraries/render-utils/src/LightStage.h @@ -62,6 +62,9 @@ public: gpu::TexturePointer map; protected: + + static const glm::mat4 _biasMatrix; + model::LightPointer _light; std::shared_ptr _frustum; @@ -70,8 +73,7 @@ public: Schema(); - glm::mat4 projection; - glm::mat4 viewInverse; + glm::mat4 reprojection; glm::float32 bias; glm::float32 scale; diff --git a/libraries/render-utils/src/Shadow.slh b/libraries/render-utils/src/Shadow.slh index 300fa5e7b9..8abbc1de3e 100644 --- a/libraries/render-utils/src/Shadow.slh +++ b/libraries/render-utils/src/Shadow.slh @@ -15,8 +15,7 @@ uniform sampler2DShadow shadowMap; struct ShadowTransform { - mat4 projection; - mat4 viewInverse; + mat4 reprojection; float bias; float scale; @@ -26,12 +25,8 @@ uniform shadowTransformBuffer { ShadowTransform _shadowTransform; }; -mat4 getShadowViewInverse() { - return _shadowTransform.viewInverse; -} - -mat4 getShadowProjection() { - return _shadowTransform.projection; +mat4 getShadowReprojection() { + return _shadowTransform.reprojection; } float getShadowScale() { @@ -44,14 +39,9 @@ float getShadowBias() { // Compute the texture coordinates from world coordinates vec4 evalShadowTexcoord(vec4 position) { - mat4 biasMatrix = mat4( - 0.5, 0.0, 0.0, 0.0, - 0.0, 0.5, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.5, 0.5, 0.5, 1.0); float bias = -getShadowBias(); - vec4 shadowCoord = biasMatrix * getShadowProjection() * getShadowViewInverse() * position; + vec4 shadowCoord = getShadowReprojection() * position; return vec4(shadowCoord.xy, shadowCoord.z + bias, 1.0); }