AGC: a small tiled storage image on a 64 KiB base is not stored before a label after its write-back went into a unit shadow #11481

Closed
opened 2026-10-26 16:39:50 +00:00 by panicDa3m0n · 0 comments
panicDa3m0n commented 2026-10-26 16:39:50 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by panicDa3m0n on 2026-10-26T16:39:50Z.


What happens

A small tiled storage image whose base is 64 KiB-aligned keeps its results only in the import's unit shadow after a write-back for a device reader (PublishScope::None). The store before the next label no longer sees it, so a direct CPU load after the label reads the import's old bytes: small tiled shadow label: 0 guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096.

Title, test or build step

A test case added to agc_driver_recorder_tests (--small-tiled-shadow-label-only, code below). It is a synthetic test on the code path: we have not seen it as a wrong image in a title.

AnyPS5 commit

main 035ac96edd, nothing on top.

System

Windows 11 (10.0.26200), NVIDIA GeForce RTX 4070 Ti SUPER (616.64), WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release.

Steps to reproduce

  1. Add the function below to core/libs/prx/libSceAgcDriver/tests/Recorder.cpp, and its option to main().
  2. Build agc_driver_recorder_tests and run APS5_PIN_WAIT_MS=2000 build/tests/agc_driver_recorder_tests.exe --small-tiled-shadow-label-only. It exits with 1.

What the test does:

  • a 1 MiB block from AllocateWatched, registered with GuestAllocations::Mutation::Add, host-imported (HostImportFor) and watched (Watched);
  • a 32x32 storage image of format 56 in kR64KBX at offset 0x40000, a 64 KiB-aligned base, filled on the GPU with vkCmdCopyBufferToImage and MarkDirty;
  • StorageTexture::FlushPending(…, "storage image creation", PublishScope::None), then a print of AnyShadowedOverlaps over the image;
  • StorageTexture::StoreReadbackBeforeLabel("readback before label");
  • a direct CPU read of the image's guest bytes.

The same steps pass on main with a 64x64 kLinear image at offset 0, or a 32x32 kStandard4KB image at 0x21000: their write-back does not go into the shadow.

