From dad6fe3b6227b5d3e78913e164b1f688de1605a8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 27 Jan 2024 22:49:54 +0000 Subject: Set light view point uniform once Not once per shadow pass --- gfx/gl/shadowMapper.cpp | 31 +++++++++++++++++++++++-------- gfx/gl/shadowMapper.h | 6 ++++-- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index ab4ac50..040010f 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -134,11 +134,14 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const const auto lightViewDir = glm::lookAt({}, dir, up); const auto lightViewPoint = camera.getPosition(); const auto bandViewExtents = getBandViewExtents(camera, lightViewDir); + fixedPoint.setViewPoint(lightViewPoint); + dynamicPoint.setViewPoint(lightViewPoint); + dynamicPointInst.setViewPoint(lightViewPoint); Definitions out; std::transform(bandViewExtents.begin(), std::prev(bandViewExtents.end()), std::next(bandViewExtents.begin()), DefinitionsInserter {out}, - [&scene, this, bands = bandViewExtents.size() - 2, &out, &lightViewPoint, &lightViewDir]( + [&scene, this, bands = bandViewExtents.size() - 2, &out, &lightViewDir]( const auto & near, const auto & far) { 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,9 +153,9 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); const auto lightViewDirProjection = lightProjection * lightViewDir; - fixedPoint.setViewProjection(lightViewPoint, lightViewDirProjection); - dynamicPoint.setViewProjection(lightViewPoint, lightViewDirProjection); - dynamicPointInst.setViewProjection(lightViewPoint, lightViewDirProjection); + fixedPoint.setViewProjection(lightViewDirProjection); + dynamicPoint.setViewProjection(lightViewDirProjection); + dynamicPointInst.setViewProjection(lightViewDirProjection); const auto & viewport = viewports[bands][out.maps]; glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w); @@ -172,13 +175,19 @@ ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : } void -ShadowMapper::FixedPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const +ShadowMapper::FixedPoint::setViewPoint(const GlobalPosition3D viewPoint) const { use(); - glUniform(viewProjectionLoc, viewProjection); glUniform(viewPointLoc, viewPoint); } +void +ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection) const +{ + use(); + glUniform(viewProjectionLoc, viewProjection); +} + void ShadowMapper::FixedPoint::use() const { @@ -192,13 +201,19 @@ ShadowMapper::DynamicPoint::DynamicPoint() : } void -ShadowMapper::DynamicPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const +ShadowMapper::DynamicPoint::setViewPoint(const GlobalPosition3D viewPoint) const { glUseProgram(*this); - glUniform(viewProjectionLoc, viewProjection); glUniform(viewPointLoc, viewPoint); } +void +ShadowMapper::DynamicPoint::setViewProjection(const glm::mat4 & viewProjection) const +{ + glUseProgram(*this); + glUniform(viewProjectionLoc, viewProjection); +} + void ShadowMapper::DynamicPoint::use(const Location & location) const { diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index d1e4e10..065db4f 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -27,7 +27,8 @@ public: class FixedPoint : public Program { public: FixedPoint(const Shader & vs); - void setViewProjection(const GlobalPosition3D, const glm::mat4 &) const; + void setViewPoint(const GlobalPosition3D) const; + void setViewProjection(const glm::mat4 &) const; void use() const; private: @@ -37,7 +38,8 @@ public: class DynamicPoint : public Program { public: DynamicPoint(); - void setViewProjection(const GlobalPosition3D, const glm::mat4 &) const; + void setViewPoint(const GlobalPosition3D) const; + void setViewProjection(const glm::mat4 &) const; void use(const Location &) const; void setModel(const Location &) const; -- cgit v1.2.3 From 892513f81ed68e05d4dedb99f51de5bc46cab6b2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 28 Jan 2024 02:25:11 +0000 Subject: Geometry shader for single pass shadow maps 2D array texture, 4 levels, geometry shader outputs to each layer for a specific band, single scene rendering. Pending massive tidy up. --- gfx/gl/sceneRenderer.cpp | 2 +- gfx/gl/shaders/commonShadowPoint.glsl | 5 +- gfx/gl/shaders/commonShadowPoint.gs | 22 +++++++++ gfx/gl/shaders/directionalLight.fs | 7 +-- gfx/gl/shaders/shadowDynamicPoint.vs | 1 - gfx/gl/shaders/shadowDynamicPointInst.vs | 1 - gfx/gl/shaders/shadowFixedPoint.vs | 1 - gfx/gl/shadowMapper.cpp | 80 +++++++++++++++++++------------- gfx/gl/shadowMapper.h | 18 ++++--- 9 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 gfx/gl/shaders/commonShadowPoint.gs diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 4c96902..b60d9af 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -81,7 +81,7 @@ SceneRenderer::render(const SceneProvider & scene) const glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, gNormal); glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, shadowMapper); + glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMapper); scene.environment(shader, *this); glDisable(GL_DEPTH_TEST); scene.lights(shader); diff --git a/gfx/gl/shaders/commonShadowPoint.glsl b/gfx/gl/shaders/commonShadowPoint.glsl index 47722d6..ba4611a 100644 --- a/gfx/gl/shaders/commonShadowPoint.glsl +++ b/gfx/gl/shaders/commonShadowPoint.glsl @@ -1,7 +1,8 @@ +out vec4 vworldPos; + void main() { vec3 worldPos = model * position; - gl_Position = viewProjection * vec4(worldPos - viewPoint + modelPos, 1); - gl_Position.z = max(gl_Position.z, -1); + vworldPos = vec4(worldPos - viewPoint + modelPos, 1); } diff --git a/gfx/gl/shaders/commonShadowPoint.gs b/gfx/gl/shaders/commonShadowPoint.gs new file mode 100644 index 0000000..b008f29 --- /dev/null +++ b/gfx/gl/shaders/commonShadowPoint.gs @@ -0,0 +1,22 @@ +#version 330 core +#extension GL_ARB_viewport_array : enable + +uniform mat4 viewProjection[4]; +uniform int viewProjections; +in vec4 vworldPos[]; +layout(triangles) in; +layout(triangle_strip, max_vertices = 12) out; + +void +main() +{ + for (int vp = 0; vp < viewProjections; ++vp) { + for (int v = 0; v < vworldPos.length(); ++v) { + gl_Position = viewProjection[vp] * vworldPos[v]; + gl_Position.z = max(gl_Position.z, -1); + gl_Layer = vp; + EmitVertex(); + } + EndPrimitive(); + } +} diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index f36d83f..d0c5062 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -9,7 +9,7 @@ in vec2 TexCoords; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; -layout(binding = 2) uniform sampler2D shadowMap; +layout(binding = 2) uniform sampler2DArray shadowMap; uniform vec3 lightDirection; uniform vec3 lightColour; @@ -34,8 +34,9 @@ isShaded(vec4 Position) const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { - const float lightSpaceDepth - = texture(shadowMap, PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw).r; + const float lightSpaceDepth = texture( + shadowMap, vec3(PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw, m)) + .r; return step(lightSpaceDepth, PositionInLightSpace.z * .5 + .5); } } diff --git a/gfx/gl/shaders/shadowDynamicPoint.vs b/gfx/gl/shaders/shadowDynamicPoint.vs index bf0f0a1..7335b9a 100644 --- a/gfx/gl/shaders/shadowDynamicPoint.vs +++ b/gfx/gl/shaders/shadowDynamicPoint.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; uniform mat3 model; uniform ivec3 modelPos; diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vs b/gfx/gl/shaders/shadowDynamicPointInst.vs index b0b80da..d0eb649 100644 --- a/gfx/gl/shaders/shadowDynamicPointInst.vs +++ b/gfx/gl/shaders/shadowDynamicPointInst.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; layout(location = 5) in mat3 model; layout(location = 8) in ivec3 modelPos; diff --git a/gfx/gl/shaders/shadowFixedPoint.vs b/gfx/gl/shaders/shadowFixedPoint.vs index 307aac2..1376388 100644 --- a/gfx/gl/shaders/shadowFixedPoint.vs +++ b/gfx/gl/shaders/shadowFixedPoint.vs @@ -2,7 +2,6 @@ include(`meshIn.glsl') -uniform mat4 viewProjection; uniform ivec3 viewPoint; const mat3 model = mat3(1); const ivec3 modelPos = ivec3(0); diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 040010f..7770c9d 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -1,6 +1,7 @@ #include "shadowMapper.h" #include "camera.h" #include "collections.h" +#include "gfx/gl/shaders/gs-commonShadowPoint.h" #include "gfx/gl/shaders/vs-shadowDynamicPoint.h" #include "gfx/gl/shaders/vs-shadowDynamicPointInst.h" #include "gfx/gl/shaders/vs-shadowFixedPoint.h" @@ -17,19 +18,21 @@ #include ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : - fixedPoint {shadowFixedPoint_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s} + fixedPoint {shadowFixedPoint_vs, commonShadowPoint_gs}, + dynamicPointInst {shadowDynamicPointInst_vs, commonShadowPoint_gs}, size {s} { - glBindTexture(GL_TEXTURE_2D, depthMap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glBindTexture(GL_TEXTURE_2D_ARRAY, depthMap); + glTexImage3D( + GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT, size.x, size.y, 4, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); static constexpr RGBA border {std::numeric_limits::infinity()}; - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, border); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0); glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { @@ -130,19 +133,16 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glClear(GL_DEPTH_BUFFER_BIT); glCullFace(GL_FRONT); + glViewport(0, 0, size.x, size.y); const auto lightViewDir = glm::lookAt({}, dir, up); const auto lightViewPoint = camera.getPosition(); const auto bandViewExtents = getBandViewExtents(camera, lightViewDir); - fixedPoint.setViewPoint(lightViewPoint); - dynamicPoint.setViewPoint(lightViewPoint); - dynamicPointInst.setViewPoint(lightViewPoint); - Definitions out; std::transform(bandViewExtents.begin(), std::prev(bandViewExtents.end()), std::next(bandViewExtents.begin()), DefinitionsInserter {out}, - [&scene, this, bands = bandViewExtents.size() - 2, &out, &lightViewDir]( - const auto & near, const auto & far) { + [this, bands = bandViewExtents.size() - 2, &out, &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); return std::make_pair(comp.get(*mm.first), comp.get(*mm.second)); @@ -153,39 +153,46 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); const auto lightViewDirProjection = lightProjection * lightViewDir; - fixedPoint.setViewProjection(lightViewDirProjection); - dynamicPoint.setViewProjection(lightViewDirProjection); - dynamicPointInst.setViewProjection(lightViewDirProjection); - - const auto & viewport = viewports[bands][out.maps]; - glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w); - scene.shadows(*this); + fixedPoint.setViewProjection(lightViewDirProjection, out.maps); + dynamicPoint.setViewProjection(lightViewDirProjection, out.maps); + dynamicPointInst.setViewProjection(lightViewDirProjection, out.maps); - return std::make_pair(lightViewDirProjection, shadowMapRegions[bands][out.maps]); + return std::make_pair(lightViewDirProjection, shadowMapRegions[0][0]); }); + fixedPoint.setViewPoint(lightViewPoint, out.maps); + dynamicPoint.setViewPoint(lightViewPoint, out.maps); + dynamicPointInst.setViewPoint(lightViewPoint, out.maps); + scene.shadows(*this); glCullFace(GL_BACK); return out; } -ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : - Program {vs}, viewProjectionLoc {*this, "viewProjection"}, viewPointLoc {*this, "viewPoint"} +ShadowMapper::FixedPoint::FixedPoint(const Shader & vs, const Shader & gs) : + Program {vs, gs}, viewProjectionLoc {{ + {*this, "viewProjection[0]"}, + {*this, "viewProjection[1]"}, + {*this, "viewProjection[2]"}, + {*this, "viewProjection[3]"}, + }}, + viewProjectionsLoc {*this, "viewProjections"}, viewPointLoc {*this, "viewPoint"} { } void -ShadowMapper::FixedPoint::setViewPoint(const GlobalPosition3D viewPoint) const +ShadowMapper::FixedPoint::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const { use(); glUniform(viewPointLoc, viewPoint); + glUniform(viewProjectionsLoc, static_cast(n)); } void -ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection) const +ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection, size_t n) const { use(); - glUniform(viewProjectionLoc, viewProjection); + glUniform(viewProjectionLoc[n], viewProjection); } void @@ -195,23 +202,30 @@ ShadowMapper::FixedPoint::use() const } ShadowMapper::DynamicPoint::DynamicPoint() : - Program {shadowDynamicPoint_vs}, viewProjectionLoc {*this, "viewProjection"}, viewPointLoc {*this, "viewPoint"}, - modelLoc {*this, "model"}, modelPosLoc {*this, "modelPos"} + Program {shadowDynamicPoint_vs, commonShadowPoint_gs}, viewProjectionLoc {{ + {*this, "viewProjection[0]"}, + {*this, "viewProjection[1]"}, + {*this, "viewProjection[2]"}, + {*this, "viewProjection[3]"}, + }}, + viewProjectionsLoc {*this, "viewProjections"}, viewPointLoc {*this, "viewPoint"}, modelLoc {*this, "model"}, + modelPosLoc {*this, "modelPos"} { } void -ShadowMapper::DynamicPoint::setViewPoint(const GlobalPosition3D viewPoint) const +ShadowMapper::DynamicPoint::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const { glUseProgram(*this); glUniform(viewPointLoc, viewPoint); + glUniform(viewProjectionsLoc, static_cast(n)); } void -ShadowMapper::DynamicPoint::setViewProjection(const glm::mat4 & viewProjection) const +ShadowMapper::DynamicPoint::setViewProjection(const glm::mat4 & viewProjection, size_t n) const { glUseProgram(*this); - glUniform(viewProjectionLoc, viewProjection); + glUniform(viewProjectionLoc[n], viewProjection); } void diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 065db4f..421e6a5 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -26,25 +26,29 @@ public: class FixedPoint : public Program { public: - FixedPoint(const Shader & vs); - void setViewPoint(const GlobalPosition3D) const; - void setViewProjection(const glm::mat4 &) const; + FixedPoint(const Shader & vs, const Shader & gs); + void setViewPoint(const GlobalPosition3D, size_t n) const; + void setViewProjection(const glm::mat4 &, size_t n) const; void use() const; private: - RequiredUniformLocation viewProjectionLoc, viewPointLoc; + std::array viewProjectionLoc; + RequiredUniformLocation viewProjectionsLoc; + RequiredUniformLocation viewPointLoc; }; class DynamicPoint : public Program { public: DynamicPoint(); - void setViewPoint(const GlobalPosition3D) const; - void setViewProjection(const glm::mat4 &) const; + void setViewPoint(const GlobalPosition3D, size_t n) const; + void setViewProjection(const glm::mat4 &, size_t n) const; void use(const Location &) const; void setModel(const Location &) const; private: - RequiredUniformLocation viewProjectionLoc, viewPointLoc; + std::array viewProjectionLoc; + RequiredUniformLocation viewProjectionsLoc; + RequiredUniformLocation viewPointLoc; RequiredUniformLocation modelLoc; RequiredUniformLocation modelPosLoc; }; -- cgit v1.2.3 From a18ea136a540d271687fd88831bc14394107dba5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 29 Jan 2024 00:18:02 +0000 Subject: Merge common parts of shadow programs into a base class --- gfx/gl/shadowMapper.cpp | 49 ++++++++++++++----------------------------------- gfx/gl/shadowMapper.h | 17 +++++++++-------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 7770c9d..4141278 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -18,8 +18,7 @@ #include ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : - fixedPoint {shadowFixedPoint_vs, commonShadowPoint_gs}, - dynamicPointInst {shadowDynamicPointInst_vs, commonShadowPoint_gs}, size {s} + fixedPoint {shadowFixedPoint_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s} { glBindTexture(GL_TEXTURE_2D_ARRAY, depthMap); glTexImage3D( @@ -169,19 +168,19 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const return out; } -ShadowMapper::FixedPoint::FixedPoint(const Shader & vs, const Shader & gs) : - Program {vs, gs}, viewProjectionLoc {{ - {*this, "viewProjection[0]"}, - {*this, "viewProjection[1]"}, - {*this, "viewProjection[2]"}, - {*this, "viewProjection[3]"}, - }}, +ShadowMapper::ShadowProgram::ShadowProgram(const Shader & vs) : + Program {vs, commonShadowPoint_gs}, viewProjectionLoc {{ + {*this, "viewProjection[0]"}, + {*this, "viewProjection[1]"}, + {*this, "viewProjection[2]"}, + {*this, "viewProjection[3]"}, + }}, viewProjectionsLoc {*this, "viewProjections"}, viewPointLoc {*this, "viewPoint"} { } void -ShadowMapper::FixedPoint::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const +ShadowMapper::ShadowProgram::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const { use(); glUniform(viewPointLoc, viewPoint); @@ -189,43 +188,23 @@ ShadowMapper::FixedPoint::setViewPoint(const GlobalPosition3D viewPoint, size_t } void -ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection, size_t n) const +ShadowMapper::ShadowProgram::setViewProjection(const glm::mat4 & viewProjection, size_t n) const { use(); glUniform(viewProjectionLoc[n], viewProjection); } void -ShadowMapper::FixedPoint::use() const +ShadowMapper::ShadowProgram::use() const { glUseProgram(*this); } -ShadowMapper::DynamicPoint::DynamicPoint() : - Program {shadowDynamicPoint_vs, commonShadowPoint_gs}, viewProjectionLoc {{ - {*this, "viewProjection[0]"}, - {*this, "viewProjection[1]"}, - {*this, "viewProjection[2]"}, - {*this, "viewProjection[3]"}, - }}, - viewProjectionsLoc {*this, "viewProjections"}, viewPointLoc {*this, "viewPoint"}, modelLoc {*this, "model"}, - modelPosLoc {*this, "modelPos"} -{ -} - -void -ShadowMapper::DynamicPoint::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const -{ - glUseProgram(*this); - glUniform(viewPointLoc, viewPoint); - glUniform(viewProjectionsLoc, static_cast(n)); -} +ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : ShadowProgram {vs} { } -void -ShadowMapper::DynamicPoint::setViewProjection(const glm::mat4 & viewProjection, size_t n) const +ShadowMapper::DynamicPoint::DynamicPoint() : + ShadowProgram {shadowDynamicPoint_vs}, modelLoc {*this, "model"}, modelPosLoc {*this, "modelPos"} { - glUseProgram(*this); - glUniform(viewProjectionLoc[n], viewProjection); } void diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 421e6a5..4f81e58 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -24,9 +24,10 @@ public: [[nodiscard]] Definitions update(const SceneProvider &, const Direction3D & direction, const Camera &) const; - class FixedPoint : public Program { + class ShadowProgram : public Program { public: - FixedPoint(const Shader & vs, const Shader & gs); + explicit ShadowProgram(const Shader & vs); + void setViewPoint(const GlobalPosition3D, size_t n) const; void setViewProjection(const glm::mat4 &, size_t n) const; void use() const; @@ -37,18 +38,18 @@ public: RequiredUniformLocation viewPointLoc; }; - class DynamicPoint : public Program { + class FixedPoint : public ShadowProgram { + public: + explicit FixedPoint(const Shader & vs); + }; + + class DynamicPoint : public ShadowProgram { public: DynamicPoint(); - void setViewPoint(const GlobalPosition3D, size_t n) const; - void setViewProjection(const glm::mat4 &, size_t n) const; void use(const Location &) const; void setModel(const Location &) const; private: - std::array viewProjectionLoc; - RequiredUniformLocation viewProjectionsLoc; - RequiredUniformLocation viewPointLoc; RequiredUniformLocation modelLoc; RequiredUniformLocation modelPosLoc; }; -- cgit v1.2.3 From 83c43ea9e39f4e6ed5e50ccced66700af39987be Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 29 Jan 2024 01:02:51 +0000 Subject: Set shadow program uniforms all in one function, merge setting of view projections --- gfx/gl/shadowMapper.cpp | 33 ++++++++++----------------------- gfx/gl/shadowMapper.h | 6 +++--- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 4141278..f3d5dea 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -140,8 +140,7 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const Definitions out; std::transform(bandViewExtents.begin(), std::prev(bandViewExtents.end()), std::next(bandViewExtents.begin()), DefinitionsInserter {out}, - [this, bands = bandViewExtents.size() - 2, &out, &lightViewDir]( - const auto & near, const auto & far) mutable { + [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); return std::make_pair(comp.get(*mm.first), comp.get(*mm.second)); @@ -152,15 +151,13 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); const auto lightViewDirProjection = lightProjection * lightViewDir; - fixedPoint.setViewProjection(lightViewDirProjection, out.maps); - dynamicPoint.setViewProjection(lightViewDirProjection, out.maps); - dynamicPointInst.setViewProjection(lightViewDirProjection, out.maps); return std::make_pair(lightViewDirProjection, shadowMapRegions[0][0]); }); - fixedPoint.setViewPoint(lightViewPoint, out.maps); - dynamicPoint.setViewPoint(lightViewPoint, out.maps); - dynamicPointInst.setViewPoint(lightViewPoint, out.maps); + std::span vps {out.projections.data(), out.maps}; + for (const auto p : std::initializer_list {&fixedPoint, &dynamicPoint, &dynamicPointInst}) { + p->setView(vps, lightViewPoint); + } scene.shadows(*this); glCullFace(GL_BACK); @@ -169,29 +166,19 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const } ShadowMapper::ShadowProgram::ShadowProgram(const Shader & vs) : - Program {vs, commonShadowPoint_gs}, viewProjectionLoc {{ - {*this, "viewProjection[0]"}, - {*this, "viewProjection[1]"}, - {*this, "viewProjection[2]"}, - {*this, "viewProjection[3]"}, - }}, + Program {vs, commonShadowPoint_gs}, viewProjectionLoc {*this, "viewProjection"}, viewProjectionsLoc {*this, "viewProjections"}, viewPointLoc {*this, "viewPoint"} { } void -ShadowMapper::ShadowProgram::setViewPoint(const GlobalPosition3D viewPoint, size_t n) const +ShadowMapper::ShadowProgram::setView( + const std::span viewProjection, const GlobalPosition3D viewPoint) const { use(); glUniform(viewPointLoc, viewPoint); - glUniform(viewProjectionsLoc, static_cast(n)); -} - -void -ShadowMapper::ShadowProgram::setViewProjection(const glm::mat4 & viewProjection, size_t n) const -{ - use(); - glUniform(viewProjectionLoc[n], viewProjection); + glUniform(viewProjectionLoc, viewProjection); + glUniform(viewProjectionsLoc, static_cast(viewProjection.size())); } void diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 4f81e58..b948f50 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -5,6 +5,7 @@ #include "program.h" #include #include +#include #include class SceneProvider; @@ -28,12 +29,11 @@ public: public: explicit ShadowProgram(const Shader & vs); - void setViewPoint(const GlobalPosition3D, size_t n) const; - void setViewProjection(const glm::mat4 &, size_t n) const; + void setView(const std::span, const GlobalPosition3D) const; void use() const; private: - std::array viewProjectionLoc; + RequiredUniformLocation viewProjectionLoc; RequiredUniformLocation viewProjectionsLoc; RequiredUniformLocation viewPointLoc; }; -- cgit v1.2.3 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(-) 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 From 72ec5dd096a3110e8085919f4c0e3320ef176657 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 30 Jan 2024 22:13:56 +0000 Subject: Adjust light view projection matrix upfront Saves doing it per pixel, per region later --- gfx/gl/sceneRenderer.cpp | 8 +++++++- gfx/gl/shaders/directionalLight.fs | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 2dd1760..517d51e 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -137,14 +137,20 @@ SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : { } +const auto toTextureSpaceMat = glm::translate(glm::identity(), glm::vec3 {0.5F}) + * glm::scale(glm::identity(), glm::vec3 {0.5F}); + void SceneRenderer::DirectionalLightProgram::setDirectionalLight( const RGB & c, const Direction3D & d, const GlobalPosition3D & p, const std::span lvp) const { + const auto toTextureSpace = [](const glm::mat4 & m) { + return toTextureSpaceMat * m; + }; glUniform(colourLoc, c); const auto nd = glm::normalize(d); glUniform(directionLoc, nd); glUniform(lightPointLoc, p); glUniform(lightViewProjectionCountLoc, static_cast(lvp.size())); - glUniform(lightViewProjectionLoc, lvp); + glUniform(lightViewProjectionLoc, std::span {lvp * toTextureSpace}); } diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index c49bb8d..24457b8 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -19,18 +19,18 @@ uniform uint lightViewProjectionCount; 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 * .5 + .5; + const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r; @@ -40,7 +40,7 @@ isShaded(vec4 Position) return 0; } - void +void main() { const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); -- cgit v1.2.3