Skip to content

Commit

Permalink
GPU: Work around nearest sampling issues on AMD
Browse files Browse the repository at this point in the history
Fixes one-line flickering display in some games.
  • Loading branch information
stenzek committed Oct 25, 2024
1 parent d8cd32d commit 6af71be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,13 +1683,13 @@ bool GPU::CompileDisplayPipelines(bool display, bool deinterlace, bool chroma_sm

case DisplayScalingMode::BilinearSmooth:
case DisplayScalingMode::BilinearInteger:
fs = shadergen.GenerateDisplayFragmentShader(true);
fs = shadergen.GenerateDisplayFragmentShader(true, false);
break;

case DisplayScalingMode::Nearest:
case DisplayScalingMode::NearestInteger:
default:
fs = shadergen.GenerateDisplayFragmentShader(false);
fs = shadergen.GenerateDisplayFragmentShader(false, true);
break;
}

Expand Down
16 changes: 13 additions & 3 deletions src/core/gpu_shadergen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,27 @@ std::string GPUShaderGen::GenerateDisplayVertexShader()
return ss.str();
}

std::string GPUShaderGen::GenerateDisplayFragmentShader(bool clamp_uv)
std::string GPUShaderGen::GenerateDisplayFragmentShader(bool clamp_uv, bool nearest)
{
std::stringstream ss;
WriteHeader(ss);
WriteDisplayUniformBuffer(ss);
DeclareTexture(ss, "samp0", 0);
DeclareFragmentEntryPoint(ss, 0, 1);
ss << "{\n";

if (clamp_uv)
ss << "{\n o_col0 = float4(SAMPLE_TEXTURE(samp0, ClampUV(v_tex0)).rgb, 1.0f);\n }";
ss << " float2 uv = ClampUV(v_tex0);\n";
else
ss << " float2 uv = v_tex0;\n";

// Work around nearest sampling precision issues on AMD graphics cards by adding 1/128 to UVs.
if (nearest)
ss << " o_col0 = float4(LOAD_TEXTURE(samp0, int2((uv * u_src_size.xy) + (1.0 / 128.0)), 0).rgb, 1.0f);\n";
else
ss << "{\n o_col0 = float4(SAMPLE_TEXTURE(samp0, v_tex0).rgb, 1.0f);\n }";
ss << " o_col0 = float4(SAMPLE_TEXTURE(samp0, ClampUV(v_tex0)).rgb, 1.0f);\n";

ss << "}\n";

return ss.str();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/gpu_shadergen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GPUShaderGen : public ShaderGen
~GPUShaderGen();

std::string GenerateDisplayVertexShader();
std::string GenerateDisplayFragmentShader(bool clamp_uv);
std::string GenerateDisplayFragmentShader(bool clamp_uv, bool nearest);
std::string GenerateDisplaySharpBilinearFragmentShader();

std::string GenerateInterleavedFieldExtractFragmentShader();
Expand Down

0 comments on commit 6af71be

Please sign in to comment.