From 74abc7acc35d8de04f4fc6f6d8caa7ee63064df0 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Thu, 1 Oct 2026 00:26:38 +1300 Subject: [PATCH] fix(agc): sample a 2D instruction over a 2D array texture through its first layer A shader sampling with MIMG DIM 2D over a T# of type 2D array was refused: "guest texture dimension disagrees with the shader's declared image shape (shape 1, dimension 2)". The access carries no slice coordinate, so the hardware samples slice 0 of the resource view, the BASE_ARRAY layer, as it does for the storage access of the previous commit. A 2D array Texture now also makes a 2D view of that layer (Texture::FirstLayerView), whether it holds its own snapshot or views the surface's storage image, and the sampled binding uses it for such an access. Every other shape/dimension mismatch still throws. --- .../Graphics/include/ShaderResources.hpp | 1 + .../libSceAgcDriver/Graphics/include/Texture.hpp | 3 +++ .../Graphics/src/ShaderResources.cpp | 6 ++++-- .../prx/libSceAgcDriver/Graphics/src/Texture.cpp | 11 +++++++++++ core/libs/prx/libSceAgcDriver/tests/Recorder.cpp | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp index a66fce072..d4d20de72 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp @@ -388,6 +388,7 @@ private: // notes (counted in MarkGpuWrites for the [buffers] line). std::size_t readOnlyBuffers = 0; std::vector> textures; + std::vector textureFirstLayer; std::vector> storageTextures; std::vector storageMips; std::vector storageFirstLayer; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp index 5b55a7d1e..b4b340fad 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp @@ -64,6 +64,7 @@ public: Texture& operator=(const Texture&) = delete; VkImageView View() const; + VkImageView FirstLayerView() const { return firstLayerView; } // The layout the image is kept in while sampled. VkImageLayout Layout() const { return layout; } VkDeviceSize AllocationBytes() const { return allocationBytes; } @@ -78,6 +79,7 @@ public: private: void release() noexcept; + void createFirstLayerView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo); // Held by value: cached textures outlive the Context of the draw that created them. Context context; @@ -86,6 +88,7 @@ private: VkImage image = VK_NULL_HANDLE; std::shared_ptr owned; VkImageView view = VK_NULL_HANDLE; + VkImageView firstLayerView = VK_NULL_HANDLE; VkImageLayout layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; VkDeviceSize allocationBytes = 0; std::shared_ptr source; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index 8176bb8d1..e93563427 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -986,7 +986,7 @@ void ShaderResources::buildComplete() { break; case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: write.pImageInfo = images.data() + images.size(); - for (const auto index : binding.imageAllocations) images.push_back({VK_NULL_HANDLE, textures[index]->View(), textures[index]->Layout()}); + for (const auto index : binding.imageAllocations) images.push_back({VK_NULL_HANDLE, textureFirstLayer[index] ? textures[index]->FirstLayerView() : textures[index]->View(), textures[index]->Layout()}); break; case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: write.pImageInfo = images.data() + images.size(); @@ -2407,7 +2407,8 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind const auto words = std::span(binding.guestDescriptor).subspan(static_cast(element) * elementWords, elementWords); const auto* record = nextRecord(); const auto resource = record != nullptr && record->decoded ? record->resource : DecodeTextureResource(words); - if (!MatchesGuestDimension(*binding.imageShape, resource.dimension)) throw std::runtime_error("AGC graphics: guest texture dimension disagrees with the shader's declared image shape (shape " + std::to_string(static_cast(*binding.imageShape)) + ", dimension " + std::to_string(static_cast(resource.dimension)) + ")"); + const bool firstLayer = binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image2D && resource.dimension == TextureDimension::k2DArray; + if (!firstLayer && !MatchesGuestDimension(*binding.imageShape, resource.dimension)) throw std::runtime_error("AGC graphics: guest texture dimension disagrees with the shader's declared image shape (shape " + std::to_string(static_cast(*binding.imageShape)) + ", dimension " + std::to_string(static_cast(resource.dimension)) + ")"); const VkComponentMapping components{ComponentSwizzleFor(resource.dstSelX), ComponentSwizzleFor(resource.dstSelY), ComponentSwizzleFor(resource.dstSelZ), ComponentSwizzleFor(resource.dstSelW)}; const auto guestBytes = record != nullptr && record->decoded ? record->guestBytes : DescribeSurface(resource).guestBytes; std::shared_ptr texture; @@ -2417,6 +2418,7 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind } if (texture == nullptr) texture = cachedTexture(context, words, resource, components, guestBytes); textures.push_back(std::move(texture)); + textureFirstLayer.push_back(firstLayer); describedRanges.push_back({"texture", resource.baseAddress, guestBytes, resource.width, resource.height, resource.format, static_cast(resource.tileMode), resource.dccAddress}); item.imageAllocations.push_back(textures.size() - 1); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp index 08ad86de0..6ad519dae 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp @@ -419,6 +419,7 @@ Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTex VkImageViewMinLodCreateInfoEXT minLod{VK_STRUCTURE_TYPE_IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT}; ChainMinLod(context, descriptor, viewInfo, minLod); Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &view), "vkCreateImageView"); + createFirstLayerView(descriptor, viewInfo); if (profile) { auto& totals = Profile(); totals.view += timer.lap(); @@ -462,6 +463,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& VkImageViewMinLodCreateInfoEXT minLod{VK_STRUCTURE_TYPE_IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT}; ChainMinLod(context, descriptor, viewInfo, minLod); Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &view), "vkCreateImageView storage view"); + createFirstLayerView(descriptor, viewInfo); if (profile) { auto& totals = Profile(); totals.view += timer.lap(); @@ -489,10 +491,19 @@ Texture::~Texture() { release(); } +void Texture::createFirstLayerView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo) { + if (descriptor.dimension != TextureDimension::k2DArray) return; + viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; + viewInfo.subresourceRange.layerCount = 1; + Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &firstLayerView), "vkCreateImageView first layer"); +} + void Texture::release() noexcept { upload.reset(); if (view) context.Function("vkDestroyImageView")(context.device, view, nullptr); view = VK_NULL_HANDLE; + if (firstLayerView) context.Function("vkDestroyImageView")(context.device, firstLayerView, nullptr); + firstLayerView = VK_NULL_HANDLE; // The image and its memory go with the last holder: this texture, or the batch still uploading it. owned.reset(); image = VK_NULL_HANDLE; diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index 722e87ff3..ff50ee64f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -1749,6 +1749,20 @@ void firstLayerViewTests(const Device& device, Recorder& recorder) { SampleProgram program(context, recorder); expectRed(program.Red(image->FirstLayerView(0), VK_IMAGE_LAYOUT_GENERAL, 0.0f), 0x40 / 255.0f, "a first-layer view does not read the BASE_ARRAY layer"); Require(image->FirstLayerView(0) == image->FirstLayerView(0), "first-layer views are not reused"); + const VkComponentMapping identity{VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}; + Texture snapshotTexture(context, detiler, resource, identity, std::span(reinterpret_cast(surface), static_cast(geometry.guestBytes))); + Require(snapshotTexture.FirstLayerView() != VK_NULL_HANDLE, "a sampled 2D array texture has no first-layer view"); + expectRed(program.Red(snapshotTexture.FirstLayerView(), snapshotTexture.Layout(), 0.0f), 0x40 / 255.0f, "a sampled first-layer view does not read the BASE_ARRAY layer"); + Texture storageView(context, image, resource, identity); + Require(storageView.FirstLayerView() != VK_NULL_HANDLE, "a sampled view of a 2D array storage image has no first-layer view"); + expectRed(program.Red(storageView.FirstLayerView(), storageView.Layout(), 0.0f), 0x40 / 255.0f, "a sampled first-layer view of a storage image does not read the BASE_ARRAY layer"); + auto flat = resource; + flat.dimension = TextureDimension::k2D; + flat.depthOrLastArray = 0; + flat.baseArray = 0; + Texture flatTexture(context, detiler, flat, identity, std::span(reinterpret_cast(surface), static_cast(DescribeSurface(flat).guestBytes))); + Require(flatTexture.FirstLayerView() == VK_NULL_HANDLE, "a 2D texture has a first-layer view"); + expectRed(program.Red(flatTexture.View(), flatTexture.Layout(), 0.0f), 0x20 / 255.0f, "a 2D texture over the surface does not read its first layer"); } int main() {