diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp index b09d3168c..84583699b 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp @@ -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); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp index 443517bba..a9954b1da 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp @@ -164,6 +164,7 @@ GuestTextureResource DecodeTextureResource(std::span words) result.depthOrLastArray = depth; result.baseArray = baseArray; result.mipCount = maxMip + 1u; + result.allocatedMipCount = maxMip + 1u; result.baseLevel = baseLevel; result.lastLevel = lastLevel; result.tileMode = tileMode; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index 5169e3ef8..60ab8c3ce 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -511,22 +511,64 @@ std::array SurfaceKey(const Context& context, const GuestTextu return {static_cast(resource.baseAddress), static_cast(resource.baseAddress >> 32u), resource.width, resource.height, (resource.depthOrLastArray << 16u) | (resource.mipCount & 0xffffu), (static_cast(resource.tileMode) << 12u) | (static_cast(resource.dimension) << 20u), resource.baseArray, static_cast(StorageFormatForGuest(context, resource.format))}; } +struct ExtendedSurfaces { + std::mutex mutex; + std::map, std::uint32_t> levels; + std::atomic 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 cachedStorageTexture(const Context& context, std::span words, const GuestTextureResource& resource, std::uint32_t mip, std::uint64_t guestBytes) { - if (DepthSurfaceAt(resource.baseAddress)) { +std::shared_ptr cachedStorageTexture(const Context& context, std::span 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(resource.baseAddress)); + std::snprintf(text, sizeof(text), "AGC graphics: storage image access to depth/stencil surface 0x%llx is not implemented", static_cast(viewed.baseAddress)); throw std::runtime_error(text); } static const bool disabled = std::getenv("APS5_NO_TEXTURE_CACHE") != nullptr; - if (disabled) return std::make_shared(context, *context.detiler, resource, mip); + if (disabled) return std::make_shared(context, *context.detiler, viewed, mip); static_cast(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); diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index 732e684a1..07a370f0e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -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(block); + const auto address = reinterpret_cast(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 words{ + static_cast(address >> 8u), + static_cast((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("vkCmdCopyImageToBuffer")(commands, image.Image(), VK_IMAGE_LAYOUT_GENERAL, readback.Handle(), 1, ©); + 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);