candlewick 0.1.0
A renderer
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 <SDL3/SDL_log.h>
6#include <utility>
7#include <span>
8
9namespace candlewick {
10
11template <typename T>
13 std::is_standard_layout_v<T> && !std::is_array_v<T> &&
14 !std::is_pointer_v<T> &&
15 (alignof(T) == 4 || alignof(T) == 8 || alignof(T) == 16);
16
18 SDL_GPUCommandBuffer *_cmdBuf;
19
20public:
21 CommandBuffer(const Device &device);
22
24 operator SDL_GPUCommandBuffer *() const { return _cmdBuf; }
25
27 CommandBuffer(const CommandBuffer &) = delete;
28
30 CommandBuffer(CommandBuffer &&other) noexcept;
31
34
37
38 friend void swap(CommandBuffer &lhs, CommandBuffer &rhs) noexcept {
39 std::swap(lhs._cmdBuf, rhs._cmdBuf);
40 }
41
42 bool submit() noexcept {
43 if (!(active() && SDL_SubmitGPUCommandBuffer(_cmdBuf)))
44 return false;
45 _cmdBuf = nullptr;
46 return true;
47 }
48
49 SDL_GPUFence *submitAndAcquireFence() noexcept {
50 SDL_GPUFence *fence = SDL_SubmitGPUCommandBufferAndAcquireFence(_cmdBuf);
51 _cmdBuf = nullptr;
52 return fence;
53 }
54
57 bool cancel() noexcept;
58
62 bool active() const noexcept { return _cmdBuf; }
63
64 ~CommandBuffer() noexcept {
65 if (active()) {
66 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
67 "CommandBuffer object is being destroyed while still active! "
68 "It will be cancelled.");
69 [[maybe_unused]] bool ret = cancel();
70 assert(ret);
71 }
72 }
73
74 template <GpuCompatibleData T>
75 CommandBuffer &pushVertexUniform(Uint32 slot_index, const T &data) {
76 return pushVertexUniformRaw(slot_index, &data, sizeof(T));
77 }
78
79 template <GpuCompatibleData T>
80 CommandBuffer &pushFragmentUniform(Uint32 slot_index, const T &data) {
81 return pushFragmentUniformRaw(slot_index, &data, sizeof(T));
82 }
83
84 template <GpuCompatibleData T>
85 CommandBuffer &pushVertexUniform(Uint32 slot_index, std::span<const T> data) {
86 return pushVertexUniformRaw(slot_index, data.data(), data.size_bytes());
87 }
88
89 template <GpuCompatibleData T>
91 std::span<const T> data) {
92 return pushFragmentUniformRaw(slot_index, data.data(), data.size_bytes());
93 }
94
96 CommandBuffer &pushVertexUniformRaw(Uint32 slot_index, const void *data,
97 Uint32 length) {
98 SDL_PushGPUVertexUniformData(_cmdBuf, slot_index, data, length);
99 return *this;
100 }
101
102 CommandBuffer &pushFragmentUniformRaw(Uint32 slot_index, const void *data,
103 Uint32 length) {
104 SDL_PushGPUFragmentUniformData(_cmdBuf, slot_index, data, length);
105 return *this;
106 }
107};
108
109} // namespace candlewick
CommandBuffer & pushFragmentUniform(Uint32 slot_index, const T &data)
Definition CommandBuffer.h:80
CommandBuffer & pushFragmentUniform(Uint32 slot_index, std::span< const T > data)
Definition CommandBuffer.h:90
CommandBuffer(const CommandBuffer &)=delete
Deleted copy constructor.
SDL_GPUFence * submitAndAcquireFence() noexcept
Definition CommandBuffer.h:49
bool active() const noexcept
Check if the command buffer is still active.
Definition CommandBuffer.h:62
~CommandBuffer() noexcept
Definition CommandBuffer.h:64
CommandBuffer & pushVertexUniform(Uint32 slot_index, const T &data)
Definition CommandBuffer.h:75
CommandBuffer & operator=(CommandBuffer &&other) noexcept
Move assignment operator.
CommandBuffer & pushVertexUniform(Uint32 slot_index, std::span< const T > data)
Definition CommandBuffer.h:85
CommandBuffer & pushVertexUniformRaw(Uint32 slot_index, const void *data, Uint32 length)
Push uniform data to the vertex shader.
Definition CommandBuffer.h:96
friend void swap(CommandBuffer &lhs, CommandBuffer &rhs) noexcept
Definition CommandBuffer.h:38
CommandBuffer & pushFragmentUniformRaw(Uint32 slot_index, const void *data, Uint32 length)
Push uniform data to the fragment shader.
Definition CommandBuffer.h:102
CommandBuffer(CommandBuffer &&other) noexcept
Move constructor.
bool submit() noexcept
Definition CommandBuffer.h:42
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:12
Definition Camera.h:8
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17