adding depth buffer, ci skip

This commit is contained in:
Kbz-8
2024-09-02 13:02:32 +02:00
parent d5eeef9559
commit b7d554553b
26 changed files with 217 additions and 192 deletions

View File

@@ -138,4 +138,10 @@ namespace mlx
}
vkUpdateDescriptorSets(RenderCore::Get().GetDevice(), writes.size(), writes.data(), 0, nullptr);
}
void Descriptor::Reallocate() noexcept
{
for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
m_set[i] = kvfAllocateDescriptorSet(RenderCore::Get().GetDevice(), m_set_layout);
}
}

View File

@@ -5,7 +5,7 @@
namespace mlx
{
void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, bool is_multisampled)
void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled)
{
m_type = type;
m_width = width;
@@ -55,6 +55,8 @@ namespace mlx
switch(m_type)
{
case ImageType::Color: kvf_type = KVF_IMAGE_COLOR; break;
case ImageType::Depth: kvf_type = KVF_IMAGE_DEPTH; break;
default: break;
}
kvfTransitionImageLayout(RenderCore::Get().GetDevice(), m_image, kvf_type, cmd, m_format, m_layout, new_layout, is_single_time_cmd_buffer);

View File

@@ -16,6 +16,7 @@ namespace Scop
p_vertex_shader = descriptor.vertex_shader;
p_fragment_shader = descriptor.fragment_shader;
p_renderer = descriptor.renderer;
p_depth = descriptor.depth;
std::vector<VkPushConstantRange> push_constants;
std::vector<VkDescriptorSetLayout> set_layouts;
@@ -37,7 +38,10 @@ namespace Scop
kvfGPipelineBuilderSetInputTopology(builder, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
kvfGPipelineBuilderSetCullMode(builder, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
kvfGPipelineBuilderEnableAlphaBlending(builder);
kvfGPipelineBuilderDisableDepthTest(builder);
if(p_depth)
kvfGPipelineBuilderEnableDepthTest(builder, (descriptor.depth_test_equal ? VK_COMPARE_OP_EQUAL : VK_COMPARE_OP_LESS), true);
else
kvfGPipelineBuilderDisableDepthTest(builder);
kvfGPipelineBuilderSetPolygonMode(builder, VK_POLYGON_MODE_FILL, 1.0f);
if(features.sampleRateShading)
kvfGPipelineBuilderSetMultisamplingShading(builder, VK_SAMPLE_COUNT_1_BIT, 0.25f);
@@ -84,6 +88,9 @@ namespace Scop
m_clears[i].color.float32[3] = clear[3];
}
if(p_depth)
m_clears.back().depthStencil = VkClearDepthStencilValue{ 1.0f, 0 };
kvfBeginRenderPass(m_renderpass, command_buffer, fb, fb_extent, m_clears.data(), m_clears.size());
vkCmdBindPipeline(command_buffer, GetPipelineBindPoint(), GetPipeline());
return true;
@@ -127,10 +134,16 @@ namespace Scop
for(NonOwningPtr<Texture> image : render_targets)
{
attachments.push_back(kvfBuildAttachmentDescription(KVF_IMAGE_COLOR, image->GetFormat(), image->GetLayout(), image->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT));
attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(image->GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), image->GetFormat(), image->GetLayout(), image->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT));
attachment_views.push_back(image->GetImageView());
}
if(p_depth)
{
attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(p_depth->GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), p_depth->GetFormat(), p_depth->GetLayout(), p_depth->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT));
attachment_views.push_back(p_depth->GetImageView());
}
m_renderpass = kvfCreateRenderPass(RenderCore::Get().GetDevice(), attachments.data(), attachments.size(), GetPipelineBindPoint());
m_clears.clear();
m_clears.resize(attachments.size());
@@ -154,6 +167,9 @@ namespace Scop
void GraphicPipeline::TransitionAttachments(VkCommandBuffer cmd)
{
if(p_depth)
p_depth->TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, cmd);
for(NonOwningPtr<Texture> image : m_attachments)
{
if(!image->IsInit())

View File

@@ -11,7 +11,7 @@ namespace mlx
struct SpriteData
{
Vec4f color;
Vec2f position;
Vec4f position;
};
void Render2DPass::Init()
@@ -43,10 +43,20 @@ namespace mlx
};
p_fragment_shader = std::make_shared<Shader>(fragment_shader, ShaderType::Fragment, std::move(fragment_shader_layout));
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
func::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
if(event.What() == Event::ResizeEventCode)
m_pipeline.Destroy();
if(event.What() == Event::DescriptorPoolResetEventCode)
{
p_texture_set->Reallocate();
p_viewer_data_set.Reallocate();
for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
{
p_viewer_data_set->SetUniformBuffer(i, 0, p_viewer_data_buffer->Get(i));
p_viewer_data_set->Update(i);
}
}
};
EventBus::RegisterListener({ functor, "__ScopRender2DPass" });
@@ -70,6 +80,7 @@ namespace mlx
pipeline_descriptor.vertex_shader = p_vertex_shader;
pipeline_descriptor.fragment_shader = p_fragment_shader;
pipeline_descriptor.color_attachments = { &render_target };
pipeline_descriptor.depth = scene.GetDepth();
pipeline_descriptor.clear_color_attachments = false;
m_pipeline.Init(pipeline_descriptor);
}
@@ -87,7 +98,7 @@ namespace mlx
for(auto sprite : scene.GetSprites())
{
SpriteData sprite_data;
sprite_data.position = Vec2f{ static_cast<float>(sprite->GetPosition().x), static_cast<float>(sprite->GetPosition().y) };
sprite_data.position = Vec4f{ sprite->GetPosition(), 1.0f };
sprite_data.color = sprite->GetColor();
if(!sprite->IsSetInit())
sprite->UpdateDescriptorSet(*p_texture_set);

View File

@@ -30,10 +30,12 @@ namespace mlx
};
p_fragment_shader = std::make_shared<Shader>(fragment_shader_code, ShaderType::Fragment, std::move(fragment_shader_layout));
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
func::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
if(event.What() == Event::ResizeEventCode)
m_pipeline.Destroy();
if(event.What() == Event::DescriptorPoolResetEventCode)
p_set->Reallocate();
};
EventBus::RegisterListener({ functor, "__ScopFinalPass" });

