From 92a4551bb296e67726c2b4e2939defdf092527de Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 10 Nov 2020 20:36:17 +1000 Subject: [PATCH] GPU/OpenGL: Support GL_EXT_blend_func_extended for dual-source blend --- src/core/gpu_hw.h | 15 +++++++++++++++ src/core/gpu_hw_opengl.cpp | 14 +++++++------- src/core/shadergen.cpp | 2 ++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 2bd162845..10d2694f6 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -225,6 +225,21 @@ protected: } } + /// Returns true if alpha blending should be enabled for drawing the current batch. + ALWAYS_INLINE bool UseAlphaBlending(TransparencyMode transparency_mode, BatchRenderMode render_mode) const + { + if (m_texture_filtering == GPUTextureFilter::Bilinear || m_texture_filtering == GPUTextureFilter::JINC2 || + m_texture_filtering == GPUTextureFilter::xBR) + { + return true; + } + + if (transparency_mode == TransparencyMode::Disabled || render_mode == BatchRenderMode::OnlyOpaque) + return false; + + return true; + } + void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) override; void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data) override; void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) override; diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index 6e0281a39..3aee7b2e9 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -240,7 +240,8 @@ void GPU_HW_OpenGL::SetCapabilities(HostDisplay* host_display) int max_dual_source_draw_buffers = 0; glGetIntegerv(GL_MAX_DUAL_SOURCE_DRAW_BUFFERS, &max_dual_source_draw_buffers); m_supports_dual_source_blend = - (max_dual_source_draw_buffers > 0) && (GLAD_GL_VERSION_3_3 || GLAD_GL_ARB_blend_func_extended); + (max_dual_source_draw_buffers > 0) && + (GLAD_GL_VERSION_3_3 || GLAD_GL_ARB_blend_func_extended || GLAD_GL_EXT_blend_func_extended); if (!m_supports_dual_source_blend) Log_WarningPrintf("Dual-source blending is not supported, this may break some mask effects."); @@ -588,12 +589,7 @@ void GPU_HW_OpenGL::DrawBatchVertices(BatchRenderMode render_mode, u32 base_vert void GPU_HW_OpenGL::SetBlendMode() { - if (m_texture_filtering == GPUTextureFilter::Nearest && (m_current_transparency_mode == TransparencyMode::Disabled || - m_current_render_mode == BatchRenderMode::OnlyOpaque)) - { - glDisable(GL_BLEND); - } - else + if (UseAlphaBlending(m_current_transparency_mode, m_current_render_mode)) { glEnable(GL_BLEND); glBlendEquationSeparate(m_current_transparency_mode == TransparencyMode::BackgroundMinusForeground ? @@ -602,6 +598,10 @@ void GPU_HW_OpenGL::SetBlendMode() GL_FUNC_ADD); glBlendFuncSeparate(GL_ONE, m_supports_dual_source_blend ? GL_SRC1_ALPHA : GL_SRC_ALPHA, GL_ONE, GL_ZERO); } + else + { + glDisable(GL_BLEND); + } } void GPU_HW_OpenGL::SetDepthFunc() diff --git a/src/core/shadergen.cpp b/src/core/shadergen.cpp index 1cd886dc6..be6d7aa12 100644 --- a/src/core/shadergen.cpp +++ b/src/core/shadergen.cpp @@ -89,6 +89,8 @@ void ShaderGen::WriteHeader(std::stringstream& ss) // Enable EXT_blend_func_extended for dual-source blend on OpenGL ES. if (GLAD_GL_EXT_blend_func_extended) ss << "#extension GL_EXT_blend_func_extended : require\n"; + if (GLAD_GL_ARB_blend_func_extended) + ss << "#extension GL_ARB_blend_func_extended : require\n"; } else if (m_render_api == HostDisplay::RenderAPI::OpenGL) {