Add namespace to all classes
This commit is contained in:
+84
-81
@@ -4,96 +4,99 @@
|
||||
#include "Instance.h"
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
enum class CommandBufferType
|
||||
namespace Copium
|
||||
{
|
||||
SingleUse, Dynamic
|
||||
};
|
||||
|
||||
class CommandBuffer
|
||||
{
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
|
||||
private:
|
||||
Instance& instance;
|
||||
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
const CommandBufferType type;
|
||||
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
|
||||
|
||||
public:
|
||||
CommandBuffer(Instance& instance, CommandBufferType type)
|
||||
: instance{instance}, type{type}
|
||||
class CommandBuffer
|
||||
{
|
||||
if (type == CommandBufferType::Dynamic)
|
||||
commandBuffers.resize(instance.GetMaxFramesInFlight());
|
||||
else
|
||||
commandBuffers.resize(1);
|
||||
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
|
||||
public:
|
||||
enum class Type
|
||||
{
|
||||
SingleUse, Dynamic
|
||||
};
|
||||
private:
|
||||
Instance& instance;
|
||||
|
||||
VkCommandBufferAllocateInfo allocateInfo{};
|
||||
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocateInfo.commandPool = instance.GetCommandPool();
|
||||
allocateInfo.commandBufferCount = commandBuffers.size();
|
||||
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, commandBuffers.data()), "Failed to allocate CommandBuffer");
|
||||
}
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
const Type type;
|
||||
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
|
||||
|
||||
~CommandBuffer()
|
||||
{
|
||||
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), commandBuffers.size(), commandBuffers.data());
|
||||
}
|
||||
public:
|
||||
CommandBuffer(Instance& instance, Type type)
|
||||
: instance{instance}, type{type}
|
||||
{
|
||||
if (type == Type::Dynamic)
|
||||
commandBuffers.resize(instance.GetMaxFramesInFlight());
|
||||
else
|
||||
commandBuffers.resize(1);
|
||||
|
||||
operator VkCommandBuffer() const
|
||||
{
|
||||
return currentCommandBuffer;
|
||||
}
|
||||
|
||||
// TODO: Test as constexpr function to see if it avoids the switch case
|
||||
void Begin()
|
||||
{
|
||||
VkCommandBufferBeginInfo beginInfo{};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = 0;
|
||||
beginInfo.pInheritanceInfo = nullptr;
|
||||
switch(type)
|
||||
{
|
||||
case CommandBufferType::SingleUse:
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
currentCommandBuffer = commandBuffers.front();
|
||||
break;
|
||||
case CommandBufferType::Dynamic:
|
||||
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
|
||||
break;
|
||||
default:
|
||||
CP_WARN("Unhandled enum case: %d", (int)type);
|
||||
VkCommandBufferAllocateInfo allocateInfo{};
|
||||
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocateInfo.commandPool = instance.GetCommandPool();
|
||||
allocateInfo.commandBufferCount = commandBuffers.size();
|
||||
CP_VK_ASSERT(vkAllocateCommandBuffers(instance.GetDevice(), &allocateInfo, commandBuffers.data()), "Failed to allocate CommandBuffer");
|
||||
}
|
||||
|
||||
vkResetCommandBuffer(currentCommandBuffer, 0);
|
||||
CP_VK_ASSERT(vkBeginCommandBuffer(currentCommandBuffer, &beginInfo), "Failed to begin command buffer");
|
||||
}
|
||||
~CommandBuffer()
|
||||
{
|
||||
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), commandBuffers.size(), commandBuffers.data());
|
||||
}
|
||||
|
||||
void End()
|
||||
{
|
||||
vkEndCommandBuffer(currentCommandBuffer);
|
||||
}
|
||||
operator VkCommandBuffer() const
|
||||
{
|
||||
return currentCommandBuffer;
|
||||
}
|
||||
|
||||
void Submit()
|
||||
{
|
||||
VkSubmitInfo submitInfo{};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = ¤tCommandBuffer;
|
||||
// TODO: Test as constexpr function to see if it avoids the switch case
|
||||
void Begin()
|
||||
{
|
||||
VkCommandBufferBeginInfo beginInfo{};
|
||||
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
beginInfo.flags = 0;
|
||||
beginInfo.pInheritanceInfo = nullptr;
|
||||
switch (type)
|
||||
{
|
||||
case Type::SingleUse:
|
||||
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
||||
currentCommandBuffer = commandBuffers.front();
|
||||
break;
|
||||
case Type::Dynamic:
|
||||
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
|
||||
break;
|
||||
default:
|
||||
CP_WARN("Unhandled enum case: %d", (int)type);
|
||||
}
|
||||
|
||||
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
||||
// TODO: if singleUse?
|
||||
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
||||
}
|
||||
vkResetCommandBuffer(currentCommandBuffer, 0);
|
||||
CP_VK_ASSERT(vkBeginCommandBuffer(currentCommandBuffer, &beginInfo), "Failed to begin command buffer");
|
||||
}
|
||||
|
||||
void SubmitAsGraphicsQueue()
|
||||
{
|
||||
instance.SubmitGraphicsQueue({currentCommandBuffer});
|
||||
}
|
||||
void End()
|
||||
{
|
||||
vkEndCommandBuffer(currentCommandBuffer);
|
||||
}
|
||||
|
||||
VkCommandBuffer GetHandle() const
|
||||
{
|
||||
return currentCommandBuffer;
|
||||
}
|
||||
};
|
||||
void Submit()
|
||||
{
|
||||
VkSubmitInfo submitInfo{};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = ¤tCommandBuffer;
|
||||
|
||||
vkQueueSubmit(instance.GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
|
||||
// TODO: if singleUse?
|
||||
vkQueueWaitIdle(instance.GetGraphicsQueue());
|
||||
}
|
||||
|
||||
void SubmitAsGraphicsQueue()
|
||||
{
|
||||
instance.SubmitGraphicsQueue({currentCommandBuffer});
|
||||
}
|
||||
|
||||
VkCommandBuffer GetHandle() const
|
||||
{
|
||||
return currentCommandBuffer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user