[GPU] 8-bit PWL gamma RT as linear 16-bit UNorm on the host

With render target HLE, directly store linear values as R16G16B16A16_UNORM
without gamma conversion, as this format provides more than enough bits
(need at least 11 per component due to the maximum scale being 2^3 in the
piecewise linear gamma curve) to represent linear values without precision
loss.

This makes blending work correctly in linear space, improving quality of
transparency, lighting passes, and fixing issues such as transparent parts
of impact and footstep decals in 4D5307E6 being bright instead.

The new behavior is enabled by default, as it hugely improves the accuracy
of emulation of this format, that is pretty commonplace in Xbox 360 games,
with likely just a small GPU memory and bandwidth usage increase, compared
to the alternatives that were previously available on the HLE RB path.

It's currently implemented only on Direct3D 12, as most of the current GPU
emulation code is planned to be phased out and redone, and no methods other
than 8-bit with pre-conversion were implemented on Vulkan previously.

To implement on Vulkan later, same conversion as in the Direct3D 12
implementation will need to be done in ownership transfer and resolve
shaders. Currently it's somewhat inconvenient to decouple the conversion
functions in `SpirvShaderTranslator` from an instance of the translator due
to vector constant usage. Later, simpler SPIR-V generation functions may be
added (`spv::Builder` usage in general is overly verbose).

The previously default method (8-bit storage with pre-conversion in shaders
and incorrect blending) can be re-enabled by setting the
"gamma_render_target_as_unorm16" configuration option to `false`. This may
be useful if the game, for instance, switches between 8_8_8_8_GAMMA and
8_8_8_8 formats for the same data frequently, as switching will result in
EDRAM range ownership transfer data copying now. Also, the old path is
preserved for Vulkan devices not supporting R16G16B16A16_UNORM with
blending.

The other workaround that was available previously, replacing the PWL
encoding with host hardware sRGB with linear-space blending in render
target management and in texture fetching, was also inherently inaccurate
in many ways (especially when games have their own PWL encoding math, like
4541080F that displayed incorrect colors on the loading screen), and
required tracking of the encoding needed for ranges in the memory.

The sRGB workaround therefore was deleted in this commit, greatly
simplifying the code in the parts of render target, texture and memory
management and shader generation that were involved in it.
This commit is contained in:
Triang3l
2026-01-17 18:09:42 +03:00
parent f2fabfdf04
commit cec9ca0ef2
34 changed files with 3072 additions and 3188 deletions

View File

@@ -149,16 +149,15 @@ DEFINE_bool(
"into account for render-to-texture, for more correct shadow filtering, "
"bloom, etc., in some cases.",
"GPU");
// Disabled by default because of full-screen effects that occur when game
// shaders assume piecewise linear (4541080F), much more severe than
// blending-related issues.
DEFINE_bool(
gamma_render_target_as_srgb, false,
"When the host can't write piecewise linear gamma directly with correct "
"blending, use sRGB output on the host for conceptually correct blending "
"in linear color space while having slightly different precision "
"distribution in the render target and severely incorrect values if the "
"game accesses the resulting colors directly as raw data.",
gamma_render_target_as_unorm16, true,
"When the host can't write 8 bits per component pixels with piecewise "
"linear gamma encoding directly with correct blending, use the 16-bit "
"unsigned normalized format, if supported, for conceptually correct "
"8_8_8_8_GAMMA render target format blending in linear color space. "
"Greatly increases accuracy for this format, but may result in render "
"target copying costs if the game switches between 8_8_8_8_GAMMA and "
"8_8_8_8 views for the same EDRAM render target.",
"GPU");
DEFINE_bool(
mrt_edram_used_range_clamp_to_min, true,
@@ -635,7 +634,6 @@ bool RenderTargetCache::Update(bool is_rasterization_done,
uint32_t edram_bases[1 + xenos::kMaxColorRenderTargets];
uint32_t resource_formats[1 + xenos::kMaxColorRenderTargets];
uint32_t rts_are_64bpp = 0;
uint32_t color_rts_are_gamma = 0;
if (is_rasterization_done) {
if (normalized_depth_control.z_enable ||
normalized_depth_control.stencil_enable) {
@@ -664,9 +662,6 @@ bool RenderTargetCache::Update(bool is_rasterization_done,
if (is_64bpp) {
rts_are_64bpp |= uint32_t(1) << rt_bit_index;
}
if (color_format == xenos::ColorRenderTargetFormat::k_8_8_8_8_GAMMA) {
color_rts_are_gamma |= uint32_t(1) << i;
}
xenos::ColorRenderTargetFormat color_resource_format;
if (interlock_barrier_only) {
// Only changes in mapping between coordinates and addresses are
@@ -752,7 +747,6 @@ bool RenderTargetCache::Update(bool is_rasterization_done,
if (!are_accumulated_render_targets_valid_) {
std::memset(last_update_accumulated_render_targets_, 0,
sizeof(last_update_accumulated_render_targets_));
last_update_accumulated_color_targets_are_gamma_ = 0;
}
return true;
}
@@ -961,26 +955,13 @@ bool RenderTargetCache::Update(bool is_rasterization_done,
std::memcpy(last_update_accumulated_render_targets_,
last_update_used_render_targets_,
sizeof(last_update_accumulated_render_targets_));
last_update_accumulated_color_targets_are_gamma_ = 0;
are_accumulated_render_targets_valid_ = true;
}
// Only update color space of render targets that actually matter here, don't
// disable gamma emulation (which may require ending the render pass) on the
// host, for example, if making a depth-only draw between color draws with a
// gamma target.
uint32_t color_rts_used_bits = depth_and_color_rts_used_bits >> 1;
// Ignore any render targets dropped before in this function for any reason.
color_rts_are_gamma &= color_rts_used_bits;
last_update_accumulated_color_targets_are_gamma_ =
(last_update_accumulated_color_targets_are_gamma_ &
~color_rts_used_bits) |
color_rts_are_gamma;
return true;
}
uint32_t RenderTargetCache::GetLastUpdateBoundRenderTargets(
bool distinguish_gamma_formats,
uint32_t* depth_and_color_formats_out) const {
if (GetPath() != Path::kHostRenderTargets) {
if (depth_and_color_formats_out) {
@@ -1001,12 +982,7 @@ uint32_t RenderTargetCache::GetLastUpdateBoundRenderTargets(
}
rts_used |= uint32_t(1) << i;
if (depth_and_color_formats_out) {
depth_and_color_formats_out[i] =
(distinguish_gamma_formats && i &&
(last_update_accumulated_color_targets_are_gamma_ &
(uint32_t(1) << (i - 1))))
? uint32_t(xenos::ColorRenderTargetFormat::k_8_8_8_8_GAMMA)
: render_target->key().resource_format;
depth_and_color_formats_out[i] = render_target->key().resource_format;
}
}
return rts_used;