Skip to content

Commit

Permalink
gsdx-ogl: add support of clip_control (depth only)
Browse files Browse the repository at this point in the history
* replace the [-1;1] depth range of openGL with the DX range [0;1].
  • Loading branch information
gregory38 committed Sep 28, 2014
1 parent 104688e commit 4659184
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
19 changes: 19 additions & 0 deletions plugins/GSdx/GSDeviceOGL.cpp
Expand Up @@ -307,6 +307,25 @@ bool GSDeviceOGL::Create(GSWnd* wnd)
// m_date.bs->SetMask(false, false, false, false);
//#endif

// ****************************************************************
// Use DX coordinate convention
// ****************************************************************


// VS gl_position.z => [-1,-1]
// FS depth => [0, 1]
// because of -1 we loose lot of precision for small GS value
// This extension allow FS depth to range from -1 to 1. So
// gl_position.z could range from [0, 1]
#ifndef ENABLE_GLES
if (GLLoader::found_GL_ARB_clip_control) {
// Change depth convention
gl_ClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
} else if (GLLoader::found_GL_NV_depth_buffer_float) {
gl_DepthRangedNV(-1.0f, 1.0f);
}
#endif

// ****************************************************************
// HW renderer shader
// ****************************************************************
Expand Down
5 changes: 2 additions & 3 deletions plugins/GSdx/GSShaderOGL.cpp
Expand Up @@ -403,9 +403,8 @@ std::string GSShaderOGL::GenGlslHeader(const std::string& entry, GLenum type, co
header += "#define ENABLE_BINDLESS_TEX\n";
}

if (GLLoader::found_GL_NV_depth_buffer_float) {
// Specific nvidia extension that seem to help for z fighting
header += "#define NV_DEPTH\n";
if (GLLoader::found_GL_NV_depth_buffer_float || GLLoader::found_GL_ARB_clip_control) {
header += "#define ZERO_TO_ONE_DEPTH\n";
}

#else
Expand Down
11 changes: 0 additions & 11 deletions plugins/GSdx/GSTextureFXOGL.cpp
Expand Up @@ -59,17 +59,6 @@ void GSDeviceOGL::CreateTextureFX()

// Help to debug FS in apitrace
m_apitrace = CompilePS(PSSelector());

// VS gl_position.z => [-1,-1]
// FS depth => [0, 1]
// because of -1 we loose lot of precision for small GS value
// This extension allow FS depth to range from -1 to 1. So
// gl_position.z could range from [0, 1]
#ifndef ENABLE_GLES
if (GLLoader::found_GL_NV_depth_buffer_float) {
gl_DepthRangedNV(-1.0f, 1.0f);
}
#endif
}

GSDepthStencilOGL* GSDeviceOGL::CreateDepthStencil(OMDepthStencilSelector dssel)
Expand Down
5 changes: 4 additions & 1 deletion plugins/GSdx/res/glsl_source.h
Expand Up @@ -664,8 +664,11 @@ static const char* tfx_glsl =
" vec2 TextureScale;\n"
"};\n"
"\n"
"#ifdef ZERO_TO_ONE_DEPTH\n"
"const float exp_min32 = exp2(-32.0f);\n"
"#else\n"
"const float exp_min31 = exp2(-31.0f);\n"
"#endif\n"
"\n"
"#ifdef SUBROUTINE_GL40\n"
"// Function pointer type\n"
Expand Down Expand Up @@ -740,7 +743,7 @@ static const char* tfx_glsl =
" p.xy = vec2(i_p) - vec2(0.05f, 0.05f);\n"
" p.xy = p.xy * VertexScale - VertexOffset;\n"
" p.w = 1.0f;\n"
"#ifdef NV_DEPTH\n"
"#ifdef ZERO_TO_ONE_DEPTH\n"
" if(VS_LOGZ == 1) {\n"
" p.z = log2(float(1u+z)) / 32.0f;\n"
" } else {\n"
Expand Down
5 changes: 4 additions & 1 deletion plugins/GSdx/res/tfx.glsl
Expand Up @@ -105,8 +105,11 @@ layout(std140, binding = 20) uniform cb20
vec2 TextureScale;
};

#ifdef ZERO_TO_ONE_DEPTH
const float exp_min32 = exp2(-32.0f);
#else
const float exp_min31 = exp2(-31.0f);
#endif

#ifdef SUBROUTINE_GL40
// Function pointer type
Expand Down Expand Up @@ -181,7 +184,7 @@ void vs_main()
p.xy = vec2(i_p) - vec2(0.05f, 0.05f);
p.xy = p.xy * VertexScale - VertexOffset;
p.w = 1.0f;
#ifdef NV_DEPTH
#ifdef ZERO_TO_ONE_DEPTH
if(VS_LOGZ == 1) {
p.z = log2(float(1u+z)) / 32.0f;
} else {
Expand Down

0 comments on commit 4659184

Please sign in to comment.