fix(agc): end a key fill's hold when the fill clears the image at once

An image's filledKeys stands for "the pending results predate the title's
key fill": sampled reads through those keys bind the clear value, and
(with APS5_KEYS_FILL_WRITEBACK_KEEP=1) a write-back leaves the keys at the
fill's code. A refresh that clears the image under the new keys ends that
hold (upload resets it), but the immediate clear of APS5_KEYS_FILL_CLEAR=1
did not. Results drawn after such a fill were then treated as dead: reads
through the keys saw the clear and a write-back kept the keys clear over
them. The immediate clear now ends the hold as the refresh does.
This commit is contained in:
Dean Galvin
2026-10-01 15:25:35 +13:00
parent 1ffaa66173
commit ea0f13027e
2 changed files with 113 additions and 0 deletions
@@ -2510,6 +2510,7 @@ bool StorageTexture::clearByKeysFill(DccKeys keys, std::uint8_t key) {
recorder->EndGpuTiming(timing, guestBytes);
originalValid = false;
uploadedKeys = keys;
filledKeys = DccKeys::Uncompressed;
keyProof = {};
forgetBorrowed(0, trackedLayers);
layerGeneration.assign(trackedLayers, GuestMemory::CollectWrites(descriptor.baseAddress, static_cast<std::size_t>(guestBytes)));
@@ -1963,6 +1963,117 @@ void firstLayerViewTests(const Device& device, Recorder& recorder) {
expectRed(program.Red(flatTexture.View(), flatTexture.Layout(), 0.0f), 0x20 / 255.0f, "a 2D texture over the surface does not read its first layer");
}
void keysFillTests(const Device& device, Recorder& recorder) {
const auto& base = device.GetContext();
if (base.hostImportAlignment == 0) {
std::cout << "host imports unavailable: DCC key fills not tested\n";
return;
}
constexpr std::uint32_t side = 256;
constexpr std::size_t surfaceBytes = side * side * 4;
constexpr std::size_t keyCount = surfaceBytes / 256;
constexpr std::size_t bytes = surfaceBytes + 65536;
#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 key fill block");
auto* texels = static_cast<std::uint8_t*>(block);
auto* keys = texels + surfaceBytes;
std::memset(texels, 0x55, surfaceBytes);
std::memset(keys, 0x00, keyCount);
const auto address = reinterpret_cast<std::uint64_t>(block);
const auto keysAddress = address + surfaceBytes;
{
GuestAllocations::Mutation mutation;
mutation.Add(block, bytes, 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) {
std::cout << "host import of the key fill block refused: DCC key fills not tested\n";
return;
}
TextureDetiler detiler(base);
auto context = base;
context.detiler = &detiler;
GuestTextureResource resource{};
resource.baseAddress = address;
resource.width = side;
resource.height = side;
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;
resource.dccAddress = keysAddress;
Require(DescribeSurface(resource).guestBytes == surfaceBytes, "the key fill surface has an unexpected size");
{
auto image = std::make_shared<StorageTexture>(context, detiler, resource, 0);
const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
const auto draw = [&](VkClearColorValue value) {
const auto commands = recorder.Commands();
recorder.Keep(image);
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_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
context.Function<PFN_vkCmdClearColorImage>("vkCmdClearColorImage")(commands, image->Image(), VK_IMAGE_LAYOUT_GENERAL, &value, 1, &range);
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 | VK_ACCESS_TRANSFER_WRITE_BIT);
image->MarkDirty();
};
const auto holds = [&](std::array<std::uint8_t, 4> texel) {
Buffer readback(context, surfaceBytes, 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_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT);
VkBufferImageCopy copy{};
copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
copy.imageExtent = {side, side, 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();
const auto pixels = readback.Bytes();
for (std::size_t i = 0; i < pixels.size(); ++i) {
if (std::to_integer<std::uint8_t>(pixels[i]) != texel[i % 4]) return false;
}
return true;
};
Require(holds({0, 0, 0, 0}), "a surface under 0000 keys was not cleared");
draw({{1.0f, 0.0f, 0.0f, 1.0f}});
Require(StorageTexture::NoteKeysFill(keysAddress, keyCount, 0x00) == 1, "a 0000 key fill did not cover the surface");
Require(image->FilledKeys() == DccKeys::Clear0000, "a key fill over pending results was not recorded");
image->Refresh();
Require(holds({0, 0, 0, 0}), "a key fill did not clear the results made before it at the next refresh");
Require(image->FilledKeys() == DccKeys::Uncompressed, "the image cleared by a refresh still holds the fill");
draw({{0.0f, 0.0f, 1.0f, 1.0f}});
Require(StorageTexture::NoteKeysFill(keysAddress, keyCount, 0x00) == 1, "a second 0000 key fill did not cover the surface");
#ifdef _WIN32
_putenv_s("APS5_KEYS_FILL_CLEAR", "1");
#else
setenv("APS5_KEYS_FILL_CLEAR", "1", 1);
#endif
Require(StorageTexture::ClearByKeysFill(keysAddress, keyCount, 0x00) == 1, "a 0000 key fill did not clear the surface at once");
Require(holds({0, 0, 0, 0}), "a key fill cleared at once left results made before it");
draw({{0.0f, 1.0f, 0.0f, 1.0f}});
Require(image->FilledKeys() == DccKeys::Uncompressed, "results drawn after a key fill cleared at once are held under the fill's code");
Require(holds({0, 255, 0, 255}), "results drawn after a key fill cleared at once were lost");
}
recorder.Sync();
}
int main() {
try {
Device device;
@@ -1982,6 +2093,7 @@ int main() {
misalignedSnapshotTests(device, recorder);
storeRunTests(device, recorder);
movedMetadataTests(device, recorder);
keysFillTests(device, recorder);
unitShadowTests(device, recorder);
storageRefreshTests(device, recorder, false);
storageRefreshTests(device, recorder, true);