diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/EmulatedColorCompare.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/EmulatedColorCompare.cpp index 92443d6f9..c664f1d29 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/EmulatedColorCompare.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/EmulatedColorCompare.cpp @@ -5,6 +5,7 @@ #include "VulkanTestDevice.hpp" #include #include +#include #include #include #include @@ -43,6 +44,7 @@ constexpr std::array Red{0, 64, 128, 255}; alignas(256) std::array Input{}; alignas(256) std::array Output{}; +alignas(256) std::array Extra{}; alignas(4096) std::array Texels{}; alignas(256) constexpr std::array Code{ @@ -50,11 +52,17 @@ alignas(256) constexpr std::array Code{ 0x34160082, 0xe0701000, 0x8001040b, 0xbf810000, 0xbf810000, 0xbf810000, }; +alignas(256) constexpr std::array OffsetCode{ + 0x1614008c, 0xe03c1000, 0x8000030a, 0xe03c1000, 0x8005060a, 0xbf8c3f70, 0x7e020306, 0x7e040307, + 0x7e0c0308, 0xf0f80108, 0x00820901, 0xbf8c3f70, 0x34160082, 0xe0701000, 0x8001090b, 0xbf810000, +}; + struct Sampler { std::uint32_t clamp; std::uint32_t filter; std::uint32_t border = BorderBlack; std::uint32_t reduction = 0; + std::uint32_t lodBias = 0; }; std::array BufferDescriptor(const void* data, std::uint32_t bytes) { @@ -77,7 +85,7 @@ std::array SamplerDescriptor(const Sampler& sampler) { return { sampler.clamp | (sampler.clamp << 3u) | (ClampEdge << 6u) | (LessEqual << 12u) | (sampler.reduction << 29u), 0u, - (sampler.filter << 20u) | (sampler.filter << 22u), + sampler.lodBias | (sampler.filter << 20u) | (sampler.filter << 22u), sampler.border << 30u, }; } @@ -109,6 +117,19 @@ void FillInput() { Input[6] = 0.6f; Input[7] = -0.1f; Input[8] = 0.5f; Input[9] = 0.3f; Input[10] = 0.5f; Input[11] = 1.1f; Input[12] = 0.9f; Input[13] = 1.05f; Input[14] = -0.05f; + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const auto x = static_cast(tid % 7u) - 3; + const auto y = static_cast((tid / 7u) % 5u) - 2; + Extra[tid * 3u + 0u] = (static_cast(x) & 0x3fu) | ((static_cast(y) & 0x3fu) << 8u) | (tid % 3u == 0u ? 0x3f0000u : 0u); + Extra[tid * 3u + 1u] = std::bit_cast(-2.5f + 0.25f * static_cast(tid % 21u)); + Extra[tid * 3u + 2u] = std::bit_cast(0.5f * static_cast(tid % 5u)); + } + Extra[0] = 0x00001f20u; +} + +std::int32_t OffsetComponent(std::uint32_t tid, std::uint32_t component) { + const auto field = (Extra[tid * 3u] >> (component * 8u)) & 0x3fu; + return static_cast(field ^ 0x20u) - 0x20; } float ReferenceTexel(int x, int y, float reference, const Sampler& sampler) { @@ -127,15 +148,17 @@ float ReferenceTexel(int x, int y, float reference, const Sampler& sampler) { return reference <= red ? 1.0f : 0.0f; } -float Expected(std::uint32_t tid, const Sampler& sampler) { +float Expected(std::uint32_t tid, const Sampler& sampler, bool offsets) { const float u = Input[tid * 3u + 1u] * static_cast(Side); const float v = Input[tid * 3u + 2u] * static_cast(Side); const float reference = std::clamp(Input[tid * 3u + 0u], 0.0f, 1.0f); - if (sampler.filter == FilterPoint) return ReferenceTexel(static_cast(std::floor(u)), static_cast(std::floor(v)), reference, sampler); + const int offsetX = offsets ? OffsetComponent(tid, 0u) : 0; + const int offsetY = offsets ? OffsetComponent(tid, 1u) : 0; + if (sampler.filter == FilterPoint) return ReferenceTexel(static_cast(std::floor(u)) + offsetX, static_cast(std::floor(v)) + offsetY, reference, sampler); const float cu = u - 0.5f; const float cv = v - 0.5f; - const int x = static_cast(std::floor(cu)); - const int y = static_cast(std::floor(cv)); + const int x = static_cast(std::floor(cu)) + offsetX; + const int y = static_cast(std::floor(cv)) + offsetY; const float a = cu - std::floor(cu); const float b = cv - std::floor(cv); const float top = ReferenceTexel(x, y, reference, sampler) * (1.0f - a) + ReferenceTexel(x + 1, y, reference, sampler) * a; @@ -143,8 +166,8 @@ float Expected(std::uint32_t tid, const Sampler& sampler) { return top * (1.0f - b) + bottom * b; } -ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std::uint32_t format, const Sampler& sampler) { - std::vector userData(20, 0u); +ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std::uint32_t format, const Sampler& sampler, std::span code = Code) { + std::vector userData(24, 0u); const auto input = BufferDescriptor(Input.data(), static_cast(sizeof(Input))); const auto output = BufferDescriptor(Output.data(), static_cast(sizeof(Output))); const auto texture = TextureDescriptor(format); @@ -153,7 +176,8 @@ ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std:: std::copy(output.begin(), output.end(), userData.begin() + 4); std::copy(texture.begin(), texture.end(), userData.begin() + 8); std::copy(samplerWords.begin(), samplerWords.end(), userData.begin() + 16); - const std::span code(Code); + const auto extra = BufferDescriptor(Extra.data(), static_cast(sizeof(Extra))); + std::copy(extra.begin(), extra.end(), userData.begin() + 20); 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{ @@ -166,13 +190,14 @@ ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std:: return ShaderRecompiler::Recompile(request); } -void Run(AgcDriver::VulkanDevice& device, const Sampler& sampler, const char* name) { +void Run(AgcDriver::VulkanDevice& device, const Sampler& sampler, const char* name, bool offsets = false) { Output.fill(-1.0f); - const auto result = Compile(device, Format8888UNorm, sampler); - device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(Code.data())); + const std::span code = offsets ? std::span(OffsetCode) : std::span(Code); + const auto result = Compile(device, Format8888UNorm, sampler, code); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); device.WaitIdle(); for (std::uint32_t tid = 0; tid < Threads; ++tid) { - const float expected = Expected(tid, sampler); + const float expected = Expected(tid, sampler, offsets); const float tolerance = sampler.filter == FilterPoint ? 0.0f : 1e-4f; Require(std::fabs(Output[tid] - expected) <= tolerance, std::string(name) + ": thread " + std::to_string(tid) + " compared to " + std::to_string(Output[tid]) + ", expected " + std::to_string(expected)); } @@ -211,6 +236,10 @@ int main() { Run(*device, {ClampBorder, FilterPoint, BorderBlack}, "point, black border"); Run(*device, {ClampBorder, FilterBilinear, BorderWhite}, "bilinear, white border"); Run(*device, {ClampBorder, FilterBilinear, BorderBlack}, "bilinear, black border"); + Run(*device, {ClampEdge, FilterPoint, BorderBlack, 0u, 0x0140u}, "point, clamp to edge, sampler LOD bias"); + Run(*device, {ClampEdge, FilterPoint}, "point, clamp to edge, offsets, bias and LOD clamp", true); + Run(*device, {ClampWrap, FilterBilinear}, "bilinear, wrap, offsets, bias and LOD clamp", true); + Run(*device, {ClampBorder, FilterBilinear, BorderWhite, 0u, 0x3f00u}, "bilinear, white border, offsets, bias and LOD clamp", true); Reject(*device, Format8888UInt, {ClampEdge, FilterPoint}, "unsupported format"); Reject(*device, Format8888UNorm, {ClampMirror, FilterPoint}, "wrap, clamp-to-edge or clamp-to-border"); Reject(*device, Format8888UNorm, {ClampHalfBorder, FilterPoint}, "wrap, clamp-to-edge or clamp-to-border"); diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index 2c46cb0a7..f0d7987b0 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -540,7 +540,6 @@ std::uint32_t emulatedCompareState(const IrResourcePlan& plan, const ResourceSna const auto function = (words[0] >> 12u) & 0x7u; const bool unnormalized = ((words[0] >> 15u) & 0x1u) != 0u; if (((words[0] >> 29u) & 0x3u) != 0u) throw std::runtime_error("comparison sampling of a color texture through a min or max reduction sampler is not implemented"); - const auto lodBias = words[2] & 0x3fffu; const auto magFilter = (words[2] >> 20u) & 0x3u; const auto minFilter = (words[2] >> 22u) & 0x3u; const auto addressMode = [](std::uint32_t clamp) { @@ -555,7 +554,7 @@ std::uint32_t emulatedCompareState(const IrResourcePlan& plan, const ResourceSna const bool border = addressX == EmulatedCompare::AddressBorder || addressY == EmulatedCompare::AddressBorder; if (border && borderType == 3u) throw std::runtime_error("comparison sampling of a color texture with a border color table is not implemented"); if (magFilter != minFilter || magFilter > 1u) throw std::runtime_error("comparison sampling of a color texture is implemented only with equal point or bilinear minification and magnification filters"); - if (unnormalized || lodBias != 0u) throw std::runtime_error("comparison sampling of a color texture does not implement unnormalized coordinates or an LOD bias"); + if (unnormalized) throw std::runtime_error("comparison sampling of a color texture does not implement unnormalized coordinates"); const auto state = EmulatedCompare::Enabled | (function << EmulatedCompare::FunctionShift) | (magFilter == 1u ? EmulatedCompare::Linear : 0u) | (addressX << EmulatedCompare::ClampXShift) | (addressY << EmulatedCompare::ClampYShift) | (border && borderType == 2u ? EmulatedCompare::BorderWhite : 0u); if (samplerState.has_value() && *samplerState != state) throw std::runtime_error("comparison sampling of a color texture through samplers that disagree is not implemented"); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp index eb77daa6d..33a933301 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp @@ -970,7 +970,6 @@ void EmitEmulatedCompareSample(SpirvValueEmitContext& ctx, const ImageEmitAccess const auto compare = image.emulatedCompare; if (access.slot != 0) ctx.Fail(access.inst, "compares against a color texture through a bindless image table, which is not implemented"); if (HasFlag(mem, RdnaImageSampleFlagDerivative) || HasFlag(mem, RdnaImageSampleFlagLod)) ctx.Fail(access.inst, "compares against a color texture with gradients or an explicit LOD, which is not implemented"); - if (setup.layout.bias != NoImageComponent || setup.layout.offset != NoImageComponent || setup.layout.clamp != NoImageComponent) ctx.Fail(access.inst, "compares against a color texture with an LOD bias, texel offset or LOD clamp, which is not implemented"); if (!HasFlag(mem, RdnaImageSampleFlagLevelZero) && (compare & EmulatedCompare::SingleLevel) == 0u) ctx.Fail(access.inst, "compares against a color texture across mip levels, which is not implemented"); const bool arrayed = image.dimension == RdnaImageDimension::Dim2DArray; if (image.dimension != RdnaImageDimension::Dim2D && !arrayed) ctx.Fail(access.inst, "compares against a color texture that is not a 2D or 2D array view, which is not implemented"); @@ -1048,9 +1047,19 @@ void EmitEmulatedCompareSample(SpirvValueEmitContext& ctx, const ImageEmitAccess }; const auto scaledU = Binary(state, spv::OpFMul, f32, extract(f32, setup.coord, 0u), Unary(state, spv::OpConvertSToF, f32, width)); const auto scaledV = Binary(state, spv::OpFMul, f32, extract(f32, setup.coord, 1u), Unary(state, spv::OpConvertSToF, f32, height)); + auto offsetX = ConstantI32(state, 0); + auto offsetY = ConstantI32(state, 0); + if (setup.layout.offset != NoImageComponent) { + const auto offset = PackedOffset(ctx, access, setup.layout); + offsetX = extract(i32, offset, 0u); + offsetY = extract(i32, offset, 1u); + } + const auto texelIndex = [&](std::uint32_t coordinate, std::uint32_t offset) { + return Binary(state, spv::OpIAdd, i32, Unary(state, spv::OpConvertFToS, i32, ext(f32, GLSLstd450Floor, {coordinate})), offset); + }; std::uint32_t result; if ((compare & EmulatedCompare::Linear) == 0u) { - result = compareTexel(Unary(state, spv::OpConvertFToS, i32, ext(f32, GLSLstd450Floor, {scaledU})), Unary(state, spv::OpConvertFToS, i32, ext(f32, GLSLstd450Floor, {scaledV}))); + result = compareTexel(texelIndex(scaledU, offsetX), texelIndex(scaledV, offsetY)); } else { const auto half = f32Constant(0.5f); const auto centreU = Binary(state, spv::OpFSub, f32, scaledU, half); @@ -1059,8 +1068,8 @@ void EmitEmulatedCompareSample(SpirvValueEmitContext& ctx, const ImageEmitAccess const auto floorV = ext(f32, GLSLstd450Floor, {centreV}); const auto weightU = Binary(state, spv::OpFSub, f32, centreU, floorU); const auto weightV = Binary(state, spv::OpFSub, f32, centreV, floorV); - const auto x0 = Unary(state, spv::OpConvertFToS, i32, floorU); - const auto y0 = Unary(state, spv::OpConvertFToS, i32, floorV); + const auto x0 = Binary(state, spv::OpIAdd, i32, Unary(state, spv::OpConvertFToS, i32, floorU), offsetX); + const auto y0 = Binary(state, spv::OpIAdd, i32, Unary(state, spv::OpConvertFToS, i32, floorV), offsetY); const auto x1 = Binary(state, spv::OpIAdd, i32, x0, ConstantI32(state, 1)); const auto y1 = Binary(state, spv::OpIAdd, i32, y0, ConstantI32(state, 1)); const auto top = ext(f32, GLSLstd450FMix, {compareTexel(x0, y0), compareTexel(x1, y0), weightU}); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 6dc3ce18d..f5b3032d5 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -220,7 +220,7 @@ Throughout the project, every function at every stage either **does exactly what - [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture. - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36. - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it. -- [Comparison sampling of a color texture](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_sample_c`/`image_sample_c_lz` on a texture that is not R32 float or R16 unorm) is emulated in the shader, since Vulkan compares only depth formats: the red channel is fetched, compared and, for bilinear filtering, blended (compare, then filter). Only 2D and 2D array views, float/unorm/snorm formats, wrap, clamp-to-edge and clamp-to-border addressing (border red 0 for the black borders, 1 for opaque white) and point or bilinear filtering at one level are implemented; half-border addressing, the border color table, gradients, LOD bias, offsets, LOD clamps, gathers, other dimensions, anisotropy and mip filtering throw. Under bilinear filtering, taps past a clamp-to-border edge compare the border value and blend with the texel taps, so results near the edge depend on the filter weights. AMD documentation does not say how a color format's reference is handled: it is clamped to [0, 1] for unorm and [-1, 1] for snorm, as Vulkan does for unorm depth, and not clamped for float. Bilinear weights are full float, not the hardware's fixed-point subtexel weights +- [Comparison sampling of a color texture](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_sample_c`/`image_sample_c_lz` on a texture that is not R32 float or R16 unorm) is emulated in the shader, since Vulkan compares only depth formats: the red channel is fetched, compared and, for bilinear filtering, blended (compare, then filter). Only 2D and 2D array views, float/unorm/snorm formats, wrap, clamp-to-edge and clamp-to-border addressing (border red 0 for the black borders, 1 for opaque white) and point or bilinear filtering at one level are implemented, with texel offsets added to the texel indices; LOD biases and clamps are accepted, as they cannot select another level of a single-level sample. Half-border addressing, the border color table, gradients, gathers, other dimensions, anisotropy and mip filtering throw. Under bilinear filtering, taps past a clamp-to-border edge compare the border value and blend with the texel taps, so results near the edge depend on the filter weights. AMD documentation does not say how a color format's reference is handled: it is clamped to [0, 1] for unorm and [-1, 1] for snorm, as Vulkan does for unorm depth, and not clamped for float. Bilinear weights are full float, not the hardware's fixed-point subtexel weights - [libScePlayerInvitationDialog](../../core/libs/prx/libScePlayerInvitationDialog/libScePlayerInvitationDialog.cpp) simulates dialog completion without displaying UI or sending invitations, so `scePlayerInvitationDialogGetResult` reports `USER_CANCELED`; its parameter ABI remains unverified. The 40-byte result layout (error code, result, 32 reserved bytes) is taken from [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Dialog/PlayerInvitationDialog.cs) and the error codes from the other common dialogs, not confirmed on a title. - [Converted unorm images](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (10_11_11_UNORM, read and written through an R32_UINT view): samples, gathers, LOD queries, 16-bit data and a DST_SEL of the missing W channel throw. - [Converted unorm image loads](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) divide by 2^n - 1 with the buffer format path's `OpFDiv`, which Vulkan allows to be up to 2.5 ULP off, so some drivers are not bit-exact.