candlewick 0.7.0-59-g23c6
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
DepthAndShadowPass.h
Go to the documentation of this file.
1
23#pragma once
24
25#include "Core.h"
26#include "Tags.h"
27#include "Texture.h"
28#include "Camera.h"
29#include "math_types.h"
30#include "LightUniforms.h"
31
32#include <span>
33
34namespace candlewick {
35
38using OpaqueCastable = std::tuple<const Mesh &, Mat4f>;
39
41static constexpr size_t kNumLights = 4;
42
48class DepthPass {
49 SDL_GPUDevice *_device = nullptr;
50
51public:
52 static constexpr Uint32 TRANSFORM_SLOT = 0;
60
61 SDL_GPUTexture *depthTexture = nullptr;
62 SDL_GPUGraphicsPipeline *pipeline = nullptr;
63
65
76 DepthPass(const Device &device, const MeshLayout &layout,
77 SDL_GPUTexture *depth_texture, SDL_GPUTextureFormat format,
78 const Config &config = {});
79
88 DepthPass(const Device &device, const MeshLayout &layout,
89 const Texture &depth_texture, const Config &config = {});
90
91 DepthPass(const DepthPass &) = delete;
92 DepthPass &operator=(const DepthPass &) = delete;
93 DepthPass(DepthPass &&other) noexcept;
94 DepthPass &operator=(DepthPass &&other) noexcept;
95
96 void render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
97 std::span<const OpaqueCastable> castables);
98
100 void release() noexcept;
101 ~DepthPass() noexcept { this->release(); }
102};
103
106 // default is 2k x 2k texture
107 Uint32 width = 2048;
108 Uint32 height = 2048;
111 bool enable_depth_bias = false;
112 bool enable_depth_clip = false;
113 Uint32 numLights = 2;
114};
115
123 SDL_GPUDevice *_device = nullptr;
124 Uint32 _numLights = 0;
125
126 void configureAtlasRegions(const ShadowPassConfig &config);
127
128public:
131 struct AtlasRegion {
132 Uint32 x;
133 Uint32 y;
134 Uint32 w;
135 Uint32 h;
136 };
137
139 SDL_GPUGraphicsPipeline *pipeline = nullptr;
140 SDL_GPUSampler *sampler = nullptr;
141 std::array<Camera, kNumLights> cam;
143 std::array<AtlasRegion, kNumLights> regions;
144
146
147 ShadowMapPass(const Device &device, const MeshLayout &layout,
148 SDL_GPUTextureFormat format, const Config &config);
149
150 ShadowMapPass(const Device &device, const MeshLayout &layout,
151 SDL_GPUTextureFormat format)
152 : ShadowMapPass(device, layout, format, {}) {}
153
154 ShadowMapPass(const ShadowMapPass &) = delete;
156 ShadowMapPass(ShadowMapPass &&other) noexcept;
158
159 bool initialized() const {
160 return (pipeline != nullptr) && (sampler != nullptr);
161 }
162
163 void render(CommandBuffer &cmdBuf, std::span<const OpaqueCastable> castables);
164
165 void release() noexcept;
166 ~ShadowMapPass() noexcept { this->release(); }
167
168 auto numLights() const noexcept { return _numLights; }
169};
170
180
190 std::span<const DirectionalLight> dirLight,
191 std::span<const OpaqueCastable> castables,
192 const AABB &worldAABB);
193
203 std::span<const DirectionalLight> dirLight,
204 std::span<const OpaqueCastable> castables,
205 const FrustumCornersType &worldSpaceCorners);
206
223inline Mat4f shadowOrthographicMatrix(const Float2 &sizes, float zMin,
224 float zMax) {
225 const float sx = 2.f / sizes.x();
226 const float sy = 2.f / sizes.y();
227 const float sz = 1.f / (zMin - zMax);
228 const float pz = 0.5f * (zMin + zMax) * sz;
229
230 Mat4f proj = Mat4f::Zero();
231 proj(0, 0) = sx;
232 proj(1, 1) = sy;
233 proj(2, 2) = sz;
234 proj(2, 3) = -pz;
235 proj(3, 3) = 1.f;
236 return proj;
237}
238
240
241} // namespace candlewick
Definition CommandBuffer.h:17
DepthPass(const Device &device, const MeshLayout &layout, const Texture &depth_texture, const Config &config={})
Create DepthPass (e.g. for a depth pre-pass) from a Device, specified mesh layout,...
SDL_GPUGraphicsPipeline * pipeline
Definition DepthAndShadowPass.h:62
DepthPass(const DepthPass &)=delete
DepthPass & operator=(DepthPass &&other) noexcept
DepthPass & operator=(const DepthPass &)=delete
DepthPass(DepthPass &&other) noexcept
DepthPass(const Device &device, const MeshLayout &layout, SDL_GPUTexture *depth_texture, SDL_GPUTextureFormat format, const Config &config={})
Create DepthPass (e.g. for a depth pre-pass) from a Device, specified mesh layout,...
DepthPass(NoInitT)
Definition DepthAndShadowPass.h:64
void release() noexcept
Release the pass resources.
void render(CommandBuffer &cmdBuf, const Mat4f &viewProj, std::span< const OpaqueCastable > castables)
SDL_GPUTexture * depthTexture
Definition DepthAndShadowPass.h:61
static constexpr Uint32 TRANSFORM_SLOT
Definition DepthAndShadowPass.h:52
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:122
Class for defining the shadow maps (as an atlas) and rendering into it.
Definition DepthAndShadowPass.h:122
auto numLights() const noexcept
Definition DepthAndShadowPass.h:168
ShadowMapPass & operator=(const ShadowMapPass &)=delete
ShadowMapPass(ShadowMapPass &&other) noexcept
ShadowPassConfig Config
Definition DepthAndShadowPass.h:129
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format, const Config &config)
ShadowMapPass(NoInitT)
Definition DepthAndShadowPass.h:145
bool initialized() const
Definition DepthAndShadowPass.h:159
ShadowMapPass(const ShadowMapPass &)=delete
Texture shadowMap
actually a texture atlas
Definition DepthAndShadowPass.h:138
SDL_GPUSampler * sampler
Definition DepthAndShadowPass.h:140
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format)
Definition DepthAndShadowPass.h:150
std::array< Camera, kNumLights > cam
Definition DepthAndShadowPass.h:141
ShadowMapPass & operator=(ShadowMapPass &&other) noexcept
SDL_GPUGraphicsPipeline * pipeline
Definition DepthAndShadowPass.h:139
void render(CommandBuffer &cmdBuf, std::span< const OpaqueCastable > castables)
void release() noexcept
std::array< AtlasRegion, kNumLights > regions
regions of the atlas
Definition DepthAndShadowPass.h:143
Definition Texture.h:9
void renderShadowPassFromAABB(CommandBuffer &cmdBuf, ShadowMapPass &passInfo, std::span< const DirectionalLight > dirLight, std::span< const OpaqueCastable > castables, const AABB &worldAABB)
Render shadow pass, using provided scene bounds.
void renderShadowPassFromFrustum(CommandBuffer &cmdBuf, ShadowMapPass &passInfo, std::span< const DirectionalLight > dirLight, std::span< const OpaqueCastable > castables, const FrustumCornersType &worldSpaceCorners)
Render shadow pass, using a provided world-space frustum.
Mat4f shadowOrthographicMatrix(const Float2 &sizes, float zMin, float zMax)
Orthographic matrix which maps to the negative-Z half-volume of the NDC cube, for depth-testing/shado...
Definition DepthAndShadowPass.h:223
Definition Camera.h:8
std::tuple< const Mesh &, Mat4f > OpaqueCastable
Intermediary argument type for shadow-casting or opaque objects. For use in depth or light pre-passes...
Definition DepthAndShadowPass.h:38
std::array< Float3, 8ul > FrustumCornersType
Definition math_types.h:15
Eigen::Vector2f Float2
Definition math_types.h:7
Eigen::Matrix4f Mat4f
Definition math_types.h:11
constexpr NoInitT NoInit
Definition Tags.h:9
Definition DepthAndShadowPass.h:53
bool enable_depth_bias
Definition DepthAndShadowPass.h:57
SDL_GPUCullMode cull_mode
Definition DepthAndShadowPass.h:54
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:55
bool enable_depth_clip
Definition DepthAndShadowPass.h:58
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:56
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6
Texture atlas region, implicitly converts to an SDLGPU viewport.
Definition DepthAndShadowPass.h:131
Uint32 x
Definition DepthAndShadowPass.h:132
Uint32 y
Definition DepthAndShadowPass.h:133
Uint32 w
Definition DepthAndShadowPass.h:134
Uint32 h
Definition DepthAndShadowPass.h:135
Definition DepthAndShadowPass.h:105
bool enable_depth_clip
Definition DepthAndShadowPass.h:112
bool enable_depth_bias
Definition DepthAndShadowPass.h:111
Uint32 width
Definition DepthAndShadowPass.h:107
Uint32 height
Definition DepthAndShadowPass.h:108
Uint32 numLights
Definition DepthAndShadowPass.h:113
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:110
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:109