candlewick 0.1.0
A renderer
Loading...
Searching...
No Matches
Renderer.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
20struct Renderer {
23 SDL_GPUTexture *swapchain;
25
26 Renderer(NoInitT) : device(NoInit), window(nullptr), swapchain(nullptr) {}
31 SDL_GPUTextureFormat suggested_depth_format);
32
35 void createDepthTexture(SDL_GPUTextureFormat suggested_depth_format);
36
37 bool initialized() const { return bool(device); }
38
41
45
49 bool acquireSwapchain(CommandBuffer &command_buffer);
50
51 bool waitForSwapchain() { return SDL_WaitForGPUSwapchain(device, window); }
52
53 SDL_GPUTextureFormat getSwapchainTextureFormat() const {
54 return SDL_GetGPUSwapchainTextureFormat(device, window);
55 }
56
58 bool hasDepthTexture() const { return depth_texture.hasValue(); }
59
60 SDL_GPUTextureFormat depthFormat() const { return depth_texture.format(); }
61
62 void destroy() noexcept;
63
64 ~Renderer() noexcept { this->destroy(); }
65};
66
67namespace rend {
68
71 void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh);
72
80 void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view);
81
89 void drawViews(SDL_GPURenderPass *pass, std::span<const MeshView> meshViews,
90 Uint32 numInstances = 1);
91
100 inline void draw(SDL_GPURenderPass *pass, const Mesh &mesh,
101 Uint32 numInstances = 1) {
102 drawViews(pass, mesh.views(), numInstances);
103 }
104
110 void drawView(SDL_GPURenderPass *pass, const MeshView &mesh,
111 Uint32 numInstances = 1);
112
114 inline void
115 bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
116 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
117 SDL_BindGPUVertexSamplers(pass, first_slot, bindings.data(),
118 Uint32(bindings.size()));
119 }
120
124 SDL_GPURenderPass *pass, Uint32 first_slot,
125 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
126 bindVertexSamplers(pass, first_slot, std::span(sampler_bindings));
127 }
128
130 inline void
131 bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot,
132 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
133 SDL_BindGPUFragmentSamplers(pass, first_slot, bindings.data(),
134 Uint32(bindings.size()));
135 }
136
140 SDL_GPURenderPass *pass, Uint32 first_slot,
141 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
142 bindFragmentSamplers(pass, first_slot, std::span(sampler_bindings));
143 }
144
145} // namespace rend
146} // 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 Renderer.h:67
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 Renderer.h:100
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 Renderer.h:131
void bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition Renderer.h:115
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
bool initialized() const
Definition Renderer.h:37
Renderer(Device &&device, Window &&window, SDL_GPUTextureFormat suggested_depth_format)
Constructor with a depth format. This will create a depth texture.
Device device
Definition Renderer.h:21
SDL_GPUTextureFormat getSwapchainTextureFormat() const
Definition Renderer.h:53
SDL_GPUTextureFormat depthFormat() const
Definition Renderer.h:60
CommandBuffer acquireCommandBuffer() const
Acquire the command buffer, starting a frame.
Definition Renderer.h:40
bool acquireSwapchain(CommandBuffer &command_buffer)
Acquire GPU swapchain.
bool waitForSwapchain()
Definition Renderer.h:51
bool hasDepthTexture() const
Check if a depth texture was created.
Definition Renderer.h:58
void destroy() noexcept
SDL_GPUTexture * swapchain
Definition Renderer.h:23
Renderer(Device &&device, Window &&window)
Constructor without a depth format.
Window window
Definition Renderer.h:22
Texture depth_texture
Definition Renderer.h:24
Renderer(NoInitT)
Definition Renderer.h:26
void createDepthTexture(SDL_GPUTextureFormat suggested_depth_format)
Add a depth texture to the rendering context.
bool waitAndAcquireSwapchain(CommandBuffer &command_buffer)
Wait until swapchain is available, then acquire it.
RAII wrapper for the SDL_Window opaque type.
Definition Window.h:11