From ede33d56e22d5582822039cd27c0da3e0a43e9bc Mon Sep 17 00:00:00 2001 From: Gjon Gojcevic Date: Wed, 7 Oct 2026 21:37:01 -0400 Subject: [PATCH 1/2] feat(agc): load and store R10_G11_B11 float images Format 43 packs R as a 10-bit and G and B as 11-bit unsigned floats, which Vulkan has no format for. Keep it in an R32_UINT image like the other packed formats without a host equivalent, and convert texels in the shader on image loads and stores; sampling it still throws. --- .../Graphics/src/TextureFormat.cpp | 2 +- .../tests/execution/ImageConvertedUnorm.cpp | 57 ++++++++++++++++++- .../RdnaDecoder/src/RdnaDescriptorFormat.cpp | 5 +- .../SpirvBackend/src/SpirvImageEmitter.cpp | 16 ++++-- 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp index 04fda0eda..d5978cf76 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/TextureFormat.cpp @@ -96,7 +96,7 @@ constexpr auto MakeFormatLookupTable() { constexpr auto kFormatLookupTable = MakeFormatLookupTable(); std::uint32_t remapGuestFormat(std::uint32_t guestFormat) { - return guestFormat == 30 || guestFormat == 34 ? 20 : guestFormat; + return guestFormat == 30 || guestFormat == 34 || guestFormat == 43 ? 20 : guestFormat; } const FormatEntry& findFormatEntry(std::uint32_t guestFormat) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp index 376473101..9db8517c6 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp @@ -34,6 +34,8 @@ constexpr std::uint32_t LoadGroups = LoadWidth / Threads; constexpr std::uint32_t Type2D = 9; constexpr std::uint32_t UnormFormat = 30; constexpr std::uint32_t UintFormat = 34; +constexpr std::uint32_t FloatFormat = 43; +constexpr std::size_t FloatOffset = 40960; constexpr std::uint32_t SwizzleXYZ1 = 0x3acu; constexpr std::uint32_t SwizzleXZY1 = 0x374u; constexpr std::uint32_t SwizzleZYX1 = 0x32eu; @@ -249,14 +251,60 @@ void CheckStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels, std::span } } -void RequireRefused(AgcDriver::VulkanDevice& device, std::span code, const std::uint8_t* texels, std::uint32_t swizzle, const std::array& extra, const std::string& reason, const std::string& what) { +constexpr std::array FloatValues{0.0f, 1.0f, 0.5f, 2.0f, 0.25f, 3.0f, 1.5f, 10.0f, 0.75f, 6.5f, 0.125f}; + +std::uint32_t SmallFloat(float value, std::uint32_t mantissaBits) { + if (value == 0.0f) return 0u; + const auto bits = std::bit_cast(value); + const auto exponent = ((bits >> 23u) & 0xffu) - 127u + 15u; + return (exponent << mantissaBits) | ((bits & 0x7fffffu) >> (23u - mantissaBits)); +} + +std::uint32_t FloatTexel(std::uint32_t index) { + return SmallFloat(FloatValues[index % 11u], 5u) | (SmallFloat(FloatValues[(index + 4u) % 11u], 6u) << 10u) | (SmallFloat(FloatValues[(index + 8u) % 11u], 6u) << 21u); +} + +void CheckFloatLoad(AgcDriver::VulkanDevice& device, std::uint8_t* texels) { + for (std::uint32_t index = 0; index < LoadWidth; ++index) { + const auto texel = FloatTexel(index); + std::memcpy(texels + index * 4u, &texel, 4u); + } + Run(device, LoadXyzw, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, LoadWidth), {}, LoadGroups); + for (std::uint32_t index = 0; index < LoadWidth; ++index) { + const std::array expected{FloatValues[index % 11u], FloatValues[(index + 4u) % 11u], FloatValues[(index + 8u) % 11u], 1.0f}; + for (std::uint32_t component = 0; component < 4u; ++component) { + const auto actual = Output[index * 4u + component]; + Require(actual == std::bit_cast(expected[component]), "image_load of R10_G11_B11_FLOAT: texel " + std::to_string(index) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(std::bit_cast(expected[component]))); + } + } +} + +void CheckFloatStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels) { + std::fill(texels, texels + Threads * 4u, static_cast(0xa5u)); + Input.fill(0u); + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + Input[tid * 4u + 0u] = std::bit_cast(FloatValues[tid % 11u]); + Input[tid * 4u + 1u] = std::bit_cast(FloatValues[(tid + 4u) % 11u]); + Input[tid * 4u + 2u] = std::bit_cast(FloatValues[(tid + 8u) % 11u]); + } + Run(device, StoreXyz, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, Threads), BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)), 1); + AgcDriver::Graphics::StorageTexture::FlushPending(reinterpret_cast(texels), Threads * 4u, nullptr, "test"); + device.WaitIdle(); + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + std::uint32_t actual = 0; + std::memcpy(&actual, texels + tid * 4u, 4u); + Require(actual == FloatTexel(tid), "image_store of R10_G11_B11_FLOAT: texel " + std::to_string(tid) + " is " + Hex(actual) + ", expected " + Hex(FloatTexel(tid))); + } +} + +void RequireRefused(AgcDriver::VulkanDevice& device, std::span code, const std::uint8_t* texels, std::uint32_t swizzle, const std::array& extra, const std::string& reason, const std::string& what, std::uint32_t format = UnormFormat) { std::string refusal; try { - Run(device, code, TextureDescriptor(texels, UnormFormat, swizzle, LoadWidth), extra, 1); + Run(device, code, TextureDescriptor(texels, format, swizzle, LoadWidth), extra, 1); } catch (const std::exception& error) { refusal = error.what(); } - Require(refusal.find(reason) != std::string::npos, what + " of a 10_11_11_UNORM image was not refused: " + refusal); + Require(refusal.find(reason) != std::string::npos, what + " of a converted image was not refused: " + refusal); } } @@ -279,6 +327,9 @@ int main() { CheckRoundTrip(*device, texels); CheckStore(*device, texels + StoreOffset, StoreXyz, 0x7u, "image_store dmask:0x7"); CheckStore(*device, texels + StoreOffset, StoreXz, 0x5u, "image_store dmask:0x5"); + CheckFloatStore(*device, texels + FloatOffset); + CheckFloatLoad(*device, texels + FloatOffset); + RequireRefused(*device, SampleLz, texels + FloatOffset, SwizzleXYZ1, PointSampler, "samples or gathers a converted float image", "image_sample_lz of R10_G11_B11_FLOAT", FloatFormat); RequireRefused(*device, SampleLz, texels, SwizzleXYZ1, PointSampler, "samples or gathers a converted unorm image", "image_sample_lz"); RequireRefused(*device, Gather4Lz, texels, SwizzleXYZ1, PointSampler, "samples or gathers a converted unorm image", "image_gather4_lz"); RequireRefused(*device, GetLod, texels, SwizzleXYZ1, PointSampler, "queries the level of detail of a converted unorm image", "image_get_lod"); diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaDescriptorFormat.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaDescriptorFormat.cpp index 21faa359a..a9cc292e6 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaDescriptorFormat.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaDescriptorFormat.cpp @@ -13,7 +13,7 @@ struct FormatInfo { bool sint32; }; -constexpr std::array kFormatInfoTable {{ +constexpr std::array kFormatInfoTable {{ {IrBufferFormat::Format8UNorm, true, false, false}, {IrBufferFormat::Format8SNorm, false, false, false}, {IrBufferFormat::Format8UInt, true, true, false}, @@ -38,6 +38,7 @@ constexpr std::array kFormatInfoTable {{ {IrBufferFormat::Format11_11_10UNorm, true, true, false}, {IrBufferFormat::Format11_11_10UInt, true, true, false}, {IrBufferFormat::Format11_11_10Float, true, false, false}, + {IrBufferFormat::Format10_11_11Float, true, true, false}, {IrBufferFormat::Format10_10_10_2UNorm, true, false, false}, {IrBufferFormat::Format10_10_10_2UInt, true, true, false}, {IrBufferFormat::Format8_8_8_8UNorm, true, false, false}, @@ -120,7 +121,7 @@ IrTextureNumericClass SampledTextureNumericClass(IrBufferFormat format) { } IrBufferFormat RemapTextureFormat(IrBufferFormat format) { - return format == IrBufferFormat::Format11_11_10UNorm || format == IrBufferFormat::Format11_11_10UInt ? IrBufferFormat::Format32UInt : format; + return format == IrBufferFormat::Format11_11_10UNorm || format == IrBufferFormat::Format11_11_10UInt || format == IrBufferFormat::Format10_11_11Float ? IrBufferFormat::Format32UInt : format; } std::uint32_t DepthBitsTextureWidth(std::uint32_t word1, std::uint32_t word3) { diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp index bf83d37f2..23264851e 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp @@ -479,15 +479,15 @@ SpirvBufferFormatInfo ImageConversionFormat(const ImageResource& image) { return {}; } const auto info = GetFormatInfo(format); - if (SampledTextureNumericClass(format) != IrTextureNumericClass::Uint || RemapTextureFormat(format) == format || (info.type != SpirvFormatComponentType::Uint && info.type != SpirvFormatComponentType::Unorm) || !info.packedBitfield || info.byteSize != sizeof(std::uint32_t) || info.componentCount == 0u || info.componentCount > 4u) { - throw std::runtime_error("image conversion format is not a packed 32-bit unsigned integer or unorm format"); + if (SampledTextureNumericClass(format) != IrTextureNumericClass::Uint || RemapTextureFormat(format) == format || (info.type != SpirvFormatComponentType::Uint && info.type != SpirvFormatComponentType::Unorm && info.type != SpirvFormatComponentType::Float) || !info.packedBitfield || info.byteSize != sizeof(std::uint32_t) || info.componentCount == 0u || info.componentCount > 4u) { + throw std::runtime_error("image conversion format is not a packed 32-bit unsigned integer, unorm or float format"); } return info; } void RequireConvertedUnormAccess(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, const SpirvBufferFormatInfo& info) { if (access.mem.dataBits == 16u) { - ctx.Fail(access.inst, "reads or writes a converted unorm image with 16-bit data, which is not implemented"); + ctx.Fail(access.inst, info.type == SpirvFormatComponentType::Float ? "reads or writes a converted float image with 16-bit data, which is not implemented" : "reads or writes a converted unorm image with 16-bit data, which is not implemented"); } for (std::uint32_t component = 0; component < 4u; component++) { const auto selector = (access.image.shaderSwizzle >> (component * 3u)) & 7u; @@ -506,7 +506,7 @@ std::uint32_t UnpackImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess if (info.format == IrBufferFormat::Invalid) { return texel; } - const bool unorm = info.type == SpirvFormatComponentType::Unorm; + const bool unorm = info.type == SpirvFormatComponentType::Unorm || info.type == SpirvFormatComponentType::Float; if (unorm) { RequireConvertedUnormAccess(ctx, access, info); } @@ -676,7 +676,7 @@ std::uint32_t PackImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess& if (info.format == IrBufferFormat::Invalid) { return texel; } - const bool unorm = info.type == SpirvFormatComponentType::Unorm; + const bool unorm = info.type == SpirvFormatComponentType::Unorm || info.type == SpirvFormatComponentType::Float; if (unorm) { RequireConvertedUnormAccess(ctx, access, info); } @@ -840,6 +840,9 @@ void EmitQueryLodOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access) { if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Unorm) { ctx.Fail(access.inst, "queries the level of detail of a converted unorm image, which is not implemented"); } + if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Float) { + ctx.Fail(access.inst, "queries the level of detail of a converted float image, which is not implemented"); + } state.module.EmitCapability(spv::CapabilityImageQuery); const auto sampled = MakeSampledImage(state, access.mem.resource, access.mem.sampler, access.slot); const auto coord = CoordF32(ctx, access, 0, RdnaImageDimensionInfoFor(access.image.dimension).spatialComponents, AddressDimension(access).spatialComponents); @@ -962,6 +965,9 @@ SampleSetup MakeSampleSetup(SpirvValueEmitContext& ctx, const ImageEmitAccess& a if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Unorm) { ctx.Fail(access.inst, "samples or gathers a converted unorm image, which needs filtering in the shader and is not implemented"); } + if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Float) { + ctx.Fail(access.inst, "samples or gathers a converted float image, which needs filtering in the shader and is not implemented"); + } const auto coord = CoordF32(ctx, access, layout.coord, dimensionInfo.coordinateComponents, AddressDimension(access).coordinateComponents); return {dimensionInfo, layout, access.image.numericClass, dref, coord}; } From 61fbade375e2017c1e87623914b994d4380c279c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Thu, 8 Oct 2026 15:04:37 +0200 Subject: [PATCH 2/2] test(agc): pin R10_G11_B11 float image edge values Store: negative, -0, NaN (quiet and low-payload), +-Inf, overflow to the largest finite value, truncation and small-float denormals. Load: Inf, denormal and largest finite codes. Values measured on gfx1035. Co-Authored-By: Claude --- .../tests/execution/ImageConvertedUnorm.cpp | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp index 9db8517c6..dc5a75a2c 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageConvertedUnorm.cpp @@ -253,6 +253,22 @@ void CheckStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels, std::span constexpr std::array FloatValues{0.0f, 1.0f, 0.5f, 2.0f, 0.25f, 3.0f, 1.5f, 10.0f, 0.75f, 6.5f, 0.125f}; +constexpr std::array, 7> FloatStoreEdges{{ + {0xbf800000u, 0x80000000u, 0x7fc00000u, 0xfc000000u}, + {0x7f800000u, 0x7149f2cau, 0x477fff00u, 0xf7feffe0u}, + {0x3f830000u, 0x3f818000u, 0x35800000u, 0x002f01e0u}, + {0x00000001u, 0xff800000u, 0xffc00000u, 0xfc000000u}, + {0x477c0000u, 0x4788b800u, 0x33000000u, 0x001effdfu}, + {0x36400000u, 0x477e0000u, 0x3dcccccdu, 0x5cdefc01u}, + {0x7f800001u, 0x7f810000u, 0x7fa00000u, 0xfa1f07e1u}, +}}; + +constexpr std::array, 3> FloatLoadEdges{{ + {0x3e0u | (0x7c0u << 10u) | (0x03fu << 21u), 0x7f800000u, 0x7f800000u, 0x387c0000u}, + {0x001u | (0x041u << 10u) | (0x7bfu << 21u), 0x36000000u, 0x38820000u, 0x477e0000u}, + {0x3dfu | (0x001u << 10u), 0x477c0000u, 0x35800000u, 0x00000000u}, +}}; + std::uint32_t SmallFloat(float value, std::uint32_t mantissaBits) { if (value == 0.0f) return 0u; const auto bits = std::bit_cast(value); @@ -266,15 +282,18 @@ std::uint32_t FloatTexel(std::uint32_t index) { void CheckFloatLoad(AgcDriver::VulkanDevice& device, std::uint8_t* texels) { for (std::uint32_t index = 0; index < LoadWidth; ++index) { - const auto texel = FloatTexel(index); + const auto texel = index < FloatLoadEdges.size() ? FloatLoadEdges[index][0] : FloatTexel(index); std::memcpy(texels + index * 4u, &texel, 4u); } Run(device, LoadXyzw, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, LoadWidth), {}, LoadGroups); for (std::uint32_t index = 0; index < LoadWidth; ++index) { - const std::array expected{FloatValues[index % 11u], FloatValues[(index + 4u) % 11u], FloatValues[(index + 8u) % 11u], 1.0f}; + std::array expected{std::bit_cast(FloatValues[index % 11u]), std::bit_cast(FloatValues[(index + 4u) % 11u]), std::bit_cast(FloatValues[(index + 8u) % 11u]), std::bit_cast(1.0f)}; + if (index < FloatLoadEdges.size()) { + expected = {FloatLoadEdges[index][1], FloatLoadEdges[index][2], FloatLoadEdges[index][3], std::bit_cast(1.0f)}; + } for (std::uint32_t component = 0; component < 4u; ++component) { const auto actual = Output[index * 4u + component]; - Require(actual == std::bit_cast(expected[component]), "image_load of R10_G11_B11_FLOAT: texel " + std::to_string(index) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(std::bit_cast(expected[component]))); + Require(actual == expected[component], "image_load of R10_G11_B11_FLOAT: texel " + std::to_string(index) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(expected[component])); } } } @@ -286,6 +305,11 @@ void CheckFloatStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels) { Input[tid * 4u + 0u] = std::bit_cast(FloatValues[tid % 11u]); Input[tid * 4u + 1u] = std::bit_cast(FloatValues[(tid + 4u) % 11u]); Input[tid * 4u + 2u] = std::bit_cast(FloatValues[(tid + 8u) % 11u]); + if (tid < FloatStoreEdges.size()) { + for (std::uint32_t component = 0; component < 3u; ++component) { + Input[tid * 4u + component] = FloatStoreEdges[tid][component]; + } + } } Run(device, StoreXyz, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, Threads), BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)), 1); AgcDriver::Graphics::StorageTexture::FlushPending(reinterpret_cast(texels), Threads * 4u, nullptr, "test"); @@ -293,7 +317,8 @@ void CheckFloatStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels) { for (std::uint32_t tid = 0; tid < Threads; ++tid) { std::uint32_t actual = 0; std::memcpy(&actual, texels + tid * 4u, 4u); - Require(actual == FloatTexel(tid), "image_store of R10_G11_B11_FLOAT: texel " + std::to_string(tid) + " is " + Hex(actual) + ", expected " + Hex(FloatTexel(tid))); + const auto expected = tid < FloatStoreEdges.size() ? FloatStoreEdges[tid][3] : FloatTexel(tid); + Require(actual == expected, "image_store of R10_G11_B11_FLOAT: texel " + std::to_string(tid) + " is " + Hex(actual) + ", expected " + Hex(expected)); } }