From 28e902b1768fb1d070d08ad8fbc6f08482903625 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sun, 12 Jan 2025 20:24:31 -0500 Subject: [PATCH] moved all descriptor to GPU resource + fixed all validation errors --- Editor/src/Windows/EditorInterface.cpp | 4 +- Nuake/src/Rendering/Vulkan/Cmd.cpp | 8 +- .../Vulkan/Pipeline/RenderPipeline.cpp | 63 +- .../Rendering/Vulkan/SceneRenderPipeline.cpp | 63 +- .../Rendering/Vulkan/SceneRenderPipeline.h | 23 + Nuake/src/Rendering/Vulkan/VkResources.h | 99 ++- Nuake/src/Rendering/Vulkan/VulkanRenderer.h | 8 +- .../src/Rendering/Vulkan/VulkanResources.cpp | 208 +++++- .../Rendering/Vulkan/VulkanSceneRenderer.cpp | 628 ++++++------------ .../Rendering/Vulkan/VulkanSceneRenderer.h | 105 +-- Resources/Shaders/Vulkan/shading.frag | 118 +--- Resources/Shaders/Vulkan/shading.vert | 53 +- Resources/Shaders/Vulkan/shadow.frag | 45 +- Resources/Shaders/Vulkan/shadow.vert | 66 +- Resources/Shaders/Vulkan/triangle.frag | 47 +- Resources/Shaders/Vulkan/triangle.vert | 67 +- 16 files changed, 779 insertions(+), 826 deletions(-) diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index ea5f8ca9..2951627f 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -625,13 +625,13 @@ namespace Nuake { framebuffer->QueueResize(viewportPanelSize * Engine::GetProject()->Settings.ResolutionScale); Ref texture = framebuffer->GetTexture(); - auto& pipeline = VkRenderer::Get().GetRenderPipeline(); + //auto& pipeline = VkRenderer::Get().GetRenderPipeline(); VkDescriptorSet textureDesc = VkRenderer::Get().DrawImage->GetImGuiDescriptorSet(); if (SelectedViewport == 1) { //pipeline.GetRenderPass("Shadow").GetDepthAttachment().Image->TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - textureDesc = pipeline.GetRenderPass("Shadow").GetDepthAttachment().Image->GetImGuiDescriptorSet(); + //textureDesc = pipeline.GetRenderPass("Shadow").GetDepthAttachment().Image->GetImGuiDescriptorSet(); texture = Engine::GetCurrentScene()->m_SceneRenderer->GetGBuffer().GetTexture(GL_COLOR_ATTACHMENT0); } else if (SelectedViewport == 2) diff --git a/Nuake/src/Rendering/Vulkan/Cmd.cpp b/Nuake/src/Rendering/Vulkan/Cmd.cpp index f7d80c9c..f29d5bc5 100644 --- a/Nuake/src/Rendering/Vulkan/Cmd.cpp +++ b/Nuake/src/Rendering/Vulkan/Cmd.cpp @@ -57,11 +57,11 @@ void Cmd::BindDescriptorSet(VkPipelineLayout pipeline, VkDescriptorSet descripto CmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline, - 0, // firstSet - set, // descriptorSetCount + set, // firstSet + 1, // descriptorSetCount &descriptor, // pointer to the descriptor set(s) - 0, // dynamicOffsetCount - nullptr // dynamicOffsets + 0, // dynamicOffsetCount + nullptr // dynamicOffsets ); } diff --git a/Nuake/src/Rendering/Vulkan/Pipeline/RenderPipeline.cpp b/Nuake/src/Rendering/Vulkan/Pipeline/RenderPipeline.cpp index aef3b2ea..e7665ece 100644 --- a/Nuake/src/Rendering/Vulkan/Pipeline/RenderPipeline.cpp +++ b/Nuake/src/Rendering/Vulkan/Pipeline/RenderPipeline.cpp @@ -284,38 +284,38 @@ bool RenderPipeline::Build() { pass.Build(); - for (auto& input : pass.GetInputs()) - { - bool alreadyFoundAttachment = false; - if (attachments.find(input) == attachments.end()) - { - if (pass.GetDepthAttachment().Image && pass.GetDepthAttachment().Name == input) - { - pass.SetInput(input, pass.GetDepthAttachment()); - alreadyFoundAttachment = true; - } - else - { - Logger::Log("Failed to build RenderPipeline. input " + input + " not found in previous passes.", "vulkan", CRITICAL); - return false; - } - } + //for (auto& input : pass.GetInputs()) + //{ + // bool alreadyFoundAttachment = false; + // if (attachments.find(input) == attachments.end()) + // { + // if (pass.GetDepthAttachment().Image && pass.GetDepthAttachment().Name == input) + // { + // pass.SetInput(input, pass.GetDepthAttachment()); + // alreadyFoundAttachment = true; + // } + // else + // { + // Logger::Log("Failed to build RenderPipeline. input " + input + " not found in previous passes.", "vulkan", CRITICAL); + // return false; + // } + // } + // + // if (!alreadyFoundAttachment) + // { + // pass.SetInput(input, attachments[input]); + // } + //} - if (!alreadyFoundAttachment) - { - pass.SetInput(input, attachments[input]); - } - } - - for (auto& attachment : pass.GetAttachments()) - { - attachments[attachment.Name] = attachment; - } - - if (pass.GetDepthAttachment().Image) - { - attachments[pass.GetDepthAttachment().Name] = pass.GetDepthAttachment(); - } + //for (auto& attachment : pass.GetAttachments()) + //{ + // attachments[attachment.Name] = attachment; + //} + // + //if (pass.GetDepthAttachment().Image) + //{ + // attachments[pass.GetDepthAttachment().Name] = pass.GetDepthAttachment(); + //} } for (auto& pass : RenderPasses) @@ -323,6 +323,7 @@ bool RenderPipeline::Build() } Built = true; + return true; } void RenderPipeline::Execute(PassRenderContext& ctx, PipelineAttachments& inputs) diff --git a/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.cpp b/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.cpp index 9334444b..f7d24663 100644 --- a/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.cpp +++ b/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.cpp @@ -8,43 +8,52 @@ using namespace Nuake; RenderPipeline SceneRenderPipeline::GBufferPipeline; + + SceneRenderPipeline::SceneRenderPipeline() { + // Initialize render targets + const Vector2 defaultSize = { 1280, 720 }; + GBufferAlbedo = std::make_shared(ImageFormat::RGBA16F, defaultSize); + GBufferDepth = std::make_shared(ImageFormat::D32F, defaultSize, ImageUsage::Depth); + GBufferNormal = std::make_shared(ImageFormat::RGBA16F, defaultSize); + GBufferMaterial = std::make_shared(ImageFormat::RGBA8, defaultSize); + ShadingOutput = std::make_shared(ImageFormat::RGBA8, defaultSize); + + // Initialize pipeline VkShaderManager& shaderMgr = VkShaderManager::Get(); GBufferPipeline = RenderPipeline(); - auto& gBufferPass = GBufferPipeline.AddPass("GBuffer"); gBufferPass.SetShaders(shaderMgr.GetShader("basic_vert"), shaderMgr.GetShader("basic_frag")); gBufferPass.AddAttachment("Albedo", ImageFormat::RGBA8); gBufferPass.AddAttachment("Normal", ImageFormat::RGBA8); gBufferPass.AddAttachment("Material", ImageFormat::RGBA8); gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth); + gBufferPass.SetPushConstant(gbufferConstant); gBufferPass.SetPreRender([&](PassRenderContext& ctx) { - auto layout = ctx.renderPass->PipelineLayout; - auto vk = VkRenderer::Get(); - ctx.commandBuffer.BindDescriptorSet(layout, vk.CameraBufferDescriptors, 0); - //ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1); - //ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3); - //ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); - //ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5); - //ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6); - //ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); + auto& layout = ctx.renderPass->PipelineLayout; + auto& gpu = GPUResources::Get(); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.ModelDescriptor, 0); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.SamplerDescriptor, 2); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.MaterialDescriptor, 3); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.TexturesDescriptor, 4); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.LightsDescriptor, 5); + ctx.commandBuffer.BindDescriptorSet(layout, gpu.CamerasDescriptor, 6); }); gBufferPass.SetRender([&](PassRenderContext& ctx) { }); - //gBufferPass.SetPushConstant(modelPushConstant); - auto& shadingPass = GBufferPipeline.AddPass("Shading"); - shadingPass.SetShaders(shaderMgr.GetShader("shading_vert"), shaderMgr.GetShader("shading_frag")); - //shadingPass.SetPushConstant(ShadingPushConstant())); - shadingPass.AddAttachment("Output", ImageFormat::RGBA8); - shadingPass.SetDepthTest(false); - shadingPass.AddInput("Albedo"); - shadingPass.AddInput("Normal"); - shadingPass.AddInput("Depth"); - shadingPass.AddInput("Material"); + //auto& shadingPass = GBufferPipeline.AddPass("Shading"); + //shadingPass.SetShaders(shaderMgr.GetShader("shading_vert"), shaderMgr.GetShader("shading_frag")); + //shadingPass.SetPushConstant(shadingConstant); + //shadingPass.AddAttachment("Output", ImageFormat::RGBA8); + //shadingPass.SetDepthTest(false); + //shadingPass.AddInput("Albedo"); + //shadingPass.AddInput("Normal"); + //shadingPass.AddInput("Depth"); + //shadingPass.AddInput("Material"); GBufferPipeline.Build(); } @@ -57,15 +66,15 @@ void SceneRenderPipeline::SetCamera(UUID camera) void SceneRenderPipeline::Render(PassRenderContext& ctx) { // Resize textures - ResizeImage(GBufferAlbedo, ctx.resolution); - ResizeImage(GBufferDepth, ctx.resolution); - ResizeImage(GBufferNormal, ctx.resolution); - ResizeImage(GBufferMaterial, ctx.resolution); - ResizeImage(ShadingOutput, ctx.resolution); + GBufferAlbedo = ResizeImage(GBufferAlbedo, ctx.resolution); + GBufferDepth = ResizeImage(GBufferDepth, ctx.resolution); + GBufferNormal = ResizeImage(GBufferNormal, ctx.resolution); + GBufferMaterial = ResizeImage(GBufferMaterial, ctx.resolution); + ShadingOutput = ResizeImage(ShadingOutput, ctx.resolution); PipelineAttachments pipelineInputs { - { GBufferMaterial, GBufferDepth, GBufferNormal, GBufferMaterial }, // GBuffer + { GBufferAlbedo, GBufferDepth, GBufferNormal, GBufferMaterial }, // GBuffer { ShadingOutput } // Shading // ... other passes }; @@ -81,7 +90,7 @@ Ref SceneRenderPipeline::ResizeImage(Ref image, const return image; } - Ref newAttachment = std::make_shared(image->GetFormat(), size); + Ref newAttachment = std::make_shared(image->GetFormat(), size, image->GetUsage()); // Register to resource manager GPUResources& gpuResources = GPUResources::Get(); diff --git a/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.h b/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.h index 6752de7d..d5fccf8b 100644 --- a/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.h +++ b/Nuake/src/Rendering/Vulkan/SceneRenderPipeline.h @@ -3,6 +3,7 @@ #include "src/Resource/UUID.h" #include "src/Rendering/Vulkan/Pipeline/RenderPipeline.h" +#include "src/Rendering/Vulkan/VkResources.h" namespace Nuake { @@ -23,6 +24,25 @@ namespace Nuake Ref GetOutput() { return DepthTexture; } }; + struct GBufferConstant + { + int Index; + int MaterialIndex; + int CameraID; + char padding[120]; // 124 bytes to reach 128 bytes + }; + + struct ShadingConstant + { + int AlbedoTextureID; + int DepthTextureID; + int NormalTextureID; + int MaterialTextureID; + int LightCount; + int CameraID; + float CascadeSplits[4]; + }; + // This class handles all the rendering of the scene class SceneRenderPipeline { @@ -38,6 +58,9 @@ namespace Nuake // Attachments Shading Ref ShadingOutput; + GBufferConstant gbufferConstant; + ShadingConstant shadingConstant; + // Holds all the texture attachments static RenderPipeline GBufferPipeline; diff --git a/Nuake/src/Rendering/Vulkan/VkResources.h b/Nuake/src/Rendering/Vulkan/VkResources.h index 7bbead47..dd4af59e 100644 --- a/Nuake/src/Rendering/Vulkan/VkResources.h +++ b/Nuake/src/Rendering/Vulkan/VkResources.h @@ -14,10 +14,64 @@ #include +#include #include namespace Nuake { + struct CameraData; + + + + struct ModelData + { + std::array Data; + }; + + // This is what is present on the shader as a structured buffer + struct MaterialBufferStruct + { + float hasAlbedo; + Vector3 albedo; + int hasNormal; + int hasMetalness; + int hasRoughness; + int hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; + }; + + // This is the *whole* buffer + struct MaterialData + { + std::array Data; + }; + + struct LightData + { + Vector3 position; + int type; + Vector4 color; + Vector3 direction; + float outerConeAngle; + float innerConeAngle; + bool castShadow; + int shadowMapTextureId[4]; + int transformId[4]; + float pad[2]; + }; + + struct LightDataContainer + { + std::array Data; + }; + struct CameraView { Matrix4 View; @@ -39,6 +93,12 @@ namespace Nuake }; + constexpr uint32_t MAX_MODEL_MATRIX = 3000; + constexpr uint32_t MAX_MATERIAL = 1000; + constexpr uint32_t MAX_TEXTURES = 500; + constexpr uint32_t MAX_CAMERAS = 100; + constexpr uint32_t MAX_LIGHTS = 100; + class GPUResources { private: @@ -50,30 +110,44 @@ namespace Nuake std::vector Cameras; std::array CamerasData; + // Bindless buffer layouts - VkDescriptorSetLayout CameraDescriptorLayout; + VkDescriptorSetLayout ModelDescriptorLayout; VkDescriptorSetLayout TriangleBufferDescriptorLayout; - VkDescriptorSetLayout ModelBufferDescriptorLayout; - VkDescriptorSetLayout ImageDescriptorLayout; VkDescriptorSetLayout SamplerDescriptorLayout; VkDescriptorSetLayout MaterialDescriptorLayout; VkDescriptorSetLayout TexturesDescriptorLayout; VkDescriptorSetLayout LightsDescriptorLayout; VkDescriptorSetLayout CamerasDescriptorLayout; - VkDescriptorSet CameraDescriptor; - VkDescriptorSet ModelDescriptor; - VkDescriptorSet SamplerDescriptor; - VkDescriptorSet MaterialDescriptor; - VkDescriptorSet LightsDescriptor; - std::map BindlessTextureMapping; std::map CameraMapping; Ref CamerasBuffer; + Ref ModelBuffer; + Ref MaterialBuffer; + Ref LightBuffer; + + // Samplers + VkSampler SamplerLinear; + VkSampler SamplerNearest; public: - VkDescriptorSet TextureDescriptor; + ModelData ModelTransforms; + MaterialData MaterialDataContainer; + + LightDataContainer LightDataContainerArray; + + public: + std::map ModelMatrixMapping; // Holds mapping between model entity and transform index + std::map MeshMaterialMapping; // Holds mapping between mesh and material index + + VkDescriptorSet ModelDescriptor; + VkDescriptorSet TriangleBufferDescriptor; + VkDescriptorSet SamplerDescriptor; + VkDescriptorSet MaterialDescriptor; + VkDescriptorSet TexturesDescriptor; + VkDescriptorSet LightsDescriptor; VkDescriptorSet CamerasDescriptor; static GPUResources& Get() @@ -107,12 +181,15 @@ namespace Nuake void ClearCameras(); std::vector GetBindlessLayout(); - + std::vector GetBindlessDescriptorSets(); uint32_t GetBindlessTextureID(const UUID& id); uint32_t GetBindlessCameraID(const UUID& id); + uint32_t GetBindlessTransformID(const UUID& id); + uint32_t GetBindlessMaterialID(const UUID& id); void RecreateBindlessTextures(); void RecreateBindlessCameras(); + void UpdateBuffers(); private: void CreateBindlessLayout(); }; diff --git a/Nuake/src/Rendering/Vulkan/VulkanRenderer.h b/Nuake/src/Rendering/Vulkan/VulkanRenderer.h index d12b9557..64cc664a 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanRenderer.h +++ b/Nuake/src/Rendering/Vulkan/VulkanRenderer.h @@ -129,11 +129,7 @@ namespace Nuake // Renderer configuration constexpr uint32_t FRAME_OVERLAP = 2; - constexpr uint32_t MAX_MODEL_MATRIX = 3000; - constexpr uint32_t MAX_MATERIAL = 1000; - constexpr uint32_t MAX_TEXTURES = 500; - constexpr uint32_t MAX_CAMERAS = 100; - constexpr uint32_t MAX_LIGHTS = 100; + class VkRenderer { private: @@ -264,7 +260,7 @@ namespace Nuake void ImmediateSubmit(std::function&& function); void UploadCameraData(const CameraData& data); - auto& GetRenderPipeline() { return this->SceneRenderer->GetRenderPipeline(); } + //auto& GetRenderPipeline() { return this->SceneRenderer->GetRenderPipeline(); } VkDescriptorSet GetViewportDescriptor() const { return DrawImageDescriptors; } Ref GetDrawImage() const { return DrawImage; } }; diff --git a/Nuake/src/Rendering/Vulkan/VulkanResources.cpp b/Nuake/src/Rendering/Vulkan/VulkanResources.cpp index 3e5bc11c..42f1c94c 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanResources.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanResources.cpp @@ -13,6 +13,9 @@ void GPUResources::Init() { CreateBindlessLayout(); CamerasBuffer = CreateBuffer(sizeof(MaterialBufferStruct) * MAX_MATERIAL, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "CamerasBuffer"); + ModelBuffer = CreateBuffer(sizeof(Matrix4) * MAX_MODEL_MATRIX, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "TransformBuffer"); + MaterialBuffer = CreateBuffer(sizeof(MaterialBufferStruct) * MAX_MATERIAL, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "MaterialBuffer"); + LightBuffer = CreateBuffer(sizeof(LightData) * MAX_LIGHTS, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "LightBuffer"); } Ref GPUResources::CreateBuffer(size_t size, BufferUsage flags, MemoryUsage usage, const std::string& name) @@ -154,45 +157,32 @@ void GPUResources::CreateBindlessLayout() auto& vk = VkRenderer::Get(); auto device = vk.GetDevice(); - // Camera + // Matrices { DescriptorLayoutBuilder builder; builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - CameraDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); + ModelDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); } + // Triangles { - // Triangle vertex buffer layout DescriptorLayoutBuilder builder; builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - TriangleBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_VERTEX_BIT); - } - - { - // Matrices - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - ModelBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_VERTEX_BIT); - } - - // Textures - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); - ImageDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); + TriangleBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); } + // Samplers { DescriptorLayoutBuilder builder; builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLER); - SamplerDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); + SamplerDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); } // Material { DescriptorLayoutBuilder builder; builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - MaterialDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); + MaterialDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); } // Textures @@ -217,16 +207,45 @@ void GPUResources::CreateBindlessLayout() } auto allocator = vk.GetDescriptorAllocator(); - TextureDescriptor = allocator.Allocate(device, TexturesDescriptorLayout); + TexturesDescriptor = allocator.Allocate(device, TexturesDescriptorLayout); CamerasDescriptor = allocator.Allocate(device, CamerasDescriptorLayout); + TriangleBufferDescriptor = allocator.Allocate(device, TriangleBufferDescriptorLayout); + SamplerDescriptor = allocator.Allocate(device, SamplerDescriptorLayout); + ModelDescriptor = allocator.Allocate(device, ModelDescriptorLayout); + LightsDescriptor = allocator.Allocate(device, LightsDescriptorLayout); + MaterialDescriptor = allocator.Allocate(device, MaterialDescriptorLayout); + CamerasDescriptor = allocator.Allocate(device, CamerasDescriptorLayout); + + // Samplers + VkSamplerCreateInfo sampler = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO }; + sampler.magFilter = VK_FILTER_NEAREST; + sampler.minFilter = VK_FILTER_NEAREST; + vkCreateSampler(device, &sampler, nullptr, &SamplerNearest); + + sampler.magFilter = VK_FILTER_LINEAR; + sampler.minFilter = VK_FILTER_LINEAR; + vkCreateSampler(device, &sampler, nullptr, &SamplerLinear); + + VkDescriptorImageInfo textureInfo = {}; + textureInfo.sampler = SamplerNearest; // Your VkSampler object + textureInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkWriteDescriptorSet samplerWrite = {}; + samplerWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + samplerWrite.dstBinding = 0; // Binding for sampler (in shader) + samplerWrite.dstSet = SamplerDescriptor; // The allocated descriptor set for the sampler + samplerWrite.descriptorCount = 1; + samplerWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; + samplerWrite.pImageInfo = &textureInfo; // Sampler info (same as texture) + + vkUpdateDescriptorSets(device, 1, &samplerWrite, 0, nullptr); } void GPUResources::RecreateBindlessTextures() { // Ideally wed have update bit enabled ondescriptors - vkQueueWaitIdle(VkRenderer::Get().GPUQueue); - if (!TextureDescriptor) + if (!TexturesDescriptor) { CreateBindlessLayout(); } @@ -242,7 +261,7 @@ void GPUResources::RecreateBindlessTextures() VkWriteDescriptorSet write{}; write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write.dstSet = TextureDescriptor; + write.dstSet = TexturesDescriptor; write.dstBinding = 0; // Binding 0 write.dstArrayElement = 0; write.descriptorCount = static_cast(imageInfos.size()); @@ -299,11 +318,115 @@ void GPUResources::RecreateBindlessCameras() vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWriteModel, 0, nullptr); } +void GPUResources::UpdateBuffers() +{ + { + void* mappedData; + vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetAllocation()), &mappedData); + memcpy(mappedData, &ModelTransforms, sizeof(ModelData)); + + VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { + VkBufferCopy copy{ 0 }; + copy.dstOffset = 0; + copy.srcOffset = 0; + copy.size = sizeof(ModelData); + + vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetBuffer(), ModelBuffer->GetBuffer(), 1, ©); + }); + + 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); + } + + // Update material buffer + { + void* mappedData; + vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->GetAllocation()), &mappedData); + memcpy(mappedData, &MaterialDataContainer, sizeof(MaterialData)); + + 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, ©); + }); + + 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); + } + + { + void* mappedData; + vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->GetAllocation()), &mappedData); + memcpy(mappedData, &LightDataContainerArray, sizeof(LightDataContainer)); + + 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, ©); + }); + + 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); + } +} + std::vector GPUResources::GetBindlessLayout() { std::vector layouts = { - CameraDescriptorLayout, - ModelBufferDescriptorLayout, + ModelDescriptorLayout, TriangleBufferDescriptorLayout, SamplerDescriptorLayout, MaterialDescriptorLayout, @@ -314,6 +437,20 @@ std::vector GPUResources::GetBindlessLayout() return layouts; } +std::vector GPUResources::GetBindlessDescriptorSets() +{ + std::vector descriptors = { + ModelDescriptor, + TriangleBufferDescriptor, + SamplerDescriptor, + MaterialDescriptor, + TexturesDescriptor, + LightsDescriptor, + CamerasDescriptor + }; + return descriptors; +} + uint32_t GPUResources::GetBindlessTextureID(const UUID& id) { if (BindlessTextureMapping.find(id) == BindlessTextureMapping.end()) @@ -333,3 +470,22 @@ uint32_t GPUResources::GetBindlessCameraID(const UUID& id) return CameraMapping[id]; } + +uint32_t GPUResources::GetBindlessTransformID(const UUID& id) +{ + if (ModelMatrixMapping.find(id) == ModelMatrixMapping.end()) + { + return 0; + } + + return ModelMatrixMapping[id]; +} + +uint32_t GPUResources::GetBindlessMaterialID(const UUID& id) +{ + if (MeshMaterialMapping.find(id) == MeshMaterialMapping.end()) + { + return 0; + } + return MeshMaterialMapping[id]; +} diff --git a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp index 4154eb11..43ed3075 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp @@ -20,6 +20,7 @@ #include "src/Rendering/Vulkan/DescriptorLayoutBuilder.h" #include "src/Scene/Components/CameraComponent.h" #include "src/Rendering/Vulkan/VkShaderManager.h" +#include "src/Rendering/Vulkan/SceneRenderPipeline.h" using namespace Nuake; @@ -37,14 +38,10 @@ void VkSceneRenderer::Init() { // Here we will create the pipeline for rendering a scene LoadShaders(); - CreateBuffers(); - CreateSamplers(); CreateDescriptors(); SetGBufferSize({ 1280, 720 }); CreatePipelines(); - ModelMatrixMapping.clear(); - MeshMaterialMapping.clear(); std::vector quadVertices = { { Vector3(-1.0f, 1.0f, 1.0f), 0.0f, Vector3(0, 0, 1), 1.0f, Vector4(1, 0, 0, 0), Vector4(0, 1, 0, 0) }, @@ -172,7 +169,7 @@ void VkSceneRenderer::BeginScene(RenderContext inContext) //passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(light.m_LightViews[0].CameraID); //ShadowPipeline.Execute(passCtx); - light.LightMapID = ShadowPipeline.GetRenderPass("Shadow").GetDepthAttachment().Image->GetID(); + //light.LightMapID = ShadowPipeline.GetRenderPass("Shadow").GetDepthAttachment().Image->GetID(); //passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(light.m_LightViews[0].CameraID); for (int i = 0; i < CSM_AMOUNT; i++) @@ -185,46 +182,28 @@ void VkSceneRenderer::BeginScene(RenderContext inContext) //passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(Context.CameraID); //GBufferPipeline.Execute(passCtx); - sceneRenderPipeline.Render(passCtx); + sceneRenderPipeline->Render(passCtx); } -ModelPushConstant modelPushConstant{}; -ShadingPushConstant shadingPushConstant; + void VkSceneRenderer::EndScene() { auto& vk = VkRenderer::Get(); auto& cmd = Context.CommandBuffer; - auto& albedo = GBufferPipeline.GetRenderPass("GBuffer").GetAttachment("Albedo"); - auto& normal = GBufferPipeline.GetRenderPass("GBuffer").GetAttachment("Normal"); - auto& shading = GBufferPipeline.GetRenderPass("Shading").GetAttachment("Output"); - auto& shadow = ShadowPipeline.GetRenderPass("Shadow").GetDepthAttachment(); - auto& selectedOutput = shading; + //auto& albedo = GBufferPipeline.GetRenderPass("GBuffer").GetAttachment("Albedo"); + //auto& normal = GBufferPipeline.GetRenderPass("GBuffer").GetAttachment("Normal"); + //auto& shading = GBufferPipeline.GetRenderPass("Shading").GetAttachment("Output"); + //auto& shadow = ShadowPipeline.GetRenderPass("Shadow").GetDepthAttachment(); + //auto& selectedOutput = shading; - cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + //cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); cmd.TransitionImageLayout(vk.DrawImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); - cmd.CopyImageToImage(selectedOutput.Image, vk.GetDrawImage()); + //cmd.CopyImageToImage(selectedOutput.Image, vk.GetDrawImage()); cmd.TransitionImageLayout(vk.DrawImage, VK_IMAGE_LAYOUT_GENERAL); - cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_GENERAL); + //cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_GENERAL); - cmd.TransitionImageLayout(shadow.Image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); -} - -void VkSceneRenderer::CreateBuffers() -{ - CameraData camData{}; - camData.View = Matrix4(1.0f); - camData.Projection = Matrix4(1.0f); - camData.InvView = Matrix4(1.0f); - camData.InvProjection = Matrix4(1.0f); - // init camera buffer - GPUResources& resources = GPUResources::Get(); - CameraBuffer = resources.CreateBuffer(sizeof(CameraData), BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "CameraBuffer"); - UpdateCameraData(camData); - - ModelBuffer = resources.CreateBuffer(sizeof(Matrix4) * MAX_MODEL_MATRIX, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "TransformBuffer"); - MaterialBuffer = resources.CreateBuffer(sizeof(MaterialBufferStruct) * MAX_MATERIAL, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "MaterialBuffer"); - LightBuffer = resources.CreateBuffer(sizeof(LightData) * MAX_LIGHTS, BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "LightBuffer"); + //cmd.TransitionImageLayout(shadow.Image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); } void VkSceneRenderer::LoadShaders() @@ -247,290 +226,175 @@ void VkSceneRenderer::LoadShaders() Shaders["shadow_vert"] = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/shadow.vert"); } -void VkSceneRenderer::CreateSamplers() -{ - auto device = VkRenderer::Get().GetDevice(); - VkSamplerCreateInfo sampler = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO }; - - sampler.magFilter = VK_FILTER_NEAREST; - sampler.minFilter = VK_FILTER_NEAREST; - - vkCreateSampler(device, &sampler, nullptr, &SamplerNearest); - - sampler.magFilter = VK_FILTER_LINEAR; - sampler.minFilter = VK_FILTER_LINEAR; - vkCreateSampler(device, &sampler, nullptr, &SamplerLinear); -} - void VkSceneRenderer::CreateDescriptors() { - auto vk = VkRenderer::Get(); - auto device = vk.GetDevice(); - - // Camera - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - CameraBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); - } - - { - // Triangle vertex buffer layout - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - TriangleBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_VERTEX_BIT); - } - - { - // Matrices - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - ModelBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_VERTEX_BIT); - } - - // Textures - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); - ImageDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); - } - - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLER); - SamplerDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); - } - - // Material - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - MaterialBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_FRAGMENT_BIT); - } - - //Bindless Textures - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, MAX_TEXTURES); - TextureBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); - } - - // bindless lights - { - DescriptorLayoutBuilder builder; - builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); - LightBufferDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS); - } - - auto allocator = vk.GetDescriptorAllocator(); - TriangleBufferDescriptors = allocator.Allocate(device, TriangleBufferDescriptorLayout); - CameraBufferDescriptors = allocator.Allocate(device, CameraBufferDescriptorLayout); - ModelBufferDescriptor = allocator.Allocate(device, ModelBufferDescriptorLayout); - SamplerDescriptor = allocator.Allocate(device, SamplerDescriptorLayout); - MaterialBufferDescriptor = allocator.Allocate(device, MaterialBufferDescriptorLayout); - TextureBufferDescriptor = allocator.Allocate(device, TextureBufferDescriptorLayout); - LightBufferDescriptor = allocator.Allocate(device, LightBufferDescriptorLayout); - //SamplerDescriptor = allocator.Allocate(device, SamplerDescriptorLayout); - - // Update descriptor set for camera - VkDescriptorBufferInfo camBufferInfo{}; - camBufferInfo.buffer = CameraBuffer->GetBuffer(); - camBufferInfo.offset = 0; - camBufferInfo.range = VK_WHOLE_SIZE; - - VkWriteDescriptorSet bufferWriteCam = {}; - bufferWriteCam.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - bufferWriteCam.pNext = nullptr; - bufferWriteCam.dstBinding = 0; - bufferWriteCam.dstSet = CameraBufferDescriptors; - bufferWriteCam.descriptorCount = 1; - bufferWriteCam.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWriteCam.pBufferInfo = &camBufferInfo; - vkUpdateDescriptorSets(device, 1, &bufferWriteCam, 0, nullptr); - - // Update Descriptor Set for Texture - VkDescriptorImageInfo textureInfo = {}; - textureInfo.sampler = SamplerNearest; // Your VkSampler object - textureInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - - // Update Descriptor Set for Sampler - VkWriteDescriptorSet samplerWrite = {}; - samplerWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - samplerWrite.dstBinding = 0; // Binding for sampler (in shader) - samplerWrite.dstSet = SamplerDescriptor; // The allocated descriptor set for the sampler - samplerWrite.descriptorCount = 1; - samplerWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; - samplerWrite.pImageInfo = &textureInfo; // Sampler info (same as texture) - - vkUpdateDescriptorSets(device, 1, &samplerWrite, 0, nullptr); - - // Textures - } void VkSceneRenderer::CreatePipelines() { - sceneRenderPipeline = SceneRenderPipeline(); + sceneRenderPipeline = CreateRef(); - ShadowPipeline = RenderPipeline(); - auto& shadowPass = ShadowPipeline.AddPass("Shadow"); - shadowPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth); - shadowPass.SetShaders(Shaders["shadow_vert"], Shaders["shadow_frag"]); - shadowPass.SetPushConstant(modelPushConstant); - shadowPass.SetPreRender([&](PassRenderContext& ctx) { - auto layout = ctx.renderPass->PipelineLayout; - ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0); - ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1); - ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3); - ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); - ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5); - ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6); - ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); - }); - shadowPass.SetRender([&](PassRenderContext& ctx) { - auto& cmd = ctx.commandBuffer; - auto& scene = ctx.scene; - auto& vk = VkRenderer::Get(); - - // Draw the scene - { - ZoneScopedN("Render Models"); - auto view = scene->m_Registry.view(); - for (auto e : view) - { - auto [transform, mesh, visibility] = view.get(e); - if (!mesh.ModelResource || !visibility.Visible) - { - continue; - } - - Entity entity = Entity((entt::entity)e, scene.get()); - for (auto& m : mesh.ModelResource->GetMeshes()) - { - Ref vkMesh = m->GetVkMesh(); - Matrix4 globalTransform = transform.GetGlobalTransform(); - - auto descSet = vkMesh->GetDescriptorSet(); - cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2); - - // Bind texture descriptor set - Ref material = m->GetMaterial(); - Ref albedo = GPUResources::Get().GetTexture(material->AlbedoImage); - - modelPushConstant.Index = ModelMatrixMapping[entity.GetID()]; - modelPushConstant.MaterialIndex = MeshMaterialMapping[vkMesh->GetID()]; - modelPushConstant.CameraID = ctx.cameraID; - - cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant); - cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer()); - cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t)); - } - } - } - }); - ShadowPipeline.Build(); - - GBufferPipeline = RenderPipeline(); - auto& gBufferPass = GBufferPipeline.AddPass("GBuffer"); - gBufferPass.SetShaders(Shaders["basic_vert"], Shaders["basic_frag"]); - gBufferPass.AddAttachment("Albedo", ImageFormat::RGBA8); - gBufferPass.AddAttachment("Normal", ImageFormat::RGBA8); - gBufferPass.AddAttachment("Material", ImageFormat::RGBA8); - gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth); - gBufferPass.SetPushConstant(modelPushConstant); - gBufferPass.SetPreRender([&](PassRenderContext& ctx) { - auto layout = ctx.renderPass->PipelineLayout; - ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0); - ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1); - ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3); - ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); - ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5); - ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6); - ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); - }); - gBufferPass.SetRender([&](PassRenderContext& ctx){ - auto& cmd = ctx.commandBuffer; - auto& scene = ctx.scene; - auto& vk = VkRenderer::Get(); - - // Draw the scene - { - ZoneScopedN("Render Models"); - auto view = scene->m_Registry.view(); - for (auto e : view) - { - auto [transform, mesh, visibility] = view.get(e); - if (!mesh.ModelResource || !visibility.Visible) - { - continue; - } - - Entity entity = Entity((entt::entity)e, scene.get()); - for (auto& m : mesh.ModelResource->GetMeshes()) - { - Ref vkMesh = m->GetVkMesh(); - Matrix4 globalTransform = transform.GetGlobalTransform(); - - auto descSet = vkMesh->GetDescriptorSet(); - cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2); - - // Bind texture descriptor set - Ref material = m->GetMaterial(); - Ref albedo = GPUResources::Get().GetTexture(material->AlbedoImage); - - modelPushConstant.Index = ModelMatrixMapping[entity.GetID()]; - modelPushConstant.MaterialIndex = MeshMaterialMapping[vkMesh->GetID()]; - modelPushConstant.CameraID = ctx.cameraID; - cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant); - - cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer()); - cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t)); - } - } - } - - }); - - auto& shadingPass = GBufferPipeline.AddPass("Shading"); - shadingPass.SetShaders(Shaders["shading_vert"], Shaders["shading_frag"]); - shadingPass.SetPushConstant(shadingPushConstant); - shadingPass.AddAttachment("Output", ImageFormat::RGBA8); - shadingPass.SetDepthTest(false); - shadingPass.AddInput("Albedo"); - shadingPass.AddInput("Normal"); - shadingPass.AddInput("Depth"); - shadingPass.AddInput("Material"); - shadingPass.SetPreRender([&](PassRenderContext& ctx) { - auto layout = ctx.renderPass->PipelineLayout; - auto cmd = ctx.commandBuffer; - cmd.BindDescriptorSet(layout, CameraBufferDescriptors, 0); - cmd.BindDescriptorSet(layout, ModelBufferDescriptor, 1); - cmd.BindDescriptorSet(layout, SamplerDescriptor, 3); - cmd.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); - cmd.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5); - cmd.BindDescriptorSet(layout, LightBufferDescriptor, 6); - cmd.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); - - auto& gpu = GPUResources::Get(); - auto& gbufferPass = GBufferPipeline.GetRenderPass("GBuffer"); - shadingPushConstant.AlbedoTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Albedo").Image->GetID()); - shadingPushConstant.DepthTextureID = gpu.GetBindlessTextureID(gbufferPass.GetDepthAttachment().Image->GetID()); - shadingPushConstant.NormalTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Normal").Image->GetID()); - shadingPushConstant.MaterialTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Material").Image->GetID()); - shadingPushConstant.CameraID = ctx.cameraID; - }); - shadingPass.SetRender([](PassRenderContext& ctx) { - auto& cmd = ctx.commandBuffer; - cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ShadingPushConstant), &shadingPushConstant); - - // Draw full screen quad - cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, quadMesh->GetDescriptorSet(), 2); - cmd.BindIndexBuffer(quadMesh->GetIndexBuffer()->GetBuffer()); - cmd.DrawIndexed(6); - }); - - GBufferPipeline.Build(); - ShadowPipeline.Build(); + //ShadowPipeline = RenderPipeline(); + //auto& shadowPass = ShadowPipeline.AddPass("Shadow"); + //shadowPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth); + //shadowPass.SetShaders(Shaders["shadow_vert"], Shaders["shadow_frag"]); + //shadowPass.SetPushConstant(modelPushConstant); + //shadowPass.SetPreRender([&](PassRenderContext& ctx) { + // auto layout = ctx.renderPass->PipelineLayout; + // ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().ModelDescriptor, 1); + // ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3); + // ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TexturesDescriptor, 5); + // ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); + //}); + //shadowPass.SetRender([&](PassRenderContext& ctx) { + // auto& cmd = ctx.commandBuffer; + // auto& scene = ctx.scene; + // auto& vk = VkRenderer::Get(); + // + // // Draw the scene + // { + // ZoneScopedN("Render Models"); + // auto view = scene->m_Registry.view(); + // for (auto e : view) + // { + // auto [transform, mesh, visibility] = view.get(e); + // if (!mesh.ModelResource || !visibility.Visible) + // { + // continue; + // } + // + // Entity entity = Entity((entt::entity)e, scene.get()); + // for (auto& m : mesh.ModelResource->GetMeshes()) + // { + // Ref vkMesh = m->GetVkMesh(); + // Matrix4 globalTransform = transform.GetGlobalTransform(); + // + // auto descSet = vkMesh->GetDescriptorSet(); + // cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2); + // + // // Bind texture descriptor set + // Ref material = m->GetMaterial(); + // Ref albedo = GPUResources::Get().GetTexture(material->AlbedoImage); + // + // + // + // modelPushConstant.Index = GPUResources::Get().GetBindlessTransformID(entity.GetID()); + // modelPushConstant.MaterialIndex = GPUResources::Get().GetBindlessMaterialID(entity.GetID()); + // modelPushConstant.CameraID = ctx.cameraID; + // + // cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant); + // cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer()); + // cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t)); + // } + // } + // } + //}); + //ShadowPipeline.Build(); + // + //GBufferPipeline = RenderPipeline(); + //auto& gBufferPass = GBufferPipeline.AddPass("GBuffer"); + //gBufferPass.SetShaders(Shaders["basic_vert"], Shaders["basic_frag"]); + //gBufferPass.AddAttachment("Albedo", ImageFormat::RGBA8); + //gBufferPass.AddAttachment("Normal", ImageFormat::RGBA8); + //gBufferPass.AddAttachment("Material", ImageFormat::RGBA8); + //gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth); + //gBufferPass.SetPushConstant(modelPushConstant); + //gBufferPass.SetPreRender([&](PassRenderContext& ctx) { + // auto layout = ctx.renderPass->PipelineLayout; + // ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().ModelDescriptor, 1); + // ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3); + // ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TexturesDescriptor, 5); + // ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6); + // ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); + //}); + //gBufferPass.SetRender([&](PassRenderContext& ctx){ + // auto& cmd = ctx.commandBuffer; + // auto& scene = ctx.scene; + // auto& vk = VkRenderer::Get(); + // + // // Draw the scene + // { + // ZoneScopedN("Render Models"); + // auto view = scene->m_Registry.view(); + // for (auto e : view) + // { + // auto [transform, mesh, visibility] = view.get(e); + // if (!mesh.ModelResource || !visibility.Visible) + // { + // continue; + // } + // + // Entity entity = Entity((entt::entity)e, scene.get()); + // for (auto& m : mesh.ModelResource->GetMeshes()) + // { + // Ref vkMesh = m->GetVkMesh(); + // Matrix4 globalTransform = transform.GetGlobalTransform(); + // + // auto descSet = vkMesh->GetDescriptorSet(); + // cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2); + // + // // Bind texture descriptor set + // Ref material = m->GetMaterial(); + // Ref albedo = GPUResources::Get().GetTexture(material->AlbedoImage); + // + // modelPushConstant.Index = GPUResources::Get().GetBindlessTransformID(entity.GetID()); + // modelPushConstant.MaterialIndex = GPUResources::Get().GetBindlessMaterialID(entity.GetID()); + // modelPushConstant.CameraID = ctx.cameraID; + // cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant); + // + // cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer()); + // cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t)); + // } + // } + // } + // + //}); + // + //auto& shadingPass = GBufferPipeline.AddPass("Shading"); + //shadingPass.SetShaders(Shaders["shading_vert"], Shaders["shading_frag"]); + //shadingPass.SetPushConstant(shadingPushConstant); + //shadingPass.AddAttachment("Output", ImageFormat::RGBA8); + //shadingPass.SetDepthTest(false); + //shadingPass.AddInput("Albedo"); + //shadingPass.AddInput("Normal"); + //shadingPass.AddInput("Depth"); + //shadingPass.AddInput("Material"); + //shadingPass.SetPreRender([&](PassRenderContext& ctx) { + // auto layout = ctx.renderPass->PipelineLayout; + // auto cmd = ctx.commandBuffer; + // cmd.BindDescriptorSet(layout, CameraBufferDescriptors, 0); + // cmd.BindDescriptorSet(layout, GPUResources::Get().ModelDescriptor, 1); + // cmd.BindDescriptorSet(layout, SamplerDescriptor, 3); + // cmd.BindDescriptorSet(layout, MaterialBufferDescriptor, 4); + // cmd.BindDescriptorSet(layout, GPUResources::Get().TexturesDescriptor, 5); + // cmd.BindDescriptorSet(layout, LightBufferDescriptor, 6); + // cmd.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7); + // + // auto& gpu = GPUResources::Get(); + // auto& gbufferPass = GBufferPipeline.GetRenderPass("GBuffer"); + // shadingPushConstant.AlbedoTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Albedo").Image->GetID()); + // shadingPushConstant.DepthTextureID = gpu.GetBindlessTextureID(gbufferPass.GetDepthAttachment().Image->GetID()); + // shadingPushConstant.NormalTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Normal").Image->GetID()); + // shadingPushConstant.MaterialTextureID = gpu.GetBindlessTextureID(gbufferPass.GetAttachment("Material").Image->GetID()); + // shadingPushConstant.CameraID = ctx.cameraID; + //}); + //shadingPass.SetRender([](PassRenderContext& ctx) { + // auto& cmd = ctx.commandBuffer; + // cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ShadingPushConstant), &shadingPushConstant); + // + // // Draw full screen quad + // cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, quadMesh->GetDescriptorSet(), 2); + // cmd.BindIndexBuffer(quadMesh->GetIndexBuffer()->GetBuffer()); + // cmd.DrawIndexed(6); + //}); + // + //GBufferPipeline.Build(); + //ShadowPipeline.Build(); } void VkSceneRenderer::SetGBufferSize(const Vector2& size) @@ -538,30 +402,6 @@ void VkSceneRenderer::SetGBufferSize(const Vector2& size) Context.Size = size; } -void VkSceneRenderer::UpdateCameraData(const CameraData& data) -{ - CameraData adjustedData = data; - adjustedData.View = data.View; - adjustedData.Projection = data.Projection; - adjustedData.InvView = data.InvView; - adjustedData.InvProjection = data.InvProjection; - adjustedData.Position = data.InvView[3]; - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().CameraStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &adjustedData, sizeof(CameraData)); - - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(CameraData); - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().CameraStagingBuffer->GetBuffer(), CameraBuffer->GetBuffer(), 1, ©); - }); - - vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), VkRenderer::Get().GetCurrentFrame().CameraStagingBuffer->GetAllocation()); -} - void VkSceneRenderer::BuildMatrixBuffer() { // This will scan and build the matrix buffer for the next frame. @@ -594,7 +434,8 @@ void VkSceneRenderer::BuildMatrixBuffer() allTransforms[currentIndex] = transform.GetGlobalTransform(); Entity entity = Entity((entt::entity)e, scene.get()); - ModelMatrixMapping[entity.GetID()] = currentIndex; + + GPUResources::Get().ModelMatrixMapping[entity.GetID()] = currentIndex; for (auto& m : mesh.ModelResource->GetMeshes()) { @@ -622,7 +463,7 @@ void VkSceneRenderer::BuildMatrixBuffer() materialBuffer.aoTextureId = material->HasAO() ? res.GetBindlessTextureID(material->AOImage) : 0; materialBuffer.roughnessTextureId = material->HasRoughness() ? res.GetBindlessTextureID(material->RoughnessImage) : 0; allMaterials[currentMaterialIndex] = materialBuffer; - MeshMaterialMapping[m->GetVkMesh()->GetID()] = currentMaterialIndex; + GPUResources::Get().MeshMaterialMapping[m->GetVkMesh()->GetID()] = currentMaterialIndex; currentMaterialIndex++; } @@ -671,121 +512,22 @@ void VkSceneRenderer::BuildMatrixBuffer() currentIndex++; } - ModelTransforms = ModelData{ allTransforms }; - MaterialDataContainer = MaterialData{ allMaterials }; - LightDataContainerArray = LightDataContainer{ allLights }; - shadingPushConstant.LightCount = currentIndex; + auto& gpu = GPUResources::Get(); + gpu.ModelTransforms = ModelData{ allTransforms }; + gpu.MaterialDataContainer = MaterialData{ allMaterials }; + gpu.LightDataContainerArray = LightDataContainer{ allLights }; + + //shadingPushConstant.LightCount = currentIndex; // Copy CSM split depths for (int i = 0; i < CSM_AMOUNT; i++) { - shadingPushConstant.CascadeSplits[i] = LightComponent::mCascadeSplitDepth[i]; + //shadingPushConstant.CascadeSplits[i] = LightComponent::mCascadeSplitDepth[i]; } } void VkSceneRenderer::UpdateTransformBuffer() { - // Update tranform buffer - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &ModelTransforms, sizeof(ModelData)); - VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(ModelData); - - vkCmdCopyBuffer(cmd, VkRenderer::Get().GetCurrentFrame().ModelStagingBuffer->GetBuffer(), ModelBuffer->GetBuffer(), 1, ©); - }); - - 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 = ModelBufferDescriptor; - bufferWriteModel.descriptorCount = 1; - bufferWriteModel.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - bufferWriteModel.pBufferInfo = &transformBufferInfo; - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWriteModel, 0, nullptr); - } - - // Update material buffer - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().MaterialStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &MaterialDataContainer, sizeof(MaterialData)); - - 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, ©); - }); - - 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 = MaterialBufferDescriptor; - 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); - } - - { - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (VkRenderer::Get().GetCurrentFrame().LightStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &LightDataContainerArray, sizeof(LightDataContainer)); - - 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, ©); - }); - - 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 = LightBufferDescriptor; - 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); - } } \ No newline at end of file diff --git a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h index 80dde6c8..495e7595 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h +++ b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h @@ -12,88 +12,10 @@ #include #include #include -#include "SceneRenderPipeline.h" - namespace Nuake { - struct CameraData; - - struct ModelPushConstant - { - int Index; - int MaterialIndex; - int CameraID; - char padding[120]; // 124 bytes to reach 128 bytes - }; - - struct ShadingPushConstant - { - int AlbedoTextureID; - int DepthTextureID; - int NormalTextureID; - int MaterialTextureID; - int LightCount; - int CameraID; - float CascadeSplits[4]; - }; - - struct ModelData - { - std::array Data; - }; - - // This is what is present on the shader as a structured buffer - struct MaterialBufferStruct - { - float hasAlbedo; - Vector3 albedo; - int hasNormal; - int hasMetalness; - int hasRoughness; - int hasAO; - float metalnessValue; - float roughnessValue; - float aoValue; - int albedoTextureId; - int normalTextureId; - int metalnessTextureId; - int roughnessTextureId; - int aoTextureId; - }; - - // This is the *whole* buffer - struct MaterialData - { - std::array Data; - }; - - struct LightData - { - Vector3 position; - int type; - Vector4 color; - Vector3 direction; - float outerConeAngle; - float innerConeAngle; - bool castShadow; - int shadowMapTextureId[4]; - int transformId[4]; - float pad[2]; - }; - - struct LightDataContainer - { - std::array Data; - }; - - - - struct CameraSceneRenderer - { - - }; - + class SceneRenderPipeline; class VkSceneRenderer { public: @@ -109,10 +31,6 @@ namespace Nuake VkDescriptorSet CameraBufferDescriptors; VkDescriptorSetLayout CameraBufferDescriptorLayout; - Ref ModelBuffer; - VkDescriptorSet ModelBufferDescriptor; - VkDescriptorSetLayout ModelBufferDescriptorLayout; - Ref MaterialBuffer; VkDescriptorSet MaterialBufferDescriptor; VkDescriptorSetLayout MaterialBufferDescriptorLayout; @@ -121,20 +39,10 @@ namespace Nuake VkDescriptorSet TextureBufferDescriptor; VkDescriptorSetLayout TextureBufferDescriptorLayout; - ModelData ModelTransforms; - MaterialData MaterialDataContainer; - - LightDataContainer LightDataContainerArray; - - std::map ModelMatrixMapping; // Holds mapping between model entity and transform index - std::map MeshMaterialMapping; // Holds mapping between mesh and material index - Ref LightBuffer; VkDescriptorSet LightBufferDescriptor; VkDescriptorSetLayout LightBufferDescriptorLayout; - VkSampler SamplerLinear; - VkSampler SamplerNearest; VkDescriptorSet SamplerDescriptor; VkDescriptorSetLayout SamplerDescriptorLayout; @@ -142,10 +50,10 @@ namespace Nuake VkDescriptorSetLayout ImageDescriptorLayout; // New pipeline stuff - RenderPipeline GBufferPipeline; - RenderPipeline ShadowPipeline; + //RenderPipeline GBufferPipeline; + //RenderPipeline ShadowPipeline; - SceneRenderPipeline sceneRenderPipeline; + Ref sceneRenderPipeline; public: UUID CurrentCamera; VkSceneRenderer(); @@ -154,16 +62,13 @@ namespace Nuake void Init(); void SetGBufferSize(const Vector2& size); - void UpdateCameraData(const CameraData& data); void BeginScene(RenderContext inContext); void EndScene(); - RenderPipeline& GetRenderPipeline() { return ShadowPipeline; } + //RenderPipeline& GetRenderPipeline() { return ShadowPipeline; } private: void LoadShaders(); - void CreateBuffers(); - void CreateSamplers(); void CreateDescriptors(); void CreatePipelines(); diff --git a/Resources/Shaders/Vulkan/shading.frag b/Resources/Shaders/Vulkan/shading.frag index 9630cf28..b0eae985 100644 --- a/Resources/Shaders/Vulkan/shading.frag +++ b/Resources/Shaders/Vulkan/shading.frag @@ -1,17 +1,30 @@ -struct Camera +// Transforms +struct ModelData { - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; + float4x4 model; }; [[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); +StructuredBuffer model : register(t1); -[[vk::binding(0, 3)]] -SamplerState mySampler : register(s0); // Sampler binding at slot s0 +// Vertex +struct Vertex +{ + float3 position; + float uv_x; + float3 normal; + float uv_y; + float3 tangent; + float3 bitangent; +}; +[[vk::binding(0, 1)]] +StructuredBuffer vertexBuffer : register(t2); + +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials struct Material { float hasAlbedo; @@ -23,13 +36,20 @@ struct Material float metalnessValue; float roughnessValue; float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; }; -[[vk::binding(0, 4)]] +[[vk::binding(0, 3)]] StructuredBuffer material; -[[vk::binding(0, 5)]] -Texture2D textures[]; // Array de 500 textures +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; +// Lights struct Light { float3 position; @@ -43,9 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -56,7 +77,7 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; struct PSInput { @@ -82,74 +103,6 @@ struct ShadingPushConstant [[vk::push_constant]] ShadingPushConstant pushConstants; -static const float2 poissonDisk64[64] = -{ - float2 ( 0.1187053, 0.7951565), - float2 ( 0.1173675, 0.6087878), - float2 (-0.09958518, 0.7248842), - float2 ( 0.4259812, 0.6152718), - float2 ( 0.3723574, 0.8892787), - float2 (-0.02289676, 0.9972908), - float2 (-0.08234791, 0.5048386), - float2 ( 0.1821235, 0.9673787), - float2 (-0.2137264, 0.9011746), - float2 ( 0.3115066, 0.4205415), - float2 ( 0.1216329, 0.383266), - float2 ( 0.5948939, 0.7594361), - float2 ( 0.7576465, 0.5336417), - float2 (-0.521125, 0.7599803), - float2 (-0.2923127, 0.6545699), - float2 ( 0.6782473, 0.22385), - float2 (-0.3077152, 0.4697627), - float2 ( 0.4484913, 0.2619455), - float2 (-0.5308799, 0.4998215), - float2 (-0.7379634, 0.5304936), - float2 ( 0.02613133, 0.1764302), - float2 (-0.1461073, 0.3047384), - float2 (-0.8451027, 0.3249073), - float2 (-0.4507707, 0.2101997), - float2 (-0.6137282, 0.3283674), - float2 (-0.2385868, 0.08716244), - float2 ( 0.3386548, 0.01528411), - float2 (-0.04230833, -0.1494652), - float2 ( 0.167115, -0.1098648), - float2 (-0.525606, 0.01572019), - float2 (-0.7966855, 0.1318727), - float2 ( 0.5704287, 0.4778273), - float2 (-0.9516637, 0.002725032), - float2 (-0.7068223, -0.1572321), - float2 ( 0.2173306, -0.3494083), - float2 ( 0.06100426, -0.4492816), - float2 ( 0.2333982, 0.2247189), - float2 ( 0.07270987, -0.6396734), - float2 ( 0.4670808, -0.2324669), - float2 ( 0.3729528, -0.512625), - float2 ( 0.5675077, -0.4054544), - float2 (-0.3691984, -0.128435), - float2 ( 0.8752473, 0.2256988), - float2 (-0.2680127, -0.4684393), - float2 (-0.1177551, -0.7205751), - float2 (-0.1270121, -0.3105424), - float2 ( 0.5595394, -0.06309237), - float2 (-0.9299136, -0.1870008), - float2 ( 0.974674, 0.03677348), - float2 ( 0.7726735, -0.06944724), - float2 (-0.4995361, -0.3663749), - float2 ( 0.6474168, -0.2315787), - float2 ( 0.1911449, -0.8858921), - float2 ( 0.3671001, -0.7970535), - float2 (-0.6970353, -0.4449432), - float2 (-0.417599, -0.7189326), - float2 (-0.5584748, -0.6026504), - float2 (-0.02624448, -0.9141423), - float2 ( 0.565636, -0.6585149), - float2 (-0.874976, -0.3997879), - float2 ( 0.9177843, -0.2110524), - float2 ( 0.8156927, -0.3969557), - float2 (-0.2833054, -0.8395444), - float2 ( 0.799141, -0.5886372) -}; - float3 WorldPosFromDepth(float depth, float2 uv, float4x4 invProj, float4x4 invView) { float z = depth; @@ -247,7 +200,6 @@ float ShadowCalculation(Light light, float3 fragPos, float3 normal) PSOutput main(PSInput input) { PSOutput output; - Camera camData = camera[0]; CameraView camView = cameras[pushConstants.CameraID]; int depthTexture = pushConstants.DepthInputTextureId; diff --git a/Resources/Shaders/Vulkan/shading.vert b/Resources/Shaders/Vulkan/shading.vert index e62485b0..7f8df807 100644 --- a/Resources/Shaders/Vulkan/shading.vert +++ b/Resources/Shaders/Vulkan/shading.vert @@ -1,21 +1,12 @@ -struct Camera -{ - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; -}; -[[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); - +// Transforms struct ModelData { float4x4 model; }; -[[vk::binding(0, 1)]] +[[vk::binding(0, 0)]] StructuredBuffer model : register(t1); +// Vertex struct Vertex { float3 position; @@ -26,9 +17,39 @@ struct Vertex float3 bitangent; }; -[[vk::binding(0, 2)]] +[[vk::binding(0, 1)]] StructuredBuffer vertexBuffer : register(t2); +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials +struct Material +{ + float hasAlbedo; + float3 albedo; + int hasNormal; + int hasMetalness; + int hasRoughness; + int hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; +}; +[[vk::binding(0, 3)]] +StructuredBuffer material; + +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; + +// Lights struct Light { float3 position; @@ -42,10 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; - +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -56,7 +77,7 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; struct ShadingPushConstant diff --git a/Resources/Shaders/Vulkan/shadow.frag b/Resources/Shaders/Vulkan/shadow.frag index 63378314..fcbd34e1 100644 --- a/Resources/Shaders/Vulkan/shadow.frag +++ b/Resources/Shaders/Vulkan/shadow.frag @@ -1,17 +1,30 @@ -struct Camera +// Transforms +struct ModelData { - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; + float4x4 model; }; [[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); +StructuredBuffer model : register(t1); -[[vk::binding(0, 3)]] -SamplerState mySampler : register(s0); // Sampler binding at slot s0 +// Vertex +struct Vertex +{ + float3 position; + float uv_x; + float3 normal; + float uv_y; + float3 tangent; + float3 bitangent; +}; +[[vk::binding(0, 1)]] +StructuredBuffer vertexBuffer : register(t2); + +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials struct Material { float hasAlbedo; @@ -29,13 +42,14 @@ struct Material int roughnessTextureId; int aoTextureId; }; +[[vk::binding(0, 3)]] +StructuredBuffer material; +// Textures [[vk::binding(0, 4)]] -StructuredBuffer material; // array de 2000 materials - -[[vk::binding(0, 5)]] -Texture2D textures[]; // Array de 500 textures +Texture2D textures[]; +// Lights struct Light { float3 position; @@ -49,9 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -62,7 +77,7 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; struct PSInput { diff --git a/Resources/Shaders/Vulkan/shadow.vert b/Resources/Shaders/Vulkan/shadow.vert index f0f9409b..4dd14f04 100644 --- a/Resources/Shaders/Vulkan/shadow.vert +++ b/Resources/Shaders/Vulkan/shadow.vert @@ -1,21 +1,12 @@ -struct Camera -{ - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; -}; -[[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); - +// Transforms struct ModelData { float4x4 model; }; -[[vk::binding(0, 1)]] +[[vk::binding(0, 0)]] StructuredBuffer model : register(t1); +// Vertex struct Vertex { float3 position; @@ -26,16 +17,39 @@ struct Vertex float3 bitangent; }; -[[vk::binding(0, 2)]] +[[vk::binding(0, 1)]] StructuredBuffer vertexBuffer : register(t2); -struct ModelPushConstant -{ - int modelIndex; // Push constant data - int materialIndex; - int cameraID; -}; +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); +// Materials +struct Material +{ + float hasAlbedo; + float3 albedo; + int hasNormal; + int hasMetalness; + int hasRoughness; + int hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; +}; +[[vk::binding(0, 3)]] +StructuredBuffer material; + +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; + +// Lights struct Light { float3 position; @@ -49,9 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -62,9 +77,16 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; +struct ModelPushConstant +{ + int modelIndex; // Push constant data + int materialIndex; + int cameraID; +}; + [[vk::push_constant]] ModelPushConstant pushConstants; @@ -92,9 +114,7 @@ VSOutput main(uint vertexIndex : SV_VertexID) { VSOutput output; - Camera camData = camera[0]; ModelData modelData = model[pushConstants.modelIndex]; - CameraView camView = cameras[pushConstants.cameraID]; // Load vertex data from the buffer diff --git a/Resources/Shaders/Vulkan/triangle.frag b/Resources/Shaders/Vulkan/triangle.frag index 06c53388..e11b7e5b 100644 --- a/Resources/Shaders/Vulkan/triangle.frag +++ b/Resources/Shaders/Vulkan/triangle.frag @@ -1,17 +1,30 @@ -struct Camera +// Transforms +struct ModelData { - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; + float4x4 model; }; [[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); +StructuredBuffer model : register(t1); -[[vk::binding(0, 3)]] -SamplerState mySampler : register(s0); // Sampler binding at slot s0 +// Vertex +struct Vertex +{ + float3 position; + float uv_x; + float3 normal; + float uv_y; + float3 tangent; + float3 bitangent; +}; +[[vk::binding(0, 1)]] +StructuredBuffer vertexBuffer : register(t2); + +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials struct Material { float hasAlbedo; @@ -29,13 +42,14 @@ struct Material int roughnessTextureId; int aoTextureId; }; +[[vk::binding(0, 3)]] +StructuredBuffer material; +// Textures [[vk::binding(0, 4)]] -StructuredBuffer material; // array de 2000 materials - -[[vk::binding(0, 5)]] -Texture2D textures[]; // Array de 500 textures +Texture2D textures[]; +// Lights struct Light { float3 position; @@ -49,9 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -62,7 +77,7 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; struct PSInput { @@ -103,7 +118,7 @@ PSOutput main(PSInput input) } normal = mul(input.TBN, normal); - normal = input.Normal / 2.0f + 0.5f; + normal = normal / 2.0f + 0.5f; output.oNormal = float4(normal, 1.0f); // MATERIAL diff --git a/Resources/Shaders/Vulkan/triangle.vert b/Resources/Shaders/Vulkan/triangle.vert index 760f9b37..8ef52480 100644 --- a/Resources/Shaders/Vulkan/triangle.vert +++ b/Resources/Shaders/Vulkan/triangle.vert @@ -1,21 +1,12 @@ -struct Camera -{ - float4x4 view; - float4x4 proj; - float4x4 invView; - float4x4 invProj; - float3 position; -}; -[[vk::binding(0, 0)]] -StructuredBuffer camera : register(t0); - +// Transforms struct ModelData { float4x4 model; }; -[[vk::binding(0, 1)]] +[[vk::binding(0, 0)]] StructuredBuffer model : register(t1); +// Vertex struct Vertex { float3 position; @@ -26,16 +17,39 @@ struct Vertex float3 bitangent; }; -[[vk::binding(0, 2)]] +[[vk::binding(0, 1)]] StructuredBuffer vertexBuffer : register(t2); -struct ModelPushConstant -{ - int modelIndex; // Push constant data - int materialIndex; - int cameraID; -}; +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); +// Materials +struct Material +{ + float hasAlbedo; + float3 albedo; + int hasNormal; + int hasMetalness; + int hasRoughness; + int hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; +}; +[[vk::binding(0, 3)]] +StructuredBuffer material; + +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; + +// Lights struct Light { float3 position; @@ -49,9 +63,10 @@ struct Light int transformId[4]; }; -[[vk::binding(0, 6)]] +[[vk::binding(0, 5)]] StructuredBuffer lights; +// Cameras struct CameraView { float4x4 View; float4x4 Projection; @@ -62,9 +77,16 @@ struct CameraView { float Near; float Far; }; -[[vk::binding(0, 7)]] +[[vk::binding(0, 6)]] StructuredBuffer cameras; +struct ModelPushConstant +{ + int modelIndex; // Push constant data + int materialIndex; + int cameraID; +}; + [[vk::push_constant]] ModelPushConstant pushConstants; @@ -82,8 +104,7 @@ VSOutput main(uint vertexIndex : SV_VertexID) { VSOutput output; - Camera camData = camera[0]; - CameraView camView = cameras[1]; + CameraView camView = cameras[pushConstants.cameraID]; ModelData modelData = model[pushConstants.modelIndex]; // Load vertex data from the buffer