candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
RenderContext.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "Device.h"
5#include "CommandBuffer.h"
6#include "Texture.h"
7#include "Mesh.h"
8#include "Window.h"
9
10#include <span>
11#include <SDL3/SDL_gpu.h>
12
13namespace candlewick {
14
15inline constexpr int sdlSampleToValue(SDL_GPUSampleCount samples) {
16 switch (samples) {
17 case SDL_GPU_SAMPLECOUNT_1:
18 return 1;
19 case SDL_GPU_SAMPLECOUNT_2:
20 return 2;
21 case SDL_GPU_SAMPLECOUNT_4:
22 return 4;
23 case SDL_GPU_SAMPLECOUNT_8:
24 return 8;
25 default:
26 return 0;
27 }
28}
29
37private:
38 Texture colorMsaa{NoInit};
39 Texture colorBuffer{NoInit}; // no MSAA
40 Texture depthBuffer{NoInit}; // no MSAA
41 bool m_msaaEnabled = false;
42 SDL_GPUTexture *swapchain{nullptr};
43
44 void createMsaaTargets(SDL_GPUSampleCount samples);
45 void createRenderTargets(SDL_GPUTextureFormat suggested_depth_format);
46
47public:
50
56 SDL_GPUTextureFormat suggested_depth_format =
57 SDL_GPU_TEXTUREFORMAT_INVALID);
58
59 RenderContext(RenderContext &&) noexcept = default;
60 RenderContext &operator=(RenderContext &&) noexcept = default;
61
62 const Texture &colorTarget() const {
63 return (m_msaaEnabled && colorMsaa) ? colorMsaa : colorBuffer;
64 }
65
66 const Texture &depthTarget() const { return depthBuffer; }
67
68 const Texture &resolvedColorTarget() const { return colorBuffer; }
69
70 bool msaaEnabled() const { return m_msaaEnabled; }
71
72 SDL_GPUSampleCount getMsaaSampleCount() const {
73 if (m_msaaEnabled && colorMsaa) {
74 return colorMsaa.sampleCount();
75 }
76 return SDL_GPU_SAMPLECOUNT_1;
77 }
78
79 void enableMSAA(SDL_GPUSampleCount samples) {
80 if (samples > SDL_GPU_SAMPLECOUNT_1) {
81 m_msaaEnabled = true;
82 createMsaaTargets(samples);
83 if (int sample_size = sdlSampleToValue(samples)) {
84 spdlog::info("MSAA enabled with {:d} samples", sample_size);
85 } else {
86 terminate_with_message("Unrecognized sample count {:d}", sample_size);
87 }
88 } else {
90 }
91 }
92
93 void disableMSAA() {
94 m_msaaEnabled = false;
95 colorMsaa.destroy();
96 spdlog::info("MSAA disabled");
97 }
98
99 bool initialized() const { return bool(device); }
100
103
107
111 bool acquireSwapchain(CommandBuffer &command_buffer);
112
114 bool waitForSwapchain() { return SDL_WaitForGPUSwapchain(device, window); }
115
118 void presentToSwapchain(CommandBuffer &command_buffer);
119
120 SDL_GPUTextureFormat getSwapchainTextureFormat() const {
121 return SDL_GetGPUSwapchainTextureFormat(device, window);
122 }
123
125 inline bool hasDepthTexture() const { return depthBuffer; }
126
127 inline void setSwapchainParameters(SDL_GPUSwapchainComposition composition,
128 SDL_GPUPresentMode present_mode) {
129 SDL_SetGPUSwapchainParameters(device, window, composition, present_mode);
130 }
131
132 SDL_GPUTextureFormat colorFormat() const { return colorBuffer.format(); }
133 SDL_GPUTextureFormat depthFormat() const { return depthBuffer.format(); }
134
135 void destroy() noexcept;
136 ~RenderContext() noexcept { this->destroy(); }
137};
138
139namespace rend {
140
143 void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh);
144
152 void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view);
153
161 void drawViews(SDL_GPURenderPass *pass, std::span<const MeshView> meshViews,
162 Uint32 numInstances = 1);
163
172 inline void draw(SDL_GPURenderPass *pass, const Mesh &mesh,
173 Uint32 numInstances = 1) {
174 drawViews(pass, mesh.views(), numInstances);
175 }
176
182 void drawView(SDL_GPURenderPass *pass, const MeshView &mesh,
183 Uint32 numInstances = 1);
184
186 inline void
187 bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
188 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
189 SDL_BindGPUVertexSamplers(pass, first_slot, bindings.data(),
190 Uint32(bindings.size()));
191 }
192
196 SDL_GPURenderPass *pass, Uint32 first_slot,
197 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
198 bindVertexSamplers(pass, first_slot, std::span(sampler_bindings));
199 }
200
202 inline void
203 bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
204 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
205 SDL_BindGPUFragmentSamplers(pass, first_slot, bindings.data(),
206 Uint32(bindings.size()));
207 }
208
212 SDL_GPURenderPass *pass, Uint32 first_slot,
213 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
214 bindFragmentSamplers(pass, first_slot, std::span(sampler_bindings));
215 }
216
217} // namespace rend
218} // namespace candlewick
Definition CommandBuffer.h:16
A view into a Mesh object.
Definition Mesh.h:22
Handle class for meshes (vertex buffers and an optional index buffer) on the GPU.
Definition Mesh.h:57
std::span< const MeshView > views() const
Definition Mesh.h:92
Definition Texture.h:9
Definition RenderContext.h:139
void drawView(SDL_GPURenderPass *pass, const MeshView &mesh, Uint32 numInstances=1)
Draw a MeshView.
void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view)
Bind a MeshView object.
void draw(SDL_GPURenderPass *pass, const Mesh &mesh, Uint32 numInstances=1)
Definition RenderContext.h:172
void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh)
Bind a Mesh object.
void drawViews(SDL_GPURenderPass *pass, std::span< const MeshView > meshViews, Uint32 numInstances=1)
Render a collection of MeshView. This collection must satisfy the invariant that they all have the sa...
void bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:203
void bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:187
Definition Camera.h:8
constexpr int sdlSampleToValue(SDL_GPUSampleCount samples)
Definition RenderContext.h:15
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
constexpr NoInitT NoInit
Definition Tags.h:9
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6
Device device
Definition RenderContext.h:48
bool msaaEnabled() const
Definition RenderContext.h:70
CommandBuffer acquireCommandBuffer() const
Acquire the command buffer, starting a frame.
Definition RenderContext.h:102
void destroy() noexcept
const Texture & colorTarget() const
Definition RenderContext.h:62
RenderContext(RenderContext &&) noexcept=default
RenderContext(NoInitT)
Definition RenderContext.h:51
void enableMSAA(SDL_GPUSampleCount samples)
Definition RenderContext.h:79
bool waitAndAcquireSwapchain(CommandBuffer &command_buffer)
Wait until swapchain is available, then acquire it.
void presentToSwapchain(CommandBuffer &command_buffer)
Present the resolved texture to the swapchain. You must acquire the swapchain beforehand.
Window window
Definition RenderContext.h:49
bool initialized() const
Definition RenderContext.h:99
void disableMSAA()
Definition RenderContext.h:93
const Texture & resolvedColorTarget() const
Definition RenderContext.h:68
void setSwapchainParameters(SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode present_mode)
Definition RenderContext.h:127
SDL_GPUTextureFormat depthFormat() const
Definition RenderContext.h:133
SDL_GPUTextureFormat getSwapchainTextureFormat() const
Definition RenderContext.h:120
bool waitForSwapchain()
Wait for the swapchain to be available.
Definition RenderContext.h:114
SDL_GPUTextureFormat colorFormat() const
Definition RenderContext.h:132
RenderContext(Device &&device, Window &&window, SDL_GPUTextureFormat suggested_depth_format=SDL_GPU_TEXTUREFORMAT_INVALID)
Constructor. The last argument (the depth texture format) is optional. By default,...
bool acquireSwapchain(CommandBuffer &command_buffer)
Acquire GPU swapchain.
SDL_GPUSampleCount getMsaaSampleCount() const
Definition RenderContext.h:72
const Texture & depthTarget() const
Definition RenderContext.h:66
bool hasDepthTexture() const
Check if a depth texture was created.
Definition RenderContext.h:125
RAII wrapper for the SDL_Window opaque type.
Definition Window.h:11