Skip to content

Commit

Permalink
GS: Remove reduced-depth-range hack
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Mar 29, 2024
1 parent 7ddf638 commit 11ee0a8
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 86 deletions.
28 changes: 0 additions & 28 deletions bin/resources/shaders/opengl/convert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,15 @@ void ps_convert_rgba8_16bits()
void ps_convert_float32_32bits()
{
// Convert a GL_FLOAT32 depth texture into a 32 bits UINT texture
#if HAS_CLIP_CONTROL
SV_Target1 = uint(exp2(32.0f) * sample_c().r);
#else
SV_Target1 = uint(exp2(24.0f) * sample_c().r);
#endif
}
#endif

#ifdef ps_convert_float32_rgba8
void ps_convert_float32_rgba8()
{
// Convert a GL_FLOAT32 depth texture into a RGBA color texture
#if HAS_CLIP_CONTROL
uint d = uint(sample_c().r * exp2(32.0f));
#else
uint d = uint(sample_c().r * exp2(24.0f));
#endif
SV_Target0 = vec4(uvec4((d & 0xFFu), ((d >> 8) & 0xFFu), ((d >> 16) & 0xFFu), (d >> 24))) / vec4(255.0);
}
#endif
Expand All @@ -105,53 +97,33 @@ void ps_convert_float32_rgba8()
void ps_convert_float16_rgb5a1()
{
// Convert a GL_FLOAT32 (only 16 lsb) depth into a RGB5A1 color texture
#if HAS_CLIP_CONTROL
uint d = uint(sample_c().r * exp2(32.0f));
#else
uint d = uint(sample_c().r * exp2(24.0f));
#endif
SV_Target0 = vec4(uvec4(d << 3, d >> 2, d >> 7, d >> 8) & uvec4(0xf8, 0xf8, 0xf8, 0x80)) / 255.0f;
}
#endif

float rgba8_to_depth32(vec4 unorm)
{
uvec4 c = uvec4(unorm * vec4(255.5f));
#if HAS_CLIP_CONTROL
return float(c.r | (c.g << 8) | (c.b << 16) | (c.a << 24)) * exp2(-32.0f);
#else
return float(c.r | (c.g << 8) | (c.b << 16) | (c.a << 24)) * exp2(-24.0f);
#endif
}

float rgba8_to_depth24(vec4 unorm)
{
uvec3 c = uvec3(unorm.rgb * vec3(255.5f));
#if HAS_CLIP_CONTROL
return float(c.r | (c.g << 8) | (c.b << 16)) * exp2(-32.0f);
#else
return float(c.r | (c.g << 8) | (c.b << 16)) * exp2(-24.0f);
#endif
}

float rgba8_to_depth16(vec4 unorm)
{
uvec2 c = uvec2(unorm.rg * vec2(255.5f));
#if HAS_CLIP_CONTROL
return float(c.r | (c.g << 8)) * exp2(-32.0f);
#else
return float(c.r | (c.g << 8)) * exp2(-24.0f);
#endif
}

float rgb5a1_to_depth16(vec4 unorm)
{
uvec4 c = uvec4(unorm * vec4(255.5f));
#if HAS_CLIP_CONTROL
return float(((c.r & 0xF8u) >> 3) | ((c.g & 0xF8u) << 2) | ((c.b & 0xF8u) << 7) | ((c.a & 0x80u) << 8)) * exp2(-32.0f);
#else
return float(((c.r & 0xF8u) >> 3) | ((c.g & 0xF8u) << 2) | ((c.b & 0xF8u) << 7) | ((c.a & 0x80u) << 8)) * exp2(-24.0f);
#endif
}

#ifdef ps_convert_rgba8_float32
Expand Down
16 changes: 2 additions & 14 deletions bin/resources/shaders/opengl/tfx_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,7 @@ mat4 sample_4p(uvec4 u)

