Skip to content

Commit

Permalink
fixing render passes issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Aug 29, 2024
1 parent 26e2237 commit a134aa7
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ScopEngine/Assets/Shaders/ScreenFragment.nzsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion ScopEngine/Assets/Shaders/ScreenVertex.nzsl
Original file line number Diff line number Diff line change
Expand Up @@ -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]](
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ScopEngine/Runtime/Includes/Graphics/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Scop
GraphicPipeline() = default;

void Init(std::shared_ptr<Shader> vertex_shader, std::shared_ptr<Shader> fragment_shader, NonOwningPtr<class Renderer> renderer, NonOwningPtr<DepthImage> depth, VkCullModeFlagBits culling = VK_CULL_MODE_FRONT_BIT, bool disable_vertex_inputs = false);
void Init(std::shared_ptr<Shader> vertex_shader, std::shared_ptr<Shader> fragment_shader, std::vector<Image> attachments, VkCullModeFlagBits culling = VK_CULL_MODE_FRONT_BIT, bool disable_vertex_inputs = false);
void Init(std::shared_ptr<Shader> vertex_shader, std::shared_ptr<Shader> fragment_shader, std::vector<NonOwningPtr<Image>> 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<float, 4> clear) noexcept;
void EndPipeline(VkCommandBuffer command_buffer) noexcept override;
void Destroy() noexcept;
Expand All @@ -31,14 +31,14 @@ namespace Scop
~GraphicPipeline() = default;

private:
void CreateFramebuffers(const std::vector<Image>& render_targets);
void CreateFramebuffers(const std::vector<NonOwningPtr<Image>>& render_targets);
void TransitionAttachments(VkCommandBuffer cmd = VK_NULL_HANDLE);

// Private override to remove access
bool BindPipeline(VkCommandBuffer) noexcept override { return false; };

private:
std::vector<Image> m_attachments;
std::vector<NonOwningPtr<Image>> m_attachments;
std::vector<VkFramebuffer> m_framebuffers;
std::vector<VkClearValue> m_clears;
std::shared_ptr<Shader> p_vertex_shader;
Expand Down
2 changes: 1 addition & 1 deletion ScopEngine/Runtime/Sources/Graphics/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VkDescriptorSet, 2> 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);
Expand Down
2 changes: 1 addition & 1 deletion ScopEngine/Runtime/Sources/Renderer/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 13 additions & 17 deletions ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Image>{}, culling, disable_vertex_inputs);
Init(std::move(vertex_shader), std::move(fragment_shader), std::vector<NonOwningPtr<Image>>{}, culling, disable_vertex_inputs);
}

void GraphicPipeline::Init(std::shared_ptr<Shader> vertex_shader, std::shared_ptr<Shader> fragment_shader, std::vector<Image> attachments, VkCullModeFlagBits culling, bool disable_vertex_inputs)
void GraphicPipeline::Init(std::shared_ptr<Shader> vertex_shader, std::shared_ptr<Shader> fragment_shader, std::vector<NonOwningPtr<Image>> attachments, VkCullModeFlagBits culling, bool disable_vertex_inputs)
{
if(!vertex_shader || !fragment_shader)
FatalError("Vulkan : invalid shaders");
Expand Down Expand Up @@ -75,11 +75,7 @@ namespace Scop
bool GraphicPipeline::BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array<float, 4> 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{};
Expand Down Expand Up @@ -137,7 +133,7 @@ namespace Scop
Message("Vulkan : graphics pipeline destroyed");
}

void GraphicPipeline::CreateFramebuffers(const std::vector<Image>& render_targets)
void GraphicPipeline::CreateFramebuffers(const std::vector<NonOwningPtr<Image>>& render_targets)
{
std::vector<VkAttachmentDescription> attachments;
std::vector<VkImageView> attachment_views;
Expand All @@ -148,10 +144,10 @@ namespace Scop
}
else
{
for(const auto& image : render_targets)
for(NonOwningPtr<Image> 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());
}
}

Expand All @@ -177,9 +173,9 @@ namespace Scop
}
else
{
for(const auto& image : render_targets)
for(NonOwningPtr<Image> 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");
}
}
Expand All @@ -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> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// This file is a part of Akel
// Authors : @kbz8
// Created : 29/08/2024
// Updated : 29/08/2024

#include <Renderer/RenderPasses/FinalPass.h>
#include <Renderer/Pipelines/Graphics.h>
#include <Renderer/Renderer.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, 4> 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<VkDescriptorSet, 2> 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());
Expand Down
9 changes: 7 additions & 2 deletions ScopEngine/ThirdParty/KVF/kvf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit a134aa7

Please sign in to comment.