From a01f1d9e834719046fbdec02a5147d2e55722bac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 29 Jan 2024 01:56:42 +0000 Subject: Remove the complications from previously storing several shadow maps in the texture --- gfx/gl/sceneRenderer.cpp | 13 +++---- gfx/gl/sceneRenderer.h | 7 ++-- gfx/gl/shaders/directionalLight.fs | 17 ++++------ gfx/gl/shadowMapper.cpp | 69 ++------------------------------------ gfx/gl/shadowMapper.h | 6 +--- 5 files changed, 19 insertions(+), 93 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index b60d9af..2dd1760 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -116,7 +116,7 @@ SceneRenderer::setDirectionalLight(const RGB & colour, const Direction3D & direc glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glViewport(0, 0, size.x, size.y); dirLight.use(); - dirLight.setDirectionalLight(colour, direction, camera.getPosition(), lvp.projections, lvp.regions, lvp.maps); + dirLight.setDirectionalLight(colour, direction, camera.getPosition(), lvp); renderQuad(); } } @@ -133,21 +133,18 @@ SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : Program {lighting_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"}, colourLoc {*this, "lightColour"}, lightPointLoc {*this, "lightPoint"}, lightViewProjectionLoc {*this, "lightViewProjection"}, - lightViewProjectionCountLoc {*this, "lightViewProjectionCount"}, - lightViewShadowMapRegionLoc {*this, "shadowMapRegion"} + lightViewProjectionCountLoc {*this, "lightViewProjectionCount"} { } void -SceneRenderer::DirectionalLightProgram::setDirectionalLight(const RGB & c, const Direction3D & d, - const GlobalPosition3D & p, const std::span lvp, - const std::span shadowMapRegions, std::size_t maps) const +SceneRenderer::DirectionalLightProgram::setDirectionalLight( + const RGB & c, const Direction3D & d, const GlobalPosition3D & p, const std::span lvp) const { glUniform(colourLoc, c); const auto nd = glm::normalize(d); glUniform(directionLoc, nd); glUniform(lightPointLoc, p); - glUniform(lightViewProjectionCountLoc, static_cast(maps)); + glUniform(lightViewProjectionCountLoc, static_cast(lvp.size())); glUniform(lightViewProjectionLoc, lvp); - glUniform(lightViewShadowMapRegionLoc, shadowMapRegions); } diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h index 797ecf1..021936f 100644 --- a/gfx/gl/sceneRenderer.h +++ b/gfx/gl/sceneRenderer.h @@ -6,7 +6,6 @@ #include "sceneProvider.h" #include "sceneShader.h" #include "shadowMapper.h" -#include #include class SceneRenderer { @@ -39,12 +38,12 @@ protected: DirectionalLightProgram(); using Program::use; - void setDirectionalLight(const RGB &, const Direction3D &, const GlobalPosition3D &, - const std::span, const std::span, std::size_t maps) const; + void setDirectionalLight( + const RGB &, const Direction3D &, const GlobalPosition3D &, const std::span) const; private: RequiredUniformLocation directionLoc, colourLoc, lightPointLoc, lightViewProjectionLoc, - lightViewProjectionCountLoc, lightViewShadowMapRegionLoc; + lightViewProjectionCountLoc; }; DeferredLightProgram lighting; diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index d0c5062..c49bb8d 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -15,35 +15,32 @@ uniform vec3 lightDirection; uniform vec3 lightColour; uniform ivec3 lightPoint; uniform mat4 lightViewProjection[MAX_MAPS]; -uniform vec4 shadowMapRegion[MAX_MAPS]; uniform uint lightViewProjectionCount; -const vec3 e1 = vec3(-1, -1, -1), e2 = vec3(1, 1, 1); +const vec3 e1 = vec3(0, 0, 0), e2 = vec3(1, 1, 1); -float + float insideShadowCube(vec3 v) { const vec3 s = step(e1, v) - step(e2, v); return s.x * s.y * s.z; } -float + float isShaded(vec4 Position) { for (uint m = 0u; m < lightViewProjectionCount; m++) { - const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; + const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz * .5 + .5; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { - const float lightSpaceDepth = texture( - shadowMap, vec3(PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw, m)) - .r; - return step(lightSpaceDepth, PositionInLightSpace.z * .5 + .5); + const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r; + return step(lightSpaceDepth, PositionInLightSpace.z); } } return 0; } -void + void main() { const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index f3d5dea..4dbee8d 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -40,48 +40,6 @@ ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : glBindFramebuffer(GL_FRAMEBUFFER, 0); } -constexpr std::array, ShadowMapper::SHADOW_BANDS> viewports {{ - {{ - {31, 31, 0, 0}, // full - }}, - {{ - {31, 31, 0, 1}, // lower half - {31, 1, 0, 1}, // upper half - }}, - {{ - {31, 31, 0, 1}, // lower half - {31, 1, 1, 1}, // upper left - {1, 1, 1, 1}, // upper right - }}, - {{ - {31, 31, 1, 1}, // lower left - {1, 31, 1, 1}, // lower right - {31, 1, 1, 1}, // upper left - {1, 1, 1, 1}, // upper right - }}, -}}; -constexpr std::array, ShadowMapper::SHADOW_BANDS> - shadowMapRegions {{ - {{ - {0.5F, 0.5F, 0.5F, 0.5F}, // full - }}, - {{ - {0.5F, 0.25F, 0.5F, 0.25F}, // lower half - {0.5F, 0.25F, 0.5F, 0.75F}, // upper half - }}, - {{ - {0.5F, 0.25F, 0.5F, 0.25F}, // lower half - {0.25F, 0.25F, 0.25F, 0.75F}, // upper left - {0.25F, 0.25F, 0.75F, 0.75F}, // upper right - }}, - - {{ - {0.25F, 0.25F, 0.25F, 0.25F}, // lower left - {0.25F, 0.25F, 0.75F, 0.25F}, // lower right - {0.25F, 0.25F, 0.25F, 0.75F}, // upper left - {0.25F, 0.25F, 0.75F, 0.75F}, // upper right - }}, - }}; constexpr std::array shadowBands { 1000, 250000, @@ -89,24 +47,6 @@ constexpr std::array shadowBands 2500000, 10000000, }; -static_assert(viewports.size() == shadowMapRegions.size()); -static_assert(shadowBands.size() == shadowMapRegions.size() + 1); - -struct DefinitionsInserter { - auto - operator++() - { - return out.maps++; - }; - - auto - operator*() - { - return std::tie(out.projections[out.maps], out.regions[out.maps]); - } - - ShadowMapper::Definitions & out; -}; std::vector> ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightViewDir) @@ -139,7 +79,7 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const const auto bandViewExtents = getBandViewExtents(camera, lightViewDir); Definitions out; std::transform(bandViewExtents.begin(), std::prev(bandViewExtents.end()), std::next(bandViewExtents.begin()), - DefinitionsInserter {out}, + std::back_inserter(out), [bands = bandViewExtents.size() - 2, &lightViewDir](const auto & near, const auto & far) mutable { const auto extents_minmax = [extents = std::span {near.begin(), far.end()}](auto && comp) { const auto mm = std::minmax_element(extents.begin(), extents.end(), comp); @@ -150,13 +90,10 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const return glm::ortho(x.first, x.second, y.first, y.second, -z.second, -z.first); }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); - const auto lightViewDirProjection = lightProjection * lightViewDir; - - return std::make_pair(lightViewDirProjection, shadowMapRegions[0][0]); + return lightProjection * lightViewDir; }); - std::span vps {out.projections.data(), out.maps}; for (const auto p : std::initializer_list {&fixedPoint, &dynamicPoint, &dynamicPointInst}) { - p->setView(vps, lightViewPoint); + p->setView(out, lightViewPoint); } scene.shadows(*this); diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index b948f50..a95d4c1 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -17,11 +17,7 @@ public: static constexpr std::size_t SHADOW_BANDS {4}; - struct Definitions { - std::array projections {}; - std::array regions {}; - size_t maps {}; - }; + using Definitions = std::vector; [[nodiscard]] Definitions update(const SceneProvider &, const Direction3D & direction, const Camera &) const; -- cgit v1.2.3