candlewick 0.1.0
A renderer
Loading...
Searching...
No Matches
DepthAndShadowPass.h
Go to the documentation of this file.
1
21#pragma once
22
23#include "Core.h"
24#include "Tags.h"
25#include "Texture.h"
26#include "Camera.h"
27#include "math_types.h"
28#include "LightUniforms.h"
29
30#include <SDL3/SDL_gpu.h>
31#include <span>
32
33namespace candlewick {
34
40class DepthPass {
41 SDL_GPUDevice *_device = nullptr;
42
43public:
44 static constexpr Uint32 TRANSFORM_SLOT = 0;
52
53 SDL_GPUTexture *depthTexture = nullptr;
54 SDL_GPUGraphicsPipeline *pipeline = nullptr;
55
57
68 DepthPass(const Device &device, const MeshLayout &layout,
69 SDL_GPUTexture *depth_texture, SDL_GPUTextureFormat format,
70 const Config &config = {});
71
80 DepthPass(const Device &device, const MeshLayout &layout,
81 const Texture &depth_texture, const Config &config = {});
82
83 DepthPass(const DepthPass &) = delete;
84 DepthPass &operator=(const DepthPass &) = delete;
85 DepthPass(DepthPass &&other) noexcept;
86 DepthPass &operator=(DepthPass &&other) noexcept;
87
88 void render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
89 std::span<const OpaqueCastable> castables);
90
92 void release() noexcept;
93 ~DepthPass() noexcept { this->release(); }
94};
95
99 SDL_GPUDevice *_device = nullptr;
100 DepthPass _depthPass{NoInit};
101
102public:
103 struct Config {
104 // default is 2k x 2k texture
105 Uint32 width = 2048;
106 Uint32 height = 2048;
108 .cull_mode = SDL_GPU_CULLMODE_NONE,
109 .depth_bias_constant_factor = 0.f,
110 .depth_bias_slope_factor = 0.f,
111 .enable_depth_bias = false,
112 .enable_depth_clip = false,
113 };
114 };
116 SDL_GPUSampler *sampler = nullptr;
118
120
121 ShadowMapPass(const Device &device, const MeshLayout &layout,
122 SDL_GPUTextureFormat format, const Config &config);
123
124 ShadowMapPass(const Device &device, const MeshLayout &layout,
125 SDL_GPUTextureFormat format)
126 : ShadowMapPass(device, layout, format, {}) {}
127
128 ShadowMapPass(const ShadowMapPass &) = delete;
130 ShadowMapPass(ShadowMapPass &&other) noexcept;
132
133 void render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
134 std::span<const OpaqueCastable> castables);
135
136 void release() noexcept;
137 ~ShadowMapPass() noexcept { this->release(); }
138};
139
141
151
152inline void ShadowMapPass::render(CommandBuffer &cmdBuf, const Mat4f &viewProj,
153 std::span<const OpaqueCastable> castables) {
154 _depthPass.render(cmdBuf, viewProj, castables);
155}
156
163 const DirectionalLight &dirLight,
164 std::span<const OpaqueCastable> castables,
165 const AABB &worldSceneBounds);
166
175 const DirectionalLight &dirLight,
176 std::span<const OpaqueCastable> castables,
177 const FrustumCornersType &worldSpaceCorners);
178
195inline Mat4f shadowOrthographicMatrix(const Float2 &sizes, float zMin,
196 float zMax) {
197 const float sx = 2.f / sizes.x();
198 const float sy = 2.f / sizes.y();
199 const float sz = 1.f / (zMin - zMax);
200 const float pz = 0.5f * (zMin + zMax) * sz;
201
202 Mat4f proj = Mat4f::Zero();
203 proj(0, 0) = sx;
204 proj(1, 1) = sy;
205 proj(2, 2) = sz;
206 proj(2, 3) = -pz;
207 proj(3, 3) = 1.f;
208 return proj;
209}
210
212
213} // namespace candlewick
Definition CommandBuffer.h:17
Helper struct for depth or light pre-passes.
Definition DepthAndShadowPass.h:40
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:54
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:56
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:53
static constexpr Uint32 TRANSFORM_SLOT
Definition DepthAndShadowPass.h:44
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:98
Helper struct for shadow mapping pass.
Definition DepthAndShadowPass.h:98
Camera cam
Definition DepthAndShadowPass.h:117
ShadowMapPass & operator=(const ShadowMapPass &)=delete
ShadowMapPass(ShadowMapPass &&other) noexcept
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format, const Config &config)
ShadowMapPass(NoInitT)
Definition DepthAndShadowPass.h:119
void render(CommandBuffer &cmdBuf, const Mat4f &viewProj, std::span< const OpaqueCastable > castables)
Definition DepthAndShadowPass.h:152
ShadowMapPass(const ShadowMapPass &)=delete
Texture shadowMap
Definition DepthAndShadowPass.h:115
SDL_GPUSampler * sampler
Definition DepthAndShadowPass.h:116
ShadowMapPass(const Device &device, const MeshLayout &layout, SDL_GPUTextureFormat format)
Definition DepthAndShadowPass.h:124
ShadowMapPass & operator=(ShadowMapPass &&other) noexcept
void release() noexcept
Definition Texture.h:9
void renderShadowPassFromAABB(CommandBuffer &cmdBuf, ShadowMapPass &passInfo, const DirectionalLight &dirLight, std::span< const OpaqueCastable > castables, const AABB &worldSceneBounds)
Render shadow pass, using provided scene bounds.
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:195
void renderShadowPassFromFrustum(CommandBuffer &cmdBuf, ShadowMapPass &passInfo, const DirectionalLight &dirLight, std::span< const OpaqueCastable > castables, const FrustumCornersType &worldSpaceCorners)
Render shadow pass, using a provided world-space frustum.
Definition Camera.h:8
ShadowMapPass::Config ShadowPassConfig
Definition DepthAndShadowPass.h:140
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
The main way of using a camera to render things.
Definition Camera.h:19
Definition DepthAndShadowPass.h:45
bool enable_depth_bias
Definition DepthAndShadowPass.h:49
SDL_GPUCullMode cull_mode
Definition DepthAndShadowPass.h:46
float depth_bias_constant_factor
Definition DepthAndShadowPass.h:47
bool enable_depth_clip
Definition DepthAndShadowPass.h:50
float depth_bias_slope_factor
Definition DepthAndShadowPass.h:48
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Definition LightUniforms.h:7
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6
Definition DepthAndShadowPass.h:103
DepthPass::Config depthPassConfig
Definition DepthAndShadowPass.h:107
Uint32 width
Definition DepthAndShadowPass.h:105
Uint32 height
Definition DepthAndShadowPass.h:106