From 1417df96d16871aed81ec8c7457df3d5e57fcead Mon Sep 17 00:00:00 2001 From: antopilo Date: Sun, 4 May 2025 22:33:47 -0400 Subject: [PATCH] Renderer is now using new buffer management system --- .../Rendering/Vulkan/BindlessDescriptor.cpp | 12 +- .../Rendering/Vulkan/BindlessDescriptor.h | 7 + Nuake/Source/Nuake/Rendering/Vulkan/Sampler.h | 23 +++ .../Rendering/Vulkan/SceneRenderPipeline.cpp | 77 ++------ .../Vulkan/VulkanAllocatedBuffer.cpp | 2 +- .../Nuake/Rendering/Vulkan/VulkanRenderer.cpp | 4 +- .../Rendering/Vulkan/VulkanResources.cpp | 165 ++---------------- 7 files changed, 67 insertions(+), 223 deletions(-) create mode 100644 Nuake/Source/Nuake/Rendering/Vulkan/Sampler.h diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.cpp b/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.cpp index 45b3acd5..3db2b14d 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.cpp +++ b/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.cpp @@ -92,7 +92,7 @@ void Descriptor::Bind(VkCommandBuffer cmd, VkPipelineLayout layout) } BindlessDescriptor::BindlessDescriptor(ResourceType type, BindlessInfo& info) : - Info(info), + Info(info), Type(type) { // Create a buffer that holds for N frame in flights of data @@ -119,7 +119,7 @@ BindlessDescriptor::BindlessDescriptor(ResourceType type, BindlessInfo& info) : // Create buffer and map, CPU -> GPU since we will writing to it directly // Size of buffer is: size_of(ResourceType) * FRAME_OVERLAP since 1 buffer will hold N Frames in flight // TODO(antopilo): move mapped pointer inside buffer directly - const BufferUsage usage = BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST; + const BufferUsage usage = BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST | BufferUsage::TRANSFER_SRC; const MemoryUsage memoryUsage = MemoryUsage::CPU_TO_GPU; const size_t size = info.ResourceElementSize[type] * info.ResourceCount[type]; const size_t totalSize = size * FRAME_OVERLAP; @@ -145,6 +145,7 @@ void BindlessDescriptor::WriteToBuffer(int32_t frameIndex, void* data, size_t si const size_t offsetSize = Info.ResourceCount[Type] * Info.ResourceElementSize[Type]; size_t offset = currentFrame * offsetSize; + LastWriteSize = size; memcpy(desc.DataPtr, data, size); } @@ -156,7 +157,7 @@ void BindlessDescriptor::Swap(int32_t frameIndex) auto& nextDesc = Descriptors[nextFrame]; // memcpy from currentFrame to next frame - memcpy(desc.DataPtr, nextDesc.DataPtr, desc.Size); + //memcpy(nextDesc.DataPtr, desc.DataPtr, LastWriteSize); } void BindlessDescriptor::Bind(VkCommandBuffer cmd, int32_t frameIndex, VkPipelineLayout layout) @@ -173,7 +174,7 @@ ResourceDescriptors::ResourceDescriptors(const ResourceDescriptorsLimits& limits AddResourceDescriptors(limits.MaxSampler, 2); AddResourceDescriptors(limits.MaxMaterial, 3); AddResourceDescriptors(limits.MaxTexture, 4); - AddResourceDescriptors(limits.MaxLight, 4); + AddResourceDescriptors(limits.MaxLight, 5); AddResourceDescriptors(limits.MaxView, 6); } @@ -195,4 +196,7 @@ void ResourceDescriptors::Bind(VkCommandBuffer cmd, int32_t frame, VkPipelineLay */ Descriptors[ResourceType::Transform].Bind(cmd, frame, layout); + Descriptors[ResourceType::Material].Bind(cmd, frame, layout); + Descriptors[ResourceType::Light].Bind(cmd, frame, layout); + Descriptors[ResourceType::View].Bind(cmd, frame, layout); } diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.h b/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.h index ec1dcbd0..72375808 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.h +++ b/Nuake/Source/Nuake/Rendering/Vulkan/BindlessDescriptor.h @@ -72,6 +72,7 @@ namespace Nuake VkDescriptorSetLayout DescriptorLayout; BindlessInfo Info; ResourceType Type; + size_t LastWriteSize = 0; public: // Delete copy @@ -91,6 +92,12 @@ namespace Nuake void Bind(VkCommandBuffer cmd, int32_t frameIndex, VkPipelineLayout layout); }; + struct ResourceDescriptorDef + { + size_t Size; + uint32_t Slot; + }; + struct ResourceDescriptorsLimits { size_t MaxTransform; diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/Sampler.h b/Nuake/Source/Nuake/Rendering/Vulkan/Sampler.h new file mode 100644 index 00000000..a1568b81 --- /dev/null +++ b/Nuake/Source/Nuake/Rendering/Vulkan/Sampler.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace Nuake +{ + enum class SamplerType + { + Linear, + Nearest + }; + + class Sampler + { + private: + VkSampler vkSampler; + float maxAnisotropy; + + public: + Sampler(SamplerType samplerType); + ~Sampler(); + }; +} \ No newline at end of file diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/SceneRenderPipeline.cpp b/Nuake/Source/Nuake/Rendering/Vulkan/SceneRenderPipeline.cpp index 8c0f6fd9..f7ff0e3e 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/SceneRenderPipeline.cpp +++ b/Nuake/Source/Nuake/Rendering/Vulkan/SceneRenderPipeline.cpp @@ -39,12 +39,8 @@ ShadowRenderPipeline::ShadowRenderPipeline() res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); Cmd& cmd = ctx.commandBuffer; - //cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); shadowPass.SetRender([&](PassRenderContext& ctx) { auto& cmd = ctx.commandBuffer; @@ -381,14 +377,11 @@ void SceneRenderPipeline::RecreatePipeline() { auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); Cmd& cmd = ctx.commandBuffer; - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); gBufferPass.SetRender([&](PassRenderContext& ctx) { @@ -491,12 +484,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); // Bind noise kernel cmd.BindDescriptorSet(layout, res.SSAOKernelDescriptor, 7); @@ -533,12 +523,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); blurConstant.sourceTextureID = res.GetBindlessTextureID(SSAOOutput->GetID()); blurConstant.sourceSize = SSAOOutput->GetSize(); @@ -609,12 +596,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& res = GPUResources::Get(); // Bindless - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); // Inputs shadingConstant.AlbedoTextureID = res.GetBindlessTextureID(GBufferAlbedo->GetID()); @@ -665,12 +649,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& res = GPUResources::Get(); // Bindless - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); // Inputs tonemapConstant.Exposure = ctx.scene->GetEnvironment()->Exposure; @@ -703,14 +684,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - //ctx.renderPass->SetClearColor({0, 0, 0, 0}); - - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); volumetricPass.SetRender([&](PassRenderContext& ctx) { @@ -760,13 +736,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); volumetricBlurPass.SetRender([&](PassRenderContext& ctx) { @@ -793,13 +765,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); volumetricCombinePass.SetRender([&](PassRenderContext& ctx) { @@ -831,13 +799,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); gizmoPass.SetRender([&](PassRenderContext& ctx) { @@ -856,13 +820,9 @@ void SceneRenderPipeline::RecreatePipeline() Cmd& cmd = ctx.commandBuffer; auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); gizmoCombinePass.SetRender([&](PassRenderContext& ctx) { @@ -892,13 +852,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - // Bindless - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); outlinePass.SetRender([&](PassRenderContext& ctx) { @@ -933,12 +889,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); linePass.SetRender([&](PassRenderContext& ctx) { @@ -958,12 +911,9 @@ void SceneRenderPipeline::RecreatePipeline() auto& layout = ctx.renderPass->PipelineLayout; auto& res = GPUResources::Get(); - cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0); + res.BindSceneData(ctx.commandBuffer.GetCmdBuffer(), layout); cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2); - cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3); cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4); - cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5); - cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6); }); lineCombinePass.SetRender([&](PassRenderContext& ctx) { @@ -980,7 +930,6 @@ void SceneRenderPipeline::RecreatePipeline() cmd.DrawIndexed(6); }); - GBufferPipeline.Build(); } diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanAllocatedBuffer.cpp b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanAllocatedBuffer.cpp index 2bc06763..f06bfdb0 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanAllocatedBuffer.cpp +++ b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanAllocatedBuffer.cpp @@ -27,7 +27,7 @@ AllocatedBuffer::AllocatedBuffer(size_t inSize, BufferUsage inFlags, MemoryUsage VmaAllocationCreateInfo vmaallocInfo = {}; vmaallocInfo.usage = static_cast(inUsage); - vmaallocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; + vmaallocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; MemUsage = inUsage; diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanRenderer.cpp b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanRenderer.cpp index 1849d353..f5a4416d 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanRenderer.cpp +++ b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanRenderer.cpp @@ -749,6 +749,7 @@ bool VkRenderer::Draw() VK_CALL(vkWaitForFences(Device, 1, &GetCurrentFrame().RenderFence, true, 1000000000)); + if (SurfaceSize != Window::Get()->GetSize()) { RecreateSwapchain(); @@ -771,6 +772,8 @@ bool VkRenderer::Draw() VK_CALL(vkResetFences(Device, 1, &GetCurrentFrame().RenderFence)); + GPUResources::Get().Swap(FrameNumber); + // Note: this will be the meat of the engine that should be here. VkCommandBuffer cmd = GetCurrentFrame().CommandBuffer; VK_CALL(vkResetCommandBuffer(cmd, 0)); @@ -881,7 +884,6 @@ void VkRenderer::EndDraw() // Increase the number of frames drawn FrameNumber++; - //GPUResources::Get().Swap(FrameNumber); } void VkRenderer::DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView) diff --git a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanResources.cpp b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanResources.cpp index 37e292f8..67ca36ba 100644 --- a/Nuake/Source/Nuake/Rendering/Vulkan/VulkanResources.cpp +++ b/Nuake/Source/Nuake/Rendering/Vulkan/VulkanResources.cpp @@ -46,12 +46,12 @@ void GPUResources::Init() ResourceDescriptorsLimits limits { - .MaxTransform = 100, - .MaxView = 100, - .MaxMaterial = 100, - .MaxTexture = 100, - .MaxLight = 100, - .MaxSampler = 100, + .MaxTransform = 3000, + .MaxView = 1000, + .MaxMaterial = 1000, + .MaxTexture = 3000, + .MaxLight = 1000, + .MaxSampler = 2, }; resourceDescriptors = CreateScope(limits); @@ -423,157 +423,16 @@ void GPUResources::RecreateBindlessCameras() i++; } - void* mappedData; - auto allocator = VulkanAllocator::Get().GetAllocator(); - vmaMapMemory(allocator, (VkRenderer::Get().GetCurrentFrame().CamerasStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, Cameras.data(), sizeof(CameraView) * Cameras.size()); - VkRenderer::Get().GetCurrentFrame().CamerasStagingBuffer->Update(); - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(CameraView) * MAX_CAMERAS; - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().CamerasStagingBuffer->GetBuffer(), CamerasBuffer->GetBuffer(), 1, ©); - CamerasBuffer->Update(); - }); - - vmaUnmapMemory(allocator, VkRenderer::Get().GetCurrentFrame().CamerasStagingBuffer->GetAllocation()); - - // Update descriptor set for camera - VkDescriptorBufferInfo transformBufferInfo{}; - transformBufferInfo.buffer = CamerasBuffer->GetBuffer(); - transformBufferInfo.offset = 0; - transformBufferInfo.range = VK_WHOLE_SIZE; - - VkWriteDescriptorSet bufferWriteModel = {}; - bufferWriteModel.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - bufferWriteModel.pNext = nullptr; - bufferWriteModel.dstBinding = 0; - bufferWriteModel.dstSet = CamerasDescriptor; - bufferWriteModel.descriptorCount = 1; - bufferWriteModel.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWriteModel.pBufferInfo = &transformBufferInfo; - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWriteModel, 0, nullptr); + const auto frameIndex = VkRenderer::Get().FrameNumber; + resourceDescriptors->UpdateBuffer(frameIndex, Cameras.data(), Cameras.size() * sizeof(CameraView)); } void GPUResources::UpdateBuffers() { - //resourceDescriptors->UpdateBuffer(VkRenderer::Get().FrameNumber, ModelTransforms.Data.data(), ModelTransforms.Data.size() * sizeof(Matrix4)); - - // Tranforms - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &ModelTransforms, sizeof(TransformData)); - - VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->Update(); - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(TransformData); - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetBuffer(), ModelBuffer->GetBuffer(), 1, ©); - - ModelBuffer->Update(); - }); - - vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetAllocation()); - - // Update descriptor set for camera - VkDescriptorBufferInfo transformBufferInfo{}; - transformBufferInfo.buffer = ModelBuffer->GetBuffer(); - transformBufferInfo.offset = 0; - transformBufferInfo.range = VK_WHOLE_SIZE; - - VkWriteDescriptorSet bufferWriteModel = {}; - bufferWriteModel.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - bufferWriteModel.pNext = nullptr; - bufferWriteModel.dstBinding = 0; - bufferWriteModel.dstSet = ModelDescriptor; - bufferWriteModel.descriptorCount = 1; - bufferWriteModel.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWriteModel.pBufferInfo = &transformBufferInfo; - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWriteModel, 0, nullptr); - } - - //resourceDescriptors->UpdateBuffer(0, MaterialDataContainer.Data.data(), MaterialDataContainer.Data.size()); - - // Update material buffer - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &MaterialDataContainer, sizeof(MaterialData)); - VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->Update(); - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(MaterialData); - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->GetBuffer(), MaterialBuffer->GetBuffer(), 1, ©); - - MaterialBuffer->Update(); - }); - - vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->GetAllocation()); - - // Update descriptor set for camera - VkDescriptorBufferInfo bufferInfo{}; - bufferInfo.buffer = MaterialBuffer->GetBuffer(); - bufferInfo.offset = 0; - bufferInfo.range = VK_WHOLE_SIZE; - - VkWriteDescriptorSet bufferWrite = {}; - bufferWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - bufferWrite.pNext = nullptr; - bufferWrite.dstBinding = 0; - bufferWrite.dstSet = MaterialDescriptor; - bufferWrite.descriptorCount = 1; - bufferWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWrite.pBufferInfo = &bufferInfo; - bufferWrite.pImageInfo = VK_NULL_HANDLE; - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWrite, 0, nullptr); - } - - //resourceDescriptors->UpdateBuffer(0, LightDataContainerArray.Data.data(), LightDataContainerArray.Data.size()); - - // Lights - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &LightDataContainerArray, sizeof(LightDataContainer)); - VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->Update(); - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(LightDataContainer); - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->GetBuffer(), LightBuffer->GetBuffer(), 1, ©); - LightBuffer->Update(); - }); - - vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->GetAllocation()); - - // Update descriptor set for camera - VkDescriptorBufferInfo bufferInfo{}; - bufferInfo.buffer = LightBuffer->GetBuffer(); - bufferInfo.offset = 0; - bufferInfo.range = VK_WHOLE_SIZE; - - VkWriteDescriptorSet bufferWrite = {}; - bufferWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - bufferWrite.pNext = nullptr; - bufferWrite.dstBinding = 0; - bufferWrite.dstSet = LightsDescriptor; - bufferWrite.descriptorCount = 1; - bufferWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWrite.pBufferInfo = &bufferInfo; - bufferWrite.pImageInfo = VK_NULL_HANDLE; - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWrite, 0, nullptr); - } + auto frameIndex = VkRenderer::Get().FrameNumber; + resourceDescriptors->UpdateBuffer(frameIndex, ModelTransforms.Data.data(), ModelTransforms.Data.size() * sizeof(Matrix4)); + resourceDescriptors->UpdateBuffer(frameIndex, MaterialDataContainer.Data.data(), MaterialDataContainer.Data.size() * sizeof(MaterialBufferStruct)); + resourceDescriptors->UpdateBuffer(frameIndex, LightDataContainerArray.Data.data(), LightDataContainerArray.Data.size() * sizeof(LightData)); } std::vector GPUResources::GetBindlessLayout()