candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
CommandBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include "Core.h"
4#include <SDL3/SDL_gpu.h>
5#include <span>
6#include <spdlog/spdlog.h>
7
8namespace candlewick {
9
10template <typename T>
12 std::is_standard_layout_v<T> && !std::is_array_v<T> &&
13 !std::is_pointer_v<T> &&
14 (alignof(T) == 4 || alignof(T) == 8 || alignof(T) == 16);
15
17 SDL_GPUCommandBuffer *m_handle;
18
19public:
20 CommandBuffer(const Device &device);
21
23 operator SDL_GPUCommandBuffer *() const { return m_handle; }
24
26 CommandBuffer(const CommandBuffer &) = delete;
27
29 CommandBuffer(CommandBuffer &&other) noexcept : m_handle(other.m_handle) {
30 other.m_handle = nullptr;
31 }
32
35
38 if (this != &other) {
39 if (active())
40 this->cancel();
41 m_handle = other.m_handle;
42 other.m_handle = nullptr;
43 }
44 return *this;
45 }
46
47 bool submit() noexcept {
48 if (!(active() && SDL_SubmitGPUCommandBuffer(m_handle)))
49 return false;
50 m_handle = nullptr;
51 return true;
52 }
53
54 SDL_GPUFence *submitAndAcquireFence() noexcept {
55 SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(m_handle);
56 m_handle = nullptr;
57 return fence;
58 }
59
62 bool cancel() noexcept;
63
67 bool active() const noexcept { return m_handle; }
68
69 ~CommandBuffer() noexcept {
70 if (active()) {
71 spdlog::warn(
72 "CommandBuffer object is being destroyed while still active. "
73 "It will be cancelled.");
74 [[maybe_unused]] bool ret = cancel();
75 CANDLEWICK_ASSERT(ret, "Failed to cancel command buffer on cleanup.");
76 }
77 }
78
79 template <GpuCompatibleData T>
80 CommandBuffer &pushVertexUniform(Uint32 slot_index, const T &data) {
81 return pushVertexUniformRaw(slot_index, &data, sizeof(T));
82 }
83
84 template <GpuCompatibleData T>
85 CommandBuffer &pushFragmentUniform(Uint32 slot_index, const T &data) {
86 return pushFragmentUniformRaw(slot_index, &data, sizeof(T));
87 }
88
89 template <GpuCompatibleData T>
90 CommandBuffer &pushVertexUniform(Uint32 slot_index, std::span<const T> data) {
91 return pushVertexUniformRaw(slot_index, data.data(), data.size_bytes());
92 }
93
94 template <GpuCompatibleData T>
96 std::span<const T> data) {
97 return pushFragmentUniformRaw(slot_index, data.data(), data.size_bytes());
98 }
99
100 template <GpuCompatibleData T, size_t N>
101 CommandBuffer &pushVertexUniform(Uint32 slot_index, const T (&data)[N]) {
102 return pushVertexUniform(slot_index, std::span<const T, N>(data));
103 }
104
105 template <GpuCompatibleData T, size_t N>
106 CommandBuffer &pushFragmentUniform(Uint32 slot_index, const T (&data)[N]) {
107 return pushFragmentUniform(slot_index, std::span<const T, N>(data));
108 }
109
111 CommandBuffer &pushVertexUniformRaw(Uint32 slot_index, const void *data,
112 Uint32 length) {
113 SDL_PushGPUVertexUniformData(m_handle, slot_index, data, length);
114 return *this;
115 }
116
117 CommandBuffer &pushFragmentUniformRaw(Uint32 slot_index, const void *data,
118 Uint32 length) {
119 SDL_PushGPUFragmentUniformData(m_handle, slot_index, data, length);
120 return *this;
121 }
122};
123
124} // namespace candlewick
#define CANDLEWICK_ASSERT(condition, msg)
Definition Core.h:7
CommandBuffer & pushVertexUniform(Uint32 slot_index, const T(&data)[N])
Definition CommandBuffer.h:101
CommandBuffer & pushFragmentUniform(Uint32 slot_index, const T &data)
Definition CommandBuffer.h:85
CommandBuffer & pushFragmentUniform(Uint32 slot_index, std::span< const T > data)
Definition CommandBuffer.h:95
CommandBuffer(const CommandBuffer &)=delete
Deleted copy constructor.
SDL_GPUFence * submitAndAcquireFence() noexcept
Definition CommandBuffer.h:54
bool active() const noexcept
Check if the command buffer is still active.
Definition CommandBuffer.h:67
~CommandBuffer() noexcept
Definition CommandBuffer.h:69
CommandBuffer & pushVertexUniform(Uint32 slot_index, const T &data)
Definition CommandBuffer.h:80
CommandBuffer & operator=(CommandBuffer &&other) noexcept
Move assignment operator.
Definition CommandBuffer.h:37
CommandBuffer & pushVertexUniform(Uint32 slot_index, std::span< const T > data)
Definition CommandBuffer.h:90
CommandBuffer & pushVertexUniformRaw(Uint32 slot_index, const void *data, Uint32 length)
Push uniform data to the vertex shader.
Definition CommandBuffer.h:111
CommandBuffer & pushFragmentUniformRaw(Uint32 slot_index, const void *data, Uint32 length)
Push uniform data to the fragment shader.
Definition CommandBuffer.h:117
CommandBuffer & pushFragmentUniform(Uint32 slot_index, const T(&data)[N])
Definition CommandBuffer.h:106
CommandBuffer(CommandBuffer &&other) noexcept
Move constructor.
Definition CommandBuffer.h:29
bool submit() noexcept
Definition CommandBuffer.h:47
CommandBuffer & operator=(const CommandBuffer &)=delete
Deleted copy assignment operator.
CommandBuffer(const Device &device)
bool cancel() noexcept
Cancel the command buffer, returning the bool value from the wrapped SDL API.
Definition CommandBuffer.h:11
Definition Camera.h:8
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17