From a7382ca9d00e0a29cda15b222d2abdd7974e2bcd Mon Sep 17 00:00:00 2001 From: "James R." Date: Sat, 30 Sep 2023 19:21:53 -0700 Subject: [PATCH] UpscaleBackbuffer::begin_pass: use separate renderpass to clear framebuffer if texture was recreated Fixes wipes potentially reading invalid data from the framebuffer if the texture was recreated but not yet rendered to. --- src/hwr2/upscale_backbuffer.cpp | 36 ++++++++++++++++++++++----------- src/hwr2/upscale_backbuffer.hpp | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/hwr2/upscale_backbuffer.cpp b/src/hwr2/upscale_backbuffer.cpp index 0d3666e5c..f7e187388 100644 --- a/src/hwr2/upscale_backbuffer.cpp +++ b/src/hwr2/upscale_backbuffer.cpp @@ -37,6 +37,19 @@ void UpscaleBackbuffer::begin_pass(Rhi& rhi, Handle ctx) remake = true; } + auto new_renderpass = [&rhi = rhi](AttachmentLoadOp load_op, AttachmentStoreOp store_op) + { + RenderPassDesc desc {}; + desc.use_depth_stencil = true; + desc.color_load_op = load_op; + desc.color_store_op = store_op; + desc.depth_load_op = load_op; + desc.depth_store_op = store_op; + desc.stencil_load_op = load_op; + desc.stencil_store_op = store_op; + return rhi.create_render_pass(desc); + }; + if (remake) { if (color_) @@ -63,23 +76,22 @@ void UpscaleBackbuffer::begin_pass(Rhi& rhi, Handle ctx) depth_tex.height = vid_height; depth_ = rhi.create_renderbuffer(depth_tex); - } - if (!renderpass_) + if (!renderpass_clear_) + { + renderpass_clear_ = new_renderpass(AttachmentLoadOp::kClear, AttachmentStoreOp::kStore); + } + } + else { - RenderPassDesc desc {}; - desc.use_depth_stencil = true; - desc.color_load_op = AttachmentLoadOp::kLoad; - desc.color_store_op = AttachmentStoreOp::kStore; - desc.depth_load_op = AttachmentLoadOp::kLoad; - desc.depth_store_op = AttachmentStoreOp::kStore; - desc.stencil_load_op = AttachmentLoadOp::kLoad; - desc.stencil_store_op = AttachmentStoreOp::kStore; - renderpass_ = rhi.create_render_pass(desc); + if (!renderpass_) + { + renderpass_ = new_renderpass(AttachmentLoadOp::kLoad, AttachmentStoreOp::kStore); + } } RenderPassBeginInfo begin_info {}; - begin_info.render_pass = renderpass_; + begin_info.render_pass = remake ? renderpass_clear_ : renderpass_; begin_info.clear_color = {0, 0, 0, 1}; begin_info.color_attachment = color_; begin_info.depth_stencil_attachment = depth_; diff --git a/src/hwr2/upscale_backbuffer.hpp b/src/hwr2/upscale_backbuffer.hpp index d7b34a276..0f6903dd1 100644 --- a/src/hwr2/upscale_backbuffer.hpp +++ b/src/hwr2/upscale_backbuffer.hpp @@ -20,6 +20,7 @@ class UpscaleBackbuffer rhi::Handle color_; rhi::Handle depth_; rhi::Handle renderpass_; + rhi::Handle renderpass_clear_; public: UpscaleBackbuffer();