From 23f8203a22f30f99220a4ce457ca7926ef9597dd Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Fri, 29 May 2015 18:58:26 +0200 Subject: [PATCH] gsdx-ogl: add some shaders to convert depth <-> color I'm afraid of rounding error --- plugins/GSdx/GSDeviceOGL.h | 2 +- plugins/GSdx/res/glsl/convert.glsl | 26 ++++++++++++++++++++++++++ plugins/GSdx/res/glsl_source.h | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/plugins/GSdx/GSDeviceOGL.h b/plugins/GSdx/GSDeviceOGL.h index 64f42e9f80..74e9053f30 100644 --- a/plugins/GSdx/GSDeviceOGL.h +++ b/plugins/GSdx/GSDeviceOGL.h @@ -486,7 +486,7 @@ class GSDeviceOGL : public GSDevice struct { GLuint vs; // program object - GLuint ps[11]; // program object + GLuint ps[13]; // program object GLuint ln; // sampler object GLuint pt; // sampler object GSDepthStencilOGL* dss; diff --git a/plugins/GSdx/res/glsl/convert.glsl b/plugins/GSdx/res/glsl/convert.glsl index a664e87459..3a7fa21f17 100644 --- a/plugins/GSdx/res/glsl/convert.glsl +++ b/plugins/GSdx/res/glsl/convert.glsl @@ -151,12 +151,38 @@ void ps_main1() #ifdef ps_main10 void ps_main10() { + // Convert a GL_FLOAT32 depth texture into a 32 bits UINT texture vec4 c = sample_c(); const float exp2_32 = exp2(32.0f); SV_Target1 = uint(exp2_32 * c.r); } #endif +#ifdef ps_main11 +void ps_main11() +{ + const float exp2_32 = exp2(32.0f); + const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0); + const vec4 bitMsk = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0); + + // Convert a GL_FLOAT32 depth texture into a RGBA texture + vec4 res = fract(vec4(sample_c().r) * bitSh); + + SV_Target0 = (res - res.xxyz * bitMsk) * 256.0f/255.0f; +} +#endif + +#ifdef ps_main12 +out float gl_FragDepth; +void ps_main12() +{ + // Convert a RRGBA texture into a float depth texture + // FIXME: I'm afraid of the accuracy + const vec4 bitSh = vec4(1.0/(256.0*256.0*256.0), 1.0/(256.0*256.0), 1.0/256.0, 1.0) * vec4(255.0/256.0); + gl_FragDepth = dot(sample_c(), bitSh); +} +#endif + #ifdef ps_main7 void ps_main7() { diff --git a/plugins/GSdx/res/glsl_source.h b/plugins/GSdx/res/glsl_source.h index 1a6cc4dacd..08a0303ee6 100644 --- a/plugins/GSdx/res/glsl_source.h +++ b/plugins/GSdx/res/glsl_source.h @@ -176,12 +176,38 @@ static const char* convert_glsl = "#ifdef ps_main10\n" "void ps_main10()\n" "{\n" + " // Convert a GL_FLOAT32 depth texture into a 32 bits UINT texture\n" " vec4 c = sample_c();\n" " const float exp2_32 = exp2(32.0f);\n" " SV_Target1 = uint(exp2_32 * c.r);\n" "}\n" "#endif\n" "\n" + "#ifdef ps_main11\n" + "void ps_main11()\n" + "{\n" + " const float exp2_32 = exp2(32.0f);\n" + " const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);\n" + " const vec4 bitMsk = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);\n" + "\n" + " // Convert a GL_FLOAT32 depth texture into a RGBA texture\n" + " vec4 res = fract(vec4(sample_c().r) * bitSh);\n" + "\n" + " SV_Target0 = (res - res.xxyz * bitMsk) * 256.0f/255.0f;\n" + "}\n" + "#endif\n" + "\n" + "#ifdef ps_main12\n" + "out float gl_FragDepth;\n" + "void ps_main12()\n" + "{\n" + " // Convert a RRGBA texture into a float depth texture\n" + " // FIXME: I'm afraid of the accuracy\n" + " const vec4 bitSh = vec4(1.0/(256.0*256.0*256.0), 1.0/(256.0*256.0), 1.0/256.0, 1.0) * vec4(255.0/256.0);\n" + " gl_FragDepth = dot(sample_c(), bitSh);\n" + "}\n" + "#endif\n" + "\n" "#ifdef ps_main7\n" "void ps_main7()\n" "{\n"