OGL: Add unrestricted depth range support.

This commit is contained in:
Jules Blok 2024-10-05 16:25:17 +02:00
parent 32521d1536
commit 73eec396e1
3 changed files with 33 additions and 7 deletions

View File

@ -349,6 +349,8 @@ bool PopulateConfig(GLContext* m_main_gl_context)
GLExtensions::Supports("GL_ARB_derivative_control") || GLExtensions::Version() >= 450; GLExtensions::Supports("GL_ARB_derivative_control") || GLExtensions::Version() >= 450;
g_Config.backend_info.bSupportsTextureQueryLevels = g_Config.backend_info.bSupportsTextureQueryLevels =
GLExtensions::Supports("GL_ARB_texture_query_levels") || GLExtensions::Version() >= 430; GLExtensions::Supports("GL_ARB_texture_query_levels") || GLExtensions::Version() >= 430;
g_Config.backend_info.bSupportsUnrestrictedDepthRange =
GLExtensions::Supports("GL_NV_depth_buffer_float");
if (GLExtensions::Supports("GL_ARB_shader_storage_buffer_object")) if (GLExtensions::Supports("GL_ARB_shader_storage_buffer_object"))
{ {

View File

@ -118,6 +118,16 @@ static void APIENTRY ClearDepthf(GLfloat depthval)
glClearDepth(depthval); glClearDepth(depthval);
} }
// Two small overrides to support unrestricted depth range
static void APIENTRY DepthRangefNV(GLfloat neardepth, GLfloat fardepth)
{
glDepthRangedNV(neardepth, fardepth);
}
static void APIENTRY ClearDepthfNV(GLfloat depthval)
{
glClearDepthdNV(depthval);
}
OGLGfx::OGLGfx(std::unique_ptr<GLContext> main_gl_context, float backbuffer_scale) OGLGfx::OGLGfx(std::unique_ptr<GLContext> main_gl_context, float backbuffer_scale)
: m_main_gl_context(std::move(main_gl_context)), : m_main_gl_context(std::move(main_gl_context)),
m_current_rasterization_state(RenderState::GetInvalidRasterizationState()), m_current_rasterization_state(RenderState::GetInvalidRasterizationState()),
@ -137,11 +147,16 @@ OGLGfx::OGLGfx(std::unique_ptr<GLContext> main_gl_context, float backbuffer_scal
if (!m_main_gl_context->IsGLES()) if (!m_main_gl_context->IsGLES())
{ {
// OpenGL 3 doesn't provide GLES like float functions for depth. if (g_ActiveConfig.backend_info.bSupportsUnrestrictedDepthRange)
// They are in core in OpenGL 4.1, so almost every driver should support them.
// But for the oldest ones, we provide fallbacks to the old double functions.
if (!GLExtensions::Supports("GL_ARB_ES2_compatibility"))
{ {
glDepthRangef = DepthRangefNV;
glClearDepthf = ClearDepthfNV;
}
else if (!GLExtensions::Supports("GL_ARB_ES2_compatibility"))
{
// OpenGL 3 doesn't provide GLES like float functions for depth.
// They are in core in OpenGL 4.1, so almost every driver should support them.
// But for the oldest ones, we provide fallbacks to the old double functions.
glDepthRangef = DepthRangef; glDepthRangef = DepthRangef;
glClearDepthf = ClearDepthf; glClearDepthf = ClearDepthf;
} }
@ -387,7 +402,10 @@ void OGLGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEn
if (zEnable) if (zEnable)
{ {
glDepthMask(zEnable ? GL_TRUE : GL_FALSE); glDepthMask(zEnable ? GL_TRUE : GL_FALSE);
glClearDepthf(float(z & 0xFFFFFF) / 16777216.0f); if (g_ActiveConfig.backend_info.bSupportsUnrestrictedDepthRange)
glClearDepthf(float(z & 0xFFFFFF));
else
glClearDepthf(float(z & 0xFFFFFF) / 16777216.0f);
clear_mask |= GL_DEPTH_BUFFER_BIT; clear_mask |= GL_DEPTH_BUFFER_BIT;
} }

View File

@ -44,9 +44,15 @@ GLenum OGLTexture::GetGLInternalFormatForTextureFormat(AbstractTextureFormat for
case AbstractTextureFormat::D24_S8: case AbstractTextureFormat::D24_S8:
return GL_DEPTH24_STENCIL8; return GL_DEPTH24_STENCIL8;
case AbstractTextureFormat::D32F: case AbstractTextureFormat::D32F:
return GL_DEPTH_COMPONENT32F; if (g_ActiveConfig.backend_info.bSupportsUnrestrictedDepthRange)
return GL_DEPTH_COMPONENT32F_NV;
else
return GL_DEPTH_COMPONENT32F;
case AbstractTextureFormat::D32F_S8: case AbstractTextureFormat::D32F_S8:
return GL_DEPTH32F_STENCIL8; if (g_ActiveConfig.backend_info.bSupportsUnrestrictedDepthRange)
return GL_DEPTH32F_STENCIL8_NV;
else
return GL_DEPTH32F_STENCIL8;
default: default:
PanicAlertFmt("Unhandled texture format."); PanicAlertFmt("Unhandled texture format.");
return storage ? GL_RGBA8 : GL_RGBA; return storage ? GL_RGBA8 : GL_RGBA;