candlewick 0.9.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
RenderContext.h
Go to the documentation of this file.
1#pragma once
2
3#include "Device.h"
4#include "CommandBuffer.h"
5#include "Texture.h"
6#include "Mesh.h"
7#include "Window.h"
8
9#include <span>
10#include <SDL3/SDL_gpu.h>
11
12namespace candlewick {
13
23 SDL_GPUTexture *swapchain;
25
27 : device(NoInit), window(nullptr), swapchain(nullptr) {}
28
32 SDL_GPUTextureFormat suggested_depth_format);
33
36 void createDepthTexture(SDL_GPUTextureFormat suggested_depth_format);
37
38 bool initialized() const { return bool(device); }
39
42
46
50 bool acquireSwapchain(CommandBuffer &command_buffer);
51
52 bool waitForSwapchain() { return SDL_WaitForGPUSwapchain(device, window); }
53
54 SDL_GPUTextureFormat getSwapchainTextureFormat() const {
55 return SDL_GetGPUSwapchainTextureFormat(device, window);
56 }
57
59 inline bool hasDepthTexture() const { return depth_texture.hasValue(); }
60
61 inline void setSwapchainParameters(SDL_GPUSwapchainComposition composition,
62 SDL_GPUPresentMode present_mode) {
63 SDL_SetGPUSwapchainParameters(device, window, composition, present_mode);
64 }
65
66 SDL_GPUTextureFormat depthFormat() const { return depth_texture.format(); }
67
68 ~RenderContext() noexcept;
69
70 void destroy() noexcept { this->~RenderContext(); }
71};
72
73namespace rend {
74
77 void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh);
78
86 void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view);
87
95 void drawViews(SDL_GPURenderPass *pass, std::span<const MeshView> meshViews,
96 Uint32 numInstances = 1);
97
106 inline void draw(SDL_GPURenderPass *pass, const Mesh &mesh,
107 Uint32 numInstances = 1) {
108 drawViews(pass, mesh.views(), numInstances);
109 }
110
116 void drawView(SDL_GPURenderPass *pass, const MeshView &mesh,
117 Uint32 numInstances = 1);
118
120 inline void
121 bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
122 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
123 SDL_BindGPUVertexSamplers(pass, first_slot, bindings.data(),
124 Uint32(bindings.size()));
125 }
126
130 SDL_GPURenderPass *pass, Uint32 first_slot,
131 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
132 bindVertexSamplers(pass, first_slot, std::span(sampler_bindings));
133 }
134
136 inline void
137 bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
138 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
139 SDL_BindGPUFragmentSamplers(pass, first_slot, bindings.data(),
140 Uint32(bindings.size()));
141 }
142
146 SDL_GPURenderPass *pass, Uint32 first_slot,
147 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
148 bindFragmentSamplers(pass, first_slot, std::span(sampler_bindings));
149 }
150
151} // namespace rend
152} // namespace candlewick
Definition CommandBuffer.h:17
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:73
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:106
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:137
void bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:121
Definition Camera.h:8
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:21
CommandBuffer acquireCommandBuffer() const
Acquire the command buffer, starting a frame.
Definition RenderContext.h:41
void destroy() noexcept
Definition RenderContext.h:70
Texture depth_texture
Definition RenderContext.h:24
RenderContext(NoInitT)
Definition RenderContext.h:26
bool waitAndAcquireSwapchain(CommandBuffer &command_buffer)
Wait until swapchain is available, then acquire it.
Window window
Definition RenderContext.h:22
bool initialized() const
Definition RenderContext.h:38
void createDepthTexture(SDL_GPUTextureFormat suggested_depth_format)
Add a depth texture to the rendering context.
RenderContext(Device &&device, Window &&window, SDL_GPUTextureFormat suggested_depth_format)
Constructor with a depth format. This will create a depth texture.
SDL_GPUTexture * swapchain
Definition RenderContext.h:23
void setSwapchainParameters(SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode present_mode)
Definition RenderContext.h:61
SDL_GPUTextureFormat depthFormat() const
Definition RenderContext.h:66
SDL_GPUTextureFormat getSwapchainTextureFormat() const
Definition RenderContext.h:54
bool waitForSwapchain()
Definition RenderContext.h:52
bool acquireSwapchain(CommandBuffer &command_buffer)
Acquire GPU swapchain.
bool hasDepthTexture() const
Check if a depth texture was created.
Definition RenderContext.h:59
RenderContext(Device &&device, Window &&window)
Constructor without a depth format.
RAII wrapper for the SDL_Window opaque type.
Definition Window.h:11