2020-03-24 21:05:32 +01:00
|
|
|
#include "VulkanCommandBuffer.h"
|
|
|
|
|
#include "VulkanInitializer.h"
|
|
|
|
|
#include "VulkanGraphics.h"
|
2020-05-05 01:51:13 +02:00
|
|
|
#include "VulkanPipeline.h"
|
2020-03-24 21:05:32 +01:00
|
|
|
#include "VulkanGraphicsEnums.h"
|
|
|
|
|
#include "VulkanFramebuffer.h"
|
|
|
|
|
#include "VulkanRenderPass.h"
|
2020-05-05 01:51:13 +02:00
|
|
|
#include "VulkanPipeline.h"
|
|
|
|
|
#include "VulkanDescriptorSets.h"
|
2020-06-02 11:46:18 +02:00
|
|
|
#include "Graphics/MeshBatch.h"
|
2020-03-24 21:05:32 +01:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Vulkan;
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
CmdBufferBase::CmdBufferBase(PGraphics graphics, VkCommandPool cmdPool)
|
2020-04-12 15:47:19 +02:00
|
|
|
: graphics(graphics), owner(cmdPool)
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CmdBufferBase::~CmdBufferBase()
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
graphics = nullptr;
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
CmdBuffer::CmdBuffer(PGraphics graphics, VkCommandPool cmdPool, PCommandBufferManager manager)
|
|
|
|
|
: CmdBufferBase(graphics, cmdPool), renderPass(nullptr), framebuffer(nullptr), subpassIndex(0), manager(manager)
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
VkCommandBufferAllocateInfo allocInfo =
|
|
|
|
|
init::CommandBufferAllocateInfo(cmdPool,
|
2020-04-12 15:47:19 +02:00
|
|
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
|
|
|
1);
|
2020-03-24 21:05:32 +01:00
|
|
|
VK_CHECK(vkAllocateCommandBuffers(graphics->getDevice(), &allocInfo, &handle))
|
2020-04-12 15:47:19 +02:00
|
|
|
|
|
|
|
|
fence = new Fence(graphics);
|
2020-03-24 21:05:32 +01:00
|
|
|
state = State::ReadyBegin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CmdBuffer::~CmdBuffer()
|
|
|
|
|
{
|
|
|
|
|
vkFreeCommandBuffers(graphics->getDevice(), owner, 1, &handle);
|
2020-04-12 15:47:19 +02:00
|
|
|
renderPass = nullptr;
|
|
|
|
|
framebuffer = nullptr;
|
|
|
|
|
waitSemaphores.clear();
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::begin()
|
|
|
|
|
{
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo =
|
|
|
|
|
init::CommandBufferBeginInfo();
|
|
|
|
|
beginInfo.pInheritanceInfo = nullptr;
|
|
|
|
|
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
|
|
|
|
|
state = State::InsideBegin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::end()
|
|
|
|
|
{
|
|
|
|
|
VK_CHECK(vkEndCommandBuffer(handle));
|
|
|
|
|
state = State::Ended;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
void CmdBuffer::beginRenderPass(PRenderPass newRenderPass, PFramebuffer newFramebuffer)
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
renderPass = newRenderPass;
|
|
|
|
|
framebuffer = newFramebuffer;
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
VkRenderPassBeginInfo beginInfo =
|
|
|
|
|
init::RenderPassBeginInfo();
|
|
|
|
|
beginInfo.clearValueCount = renderPass->getClearValueCount();
|
|
|
|
|
beginInfo.pClearValues = renderPass->getClearValues();
|
|
|
|
|
beginInfo.renderArea = renderPass->getRenderArea();
|
|
|
|
|
beginInfo.renderPass = renderPass->getHandle();
|
|
|
|
|
beginInfo.framebuffer = framebuffer->getHandle();
|
|
|
|
|
vkCmdBeginRenderPass(handle, &beginInfo, renderPass->getSubpassContents());
|
2020-04-12 15:47:19 +02:00
|
|
|
state = State::RenderPassActive;
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::endRenderPass()
|
|
|
|
|
{
|
|
|
|
|
vkCmdEndRenderPass(handle);
|
2020-04-12 15:47:19 +02:00
|
|
|
state = State::InsideBegin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::executeCommands(Array<PSecondaryCmdBuffer> commands)
|
|
|
|
|
{
|
|
|
|
|
assert(state == State::RenderPassActive);
|
|
|
|
|
Array<VkCommandBuffer> cmdBuffers(commands.size());
|
|
|
|
|
for (uint32 i = 0; i < commands.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
cmdBuffers[i] = commands[i]->getHandle();
|
|
|
|
|
}
|
|
|
|
|
vkCmdExecuteCommands(handle, cmdBuffers.size(), cmdBuffers.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::addWaitSemaphore(VkPipelineStageFlags flags, PSemaphore semaphore)
|
|
|
|
|
{
|
|
|
|
|
waitSemaphores.add(semaphore);
|
|
|
|
|
waitFlags.add(flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CmdBuffer::refreshFence()
|
|
|
|
|
{
|
|
|
|
|
if (state == State::Submitted)
|
|
|
|
|
{
|
|
|
|
|
if (fence->isSignaled())
|
|
|
|
|
{
|
|
|
|
|
state = State::ReadyBegin;
|
|
|
|
|
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
|
|
|
|
|
|
|
|
|
|
fence->reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assert(!fence->isSignaled());
|
|
|
|
|
}
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-05 01:51:13 +02:00
|
|
|
PFence CmdBuffer::getFence()
|
|
|
|
|
{
|
|
|
|
|
return fence;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
PCommandBufferManager CmdBuffer::getManager()
|
|
|
|
|
{
|
|
|
|
|
return manager;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
SecondaryCmdBuffer::SecondaryCmdBuffer(PGraphics graphics, VkCommandPool cmdPool)
|
2020-03-24 21:05:32 +01:00
|
|
|
: CmdBufferBase(graphics, cmdPool)
|
|
|
|
|
{
|
|
|
|
|
VkCommandBufferAllocateInfo allocInfo =
|
|
|
|
|
init::CommandBufferAllocateInfo(cmdPool,
|
2020-04-12 15:47:19 +02:00
|
|
|
VK_COMMAND_BUFFER_LEVEL_SECONDARY,
|
|
|
|
|
1);
|
2020-03-24 21:05:32 +01:00
|
|
|
VK_CHECK(vkAllocateCommandBuffers(graphics->getDevice(), &allocInfo, &handle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SecondaryCmdBuffer::~SecondaryCmdBuffer()
|
|
|
|
|
{
|
|
|
|
|
vkFreeCommandBuffers(graphics->getDevice(), owner, 1, &handle);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
void SecondaryCmdBuffer::begin(PCmdBuffer parent)
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo =
|
|
|
|
|
init::CommandBufferBeginInfo();
|
|
|
|
|
VkCommandBufferInheritanceInfo inheritanceInfo =
|
|
|
|
|
init::CommandBufferInheritanceInfo();
|
|
|
|
|
inheritanceInfo.framebuffer = parent->framebuffer->getHandle();
|
|
|
|
|
inheritanceInfo.renderPass = parent->renderPass->getHandle();
|
|
|
|
|
inheritanceInfo.subpass = parent->subpassIndex;
|
|
|
|
|
beginInfo.pInheritanceInfo = &inheritanceInfo;
|
2020-10-03 11:00:10 +02:00
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
|
2020-03-24 21:05:32 +01:00
|
|
|
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SecondaryCmdBuffer::end()
|
|
|
|
|
{
|
|
|
|
|
VK_CHECK(vkEndCommandBuffer(handle));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
void SecondaryCmdBuffer::setViewport(Gfx::PViewport viewport)
|
|
|
|
|
{
|
|
|
|
|
VkViewport vp = viewport.cast<Viewport>()->getHandle();
|
|
|
|
|
VkRect2D scissors = init::Rect2D(viewport->getSizeX(), viewport->getSizeY(), viewport->getOffsetX(), viewport->getOffsetY());
|
|
|
|
|
vkCmdSetViewport(handle, 0, 1, &vp);
|
|
|
|
|
vkCmdSetScissor(handle, 0, 1, &scissors);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 01:51:13 +02:00
|
|
|
void SecondaryCmdBuffer::bindPipeline(Gfx::PGraphicsPipeline gfxPipeline)
|
|
|
|
|
{
|
|
|
|
|
pipeline = gfxPipeline.cast<GraphicsPipeline>();
|
|
|
|
|
pipeline->bind(handle);
|
|
|
|
|
}
|
|
|
|
|
void SecondaryCmdBuffer::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorSet setHandle = descriptorSet.cast<DescriptorSet>()->getHandle();
|
|
|
|
|
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr);
|
|
|
|
|
}
|
2020-10-14 01:49:43 +02:00
|
|
|
void SecondaryCmdBuffer::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets)
|
2020-10-03 11:00:10 +02:00
|
|
|
{
|
|
|
|
|
VkDescriptorSet* sets = new VkDescriptorSet[descriptorSets.size()];
|
|
|
|
|
for(uint32 i = 0; i < descriptorSets.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
auto descriptorSet = descriptorSets[i].cast<DescriptorSet>();
|
|
|
|
|
sets[descriptorSet->getSetIndex()] = descriptorSet->getHandle();
|
|
|
|
|
}
|
|
|
|
|
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), 0, descriptorSets.size(), sets, 0, nullptr);
|
|
|
|
|
delete[] sets;
|
|
|
|
|
}
|
2020-06-08 01:44:47 +02:00
|
|
|
void SecondaryCmdBuffer::bindVertexBuffer(const Array<VertexInputStream>& streams)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2020-06-08 01:44:47 +02:00
|
|
|
Array<VkBuffer> buffers(streams.size());
|
|
|
|
|
Array<VkDeviceSize> offsets(streams.size());
|
|
|
|
|
for(uint32 i = 0; i < streams.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
PVertexBuffer buf = streams[i].vertexBuffer.cast<VertexBuffer>();
|
|
|
|
|
buffers[i] = buf->getHandle();
|
|
|
|
|
offsets[i] = streams[i].offset;
|
|
|
|
|
};
|
|
|
|
|
vkCmdBindVertexBuffers(handle, 0, streams.size(), buffers.data(), offsets.data());
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
void SecondaryCmdBuffer::bindIndexBuffer(Gfx::PIndexBuffer indexBuffer)
|
|
|
|
|
{
|
|
|
|
|
PIndexBuffer buf = indexBuffer.cast<IndexBuffer>();
|
2020-10-23 01:47:56 +02:00
|
|
|
vkCmdBindIndexBuffer(handle, buf->getHandle(), 0, cast(buf->getIndexType()));
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
void SecondaryCmdBuffer::draw(const MeshBatchElement& data)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
2020-06-02 11:46:18 +02:00
|
|
|
vkCmdDrawIndexed(handle, data.indexBuffer->getNumIndices(), data.numInstances, data.minVertexIndex, data.baseVertexIndex, 0);
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
CommandBufferManager::CommandBufferManager(PGraphics graphics, PQueue queue)
|
2020-06-08 01:44:47 +02:00
|
|
|
: graphics(graphics), queue(queue), queueFamilyIndex(queue->getFamilyIndex())
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
VkCommandPoolCreateInfo info =
|
|
|
|
|
init::CommandPoolCreateInfo();
|
|
|
|
|
info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
|
|
|
info.queueFamilyIndex = queue->getFamilyIndex();
|
|
|
|
|
|
|
|
|
|
VK_CHECK(vkCreateCommandPool(graphics->getDevice(), &info, nullptr, &commandPool));
|
|
|
|
|
|
2020-09-19 14:36:50 +02:00
|
|
|
activeCmdBuffer = new CmdBuffer(graphics, commandPool, this);
|
2020-03-24 21:05:32 +01:00
|
|
|
activeCmdBuffer->begin();
|
2020-09-19 14:36:50 +02:00
|
|
|
std::lock_guard lock(allocatedBufferLock);
|
2020-04-12 15:47:19 +02:00
|
|
|
allocatedBuffers.add(activeCmdBuffer);
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandBufferManager::~CommandBufferManager()
|
|
|
|
|
{
|
|
|
|
|
vkDestroyCommandPool(graphics->getDevice(), commandPool, nullptr);
|
2020-04-12 15:47:19 +02:00
|
|
|
graphics = nullptr;
|
|
|
|
|
queue = nullptr;
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PCmdBuffer CommandBufferManager::getCommands()
|
|
|
|
|
{
|
|
|
|
|
return activeCmdBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PSecondaryCmdBuffer CommandBufferManager::createSecondaryCmdBuffer()
|
|
|
|
|
{
|
2020-04-01 02:17:49 +02:00
|
|
|
return new SecondaryCmdBuffer(graphics, commandPool);
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:17:49 +02:00
|
|
|
void CommandBufferManager::submitCommands(PSemaphore signalSemaphore)
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
if (activeCmdBuffer->state == CmdBuffer::State::InsideBegin || activeCmdBuffer->state == CmdBuffer::State::RenderPassActive)
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
if (activeCmdBuffer->state == CmdBuffer::State::RenderPassActive)
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
|
|
|
|
std::cout << "End of renderpass forced" << std::endl;
|
|
|
|
|
activeCmdBuffer->endRenderPass();
|
|
|
|
|
}
|
|
|
|
|
activeCmdBuffer->end();
|
2020-04-12 15:47:19 +02:00
|
|
|
if (signalSemaphore != nullptr)
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
|
|
|
|
queue->submitCommandBuffer(activeCmdBuffer, signalSemaphore->getHandle());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
queue->submitCommandBuffer(activeCmdBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-19 14:36:50 +02:00
|
|
|
std::lock_guard lock(allocatedBufferLock);
|
2020-04-12 15:47:19 +02:00
|
|
|
for (uint32 i = 0; i < allocatedBuffers.size(); ++i)
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
|
|
|
|
PCmdBuffer cmdBuffer = allocatedBuffers[i];
|
2020-04-12 15:47:19 +02:00
|
|
|
cmdBuffer->refreshFence();
|
|
|
|
|
if (cmdBuffer->state == CmdBuffer::State::ReadyBegin)
|
2020-04-01 02:17:49 +02:00
|
|
|
{
|
|
|
|
|
activeCmdBuffer = cmdBuffer;
|
|
|
|
|
activeCmdBuffer->begin();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assert(cmdBuffer->state == CmdBuffer::State::Submitted);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-19 14:36:50 +02:00
|
|
|
activeCmdBuffer = new CmdBuffer(graphics, commandPool, this);
|
2020-04-01 02:17:49 +02:00
|
|
|
allocatedBuffers.add(activeCmdBuffer);
|
|
|
|
|
activeCmdBuffer->begin();
|
2020-03-24 21:05:32 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
|
|
|
|
|
void CommandBufferManager::waitForCommands(PCmdBuffer cmdBuffer, uint32 timeout)
|
|
|
|
|
{
|
|
|
|
|
cmdBuffer->fence->wait(timeout);
|
|
|
|
|
cmdBuffer->refreshFence();
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|