A bit more refactoring to expose the begin/end transactional nature.

This commit is contained in:
Zed A. Shaw 2025-12-10 12:58:55 -05:00
parent b5c6774412
commit 3d36517b4f
3 changed files with 59 additions and 57 deletions

View file

@ -2,7 +2,6 @@
void DescriptorLayoutBuilder::add_binding(uint32_t binding, VkDescriptorType type)
{
VkDescriptorSetLayoutBinding newbind{
.binding = binding,
.descriptorType = type,

View file

@ -71,8 +71,7 @@ void VulkanEngine::cleanup()
loadedEngine = nullptr;
}
void VulkanEngine::draw()
{
Transaction VulkanEngine::begin_transaction() {
// ZED: begin command transaction
// 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));
@ -90,64 +89,22 @@ void VulkanEngine::draw()
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
return {cmd, swapchainImageIndex};
}
// 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));
void VulkanEngine::commit_transaction(Transaction& t) {
VK_CHECK(vkEndCommandBuffer(t.cmd));
// ZED: submit command to 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 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 signalInfo = vkinit::semaphore_submit_info(VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT, get_current_frame()._renderSemaphore);
VkSubmitInfo2 submit = vkinit::submit_info(&cmdinfo,&signalInfo,&waitInfo);
VkSubmitInfo2 submit = vkinit::submit_info(&cmdinfo, &signalInfo, &waitInfo);
//submit command buffer to the queue and execute it.
// _renderFence will now block until the graphic commands finish execution
@ -164,7 +121,7 @@ void VulkanEngine::draw()
.pWaitSemaphores = &get_current_frame()._renderSemaphore,
.swapchainCount = 1,
.pSwapchains = &_swapchain,
.pImageIndices = &swapchainImageIndex,
.pImageIndices = &t.swapchainImageIndex,
};
VK_CHECK(vkQueuePresentKHR(_graphicsQueue, &presentInfo));
@ -173,6 +130,44 @@ void VulkanEngine::draw()
_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()
{
//main loop
@ -393,7 +388,7 @@ void VulkanEngine::init_descriptors() {
{ 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
{
@ -404,14 +399,14 @@ void VulkanEngine::init_descriptors() {
// other code
//allocate a descriptor set for our draw image
_drawImageDescriptors = globalDescriptorAllocator.allocate(_device,_drawImageDescriptorLayout);
_drawImageDescriptors = _globalDescriptorAllocator.allocate(_device,_drawImageDescriptorLayout);
VkDescriptorImageInfo imgInfo{
.imageView = _drawImage.imageView,
.imageLayout = VK_IMAGE_LAYOUT_GENERAL,
};
VkWriteDescriptorSet drawImageWrite = {
VkWriteDescriptorSet drawImageWrite{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = _drawImageDescriptors,
.dstBinding = 0,
@ -424,7 +419,7 @@ void VulkanEngine::init_descriptors() {
//make sure both the descriptor allocator and the new layout get cleaned up properly
_mainDeletionQueue.push_function([&]() {
globalDescriptorAllocator.destroy_pool(_device);
_globalDescriptorAllocator.destroy_pool(_device);
vkDestroyDescriptorSetLayout(_device, _drawImageDescriptorLayout, nullptr);
});

View file

@ -7,6 +7,11 @@
#include <vk_descriptors.h>
#include "vk_gui.h"
struct Transaction {
VkCommandBuffer cmd;
uint32_t swapchainImageIndex = 0;
};
struct DeletionQueue {
std::deque<std::function<void()>> deletors;
@ -42,7 +47,7 @@ public:
VkPipeline _gradientPipeline;
VkPipelineLayout _gradientPipelineLayout;
DescriptorAllocator globalDescriptorAllocator;
DescriptorAllocator _globalDescriptorAllocator;
VkDescriptorSet _drawImageDescriptors;
VkDescriptorSetLayout _drawImageDescriptorLayout;
@ -108,6 +113,9 @@ public:
void immediate_submit(std::function<void(VkCommandBuffer cmd)>&& function);
Transaction begin_transaction();
void commit_transaction(Transaction& cmd);
private:
void init_vulkan();
void init_swapchain();