[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

@@ -246,8 +246,7 @@ void SharedMemory::FireWatches(uint32_t page_first, uint32_t page_last,
}
}
void SharedMemory::RangeWrittenByGpu(uint32_t start, uint32_t length,
bool is_resolve) {
void SharedMemory::RangeWrittenByGpu(uint32_t start, uint32_t length) {
if (length == 0 || start >= kBufferSize) {
return;
}
@@ -262,7 +261,7 @@ void SharedMemory::RangeWrittenByGpu(uint32_t start, uint32_t length,
// Mark the range as valid (so pages are not reuploaded until modified by the
// CPU) and watch it so the CPU can reuse it and this will be caught.
MakeRangeValid(start, length, true, is_resolve);
MakeRangeValid(start, length, true);
}
bool SharedMemory::AllocateSparseHostGpuMemoryRange(
@@ -274,9 +273,7 @@ bool SharedMemory::AllocateSparseHostGpuMemoryRange(
}
void SharedMemory::MakeRangeValid(uint32_t start, uint32_t length,
bool written_by_gpu,
bool written_by_gpu_resolve) {
assert_false(written_by_gpu_resolve && !written_by_gpu);
bool written_by_gpu) {
if (length == 0 || start >= kBufferSize) {
return;
}
@@ -305,11 +302,6 @@ void SharedMemory::MakeRangeValid(uint32_t start, uint32_t length,
} else {
block.valid_and_gpu_written &= ~valid_bits;
}
if (written_by_gpu_resolve) {
block.valid_and_gpu_resolved |= valid_bits;
} else {
block.valid_and_gpu_resolved &= ~valid_bits;
}
}
}
@@ -344,13 +336,9 @@ void SharedMemory::UnlinkWatchRange(WatchRange* range) {
watch_range_first_free_ = range;
}
bool SharedMemory::RequestRange(uint32_t start, uint32_t length,
bool* any_data_resolved_out) {
bool SharedMemory::RequestRange(uint32_t start, uint32_t length) {
if (!length) {
// Some texture or buffer is empty, for example - safe to draw in this case.
if (any_data_resolved_out) {
*any_data_resolved_out = false;
}
return true;
}
if (start > kBufferSize || (kBufferSize - start) < length) {
@@ -376,20 +364,14 @@ bool SharedMemory::RequestRange(uint32_t start, uint32_t length,
for (uint32_t i = block_first; i <= block_last; ++i) {
const SystemPageFlagsBlock& block = system_page_flags_[i];
uint64_t block_valid = block.valid;
uint64_t block_resolved = block.valid_and_gpu_resolved;
// Consider pages in the block outside the requested range valid.
if (i == block_first) {
uint64_t block_before = (uint64_t(1) << (page_first & 63)) - 1;
block_valid |= block_before;
block_resolved &= ~block_before;
}
if (i == block_last && (page_last & 63) != 63) {
uint64_t block_inside = (uint64_t(1) << ((page_last & 63) + 1)) - 1;
block_valid |= ~block_inside;
block_resolved &= block_inside;
}
if (block_resolved) {
any_data_resolved = true;
}
while (true) {
@@ -425,9 +407,6 @@ bool SharedMemory::RequestRange(uint32_t start, uint32_t length,
upload_ranges_.push_back(
std::make_pair(range_start, page_last + 1 - range_start));
}
if (any_data_resolved_out) {
*any_data_resolved_out = any_data_resolved;
}
if (upload_ranges_.empty()) {
return true;
}
@@ -493,7 +472,6 @@ std::pair<uint32_t, uint32_t> SharedMemory::MemoryInvalidationCallback(
SystemPageFlagsBlock& block = system_page_flags_[i];
block.valid &= ~invalidate_bits;
block.valid_and_gpu_written &= ~invalidate_bits;
block.valid_and_gpu_resolved &= ~invalidate_bits;
}
FireWatches(page_first, page_last, false);