From a134aa738d7114238dbbeec2b71b7a2c8992f676 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 29 Aug 2024 16:26:05 +0200 Subject: [PATCH] fixing render passes issues --- ScopEngine/Assets/Shaders/ScreenFragment.nzsl | 2 +- ScopEngine/Assets/Shaders/ScreenVertex.nzsl | 4 ++- .../Runtime/Includes/Graphics/Material.h | 4 +-- .../Includes/Renderer/Pipelines/Graphics.h | 6 ++-- ScopEngine/Runtime/Sources/Graphics/Model.cpp | 2 +- ScopEngine/Runtime/Sources/Renderer/Image.cpp | 2 +- .../Sources/Renderer/Pipelines/Graphics.cpp | 30 ++++++++----------- .../Renderer/RenderPasses/FinalPass.cpp | 5 ---- .../Renderer/RenderPasses/ForwardPass.cpp | 4 +-- .../Sources/Renderer/RenderPasses/Passes.cpp | 1 + .../Renderer/RenderPasses/SkyboxPass.cpp | 4 +-- ScopEngine/ThirdParty/KVF/kvf.h | 9 ++++-- 12 files changed, 36 insertions(+), 37 deletions(-) diff --git a/ScopEngine/Assets/Shaders/ScreenFragment.nzsl b/ScopEngine/Assets/Shaders/ScreenFragment.nzsl index 3b51148..336120d 100644 --- a/ScopEngine/Assets/Shaders/ScreenFragment.nzsl +++ b/ScopEngine/Assets/Shaders/ScreenFragment.nzsl @@ -20,6 +20,6 @@ external fn main(input: VertOut) -> FragOut { let output: FragOut; - output.color = vec4[f32](u_texture.Sample(input.uv).rgb, 1.0); + output.color = u_texture.Sample(input.uv); return output; } diff --git a/ScopEngine/Assets/Shaders/ScreenVertex.nzsl b/ScopEngine/Assets/Shaders/ScreenVertex.nzsl index bde5f6b..94a4440 100644 --- a/ScopEngine/Assets/Shaders/ScreenVertex.nzsl +++ b/ScopEngine/Assets/Shaders/ScreenVertex.nzsl @@ -8,7 +8,8 @@ struct VertIn struct VertOut { - [location(0)] uv: vec2[f32] + [location(0)] uv: vec2[f32], + [builtin(position)] position: vec4[f32] } const vertices = array[vec2[f32]]( @@ -23,6 +24,7 @@ fn main(input: VertIn) -> VertOut let position = vertices[input.vert_index]; let output: VertOut; + output.position = vec4[f32](position, 0.0, 1.0); output.uv = position * 0.5 + vec2[f32](0.5, 0.5); return output; diff --git a/ScopEngine/Runtime/Includes/Graphics/Material.h b/ScopEngine/Runtime/Includes/Graphics/Material.h index eaa778a..ea19c3a 100644 --- a/ScopEngine/Runtime/Includes/Graphics/Material.h +++ b/ScopEngine/Runtime/Includes/Graphics/Material.h @@ -44,11 +44,11 @@ namespace Scop m_set = set.Duplicate(); } - inline void Bind(std::size_t frame_index) + inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) { m_set.SetImage(frame_index, 0, *m_textures.albedo); m_set.SetUniformBuffer(frame_index, 1, m_data_buffer.Get(frame_index)); - m_set.Update(frame_index); + m_set.Update(frame_index, cmd); static CPUBuffer buffer(sizeof(MaterialData)); std::memcpy(buffer.GetData(), &m_data, buffer.GetSize()); diff --git a/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h b/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h index 94e48e2..066ac1e 100644 --- a/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h +++ b/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h @@ -19,7 +19,7 @@ namespace Scop GraphicPipeline() = default; void Init(std::shared_ptr vertex_shader, std::shared_ptr fragment_shader, NonOwningPtr renderer, NonOwningPtr depth, VkCullModeFlagBits culling = VK_CULL_MODE_FRONT_BIT, bool disable_vertex_inputs = false); - void Init(std::shared_ptr vertex_shader, std::shared_ptr fragment_shader, std::vector attachments, VkCullModeFlagBits culling = VK_CULL_MODE_FRONT_BIT, bool disable_vertex_inputs = false); + void Init(std::shared_ptr vertex_shader, std::shared_ptr fragment_shader, std::vector> attachments, VkCullModeFlagBits culling = VK_CULL_MODE_FRONT_BIT, bool disable_vertex_inputs = false); bool BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array clear) noexcept; void EndPipeline(VkCommandBuffer command_buffer) noexcept override; void Destroy() noexcept; @@ -31,14 +31,14 @@ namespace Scop ~GraphicPipeline() = default; private: - void CreateFramebuffers(const std::vector& render_targets); + void CreateFramebuffers(const std::vector>& render_targets); void TransitionAttachments(VkCommandBuffer cmd = VK_NULL_HANDLE); // Private override to remove access bool BindPipeline(VkCommandBuffer) noexcept override { return false; }; private: - std::vector m_attachments; + std::vector> m_attachments; std::vector m_framebuffers; std::vector m_clears; std::shared_ptr p_vertex_shader; diff --git a/ScopEngine/Runtime/Sources/Graphics/Model.cpp b/ScopEngine/Runtime/Sources/Graphics/Model.cpp index 8a23f85..45e30a5 100644 --- a/ScopEngine/Runtime/Sources/Graphics/Model.cpp +++ b/ScopEngine/Runtime/Sources/Graphics/Model.cpp @@ -32,7 +32,7 @@ namespace Scop material = m_materials[i]; if(!material->IsSetInit()) material->UpdateDescriptorSet(set); - material->Bind(frame_index); + material->Bind(frame_index, cmd); std::array sets = { matrices_set.GetSet(frame_index), material->GetSet(frame_index) }; vkCmdBindDescriptorSets(cmd, pipeline.GetPipelineBindPoint(), pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); p_mesh->Draw(cmd, drawcalls, polygondrawn, i); diff --git a/ScopEngine/Runtime/Sources/Renderer/Image.cpp b/ScopEngine/Runtime/Sources/Renderer/Image.cpp index 6eb40af..49c0478 100644 --- a/ScopEngine/Runtime/Sources/Renderer/Image.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/Image.cpp @@ -36,7 +36,7 @@ namespace Scop { if(new_layout == m_layout) return; - bool is_single_time_cmd_buffer = cmd == VK_NULL_HANDLE; + bool is_single_time_cmd_buffer = (cmd == VK_NULL_HANDLE); if(is_single_time_cmd_buffer) cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice()); kvfTransitionImageLayout(RenderCore::Get().GetDevice(), m_image, cmd, m_format, m_layout, new_layout, is_single_time_cmd_buffer); diff --git a/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp b/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp index a1a4e46..d2064da 100644 --- a/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -28,10 +28,10 @@ namespace Scop }; EventBus::RegisterListener({ functor, std::to_string((std::uintptr_t)(void**)this) }); - Init(std::move(vertex_shader), std::move(fragment_shader), std::vector{}, culling, disable_vertex_inputs); + Init(std::move(vertex_shader), std::move(fragment_shader), std::vector>{}, culling, disable_vertex_inputs); } - void GraphicPipeline::Init(std::shared_ptr vertex_shader, std::shared_ptr fragment_shader, std::vector attachments, VkCullModeFlagBits culling, bool disable_vertex_inputs) + void GraphicPipeline::Init(std::shared_ptr vertex_shader, std::shared_ptr fragment_shader, std::vector> attachments, VkCullModeFlagBits culling, bool disable_vertex_inputs) { if(!vertex_shader || !fragment_shader) FatalError("Vulkan : invalid shaders"); @@ -75,11 +75,7 @@ namespace Scop bool GraphicPipeline::BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array clear) noexcept { TransitionAttachments(command_buffer); - VkFramebuffer fb; - if(p_renderer) - fb = m_framebuffers[framebuffer_index]; - else - fb = m_framebuffers[0]; + VkFramebuffer fb = m_framebuffers[framebuffer_index]; VkExtent2D fb_extent = kvfGetFramebufferSize(fb); VkViewport viewport{}; @@ -137,7 +133,7 @@ namespace Scop Message("Vulkan : graphics pipeline destroyed"); } - void GraphicPipeline::CreateFramebuffers(const std::vector& render_targets) + void GraphicPipeline::CreateFramebuffers(const std::vector>& render_targets) { std::vector attachments; std::vector attachment_views; @@ -148,10 +144,10 @@ namespace Scop } else { - for(const auto& image : render_targets) + for(NonOwningPtr image : render_targets) { - attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(image.GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), image.GetFormat(), image.GetLayout(), image.GetLayout(), true)); - attachment_views.push_back(image.GetImageView()); + attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(image->GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), image->GetFormat(), image->GetLayout(), image->GetLayout(), true)); + attachment_views.push_back(image->GetImageView()); } } @@ -177,9 +173,9 @@ namespace Scop } else { - for(const auto& image : render_targets) + for(NonOwningPtr image : render_targets) { - m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image.GetWidth(), .height = image.GetHeight() })); + m_framebuffers.push_back(kvfCreateFramebuffer(RenderCore::Get().GetDevice(), m_renderpass, attachment_views.data(), attachment_views.size(), { .width = image->GetWidth(), .height = image->GetHeight() })); Message("Vulkan : framebuffer created"); } } @@ -190,12 +186,12 @@ namespace Scop if(p_depth) p_depth->TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, cmd); - for(auto& image : m_attachments) + for(NonOwningPtr image : m_attachments) { - if(!image.IsInit()) + if(!image->IsInit()) continue; - if(!kvfIsDepthFormat(image.GetFormat())) - image.TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, cmd); + if(!kvfIsDepthFormat(image->GetFormat())) + image->TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, cmd); } } } diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/FinalPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/FinalPass.cpp index 697a7dd..dea1098 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/FinalPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/FinalPass.cpp @@ -1,8 +1,3 @@ -// This file is a part of Akel -// Authors : @kbz8 -// Created : 29/08/2024 -// Updated : 29/08/2024 - #include #include #include diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp index 1e95b29..7244bce 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp @@ -20,10 +20,10 @@ namespace Scop GraphicPipeline& pipeline = scene.GetPipeline(); if(pipeline.GetPipeline() == VK_NULL_HANDLE) - pipeline.Init(RenderCore::Get().GetDefaultVertexShader(), scene.GetFragmentShader(), { render_target, scene.GetDepth() }); + pipeline.Init(RenderCore::Get().GetDefaultVertexShader(), scene.GetFragmentShader(), { &render_target, &scene.GetDepth() }); VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); - pipeline.BindPipeline(cmd, renderer.GetSwapchainImageIndex(), clear_color); + pipeline.BindPipeline(cmd, 0, clear_color); for(auto& actor : scene.GetActors()) { ModelData model_data; diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/Passes.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/Passes.cpp index b2a2b9f..2b010cf 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/Passes.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/Passes.cpp @@ -25,6 +25,7 @@ namespace Scop }; EventBus::RegisterListener({ functor, "__ScopRenderPasses" }); auto extent = kvfGetSwapchainImagesSize(renderer.GetSwapchain()); + m_main_render_texture.Init({}, extent.width, extent.height); } m_forward.Pass(scene, renderer, m_main_render_texture); diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/SkyboxPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/SkyboxPass.cpp index 20886b8..f3d0668 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/SkyboxPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/SkyboxPass.cpp @@ -46,11 +46,11 @@ namespace Scop void SkyboxPass::Pass(Scene& scene, Renderer& renderer, class Texture& render_target) { if(m_pipeline.GetPipeline() == VK_NULL_HANDLE) - m_pipeline.Init(p_vertex_shader, p_fragment_shader, { render_target, scene.GetDepth() }, VK_CULL_MODE_NONE); + m_pipeline.Init(p_vertex_shader, p_fragment_shader, { &render_target, &scene.GetDepth() }, VK_CULL_MODE_NONE); std::array clear_color = { 0.0f, 0.0f, 0.0f, 1.0f }; VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); - m_pipeline.BindPipeline(cmd, renderer.GetSwapchainImageIndex(), clear_color); + m_pipeline.BindPipeline(cmd, 0, clear_color); std::array sets = { scene.GetForwardData().matrices_set->GetSet(renderer.GetCurrentFrameIndex()), p_set->GetSet(renderer.GetCurrentFrameIndex()) }; vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); m_screen_quad->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); diff --git a/ScopEngine/ThirdParty/KVF/kvf.h b/ScopEngine/ThirdParty/KVF/kvf.h index ae7d583..07e1984 100755 --- a/ScopEngine/ThirdParty/KVF/kvf.h +++ b/ScopEngine/ThirdParty/KVF/kvf.h @@ -148,6 +148,7 @@ VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkForma VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear); VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point); +VkRenderPass kvfCreateRenderPassWithSubpassDependencies(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point, VkSubpassDependency* dependencies, size_t dependencies_count); void kvfDestroyRenderPass(VkDevice device, VkRenderPass renderpass); void kvfBeginRenderPass(VkRenderPass pass, VkCommandBuffer cmd, VkFramebuffer framebuffer, VkExtent2D framebuffer_extent, VkClearValue* clears, size_t clears_count); @@ -1863,7 +1864,11 @@ VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR sw VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point) { KVF_ASSERT(device != VK_NULL_HANDLE); + return kvfCreateRenderPassWithSubpassDependencies(device, attachments, attachments_count, bind_point, NULL, 0); +} +VkRenderPass kvfCreateRenderPassWithSubpassDependencies(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point, VkSubpassDependency* dependencies, size_t dependencies_count) +{ size_t color_attachment_count = 0; size_t depth_attachment_count = 0; @@ -1918,8 +1923,8 @@ VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attac renderpass_create_info.pAttachments = attachments; renderpass_create_info.subpassCount = 1; renderpass_create_info.pSubpasses = &subpass; - renderpass_create_info.dependencyCount = 0; - renderpass_create_info.pDependencies = NULL; + renderpass_create_info.dependencyCount = dependencies_count; + renderpass_create_info.pDependencies = dependencies; VkRenderPass render_pass = VK_NULL_HANDLE; __kvfCheckVk(vkCreateRenderPass(device, &renderpass_create_info, NULL, &render_pass));