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);
}
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)
{
// 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.
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)

View File

@ -47,7 +47,7 @@ public:
void Flush() 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;
void SetPipeline(const AbstractPipeline* pipeline) override;

View File

@ -47,7 +47,7 @@ public:
void WaitForGPUIdle() 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;
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,
const MathUtil::Rectangle<int>& target_rc,
void Metal::Gfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc,
bool color_enable, bool alpha_enable, bool z_enable, u32 color, u32 z)
{
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_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);
}

View File

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

View File

@ -42,7 +42,7 @@ public:
void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override;
void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {},
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;
void SetScissorRect(const MathUtil::Rectangle<int>& rc) 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);
}
void SWGfx::ClearRegion(const MathUtil::Rectangle<int>& rc,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
void SWGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
bool alphaEnable, bool zEnable, u32 color, u32 z)
{
EfbCopy::ClearEfb();

View File

@ -46,7 +46,7 @@ public:
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;
private:

View File

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

View File

@ -53,7 +53,7 @@ public:
void WaitForGPUIdle() 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;
void SetPipeline(const AbstractPipeline* pipeline) override;

View File

@ -54,11 +54,35 @@ void AbstractGfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer,
m_current_framebuffer = framebuffer;
}
void AbstractGfx::ClearRegion(const MathUtil::Rectangle<int>& rc,
const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
void AbstractGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
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,

View File

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

View File

@ -342,7 +342,7 @@ void ClearScreen(const MathUtil::Rectangle<int>& rc)
color = RGBA8ToRGB565ToRGBA8(color);
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/AbstractStagingTexture.h"
#include "VideoCommon/AbstractTexture.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/FramebufferShaderGen.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;
}
void FramebufferManager::ClearEFB(const MathUtil::Rectangle<int>& rc, bool clear_color,
bool clear_alpha, bool clear_z, u32 color, u32 z)
void FramebufferManager::ClearEFB(const MathUtil::Rectangle<int>& rc, bool color_enable,
bool alpha_enable, bool z_enable, u32 color, u32 z)
{
FlushEFBPokes();
FlagPeekCacheAsOutOfDate();
g_gfx->BeginUtilityDrawing();
// Set up uniforms.
struct Uniforms
// Native -> EFB coordinates
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];
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));
// Force alpha writes, and clear the alpha channel.
alpha_enable = true;
color &= 0x00FFFFFF;
}
const auto target_rc = g_gfx->ConvertFramebufferRectangle(g_renderer->ConvertEFBRectangle(rc),
m_efb_framebuffer.get());
g_gfx->SetPipeline(m_efb_clear_pipelines[clear_color][clear_alpha][clear_z].get());
g_gfx->SetViewportAndScissor(target_rc);
g_gfx->Draw(0, 3);
g_gfx->EndUtilityDrawing();
g_gfx->ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z);
// Scissor rect must be restored.
BPFunctions::SetScissorAndViewport();
}
bool FramebufferManager::CompileClearPipelines()
@ -849,9 +848,9 @@ bool FramebufferManager::CompileClearPipelines()
config.depth_state.testenable = 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);
if (!m_efb_clear_pipelines[color_enable][alpha_enable][depth_enable])
if (!m_clear_pipelines[color_enable][alpha_enable][depth_enable])
return false;
}
}
@ -868,12 +867,18 @@ void FramebufferManager::DestroyClearPipelines()
{
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)
{
// 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,
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.
u32 PeekEFBColor(u32 x, u32 y);
float PeekEFBDepth(u32 x, u32 y);
@ -206,7 +208,7 @@ protected:
// EFB clear pipelines
// Indexed by [color_write_enabled][alpha_write_enabled][depth_write_enabled]
std::array<std::array<std::array<std::unique_ptr<AbstractPipeline>, 2>, 2>, 2>
m_efb_clear_pipelines;
m_clear_pipelines;
// EFB poke drawing setup
std::unique_ptr<NativeVertexFormat> m_poke_vertex_format;

View File

@ -63,35 +63,6 @@ Renderer::Renderer()
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)
{
g_framebuffer_manager->ReinterpretPixelData(convtype);

View File

@ -52,8 +52,6 @@ public:
float EFBToScaledXf(float x) 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 u32 AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data);