The test
bool smallTiledShadowLabelTests(const Device& device, Recorder& recorder) {
    using namespace AgcDriver::GuestMemory;
    const auto& base = device.GetContext();
    if (!UnitShadowEnabled() || base.hostImportAlignment == 0) {
        std::cout << "small tiled shadow label: unit shadows or host imports are unavailable\n";
        return false;
    }
    constexpr std::size_t bytes = 1u << 20u;
    constexpr std::uint64_t offset = 0x40000;
    void* block = AllocateWatched(bytes, 65536);
    if (block == nullptr) {
        std::cout << "small tiled shadow label: no write watching\n";
        return false;
    }
    struct Release {
        void* block;
        ~Release() { ReleaseWatched(block, bytes); }
    } release{block};
    auto* memory = static_cast<std::uint8_t*>(block);
    const auto address = reinterpret_cast<std::uint64_t>(block);
    std::memset(memory, 0x55, bytes);
    {
        GuestAllocations::Mutation mutation;
        mutation.Add(block, bytes, true, true, true);
    }
    struct Unregister {
        const Context& context;
        void* block;
        std::uint64_t address;
        ~Unregister() {
            {
                GuestAllocations::Mutation mutation;
                mutation.Remove(block);
            }
            HostImportFor(context, address, bytes);
        }
    } unregister{base, block, address};
    if (HostImportFor(base, address, bytes) == nullptr || !Watched(address, bytes)) {
        std::cout << "small tiled shadow label: the block is not imported and watched\n";
        return false;
    }
    CollectWritesUncached(address, bytes);
    TextureDetiler detiler(base);
    auto context = base;
    context.detiler = &detiler;
    const auto settle = [&] {
        recorder.Submit();
        device.WaitQueue();
        recorder.Sync();
    };
    GuestTextureResource resource{};
    resource.baseAddress = address + offset;
    resource.width = 32;
    resource.height = 32;
    resource.mipCount = 1;
    resource.tileMode = TextureTileMode::kR64KBX;
    resource.dimension = TextureDimension::k2D;
    resource.format = 56;
    resource.dstSelX = 4;
    resource.dstSelY = 5;
    resource.dstSelZ = 6;
    resource.dstSelW = 7;
    const auto layout = DescribeSurface(resource);
    Require(layout.guestBytes <= StorageTexture::SmallStoreBytes && offset + layout.guestBytes <= bytes, "small tiled shadow label: the surface is not a small image inside the block");
    auto image = StorageTexture::Create(context, detiler, resource, 0);
    image->Refresh();
    settle();
    auto pattern = std::make_shared<Buffer>(context, static_cast<std::size_t>(32) * 32 * 4, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
    std::fill(pattern->Bytes().begin(), pattern->Bytes().end(), std::byte{0x3c});
    {
        const auto commands = recorder.Commands();
        recorder.Keep(image);
        recorder.Keep(pattern);
        RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
        VkBufferImageCopy copy{};
        copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
        copy.imageExtent = {32, 32, 1};
        context.Function<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage")(commands, pattern->Handle(), image->Image(), VK_IMAGE_LAYOUT_GENERAL, 1, &copy);
        RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT);
        image->MarkDirty();
    }
    Require(StorageTexture::FlushPending(resource.baseAddress, static_cast<std::size_t>(layout.guestBytes), nullptr, "storage image creation", PublishScope::None), "small tiled shadow label: the flush for storage image creation stored nothing");
    settle();
    std::cout << "small tiled shadow label: in a unit shadow after the flush: " << (AnyShadowedOverlaps(resource.baseAddress, static_cast<std::size_t>(layout.guestBytes)) ? "yes" : "no") << "\n";
    StorageTexture::StoreReadbackBeforeLabel("readback before label");
    settle();
    const auto* cpu = memory + offset;
    const auto held = static_cast<std::size_t>(std::count(cpu, cpu + layout.guestBytes, std::uint8_t{0x3c}));
    Require(held == static_cast<std::size_t>(32) * 32 * 4, "small tiled shadow label: " + std::to_string(held) + " guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096");
    std::cout << "small tiled shadow label: ok\n";
    return true;
}

In main(), next to the other --…-only options:

        if (argc == 2 && std::string_view(argv[1]) == "--small-tiled-shadow-label-only") {
            return smallTiledShadowLabelTests(device, recorder) ? 0 : 77;
        }

Log

The test's lines. The environment lines before them ([time], the host CPU line, [write-watch] and a second Vulkan device) are left out.

Vulkan device: NVIDIA GeForce RTX 4070 Ti SUPER
host imports are compared
small tiled shadow label: in a unit shadow after the flush: yes
AGC graphics: small tiled shadow label: 0 guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096

What was checked

Where it goes wrong, reading main 035ac96edd:

  • blockUnits (Graphics/src/Texture.cpp:1442) holds for a tiled image at a 64 KiB-aligned base. Its write-back goes through writeBackWindows into the import's unit shadow.
  • A write-back for a device reader with PublishScope::None takes the image off the pending list and leaves its bytes in the shadow. These write-backs are "storage image creation" in ShaderResources.cpp:1242 and DepthSurface.cpp:1429, and "storage refresh" in Texture.cpp:2193.
  • At the next label nothing publishes the image's units:
    • StoreReadbackBeforeLabel (Texture.cpp:4540) walks only the pending images;
    • WriteLabelOnGpu publishes the shadow only over the label's own bytes.
  • A plain CPU load of those bytes does not reach the driver: it does not protect pages held in a unit shadow.

This is the class 14fd9853db ("also store small tiled images before a label") meant to keep stored before a label. Linear images and small tiled images off a 64 KiB base are correct on main, because their write-back does not go into the shadow.

The natural fix looks like the criterion the store before a label already uses (at most SmallStoreBytes), applied to the block write-back in writeBackWindows too, so that these images stay out of the shadow. Having the label publish the shadowed units of this class would be another way. If the first direction is the right one, I can take it.

