From 52d3e63223ec43644ec70fbf40feb11d1a064b3d Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Fri, 20 Jun 2025 19:40:15 +0200 Subject: [PATCH] Depth Buffers Added. --- Blaze.vcxproj | 1 + Blaze.vcxproj.filters | 3 ++ Blaze/Blaze.cpp | 25 ++++++++++-- Blaze/EntityManager.h | 5 +-- Blaze/Frame.cpp | 87 +++++++++++++++++++++++++++++++++++++++++- Blaze/Frame.h | 19 +++++++-- Blaze/GlobalMemory.cpp | 2 +- Blaze/MiscData.cpp | 9 +++-- Blaze/MiscData.h | 3 +- Blaze/RenderDevice.cpp | 2 +- Blaze/RenderDevice.h | 9 ++--- Blaze/TextureManager.h | 4 +- Blaze/VulkanHeader.h | 7 ++++ 13 files changed, 147 insertions(+), 29 deletions(-) create mode 100644 Blaze/VulkanHeader.h diff --git a/Blaze.vcxproj b/Blaze.vcxproj index ba571d0..05677ab 100644 --- a/Blaze.vcxproj +++ b/Blaze.vcxproj @@ -180,6 +180,7 @@ + diff --git a/Blaze.vcxproj.filters b/Blaze.vcxproj.filters index 80be8b5..c297453 100644 --- a/Blaze.vcxproj.filters +++ b/Blaze.vcxproj.filters @@ -77,6 +77,9 @@ Header Files + + Header Files + diff --git a/Blaze/Blaze.cpp b/Blaze/Blaze.cpp index 5babe39..4ef4f04 100644 --- a/Blaze/Blaze.cpp +++ b/Blaze/Blaze.cpp @@ -6,14 +6,14 @@ #include #include -#include - #define SDL_MAIN_USE_CALLBACKS 1 #include #include #include #include +#include "VulkanHeader.h" + #include "AppState.h" #include "EntityManager.h" #include "Frame.h" @@ -168,8 +168,27 @@ SDL_AppResult SDL_AppIterate( void* appstate ) .float32 = { 0.0f, 0.0f, 0.0f, 1.0f }, }; + VkClearDepthStencilValue constexpr static DEPTH_STENCIL_CLEAR = { + .depth = 1.0f, + .stencil = 0, + }; + VK_CHECK( vkBeginCommandBuffer( cmd, &beginInfo ) ); { + + VkRenderingAttachmentInfo const depthAttachmentInfo = { + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .pNext = nullptr, + .imageView = currentFrame.depthView, + .imageLayout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, + .resolveMode = VK_RESOLVE_MODE_NONE, + .resolveImageView = nullptr, + .resolveImageLayout = VK_IMAGE_LAYOUT_UNDEFINED, + .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + .clearValue = { .depthStencil = DEPTH_STENCIL_CLEAR }, + }; + VkRenderingAttachmentInfo const attachmentInfo = { .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .pNext = nullptr, @@ -192,7 +211,7 @@ SDL_AppResult SDL_AppIterate( void* appstate ) .viewMask = 0, .colorAttachmentCount = 1, .pColorAttachments = &attachmentInfo, - .pDepthAttachment = nullptr, + .pDepthAttachment = &depthAttachmentInfo, .pStencilAttachment = nullptr, }; diff --git a/Blaze/EntityManager.h b/Blaze/EntityManager.h index 558e1e1..2497f78 100644 --- a/Blaze/EntityManager.h +++ b/Blaze/EntityManager.h @@ -2,13 +2,10 @@ #include -#include - -#include - #include #include +#include "VulkanHeader.h" // TODO: Remove this dependency #include "TextureManager.h" diff --git a/Blaze/Frame.cpp b/Blaze/Frame.cpp index db0d621..d5676ed 100644 --- a/Blaze/Frame.cpp +++ b/Blaze/Frame.cpp @@ -15,12 +15,18 @@ Frame::Frame( VkCommandBuffer const commandBuffer, VkSemaphore const imageAcquiredSemaphore, VkSemaphore const renderFinishedSemaphore, - VkFence const frameReadyToReuse ) + VkFence const frameReadyToReuse, + VkImage const depthImage, + VmaAllocation const depthAllocation, + VkImageView const depthView ) : commandPool{ commandPool } , commandBuffer{ commandBuffer } , imageAcquiredSemaphore{ imageAcquiredSemaphore } , renderFinishedSemaphore{ renderFinishedSemaphore } , frameReadyToReuse{ frameReadyToReuse } + , depthImage{ depthImage } + , depthAllocation{ depthAllocation } + , depthView{ depthView } {} void Frame::destroy( RenderDevice const& renderDevice ) @@ -29,6 +35,9 @@ void Frame::destroy( RenderDevice const& renderDevice ) VkDevice const device = renderDevice.device; + vkDestroyImageView( device, Take( depthView ), nullptr ); + vmaDestroyImage( renderDevice.gpuAllocator, Take( depthImage ), Take( depthAllocation ) ); + vkDestroyCommandPool( device, Take( commandPool ), nullptr ); vkDestroyFence( device, Take( frameReadyToReuse ), nullptr ); vkDestroySemaphore( device, Take( imageAcquiredSemaphore ), nullptr ); @@ -41,7 +50,12 @@ Frame::~Frame() ASSERT( !isInit() ); } -void Frame_Create( Frame* frame, VkDevice const device, uint32_t const directQueueFamilyIndex ) +void Frame_Create( + Frame* frame, + VkDevice const device, + VmaAllocator const gpuAllocator, + uint32_t const directQueueFamilyIndex, + VkExtent2D const swapchainExtent ) { VkCommandPool commandPool; VkCommandBuffer commandBuffer; @@ -49,6 +63,10 @@ void Frame_Create( Frame* frame, VkDevice const device, uint32_t const directQue VkSemaphore renderFinishedSemaphore; VkFence frameReadyToReuse; + VkImage depthImage; + VmaAllocation depthAllocation; + VkImageView depthView; + { VkCommandPoolCreateInfo const commandPoolCreateInfo = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, @@ -83,9 +101,74 @@ void Frame_Create( Frame* frame, VkDevice const device, uint32_t const directQue VK_CHECK( vkCreateFence( device, &fenceCreateInfo, nullptr, &frameReadyToReuse ) ); } + { + VkImageCreateInfo const depthImageCreateInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_D32_SFLOAT, + .extent = { swapchainExtent.width, swapchainExtent.height, 1 }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .queueFamilyIndexCount = 0, + .pQueueFamilyIndices = nullptr, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED + }; + + VmaAllocationCreateInfo constexpr depthAllocationCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, + .usage = VMA_MEMORY_USAGE_GPU_ONLY, + .requiredFlags = 0, + .preferredFlags = 0, + .memoryTypeBits = 0, + .pool = nullptr, + .pUserData = nullptr, + .priority = 1.0f, + }; + + VK_CHECK( vmaCreateImage( + gpuAllocator, &depthImageCreateInfo, &depthAllocationCreateInfo, &depthImage, &depthAllocation, nullptr ) ); + + VkImageSubresourceRange constexpr subresourceRange = { + .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }; + + VkComponentMapping constexpr componentMapping = { + .r = VK_COMPONENT_SWIZZLE_IDENTITY, + .g = VK_COMPONENT_SWIZZLE_IDENTITY, + .b = VK_COMPONENT_SWIZZLE_IDENTITY, + .a = VK_COMPONENT_SWIZZLE_IDENTITY, + }; + + VkImageViewCreateInfo const imageViewCreateInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .image = depthImage, + .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = depthImageCreateInfo.format, + .components = componentMapping, + .subresourceRange = subresourceRange, + }; + + VK_CHECK( vkCreateImageView( device, &imageViewCreateInfo, nullptr, &depthView ) ); + } + frame->commandPool = commandPool; frame->commandBuffer = commandBuffer; frame->imageAcquiredSemaphore = imageAcquiredSemaphore; frame->renderFinishedSemaphore = renderFinishedSemaphore; frame->frameReadyToReuse = frameReadyToReuse; + frame->depthImage = depthImage; + frame->depthView = depthView; + frame->depthAllocation = depthAllocation; } diff --git a/Blaze/Frame.h b/Blaze/Frame.h index 2b7d9df..38cebd4 100644 --- a/Blaze/Frame.h +++ b/Blaze/Frame.h @@ -1,7 +1,8 @@ #pragma once #include -#include + +#include "VulkanHeader.h" struct RenderDevice; @@ -13,6 +14,10 @@ struct Frame VkSemaphore renderFinishedSemaphore; VkFence frameReadyToReuse; + VkImage depthImage; + VmaAllocation depthAllocation; + VkImageView depthView; + [[nodiscard]] bool isInit() const; Frame( @@ -20,7 +25,10 @@ struct Frame VkCommandBuffer commandBuffer, VkSemaphore imageAcquiredSemaphore, VkSemaphore renderFinishedSemaphore, - VkFence frameReadyToReuse ); + VkFence frameReadyToReuse, + VkImage depthImage, + VmaAllocation depthAllocation, + VkImageView depthView ); void destroy( RenderDevice const& renderDevice ); @@ -32,4 +40,9 @@ struct Frame ~Frame(); }; -void Frame_Create( Frame* frame, VkDevice device, uint32_t directQueueFamilyIndex ); +void Frame_Create( + Frame* frame, + VkDevice device, + VmaAllocator gpuAllocator, + uint32_t directQueueFamilyIndex, + VkExtent2D swapchainExtent ); diff --git a/Blaze/GlobalMemory.cpp b/Blaze/GlobalMemory.cpp index 4424178..bd13e10 100644 --- a/Blaze/GlobalMemory.cpp +++ b/Blaze/GlobalMemory.cpp @@ -25,7 +25,7 @@ std::byte* GlobalMemory::allocate( size_t const size ) std::byte* retVal = memory; memset( retVal, 0, size ); - memory += size; + memory += size; available -= size; SDL_LogInfo( SDL_LOG_CATEGORY_SYSTEM, diff --git a/Blaze/MiscData.cpp b/Blaze/MiscData.cpp index 1177e1a..ce8b288 100644 --- a/Blaze/MiscData.cpp +++ b/Blaze/MiscData.cpp @@ -195,9 +195,9 @@ bool MiscData::init( RenderDevice const& renderDevice ) .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .pNext = nullptr, .flags = 0, - .depthTestEnable = VK_FALSE, - .depthWriteEnable = VK_FALSE, - .depthCompareOp = VK_COMPARE_OP_ALWAYS, + .depthTestEnable = VK_TRUE, + .depthWriteEnable = VK_TRUE, + .depthCompareOp = VK_COMPARE_OP_LESS, .depthBoundsTestEnable = VK_FALSE, .stencilTestEnable = VK_FALSE, .front = {}, @@ -243,6 +243,7 @@ bool MiscData::init( RenderDevice const& renderDevice ) .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR, .colorAttachmentCount = 1, .pColorAttachmentFormats = &renderDevice.swapchainFormat, + .depthAttachmentFormat = VK_FORMAT_D32_SFLOAT, }; VkGraphicsPipelineCreateInfo const graphicsPipelineCreateInfo = { @@ -448,7 +449,7 @@ bool MiscData::init( RenderDevice const& renderDevice ) // Frame Time frameTimeEntryCount = 16; memset( frameTime, 0, frameTimeEntryCount * sizeof( float ) ); - frameTimeSum = 0; + frameTimeSum = 0; frameTimeWriteHead = 0; return true; diff --git a/Blaze/MiscData.h b/Blaze/MiscData.h index fabd893..4bfe827 100644 --- a/Blaze/MiscData.h +++ b/Blaze/MiscData.h @@ -1,9 +1,8 @@ #pragma once #include -#include -#include +#include "VulkanHeader.h" #include diff --git a/Blaze/RenderDevice.cpp b/Blaze/RenderDevice.cpp index 28820c1..d95993c 100644 --- a/Blaze/RenderDevice.cpp +++ b/Blaze/RenderDevice.cpp @@ -389,7 +389,7 @@ RenderDevice* RenderDevice_Create( GlobalMemory* mem, RenderDevice::CreateInfo c Frame* frames = reinterpret_cast( mem->allocate( sizeof( Frame ) * swapchainImageCount ) ); for ( uint32_t i = 0; i != swapchainImageCount; ++i ) { - Frame_Create( frames + i, device, directQueueFamilyIndex.value() ); + Frame_Create( frames + i, device, gpuAllocator, directQueueFamilyIndex.value(), swapchainExtent ); } std::byte* allocation = mem->allocate( sizeof( RenderDevice ), alignof( RenderDevice ) ); diff --git a/Blaze/RenderDevice.h b/Blaze/RenderDevice.h index c7ba37d..95c1564 100644 --- a/Blaze/RenderDevice.h +++ b/Blaze/RenderDevice.h @@ -1,14 +1,11 @@ #pragma once -#include - -#define VMA_STATIC_VULKAN_FUNCTIONS 0 -#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 -#include - #include #include +#include "TextureManager.h" +#include "VulkanHeader.h" + struct GlobalMemory; struct Frame; struct TextureManager; diff --git a/Blaze/TextureManager.h b/Blaze/TextureManager.h index a786d6c..75e6457 100644 --- a/Blaze/TextureManager.h +++ b/Blaze/TextureManager.h @@ -2,12 +2,10 @@ #include #include -#include - -#include #include "MacroUtils.h" #include "RenderDevice.h" +#include "VulkanHeader.h" struct GlobalMemory; diff --git a/Blaze/VulkanHeader.h b/Blaze/VulkanHeader.h new file mode 100644 index 0000000..58eae2e --- /dev/null +++ b/Blaze/VulkanHeader.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 +#include