Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
RingCommandContext.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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 <glad/vulkan.h>
24#include <memory>
25#include <vector>
26
27namespace Renderer
28{
29
30namespace Backend
31{
32
33namespace Vulkan
34{
35
36class CBuffer;
37class CDevice;
38
39/**
40 * A simple helper class to decouple command buffers rotation from frames
41 * presenting. It might be useful when sometimes we need to submit more work
42 * than we can usually have during a frame. For example if we need to upload
43 * something, an upload buffer is full and we can't extend it at the moment.
44 * Then the only way is to wait until uploading is done and submit more work.
45 * @note not thread-safe, should be created and used from the same thread.
46 */
48{
49public:
51 CDevice* device, const size_t size, const uint32_t queueFamilyIndex,
52 CSubmitScheduler& submitScheduler);
54
55 /**
56 * @return the current available command buffer. If there is none waits until
57 * it appeared.
58 */
59 VkCommandBuffer GetCommandBuffer();
60
61 /**
62 * Submits the current command buffer to the SubmitScheduler.
63 */
64 void Flush();
65
66 /**
67 * The same as Flush but also waits until it's completed. It means it
68 * forces SubmitScheduler to submit all previously queued work to GPU.
69 */
70 void FlushAndWait();
71
72 /**
73 * Schedules uploads until next render pass or flush.
74 * @note doesn't save a command buffer returned by GetCommandBuffer during
75 * scheduling uploads, because it might be changed.
76 */
77 void ScheduleUpload(
78 CTexture* texture, const Format dataFormat,
79 const void* data, const size_t dataSize,
80 const uint32_t level, const uint32_t layer);
81 void ScheduleUpload(
82 CTexture* texture, const Format dataFormat,
83 const void* data, const size_t dataSize,
84 const uint32_t xOffset, const uint32_t yOffset,
85 const uint32_t width, const uint32_t height,
86 const uint32_t level, const uint32_t layer);
87
88 void ScheduleUpload(
89 CBuffer* buffer, const void* data, const uint32_t dataOffset,
90 const uint32_t dataSize);
91 using UploadBufferFunction = std::function<void(u8*)>;
92 void ScheduleUpload(
93 CBuffer* buffer,
94 const uint32_t dataOffset, const uint32_t dataSize,
95 const UploadBufferFunction& uploadFunction);
96
97private:
98 void Begin();
99 void End();
100
101 void ScheduleUpload(
102 CBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
103 const uint32_t acquiredOffset);
104
106 const uint32_t requiredSize, const uint32_t requiredAlignment);
108 const uint32_t requiredSize, const uint32_t requiredAlignment) const;
109
110 CDevice* m_Device = nullptr;
112
113 std::unique_ptr<CBuffer> m_StagingBuffer;
117
118 struct RingItem
119 {
120 VkCommandPool commandPool = VK_NULL_HANDLE;
121 VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
123 bool isBegan = false;
125 };
126 std::vector<RingItem> m_Ring;
127 size_t m_RingIndex = 0;
128
129 void WaitUntilFree(RingItem& item);
130};
131
132} // namespace Vulkan
133
134} // namespace Backend
135
136} // namespace Renderer
137
138#endif // INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
Definition: Buffer.h:39
Definition: Device.h:60
A simple helper class to decouple command buffers rotation from frames presenting.
Definition: RingCommandContext.h:48
uint32_t m_MaxStagingBufferCapacity
Definition: RingCommandContext.h:116
std::vector< RingItem > m_Ring
Definition: RingCommandContext.h:126
size_t m_RingIndex
Definition: RingCommandContext.h:127
std::unique_ptr< CBuffer > m_StagingBuffer
Definition: RingCommandContext.h:113
CRingCommandContext(CDevice *device, const size_t size, const uint32_t queueFamilyIndex, CSubmitScheduler &submitScheduler)
Definition: RingCommandContext.cpp:52
~CRingCommandContext()
Definition: RingCommandContext.cpp:91
uint32_t GetFreeSpaceOffset(const uint32_t requiredSize, const uint32_t requiredAlignment) const
Definition: RingCommandContext.cpp:410
CSubmitScheduler & m_SubmitScheduler
Definition: RingCommandContext.h:111
void FlushAndWait()
The same as Flush but also waits until it's completed.
Definition: RingCommandContext.cpp:125
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:136
uint32_t m_OptimalBufferCopyOffsetAlignment
Definition: RingCommandContext.h:115
VkCommandBuffer GetCommandBuffer()
Definition: RingCommandContext.cpp:104
CDevice * m_Device
Definition: RingCommandContext.h:110
void Flush()
Submits the current command buffer to the SubmitScheduler.
Definition: RingCommandContext.cpp:112
uint32_t m_StagingBufferCurrentFirst
Definition: RingCommandContext.h:114
void End()
Definition: RingCommandContext.cpp:321
uint32_t m_StagingBufferLast
Definition: RingCommandContext.h:114
void WaitUntilFree(RingItem &item)
Definition: RingCommandContext.cpp:331
std::function< void(u8 *)> UploadBufferFunction
Definition: RingCommandContext.h:91
void Begin()
Definition: RingCommandContext.cpp:303
uint32_t AcquireFreeSpace(const uint32_t requiredSize, const uint32_t requiredAlignment)
Definition: RingCommandContext.cpp:342
uint32_t m_StagingBufferFirst
Definition: RingCommandContext.h:114
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:47
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:50
uint32_t SubmitHandle
Definition: SubmitScheduler.h:49
Definition: Texture.h:41
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
Definition: VideoMode.h:29
VkCommandPool commandPool
Definition: RingCommandContext.h:120
VkCommandBuffer commandBuffer
Definition: RingCommandContext.h:121
uint32_t stagingBufferLast
Definition: RingCommandContext.h:124
bool isBegan
Definition: RingCommandContext.h:123
CSubmitScheduler::SubmitHandle handle
Definition: RingCommandContext.h:122
uint32_t stagingBufferFirst
Definition: RingCommandContext.h:124
uint8_t u8
Definition: types.h:37
unsigned int uint32_t
Definition: wposix_types.h:53