From b70211ee86067ff622261c24385fd87be60f9ba0 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 25 Jan 2025 17:10:31 -0500 Subject: [PATCH] Cleaned up VulkanRenderer --- Nuake/src/Rendering/Vulkan/VulkanRenderer.cpp | 145 +----------------- Nuake/src/Rendering/Vulkan/VulkanRenderer.h | 71 +++------ 2 files changed, 24 insertions(+), 192 deletions(-) diff --git a/Nuake/src/Rendering/Vulkan/VulkanRenderer.cpp b/Nuake/src/Rendering/Vulkan/VulkanRenderer.cpp index 51f0807e..fc6ba318 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanRenderer.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanRenderer.cpp @@ -32,7 +32,7 @@ #include -bool NKUseValidationLayer = true; +bool NKUseValidationLayer = false; using namespace Nuake; @@ -91,11 +91,6 @@ void VkRenderer::Initialize() InitSync(); - ShaderCompiler& shaderCompiler = ShaderCompiler::Get(); - TriangleVertShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/triangle.vert"); - BackgroundShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/background.comp"); - TriangleFragShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/triangle.frag"); - std::vector rect_vertices; rect_vertices.resize(4); @@ -128,14 +123,8 @@ void VkRenderer::Initialize() camData.Projection = Matrix4(1.0f); // init camera buffer - CameraBuffer = resources.CreateBuffer(sizeof(CameraData), BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "CameraBuffer"); - UploadCameraData(camData); - InitDescriptors(); - //InitPipeline(); - //InitTrianglePipeline(); - InitImgui(); SceneRenderer = CreateRef(); @@ -179,7 +168,6 @@ void VkRenderer::CleanUp() vkDestroyInstance(Instance, nullptr); } - void VkRenderer::GetInstance() { vkb::InstanceBuilder builder; @@ -234,7 +222,6 @@ void VkRenderer::RecreateSwapchain() CreateSwapchain(Window::Get()->GetSize()); UpdateDescriptorSets(); - // Update sceneRenderer SceneRenderer->SetGBufferSize(Window::Get()->GetSize()); } @@ -355,23 +342,7 @@ void VkRenderer::InitDescriptors() DrawImageDescriptorLayout = builder.Build(Device, VK_SHADER_STAGE_COMPUTE_BIT); } - // Camera buffer - { - 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_ALL); - } - DrawImageDescriptors = GlobalDescriptorAllocator.Allocate(Device, DrawImageDescriptorLayout); - TriangleBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, TriangleBufferDescriptorLayout); - CameraBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, CameraBufferDescriptorLayout); UpdateDescriptorSets(); @@ -409,78 +380,6 @@ void VkRenderer::UpdateDescriptorSets() vkUpdateDescriptorSets(Device, 1, &drawImageWrite, 0, nullptr); } -void VkRenderer::InitPipeline() -{ - InitBackgroundPipeline(); -} - -void VkRenderer::InitBackgroundPipeline() -{ - VkPipelineLayoutCreateInfo computeLayout{}; - computeLayout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - computeLayout.pNext = nullptr; - computeLayout.pSetLayouts = &DrawImageDescriptorLayout; - computeLayout.setLayoutCount = 1; - - VK_CALL(vkCreatePipelineLayout(Device, &computeLayout, nullptr, &PipelineLayout)); - - VkPipelineShaderStageCreateInfo stageinfo{}; - stageinfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageinfo.pNext = nullptr; - stageinfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; - stageinfo.module = BackgroundShader->GetModule(); - stageinfo.pName = "main"; - - VkComputePipelineCreateInfo computePipelineCreateInfo{}; - computePipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; - computePipelineCreateInfo.pNext = nullptr; - computePipelineCreateInfo.layout = PipelineLayout; - computePipelineCreateInfo.stage = stageinfo; - - VK_CALL(vkCreateComputePipelines(Device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &Pipeline)); - - MainDeletionQueue.push_function([&]() { - vkDestroyPipelineLayout(Device, PipelineLayout, nullptr); - vkDestroyPipeline(Device, Pipeline, nullptr); - }); -} - -void VkRenderer::InitTrianglePipeline() -{ - VkPushConstantRange bufferRange{}; - bufferRange.offset = 0; - bufferRange.size = sizeof(GPUDrawPushConstants); - bufferRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - - VkDescriptorSetLayout layouts[] = { CameraBufferDescriptorLayout, TriangleBufferDescriptorLayout }; - - VkPipelineLayoutCreateInfo pipeline_layout_info = VulkanInit::PipelineLayoutCreateInfo(); - pipeline_layout_info.pPushConstantRanges = &bufferRange; - pipeline_layout_info.pushConstantRangeCount = 0; - pipeline_layout_info.pSetLayouts = layouts; - pipeline_layout_info.setLayoutCount = 2; - VK_CALL(vkCreatePipelineLayout(Device, &pipeline_layout_info, nullptr, &TrianglePipelineLayout)); - - //use the triangle layout we created - PipelineBuilder pipelineBuilder; - pipelineBuilder.PipelineLayout = TrianglePipelineLayout; - pipelineBuilder.SetShaders(TriangleVertShader->GetModule(), TriangleFragShader->GetModule()); - pipelineBuilder.SetInputTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); - pipelineBuilder.SetPolygonMode(VK_POLYGON_MODE_FILL); - pipelineBuilder.SetCullMode(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE); - pipelineBuilder.SetMultiSamplingNone(); - pipelineBuilder.DisableBlending(); - pipelineBuilder.DisableDepthTest(); - pipelineBuilder.SetColorAttachment(static_cast(DrawImage->GetFormat())); - pipelineBuilder.SetDepthFormat(VK_FORMAT_UNDEFINED); - TrianglePipeline = pipelineBuilder.BuildPipeline(Device); - - MainDeletionQueue.push_function([&]() { - vkDestroyPipelineLayout(Device, TrianglePipelineLayout, nullptr); - vkDestroyPipeline(Device, TrianglePipeline, nullptr); - }); -} - void VkRenderer::DrawScene(RenderContext ctx) { SceneRenderer->BeginScene(ctx); @@ -759,21 +658,6 @@ void VkRenderer::EndDraw() FrameNumber++; } -void VkRenderer::DrawBackground(VkCommandBuffer cmd) -{ - // This works - VkClearColorValue clearValue; - //float flash = std::abs(std::sin(FrameNumber / 120.f)); - clearValue = { { 0.0f, 0.0f, 0.0f, 1.0f } }; - VkImageSubresourceRange clearRange = VulkanInit::ImageSubResourceRange(VK_IMAGE_ASPECT_COLOR_BIT); - vkCmdClearColorImage(cmd, DrawImage->GetImage(), VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange); - - // This doesnt!! - //vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, Pipeline); - //vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, PipelineLayout, 0, 1, &DrawImageDescriptors, 0, nullptr); - //vkCmdDispatch(cmd, std::ceil(DrawExtent.width / 16.0), std::ceil(DrawExtent.height / 16.0), 1); -} - void VkRenderer::DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView) { VkRenderingAttachmentInfo colorAttachment = VulkanInit::AttachmentInfo(targetImageView, nullptr, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); @@ -812,33 +696,6 @@ void VkRenderer::ImmediateSubmit(std::function&& func VK_CALL(vkWaitForFences(Device, 1, &ImguiFence, true, 9999999999)); } -void VkRenderer::UploadCameraData(const CameraData& data) -{ - CameraData adjustedData = data; - adjustedData.View = Matrix4(1.0f); //data.View; - adjustedData.View = data.View; - adjustedData.Projection = glm::perspective(glm::radians(70.f), (float)DrawExtent.width / (float)DrawExtent.height, 0.0001f, 10000.0f); - adjustedData.Position = data.View[3]; - //adjustedData.Projection[1][1] *= -1; - - void* mappedData; - vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (GetCurrentFrame().CameraStagingBuffer->GetAllocation()), &mappedData); - memcpy(mappedData, &adjustedData, sizeof(CameraData)); - - ImmediateSubmit([&](VkCommandBuffer cmd) { - VkBufferCopy copy{ 0 }; - copy.dstOffset = 0; - copy.srcOffset = 0; - copy.size = sizeof(CameraData); - - vkCmdCopyBuffer(cmd, GetCurrentFrame().CameraStagingBuffer->GetBuffer(), CameraBuffer->GetBuffer(), 1, ©); - }); - - vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), GetCurrentFrame().CameraStagingBuffer->GetAllocation()); -} - - - void DescriptorAllocator::InitPool(VkDevice device, uint32_t maxSets, std::span poolRatios) { std::vector poolSizes; diff --git a/Nuake/src/Rendering/Vulkan/VulkanRenderer.h b/Nuake/src/Rendering/Vulkan/VulkanRenderer.h index 4aea8d4c..2e88ba57 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanRenderer.h +++ b/Nuake/src/Rendering/Vulkan/VulkanRenderer.h @@ -90,6 +90,7 @@ namespace Nuake Ref MaterialStagingBuffer; // Materials Ref LightStagingBuffer; // Lights Ref CamerasStagingBuffer; // Draw image + // Semaphore are for GPU -> GPU sync // Fence are for CPU -> GPU // Two, one for window, other for rendering @@ -160,50 +161,20 @@ namespace Nuake uint32_t GPUQueueFamily; DeletionQueue MainDeletionQueue; - - // Descriptors DescriptorAllocator GlobalDescriptorAllocator; public: VkDescriptorSet DrawImageDescriptors; VkDescriptorSetLayout DrawImageDescriptorLayout; - - VkDescriptorSet TriangleBufferDescriptors; - VkDescriptorSetLayout TriangleBufferDescriptorLayout; - - VkDescriptorSet CameraBufferDescriptors; - VkDescriptorSetLayout CameraBufferDescriptorLayout; - - // Pipeline - VkPipeline Pipeline; - VkPipelineLayout PipelineLayout; - - VkPipelineLayout TrianglePipelineLayout; - VkPipeline TrianglePipeline; - - Ref BackgroundShader; - Ref TriangleVertShader; - Ref TriangleFragShader; - // Imgui + // Imgui stuff VkFence ImguiFence; VkCommandBuffer ImguiCommandBuffer; VkCommandPool ImguiCommandPool; - // Buffers - - Ref CameraBuffer; Ref SceneRenderer; public: - Ref Rect; - VkQueue GPUQueue; - - Ref DrawImage; - Ref DepthImage; - VkExtent2D DrawExtent; - FrameData& GetCurrentFrame() { return Frames[FrameNumber % FRAME_OVERLAP]; }; - static VkRenderer& Get() { static VkRenderer instance; @@ -213,10 +184,13 @@ namespace Nuake VkRenderer() = default; ~VkRenderer(); - VkDevice GetDevice() const - { - return Device; - } + public: + Ref Rect; + VkQueue GPUQueue; + + Ref DrawImage; + Ref DepthImage; + VkExtent2D DrawExtent; public: void Initialize(); @@ -233,18 +207,24 @@ namespace Nuake void InitSync(); void InitDescriptors(); void UpdateDescriptorSets(); - void InitPipeline(); - void InitBackgroundPipeline(); - void InitTrianglePipeline(); void DrawScene(RenderContext ctx); void InitImgui(); void BeginScene(const UUID& camera); - bool Draw(); - void EndDraw(); + public: + VkDevice GetDevice() const + { + return Device; + } + + FrameData& GetCurrentFrame() + { + return Frames[FrameNumber % FRAME_OVERLAP]; + }; + DescriptorAllocator& GetDescriptorAllocator() { return GlobalDescriptorAllocator; @@ -255,15 +235,10 @@ namespace Nuake return Cmd(GetCurrentFrame().CommandBuffer); } - void DrawBackground(VkCommandBuffer cmd); - void DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView); - - void ImmediateSubmit(std::function&& function); - - void UploadCameraData(const CameraData& data); - //auto& GetRenderPipeline() { return this->SceneRenderer->GetRenderPipeline(); } VkDescriptorSet GetViewportDescriptor() const { return DrawImageDescriptors; } Ref GetDrawImage() const { return DrawImage; } + + void DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView); + void ImmediateSubmit(std::function&& function); }; - } \ No newline at end of file