Skip to content

Commit

Permalink
Update for Rendering and presentation beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
Khrysys committed Oct 26, 2023
1 parent 3e9cfe2 commit 6b52de2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/dragon/graphics/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ namespace Dragon::Graphics {
std::vector<VkFramebuffer> framebuffers; /**Framebuffers for rendering multiple frames to the window*/
VkCommandPool commandPool; /**<The thing responsible for the graphical GPU commands here*/
VkCommandBuffer commandBuffer;
VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore;
VkFence inFlightFence;

void recordCommandBuffer(VkCommandBuffer commandBuffer, size_t imageIndex);
public:
/**
Expand Down
4 changes: 1 addition & 3 deletions src/window_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ Dragon::Swapchain Dragon::Graphics::Window::recreateSwapchain(Dragon::Device dev
return result.value();
}

void Dragon::Graphics::Window::update(Dragon::Graphics::Engine* parent) {
if(this->shouldClose()) return;
}


void Dragon::Graphics::Window::close(VkInstance instance, VkDevice device) {
vkDestroyCommandPool(device, this->commandPool, nullptr);
Expand Down
22 changes: 21 additions & 1 deletion src/window_finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,25 @@ void createPipeline(Dragon::Device device, VkPipelineLayout pipelineLayout, VkRe
vkDestroyShaderModule(device, vert, nullptr);
vkDestroyShaderModule(device, frag, nullptr);
}

void createSyncObjects(Dragon::Device device, VkSemaphore* imageAvailableSemaphore, VkSemaphore* renderFinishedSemaphore, VkFence* inFlightFence) {
VkSemaphoreCreateInfo semaphoreInfo{};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VkFenceCreateInfo fenceInfo{};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
VkResult result = vkCreateSemaphore(device, &semaphoreInfo, nullptr, imageAvailableSemaphore);
if (result != VK_SUCCESS) {
throw fmt::format("vkCreateSemaphore failed with {}", string_VkResult(result));
}
result = vkCreateSemaphore(device, &semaphoreInfo, nullptr, renderFinishedSemaphore);
if (result != VK_SUCCESS) {
throw fmt::format("vkCreateSemaphore failed with {}", string_VkResult(result));
}
result = vkCreateFence(device, &fenceInfo, nullptr, inFlightFence);
if(result != VK_SUCCESS) {
throw fmt::format("vkCreateFence failed with {}", string_VkResult(result));
}
}
void Dragon::Graphics::Window::finalize(Dragon::Graphics::Engine* parent) {
this->swapchain = this->recreateSwapchain(parent->getParent()->getDevice());

Expand Down Expand Up @@ -226,4 +244,6 @@ void Dragon::Graphics::Window::finalize(Dragon::Graphics::Engine* parent) {
if (result != VK_SUCCESS) {
throw fmt::format("failed to allocate command buffers with {}", string_VkResult(result));
}

createSyncObjects(parent->getParent()->getDevice(), &this->imageAvailableSemaphore, &this->renderFinishedSemaphore, &this->inFlightFence);
}
33 changes: 32 additions & 1 deletion src/window_update.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <dragon/graphics.hpp>

void Dragon::Graphics::Window::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) {
void Dragon::Graphics::Window::recordCommandBuffer(VkCommandBuffer commandBuffer, size_t imageIndex) {
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = 0; // Optional
Expand Down Expand Up @@ -48,4 +48,35 @@ void Dragon::Graphics::Window::recordCommandBuffer(VkCommandBuffer commandBuffer
if(result != VK_SUCCESS) {
throw fmt::format("vkEndCommandBuffer failed with {}", string_VkResult(result));
}
}

void Dragon::Graphics::Window::update(Dragon::Graphics::Engine* parent) {
if(this->shouldClose()) return;
vkWaitForFences(parent->getParent()->getDevice(), 1, &this->inFlightFence, VK_TRUE, UINT64_MAX);
vkResetFences(parent->getParent()->getDevice(), 1, &this->inFlightFence);
uint32_t imageIndex;
vkAcquireNextImageKHR(parent->getParent()->getDevice(), this->swapchain, UINT64_MAX, this->imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
vkResetCommandBuffer(this->commandBuffer, 0);
this->recordCommandBuffer(this->commandBuffer, imageIndex);

VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;

VkSemaphore waitSemaphores[] = {this->imageAvailableSemaphore};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &this->commandBuffer;

VkSemaphore signalSemaphores[] = {this->renderFinishedSemaphore};
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;

VkResult result = vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFence);

if (result != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
}
}

0 comments on commit 6b52de2

Please sign in to comment.