Skip to content
Merged
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
10 changes: 9 additions & 1 deletion core/kyty/graphics/host_gpu/graphicContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ struct GraphicContext {
[[nodiscard]] bool CreateAllocator();
void DestroyAllocator();
void LogMemoryBudget() const;
[[nodiscard]] bool CanReportMemoryUsage() const noexcept { return memory_budget_ext_enabled; }
[[nodiscard]] bool CanReportMemoryUsage() const noexcept {
return memory_budget_ext_enabled || m_memory_override_enabled;
}
[[nodiscard]] uint64_t GetDeviceMemoryUsage() const;
[[nodiscard]] uint64_t GetTotalMemoryBudget() const;

Expand All @@ -140,11 +142,17 @@ struct GraphicContext {
[[nodiscard]] uint32_t GetHeapUsage(std::span<HeapUsage, VK_MAX_MEMORY_HEAPS> heaps) const;
[[nodiscard]] bool CreateImage(const vk::ImageCreateInfo& info, VulkanImage& image);
void DeleteImage(VulkanImage& image);
void TrimRecycledImages();
[[nodiscard]] size_t RecycledImageCount() const;

uint32_t screen_width = 0;
uint32_t screen_height = 0;

private:
friend struct VramBudgetTestAccess;
bool m_memory_override_enabled = false;
uint64_t m_memory_budget_override = 0;
uint64_t m_memory_usage_override = 0;
mutable std::mutex m_format_properties_mutex;
mutable std::map<vk::Format, vk::FormatProperties> m_format_properties;
mutable std::mutex m_image_format_properties_mutex;
Expand Down
22 changes: 15 additions & 7 deletions core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,19 @@ BufferCache::BufferCache(GraphicContext& graphics, CommandScheduler& scheduler,
if (!m_graphics.CanReportMemoryUsage()) {
return;
}
UpdateMemoryThresholds(m_graphics.GetTotalMemoryBudget());
}

void BufferCache::UpdateMemoryThresholds(uint64_t budget_bytes) {
m_memory_budget = budget_bytes;
constexpr int64_t GiB = 1024ll * 1024 * 1024;
constexpr int64_t target_threshold = 8 * GiB;
const auto budget =
static_cast<int64_t>(std::min<uint64_t>(m_graphics.GetTotalMemoryBudget(), INT64_MAX));
const auto threshold = std::min(budget, target_threshold);
const auto expected = std::min(budget - 6 * threshold / 10, budget - GiB);
const auto critical = std::min(budget - 2 * threshold / 10, budget - GiB / 2);
m_trigger_gc_memory = static_cast<uint64_t>(std::max<int64_t>(expected, GiB));
m_critical_gc_memory = static_cast<uint64_t>(std::max<int64_t>(critical, 2 * GiB));
const auto budget = static_cast<int64_t>(std::min<uint64_t>(budget_bytes, INT64_MAX));
const auto threshold = std::min(budget, target_threshold);
const auto expected = std::min(budget - 6 * threshold / 10, budget - GiB);
const auto critical = std::min(budget - 2 * threshold / 10, budget - GiB / 2);
m_trigger_gc_memory = static_cast<uint64_t>(std::max<int64_t>(expected, GiB));
m_critical_gc_memory = static_cast<uint64_t>(std::max<int64_t>(critical, 2 * GiB));
}

BufferCache::~BufferCache() {
Expand Down Expand Up @@ -3251,6 +3255,10 @@ void BufferCache::RunGarbageCollector() {
const auto tick = m_gc_tick++;
CollectStaleBuffers(tick);
if (m_graphics.CanReportMemoryUsage()) {
const auto budget = m_graphics.GetTotalMemoryBudget();
if (budget != m_memory_budget) {
UpdateMemoryThresholds(budget);
}
m_total_used_memory = m_graphics.GetDeviceMemoryUsage();
}
if (m_total_used_memory < m_trigger_gc_memory) {
Expand Down
3 changes: 3 additions & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class BufferCache {
private:
friend struct BufferCacheTestAccess;
friend struct BufferCacheGcTestAccess;
friend struct VramBudgetTestAccess;
friend struct IndirectDispatchTestAccess;
friend class TextureCache;

Expand Down Expand Up @@ -600,9 +601,11 @@ class BufferCache {
static constexpr size_t WrittenMemoSlots = 1024;
std::unique_ptr<std::array<WrittenMemo, WrittenMemoSlots>> m_written_memo =
std::make_unique<std::array<WrittenMemo, WrittenMemoSlots>>();
uint64_t m_memory_budget = 0;
uint64_t m_trigger_gc_memory = 1ull * 1024 * 1024 * 1024;
uint64_t m_critical_gc_memory = 2ull * 1024 * 1024 * 1024;
uint64_t m_gc_tick = 0;
void UpdateMemoryThresholds(uint64_t budget_bytes);
void NoteBufferRegions(const Buffer& buffer, bool insert);
void GrowRegionTables(uint64_t region);
std::vector<uint32_t> m_region_buffer_count;
Expand Down
33 changes: 22 additions & 11 deletions core/kyty/graphics/host_gpu/renderer/cache/textureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ TextureCache::TextureCache(GraphicContext& graphics, CommandScheduler& scheduler
m_readback_storage_images = write_back_value != "0";
#endif
if (m_graphics.CanReportMemoryUsage()) {
constexpr int64_t GiB = 1024ll * 1024 * 1024;
const auto budget =
static_cast<int64_t>(std::min<uint64_t>(m_graphics.GetTotalMemoryBudget(), INT64_MAX));
const auto threshold = std::min<int64_t>(budget, 8 * GiB);
m_pressure_gc_memory = static_cast<uint64_t>(
std::max<int64_t>(std::min(budget - 6 * threshold / 10, budget - GiB), GiB + GiB / 2));
m_critical_gc_memory = static_cast<uint64_t>(
std::max<int64_t>(std::min(budget - 2 * threshold / 10, budget - GiB / 2), 3 * GiB));
m_trigger_gc_memory = static_cast<uint64_t>(std::max<int64_t>((budget - threshold) / 2, 0));
UpdateMemoryThresholds(m_graphics.GetTotalMemoryBudget());
}
}

void TextureCache::UpdateMemoryThresholds(uint64_t budget_bytes) {
m_memory_budget = budget_bytes;
constexpr int64_t GiB = 1024ll * 1024 * 1024;
const auto budget = static_cast<int64_t>(std::min<uint64_t>(budget_bytes, INT64_MAX));
const auto threshold = std::min<int64_t>(budget, 8 * GiB);
m_pressure_gc_memory = static_cast<uint64_t>(
std::max<int64_t>(std::min(budget - 6 * threshold / 10, budget - GiB), GiB + GiB / 2));
m_critical_gc_memory = static_cast<uint64_t>(
std::max<int64_t>(std::min(budget - 2 * threshold / 10, budget - GiB / 2), 3 * GiB));
m_trigger_gc_memory = static_cast<uint64_t>(std::max<int64_t>((budget - threshold) / 2, 0));
}

TextureCache::~TextureCache() {
m_slot_images.ForEach([&](ImageId id, const Image& image) KYTY_NO_THREAD_SAFETY_ANALYSIS {
if (image.registered) {
Expand Down Expand Up @@ -3777,6 +3781,10 @@ void TextureCache::RunGarbageCollector() {
const uint64_t tick = m_gc_tick++;
DiscardParkedImages(0, 0);
if (m_graphics.CanReportMemoryUsage()) {
const auto budget = m_graphics.GetTotalMemoryBudget();
if (budget != m_memory_budget) {
UpdateMemoryThresholds(budget);
}
m_total_used_memory = m_graphics.GetDeviceMemoryUsage();
}
if (m_total_used_memory < m_trigger_gc_memory) {
Expand All @@ -3788,10 +3796,10 @@ void TextureCache::RunGarbageCollector() {
uint64_t age = 16;
size_t deletions = 10;
if (aggressive) {
age = 160;
age = 2;
deletions = 40;
} else if (pressured) {
age = GcDownloadAge;
age = 8;
deletions = 20;
}
age = std::min(age, tick);
Expand Down Expand Up @@ -3835,6 +3843,9 @@ void TextureCache::RunGarbageCollector() {
if (m_total_used_memory >= m_critical_gc_memory) {
collect(true);
}
if (m_total_used_memory >= m_pressure_gc_memory) {
m_graphics.TrimRecycledImages();
}
}

void TextureCache::ProcessDownloadImages() {
Expand Down
3 changes: 3 additions & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/textureCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,12 @@ class TextureCache {
std::unordered_map<ImageId, std::vector<ImageId>> m_stencil_copies;
std::vector<std::pair<ImageId, uint64_t>> m_parked_images;
std::map<uint64_t, MetaDataInfo> m_surface_metas;
uint64_t m_memory_budget = 0;
uint64_t m_total_used_memory = 0;
uint64_t m_trigger_gc_memory = 0;
uint64_t m_pressure_gc_memory = 1536ull * 1024 * 1024;
uint64_t m_critical_gc_memory = 3ull * 1024 * 1024 * 1024;
void UpdateMemoryThresholds(uint64_t budget_bytes);
uint64_t m_gc_tick = 0;
std::atomic<uint32_t> m_partial_candidates {0};
uint64_t m_image_set_epoch = 0;
Expand All @@ -499,6 +501,7 @@ class TextureCache {
[[nodiscard]] uint64_t ImageEpochInRegionUnlocked(uint64_t address, uint64_t size) const;

friend struct TextureCacheTestAccess;
friend struct VramBudgetTestAccess;
friend class BufferCache;
friend class RenderExecutor;
friend class ImageHeap;
Expand Down
18 changes: 18 additions & 0 deletions core/kyty/graphics/host_gpu/vma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ void GraphicContext::LogMemoryBudget() const {
}

uint64_t GraphicContext::GetDeviceMemoryUsage() const {
if (m_memory_override_enabled) {
return m_memory_usage_override;
}
if (!CanReportMemoryUsage() || allocator == nullptr) {
return 0;
}
Expand Down Expand Up @@ -237,6 +240,9 @@ uint32_t GraphicContext::GetHeapUsage(std::span<HeapUsage, VK_MAX_MEMORY_HEAPS>
}

uint64_t GraphicContext::GetTotalMemoryBudget() const {
if (m_memory_override_enabled) {
return m_memory_budget_override;
}
if (allocator == nullptr) {
return 0;
}
Expand Down Expand Up @@ -357,4 +363,16 @@ void GraphicContext::DeleteImage(VulkanImage& image) {
image.allocation = nullptr;
}

void GraphicContext::TrimRecycledImages() {
if (allocator == nullptr) {
return;
}
DestroyRecycledImages(allocator);
}

size_t GraphicContext::RecycledImageCount() const {
std::lock_guard lock(g_recycled_mutex);
return g_recycled.size();
}

}
8 changes: 8 additions & 0 deletions core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ if(NOT ANYPS5_KYTY_DLL AND (WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
set_tests_properties(kyty_shim_buffer_cache_pages PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77
ENVIRONMENT "KYTY_GPU_PIPELINE=0"
PASS_REGULAR_EXPRESSION "kyty_shim_buffer_cache_pages: ok" FAIL_REGULAR_EXPRESSION "FAILED")
target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/VramBudget.cpp)
add_test_executable(kyty_shim_vram_budget_tests ${CMAKE_CURRENT_SOURCE_DIR}/VramBudgetMain.cpp)
configure_host_test_executable(kyty_shim_vram_budget_tests)
target_link_libraries(kyty_shim_vram_budget_tests PRIVATE libSceAgcDriver libkernel libc)
add_test(NAME kyty_shim_vram_budget COMMAND kyty_shim_vram_budget_tests)
set_tests_properties(kyty_shim_vram_budget PROPERTIES TIMEOUT 25 SKIP_RETURN_CODE 77
ENVIRONMENT "KYTY_GPU_PIPELINE=0"
PASS_REGULAR_EXPRESSION "kyty_shim_vram_budget: ok" FAIL_REGULAR_EXPRESSION "FAILED")
if(NOT ANYPS5_ENABLE_ASAN)
target_sources(libSceAgcDriver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensions.cpp)
add_test_executable(kyty_shim_indirect_dispatch_dimensions_tests ${CMAKE_CURRENT_SOURCE_DIR}/IndirectDispatchDimensionsMain.cpp)
Expand Down
Loading