int fetch_raw_depth()
{
#if HAS_CLIP_CONTROL
float multiplier = exp2(32.0f);
#else
float multiplier = exp2(24.0f);
#endif

#if PS_TEX_IS_FB == 1
return int(fetch_rt().r * multiplier);
Expand Down Expand Up @@ -423,21 +419,13 @@ vec4 sample_depth(vec2 st)
#elif PS_DEPTH_FMT == 1
// Based on ps_convert_float32_rgba8 of convert
// Convert a GL_FLOAT32 depth texture into a RGBA color texture
#if HAS_CLIP_CONTROL
uint d = uint(fetch_c(uv).r * exp2(32.0f));
#else
uint d = uint(fetch_c(uv).r * exp2(24.0f));
#endif
uint d = uint(fetch_c(uv).r * exp2(32.0f));
t = vec4(uvec4((d & 0xFFu), ((d >> 8) & 0xFFu), ((d >> 16) & 0xFFu), (d >> 24)));

#elif PS_DEPTH_FMT == 2
// Based on ps_convert_float16_rgb5a1 of convert
// Convert a GL_FLOAT32 (only 16 lsb) depth into a RGB5A1 color texture
#if HAS_CLIP_CONTROL
uint d = uint(fetch_c(uv).r * exp2(32.0f));
#else
uint d = uint(fetch_c(uv).r * exp2(24.0f));
#endif
uint d = uint(fetch_c(uv).r * exp2(32.0f));
t = vec4(uvec4((d & 0x1Fu), ((d >> 5) & 0x1Fu), ((d >> 10) & 0x1Fu), (d >> 15) & 0x01u)) * vec4(8.0f, 8.0f, 8.0f, 128.0f);

#elif PS_DEPTH_FMT == 3
Expand Down
27 changes: 5 additions & 22 deletions bin/resources/shaders/opengl/tfx_vgs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,10 @@ void vs_main()
// example: ceil(afterseveralvertextransformations(y = 133)) => 134 => line 133 stays empty
// input granularity is 1/16 pixel, anything smaller than that won't step drawing up/left by one pixel
// example: 133.0625 (133 + 1/16) should start from line 134, ceil(133.0625 - 0.05) still above 133
vec4 p;

p.xy = vec2(i_p) - vec2(0.05f, 0.05f);
p.xy = p.xy * VertexScale - VertexOffset;
p.w = 1.0f;

#if HAS_CLIP_CONTROL
p.z = float(z) * exp_min32;
#else
// GLES doesn't support ARB_clip_control, so remap it to -1..1. We also reduce the range from 32 bits
// to 24 bits, which means some games with very large depth ranges will not render correctly. But,
// for most, it's okay, and really, the best we can do.
p.z = min(float(z) * exp2(-23.0f), 2.0f) - 1.0f;
#endif

gl_Position = p;
gl_Position.xy = vec2(i_p) - vec2(0.05f, 0.05f);
gl_Position.xy = gl_Position.xy * VertexScale - VertexOffset;
gl_Position.z = float(z) * exp_min32;
gl_Position.w = 1.0f;

texture_coord();

Expand Down Expand Up @@ -144,13 +132,8 @@ ProcessedVertex load_vertex(uint index)
uint z = min(i_z, MaxDepth);
vtx.p.xy = vec2(i_p) - vec2(0.05f, 0.05f);
vtx.p.xy = vtx.p.xy * VertexScale - VertexOffset;
vtx.p.w = 1.0f;

#if HAS_CLIP_CONTROL
vtx.p.z = float(z) * exp_min32;
#else
vtx.p.z = min(float(z) * exp2(-23.0f), 2.0f) - 1.0f;
#endif
vtx.p.w = 1.0f;

vec2 uv = vec2(i_uv) - TextureOffset;
vec2 st = i_st - TextureOffset;
Expand Down
1 change: 0 additions & 1 deletion pcsx2/GS/Renderers/Common/GSDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,6 @@ class GSDevice : public GSAlignedClass<32>
bool dxt_textures : 1; ///< Supports DXTn texture compression, i.e. S3TC and BC1-3.
bool bptc_textures : 1; ///< Supports BC6/7 texture compression.
bool framebuffer_fetch : 1; ///< Can sample from the framebuffer without texture barriers.
bool clip_control : 1; ///< Can use 0..1 depth range instead of -1..1.
bool stencil_buffer : 1; ///< Supports stencil buffer, and can use for DATE.
bool cas_sharpening : 1; ///< Supports sufficient functionality for contrast adaptive sharpening.
bool test_and_sample_depth: 1; ///< Supports concurrently binding the depth-stencil buffer for sampling and depth testing.
Expand Down
1 change: 0 additions & 1 deletion pcsx2/GS/Renderers/DX11/GSDevice11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ GSDevice11::GSDevice11()
m_features.bptc_textures = false;
m_features.framebuffer_fetch = false;
m_features.stencil_buffer = true;
m_features.clip_control = true;
m_features.cas_sharpening = true;
m_features.test_and_sample_depth = false;
}
Expand Down
1 change: 0 additions & 1 deletion pcsx2/GS/Renderers/DX12/GSDevice12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,6 @@ bool GSDevice12::CheckFeatures()
m_features.point_expand = false;
m_features.line_expand = false;
m_features.framebuffer_fetch = false;
m_features.clip_control = true;
m_features.stencil_buffer = true;
m_features.cas_sharpening = true;
m_features.test_and_sample_depth = false;
Expand Down
6 changes: 3 additions & 3 deletions pcsx2/GS/Renderers/HW/GSRendererHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3393,7 +3393,7 @@ void GSRendererHW::EmulateZbuffer(const GSTextureCache::Target* ds)
}
else if (!m_cached_ctx.ZBUF.ZMSK)
{
m_conf.cb_ps.TA_MaxDepth_Af.z = static_cast<float>(max_z) * (g_gs_device->Features().clip_control ? 0x1p-32f : 0x1p-24f);
m_conf.cb_ps.TA_MaxDepth_Af.z = static_cast<float>(max_z) * 0x1p-32f;
m_conf.ps.zclamp = 1;
}
}
Expand Down Expand Up @@ -3950,7 +3950,7 @@ void GSRendererHW::EmulateBlending(int rt_alpha_min, int rt_alpha_max, bool& DAT
const bool blend_non_recursive = !!(blend_flag & BLEND_NO_REC);

// BLEND MIX selection, use a mix of hw/sw blending
const bool blend_mix1 = !!(blend_flag & BLEND_MIX1) && !(m_conf.ps.blend_b == m_conf.ps.blend_d && (alpha_c0_high_min_one || alpha_c2_high_one));
const bool blend_mix1 = !!(blend_flag & BLEND_MIX1);
const bool blend_mix2 = !!(blend_flag & BLEND_MIX2);
const bool blend_mix3 = !!(blend_flag & BLEND_MIX3);
bool blend_mix = (blend_mix1 || blend_mix2 || blend_mix3) && COLCLAMP.CLAMP;
Expand Down Expand Up @@ -6395,7 +6395,7 @@ bool GSRendererHW::TryTargetClear(GSTextureCache::Target* rt, GSTextureCache::Ta
{
const u32 max_z = 0xFFFFFFFF >> (GSLocalMemory::m_psm[m_cached_ctx.ZBUF.PSM].fmt * 8);
const u32 z = std::min(max_z, m_vertex.buff[1].XYZ.Z);
const float d = static_cast<float>(z) * (g_gs_device->Features().clip_control ? 0x1p-32f : 0x1p-24f);
const float d = static_cast<float>(z) * 0x1p-32f;
GL_INS("TryTargetClear(): DS at %x <= %f", ds->m_TEX0.TBP0, d);
g_gs_device->ClearDepth(ds->m_texture, d);
ds->m_alpha_max = z >> 24;
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/GS/Renderers/HW/GSTextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2795,7 +2795,7 @@ void GSTextureCache::ScaleTargetForDisplay(Target* t, const GIFRegTEX0& dispfb,

float GSTextureCache::ConvertColorToDepth(u32 c, ShaderConvert convert)
{
const float mult = std::exp2(g_gs_device->Features().clip_control ? -32.0f : -24.0f);
const float mult = std::exp2(-32.0f);
switch (convert)
{
case ShaderConvert::RGB5A1_TO_FLOAT16:
Expand All @@ -2817,7 +2817,7 @@ float GSTextureCache::ConvertColorToDepth(u32 c, ShaderConvert convert)

u32 GSTextureCache::ConvertDepthToColor(float d, ShaderConvert convert)
{
const float mult = std::exp2(g_gs_device->Features().clip_control ? 32.0f : 24.0f);
const float mult = std::exp2(32.0f);
switch (convert)
{
case ShaderConvert::FLOAT16_TO_RGB5A1:
Expand Down
1 change: 0 additions & 1 deletion pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,6 @@ static void OnMainThread(Fn&& fn)
m_features.dxt_textures = true;
m_features.bptc_textures = true;
m_features.framebuffer_fetch = m_dev.features.framebuffer_fetch && !GSConfig.DisableFramebufferFetch;
m_features.clip_control = true;
m_features.stencil_buffer = true;
m_features.cas_sharpening = true;
m_features.test_and_sample_depth = true;
Expand Down
18 changes: 7 additions & 11 deletions pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ bool GSDeviceOGL::Create()
// This extension allow FS depth to range from -1 to 1. So
// gl_position.z could range from [0, 1]
// Change depth convention
if (m_features.clip_control)
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);

// ****************************************************************
// HW renderer shader
Expand Down Expand Up @@ -671,6 +670,12 @@ bool GSDeviceOGL::CheckFeatures(bool& buggy_pbo)
"GS", "GL_ARB_copy_image is not supported, this is required for the OpenGL renderer.");
return false;
}
if (!GLAD_GL_VERSION_4_5 && !GLAD_GL_ARB_clip_control)
{
Host::ReportFormattedErrorAsync(
"GS", "GL_ARB_clip_control is not supported, this is required for the OpenGL renderer.");
return false;
}

if (!GLAD_GL_ARB_viewport_array)
{
Expand Down Expand Up @@ -733,10 +738,6 @@ bool GSDeviceOGL::CheckFeatures(bool& buggy_pbo)
m_features.bptc_textures =
GLAD_GL_VERSION_4_2 || GLAD_GL_ARB_texture_compression_bptc || GLAD_GL_EXT_texture_compression_bptc;
m_features.prefer_new_textures = false;
m_features.clip_control = GLAD_GL_ARB_clip_control;
if (!m_features.clip_control)
Host::AddOSDMessage(
"GL_ARB_clip_control is not supported, this will cause rendering issues.", Host::OSD_ERROR_DURATION);
m_features.stencil_buffer = true;
m_features.test_and_sample_depth = m_features.texture_barrier;

Expand Down Expand Up @@ -1281,11 +1282,6 @@ std::string GSDeviceOGL::GenGlslHeader(const std::string_view& entry, GLenum typ
else
header += "#define HAS_FRAMEBUFFER_FETCH 0\n";

if (m_features.clip_control)
header += "#define HAS_CLIP_CONTROL 1\n";
else
header += "#define HAS_CLIP_CONTROL 0\n";

// Allow to puts several shader in 1 files
switch (type)
{
Expand Down
3 changes: 1 addition & 2 deletions pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2669,7 +2669,6 @@ bool GSDeviceVK::CheckFeatures()

m_features.prefer_new_textures = true;
m_features.provoking_vertex_last = m_optional_extensions.vk_ext_provoking_vertex;
m_features.clip_control = true;
m_features.vs_expand = !GSConfig.DisableVertexShaderExpand;

if (!m_features.texture_barrier)
Expand Down Expand Up @@ -2699,7 +2698,7 @@ bool GSDeviceVK::CheckFeatures()
m_features.line_expand =
(m_device_features.wideLines && limits.lineWidthRange[0] <= f_upscale && limits.lineWidthRange[1] >= f_upscale);

DevCon.WriteLn("Optional features:%s%s%s%s%s%s", m_features.primitive_id ? " primitive_id" : "",
DevCon.WriteLn("Optional features:%s%s%s%s%s", m_features.primitive_id ? " primitive_id" : "",
m_features.texture_barrier ? " texture_barrier" : "", m_features.framebuffer_fetch ? " framebuffer_fetch" : "",
m_features.provoking_vertex_last ? " provoking_vertex_last" : "", m_features.vs_expand ? " vs_expand" : "");
DevCon.WriteLn("Using %s for point expansion and %s for line expansion.",
Expand Down

0 comments on commit 11ee0a8

Please sign in to comment.