Updated to the next chapter of vkguide https://vkguide.dev/docs/new_chapter_2/vulkan_shader_drawing/
This commit is contained in:
parent
55e38788b4
commit
40717cf8e4
9 changed files with 216 additions and 14 deletions
|
|
@ -12,6 +12,9 @@
|
|||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#define VMA_IMPLEMENTATION
|
||||
#include "vk_mem_alloc.h"
|
||||
|
||||
constexpr bool bUseValidationLayers = false;
|
||||
|
||||
VulkanEngine* loadedEngine = nullptr;
|
||||
|
|
@ -59,8 +62,12 @@ void VulkanEngine::cleanup()
|
|||
vkDestroyFence(_device, _frames[i]._renderFence, nullptr);
|
||||
vkDestroySemaphore(_device, _frames[i]._renderSemaphore, nullptr);
|
||||
vkDestroySemaphore(_device ,_frames[i]._swapchainSemaphore, nullptr);
|
||||
|
||||
_frames[i]._deletionQueue.flush();
|
||||
}
|
||||
|
||||
_mainDeletionQueue.flush();
|
||||
|
||||
destroy_swapchain();
|
||||
|
||||
vkDestroySurfaceKHR(_instance, _surface, nullptr);
|
||||
|
|
@ -77,6 +84,8 @@ void VulkanEngine::draw()
|
|||
{
|
||||
// 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));
|
||||
get_current_frame()._deletionQueue.flush();
|
||||
|
||||
VK_CHECK(vkResetFences(_device, 1, &get_current_frame()._renderFence));
|
||||
|
||||
uint32_t swapchainImageIndex = 0;
|
||||
|
|
@ -87,19 +96,28 @@ void VulkanEngine::draw()
|
|||
|
||||
VkCommandBufferBeginInfo cmdBeginInfo = vkinit::command_buffer_begin_info(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
|
||||
|
||||
_drawExtent.width = _drawImage.imageExtent.width;
|
||||
_drawExtent.height = _drawImage.imageExtent.height;
|
||||
|
||||
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
|
||||
// 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
|
||||
|
||||
VkClearColorValue clearValue;
|
||||
float flash = std::abs(std::sin(float(_frameNumber) / 120.0f));
|
||||
clearValue = { { 0.0f, 0.0f, flash, 1.0f} };
|
||||
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
|
||||
|
||||
VkImageSubresourceRange clearRange = vkinit::image_subresource_range(VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
draw_background(cmd);
|
||||
|
||||
vkCmdClearColorImage(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange);
|
||||
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
|
||||
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);
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||
VK_CHECK(vkEndCommandBuffer(cmd));
|
||||
|
||||
//prepare the submission to the queue.
|
||||
|
|
@ -226,6 +244,18 @@ void VulkanEngine::init_vulkan() {
|
|||
|
||||
_graphicsQueue = vkbDevice.get_queue(vkb::QueueType::graphics).value();
|
||||
_graphicsQueueFamily = vkbDevice.get_queue_index(vkb::QueueType::graphics).value();
|
||||
|
||||
// initialize the memory allocator
|
||||
VmaAllocatorCreateInfo allocatorInfo{};
|
||||
allocatorInfo.physicalDevice = _chosenGPU;
|
||||
allocatorInfo.device = _device;
|
||||
allocatorInfo.instance = _instance;
|
||||
allocatorInfo.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
|
||||
vmaCreateAllocator(&allocatorInfo, &_allocator);
|
||||
|
||||
_mainDeletionQueue.push_function([&]() {
|
||||
vmaDestroyAllocator(_allocator);
|
||||
});
|
||||
}
|
||||
|
||||
void VulkanEngine::create_swapchain(uint32_t width, uint32_t height) {
|
||||
|
|
@ -261,6 +291,41 @@ void VulkanEngine::destroy_swapchain() {
|
|||
|
||||
void VulkanEngine::init_swapchain() {
|
||||
create_swapchain(_windowExtent.width, _windowExtent.height);
|
||||
|
||||
VkExtent3D drawImageExtent = {
|
||||
_windowExtent.width,
|
||||
_windowExtent.height,
|
||||
1
|
||||
};
|
||||
|
||||
_drawImage.imageFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
|
||||
_drawImage.imageExtent = drawImageExtent;
|
||||
|
||||
VkImageUsageFlags drawImageUsages{};
|
||||
drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
||||
drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
drawImageUsages |= VK_IMAGE_USAGE_STORAGE_BIT;
|
||||
drawImageUsages |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
|
||||
VkImageCreateInfo rimg_info = vkinit::image_create_info(_drawImage.imageFormat, drawImageUsages, drawImageExtent);
|
||||
|
||||
VmaAllocationCreateInfo rimg_allocinfo{};
|
||||
rimg_allocinfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||
rimg_allocinfo.requiredFlags = VkMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
|
||||
//allocate and create the image
|
||||
vmaCreateImage(_allocator, &rimg_info, &rimg_allocinfo, &_drawImage.image, &_drawImage.allocation, nullptr);
|
||||
|
||||
//build a image-view for the draw image to use for rendering
|
||||
VkImageViewCreateInfo rview_info = vkinit::imageview_create_info(_drawImage.imageFormat, _drawImage.image, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
|
||||
VK_CHECK(vkCreateImageView(_device, &rview_info, nullptr, &_drawImage.imageView));
|
||||
|
||||
//add to deletion queues
|
||||
_mainDeletionQueue.push_function([=, this]() {
|
||||
vkDestroyImageView(_device, _drawImage.imageView, nullptr);
|
||||
vmaDestroyImage(_allocator, _drawImage.image, _drawImage.allocation);
|
||||
});
|
||||
}
|
||||
|
||||
void VulkanEngine::init_commands() {
|
||||
|
|
@ -291,3 +356,13 @@ void VulkanEngine::init_sync_structures() {
|
|||
}
|
||||
}
|
||||
|
||||
void VulkanEngine::draw_background(VkCommandBuffer cmd)
|
||||
{
|
||||
VkClearColorValue clearValue;
|
||||
float flash = std::abs(std::sin(float(_frameNumber) / 120.0f));
|
||||
clearValue = { { 0.0f, 0.0f, flash, 1.0f} };
|
||||
|
||||
VkImageSubresourceRange clearRange = vkinit::image_subresource_range(VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
|
||||
vkCmdClearColorImage(cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue