Fixed validation errors

This commit is contained in:
antopilo
2025-05-04 12:11:37 -04:00
parent f88c76445f
commit f57393d354
9 changed files with 98 additions and 48 deletions

View File

@@ -15,6 +15,7 @@ std::string GetResourceTypeName(ResourceType type)
{
switch (type)
{
case ResourceType::Transform: return "Transform";
case ResourceType::View: return "View";
case ResourceType::Material: return "Material";
case ResourceType::Texture: return "Texture";
@@ -58,8 +59,8 @@ int32_t Descriptor::GetResourceSlot(const UUID& id) const
return 0; // Resource not found
}
Descriptor::Descriptor(Ref<AllocatedBuffer> buffer, VkDescriptorSetLayout layout, uint8_t* ptr, size_t offset, size_t size, BindlessInfo& info)
: DataPtr(ptr), Offset(offset), Size(size), Info(info)
Descriptor::Descriptor(Ref<AllocatedBuffer> buffer, VkDescriptorSetLayout layout, uint8_t* ptr, size_t offset, size_t size, BindlessInfo& info, uint32_t bindingSlot)
: DataPtr(ptr), Offset(offset), Size(size), Info(info), BindingSlot(bindingSlot)
{
auto& vk = VkRenderer::Get();
auto& allocator = VkRenderer::Get().GetDescriptorAllocator();
@@ -84,24 +85,32 @@ Descriptor::Descriptor(Ref<AllocatedBuffer> buffer, VkDescriptorSetLayout layout
vkUpdateDescriptorSets(vk.GetDevice(), 1, &write, 0, nullptr);
}
void Descriptor::Bind(VkCommandBuffer cmd, VkPipelineLayout layout)
{
Cmd command(cmd);
command.BindDescriptorSet(layout, DescriptorSet, BindingSlot);
}
BindlessDescriptor::BindlessDescriptor(ResourceType type, BindlessInfo& info) :
Info(info),
Type(type)
{
// Create a buffer that holds for N frame in flights of data
const std::string& resourceName = GetResourceTypeName(type);
// Build descriptor layout
DescriptorLayoutBuilder builder;
switch (type)
{
case ResourceType::View:
case ResourceType::Material:
case ResourceType::Light:
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
break;
case ResourceType::Texture:
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
break;
case ResourceType::Sampler:
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLER);
break;
default:
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
break;
}
DescriptorLayout = builder.Build(VkRenderer::Get().GetDevice(), VK_SHADER_STAGE_ALL_GRAPHICS);
@@ -114,6 +123,7 @@ BindlessDescriptor::BindlessDescriptor(ResourceType type, BindlessInfo& info) :
const MemoryUsage memoryUsage = MemoryUsage::CPU_TO_GPU;
const size_t size = info.ResourceElementSize[type] * info.ResourceCount[type];
const size_t totalSize = size * FRAME_OVERLAP;
Buffer = CreateRef<AllocatedBuffer>(resourceName + "GPUBuffer", totalSize, usage, memoryUsage);
// Map to a host-visible pointer
@@ -123,7 +133,7 @@ BindlessDescriptor::BindlessDescriptor(ResourceType type, BindlessInfo& info) :
{
const size_t offset = i * size;
uint8_t* partitionStart = static_cast<uint8_t*>(mappedData) + offset;
Descriptors.emplace_back(Buffer, DescriptorLayout, partitionStart, offset, size, info);
Descriptors.emplace_back(Buffer, DescriptorLayout, partitionStart, offset, size, info, Info.ResourceBindingSlot[Type]);
}
}
@@ -149,13 +159,22 @@ void BindlessDescriptor::Swap(int32_t frameIndex)
memcpy(desc.DataPtr, nextDesc.DataPtr, desc.Size);
}
void BindlessDescriptor::Bind(VkCommandBuffer cmd, int32_t frameIndex, VkPipelineLayout layout)
{
int currentFrame = frameIndex % FRAME_OVERLAP;
auto& desc = Descriptors[currentFrame];
desc.Bind(cmd, layout);
}
ResourceDescriptors::ResourceDescriptors(const ResourceDescriptorsLimits& limits)
{
AddResourceDescriptors<ResourceType::View, CameraView>(limits.MaxView);
AddResourceDescriptors<ResourceType::Material, MaterialBufferStruct>(limits.MaxMaterial);
AddResourceDescriptors<ResourceType::Texture, VkDescriptorImageInfo>(limits.MaxTexture);
AddResourceDescriptors<ResourceType::Light, LightData>(limits.MaxLight);
AddResourceDescriptors<ResourceType::Sampler, VkDescriptorImageInfo>(limits.MaxSampler);
AddResourceDescriptors<ResourceType::Transform, Matrix4>(limits.MaxTransform, 0);
AddResourceDescriptors<ResourceType::Sampler, VkDescriptorImageInfo>(limits.MaxSampler, 2);
AddResourceDescriptors<ResourceType::Material, MaterialBufferStruct>(limits.MaxMaterial, 3);
AddResourceDescriptors<ResourceType::Texture, VkDescriptorImageInfo>(limits.MaxTexture, 4);
AddResourceDescriptors<ResourceType::Light, LightData>(limits.MaxLight, 4);
AddResourceDescriptors<ResourceType::View, CameraView>(limits.MaxView, 6);
}
void ResourceDescriptors::Swap(int32_t frameIndex)
@@ -164,4 +183,16 @@ void ResourceDescriptors::Swap(int32_t frameIndex)
{
desc.Swap(frameIndex);
}
}
}
void ResourceDescriptors::Bind(VkCommandBuffer cmd, int32_t frame, VkPipelineLayout layout)
{
/*
for (auto& [type, desc] : Descriptors)
{
desc.Bind(cmd, frame, layout);
}
*/
Descriptors[ResourceType::Transform].Bind(cmd, frame, layout);
}

View File

@@ -14,6 +14,7 @@ namespace Nuake
{
View,
Material,
Transform,
Texture,
Light,
Sampler
@@ -24,6 +25,7 @@ namespace Nuake
std::map<ResourceType, size_t> ResourceElementSize;
std::map<ResourceType, size_t> ResourceCount;
std::map<ResourceType, AllocatedBuffer> ResourceBuffers;
std::map<ResourceType, uint32_t> ResourceBindingSlot;
};
class DescriptorSlot
@@ -41,6 +43,7 @@ namespace Nuake
uint8_t* DataPtr;
size_t Size;
size_t Offset;
uint32_t BindingSlot;
private:
BindlessInfo& Info;
@@ -49,15 +52,15 @@ namespace Nuake
std::map<UUID, int> SlotMapping;
std::vector<DescriptorSlot> Slots;
public:
Descriptor(Ref<AllocatedBuffer> buffer, VkDescriptorSetLayout layout, uint8_t* ptr, size_t offset, size_t size, BindlessInfo& info);
Descriptor(Ref<AllocatedBuffer> buffer, VkDescriptorSetLayout layout, uint8_t* ptr, size_t offset, size_t size, BindlessInfo& info, uint32_t bindingSlot);
Descriptor() = default;
~Descriptor() = default;
int32_t LoadResource(const UUID& id);
int32_t GetResourceSlot(const UUID& id) const;
void Bind(VkCommandBuffer cmd, VkPipelineLayout layout);
};
// Contains buffer for N frames of a resource type
@@ -85,10 +88,12 @@ namespace Nuake
void WriteToBuffer(int32_t frameIndex, void* data, size_t size);
void Swap(int32_t frameIndex);
void Bind(VkCommandBuffer cmd, int32_t frameIndex, VkPipelineLayout layout);
};
struct ResourceDescriptorsLimits
{
size_t MaxTransform;
size_t MaxView;
size_t MaxMaterial;
size_t MaxTexture;
@@ -122,11 +127,14 @@ namespace Nuake
}
template<ResourceType T, typename S>
void AddResourceDescriptors(const size_t size)
void AddResourceDescriptors(const size_t size, const uint32_t bindingSlot)
{
Info.ResourceElementSize[T] = sizeof(S);
Info.ResourceCount[T] = size;
Info.ResourceBindingSlot[T] = bindingSlot;
Descriptors[T] = BindlessDescriptor(T, Info);
}
void Bind(VkCommandBuffer cmd, int32_t frame, VkPipelineLayout layout);
};
}

View File

@@ -1,5 +1,6 @@
#include "Cmd.h"
#include "VulkanAllocatedBuffer.h"
#include "VkResources.h"
using namespace Nuake;
@@ -18,6 +19,12 @@ void Cmd::BindPipeline(VkPipeline pipeline) const
vkCmdBindPipeline(CmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
}
void Cmd::BindSceneData(VkPipelineLayout pipelineLayout)
{
auto& res = GPUResources::Get();
res.BindSceneData(CmdBuffer, pipelineLayout);
}
void Cmd::SetViewport(const Vector2 & size) const
{
VkViewport viewport = {};

View File

@@ -57,6 +57,7 @@ namespace Nuake
void DebugMarker(const std::string& name, Color color = Color(1, 0, 0, 1));
void BindPipeline(VkPipeline pipeline) const;
void BindSceneData(VkPipelineLayout pipelineLayout);
void SetViewport(const Vector2& size) const;
void SetScissor(const Vector2& size) const;
void ClearColorImage(Ref<VulkanImage> img, Color color = Color(0, 0, 0, 1)) const;

View File

@@ -36,9 +36,10 @@ ShadowRenderPipeline::ShadowRenderPipeline()
shadowPass.SetPreRender([&](PassRenderContext& ctx) {
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.ModelDescriptor, 0);
cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2);
cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3);
cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4);

View File

@@ -48,8 +48,8 @@ namespace Nuake
VkDescriptorSet TestDescriptorSet;
};
private:
Scope<ResourceDescriptors> resourceDescriptors;
private:
FrameData frameData[FRAME_OVERLAP];
@@ -154,6 +154,8 @@ namespace Nuake
void CleanUp(uint32_t frame);
void Swap(uint32_t frame);
void BindSceneData(VkCommandBuffer cmd, VkPipelineLayout pipeline);
private:
void CreateBindlessLayout();
CleanUpStack& GetFrameCleanUpStack(uint32_t frame);

View File

@@ -881,7 +881,7 @@ void VkRenderer::EndDraw()
// Increase the number of frames drawn
FrameNumber++;
GPUResources::Get().Swap(FrameNumber);
//GPUResources::Get().Swap(FrameNumber);
}
void VkRenderer::DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView)

View File

@@ -46,16 +46,15 @@ void GPUResources::Init()
ResourceDescriptorsLimits limits
{
.MaxView = 1000,
.MaxMaterial = 1000,
.MaxTexture = 1000,
.MaxLight = 1000,
.MaxSampler = 1000,
.MaxTransform = 100,
.MaxView = 100,
.MaxMaterial = 100,
.MaxTexture = 100,
.MaxLight = 100,
.MaxSampler = 100,
};
resourceDescriptors = CreateScope<ResourceDescriptors>(limits);
//resourceDescriptors->UpdateBuffer<ResourceType::View>(0, testDataVector.data(), testDataVector.size());
//
//testDataVector[0].myView = 1337;
@@ -322,16 +321,6 @@ void GPUResources::CreateBindlessLayout()
VulkanUtil::SetDebugName(CamerasDescriptorLayout, "CamerasDescriptorLayout");
}
ResourceDescriptorsLimits limits
{
.MaxView = 10,
.MaxMaterial = 1,
.MaxTexture = 20,
.MaxLight = 10,
.MaxSampler = 2,
};
ResourceDescriptors descriptors = ResourceDescriptors(limits);
auto allocator = vk.GetDescriptorAllocator();
TexturesDescriptor = allocator.Allocate(device, TexturesDescriptorLayout);
CamerasDescriptor = allocator.Allocate(device, CamerasDescriptorLayout);
@@ -390,7 +379,7 @@ void GPUResources::CreateBindlessLayout()
void GPUResources::RecreateBindlessTextures()
{
// Ideally wed have update bit enabled ondescriptors
//vkQueueWaitIdle(VkRenderer::Get().GPUQueue);
vkQueueWaitIdle(VkRenderer::Get().GPUQueue);
if (!TexturesDescriptor)
{
@@ -470,32 +459,34 @@ void GPUResources::RecreateBindlessCameras()
void GPUResources::UpdateBuffers()
{
//resourceDescriptors->UpdateBuffer<ResourceType::Tranform>(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(ModelData));
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(ModelData);
copy.size = sizeof(TransformData);
vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetBuffer(), ModelBuffer->GetBuffer(), 1, &copy);
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;
@@ -507,6 +498,8 @@ void GPUResources::UpdateBuffers()
vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWriteModel, 0, nullptr);
}
//resourceDescriptors->UpdateBuffer<ResourceType::Material>(0, MaterialDataContainer.Data.data(), MaterialDataContainer.Data.size());
// Update material buffer
{
void* mappedData;
@@ -544,6 +537,8 @@ void GPUResources::UpdateBuffers()
vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWrite, 0, nullptr);
}
//resourceDescriptors->UpdateBuffer<ResourceType::Light>(0, LightDataContainerArray.Data.data(), LightDataContainerArray.Data.size());
// Lights
{
void* mappedData;
@@ -680,6 +675,11 @@ void GPUResources::Swap(uint32_t frame)
resourceDescriptors->Swap(frame);
}
void GPUResources::BindSceneData(VkCommandBuffer cmd, VkPipelineLayout pipeline)
{
resourceDescriptors->Bind(cmd, VkRenderer::Get().FrameNumber, pipeline);
}
CleanUpStack& GPUResources::GetFrameCleanUpStack(uint32_t frame)
{
return DeletionQueue[frame % FRAME_OVERLAP];

View File

@@ -640,7 +640,7 @@ void VkSceneRenderer::PrepareScenes(const std::vector<Ref<Scene>>& scenes, Rende
gpu.RecreateBindlessCameras();
}
gpu.ModelTransforms = ModelData{ allTransforms };
gpu.ModelTransforms = TransformData{ allTransforms };
gpu.MaterialDataContainer = MaterialData{ allMaterials };
gpu.LightDataContainerArray = LightDataContainer{ allLights };
gpu.LightCount = lightCount;