Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
RingCommandContext.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
19#define INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
20
22
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <glad/vulkan.h>
27#include <memory>
28#include <vector>
29
30namespace Renderer::Backend { enum class Format; }
31namespace Renderer::Backend::Vulkan { class CBuffer; }
32namespace Renderer::Backend::Vulkan { class CDevice; }
33namespace Renderer::Backend::Vulkan { class CTexture; }
34
35namespace Renderer
36{
37
38namespace Backend
39{
40
41namespace Vulkan
42{
43
44/**
45 * A simple helper class to decouple command buffers rotation from frames
46 * presenting. It might be useful when sometimes we need to submit more work
47 * than we can usually have during a frame. For example if we need to upload
48 * something, an upload buffer is full and we can't extend it at the moment.
49 * Then the only way is to wait until uploading is done and submit more work.
50 * @note not thread-safe, should be created and used from the same thread.
51 */
53{
54public:
55 static std::unique_ptr<CRingCommandContext> Create(
56 CDevice* device, const size_t size, const uint32_t queueFamilyIndex,
57 CSubmitScheduler& submitScheduler);
58
60
61 /**
62 * @return the current available command buffer. If there is none waits until
63 * it appeared.
64 */
65 VkCommandBuffer GetCommandBuffer();
66
67 /**
68 * Submits the current command buffer to the SubmitScheduler.
69 */
70 void Flush();
71
72 /**
73 * The same as Flush but also waits until it's completed. It means it
74 * forces SubmitScheduler to submit all previously queued work to GPU.
75 */
76 void FlushAndWait();
77
78 /**
79 * Schedules uploads until next render pass or flush.
80 * @note doesn't save a command buffer returned by GetCommandBuffer during
81 * scheduling uploads, because it might be changed.
82 */
83 void ScheduleUpload(
84 CTexture* texture, const Format dataFormat,
85 const void* data, const size_t dataSize,
86 const uint32_t level, const uint32_t layer);
87 void ScheduleUpload(
88 CTexture* texture, const Format dataFormat,
89 const void* data, const size_t dataSize,
90 const uint32_t xOffset, const uint32_t yOffset,
91 const uint32_t width, const uint32_t height,
92 const uint32_t level, const uint32_t layer);
93
94 void ScheduleUpload(
95 CBuffer* buffer, const void* data, const uint32_t dataOffset,
96 const uint32_t dataSize);
97 using UploadBufferFunction = std::function<void(std::uint8_t*)>;
98 void ScheduleUpload(
99 CBuffer* buffer,
100 const uint32_t dataOffset, const uint32_t dataSize,
101 const UploadBufferFunction& uploadFunction);
102
103private:
105 CDevice* device, CSubmitScheduler& submitScheduler);
106
107 void Begin();
108 void End();
109
110 void ScheduleUpload(
111 CBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
112 const uint32_t acquiredOffset);
113
114 uint32_t AcquireFreeSpace(
115 const uint32_t requiredSize, const uint32_t requiredAlignment);
116 uint32_t GetFreeSpaceOffset(
117 const uint32_t requiredSize, const uint32_t requiredAlignment) const;
118
119 CDevice* m_Device = nullptr;
121
122 std::unique_ptr<CBuffer> m_StagingBuffer;
126
127 struct RingItem
128 {
129 VkCommandPool commandPool = VK_NULL_HANDLE;
130 VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
132 bool isBegan = false;
134 };
135 std::vector<RingItem> m_Ring;
136 size_t m_RingIndex = 0;
137
138 void WaitUntilFree(RingItem& item);
139};
140
141} // namespace Vulkan
142
143} // namespace Backend
144
145} // namespace Renderer
146
147#endif // INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
Represents a texture object.
Definition: TextureManager.h:271
Definition: Buffer.h:41
Definition: Device.h:67
A simple helper class to decouple command buffers rotation from frames presenting.
Definition: RingCommandContext.h:53
uint32_t m_MaxStagingBufferCapacity
Definition: RingCommandContext.h:125
std::vector< RingItem > m_Ring
Definition: RingCommandContext.h:135
size_t m_RingIndex
Definition: RingCommandContext.h:136
std::unique_ptr< CBuffer > m_StagingBuffer
Definition: RingCommandContext.h:122
static std::unique_ptr< CRingCommandContext > Create(CDevice *device, const size_t size, const uint32_t queueFamilyIndex, CSubmitScheduler &submitScheduler)
Definition: RingCommandContext.cpp:61
std::function< void(std::uint8_t *)> UploadBufferFunction
Definition: RingCommandContext.h:97
CRingCommandContext(CDevice *device, CSubmitScheduler &submitScheduler)
Definition: RingCommandContext.cpp:104
~CRingCommandContext()
Definition: RingCommandContext.cpp:110
uint32_t GetFreeSpaceOffset(const uint32_t requiredSize, const uint32_t requiredAlignment) const
Definition: RingCommandContext.cpp:434
CSubmitScheduler & m_SubmitScheduler
Definition: RingCommandContext.h:120
void FlushAndWait()
The same as Flush but also waits until it's completed.
Definition: RingCommandContext.cpp:144
void ScheduleUpload(CTexture *texture, const Format dataFormat, const void *data, const size_t dataSize, const uint32_t level, const uint32_t layer)
Schedules uploads until next render pass or flush.
Definition: RingCommandContext.cpp:155
uint32_t m_OptimalBufferCopyOffsetAlignment
Definition: RingCommandContext.h:124
VkCommandBuffer GetCommandBuffer()
Definition: RingCommandContext.cpp:123
CDevice * m_Device
Definition: RingCommandContext.h:119
void Flush()
Submits the current command buffer to the SubmitScheduler.
Definition: RingCommandContext.cpp:131
uint32_t m_StagingBufferCurrentFirst
Definition: RingCommandContext.h:123
void End()
Definition: RingCommandContext.cpp:345
uint32_t m_StagingBufferLast
Definition: RingCommandContext.h:123
void WaitUntilFree(RingItem &item)
Definition: RingCommandContext.cpp:355
void Begin()
Definition: RingCommandContext.cpp:327
uint32_t AcquireFreeSpace(const uint32_t requiredSize, const uint32_t requiredAlignment)
Definition: RingCommandContext.cpp:366
uint32_t m_StagingBufferFirst
Definition: RingCommandContext.h:123
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:46
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:49
uint32_t SubmitHandle
Definition: SubmitScheduler.h:48
Definition: Texture.h:43
Definition: Buffer.cpp:37
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
Definition: Canvas2D.h:30
VkCommandPool commandPool
Definition: RingCommandContext.h:129
VkCommandBuffer commandBuffer
Definition: RingCommandContext.h:130
uint32_t stagingBufferLast
Definition: RingCommandContext.h:133
bool isBegan
Definition: RingCommandContext.h:132
CSubmitScheduler::SubmitHandle handle
Definition: RingCommandContext.h:131
uint32_t stagingBufferFirst
Definition: RingCommandContext.h:133