candlewick 0.6.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 "math_types.h"
30#include "LightUniforms.h"
31
32#include <SDL3/SDL_gpu.h>
33#include <span>
34
35namespace candlewick {
36
42class DepthPass {
43 SDL_GPUDevice *_device = nullptr;
44
45public:
46 static constexpr Uint32 TRANSFORM_SLOT = 0;
54
55 SDL_GPUTexture *depthTexture = nullptr;
56 SDL_GPUGraphicsPipeline *pipeline = nullptr;
57
59
70 DepthPass(const Device &device, const MeshLayout &layout,
71 SDL_GPUTexture *depth_texture, SDL_GPUTextureFormat format,
72 const Config &config = {});
73
82 DepthPass(const Device &device, const MeshLayout &layout,
83 const Texture &depth_texture, const Config &config = {});
84
85 DepthPass(const DepthPass &) = delete;
86 DepthPass &operator=(const DepthPass &) = delete;
87 DepthPass(DepthPass &&other) noexcept;
88 DepthPass &operator=(DepthPass &&other) noexcept;
89
90 void render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
91 std::span<const OpaqueCastable> castables);
92
94 void release() noexcept;
95 ~DepthPass() noexcept { this->release(); }
96};
97
98static constexpr size_t kNumLights = 4;
99
102 // default is 2k x 2k texture
103 Uint32 width = 2048;
104 Uint32 height = 2048;
107 bool enable_depth_bias = false;
108 bool enable_depth_clip = false;
109 Uint32 numLights = 2;
110};
111
119 SDL_GPUDevice *_device = nullptr;
120 Uint32 _numLights = 0;
121
122 void configureAtlasRegions(const ShadowPassConfig &config);
123
124public:
127 struct AtlasRegion {
128 Uint32 x;
129 Uint32 y;
130 Uint32 w;
131 Uint32 h;
132 };
133
135 SDL_GPUGraphicsPipeline *pipeline = nullptr;
136 SDL_GPUSampler *sampler = nullptr;
137 std::array<Camera, kNumLights> cam;
139 std::array<AtlasRegion, kNumLights> regions;
140
142
143 ShadowMapPass(const Device &device, const MeshLayout &layout,
144 SDL_GPUTextureFormat format, const Config &config);
145
146 ShadowMapPass(const Device &device, const MeshLayout &layout,
147 SDL_GPUTextureFormat format)
148 : ShadowMapPass(device, layout, format, {}) {}
149
150 ShadowMapPass(const ShadowMapPass &) = delete;
152 ShadowMapPass(ShadowMapPass &&other) noexcept;
154
155 void render(CommandBuffer &cmdBuf, std::span<const OpaqueCastable> castables);
156
157 void release() noexcept;
158 ~ShadowMapPass() noexcept { this->release(); }
159
160 auto numLights() const noexcept { return _numLights; }
161};
162
172
182 std::span<const DirectionalLight> dirLight,
183 std::span<const OpaqueCastable> castables,
184 const AABB &worldAABB);
185
195 std::span<const DirectionalLight> dirLight,
196 std::span<const OpaqueCastable> castables,
197 const FrustumCornersType &worldSpaceCorners);
198
215inline Mat4f shadowOrthographicMatrix(const Float2 &sizes, float zMin,
216 float zMax) {
217 const float sx = 2.f / sizes.x();
218 const float sy = 2.f / sizes.y();
219 const float sz = 1.f / (zMin - zMax);
220 const float pz = 0.5f * (zMin + zMax) * sz;
221
222 Mat4f proj = Mat4f::Zero();
223 proj(0, 0) = sx;
224 proj(1, 1) = sy;
225 proj(2, 2) = sz;
226 proj(2, 3) = -pz;
227 proj(3, 3) = 1.f;
228 return proj;
229}
230
232
233} // 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:56
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:58
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:55
static constexpr Uint32 TRANSFORM_SLOT
Definition DepthAndShadowPass.h:46
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:98
Class for defining the shadow maps (as an atlas) and rendering into it.
Definition DepthAndShadowPass.h:118
auto numLights() const noexcept
Definition DepthAndShadowPass.h:160
ShadowMapPass & operator=(const ShadowMapPass &)=delete
ShadowMapPass(ShadowMapPass &&other) noexcept
ShadowPassConfig Config
Definition DepthAndShadowPass.h:125
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format, const Config &config)
ShadowMapPass(NoInitT)
Definition DepthAndShadowPass.h:141
ShadowMapPass(const ShadowMapPass &)=delete
Texture shadowMap
actually a texture atlas
Definition DepthAndShadowPass.h:134
SDL_GPUSampler * sampler
Definition DepthAndShadowPass.h:136
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format)
Definition DepthAndShadowPass.h:146
std::array< Camera, kNumLights > cam
Definition DepthAndShadowPass.h:137
ShadowMapPass & operator=(ShadowMapPass &&other) noexcept
SDL_GPUGraphicsPipeline * pipeline
Definition DepthAndShadowPass.h:135
void render(CommandBuffer &cmdBuf, std::span< const OpaqueCastable > castables)
void release() noexcept
std::array< AtlasRegion, kNumLights > regions
regions of the atlas
Definition DepthAndShadowPass.h:139
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:215
Definition Camera.h:8
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:47
bool enable_depth_bias
Definition DepthAndShadowPass.h:51
SDL_GPUCullMode cull_mode
Definition DepthAndShadowPass.h:48
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:49
bool enable_depth_clip
Definition DepthAndShadowPass.h:52
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:50
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:127
Uint32 x
Definition DepthAndShadowPass.h:128
Uint32 y
Definition DepthAndShadowPass.h:129
Uint32 w
Definition DepthAndShadowPass.h:130
Uint32 h
Definition DepthAndShadowPass.h:131
Definition DepthAndShadowPass.h:101
bool enable_depth_clip
Definition DepthAndShadowPass.h:108
bool enable_depth_bias
Definition DepthAndShadowPass.h:107
Uint32 width
Definition DepthAndShadowPass.h:103
Uint32 height
Definition DepthAndShadowPass.h:104
Uint32 numLights
Definition DepthAndShadowPass.h:109
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:106
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:105