candlewick 0.6.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 bool hasDepthTexture() const { return depth_texture.hasValue(); }
60
61 SDL_GPUTextureFormat depthFormat() const { return depth_texture.format(); }
62
63 ~RenderContext() noexcept;
64
65 void destroy() noexcept { this->~RenderContext(); }
66};
67
68namespace rend {
69
72 void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh);
73
81 void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view);
82
90 void drawViews(SDL_GPURenderPass *pass, std::span<const MeshView> meshViews,
91 Uint32 numInstances = 1);
92
101 inline void draw(SDL_GPURenderPass *pass, const Mesh &mesh,
102 Uint32 numInstances = 1) {
103 drawViews(pass, mesh.views(), numInstances);
104 }
105
111 void drawView(SDL_GPURenderPass *pass, const MeshView &mesh,
112 Uint32 numInstances = 1);
113
115 inline void
116 bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
117 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
118 SDL_BindGPUVertexSamplers(pass, first_slot, bindings.data(),
119 Uint32(bindings.size()));
120 }
121
125 SDL_GPURenderPass *pass, Uint32 first_slot,
126 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
127 bindVertexSamplers(pass, first_slot, std::span(sampler_bindings));
128 }
129
131 inline void
132 bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
133 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
134 SDL_BindGPUFragmentSamplers(pass, first_slot, bindings.data(),
135 Uint32(bindings.size()));
136 }
137
141 SDL_GPURenderPass *pass, Uint32 first_slot,
142 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
143 bindFragmentSamplers(pass, first_slot, std::span(sampler_bindings));
144 }
145
146} // namespace rend
147} // 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:68
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:101
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:132
void bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:116
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:65
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
SDL_GPUTextureFormat depthFormat() const
Definition RenderContext.h:61
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