Depth Buffers Added.

This commit is contained in:
Anish Bhobe 2025-06-20 19:40:15 +02:00
parent babbe93479
commit 52d3e63223
13 changed files with 147 additions and 29 deletions

View File

@ -180,6 +180,7 @@
<ClInclude Include="Blaze\MiscData.h" />
<ClInclude Include="Blaze\RenderDevice.h" />
<ClInclude Include="Blaze\TextureManager.h" />
<ClInclude Include="Blaze\VulkanHeader.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Blaze\AppState.cpp" />

View File

@ -77,6 +77,9 @@
<ClInclude Include="Blaze\TextureManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Blaze\VulkanHeader.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Blaze\AppState.cpp">

View File

@ -6,14 +6,14 @@
#include <limits>
#include <span>
#include <volk.h>
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_vulkan.h>
#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,
};

View File

@ -2,13 +2,10 @@
#include <cstdint>
#include <volk.h>
#include <vma/vk_mem_alloc.h>
#include <DirectXMath.h>
#include <span>
#include "VulkanHeader.h"
// TODO: Remove this dependency
#include "TextureManager.h"

View File

@ -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;
}

View File

@ -1,7 +1,8 @@
#pragma once
#include <utility>
#include <volk.h>
#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 );

View File

@ -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 = {

View File

@ -1,9 +1,8 @@
#pragma once
#include <array>
#include <volk.h>
#include <vma/vk_mem_alloc.h>
#include "VulkanHeader.h"
#include <DirectXMath.h>

View File

@ -389,7 +389,7 @@ RenderDevice* RenderDevice_Create( GlobalMemory* mem, RenderDevice::CreateInfo c
Frame* frames = reinterpret_cast<Frame*>( 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 ) );

View File

@ -1,14 +1,11 @@
#pragma once
#include <volk.h>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#include <vma/vk_mem_alloc.h>
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_vulkan.h>
#include "TextureManager.h"
#include "VulkanHeader.h"
struct GlobalMemory;
struct Frame;
struct TextureManager;

View File

@ -2,12 +2,10 @@
#include <optional>
#include <span>
#include <volk.h>
#include <vma/vk_mem_alloc.h>
#include "MacroUtils.h"
#include "RenderDevice.h"
#include "VulkanHeader.h"
struct GlobalMemory;

7
Blaze/VulkanHeader.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <volk.h>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#include <vma/vk_mem_alloc.h>