candlewick 0.10.0
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 "GraphicsPipeline.h"
30#include "math_types.h"
31#include "LightUniforms.h"
32
33#include <span>
34
35namespace candlewick {
36
39using OpaqueCastable = std::tuple<const Mesh &, Mat4f>;
40
42static constexpr size_t kNumLights = 4;
43
49class DepthPass {
50 SDL_GPUDevice *_device = nullptr;
51
52public:
53 static constexpr Uint32 TRANSFORM_SLOT = 0;
54 struct Config {
55 SDL_GPUCullMode cull_mode;
60 const char *pipeline_name;
61 SDL_GPUSampleCount sample_count;
62 };
63
64 SDL_GPUTexture *depthTexture = nullptr;
66
68
79 DepthPass(const Device &device, const MeshLayout &layout,
80 SDL_GPUTexture *depth_texture, SDL_GPUTextureFormat format,
81 const Config &config = {});
82
91 DepthPass(const Device &device, const MeshLayout &layout,
92 const Texture &texture, const Config &config = {})
93 : DepthPass(device, layout, texture, texture.format(), config) {}
94
95 DepthPass(const DepthPass &) = delete;
96 DepthPass &operator=(const DepthPass &) = delete;
97 DepthPass(DepthPass &&other) noexcept;
98 DepthPass &operator=(DepthPass &&other) noexcept;
99
100 void render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
101 std::span<const OpaqueCastable> castables);
102
104 void release() noexcept;
106 ~DepthPass() noexcept { this->release(); }
107};
108
111 // default is 2k x 2k texture
112 Uint32 width = 2048;
113 Uint32 height = 2048;
116 bool enable_depth_bias = false;
117 bool enable_depth_clip = false;
118 Uint32 numLights = 2;
119};
120
128 SDL_GPUDevice *m_device = nullptr;
129 Uint32 m_numLights = 0;
130
131 void configureAtlasRegions(const ShadowPassConfig &config);
132
133public:
136 struct AtlasRegion {
137 Uint32 x;
138 Uint32 y;
139 Uint32 w;
140 Uint32 h;
141 };
142
145 SDL_GPUSampler *sampler = nullptr;
146 std::array<Camera, kNumLights> cam;
148 std::array<AtlasRegion, kNumLights> regions;
149
151
152 ShadowMapPass(const Device &device, const MeshLayout &layout,
153 SDL_GPUTextureFormat format, const Config &config);
154
155 ShadowMapPass(const Device &device, const MeshLayout &layout,
156 SDL_GPUTextureFormat format)
157 : ShadowMapPass(device, layout, format, {}) {}
158
159 ShadowMapPass(const ShadowMapPass &) = delete;
161 ShadowMapPass(ShadowMapPass &&other) noexcept;
163
164 bool initialized() const {
165 return pipeline.initialized() && (sampler != nullptr);
166 }
167
168 void render(CommandBuffer &cmdBuf, std::span<const OpaqueCastable> castables);
169
170 void release() noexcept;
171 ~ShadowMapPass() noexcept { this->release(); }
172
173 auto numLights() const noexcept { return m_numLights; }
174};
175
185
195 std::span<const DirectionalLight> dirLight,
196 std::span<const OpaqueCastable> castables,
197 const AABB &worldAABB);
198
208 std::span<const DirectionalLight> dirLight,
209 std::span<const OpaqueCastable> castables,
210 const FrustumCornersType &worldSpaceCorners);
211
228inline Mat4f shadowOrthographicMatrix(const Float2 &sizes, float zMin,
229 float zMax) {
230 const float sx = 2.f / sizes.x();
231 const float sy = 2.f / sizes.y();
232 const float sz = 1.f / (zMin - zMax);
233 const float pz = 0.5f * (zMin + zMax) * sz;
234
235 Mat4f proj = Mat4f::Zero();
236 proj(0, 0) = sx;
237 proj(1, 1) = sy;
238 proj(2, 2) = sz;
239 proj(2, 3) = -pz;
240 proj(3, 3) = 1.f;
241 return proj;
242}
243
245
246} // namespace candlewick
Definition CommandBuffer.h:16
Helper struct for depth or light pre-passes.
Definition DepthAndShadowPass.h:49
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:67
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:64
GraphicsPipeline pipeline
Definition DepthAndShadowPass.h:65
static constexpr Uint32 TRANSFORM_SLOT
Definition DepthAndShadowPass.h:53
DepthPass(const Device &device, const MeshLayout &layout, const Texture &texture, const Config &config={})
Create DepthPass (e.g. for a depth pre-pass) from a Device, specified mesh layout,...
Definition DepthAndShadowPass.h:91
Class representing a graphics pipeline.
Definition GraphicsPipeline.h:15
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:127
auto numLights() const noexcept
Definition DepthAndShadowPass.h:173
ShadowMapPass & operator=(const ShadowMapPass &)=delete
ShadowMapPass(ShadowMapPass &&other) noexcept
ShadowPassConfig Config
Definition DepthAndShadowPass.h:134
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format, const Config &config)
ShadowMapPass(NoInitT)
Definition DepthAndShadowPass.h:150
bool initialized() const
Definition DepthAndShadowPass.h:164
GraphicsPipeline pipeline
Definition DepthAndShadowPass.h:144
ShadowMapPass(const ShadowMapPass &)=delete
Texture shadowMap
actually a texture atlas
Definition DepthAndShadowPass.h:143
SDL_GPUSampler * sampler
Definition DepthAndShadowPass.h:145
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format)
Definition DepthAndShadowPass.h:155
std::array< Camera, kNumLights > cam
Definition DepthAndShadowPass.h:146
ShadowMapPass & operator=(ShadowMapPass &&other) noexcept
void render(CommandBuffer &cmdBuf, std::span< const OpaqueCastable > castables)
void release() noexcept
std::array< AtlasRegion, kNumLights > regions
regions of the atlas
Definition DepthAndShadowPass.h:148
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:228
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:39
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:54
bool enable_depth_bias
Definition DepthAndShadowPass.h:58
const char * pipeline_name
Definition DepthAndShadowPass.h:60
SDL_GPUCullMode cull_mode
Definition DepthAndShadowPass.h:55
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:56
bool enable_depth_clip
Definition DepthAndShadowPass.h:59
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:57
SDL_GPUSampleCount sample_count
Definition DepthAndShadowPass.h:61
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:136
Uint32 x
Definition DepthAndShadowPass.h:137
Uint32 y
Definition DepthAndShadowPass.h:138
Uint32 w
Definition DepthAndShadowPass.h:139
Uint32 h
Definition DepthAndShadowPass.h:140
Definition DepthAndShadowPass.h:110
bool enable_depth_clip
Definition DepthAndShadowPass.h:117
bool enable_depth_bias
Definition DepthAndShadowPass.h:116
Uint32 width
Definition DepthAndShadowPass.h:112
Uint32 height
Definition DepthAndShadowPass.h:113
Uint32 numLights
Definition DepthAndShadowPass.h:118
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:115
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:114