fix(agc): keep one storage image for a surface viewed past its last mip

The descriptors of one surface share its storage image through the
surface key, which includes the level count. A view past MAX_MIP
counts the extra levels, so its storage image was a second image of
the same memory, and the two flushed and re-uploaded each other every
frame (PPSA21564 writes level 6 of each 1920x1080 bloom chain after
building levels 1 to 5 through MAX_MIP 5 views).

A surface once viewed past its allocated levels now keeps a single
image holding the extended chain, and its other descriptors look that
image up; the allocated-level image it replaces is evicted with its
results stored. The extra levels lie in the allocation (the decode
proved that), so levels 0 to MAX_MIP keep their layout and the views
of them read the same texels.
This commit is contained in:
Dean Galvin
2026-10-04 19:50:47 +13:00
parent 3d3e687971
commit b6298bf738
4 changed files with 134 additions and 4 deletions
@@ -61,6 +61,7 @@ struct GuestTextureResource {
std::uint64_t dccAddress = 0;
bool dccAlphaOnMsb = false;
std::uint32_t minLod = 0;
std::uint32_t allocatedMipCount = 0;
};
float EffectiveMinLod(const GuestTextureResource& resource);
@@ -164,6 +164,7 @@ GuestTextureResource DecodeTextureResource(std::span<const std::uint32_t> words)
result.depthOrLastArray = depth;
result.baseArray = baseArray;
result.mipCount = maxMip + 1u;
result.allocatedMipCount = maxMip + 1u;
result.baseLevel = baseLevel;
result.lastLevel = lastLevel;
result.tileMode = tileMode;
@@ -511,22 +511,64 @@ std::array<std::uint32_t, 8> SurfaceKey(const Context& context, const GuestTextu
return {static_cast<std::uint32_t>(resource.baseAddress), static_cast<std::uint32_t>(resource.baseAddress >> 32u), resource.width, resource.height, (resource.depthOrLastArray << 16u) | (resource.mipCount & 0xffffu), (static_cast<std::uint32_t>(resource.tileMode) << 12u) | (static_cast<std::uint32_t>(resource.dimension) << 20u), resource.baseArray, static_cast<std::uint32_t>(StorageFormatForGuest(context, resource.format))};
}
struct ExtendedSurfaces {
std::mutex mutex;
std::map<std::array<std::uint32_t, 8>, std::uint32_t> levels;
std::atomic<bool> any{false};
};
ExtendedSurfaces& ExtendedChains() {
static ExtendedSurfaces surfaces;
return surfaces;
}
std::uint32_t AllocatedLevels(const GuestTextureResource& resource) {
return resource.allocatedMipCount != 0 ? resource.allocatedMipCount : resource.mipCount;
}
GuestTextureResource StorageSurface(const Context& context, const GuestTextureResource& viewed) {
auto& surfaces = ExtendedChains();
const bool extended = viewed.mipCount > AllocatedLevels(viewed);
if (!extended && !surfaces.any.load(std::memory_order_acquire)) return viewed;
auto surface = viewed;
surface.mipCount = AllocatedLevels(viewed);
const auto identity = SurfaceKey(context, surface);
std::lock_guard lock(surfaces.mutex);
if (extended) {
auto& levels = surfaces.levels[identity];
levels = std::max(levels, viewed.mipCount);
surfaces.any.store(true, std::memory_order_release);
surface.mipCount = levels;
} else if (const auto it = surfaces.levels.find(identity); it != surfaces.levels.end()) {
surface.mipCount = it->second;
} else {
surface.mipCount = viewed.mipCount;
}
return surface;
}
// `guestBytes` is the surface size when the caller described the surface already (0: described here).
std::shared_ptr<StorageTexture> cachedStorageTexture(const Context& context, std::span<const std::uint32_t> words, const GuestTextureResource& resource, std::uint32_t mip, std::uint64_t guestBytes) {
if (DepthSurfaceAt(resource.baseAddress)) {
std::shared_ptr<StorageTexture> cachedStorageTexture(const Context& context, std::span<const std::uint32_t> words, const GuestTextureResource& viewed, std::uint32_t mip, std::uint64_t guestBytes) {
if (DepthSurfaceAt(viewed.baseAddress)) {
char text[112];
std::snprintf(text, sizeof(text), "AGC graphics: storage image access to depth/stencil surface 0x%llx is not implemented", static_cast<unsigned long long>(resource.baseAddress));
std::snprintf(text, sizeof(text), "AGC graphics: storage image access to depth/stencil surface 0x%llx is not implemented", static_cast<unsigned long long>(viewed.baseAddress));
throw std::runtime_error(text);
}
static const bool disabled = std::getenv("APS5_NO_TEXTURE_CACHE") != nullptr;
if (disabled) return std::make_shared<StorageTexture>(context, *context.detiler, resource, mip);
if (disabled) return std::make_shared<StorageTexture>(context, *context.detiler, viewed, mip);
static_cast<void>(words);
const bool profile = LookupOutcomes::Profiled();
auto start = profile ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};
auto& counters = TextureCounts();
const auto resource = StorageSurface(context, viewed);
const StorageKey key{context.device, SurfaceKey(context, resource)};
auto& cache = StorageTextures();
std::lock_guard lock(cache.mutex);
if (resource.mipCount > AllocatedLevels(resource)) {
auto allocated = resource;
allocated.mipCount = AllocatedLevels(resource);
if (const auto found = findStorage(cache, {context.device, SurfaceKey(context, allocated)}); found != cache.entries.end()) evictStorage(cache, found);
}
auto it = findStorage(cache, key);
if (it != cache.entries.end() && MetadataMoved(*it->texture, resource)) {
evictStorage(cache, it);
@@ -768,6 +768,91 @@ void movedMetadataTests(const Device& device, Recorder& recorder) {
Require(holds(*back, {0x55, 0x55, 0x55, 0x55}), "the image remade under the first keys does not hold the stored texels");
}
void viewPastLastMipTests(const Device& device, Recorder& recorder) {
const auto& base = device.GetContext();
if (base.hostImportAlignment == 0) {
std::cout << "host imports unavailable: views past the last mip not tested\n";
return;
}
constexpr std::uint32_t side = 256;
constexpr std::size_t bytes = 0x60000;
#ifdef _WIN32
void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
void* block = std::aligned_alloc(65536, bytes);
#endif
Require(block != nullptr, "cannot allocate the past-last-mip block");
auto* texels = static_cast<std::uint8_t*>(block);
const auto address = reinterpret_cast<std::uint64_t>(block);
{
GuestAllocations::Mutation mutation;
mutation.Add(block, bytes, true, true);
}
struct Unregister {
const Context& context;
void* block;
std::uint64_t address;
~Unregister() {
ClearCachedTextures(context.device);
{
GuestAllocations::Mutation mutation;
mutation.Remove(block);
}
HostImportFor(context, address, bytes);
}
} unregister{base, block, address};
if (HostImportFor(base, address, bytes) == nullptr) {
std::cout << "host import of the past-last-mip block refused: views past the last mip not tested\n";
return;
}
TextureDetiler detiler(base);
auto context = base;
context.detiler = &detiler;
const auto view = [&](std::uint32_t level) {
const std::array<std::uint32_t, 8> words{
static_cast<std::uint32_t>(address >> 8u),
static_cast<std::uint32_t>((address >> 40u) & 0xffu) | (56u << 20u) | (((side - 1u) & 3u) << 30u),
((side - 1u) >> 2u) | ((side - 1u) << 14u),
0xfacu | (level << 12u) | (level << 16u) | (0x1bu << 20u) | (9u << 28u),
0u,
2u << 4u,
0u,
0u,
};
return DecodeTextureResource(words);
};
const auto texel = [&](const StorageTexture& image, std::uint32_t level) {
Buffer readback(context, 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
const auto commands = recorder.Commands();
RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT);
VkBufferImageCopy copy{};
copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, 0, 1};
copy.imageExtent = {1, 1, 1};
context.Function<PFN_vkCmdCopyImageToBuffer>("vkCmdCopyImageToBuffer")(commands, image.Image(), VK_IMAGE_LAYOUT_GENERAL, readback.Handle(), 1, &copy);
RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT);
recorder.Submit();
device.WaitQueue();
recorder.Sync();
std::uint32_t value = 0;
std::memcpy(&value, readback.Bytes().data(), sizeof(value));
return value;
};
std::memset(texels, 0x55, bytes);
std::memset(texels + 0x8400, 0x66, 4);
std::memset(texels + 0x4800, 0x77, 4);
const auto allocatedView = view(2);
const auto pastView = view(3);
Require(allocatedView.mipCount == 3 && pastView.mipCount == 4 && DescribeSurface(pastView).guestBytes == bytes, "a 256x256 32 bpp SW_64KB_R_X surface viewed past MAX_MIP 2 decoded wrongly");
const auto allocated = CachedStorageSurface(context, allocatedView);
Require(allocated->Descriptor().mipCount == 3, "the allocated levels' storage image has an unexpected level count");
const auto past = CachedStorageSurface(context, pastView);
Require(past->Descriptor().mipCount == 4, "the storage image of a view past the last mip lacks the level it names");
Require(!StorageImageCached(context, allocated.get()), "the allocated levels' image is still the surface's after a view past its last mip");
Require(CachedStorageSurface(context, allocatedView) == past, "a view of the allocated levels took another image than the view past the last mip");
Require(texel(*past, 2) == 0x66666666u, "level 2 was not read from its addrlib tail slot");
Require(texel(*past, 3) == 0x77777777u, "the level past the last mip was not read from its addrlib tail slot");
}
void resourceReadTests(const Device& device, Recorder& recorder) {
const auto& context = device.GetContext();
if (context.hostImportAlignment == 0) {
@@ -2455,6 +2540,7 @@ int main() {
RunResidentPresentTests(device.GetContext());
storeRunTests(device, recorder);
movedMetadataTests(device, recorder);
viewPastLastMipTests(device, recorder);
keysFillTests(device, recorder);
unitShadowTests(device, recorder);
storageRefreshTests(device, recorder, false);