Skip to content
Open
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
8 changes: 7 additions & 1 deletion core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2059,14 +2059,17 @@ void BufferCache::FillBuffer(uint64_t vaddr, uint64_t size, uint32_t value, bool
if (vaddr == 0) {
EXIT("BufferCache: invalid fill memory address\n");
}
(void)m_texture_cache.ClearMeta(vaddr);
const bool meta = m_texture_cache.ClearMeta(vaddr);
if (!IsRegionGpuModified(vaddr, size) && !ServesInPlace(vaddr, size) && !DeferredLabelPending(vaddr, size)) {
InvalidateMemory(vaddr, size);
m_texture_cache.InvalidateMemory(vaddr, size);
DrainHostCopies();
auto* destination = reinterpret_cast<uint32_t*>(vaddr);
std::fill(destination, destination + size / sizeof(uint32_t), value);
LibKernel::Memory::NoteEmulatorWrite(vaddr, size);
if (!meta) {
m_texture_cache.NoteMetaFill(vaddr, size, value, true);
}
return;
}

Expand All @@ -2076,6 +2079,9 @@ void BufferCache::FillBuffer(uint64_t vaddr, uint64_t size, uint32_t value, bool
m_in_place_write = InPlaceMemory::WriteKind::Binding;
m_texture_cache.ProtectPendingImageWriteBacks(vaddr, size);
dst->Fill(dst_offset, size, value);
if (!meta) {
m_texture_cache.NoteMetaFill(vaddr, size, value, false);
}
}

bool BufferCache::ReplicateBuffer(uint64_t destination, uint64_t count, uint64_t source,
Expand Down
264 changes: 264 additions & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/textureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ TextureCache::TextureCache(GraphicContext& graphics, CommandScheduler& scheduler
}

TextureCache::~TextureCache() {
{
Common::ScopedLock pending {m_pending_meta_lock};
ErasePendingMetaClearsLocked(m_pending_meta_clears.begin(), m_pending_meta_clears.end());
}
m_slot_images.ForEach([&](ImageId id, const Image& image) KYTY_NO_THREAD_SAFETY_ANALYSIS {
if (image.registered) {
UnregisterImage(id);
Expand Down Expand Up @@ -2799,6 +2803,10 @@ vk::ImageView TextureCache::FindRenderTarget(ImageId id, const ImageDesc& desc)

vk::ImageView TextureCache::FindDepthTarget(ImageId id, const ImageDesc& desc, ImageId* stencil) {
CollectHotPagesOf(id);
if (desc.info.HasMetadata() && m_pending_meta_count.load(std::memory_order_acquire) != 0 &&
m_page_manager.HasHotPages()) {
m_scheduler.Context().CollectHotPageWrites(desc.info.metadata.range.address, desc.info.metadata.range.size);
}
Common::ScopedLock lock {m_lock};
auto& image = m_slot_images[id];
if (!image.registered || image.depth_id || image.binding.needs_rebind) {
Expand Down Expand Up @@ -2827,6 +2835,9 @@ vk::ImageView TextureCache::FindDepthTarget(ImageId id, const ImageDesc& desc, I
image.info.metadata = desc.info.metadata;
if (desc.info.HasMetadata()) {
const auto [metadata, inserted] = m_surface_metas.try_emplace(desc.info.metadata.range.address);
if (inserted) {
ApplyPendingMetaClears(image.info, metadata->second);
}
if (inserted && image.info.htile_initial_clear) {
metadata->second.clear_mask.set();
}
Expand Down Expand Up @@ -2894,6 +2905,7 @@ void TextureCache::CommitGpuWrite(Image& image) {
image.MarkGpuModified();
image.gpu_write_sequence =
Image::gpu_write_sequences.fetch_add(1u, std::memory_order_relaxed) + 1u;
DropPendingMetaClears(image.info.data.address, image.info.data.size);
}

bool TextureCache::ClearImageFromBuffer(CommandBuffer& command, uint64_t address, uint64_t size,
Expand Down Expand Up @@ -3461,6 +3473,7 @@ void TextureCache::ProtectPendingImageWriteBacks(uint64_t address, uint64_t size
}

void TextureCache::InvalidateMemoryFromGPU(uint64_t address, uint64_t size) {
DropPendingMetaClears(address, size);
Common::ScopedLock lock {m_lock};
ForEachImageInRegion(address, size, [&](ImageId, Image& image) {
if (image.info.data.address != address) {
Expand Down Expand Up @@ -3576,6 +3589,7 @@ bool TextureCache::CopyToImage(uint64_t address, uint64_t source, uint64_t size)
copy.imageSubresource.baseArrayLayer = static_cast<uint32_t>(offset / stride);
}
image.Upload(copies, texels.buffer, texels.offset, texels.size);
DropPendingMetaClears(address, size);
return true;
}

Expand All @@ -3594,6 +3608,7 @@ bool TextureCache::IsRegionGpuModified(uint64_t address, uint64_t size, bool inc
}

bool TextureCache::InvalidateMemory(uint64_t address, uint64_t size) {
DropPendingMetaClearsAround(address, size);
ImagePageTable::PageRange pages {};
if (ImagePageTable::TryGetPageRange(address, size, pages) &&
!m_registered_pages.Any(pages.first, pages.last_exclusive)) {
Expand Down Expand Up @@ -3700,6 +3715,236 @@ bool TextureCache::CoversMeta(uint64_t address, uint64_t size) {
return size >= info.metadata.range.size / std::max(info.resources.layers, 1u) && size <= info.metadata.range.size;
}

bool TextureCache::OverlapsSurfaceMetaLocked(uint64_t address, uint64_t size) const {
const uint64_t end = size > UINT64_MAX - address ? UINT64_MAX : address + size;
auto found = m_surface_metas.lower_bound(address);
if (found != m_surface_metas.end() && found->first < end) {
return true;
}
if (found == m_surface_metas.begin()) {
return false;
}
--found;
const auto* owner = m_slot_images.try_get(found->second.owner);
return owner == nullptr || owner->info.metadata.range.address != found->first ||
owner->info.metadata.range.End() > address;
}

void TextureCache::NoteMetaFill(uint64_t address, uint64_t size, uint32_t value, bool in_memory) {
if ((value & 0xfu) != 0 || size == 0 || (!in_memory && !GuestRange {address, size}.Valid())) {
return;
}
Common::ScopedLock lock {m_lock};
if (OverlapsSurfaceMetaLocked(address, size)) {
return;
}
Common::ScopedLock pending {m_pending_meta_lock};
DropPendingMetaClearsLocked(address, size);
m_pending_meta_clears[address] = {size, m_gc_tick, value, in_memory};
std::array<uint64_t, 64> granules {};
AddPendingMetaGranules(granules, address, size);
for (size_t word = 0; word < granules.size(); ++word) {
if (granules[word] != 0) {
m_pending_meta_granules[word].fetch_or(granules[word], std::memory_order_relaxed);
}
}
UpdatePendingMetaBoundsLocked();
if (!in_memory) {
HoldPendingMetaPagesLocked(address, size, true);
}
}

void TextureCache::HoldPendingMetaPagesLocked(uint64_t address, uint64_t size, bool hold) {
const auto first = Common::AlignDown(address, TRACKER_PAGE_SIZE);
const auto last = Common::AlignDown(address + size - 1, TRACKER_PAGE_SIZE);
const auto watch = [&](uint64_t begin, uint64_t end) {
if (begin >= end) {
return;
}
if (hold) {
m_page_manager.UpdatePageWatchers<true>(begin, end - begin);
} else {
m_page_manager.UpdatePageWatchers<false>(begin, end - begin);
}
};
const auto edge = [&](uint64_t page) KYTY_REQUIRES(m_pending_meta_lock) {
if (hold) {
if (++m_pending_meta_pages[page] == 1) {
watch(page, page + TRACKER_PAGE_SIZE);
}
} else if (const auto found = m_pending_meta_pages.find(page);
found != m_pending_meta_pages.end() && --found->second == 0) {
m_pending_meta_pages.erase(found);
watch(page, page + TRACKER_PAGE_SIZE);
}
};
edge(first);
if (last != first) {
watch(first + TRACKER_PAGE_SIZE, last);
edge(last);
}
}

void TextureCache::AddPendingMetaGranules(std::array<uint64_t, 64>& words, uint64_t address, uint64_t size) {
const auto first = address >> PendingMetaGranuleBits;
const auto last = (address + size - 1) >> PendingMetaGranuleBits;
for (auto granule = first; granule <= last && granule - first < 4096u; ++granule) {
const auto bit = granule & 4095u;
words[bit >> 6u] |= uint64_t {1} << (bit & 63u);
}
}

void TextureCache::UpdatePendingMetaBoundsLocked() {
if (m_pending_meta_clears.empty()) {
m_pending_meta_begin.store(UINT64_MAX, std::memory_order_relaxed);
m_pending_meta_end.store(0, std::memory_order_relaxed);
for (auto& word: m_pending_meta_granules) {
word.store(0, std::memory_order_relaxed);
}
} else {
const auto& [last_address, last] = *m_pending_meta_clears.rbegin();
m_pending_meta_begin.store(m_pending_meta_clears.begin()->first, std::memory_order_relaxed);
m_pending_meta_end.store(last_address + last.size, std::memory_order_relaxed);
}
m_pending_meta_count.store(m_pending_meta_clears.size(), std::memory_order_release);
}

void TextureCache::DropPendingMetaClears(uint64_t address, uint64_t size) {
if (size == 0 || m_pending_meta_count.load(std::memory_order_acquire) == 0) {
return;
}
const uint64_t end = size > UINT64_MAX - address ? UINT64_MAX : address + size;
if (end <= m_pending_meta_begin.load(std::memory_order_relaxed) ||
address >= m_pending_meta_end.load(std::memory_order_relaxed) || !MaybePendingMeta(address, end)) {
return;
}
Common::ScopedLock lock {m_pending_meta_lock};
DropPendingMetaClearsLocked(address, size);
}

bool TextureCache::MaybePendingMeta(uint64_t address, uint64_t end) const {
const auto first = address >> PendingMetaGranuleBits;
const auto last = (end - 1) >> PendingMetaGranuleBits;
if (last - first >= 64u) {
return true;
}
for (auto granule = first; granule <= last; ++granule) {
const auto bit = granule & 4095u;
if (((m_pending_meta_granules[bit >> 6u].load(std::memory_order_relaxed) >> (bit & 63u)) & 1u) != 0) {
return true;
}
}
return false;
}

void TextureCache::DropPendingMetaClearsLocked(uint64_t address, uint64_t size) {
if (size == 0) {
return;
}
auto first = m_pending_meta_clears.lower_bound(address);
if (first != m_pending_meta_clears.begin()) {
const auto previous = std::prev(first);
if (previous->first + previous->second.size > address) {
first = previous;
}
}
const auto last =
size > UINT64_MAX - address ? m_pending_meta_clears.end() : m_pending_meta_clears.lower_bound(address + size);
if (first == last) {
return;
}
ErasePendingMetaClearsLocked(first, last);
UpdatePendingMetaBoundsLocked();
}

void TextureCache::ErasePendingMetaClearsLocked(PendingMetaClears::iterator first, PendingMetaClears::iterator last) {
for (auto it = first; it != last; ++it) {
if (!it->second.in_memory) {
HoldPendingMetaPagesLocked(it->first, it->second.size, false);
}
}
m_pending_meta_clears.erase(first, last);
}

void TextureCache::DropPendingMetaClearsAround(uint64_t address, uint64_t size) {
if (size == 0 || m_pending_meta_count.load(std::memory_order_acquire) == 0) {
return;
}
const uint64_t end = size > UINT64_MAX - address ? UINT64_MAX : address + size;
const uint64_t page_begin = Common::AlignDown(address, TRACKER_PAGE_SIZE);
const uint64_t page_end = end > UINT64_MAX - TRACKER_PAGE_SIZE ? UINT64_MAX : Common::AlignUp(end, TRACKER_PAGE_SIZE);
if (page_end <= m_pending_meta_begin.load(std::memory_order_relaxed) ||
page_begin >= m_pending_meta_end.load(std::memory_order_relaxed) || !MaybePendingMeta(page_begin, page_end)) {
return;
}
Common::ScopedLock pending {m_pending_meta_lock};
auto it = m_pending_meta_clears.lower_bound(page_begin);
if (it != m_pending_meta_clears.begin()) {
const auto previous = std::prev(it);
if (previous->first + previous->second.size > page_begin) {
it = previous;
}
}
bool erased = false;
while (it != m_pending_meta_clears.end() && it->first < page_end) {
const auto next = std::next(it);
if (!it->second.in_memory || (it->first < end && address < it->first + it->second.size)) {
ErasePendingMetaClearsLocked(it, next);
erased = true;
}
it = next;
}
if (erased) {
UpdatePendingMetaBoundsLocked();
}
}

static bool HoldsFill(uint64_t address, uint64_t size, uint32_t value) {
std::array<uint32_t, 256> words {};
for (uint64_t offset = 0; offset + sizeof(uint32_t) <= size;) {
const auto count = std::min<uint64_t>(words.size(), (size - offset) / sizeof(uint32_t));
if (!LibKernel::Memory::TryReadBacking(address + offset, words.data(), count * sizeof(uint32_t)) ||
!std::all_of(words.begin(), words.begin() + count, [value](uint32_t word) { return word == value; })) {
return false;
}
offset += count * sizeof(uint32_t);
}
return true;
}

void TextureCache::ApplyPendingMetaClears(const ImageInfo& info, MetaDataInfo& meta) {
if (m_pending_meta_count.load(std::memory_order_acquire) == 0) {
return;
}
const auto range = info.metadata.range;
const uint64_t layers = std::max(info.resources.layers, 1u);
const uint64_t slice = range.size / layers;
Common::ScopedLock lock {m_pending_meta_lock};
auto first = m_pending_meta_clears.lower_bound(range.address);
if (first != m_pending_meta_clears.begin()) {
const auto previous = std::prev(first);
if (previous->first + previous->second.size > range.address) {
first = previous;
}
}
const auto last = m_pending_meta_clears.lower_bound(range.End());
for (auto it = first; slice != 0 && it != last; ++it) {
const auto [address, pending] = *it;
if (m_gc_tick - pending.tick > 1 || (info.metadata.stencil_compressed && ((pending.value >> 8u) & 0x3u) != 0)) {
continue;
}
for (uint64_t layer = 0; layer < layers && layer < meta.clear_mask.size(); ++layer) {
const auto begin = range.address + layer * slice;
if (begin >= address && begin + slice <= address + pending.size &&
(!pending.in_memory || HoldsFill(begin, slice, pending.value))) {
meta.clear_mask.set(layer);
}
}
}
ErasePendingMetaClearsLocked(first, last);
UpdatePendingMetaBoundsLocked();
}

bool TextureCache::TouchMeta(uint64_t address, uint32_t slice, bool is_clear) {
Common::ScopedLock lock {m_lock};
const auto found = m_surface_metas.find(address);
Expand Down Expand Up @@ -3737,6 +3982,7 @@ void TextureCache::UnmapMemory(uint64_t address, uint64_t size) {
DiscardParkedImages(address, size);
m_surface_metas.erase(m_surface_metas.lower_bound(address),
m_surface_metas.lower_bound(address + size));
DropPendingMetaClears(address, size);
m_meta_epoch.fetch_add(1, std::memory_order_relaxed);
Common::SmallVector<ImageId, 16> deleted_images;
ForEachImageInRegion(address, size, [&](ImageId id, Image&) { deleted_images.push_back(id); });
Expand Down Expand Up @@ -3775,6 +4021,24 @@ void TextureCache::MapMemory(uint64_t address, uint64_t size) {
void TextureCache::RunGarbageCollector() {
Common::ScopedLock lock {m_lock};
const uint64_t tick = m_gc_tick++;
{
Common::ScopedLock pending {m_pending_meta_lock};
for (auto it = m_pending_meta_clears.begin(); it != m_pending_meta_clears.end();) {
const auto next = std::next(it);
if (m_gc_tick - it->second.tick > 1) {
ErasePendingMetaClearsLocked(it, next);
}
it = next;
}
std::array<uint64_t, 64> granules {};
for (const auto& [address, record]: m_pending_meta_clears) {
AddPendingMetaGranules(granules, address, record.size);
}
for (size_t word = 0; word < granules.size(); ++word) {
m_pending_meta_granules[word].store(granules[word], std::memory_order_relaxed);
}
UpdatePendingMetaBoundsLocked();
}
DiscardParkedImages(0, 0);
if (m_graphics.CanReportMemoryUsage()) {
m_total_used_memory = m_graphics.GetDeviceMemoryUsage();
Expand Down
Loading
Loading