92 lines
2.6 KiB
C++
92 lines
2.6 KiB
C++
#include <destrum/Graphics/Managers/FrameManager.h>
|
|
|
|
#include <volk.h>
|
|
|
|
#include <tracy/Tracy.hpp>
|
|
|
|
#include <destrum/Graphics/Init.h>
|
|
|
|
#include "destrum/Graphics/Util.h"
|
|
|
|
FrameManager::~FrameManager() {
|
|
}
|
|
|
|
void FrameManager::init(VkDevice dev, std::uint32_t queueFamily) {
|
|
device = dev;
|
|
|
|
try {
|
|
const auto poolCreateInfo = vkinit::commandPoolCreateInfo(
|
|
VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
|
|
queueFamily);
|
|
|
|
for (std::uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
|
|
auto& commandPool = frames[i].commandPool;
|
|
VK_CHECK(vkCreateCommandPool(device, &poolCreateInfo, nullptr, &commandPool));
|
|
|
|
const auto cmdAllocInfo = vkinit::commandBufferAllocateInfo(commandPool, 1);
|
|
auto& mainCommandBuffer = frames[i].commandBuffer;
|
|
VK_CHECK(vkAllocateCommandBuffers(device, &cmdAllocInfo, &mainCommandBuffer));
|
|
|
|
const std::string poolName = "frame " + std::to_string(i) + " command pool";
|
|
const std::string cmdName = "frame " + std::to_string(i) + " command buffer";
|
|
vkutil::addDebugLabel(device, commandPool, poolName.c_str());
|
|
vkutil::addDebugLabel(device, mainCommandBuffer, cmdName.c_str());
|
|
}
|
|
} catch (...) {
|
|
cleanup(dev);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
void FrameManager::cleanup(VkDevice dev) {
|
|
for (std::uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
|
|
if (frames[i].commandPool != VK_NULL_HANDLE && dev != VK_NULL_HANDLE) {
|
|
vkDestroyCommandPool(dev, frames[i].commandPool, nullptr);
|
|
}
|
|
frames[i].commandPool = VK_NULL_HANDLE;
|
|
frames[i].commandBuffer = VK_NULL_HANDLE;
|
|
}
|
|
frameNumber = 0;
|
|
device = VK_NULL_HANDLE;
|
|
}
|
|
|
|
VkCommandBuffer FrameManager::beginFrame() {
|
|
ZoneScopedN("FrameManager::beginFrame");
|
|
|
|
const auto& frame = getCurrentFrame();
|
|
const auto& cmd = frame.commandBuffer;
|
|
|
|
const auto cmdBeginInfo = VkCommandBufferBeginInfo{
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
|
};
|
|
|
|
{
|
|
ZoneScopedN("vkBeginCommandBuffer");
|
|
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
|
|
}
|
|
|
|
return cmd;
|
|
}
|
|
|
|
void FrameManager::endFrame(VkCommandBuffer cmd) {
|
|
ZoneScopedN("FrameManager::endFrame");
|
|
|
|
{
|
|
ZoneScopedN("vkEndCommandBuffer");
|
|
VK_CHECK(vkEndCommandBuffer(cmd));
|
|
}
|
|
|
|
}
|
|
|
|
void FrameManager::nextFrame() {
|
|
frameNumber++;
|
|
FrameMark;
|
|
}
|
|
|
|
void FrameManager::waitIdle() const {
|
|
if (device != VK_NULL_HANDLE) {
|
|
VK_CHECK(vkDeviceWaitIdle(device));
|
|
}
|
|
}
|