A bit more refactoring to expose the begin/end transactional nature.
This commit is contained in:
parent
b5c6774412
commit
3d36517b4f
3 changed files with 59 additions and 57 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
void DescriptorLayoutBuilder::add_binding(uint32_t binding, VkDescriptorType type)
|
void DescriptorLayoutBuilder::add_binding(uint32_t binding, VkDescriptorType type)
|
||||||
{
|
{
|
||||||
|
|
||||||
VkDescriptorSetLayoutBinding newbind{
|
VkDescriptorSetLayoutBinding newbind{
|
||||||
.binding = binding,
|
.binding = binding,
|
||||||
.descriptorType = type,
|
.descriptorType = type,
|
||||||
|
|
|
||||||
103
vk_engine.cpp
103
vk_engine.cpp
|
|
@ -71,8 +71,7 @@ void VulkanEngine::cleanup()
|
||||||
loadedEngine = nullptr;
|
loadedEngine = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanEngine::draw()
|
Transaction VulkanEngine::begin_transaction() {
|
||||||
{
|
|
||||||
// ZED: begin command transaction
|
// ZED: begin command transaction
|
||||||
// wait until the gpu has finished rendering the last frame. Timeout of 1 second
|
// wait until the gpu has finished rendering the last frame. Timeout of 1 second
|
||||||
VK_CHECK(vkWaitForFences(_device, 1, &get_current_frame()._renderFence, true, 1000000000));
|
VK_CHECK(vkWaitForFences(_device, 1, &get_current_frame()._renderFence, true, 1000000000));
|
||||||
|
|
@ -90,59 +89,17 @@ void VulkanEngine::draw()
|
||||||
|
|
||||||
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
|
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
|
||||||
|
|
||||||
|
return {cmd, swapchainImageIndex};
|
||||||
|
}
|
||||||
|
|
||||||
|
void VulkanEngine::commit_transaction(Transaction& t) {
|
||||||
|
VK_CHECK(vkEndCommandBuffer(t.cmd));
|
||||||
|
|
||||||
|
|
||||||
// ZED: draw the things
|
|
||||||
_drawExtent.width = _drawImage.imageExtent.width;
|
|
||||||
_drawExtent.height = _drawImage.imageExtent.height;
|
|
||||||
|
|
||||||
// transition our main draw image into general layout so we can write into it
|
|
||||||
// we will overwrite it all so we dont care about what was the older layout
|
|
||||||
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
|
|
||||||
|
|
||||||
draw_background(cmd);
|
|
||||||
|
|
||||||
// ZED: ?
|
|
||||||
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ZED: ?
|
|
||||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
|
||||||
|
|
||||||
vkutil::copy_image_to_image(cmd, _drawImage.image,
|
|
||||||
_swapchainImages[swapchainImageIndex],
|
|
||||||
_drawExtent, _swapchainExtent);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ZED: ?
|
|
||||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
|
||||||
|
|
||||||
_gui.draw_imgui(_swapchainExtent, cmd, _swapchainImageViews[swapchainImageIndex]);
|
|
||||||
// ZED: end drawing the things
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ZED: finalize image and commit command buffer
|
|
||||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
|
||||||
|
|
||||||
VK_CHECK(vkEndCommandBuffer(cmd));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ZED: submit command to queue
|
// ZED: submit command to queue
|
||||||
//prepare the submission to the queue.
|
//prepare the submission to the queue.
|
||||||
//we want to wait on the _presentSemaphore, as that semaphore is signaled when the swapchain is ready
|
//we want to wait on the _presentSemaphore, as that semaphore is signaled when the swapchain is ready
|
||||||
//we will signal the _renderSemaphore, to signal that rendering has finished
|
//we will signal the _renderSemaphore, to signal that rendering has finished
|
||||||
VkCommandBufferSubmitInfo cmdinfo = vkinit::command_buffer_submit_info(cmd);
|
VkCommandBufferSubmitInfo cmdinfo = vkinit::command_buffer_submit_info(t.cmd);
|
||||||
|
|
||||||
VkSemaphoreSubmitInfo waitInfo = vkinit::semaphore_submit_info(VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR,get_current_frame()._swapchainSemaphore);
|
VkSemaphoreSubmitInfo waitInfo = vkinit::semaphore_submit_info(VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR,get_current_frame()._swapchainSemaphore);
|
||||||
VkSemaphoreSubmitInfo signalInfo = vkinit::semaphore_submit_info(VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT, get_current_frame()._renderSemaphore);
|
VkSemaphoreSubmitInfo signalInfo = vkinit::semaphore_submit_info(VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT, get_current_frame()._renderSemaphore);
|
||||||
|
|
@ -164,7 +121,7 @@ void VulkanEngine::draw()
|
||||||
.pWaitSemaphores = &get_current_frame()._renderSemaphore,
|
.pWaitSemaphores = &get_current_frame()._renderSemaphore,
|
||||||
.swapchainCount = 1,
|
.swapchainCount = 1,
|
||||||
.pSwapchains = &_swapchain,
|
.pSwapchains = &_swapchain,
|
||||||
.pImageIndices = &swapchainImageIndex,
|
.pImageIndices = &t.swapchainImageIndex,
|
||||||
};
|
};
|
||||||
|
|
||||||
VK_CHECK(vkQueuePresentKHR(_graphicsQueue, &presentInfo));
|
VK_CHECK(vkQueuePresentKHR(_graphicsQueue, &presentInfo));
|
||||||
|
|
@ -173,6 +130,44 @@ void VulkanEngine::draw()
|
||||||
_frameNumber++;
|
_frameNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VulkanEngine::draw()
|
||||||
|
{
|
||||||
|
auto t = begin_transaction();
|
||||||
|
|
||||||
|
// ZED: draw the things
|
||||||
|
_drawExtent.width = _drawImage.imageExtent.width;
|
||||||
|
_drawExtent.height = _drawImage.imageExtent.height;
|
||||||
|
|
||||||
|
// transition our main draw image into general layout so we can write into it
|
||||||
|
// we will overwrite it all so we dont care about what was the older layout
|
||||||
|
vkutil::transition_image(t.cmd, _drawImage.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
|
||||||
|
|
||||||
|
draw_background(t.cmd);
|
||||||
|
|
||||||
|
// ZED: ?
|
||||||
|
vkutil::transition_image(t.cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||||
|
|
||||||
|
// ZED: ?
|
||||||
|
vkutil::transition_image(t.cmd, _swapchainImages[t.swapchainImageIndex],
|
||||||
|
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||||
|
|
||||||
|
vkutil::copy_image_to_image(t.cmd, _drawImage.image,
|
||||||
|
_swapchainImages[t.swapchainImageIndex],
|
||||||
|
_drawExtent, _swapchainExtent);
|
||||||
|
|
||||||
|
// ZED: ?
|
||||||
|
vkutil::transition_image(t.cmd, _swapchainImages[t.swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||||
|
|
||||||
|
_gui.draw_imgui(_swapchainExtent, t.cmd, _swapchainImageViews[t.swapchainImageIndex]);
|
||||||
|
// ZED: end drawing the things
|
||||||
|
|
||||||
|
// ZED: finalize image and commit command buffer
|
||||||
|
vkutil::transition_image(t.cmd, _swapchainImages[t.swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||||
|
|
||||||
|
commit_transaction(t);
|
||||||
|
}
|
||||||
|
|
||||||
void VulkanEngine::run()
|
void VulkanEngine::run()
|
||||||
{
|
{
|
||||||
//main loop
|
//main loop
|
||||||
|
|
@ -393,7 +388,7 @@ void VulkanEngine::init_descriptors() {
|
||||||
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1 }
|
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1 }
|
||||||
};
|
};
|
||||||
|
|
||||||
globalDescriptorAllocator.init_pool(_device, 10, sizes);
|
_globalDescriptorAllocator.init_pool(_device, 10, sizes);
|
||||||
|
|
||||||
// make the descriptor set layout for our compute draw
|
// make the descriptor set layout for our compute draw
|
||||||
{
|
{
|
||||||
|
|
@ -404,14 +399,14 @@ void VulkanEngine::init_descriptors() {
|
||||||
|
|
||||||
// other code
|
// other code
|
||||||
//allocate a descriptor set for our draw image
|
//allocate a descriptor set for our draw image
|
||||||
_drawImageDescriptors = globalDescriptorAllocator.allocate(_device,_drawImageDescriptorLayout);
|
_drawImageDescriptors = _globalDescriptorAllocator.allocate(_device,_drawImageDescriptorLayout);
|
||||||
|
|
||||||
VkDescriptorImageInfo imgInfo{
|
VkDescriptorImageInfo imgInfo{
|
||||||
.imageView = _drawImage.imageView,
|
.imageView = _drawImage.imageView,
|
||||||
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
|
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
VkWriteDescriptorSet drawImageWrite = {
|
VkWriteDescriptorSet drawImageWrite{
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||||
.dstSet = _drawImageDescriptors,
|
.dstSet = _drawImageDescriptors,
|
||||||
.dstBinding = 0,
|
.dstBinding = 0,
|
||||||
|
|
@ -424,7 +419,7 @@ void VulkanEngine::init_descriptors() {
|
||||||
|
|
||||||
//make sure both the descriptor allocator and the new layout get cleaned up properly
|
//make sure both the descriptor allocator and the new layout get cleaned up properly
|
||||||
_mainDeletionQueue.push_function([&]() {
|
_mainDeletionQueue.push_function([&]() {
|
||||||
globalDescriptorAllocator.destroy_pool(_device);
|
_globalDescriptorAllocator.destroy_pool(_device);
|
||||||
|
|
||||||
vkDestroyDescriptorSetLayout(_device, _drawImageDescriptorLayout, nullptr);
|
vkDestroyDescriptorSetLayout(_device, _drawImageDescriptorLayout, nullptr);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
10
vk_engine.h
10
vk_engine.h
|
|
@ -7,6 +7,11 @@
|
||||||
#include <vk_descriptors.h>
|
#include <vk_descriptors.h>
|
||||||
#include "vk_gui.h"
|
#include "vk_gui.h"
|
||||||
|
|
||||||
|
struct Transaction {
|
||||||
|
VkCommandBuffer cmd;
|
||||||
|
uint32_t swapchainImageIndex = 0;
|
||||||
|
};
|
||||||
|
|
||||||
struct DeletionQueue {
|
struct DeletionQueue {
|
||||||
std::deque<std::function<void()>> deletors;
|
std::deque<std::function<void()>> deletors;
|
||||||
|
|
||||||
|
|
@ -42,7 +47,7 @@ public:
|
||||||
VkPipeline _gradientPipeline;
|
VkPipeline _gradientPipeline;
|
||||||
VkPipelineLayout _gradientPipelineLayout;
|
VkPipelineLayout _gradientPipelineLayout;
|
||||||
|
|
||||||
DescriptorAllocator globalDescriptorAllocator;
|
DescriptorAllocator _globalDescriptorAllocator;
|
||||||
VkDescriptorSet _drawImageDescriptors;
|
VkDescriptorSet _drawImageDescriptors;
|
||||||
VkDescriptorSetLayout _drawImageDescriptorLayout;
|
VkDescriptorSetLayout _drawImageDescriptorLayout;
|
||||||
|
|
||||||
|
|
@ -108,6 +113,9 @@ public:
|
||||||
|
|
||||||
void immediate_submit(std::function<void(VkCommandBuffer cmd)>&& function);
|
void immediate_submit(std::function<void(VkCommandBuffer cmd)>&& function);
|
||||||
|
|
||||||
|
Transaction begin_transaction();
|
||||||
|
void commit_transaction(Transaction& cmd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init_vulkan();
|
void init_vulkan();
|
||||||
void init_swapchain();
|
void init_swapchain();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue