Refactor ClearRegion

And fix bug where opengl was getting the wrong coordinates
This commit is contained in:
Scott Mansell 2023-01-31 15:44:38 +13:00
parent b753641dd4
commit e009002411
17 changed files with 77 additions and 82 deletions

View File

@ -103,7 +103,7 @@ void Gfx::WaitForGPUIdle()
ExecuteCommandList(true); ExecuteCommandList(true);
} }
void Gfx::ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void Gfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z)
{ {
// Use a fast path without the shader if both color/alpha are enabled. // Use a fast path without the shader if both color/alpha are enabled.
@ -145,7 +145,7 @@ void Gfx::ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectan
// Anything left over, fall back to clear triangle. // Anything left over, fall back to clear triangle.
if (color_enable || alpha_enable || z_enable) if (color_enable || alpha_enable || z_enable)
::AbstractGfx::ClearRegion(rc, target_rc, color_enable, alpha_enable, z_enable, color, z); ::AbstractGfx::ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
} }
void Gfx::SetPipeline(const AbstractPipeline* pipeline) void Gfx::SetPipeline(const AbstractPipeline* pipeline)

View File

@ -47,7 +47,7 @@ public:
void Flush() override; void Flush() override;
void WaitForGPUIdle() override; void WaitForGPUIdle() override;
void ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override; bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override;
void SetPipeline(const AbstractPipeline* pipeline) override; void SetPipeline(const AbstractPipeline* pipeline) override;

View File

@ -47,7 +47,7 @@ public:
void WaitForGPUIdle() override; void WaitForGPUIdle() override;
void OnConfigChanged(u32 bits) override; void OnConfigChanged(u32 bits) override;
void ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override; bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override;
void SetPipeline(const AbstractPipeline* pipeline) override; void SetPipeline(const AbstractPipeline* pipeline) override;

View File

@ -285,8 +285,7 @@ void Metal::Gfx::OnConfigChanged(u32 bits)
} }
} }
void Metal::Gfx::ClearRegion(const MathUtil::Rectangle<int>& rc, void Metal::Gfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc,
const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z)
{ {
u32 framebuffer_width = m_current_framebuffer->GetWidth(); u32 framebuffer_width = m_current_framebuffer->GetWidth();
@ -332,7 +331,7 @@ void Metal::Gfx::ClearRegion(const MathUtil::Rectangle<int>& rc,
} }
g_state_tracker->EnableEncoderLabel(false); g_state_tracker->EnableEncoderLabel(false);
g_framebuffer_manager->ClearEFB(rc, color_enable, alpha_enable, z_enable, color, z); AbstractGfx::ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
g_state_tracker->EnableEncoderLabel(true); g_state_tracker->EnableEncoderLabel(true);
} }

View File

@ -373,8 +373,7 @@ void OGLGfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const Clea
glDepthMask(m_current_depth_state.updateenable); glDepthMask(m_current_depth_state.updateenable);
} }
void OGLGfx::ClearRegion(const MathUtil::Rectangle<int>& rc, void OGLGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
bool alphaEnable, bool zEnable, u32 color, u32 z) bool alphaEnable, bool zEnable, u32 color, u32 z)
{ {
u32 clear_mask = 0; u32 clear_mask = 0;

View File

@ -42,7 +42,7 @@ public:
void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override; void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override;
void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {}, void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {},
float depth_value = 0.0f) override; float depth_value = 0.0f) override;
void ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool colorEnable, bool alphaEnable, bool zEnable, u32 color, u32 z) override; bool colorEnable, bool alphaEnable, bool zEnable, u32 color, u32 z) override;
void SetScissorRect(const MathUtil::Rectangle<int>& rc) override; void SetScissorRect(const MathUtil::Rectangle<int>& rc) override;
void SetTexture(u32 index, const AbstractTexture* texture) override; void SetTexture(u32 index, const AbstractTexture* texture) override;

View File

@ -107,8 +107,7 @@ void SWGfx::ShowImage(const AbstractTexture* source_texture,
m_window->ShowImage(source_texture, source_rc); m_window->ShowImage(source_texture, source_rc);
} }
void SWGfx::ClearRegion(const MathUtil::Rectangle<int>& rc, void SWGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
bool alphaEnable, bool zEnable, u32 color, u32 z) bool alphaEnable, bool zEnable, u32 color, u32 z)
{ {
EfbCopy::ClearEfb(); EfbCopy::ClearEfb();

View File

@ -46,7 +46,7 @@ public:
void SetScissorRect(const MathUtil::Rectangle<int>& rc) override; void SetScissorRect(const MathUtil::Rectangle<int>& rc) override;
void ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool colorEnable, bool alphaEnable, bool zEnable, u32 color, u32 z) override; bool colorEnable, bool alphaEnable, bool zEnable, u32 color, u32 z) override;
private: private:

View File

@ -100,8 +100,7 @@ void VKGfx::SetPipeline(const AbstractPipeline* pipeline)
StateTracker::GetInstance()->SetPipeline(static_cast<const VKPipeline*>(pipeline)); StateTracker::GetInstance()->SetPipeline(static_cast<const VKPipeline*>(pipeline));
} }
void VKGfx::ClearRegion(const MathUtil::Rectangle<int>& rc, void VKGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool color_enable,
const MathUtil::Rectangle<int>& target_rc, bool color_enable,
bool alpha_enable, bool z_enable, u32 color, u32 z) bool alpha_enable, bool z_enable, u32 color, u32 z)
{ {
VkRect2D target_vk_rc = { VkRect2D target_vk_rc = {
@ -191,7 +190,7 @@ void VKGfx::ClearRegion(const MathUtil::Rectangle<int>& rc,
if (!color_enable && !alpha_enable && !z_enable) if (!color_enable && !alpha_enable && !z_enable)
return; return;
g_framebuffer_manager->ClearEFB(rc, color_enable, alpha_enable, z_enable, color, z); AbstractGfx::ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
} }
void VKGfx::Flush() void VKGfx::Flush()

View File

@ -53,7 +53,7 @@ public:
void WaitForGPUIdle() override; void WaitForGPUIdle() override;
void OnConfigChanged(u32 bits) override; void OnConfigChanged(u32 bits) override;
void ClearRegion(const MathUtil::Rectangle<int>& rc, const MathUtil::Rectangle<int>& target_rc, void ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override; bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z) override;
void SetPipeline(const AbstractPipeline* pipeline) override; void SetPipeline(const AbstractPipeline* pipeline) override;

View File

@ -54,11 +54,35 @@ void AbstractGfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer,
m_current_framebuffer = framebuffer; m_current_framebuffer = framebuffer;
} }
void AbstractGfx::ClearRegion(const MathUtil::Rectangle<int>& rc, void AbstractGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
bool alphaEnable, bool zEnable, u32 color, u32 z) bool alphaEnable, bool zEnable, u32 color, u32 z)
{ {
g_framebuffer_manager->ClearEFB(rc, colorEnable, alphaEnable, zEnable, color, z); // This is a generic fallback for any ClearRegion operations that backends don't support.
// It simply draws a Quad.
BeginUtilityDrawing();
// Set up uniforms.
struct Uniforms
{
float clear_color[4];
float clear_depth;
float padding1, padding2, padding3;
};
static_assert(std::is_standard_layout<Uniforms>::value);
Uniforms uniforms = {{static_cast<float>((color >> 16) & 0xFF) / 255.0f,
static_cast<float>((color >> 8) & 0xFF) / 255.0f,
static_cast<float>((color >> 0) & 0xFF) / 255.0f,
static_cast<float>((color >> 24) & 0xFF) / 255.0f},
static_cast<float>(z & 0xFFFFFF) / 16777216.0f};
if (!g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
uniforms.clear_depth = 1.0f - uniforms.clear_depth;
g_vertex_manager->UploadUtilityUniforms(&uniforms, sizeof(uniforms));
g_gfx->SetPipeline(g_framebuffer_manager->GetClearPipeline(colorEnable, alphaEnable, zEnable));
g_gfx->SetViewportAndScissor(target_rc);
g_gfx->Draw(0, 3);
EndUtilityDrawing();
} }
void AbstractGfx::SetViewportAndScissor(const MathUtil::Rectangle<int>& rect, float min_depth, void AbstractGfx::SetViewportAndScissor(const MathUtil::Rectangle<int>& rect, float min_depth,

View File

@ -82,8 +82,7 @@ public:
virtual void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, virtual void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer,
const ClearColor& color_value = {}, float depth_value = 0.0f); const ClearColor& color_value = {}, float depth_value = 0.0f);
virtual void ClearRegion(const MathUtil::Rectangle<int>& rc, virtual void ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
bool alphaEnable, bool zEnable, u32 color, u32 z); bool alphaEnable, bool zEnable, u32 color, u32 z);
// Drawing with currently-bound pipeline state. // Drawing with currently-bound pipeline state.

View File

@ -342,7 +342,7 @@ void ClearScreen(const MathUtil::Rectangle<int>& rc)
color = RGBA8ToRGB565ToRGBA8(color); color = RGBA8ToRGB565ToRGBA8(color);
z = Z24ToZ16ToZ24(z); z = Z24ToZ16ToZ24(z);
} }
g_renderer->ClearScreen(rc, colorEnable, alphaEnable, zEnable, color, z); g_framebuffer_manager->ClearEFB(rc, colorEnable, alphaEnable, zEnable, color, z);
} }
} }

View File

@ -16,6 +16,7 @@
#include "VideoCommon/AbstractShader.h" #include "VideoCommon/AbstractShader.h"
#include "VideoCommon/AbstractStagingTexture.h" #include "VideoCommon/AbstractStagingTexture.h"
#include "VideoCommon/AbstractTexture.h" #include "VideoCommon/AbstractTexture.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/DriverDetails.h" #include "VideoCommon/DriverDetails.h"
#include "VideoCommon/FramebufferShaderGen.h" #include "VideoCommon/FramebufferShaderGen.h"
#include "VideoCommon/RenderBase.h" #include "VideoCommon/RenderBase.h"
@ -787,36 +788,34 @@ void FramebufferManager::PopulateEFBCache(bool depth, u32 tile_index, bool async
data.tiles[tile_index].present = true; data.tiles[tile_index].present = true;
} }
void FramebufferManager::ClearEFB(const MathUtil::Rectangle<int>& rc, bool clear_color, void FramebufferManager::ClearEFB(const MathUtil::Rectangle<int>& rc, bool color_enable,
bool clear_alpha, bool clear_z, u32 color, u32 z) bool alpha_enable, bool z_enable, u32 color, u32 z)
{ {
FlushEFBPokes(); FlushEFBPokes();
FlagPeekCacheAsOutOfDate(); FlagPeekCacheAsOutOfDate();
g_gfx->BeginUtilityDrawing();
// Set up uniforms. // Native -> EFB coordinates
struct Uniforms MathUtil::Rectangle<int> target_rc = g_renderer->ConvertEFBRectangle(rc);
target_rc = g_gfx->ConvertFramebufferRectangle(target_rc, m_efb_framebuffer.get());
target_rc.ClampUL(0, 0, g_renderer->GetTargetWidth(), g_renderer->GetTargetHeight());
// Determine whether the EFB has an alpha channel. If it doesn't, we can clear the alpha
// channel to 0xFF.
// On backends that don't allow masking Alpha clears, this allows us to use the fast path
// almost all the time
if (bpmem.zcontrol.pixel_format == PixelFormat::RGB565_Z16 ||
bpmem.zcontrol.pixel_format == PixelFormat::RGB8_Z24 ||
bpmem.zcontrol.pixel_format == PixelFormat::Z24)
{ {
float clear_color[4]; // Force alpha writes, and clear the alpha channel.
float clear_depth; alpha_enable = true;
float padding1, padding2, padding3; color &= 0x00FFFFFF;
}; }
static_assert(std::is_standard_layout<Uniforms>::value);
Uniforms uniforms = {{static_cast<float>((color >> 16) & 0xFF) / 255.0f,
static_cast<float>((color >> 8) & 0xFF) / 255.0f,
static_cast<float>((color >> 0) & 0xFF) / 255.0f,
static_cast<float>((color >> 24) & 0xFF) / 255.0f},
static_cast<float>(z & 0xFFFFFF) / 16777216.0f};
if (!g_ActiveConfig.backend_info.bSupportsReversedDepthRange)
uniforms.clear_depth = 1.0f - uniforms.clear_depth;
g_vertex_manager->UploadUtilityUniforms(&uniforms, sizeof(uniforms));
const auto target_rc = g_gfx->ConvertFramebufferRectangle(g_renderer->ConvertEFBRectangle(rc), g_gfx->ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
m_efb_framebuffer.get());
g_gfx->SetPipeline(m_efb_clear_pipelines[clear_color][clear_alpha][clear_z].get()); // Scissor rect must be restored.
g_gfx->SetViewportAndScissor(target_rc); BPFunctions::SetScissorAndViewport();
g_gfx->Draw(0, 3);
g_gfx->EndUtilityDrawing();
} }
bool FramebufferManager::CompileClearPipelines() bool FramebufferManager::CompileClearPipelines()
@ -849,9 +848,9 @@ bool FramebufferManager::CompileClearPipelines()
config.depth_state.testenable = depth_enable != 0; config.depth_state.testenable = depth_enable != 0;
config.depth_state.updateenable = depth_enable != 0; config.depth_state.updateenable = depth_enable != 0;
m_efb_clear_pipelines[color_enable][alpha_enable][depth_enable] = m_clear_pipelines[color_enable][alpha_enable][depth_enable] =
g_gfx->CreatePipeline(config); g_gfx->CreatePipeline(config);
if (!m_efb_clear_pipelines[color_enable][alpha_enable][depth_enable]) if (!m_clear_pipelines[color_enable][alpha_enable][depth_enable])
return false; return false;
} }
} }
@ -868,12 +867,18 @@ void FramebufferManager::DestroyClearPipelines()
{ {
for (u32 depth_enable = 0; depth_enable < 2; depth_enable++) for (u32 depth_enable = 0; depth_enable < 2; depth_enable++)
{ {
m_efb_clear_pipelines[color_enable][alpha_enable][depth_enable].reset(); m_clear_pipelines[color_enable][alpha_enable][depth_enable].reset();
} }
} }
} }
} }
AbstractPipeline* FramebufferManager::GetClearPipeline(bool colorEnable, bool alphaEnable,
bool zEnable) const
{
return m_clear_pipelines[colorEnable][alphaEnable][zEnable].get();
}
void FramebufferManager::PokeEFBColor(u32 x, u32 y, u32 color) void FramebufferManager::PokeEFBColor(u32 x, u32 y, u32 color)
{ {
// Flush if we exceeded the number of vertices per batch. // Flush if we exceeded the number of vertices per batch.

View File

@ -95,6 +95,8 @@ public:
void ClearEFB(const MathUtil::Rectangle<int>& rc, bool clear_color, bool clear_alpha, void ClearEFB(const MathUtil::Rectangle<int>& rc, bool clear_color, bool clear_alpha,
bool clear_z, u32 color, u32 z); bool clear_z, u32 color, u32 z);
AbstractPipeline* GetClearPipeline(bool clear_color, bool clear_alpha, bool clear_z) const;
// Reads a framebuffer value back from the GPU. This may block if the cache is not current. // Reads a framebuffer value back from the GPU. This may block if the cache is not current.
u32 PeekEFBColor(u32 x, u32 y); u32 PeekEFBColor(u32 x, u32 y);
float PeekEFBDepth(u32 x, u32 y); float PeekEFBDepth(u32 x, u32 y);
@ -206,7 +208,7 @@ protected:
// EFB clear pipelines // EFB clear pipelines
// Indexed by [color_write_enabled][alpha_write_enabled][depth_write_enabled] // Indexed by [color_write_enabled][alpha_write_enabled][depth_write_enabled]
std::array<std::array<std::array<std::unique_ptr<AbstractPipeline>, 2>, 2>, 2> std::array<std::array<std::array<std::unique_ptr<AbstractPipeline>, 2>, 2>, 2>
m_efb_clear_pipelines; m_clear_pipelines;
// EFB poke drawing setup // EFB poke drawing setup
std::unique_ptr<NativeVertexFormat> m_poke_vertex_format; std::unique_ptr<NativeVertexFormat> m_poke_vertex_format;

View File

@ -63,35 +63,6 @@ Renderer::Renderer()
Renderer::~Renderer() = default; Renderer::~Renderer() = default;
void Renderer::ClearScreen(const MathUtil::Rectangle<int>& rc, bool color_enable, bool alpha_enable,
bool z_enable, u32 color, u32 z)
{
g_framebuffer_manager->FlushEFBPokes();
g_framebuffer_manager->FlagPeekCacheAsOutOfDate();
// Native -> EFB coordinates
MathUtil::Rectangle<int> target_rc = Renderer::ConvertEFBRectangle(rc);
target_rc.ClampUL(0, 0, m_target_width, m_target_height);
// Determine whether the EFB has an alpha channel. If it doesn't, we can clear the alpha
// channel to 0xFF.
// On backends that don't allow masking Alpha clears, this allows us to use the fast path
// almost all the time
if (bpmem.zcontrol.pixel_format == PixelFormat::RGB565_Z16 ||
bpmem.zcontrol.pixel_format == PixelFormat::RGB8_Z24 ||
bpmem.zcontrol.pixel_format == PixelFormat::Z24)
{
// Force alpha writes, and clear the alpha channel.
alpha_enable = true;
color &= 0x00FFFFFF;
}
g_gfx->ClearRegion(rc, target_rc, color_enable, alpha_enable, z_enable, color, z);
// Scissor rect must be restored.
BPFunctions::SetScissorAndViewport();
}
void Renderer::ReinterpretPixelData(EFBReinterpretType convtype) void Renderer::ReinterpretPixelData(EFBReinterpretType convtype)
{ {
g_framebuffer_manager->ReinterpretPixelData(convtype); g_framebuffer_manager->ReinterpretPixelData(convtype);

View File

@ -52,8 +52,6 @@ public:
float EFBToScaledXf(float x) const; float EFBToScaledXf(float x) const;
float EFBToScaledYf(float y) const; float EFBToScaledYf(float y) const;
void ClearScreen(const MathUtil::Rectangle<int>& rc, bool colorEnable, bool alphaEnable,
bool zEnable, u32 color, u32 z);
virtual void ReinterpretPixelData(EFBReinterpretType convtype); virtual void ReinterpretPixelData(EFBReinterpretType convtype);
virtual u32 AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data); virtual u32 AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data);