Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/tests/video_core/writeback_switches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ TEST_CASE("Overlap write-backs stay on only for the tested titles",
REQUIRE_FALSE(
WritebackDefault(FireEmblemFortunesWeaveProgramId, RendererBackend::OpenGL_SPIRV));
}

TEST_CASE("EDEN_NX2_OOT_STALE_WRITEBACK takes 1 or 2 and nothing else",
"[video_core][texture_cache][writeback]") {
using namespace VideoCommon::WritebackSwitches;
REQUIRE(ParseOcarinaStaleWriteback(nullptr) == OcarinaStaleWriteback::Off);
REQUIRE(ParseOcarinaStaleWriteback("") == OcarinaStaleWriteback::Off);
REQUIRE(ParseOcarinaStaleWriteback("0") == OcarinaStaleWriteback::Off);
REQUIRE(ParseOcarinaStaleWriteback("1") == OcarinaStaleWriteback::Sampled);
REQUIRE(ParseOcarinaStaleWriteback("2") == OcarinaStaleWriteback::Default);
REQUIRE(ParseOcarinaStaleWriteback("3") == OcarinaStaleWriteback::Off);
REQUIRE(ParseOcarinaStaleWriteback("12") == OcarinaStaleWriteback::Off);
}
5 changes: 5 additions & 0 deletions src/video_core/texture_cache/image_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ struct ImageBase {
u64 load_tick = 0;
/// frame_tick + 1 of the first frame a render pass other than a clear wrote it, or 0.
u64 first_written_frame = 0;
/// frame_tick + 1 of the frame the GPU last wrote it (for contents joined or synchronized from
/// other images, the frame they were written), and of the last frame it was prepared for
/// reading, or 0 (EDEN_NX2_OOT_STALE_WRITEBACK=1).
u64 last_written_frame = 0;
u64 last_sampled_frame = 0;
bool xc2_lut_view = false;
/// Levels whose every layer a shader last wrote through a storage view. Any other GPU write
/// (render pass, clear, copy, alias copy) and an upload from guest memory clear them.
Expand Down
35 changes: 33 additions & 2 deletions src/video_core/texture_cache/texture_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ void TextureCache<P>::RunGarbageCollector(bool out_of_memory, bool at_critical)
// Not const, here and below: with OpenGL both would be constant expressions, which the
// lambda below must then not capture (-Wunused-lambda-capture), while Vulkan's must be.
bool download_overlaps = std::is_trivially_destructible_v<DownloadMap> &&
WritebackSwitches::WritebackDefault();
WritebackSwitches::AnyWritebackOn();
// EDEN_NX2_OOT_STALE_WRITEBACK=1: only the overlaps a later frame sampled after their last
// write. Ocarina renders its pause menu's item preview once and samples it every frame; once
// the collector dropped it, it was made again over the stale pages without an upload and
// showed recycled host memory (#2056).
bool sampled_only = !WritebackSwitches::WritebackDefault();
// The collector does not wait for its
// write-backs (each wait was 5-10 ms, several a second in combat); they reach guest memory
// when the GPU is done with them.
Expand Down Expand Up @@ -240,7 +245,8 @@ void TextureCache<P>::RunGarbageCollector(bool out_of_memory, bool at_critical)
};
const auto Cleanup = [this, &num_iterations, &high_priority_mode, &aggressive_mode, &CanEvict,
&evict_bad_overlaps, &keep, &keep_ticks, out_of_memory,
&overlap_downloads, download_overlaps, async_downloads](ImageId image_id) {
&overlap_downloads, download_overlaps, async_downloads,
sampled_only](ImageId image_id) {
if (num_iterations == 0) {
return true;
}
Expand All @@ -255,6 +261,8 @@ void TextureCache<P>::RunGarbageCollector(bool out_of_memory, bool at_critical)
// already hold all of it (then its data is not lost with it).
const bool overlap_writeback =
download_overlaps && True(image.flags & ImageFlagBits::BadOverlap) &&
(!sampled_only || (image.last_written_frame != 0 &&
image.last_sampled_frame > image.last_written_frame)) &&
(async_downloads || image.guest_size_bytes <= OverlapDownloadMaxBytes()) &&
IsDownloadable(image) && HasGuestRange(image);
const bool covered =
Expand Down Expand Up @@ -297,6 +305,18 @@ void TextureCache<P>::RunGarbageCollector(bool out_of_memory, bool at_critical)
++gc_download_deletes;
} else if (overlap_writeback && !covered) {
download_queued = true;
if (sampled_only) {
static u64 sampled_writebacks{};
if (sampled_writebacks++ % 256 < 8) {
LOG_INFO(HW_GPU,
"OoT stale write-back #{}: gpu={:#x} {}x{} format={} {} KiB, "
"written frame {}, sampled frame {}",
sampled_writebacks, image.gpu_addr, image.info.size.width,
image.info.size.height, static_cast<u32>(image.info.format),
image.guest_size_bytes / 1024, image.last_written_frame - 1,
image.last_sampled_frame - 1);
}
}
NoteOverlapWriteback(image);
// Deferred also when waiting: a later image's synchronous download in this pass
// finishes the GPU work, after which the pool would hand this buffer out again.
Expand Down Expand Up @@ -5021,6 +5041,8 @@ ImageId TextureCache<P>::JoinImages(const ImageInfo& info, GPUVAddr gpu_addr, DA
}
CopyImage(new_image_id, aliased->id, aliased->copies);
SetModificationTick(slot_images, new_image, overlap.modification_tick);
new_image.last_written_frame =
(std::max)(new_image.last_written_frame, overlap.last_written_frame);
continue;
}
if (True(overlap.flags & ImageFlagBits::GpuModified)) {
Expand All @@ -5036,6 +5058,8 @@ ImageId TextureCache<P>::JoinImages(const ImageInfo& info, GPUVAddr gpu_addr, DA
runtime.CopyImage(new_image, overlap, copies);
}
SetModificationTick(slot_images, new_image, overlap.modification_tick);
new_image.last_written_frame =
(std::max)(new_image.last_written_frame, overlap.last_written_frame);
}
if (True(overlap.flags & ImageFlagBits::Tracked)) {
UntrackImage(overlap, copy_object.id);
Expand Down Expand Up @@ -5993,6 +6017,7 @@ void TextureCache<P>::RemoveFramebuffers(std::span<const ImageViewId> removed_vi
template <class P>
void TextureCache<P>::MarkModification(ImageBase& image) noexcept {
image.flags |= ImageFlagBits::GpuModified;
image.last_written_frame = frame_tick + 1;
image.storage_written_levels = 0;
SetModificationTick(slot_images, image, ++modification_tick);
if (image.overlapping_images.empty() || Settings::GetCurrentProgramID() != Xc2ProgramId) {
Expand Down Expand Up @@ -6021,10 +6046,12 @@ void TextureCache<P>::SynchronizeAliases(ImageId image_id) {
bool any_rescaled = True(image.flags & ImageFlagBits::Rescaled);
bool any_modified = True(image.flags & ImageFlagBits::GpuModified);
u64 most_recent_tick = image.modification_tick;
u64 last_written_frame = image.last_written_frame;
for (const AliasedImage& aliased : image.aliased_images) {
ImageBase& aliased_image = slot_images[aliased.id];
if (image.modification_tick < aliased_image.modification_tick) {
most_recent_tick = (std::max)(most_recent_tick, aliased_image.modification_tick);
last_written_frame = (std::max)(last_written_frame, aliased_image.last_written_frame);
aliased_images.push_back(&aliased);
any_rescaled |= True(aliased_image.flags & ImageFlagBits::Rescaled);
any_modified |= True(aliased_image.flags & ImageFlagBits::GpuModified);
Expand All @@ -6045,6 +6072,7 @@ void TextureCache<P>::SynchronizeAliases(ImageId image_id) {
}
SetModificationTick(slot_images, image, most_recent_tick);
image.newest_alias_tick = most_recent_tick;
image.last_written_frame = last_written_frame;
image.storage_written_levels = 0;
if (any_modified) {
image.flags |= ImageFlagBits::GpuModified;
Expand Down Expand Up @@ -6080,6 +6108,9 @@ void TextureCache<P>::PrepareImage(ImageId image_id, bool is_modification, bool
}
}
Image& image = slot_images[image_id];
if (!is_modification) {
image.last_sampled_frame = frame_tick + 1;
}
if (is_modification && !invalidate && !async_overlap_downloads.empty() &&
WritebackWouldReload(image)) {
// Its write-back landing would reload it, but not once the GPU has written to it: a
Expand Down
42 changes: 39 additions & 3 deletions src/video_core/texture_cache/writeback_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#pragma once

#include <cstdlib>

#include "common/common_types.h"
#include "common/settings.h"

Expand All @@ -11,6 +13,7 @@ namespace VideoCommon::WritebackSwitches {
inline constexpr u64 DuskbloodsNetworkTestProgramId = 0x0400D1D02399A000ULL;
inline constexpr u64 KirbyForgottenLandNs2eProgramId = 0x04004D300C5AE001ULL;
inline constexpr u64 FireEmblemFortunesWeaveProgramId = 0x04007EF01F8AE000ULL;
inline constexpr u64 OcarinaOfTimeProgramId = 0x0400486024DEC000ULL;

/// Titles whose collector and join write-backs and stale-page uploads are on: they sample
/// GPU-written images the collector would drop, which otherwise show recycled host memory
Expand All @@ -25,13 +28,46 @@ inline constexpr u64 FireEmblemFortunesWeaveProgramId = 0x04007EF01F8AE000ULL;
backend == Settings::RendererBackend::Vulkan);
}

/// EDEN_NX2_OOT_STALE_WRITEBACK, Ocarina of Time only (#2056 test): 1 writes back the BadOverlap
/// images the collector drops that a later frame sampled after their last write (the pause
/// menu's item preview), 2 turns on the WritebackDefault set. Unset or anything else: off.
enum class OcarinaStaleWriteback : u8 { Off, Sampled, Default };

[[nodiscard]] constexpr OcarinaStaleWriteback ParseOcarinaStaleWriteback(const char* value) {
if (value == nullptr || value[0] == '\0' || value[1] != '\0') {
return OcarinaStaleWriteback::Off;
}
return value[0] == '1' ? OcarinaStaleWriteback::Sampled
: value[0] == '2' ? OcarinaStaleWriteback::Default
: OcarinaStaleWriteback::Off;
}

[[nodiscard]] inline OcarinaStaleWriteback RequestedOcarinaStaleWriteback() {
static const OcarinaStaleWriteback mode =
ParseOcarinaStaleWriteback(std::getenv("EDEN_NX2_OOT_STALE_WRITEBACK"));
return mode;
}

[[nodiscard]] inline OcarinaStaleWriteback OcarinaStaleWritebackMode() {
return Settings::GetCurrentProgramID() == OcarinaOfTimeProgramId
? RequestedOcarinaStaleWriteback()
: OcarinaStaleWriteback::Off;
}

[[nodiscard]] inline bool WritebackDefault() {
return WritebackDefault(Settings::GetCurrentProgramID(),
Settings::values.renderer_backend.GetValue());
const Settings::RendererBackend backend = Settings::values.renderer_backend.GetValue();
return WritebackDefault(Settings::GetCurrentProgramID(), backend) ||
(OcarinaStaleWritebackMode() == OcarinaStaleWriteback::Default &&
backend == Settings::RendererBackend::Vulkan);
}

/// The collector writes back only the BadOverlap images sampled after their last write.
[[nodiscard]] inline bool SampledWriteback() {
return OcarinaStaleWritebackMode() == OcarinaStaleWriteback::Sampled;
}

[[nodiscard]] inline bool AnyWritebackOn() {
return WritebackDefault();
return WritebackDefault() || SampledWriteback();
}

[[nodiscard]] inline bool UploadStale() {
Expand Down
Loading