HostDisplay: Blit before post processing, not after

Should be a tiny performance boost on tilers.
This commit is contained in:
Connor McLaughlin 2022-10-13 19:57:14 +10:00
parent adf41b9bbd
commit 2c867bc3df
3 changed files with 40 additions and 50 deletions

View file

@ -21,6 +21,8 @@ Log_SetChannel(D3D11HostDisplay);
#pragma comment(lib, "d3d11.lib") #pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib") #pragma comment(lib, "dxgi.lib")
static constexpr std::array<float, 4> s_clear_color = {};
D3D11HostDisplay::D3D11HostDisplay() = default; D3D11HostDisplay::D3D11HostDisplay() = default;
D3D11HostDisplay::~D3D11HostDisplay() D3D11HostDisplay::~D3D11HostDisplay()
@ -706,10 +708,6 @@ bool D3D11HostDisplay::Render(bool skip_present)
if (m_vsync && m_gpu_timing_enabled) if (m_vsync && m_gpu_timing_enabled)
PopTimestampQuery(); PopTimestampQuery();
static constexpr std::array<float, 4> clear_color = {};
m_context->ClearRenderTargetView(m_swap_chain_rtv.Get(), clear_color.data());
m_context->OMSetRenderTargets(1, m_swap_chain_rtv.GetAddressOf(), nullptr);
RenderDisplay(); RenderDisplay();
if (ImGui::GetCurrentContext()) if (ImGui::GetCurrentContext())
@ -746,7 +744,7 @@ bool D3D11HostDisplay::RenderScreenshot(u32 width, u32 height, std::vector<u32>*
if (HasDisplayTexture()) if (HasDisplayTexture())
{ {
const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height, 0); const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height);
if (!m_post_processing_chain.IsEmpty()) if (!m_post_processing_chain.IsEmpty())
{ {
@ -783,12 +781,9 @@ void D3D11HostDisplay::RenderImGui()
void D3D11HostDisplay::RenderDisplay() void D3D11HostDisplay::RenderDisplay()
{ {
if (!HasDisplayTexture())
return;
const auto [left, top, width, height] = CalculateDrawRect(GetWindowWidth(), GetWindowHeight()); const auto [left, top, width, height] = CalculateDrawRect(GetWindowWidth(), GetWindowHeight());
if (!m_post_processing_chain.IsEmpty()) if (HasDisplayTexture() && !m_post_processing_chain.IsEmpty())
{ {
ApplyPostProcessingChain(m_swap_chain_rtv.Get(), left, top, width, height, ApplyPostProcessingChain(m_swap_chain_rtv.Get(), left, top, width, height,
static_cast<D3D11::Texture*>(m_display_texture), m_display_texture_view_x, static_cast<D3D11::Texture*>(m_display_texture), m_display_texture_view_x,
@ -797,6 +792,12 @@ void D3D11HostDisplay::RenderDisplay()
return; return;
} }
m_context->ClearRenderTargetView(m_swap_chain_rtv.Get(), s_clear_color.data());
m_context->OMSetRenderTargets(1, m_swap_chain_rtv.GetAddressOf(), nullptr);
if (!HasDisplayTexture())
return;
RenderDisplay(left, top, width, height, static_cast<D3D11::Texture*>(m_display_texture), m_display_texture_view_x, RenderDisplay(left, top, width, height, static_cast<D3D11::Texture*>(m_display_texture), m_display_texture_view_x,
m_display_texture_view_y, m_display_texture_view_width, m_display_texture_view_height, m_display_texture_view_y, m_display_texture_view_width, m_display_texture_view_height,
IsUsingLinearFiltering()); IsUsingLinearFiltering());
@ -1048,8 +1049,6 @@ void D3D11HostDisplay::ApplyPostProcessingChain(ID3D11RenderTargetView* final_ta
s32 texture_view_x, s32 texture_view_y, s32 texture_view_width, s32 texture_view_x, s32 texture_view_y, s32 texture_view_width,
s32 texture_view_height, u32 target_width, u32 target_height) s32 texture_view_height, u32 target_width, u32 target_height)
{ {
static constexpr std::array<float, 4> clear_color = {0.0f, 0.0f, 0.0f, 1.0f};
if (!CheckPostProcessingRenderTargets(target_width, target_height)) if (!CheckPostProcessingRenderTargets(target_width, target_height))
{ {
RenderDisplay(final_left, final_top, final_width, final_height, texture, texture_view_x, texture_view_y, RenderDisplay(final_left, final_top, final_width, final_height, texture, texture_view_x, texture_view_y,
@ -1058,7 +1057,7 @@ void D3D11HostDisplay::ApplyPostProcessingChain(ID3D11RenderTargetView* final_ta
} }
// downsample/upsample - use same viewport for remainder // downsample/upsample - use same viewport for remainder
m_context->ClearRenderTargetView(m_post_processing_input_texture.GetD3DRTV(), clear_color.data()); m_context->ClearRenderTargetView(m_post_processing_input_texture.GetD3DRTV(), s_clear_color.data());
m_context->OMSetRenderTargets(1, m_post_processing_input_texture.GetD3DRTVArray(), nullptr); m_context->OMSetRenderTargets(1, m_post_processing_input_texture.GetD3DRTVArray(), nullptr);
RenderDisplay(final_left, final_top, final_width, final_height, texture, texture_view_x, texture_view_y, RenderDisplay(final_left, final_top, final_width, final_height, texture, texture_view_x, texture_view_y,
texture_view_width, texture_view_height, IsUsingLinearFiltering()); texture_view_width, texture_view_height, IsUsingLinearFiltering());
@ -1075,15 +1074,9 @@ void D3D11HostDisplay::ApplyPostProcessingChain(ID3D11RenderTargetView* final_ta
for (u32 i = 0; i < static_cast<u32>(m_post_processing_stages.size()); i++) for (u32 i = 0; i < static_cast<u32>(m_post_processing_stages.size()); i++)
{ {
PostProcessingStage& pps = m_post_processing_stages[i]; PostProcessingStage& pps = m_post_processing_stages[i];
if (i == final_stage) ID3D11RenderTargetView* rtv = (i == final_stage) ? final_target : pps.output_texture.GetD3DRTV();
{ m_context->ClearRenderTargetView(rtv, s_clear_color.data());
m_context->OMSetRenderTargets(1, &final_target, nullptr); m_context->OMSetRenderTargets(1, &rtv, nullptr);
}
else
{
m_context->ClearRenderTargetView(pps.output_texture.GetD3DRTV(), clear_color.data());
m_context->OMSetRenderTargets(1, pps.output_texture.GetD3DRTVArray(), nullptr);
}
m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); m_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_context->VSSetShader(pps.vertex_shader.Get(), nullptr, 0); m_context->VSSetShader(pps.vertex_shader.Get(), nullptr, 0);

View file

@ -620,9 +620,7 @@ bool OpenGLHostDisplay::Render(bool skip_present)
} }
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
RenderDisplay(); RenderDisplay();
@ -652,22 +650,23 @@ bool OpenGLHostDisplay::RenderScreenshot(u32 width, u32 height, std::vector<u32>
} }
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
texture.BindFramebuffer(GL_FRAMEBUFFER);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if (HasDisplayTexture()) const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height);
if (HasDisplayTexture() && !m_post_processing_chain.IsEmpty())
{ {
const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height, 0); ApplyPostProcessingChain(texture.GetGLFramebufferID(), left, height - top - draw_height, draw_width, draw_height,
static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x,
m_display_texture_view_y, m_display_texture_view_width, m_display_texture_view_height,
width, height);
}
else
{
texture.BindFramebuffer(GL_FRAMEBUFFER);
glClear(GL_COLOR_BUFFER_BIT);
if (!m_post_processing_chain.IsEmpty()) if (HasDisplayTexture())
{
ApplyPostProcessingChain(texture.GetGLFramebufferID(), left, height - top - draw_height, draw_width, draw_height,
static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x,
m_display_texture_view_y, m_display_texture_view_width, m_display_texture_view_height,
width, height);
}
else
{ {
RenderDisplay(left, height - top - draw_height, draw_width, draw_height, RenderDisplay(left, height - top - draw_height, draw_width, draw_height,
static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x, m_display_texture_view_y, static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x, m_display_texture_view_y,
@ -693,12 +692,9 @@ void OpenGLHostDisplay::RenderImGui()
void OpenGLHostDisplay::RenderDisplay() void OpenGLHostDisplay::RenderDisplay()
{ {
if (!HasDisplayTexture())
return;
const auto [left, top, width, height] = CalculateDrawRect(GetWindowWidth(), GetWindowHeight()); const auto [left, top, width, height] = CalculateDrawRect(GetWindowWidth(), GetWindowHeight());
if (!m_post_processing_chain.IsEmpty()) if (HasDisplayTexture() && !m_post_processing_chain.IsEmpty())
{ {
ApplyPostProcessingChain(0, left, GetWindowHeight() - top - height, width, height, ApplyPostProcessingChain(0, left, GetWindowHeight() - top - height, width, height,
static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x, static_cast<GL::Texture*>(m_display_texture), m_display_texture_view_x,
@ -707,6 +703,12 @@ void OpenGLHostDisplay::RenderDisplay()
return; return;
} }
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT);
if (!HasDisplayTexture())
return;
RenderDisplay(left, GetWindowHeight() - top - height, width, height, static_cast<GL::Texture*>(m_display_texture), RenderDisplay(left, GetWindowHeight() - top - height, width, height, static_cast<GL::Texture*>(m_display_texture),
m_display_texture_view_x, m_display_texture_view_y, m_display_texture_view_width, m_display_texture_view_x, m_display_texture_view_y, m_display_texture_view_width,
m_display_texture_view_height, IsUsingLinearFiltering()); m_display_texture_view_height, IsUsingLinearFiltering());
@ -877,6 +879,7 @@ bool OpenGLHostDisplay::SetPostProcessingChain(const std::string_view& config)
m_post_processing_ubo->Unbind(); m_post_processing_ubo->Unbind();
} }
m_post_processing_timer.Reset();
return true; return true;
} }
@ -924,7 +927,7 @@ void OpenGLHostDisplay::ApplyPostProcessingChain(GLuint final_target, s32 final_
} }
// downsample/upsample - use same viewport for remainder // downsample/upsample - use same viewport for remainder
m_post_processing_input_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER); m_post_processing_input_texture.BindFramebuffer(GL_FRAMEBUFFER);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
RenderDisplay(final_left, target_height - final_top - final_height, final_width, final_height, texture, RenderDisplay(final_left, target_height - final_top - final_height, final_width, final_height, texture,
texture_view_x, texture_view_y, texture_view_width, texture_view_height, IsUsingLinearFiltering()); texture_view_x, texture_view_y, texture_view_width, texture_view_height, IsUsingLinearFiltering());
@ -943,15 +946,8 @@ void OpenGLHostDisplay::ApplyPostProcessingChain(GLuint final_target, s32 final_
for (u32 i = 0; i < static_cast<u32>(m_post_processing_stages.size()); i++) for (u32 i = 0; i < static_cast<u32>(m_post_processing_stages.size()); i++)
{ {
PostProcessingStage& pps = m_post_processing_stages[i]; PostProcessingStage& pps = m_post_processing_stages[i];
if (i == final_stage) glBindFramebuffer(GL_FRAMEBUFFER, (i == final_stage) ? final_target : pps.output_texture.GetGLFramebufferID());
{ glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, final_target);
}
else
{
pps.output_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
glClear(GL_COLOR_BUFFER_BIT);
}
pps.program.Bind(); pps.program.Bind();

View file

@ -713,7 +713,7 @@ bool VulkanHostDisplay::RenderScreenshot(u32 width, u32 height, std::vector<u32>
"VulkanHostDisplay::RenderScreenshot: %ux%u", width, height); "VulkanHostDisplay::RenderScreenshot: %ux%u", width, height);
tex.TransitionToLayout(g_vulkan_context->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); tex.TransitionToLayout(g_vulkan_context->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height, 0); const auto [left, top, draw_width, draw_height] = CalculateDrawRect(width, height);
if (!m_post_processing_chain.IsEmpty()) if (!m_post_processing_chain.IsEmpty())
{ {
@ -1025,6 +1025,7 @@ bool VulkanHostDisplay::SetPostProcessingChain(const std::string_view& config)
} }
Vulkan::Util::SetObjectName(g_vulkan_context->GetDevice(), m_post_processing_ubo.GetBuffer(), Vulkan::Util::SetObjectName(g_vulkan_context->GetDevice(), m_post_processing_ubo.GetBuffer(),
"Post Processing Uniform Buffer"); "Post Processing Uniform Buffer");
m_post_processing_timer.Reset();
return true; return true;
} }