From 7c430c9d3ca241b0c5c9eb2cedda37d71588a128 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Wed, 4 May 2016 18:05:45 +0200 Subject: [PATCH] glsl: fix coordinate in channel depth effect UV can't be used directly in channel effect Properly fix Urban Chaos smoke --- plugins/GSdx/res/glsl/tfx_fs.glsl | 36 ++++++++++++++++++------------- plugins/GSdx/res/glsl_source.h | 36 ++++++++++++++++++------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/plugins/GSdx/res/glsl/tfx_fs.glsl b/plugins/GSdx/res/glsl/tfx_fs.glsl index b7ca4c2d8f..c6bb84933c 100644 --- a/plugins/GSdx/res/glsl/tfx_fs.glsl +++ b/plugins/GSdx/res/glsl/tfx_fs.glsl @@ -176,14 +176,24 @@ mat4 sample_4p(vec4 u) return c; } -////////////////////////////////////////////////////////////////////// -// Depth sampling -////////////////////////////////////////////////////////////////////// +int fetch_raw_depth() +{ + return int(texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0).r * exp2(32.0f)); +} + +vec4 fetch_raw_color() +{ + return texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0); +} + vec4 fetch_c(ivec2 uv) { return texelFetch(TextureSampler, ivec2(uv), 0); } +////////////////////////////////////////////////////////////////////// +// Depth sampling +////////////////////////////////////////////////////////////////////// ivec2 clamp_wrap_uv_depth(ivec2 uv) { ivec2 uv_out = uv; @@ -226,8 +236,14 @@ vec4 sample_depth(vec2 st) vec4 t; #if PS_URBAN_CHAOS_HACK == 1 - // Convert a GL_FLOAT32 to a special color format expected by the game - int depth = int(fetch_c(uv).r * exp2(32.0f)); + // Depth buffer is read as a RGB5A1 texture. The game try to extract the green channel. + // So it will do a first channel trick to extract lsb, value is right-shifted. + // Then a new channel trick to extract msb which will shifted to the left. + // OpenGL uses a FLOAT32 format for the depth so it requires a couple of conversion. + // To be faster both steps (msb&lsb) are done in a single pass. + + // Warning: UV can't be used in channel effect + int depth = fetch_raw_depth(); // Convert lsb based on the palette t = texelFetch(PaletteSampler, ivec2((depth & 0xFF), 0), 0); @@ -281,16 +297,6 @@ vec4 sample_depth(vec2 st) ////////////////////////////////////////////////////////////////////// // Fetch a Single Channel ////////////////////////////////////////////////////////////////////// -int fetch_raw_depth() -{ - return int(texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0).r * exp2(32.0f)); -} - -vec4 fetch_raw_color() -{ - return texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0); -} - vec4 fetch_red() { #if PS_DEPTH_FMT == 1 || PS_DEPTH_FMT == 2 diff --git a/plugins/GSdx/res/glsl_source.h b/plugins/GSdx/res/glsl_source.h index 646b527218..9466f6f2a1 100644 --- a/plugins/GSdx/res/glsl_source.h +++ b/plugins/GSdx/res/glsl_source.h @@ -1020,14 +1020,24 @@ static const char* const tfx_fs_all_glsl = " return c;\n" "}\n" "\n" - "//////////////////////////////////////////////////////////////////////\n" - "// Depth sampling\n" - "//////////////////////////////////////////////////////////////////////\n" + "int fetch_raw_depth()\n" + "{\n" + " return int(texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0).r * exp2(32.0f));\n" + "}\n" + "\n" + "vec4 fetch_raw_color()\n" + "{\n" + " return texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0);\n" + "}\n" + "\n" "vec4 fetch_c(ivec2 uv)\n" "{\n" " return texelFetch(TextureSampler, ivec2(uv), 0);\n" "}\n" "\n" + "//////////////////////////////////////////////////////////////////////\n" + "// Depth sampling\n" + "//////////////////////////////////////////////////////////////////////\n" "ivec2 clamp_wrap_uv_depth(ivec2 uv)\n" "{\n" " ivec2 uv_out = uv;\n" @@ -1070,8 +1080,14 @@ static const char* const tfx_fs_all_glsl = "\n" " vec4 t;\n" "#if PS_URBAN_CHAOS_HACK == 1\n" - " // Convert a GL_FLOAT32 to a special color format expected by the game\n" - " int depth = int(fetch_c(uv).r * exp2(32.0f));\n" + " // Depth buffer is read as a RGB5A1 texture. The game try to extract the green channel.\n" + " // So it will do a first channel trick to extract lsb, value is right-shifted.\n" + " // Then a new channel trick to extract msb which will shifted to the left.\n" + " // OpenGL uses a FLOAT32 format for the depth so it requires a couple of conversion.\n" + " // To be faster both steps (msb&lsb) are done in a single pass.\n" + "\n" + " // Warning: UV can't be used in channel effect\n" + " int depth = fetch_raw_depth();\n" "\n" " // Convert lsb based on the palette\n" " t = texelFetch(PaletteSampler, ivec2((depth & 0xFF), 0), 0);\n" @@ -1125,16 +1141,6 @@ static const char* const tfx_fs_all_glsl = "//////////////////////////////////////////////////////////////////////\n" "// Fetch a Single Channel\n" "//////////////////////////////////////////////////////////////////////\n" - "int fetch_raw_depth()\n" - "{\n" - " return int(texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0).r * exp2(32.0f));\n" - "}\n" - "\n" - "vec4 fetch_raw_color()\n" - "{\n" - " return texelFetch(RawTextureSampler, ivec2(gl_FragCoord.xy), 0);\n" - "}\n" - "\n" "vec4 fetch_red()\n" "{\n" "#if PS_DEPTH_FMT == 1 || PS_DEPTH_FMT == 2\n"