From e2ac1a669056401ad7576879776c20c4218354f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Sun, 4 Oct 2026 22:32:11 +0200 Subject: [PATCH] feat(shader): implement image_load_pck and image_load_mip_pck with signed variants Return the raw texel bits: dmask bit i selects texel dword i, dwords past the texel read 0 and the _sgn forms sign-extend texels narrower than 32 bits (measured on RDNA2 hardware). Only formats whose bits are exactly recoverable from the sampled view are accepted (unsigned/signed integer, unorm up to 16 bits, 32-bit float components), on a direct sampled image with identity swizzle; anything else throws. Co-Authored-By: Claude --- .../tests/execution/ImageLoadPacked.cpp | 209 ++++++++++++++++++ .../IrMetadata/ResourceInfo.hpp | 1 + .../IrMetadata/Resources.hpp | 2 + .../Optimization/ResourceMaterializer.hpp | 1 + .../Optimization/src/ResourceMaterializer.cpp | 14 +- .../Optimization/src/ResourceTracker.cpp | 3 +- .../include/RdnaDecoder/RdnaOpcode.hpp | 4 + .../RdnaDecoder/src/RdnaImageOpDecoder.cpp | 8 +- .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 4 + core/shader/recompiler/ShaderDiskCache.cpp | 9 +- .../SpirvBackend/src/SpirvImageEmitter.cpp | 52 +++++ .../MemoryInstructions/ImageInstructions.cpp | 4 +- .../MemoryInstructionDispatch.cpp | 4 + 13 files changed, 308 insertions(+), 7 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ImageLoadPacked.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageLoadPacked.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageLoadPacked.cpp new file mode 100644 index 000000000..b0612df8b --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageLoadPacked.cpp @@ -0,0 +1,209 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Words = 32; +constexpr std::uint32_t Results = 17; +constexpr std::uint32_t Width = 32; +constexpr std::uint32_t Levels = 2; +constexpr std::uint32_t Type2D = 9; +constexpr std::uint32_t IdentitySwizzle = 0xfacu; +alignas(256) std::array Buffer{}; +alignas(256) std::array Texels{}; + +alignas(256) constexpr std::array Code{ + 0x34060087, 0x7e3c0300, 0x7e3e0280, 0x36420087, 0x7e440280, 0xd5480023, 0x02050700, 0xf0081f08, + 0x00010a1e, 0xf00c1f08, 0x00010e1e, 0xf0081a08, 0x0001121e, 0xf00c1408, 0x0001141e, 0xf0101f08, + 0x00011521, 0xf0141308, 0x00011921, 0xbf8c3f70, 0xe0701000, 0x80000a03, 0xe0701004, 0x80000b03, + 0xe0701008, 0x80000c03, 0xe070100c, 0x80000d03, 0xe0701010, 0x80000e03, 0xe0701014, 0x80000f03, + 0xe0701018, 0x80001003, 0xe070101c, 0x80001103, 0xe0701020, 0x80001203, 0xe0701024, 0x80001303, + 0xe0701028, 0x80001403, 0xe070102c, 0x80001503, 0xe0701030, 0x80001603, 0xe0701034, 0x80001703, + 0xe0701038, 0x80001803, 0xe070103c, 0x80001903, 0xe0701040, 0x80001a03, 0xbf810000, +}; + +struct Format { + const char* name; + std::uint32_t format; + std::uint32_t bytes; + bool floating; +}; + +constexpr std::array Formats{{ + {"8_UNORM", 1u, 1u, false}, + {"16_UNORM", 7u, 2u, false}, + {"16_SINT", 12u, 2u, false}, + {"8_8_UNORM", 14u, 2u, false}, + {"32_FLOAT", 22u, 4u, true}, + {"16_16_SINT", 28u, 4u, false}, + {"8_8_8_8_UNORM", 56u, 4u, false}, + {"8_8_8_8_SINT", 61u, 4u, false}, + {"32_32_UINT", 62u, 8u, false}, + {"16_16_16_16_UNORM", 65u, 8u, false}, + {"32_32_32_32_FLOAT", 77u, 16u, true}, +}}; + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +std::vector Mips(const Format& format) { + return AgcDriver::Graphics::ComputeMipLayout(AgcDriver::Graphics::TextureTileMode::kLinear, format.format, Width, 1u, Levels); +} + +std::uint8_t* Texel(const AgcDriver::Graphics::TileMipLayout& mip, const Format& format, std::uint32_t x) { + return &Texels[mip.tiledOffset + static_cast(x) * format.bytes]; +} + +void FillTexture(const Format& format) { + const auto mips = Mips(format); + Require(AgcDriver::Graphics::ComputeSurfaceSize(mips, 1) <= Texels.size(), std::string("image load packed: the mip chain of ") + format.name + " does not fit the texel storage"); + Texels.fill(0xeeu); + for (std::uint32_t level = 0; level < Levels; ++level) { + for (std::uint32_t x = 0; x < mips[level].width; ++x) { + auto* texel = Texel(mips[level], format, x); + for (std::uint32_t byte = 0; byte < format.bytes; ++byte) { + texel[byte] = static_cast(0x80u + 37u * (x * format.bytes + byte) + 7u * x + 101u * level); + } + if (format.floating) { + for (std::uint32_t word = 0; word < format.bytes / 4u; ++word) { + std::uint32_t bits; + std::memcpy(&bits, texel + word * 4u, 4u); + bits = (bits & 0x807fffffu) | ((0x60u + ((bits >> 23u) & 0x3fu)) << 23u); + std::memcpy(texel + word * 4u, &bits, 4u); + } + } + } + } +} + +std::array RawTexel(const Format& format, std::uint32_t level, std::uint32_t x, bool sign) { + const auto mips = Mips(format); + std::array words{}; + std::memcpy(words.data(), Texel(mips[level], format, x), format.bytes); + if (sign && format.bytes < 4u) { + const auto shift = 32u - format.bytes * 8u; + words[0] = static_cast(static_cast(words[0] << shift) >> shift); + } + return words; +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +std::array TextureDescriptor(const void* data, std::uint32_t format, std::uint32_t swizzle) { + const auto address = static_cast(reinterpret_cast(data)); + return { + static_cast(address >> 8u), + static_cast((address >> 40u) & 0xffu) | (format << 20u) | (((Width - 1u) & 3u) << 30u), + (Width - 1u) >> 2u, + swizzle | ((Levels - 1u) << 16u) | (Type2D << 28u), + 0u, + (Levels - 1u) << 4u, + 0u, + 0u, + }; +} + +void Run(AgcDriver::VulkanDevice& device, std::uint32_t format, std::uint32_t swizzle) { + Buffer.fill(0xdeadbeefu); + std::vector userData(16, 0u); + const auto buffer = BufferDescriptor(Buffer.data(), static_cast(Buffer.size() * 4u)); + const auto texture = TextureDescriptor(Texels.data(), format, swizzle); + std::copy(buffer.begin(), buffer.end(), userData.begin()); + std::copy(texture.begin(), texture.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check(const Format& format) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const auto plain = RawTexel(format, 0u, tid, false); + const auto sign = RawTexel(format, 0u, tid, true); + const auto level = (tid >> 3u) & 1u; + const auto mipPlain = RawTexel(format, level, tid & 7u, false); + const auto mipSign = RawTexel(format, level, tid & 7u, true); + const std::array expected{ + plain[0], plain[1], plain[2], plain[3], + sign[0], sign[1], sign[2], sign[3], + plain[1], plain[3], + sign[2], + mipPlain[0], mipPlain[1], mipPlain[2], mipPlain[3], + mipSign[0], mipSign[1], + }; + constexpr std::array names{ + "image_load_pck dmask:0xf [0]", "image_load_pck dmask:0xf [1]", "image_load_pck dmask:0xf [2]", "image_load_pck dmask:0xf [3]", + "image_load_pck_sgn dmask:0xf [0]", "image_load_pck_sgn dmask:0xf [1]", "image_load_pck_sgn dmask:0xf [2]", "image_load_pck_sgn dmask:0xf [3]", + "image_load_pck dmask:0xa [0]", "image_load_pck dmask:0xa [1]", + "image_load_pck_sgn dmask:0x4", + "image_load_mip_pck dmask:0xf [0]", "image_load_mip_pck dmask:0xf [1]", "image_load_mip_pck dmask:0xf [2]", "image_load_mip_pck dmask:0xf [3]", + "image_load_mip_pck_sgn dmask:0x3 [0]", "image_load_mip_pck_sgn dmask:0x3 [1]", + }; + for (std::uint32_t index = 0; index < Results; ++index) { + const auto actual = Buffer[tid * Words + index]; + Require(actual == expected[index], std::string(format.name) + " " + names[index] + ": thread " + std::to_string(tid) + " is " + Hex(actual) + ", expected " + Hex(expected[index])); + } + } +} + +void RequireRefused(AgcDriver::VulkanDevice& device, std::uint32_t format, std::uint32_t swizzle, const std::string& reason, const std::string& what) { + std::string refusal; + try { + Run(device, format, swizzle); + } catch (const std::exception& error) { + refusal = error.what(); + } + Require(refusal.find(reason) != std::string::npos, "image_load_pck of " + what + " was not refused: " + refusal); +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + for (const auto& format : Formats) { + FillTexture(format); + Run(*device, format.format, IdentitySwizzle); + Check(format); + } + FillTexture(Formats[6]); + RequireRefused(*device, 57u, IdentitySwizzle, "not recoverable", "8_8_8_8_SNORM"); + RequireRefused(*device, 56u, 0xf2eu, "identity swizzle", "a swizzled texture"); + std::puts("image load packed tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp index 996f4e0a0..ee85759d2 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourceInfo.hpp @@ -43,6 +43,7 @@ struct MemoryInfo { bool d16 = false; bool imageHasMip = false; bool imageR128 = false; + bool imagePacked = false; bool idxen = false; bool offen = false; bool planningOnly = false; diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp index bbe52b016..5995b2a43 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp @@ -52,6 +52,8 @@ struct ImageResource { bool r128 = false; bool depthBits = false; bool depthUnorm16 = false; + bool packed = false; + IrBufferFormat packedFormat = IrBufferFormat::Invalid; std::uint32_t indirectRoot = NoIndirectImage; std::uint32_t indirectMappingOffset = 0; std::uint32_t indirectSearchIterations = 0; diff --git a/core/shader/recompiler/Optimization/include/Optimization/ResourceMaterializer.hpp b/core/shader/recompiler/Optimization/include/Optimization/ResourceMaterializer.hpp index 0bbff3eb4..4ba66c32a 100644 --- a/core/shader/recompiler/Optimization/include/Optimization/ResourceMaterializer.hpp +++ b/core/shader/recompiler/Optimization/include/Optimization/ResourceMaterializer.hpp @@ -31,6 +31,7 @@ struct ResourceSpecialization { bool fmask = false; bool depthBits = false; bool depthUnorm16 = false; + IrBufferFormat packedFormat = IrBufferFormat::Invalid; bool operator==(const Image& other) const; }; diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index 3ef05a961..adc4c1a40 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -45,6 +45,7 @@ struct DecodedImage { bool fmask = false; bool depthBits = false; bool depthUnorm16 = false; + IrBufferFormat packedFormat = IrBufferFormat::Invalid; }; ShaderBufferResource decodeBufferDescriptor(const DescriptorValue& value) { @@ -158,6 +159,12 @@ DecodedImage decodeImageDescriptor(const DescriptorValue& descriptor, const Imag if (decoded.fmask && (storage || base.depthCompare || base.indirectRoot != ImageResource::NoIndirectImage)) { throw std::runtime_error("FMASK requires a direct sampled image load"); } + if (base.packed) { + if (storage || base.indirectRoot != ImageResource::NoIndirectImage || descriptorImageSwizzle(descriptor) != ShaderImageIdentitySwizzle) { + throw std::runtime_error("packed image load requires a direct sampled image with identity swizzle"); + } + decoded.packedFormat = format; + } decoded.conversionFormat = RemapTextureFormat(format) != format ? format : IrBufferFormat::Invalid; if (storage || decoded.conversionFormat != IrBufferFormat::Invalid) { decoded.shaderSwizzle = descriptorImageSwizzle(descriptor); @@ -291,6 +298,9 @@ void resolveTableImage(const IrResourcePlan& plan, std::uint32_t imageIndex, con if (image.resourceClass != ImageResourceClass::Sampled) { rejectTable(BindlessRejection::Storage, "bindless storage image tables are unsupported"); } + if (image.packed) { + throw std::runtime_error("bindless packed image tables are unsupported"); + } const auto slots = ResourceMaterializer::BindlessSlots(); DescriptorValue heapValue; walker.EvaluateDescriptorSource(plan, table.heapSource, runtime, heapValue); @@ -534,6 +544,7 @@ void buildResourceSpecialization(const IrResourcePlan& plan, ResourceSnapshot& s entry.fmask = decoded.fmask; entry.depthBits = decoded.depthBits; entry.depthUnorm16 = decoded.depthUnorm16; + entry.packedFormat = decoded.packedFormat; result.images.push_back(entry); } @@ -620,6 +631,7 @@ void ResourceMaterializer::Apply(IrProgram& program, const ResourceSpecializatio image.cube = source.cube; image.depthBits = source.depthBits; image.depthUnorm16 = source.depthUnorm16; + image.packedFormat = source.packedFormat; image.indirectResources.clear(); } for (std::uint32_t index = 0; index < images.size(); index++) { @@ -948,7 +960,7 @@ bool ResourceSpecialization::Buffer::operator==(const Buffer& other) const { } bool ResourceSpecialization::Image::operator==(const Image& other) const { - return numericClass == other.numericClass && dimension == other.dimension && mipCount == other.mipCount && conversionFormat == other.conversionFormat && shaderSwizzle == other.shaderSwizzle && indirectRoot == other.indirectRoot && indirectMappingOffset == other.indirectMappingOffset && indirectSearchIterations == other.indirectSearchIterations && cube == other.cube && fmask == other.fmask && depthBits == other.depthBits && depthUnorm16 == other.depthUnorm16; + return numericClass == other.numericClass && dimension == other.dimension && mipCount == other.mipCount && conversionFormat == other.conversionFormat && shaderSwizzle == other.shaderSwizzle && indirectRoot == other.indirectRoot && indirectMappingOffset == other.indirectMappingOffset && indirectSearchIterations == other.indirectSearchIterations && cube == other.cube && fmask == other.fmask && depthBits == other.depthBits && depthUnorm16 == other.depthUnorm16 && packedFormat == other.packedFormat; } bool ResourceSpecialization::operator==(const ResourceSpecialization& other) const { diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 84eaa9545..450a60b45 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -861,7 +861,7 @@ private: const bool depth = (memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0; for (std::uint32_t i = 0; i < m_info.images.size(); i++) { auto& image = m_info.images[i]; - if (image.source == source && image.resourceClass == resourceClass && image.dimension == memory.imageDimension && image.mipMode == mip && image.depthCompare == depth && image.r128 == memory.imageR128) { + if (image.source == source && image.resourceClass == resourceClass && image.dimension == memory.imageDimension && image.mipMode == mip && image.depthCompare == depth && image.r128 == memory.imageR128 && image.packed == memory.imagePacked) { Merge(image, op, pc); return i; } @@ -877,6 +877,7 @@ private: image.mipMode = mip; image.depthCompare = depth; image.r128 = memory.imageR128; + image.packed = memory.imagePacked; Merge(image, op, pc); m_info.images.push_back(image); return static_cast(m_info.images.size() - 1); diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 74a73cd99..9baa52db7 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -822,6 +822,10 @@ enum class RdnaOpcode : std::uint16_t { DsReadAddtidB32, ImageGetLod, ImageLoadMip, + ImageLoadPck, + ImageLoadPckSgn, + ImageLoadMipPck, + ImageLoadMipPckSgn, ImageStoreMip, ImageAtomicSwap, ImageAtomicAdd, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp index e5deace25..d90500232 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp @@ -97,6 +97,10 @@ constexpr ImageOpcodeInfo imageOpcodes[] = { {0x1cu, RdnaOpcode::ImageAtomicDec, nullptr, 0, false, false, true}, {0x00u, RdnaOpcode::ImageLoad, nullptr, 0, false, false, false}, {0x01u, RdnaOpcode::ImageLoadMip, nullptr, 0, false, false, false}, + {0x02u, RdnaOpcode::ImageLoadPck, nullptr, 0, false, false, false}, + {0x03u, RdnaOpcode::ImageLoadPckSgn, nullptr, 0, false, false, false}, + {0x04u, RdnaOpcode::ImageLoadMipPck, nullptr, 0, false, false, false}, + {0x05u, RdnaOpcode::ImageLoadMipPckSgn, nullptr, 0, false, false, false}, {0x08u, RdnaOpcode::ImageStore, nullptr, 0, false, false, false}, {0x09u, RdnaOpcode::ImageStoreMip, nullptr, 0, false, false, false}, {0x0eu, RdnaOpcode::ImageGetResinfo, nullptr, 0, false, false, false}, @@ -282,7 +286,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span> 3u) & 7u); const bool multisampled = dimension == RdnaImageDimension::Dim2DMsaa || dimension == RdnaImageDimension::Dim2DMsaaArray; - if (multisampled && (info.sample || info.gather || opcode == 0x60u || opcode == 1u || opcode == 9u)) { + if (multisampled && (info.sample || info.gather || opcode == 0x60u || (opcode >= 1u && opcode <= 5u) || opcode == 9u)) { throw std::runtime_error("unsupported multisampled MIMG operation"); } const auto dmask = (word0 >> 8u) & 15u; @@ -295,7 +299,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span(specialization.boundDescriptors)); const auto& switches = switchKey(); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp index dfbaf9973..1807dc99b 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp @@ -448,6 +448,55 @@ std::uint32_t UnpackImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess return result; } +std::uint32_t PackedImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, std::uint32_t texel) { + auto& state = ctx.state; + const auto format = access.image.packedFormat; + if (format == IrBufferFormat::Invalid || access.table.mapped != 0 || access.image.depthBits || access.image.conversionFormat != IrBufferFormat::Invalid) { + ctx.Fail(access.inst, "reads packed texels from an image without a packed format"); + } + const auto info = GetFormatInfo(format); + std::uint32_t texelBits = 0; + for (std::uint32_t component = 0; component < info.componentCount; component++) { + const auto bits = info.componentBits[component]; + const bool exact = info.type == SpirvFormatComponentType::Uint || info.type == SpirvFormatComponentType::Sint || (info.type == SpirvFormatComponentType::Unorm && bits <= 16u) || (info.type == SpirvFormatComponentType::Float && bits == 32u); + if (info.packedBitfield || !exact) { + ctx.Fail(access.inst, "reads packed texels of a format whose bits are not recoverable from the view"); + } + texelBits += bits; + } + const auto numericClass = access.image.numericClass; + std::uint32_t words[4] = {ConstantU32(state, 0), ConstantU32(state, 0), ConstantU32(state, 0), ConstantU32(state, 0)}; + std::uint32_t position = 0; + for (std::uint32_t component = 0; component < info.componentCount; component++) { + const auto bits = info.componentBits[component]; + const auto scalar = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeExtract, ImageScalarType(state, numericClass), scalar, texel, component); + std::uint32_t value; + if (info.type == SpirvFormatComponentType::Unorm) { + const auto scaled = Binary(state, spv::OpFMul, TypeF32(state), scalar, ConstantF32Value(state, static_cast((1u << bits) - 1u))); + value = Unary(state, spv::OpConvertFToU, TypeU32(state), Binary(state, spv::OpFAdd, TypeF32(state), scaled, ConstantF32Value(state, 0.5f))); + } else { + value = SampledComponentBits(ctx, scalar, numericClass); + } + if (bits < 32u) { + value = Binary(state, spv::OpBitwiseAnd, TypeU32(state), value, ConstantU32(state, (1u << bits) - 1u)); + } + const auto word = position / 32u; + if (position % 32u != 0u) { + value = Binary(state, spv::OpShiftLeftLogical, TypeU32(state), value, ConstantU32(state, position % 32u)); + } + words[word] = Binary(state, spv::OpBitwiseOr, TypeU32(state), words[word], value); + position += bits; + } + if (access.mem.dataSigned && texelBits < 32u) { + const auto shift = ConstantU32(state, 32u - texelBits); + words[0] = Binary(state, spv::OpShiftRightArithmetic, TypeU32(state), Binary(state, spv::OpShiftLeftLogical, TypeU32(state), words[0], shift), shift); + } + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpCompositeConstruct, TypeU32Vector(state, 4), result, words[0], words[1], words[2], words[3]); + return result; +} + std::uint32_t UnpackImageGather(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, std::uint32_t gathered) { auto& state = ctx.state; const auto info = ImageConversionFormat(access.image); @@ -621,6 +670,9 @@ void EmitReadOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access) { } else { state.module.AddFunction(spv::OpImageFetch, ImageVectorType(state, numericClass, 4), color, descriptor, coord, spv::ImageOperandsLodMask, LodU32(ctx, access)); } + if (access.mem.imagePacked) { + return PackedImageTexel(ctx, access, color); + } return TableResult(ctx, access, ResultVector(ctx, access, UnpackImageTexel(ctx, access, color), numericClass, false, false)); })); } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp index 412c979ab..761b5f0e3 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp @@ -34,7 +34,9 @@ MemoryInfo imageMemoryInfoFromInstruction(const RdnaInstruction& inst) { memory.imageSampleFlags = inst.imageSampleFlags; memory.imageDimension = inst.imageDimension; memory.imageAddressComponents = inst.imageAddressComponents; - memory.imageHasMip = inst.op == RdnaOpcode::ImageLoadMip || inst.op == RdnaOpcode::ImageStoreMip; + memory.imageHasMip = inst.op == RdnaOpcode::ImageLoadMip || inst.op == RdnaOpcode::ImageStoreMip || inst.op == RdnaOpcode::ImageLoadMipPck || inst.op == RdnaOpcode::ImageLoadMipPckSgn; + memory.imagePacked = inst.op == RdnaOpcode::ImageLoadPck || inst.op == RdnaOpcode::ImageLoadPckSgn || inst.op == RdnaOpcode::ImageLoadMipPck || inst.op == RdnaOpcode::ImageLoadMipPckSgn; + memory.dataSigned = inst.op == RdnaOpcode::ImageLoadPckSgn || inst.op == RdnaOpcode::ImageLoadMipPckSgn; memory.imageR128 = inst.imageR128; return memory; } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index 30679b00e..7134a3c5a 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -375,6 +375,10 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { return imageAtomic(inst, IrOpcode::ImageAtomicDec32); case RdnaOpcode::ImageLoad: case RdnaOpcode::ImageLoadMip: + case RdnaOpcode::ImageLoadPck: + case RdnaOpcode::ImageLoadPckSgn: + case RdnaOpcode::ImageLoadMipPck: + case RdnaOpcode::ImageLoadMipPckSgn: return imageLoad(inst); case RdnaOpcode::ImageStore: case RdnaOpcode::ImageStoreMip: