Merge pull request #181 from oneandonlydean/fix/agc-sample-2d-over-array

fix(agc): sample a 2D instruction over a 2D array texture through its first layer
This commit is contained in:
boykopovar
2026-10-01 11:28:42 +03:00
committed by GitHub
5 changed files with 33 additions and 2 deletions
@@ -388,6 +388,7 @@ private:
// notes (counted in MarkGpuWrites for the [buffers] line).
std::size_t readOnlyBuffers = 0;
std::vector<std::shared_ptr<Texture>> textures;
std::vector<bool> textureFirstLayer;
std::vector<std::shared_ptr<StorageTexture>> storageTextures;
std::vector<std::uint32_t> storageMips;
std::vector<bool> storageFirstLayer;
@@ -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<OwnedImage> 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<ResidentColor> source;
@@ -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<const std::uint32_t>(binding.guestDescriptor).subspan(static_cast<std::size_t>(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<int>(*binding.imageShape)) + ", dimension " + std::to_string(static_cast<int>(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<int>(*binding.imageShape)) + ", dimension " + std::to_string(static_cast<int>(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> 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<int>(resource.tileMode), resource.dccAddress});
item.imageAllocations.push_back(textures.size() - 1);
}
@@ -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<PFN_vkCreateImageView>("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<StorageTexture>&
VkImageViewMinLodCreateInfoEXT minLod{VK_STRUCTURE_TYPE_IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT};
ChainMinLod(context, descriptor, viewInfo, minLod);
Check(context.Function<PFN_vkCreateImageView>("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<PFN_vkCreateImageView>("vkCreateImageView")(context.device, &viewInfo, nullptr, &firstLayerView), "vkCreateImageView first layer");
}
void Texture::release() noexcept {
upload.reset();
if (view) context.Function<PFN_vkDestroyImageView>("vkDestroyImageView")(context.device, view, nullptr);
view = VK_NULL_HANDLE;
if (firstLayerView) context.Function<PFN_vkDestroyImageView>("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;
@@ -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<const std::byte>(reinterpret_cast<const std::byte*>(surface), static_cast<std::size_t>(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<const std::byte>(reinterpret_cast<const std::byte*>(surface), static_cast<std::size_t>(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() {