Added attachment resizing + viewport scissoring

This commit is contained in:
antopilo
2025-01-07 19:49:58 -05:00
parent d69c56bed8
commit bb4b882ed4
3 changed files with 49 additions and 4 deletions

View File

@@ -13,6 +13,11 @@ TextureAttachment::TextureAttachment(const std::string& name, ImageFormat format
Name(name),
Format(format)
{
Image = std::make_shared<VulkanImage>(format, Vector2(1280, 720));
// Create default texture I guess?
auto& gpuResources = GPUResources::Get();
gpuResources.AddTexture(Image);
}
RenderPass::RenderPass(const std::string& name) :
@@ -22,6 +27,24 @@ RenderPass::RenderPass(const std::string& name) :
void RenderPass::ClearAttachments(PassRenderContext& ctx)
{
// Resize
for(auto& attachment : Attachments)
{
if (attachment.Image->GetSize() == ctx.resolution)
{
continue;
}
auto newAttachment = std::make_shared<VulkanImage>(attachment.Format, ctx.resolution);
attachment.Image = newAttachment;
// Register to resource manager
auto& gpuResources = GPUResources::Get();
gpuResources.AddTexture(newAttachment);
// TODO: Queue deletion of old textures
}
// Clear all color attachments
VkClearColorValue clearValue = { { 0.0f, 0.0f, 0.0f, 1.0f } };
VkImageSubresourceRange clearRange = VulkanInit::ImageSubResourceRange(VK_IMAGE_ASPECT_COLOR_BIT);
@@ -52,6 +75,7 @@ void RenderPass::Render(PassRenderContext& ctx)
PreRender(ctx);
}
// Begin rendering and bind pipeline
std::vector<VkRenderingAttachmentInfo> renderAttachmentInfos;
renderAttachmentInfos.reserve(Attachments.size());
@@ -73,6 +97,24 @@ void RenderPass::Render(PassRenderContext& ctx)
{
vkCmdBindPipeline(ctx.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline);
VkViewport viewport = {};
viewport.x = 0;
viewport.y = 0;
viewport.width = ctx.resolution.x;
viewport.height = ctx.resolution.y;
viewport.minDepth = 0.f;
viewport.maxDepth = 1.f;
vkCmdSetViewport(ctx.commandBuffer, 0, 1, &viewport);
VkRect2D scissor = {};
scissor.offset.x = 0;
scissor.offset.y = 0;
scissor.extent.width = ctx.resolution.x;
scissor.extent.height = ctx.resolution.y;
vkCmdSetScissor(ctx.commandBuffer, 0, 1, &scissor);
if (RenderCb)
{
RenderCb(ctx);

View File

@@ -11,6 +11,7 @@ namespace Nuake
{
Ref<Scene> CurrentScene; // The scene we are rendering
VkCommandBuffer CommandBuffer; // The command buffer we are recording into
Vector2 Size;
// ...
// We might add more to this!
};

View File

@@ -109,7 +109,8 @@ void VkSceneRenderer::Init()
void VkSceneRenderer::BeginScene(RenderContext inContext)
{
Context = inContext;
Context.CommandBuffer = inContext.CommandBuffer;
Context.CurrentScene = inContext.CurrentScene;
// Collect all global transform of things we will render
BuildMatrixBuffer();
@@ -129,7 +130,7 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
throw std::runtime_error("Draw image is not initialized");
}
PassRenderContext passCtx = { inContext.CurrentScene, inContext.CommandBuffer };
PassRenderContext passCtx = { inContext.CurrentScene, inContext.CommandBuffer, Context.Size };
GBufferPipeline.Execute(passCtx);
// Ensure the pipeline is valid
@@ -251,7 +252,6 @@ void VkSceneRenderer::DrawScene()
for (auto e : view)
{
auto [transform, mesh, visibility] = view.get<TransformComponent, ModelComponent, VisibilityComponent>(e);
if (!mesh.ModelResource || !visibility.Visible)
{
continue;
@@ -290,7 +290,6 @@ void VkSceneRenderer::DrawScene()
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, BasicPipelineLayout, 3, 1, &imageSet, 0, nullptr);
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, BasicPipelineLayout, 4, 1, &SamplerDescriptor, 0, nullptr);
modelPushConstant.Index = ModelMatrixMapping[entity.GetID()];
modelPushConstant.MaterialIndex = MeshMaterialMapping[vkMesh->GetID()];
@@ -506,11 +505,14 @@ void VkSceneRenderer::CreatePipelines()
gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth);
gBufferPass.SetPushConstant<ModelPushConstant>(modelPushConstant);
gBufferPass.SetPreRender([](PassRenderContext& ctx) {});
GBufferPipeline.Build();
}
void VkSceneRenderer::SetGBufferSize(const Vector2& size)
{
Context.Size = size;
GBufferAlbedo = CreateRef<VulkanImage>(ImageFormat::RGBA16F, size);
GBufferDepthImage = CreateRef<VulkanImage>(ImageFormat::D32F, size, ImageUsage::Depth);
GBufferNormal = CreateRef<VulkanImage>(ImageFormat::RGBA16F, size);