21 #include "context.hpp" 22 #include "platform/platform.hpp" 26 Context::PerFrame::PerFrame(VkDevice device,
unsigned graphicsQueueIndex)
28 , fenceManager(device)
29 , commandManager(device, VK_COMMAND_BUFFER_LEVEL_PRIMARY, graphicsQueueIndex)
30 , queueIndex(graphicsQueueIndex)
34 void Context::PerFrame::setSecondaryCommandManagersCount(
unsigned count)
36 secondaryCommandManagers.clear();
37 for (
unsigned i = 0; i < count; i++)
39 secondaryCommandManagers.emplace_back(
46 return pPlatform->getPhysicalDevice();
49 VkSemaphore Context::PerFrame::setSwapchainAcquireSemaphore(VkSemaphore acquireSemaphore)
51 VkSemaphore ret = swapchainAcquireSemaphore;
52 swapchainAcquireSemaphore = acquireSemaphore;
56 void Context::PerFrame::setSwapchainReleaseSemaphore(VkSemaphore releaseSemaphore)
58 if (swapchainReleaseSemaphore != VK_NULL_HANDLE)
59 vkDestroySemaphore(device, swapchainReleaseSemaphore,
nullptr);
60 swapchainReleaseSemaphore = releaseSemaphore;
63 void Context::PerFrame::beginFrame()
65 fenceManager.beginFrame();
66 commandManager.beginFrame();
67 for (
auto &pManager : secondaryCommandManagers)
68 pManager->beginFrame();
71 Context::PerFrame::~PerFrame()
73 if (swapchainAcquireSemaphore != VK_NULL_HANDLE)
74 vkDestroySemaphore(device, swapchainAcquireSemaphore,
nullptr);
75 if (swapchainReleaseSemaphore != VK_NULL_HANDLE)
76 vkDestroySemaphore(device, swapchainReleaseSemaphore,
nullptr);
79 void Context::waitIdle()
81 vkDeviceWaitIdle(device);
88 this->pPlatform = pPlatform;
100 setRenderingThreadCount(renderingThreadCount);
102 return RESULT_SUCCESS;
107 submitCommandBuffer(cmd, VK_NULL_HANDLE, VK_NULL_HANDLE);
117 if (getSwapchainReleaseSemaphore() == VK_NULL_HANDLE && getSwapchainAcquireSemaphore() != VK_NULL_HANDLE)
119 VkSemaphore releaseSemaphore;
120 VkSemaphoreCreateInfo semaphoreInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
121 VK_CHECK(vkCreateSemaphore(device, &semaphoreInfo,
nullptr, &releaseSemaphore));
122 perFrame[swapchainIndex]->setSwapchainReleaseSemaphore(releaseSemaphore);
125 submitCommandBuffer(cmd, getSwapchainAcquireSemaphore(), getSwapchainReleaseSemaphore());
128 void Context::submitCommandBuffer(VkCommandBuffer cmd, VkSemaphore acquireSemaphore, VkSemaphore releaseSemaphore)
132 VkFence fence = getFenceManager().requestClearedFence();
134 VkSubmitInfo info = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
135 info.commandBufferCount = 1;
136 info.pCommandBuffers = &cmd;
138 const VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
139 info.waitSemaphoreCount = acquireSemaphore != VK_NULL_HANDLE ? 1 : 0;
140 info.pWaitSemaphores = &acquireSemaphore;
141 info.pWaitDstStageMask = &waitStage;
142 info.signalSemaphoreCount = releaseSemaphore != VK_NULL_HANDLE ? 1 : 0;
143 info.pSignalSemaphores = &releaseSemaphore;
145 VK_CHECK(vkQueueSubmit(queue, 1, &info, fence));
void submit(VkCommandBuffer cmdBuffer)
Submit a command buffer to the queue.
Result onPlatformUpdate(Platform *pPlatform)
Called by the platform internally when platform either initializes itself or the swapchain has been r...
void submitSwapchain(VkCommandBuffer cmdBuffer)
Submit a command buffer to the queue which renders to the swapchain image.
CommandBufferManager(VkDevice device, VkCommandBufferLevel bufferLevel, unsigned graphicsQueueIndex)
Constructor.
VkPhysicalDevice getPhysicalDevice() const
Gets the Vulkan physical device assigned to the context.