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