View File

@@ -15,7 +15,7 @@ namespace mlx
{
if(!m_main_render_texture.IsInit())
{
std::function<void(const EventBase&)> functor = [this, renderer](const EventBase& event)
func::function<void(const EventBase&)> functor = [this, renderer](const EventBase& event)
{
if(event.What() == Event::ResizeEventCode)
{
@@ -31,6 +31,7 @@ namespace mlx
}
m_main_render_texture.Clear(renderer.GetActiveCommandBuffer(), Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f });
scene.GetDepth().Clear(renderer.GetActiveCommandBuffer(), {});
m_2Dpass.Pass(scene, renderer, m_main_render_texture);
m_final.Pass(scene, renderer, m_main_render_texture);

View File

@@ -18,11 +18,16 @@ namespace mlx
{
Event What() const override { return Event::FrameBeginEventCode; }
};
struct DescriptorPoolResetEventBroadcast : public EventBase
{
Event What() const override { return Event::DescriptorPoolResetEventCode; }
};
}
void Renderer::Init(NonOwningPtr<Window> window)
{
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
func::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
if(event.What() == Event::ResizeEventCode)
this->RequireFramebufferResize();
@@ -86,6 +91,7 @@ namespace mlx
}
m_current_frame_index = (m_current_frame_index + 1) % MAX_FRAMES_IN_FLIGHT;
kvfResetDeviceDescriptorPools(RenderCore::Get().GetDevice());
EventBus::SendBroadcast(Internal::DescriptorPoolResetEventBroadcast{});
}
void Renderer::CreateSwapchain()