@alfalfa-star, since 14fd9853db is yours: does this match what that change meant to cover?

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6044035915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/11481). Originally posted by [panicDa3m0n](https://github.com/panicDa3m0n) on 2026-10-26T16:39:50Z. --- ### What happens A small tiled storage image whose base is 64 KiB-aligned keeps its results only in the import's unit shadow after a write-back for a device reader (`PublishScope::None`). The store before the next label no longer sees it, so a direct CPU load after the label reads the import's old bytes: `small tiled shadow label: 0 guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096`. ### Title, test or build step A test case added to `agc_driver_recorder_tests` (`--small-tiled-shadow-label-only`, code below). It is a synthetic test on the code path: we have not seen it as a wrong image in a title. ### AnyPS5 commit `main` 035ac96edd, nothing on top. ### System Windows 11 (10.0.26200), NVIDIA GeForce RTX 4070 Ti SUPER (616.64), WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release. ### Steps to reproduce 1. Add the function below to `core/libs/prx/libSceAgcDriver/tests/Recorder.cpp`, and its option to `main()`. 2. Build `agc_driver_recorder_tests` and run `APS5_PIN_WAIT_MS=2000 build/tests/agc_driver_recorder_tests.exe --small-tiled-shadow-label-only`. It exits with 1. What the test does: - a 1 MiB block from `AllocateWatched`, registered with `GuestAllocations::Mutation::Add`, host-imported (`HostImportFor`) and watched (`Watched`); - a 32x32 storage image of format 56 in `kR64KBX` at offset 0x40000, a 64 KiB-aligned base, filled on the GPU with `vkCmdCopyBufferToImage` and `MarkDirty`; - `StorageTexture::FlushPending(…, "storage image creation", PublishScope::None)`, then a print of `AnyShadowedOverlaps` over the image; - `StorageTexture::StoreReadbackBeforeLabel("readback before label")`; - a direct CPU read of the image's guest bytes. The same steps pass on `main` with a 64x64 `kLinear` image at offset 0, or a 32x32 `kStandard4KB` image at 0x21000: their write-back does not go into the shadow. <details><summary>The test</summary> ```cpp bool smallTiledShadowLabelTests(const Device& device, Recorder& recorder) { using namespace AgcDriver::GuestMemory; const auto& base = device.GetContext(); if (!UnitShadowEnabled() || base.hostImportAlignment == 0) { std::cout << "small tiled shadow label: unit shadows or host imports are unavailable\n"; return false; } constexpr std::size_t bytes = 1u << 20u; constexpr std::uint64_t offset = 0x40000; void* block = AllocateWatched(bytes, 65536); if (block == nullptr) { std::cout << "small tiled shadow label: no write watching\n"; return false; } struct Release { void* block; ~Release() { ReleaseWatched(block, bytes); } } release{block}; auto* memory = static_cast<std::uint8_t*>(block); const auto address = reinterpret_cast<std::uint64_t>(block); std::memset(memory, 0x55, bytes); { GuestAllocations::Mutation mutation; mutation.Add(block, bytes, true, true, true); } struct Unregister { const Context& context; void* block; std::uint64_t address; ~Unregister() { { GuestAllocations::Mutation mutation; mutation.Remove(block); } HostImportFor(context, address, bytes); } } unregister{base, block, address}; if (HostImportFor(base, address, bytes) == nullptr || !Watched(address, bytes)) { std::cout << "small tiled shadow label: the block is not imported and watched\n"; return false; } CollectWritesUncached(address, bytes); TextureDetiler detiler(base); auto context = base; context.detiler = &detiler; const auto settle = [&] { recorder.Submit(); device.WaitQueue(); recorder.Sync(); }; GuestTextureResource resource{}; resource.baseAddress = address + offset; resource.width = 32; resource.height = 32; resource.mipCount = 1; resource.tileMode = TextureTileMode::kR64KBX; resource.dimension = TextureDimension::k2D; resource.format = 56; resource.dstSelX = 4; resource.dstSelY = 5; resource.dstSelZ = 6; resource.dstSelW = 7; const auto layout = DescribeSurface(resource); Require(layout.guestBytes <= StorageTexture::SmallStoreBytes && offset + layout.guestBytes <= bytes, "small tiled shadow label: the surface is not a small image inside the block"); auto image = StorageTexture::Create(context, detiler, resource, 0); image->Refresh(); settle(); auto pattern = std::make_shared<Buffer>(context, static_cast<std::size_t>(32) * 32 * 4, VK_BUFFER_USAGE_TRANSFER_SRC_BIT); std::fill(pattern->Bytes().begin(), pattern->Bytes().end(), std::byte{0x3c}); { const auto commands = recorder.Commands(); recorder.Keep(image); recorder.Keep(pattern); RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); VkBufferImageCopy copy{}; copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; copy.imageExtent = {32, 32, 1}; context.Function<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage")(commands, pattern->Handle(), image->Image(), VK_IMAGE_LAYOUT_GENERAL, 1, &copy); RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT); image->MarkDirty(); } Require(StorageTexture::FlushPending(resource.baseAddress, static_cast<std::size_t>(layout.guestBytes), nullptr, "storage image creation", PublishScope::None), "small tiled shadow label: the flush for storage image creation stored nothing"); settle(); std::cout << "small tiled shadow label: in a unit shadow after the flush: " << (AnyShadowedOverlaps(resource.baseAddress, static_cast<std::size_t>(layout.guestBytes)) ? "yes" : "no") << "\n"; StorageTexture::StoreReadbackBeforeLabel("readback before label"); settle(); const auto* cpu = memory + offset; const auto held = static_cast<std::size_t>(std::count(cpu, cpu + layout.guestBytes, std::uint8_t{0x3c})); Require(held == static_cast<std::size_t>(32) * 32 * 4, "small tiled shadow label: " + std::to_string(held) + " guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096"); std::cout << "small tiled shadow label: ok\n"; return true; } ``` In `main()`, next to the other `--…-only` options: ```cpp if (argc == 2 && std::string_view(argv[1]) == "--small-tiled-shadow-label-only") { return smallTiledShadowLabelTests(device, recorder) ? 0 : 77; } ``` </details> ### Log The test's lines. The environment lines before them (`[time]`, the host CPU line, `[write-watch]` and a second `Vulkan device`) are left out. ```text Vulkan device: NVIDIA GeForce RTX 4070 Ti SUPER host imports are compared small tiled shadow label: in a unit shadow after the flush: yes AGC graphics: small tiled shadow label: 0 guest bytes read by the CPU after a flush for storage image creation and the store before a label hold the image's value, expected 4096 ``` ### What was checked Where it goes wrong, reading `main` 035ac96edd: - `blockUnits` (`Graphics/src/Texture.cpp:1442`) holds for a tiled image at a 64 KiB-aligned base. Its write-back goes through `writeBackWindows` into the import's unit shadow. - A write-back for a device reader with `PublishScope::None` takes the image off the pending list and leaves its bytes in the shadow. These write-backs are `"storage image creation"` in `ShaderResources.cpp:1242` and `DepthSurface.cpp:1429`, and `"storage refresh"` in `Texture.cpp:2193`. - At the next label nothing publishes the image's units: - `StoreReadbackBeforeLabel` (`Texture.cpp:4540`) walks only the pending images; - `WriteLabelOnGpu` publishes the shadow only over the label's own bytes. - A plain CPU load of those bytes does not reach the driver: it does not protect pages held in a unit shadow. This is the class 14fd9853db ("also store small tiled images before a label") meant to keep stored before a label. Linear images and small tiled images off a 64 KiB base are correct on `main`, because their write-back does not go into the shadow. The natural fix looks like the criterion the store before a label already uses (at most `SmallStoreBytes`), applied to the block write-back in `writeBackWindows` too, so that these images stay out of the shadow. Having the label publish the shadowed units of this class would be another way. If the first direction is the right one, I can take it. @alfalfa-star, since 14fd9853db is yours: does this match what that change meant to cover? AI-assisted: yes
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#11481
No description provided.