mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #668 from ThisIsAkill/fix/agc-emulated-color-compare-main
fix(agc): emulate comparison sampling of color textures in the shader
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp"
|
||||
#include "Recompiler.hpp"
|
||||
#include "VulkanTestDevice.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using AgcDriver::Graphics::Require;
|
||||
using ShaderRecompiler::ShaderStage;
|
||||
|
||||
constexpr std::uint32_t Threads = 32;
|
||||
constexpr std::uint32_t Side = 2;
|
||||
constexpr std::uint32_t Format8888UNorm = 56;
|
||||
constexpr std::uint32_t Format8888UInt = 60;
|
||||
constexpr std::uint32_t Format32Float = 22;
|
||||
constexpr std::uint32_t Type2D = 9;
|
||||
constexpr std::uint32_t LessEqual = 3;
|
||||
constexpr std::uint32_t ClampWrap = 0;
|
||||
constexpr std::uint32_t ClampMirror = 1;
|
||||
constexpr std::uint32_t ClampEdge = 2;
|
||||
constexpr std::uint32_t ClampHalfBorder = 4;
|
||||
constexpr std::uint32_t ClampBorder = 6;
|
||||
constexpr std::uint32_t BorderBlack = 0;
|
||||
constexpr std::uint32_t BorderWhite = 2;
|
||||
constexpr std::uint32_t BorderTable = 3;
|
||||
constexpr std::uint32_t FilterPoint = 0;
|
||||
constexpr std::uint32_t FilterBilinear = 1;
|
||||
constexpr std::uint32_t FilterAnisoBilinear = 3;
|
||||
constexpr std::array<std::uint8_t, 4> Red{0, 64, 128, 255};
|
||||
|
||||
alignas(256) std::array<float, Threads * 3> Input{};
|
||||
alignas(256) std::array<float, Threads> Output{};
|
||||
alignas(4096) std::array<std::uint8_t, 4096> Texels{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 13> Code{
|
||||
0x1614008c, 0xe03c1000, 0x8000010a, 0xbf8c3f70, 0xf0bc0108, 0x00820401, 0xbf8c3f70,
|
||||
0x34160082, 0xe0701000, 0x8001040b, 0xbf810000, 0xbf810000, 0xbf810000,
|
||||
};
|
||||
|
||||
struct Sampler {
|
||||
std::uint32_t clamp;
|
||||
std::uint32_t filter;
|
||||
std::uint32_t border = BorderBlack;
|
||||
};
|
||||
|
||||
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(data);
|
||||
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), bytes, 0x01016facu};
|
||||
}
|
||||
|
||||
std::array<std::uint32_t, 8> TextureDescriptor(std::uint32_t format) {
|
||||
const auto address = static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(Texels.data()));
|
||||
return {
|
||||
static_cast<std::uint32_t>(address >> 8u),
|
||||
static_cast<std::uint32_t>((address >> 40u) & 0xffu) | (format << 20u) | (((Side - 1u) & 3u) << 30u),
|
||||
((Side - 1u) >> 2u) | ((Side - 1u) << 14u),
|
||||
0xfacu | (Type2D << 28u),
|
||||
0u, 0u, 0u, 0u,
|
||||
};
|
||||
}
|
||||
|
||||
std::array<std::uint32_t, 4> SamplerDescriptor(const Sampler& sampler) {
|
||||
return {
|
||||
sampler.clamp | (sampler.clamp << 3u) | (ClampEdge << 6u) | (LessEqual << 12u),
|
||||
0u,
|
||||
(sampler.filter << 20u) | (sampler.filter << 22u),
|
||||
sampler.border << 30u,
|
||||
};
|
||||
}
|
||||
|
||||
void FillTexels() {
|
||||
Texels.fill(0);
|
||||
const auto descriptor = TextureDescriptor(Format8888UNorm);
|
||||
const auto surface = AgcDriver::Graphics::DescribeSurface(AgcDriver::Graphics::DecodeTextureResource(descriptor));
|
||||
const auto& mip = surface.mips.at(0);
|
||||
for (std::uint32_t y = 0; y < Side; ++y) {
|
||||
for (std::uint32_t x = 0; x < Side; ++x) {
|
||||
auto* texel = Texels.data() + mip.tiledOffset + y * mip.pitchBytes + x * 4u;
|
||||
texel[0] = Red[y * Side + x];
|
||||
texel[1] = 0x11;
|
||||
texel[2] = 0x22;
|
||||
texel[3] = 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FillInput() {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
|
||||
Input[tid * 3u + 0u] = -0.6f + 0.07f * static_cast<float>(tid);
|
||||
Input[tid * 3u + 1u] = 1.55f - 0.065f * static_cast<float>(tid);
|
||||
Input[tid * 3u + 2u] = -0.25f + 0.05f * static_cast<float>((tid * 7u) % 32u);
|
||||
}
|
||||
Input[0] = 0.0f; Input[1] = 0.0f; Input[2] = 0.0f;
|
||||
Input[3] = 0.999f; Input[4] = 0.999f; Input[5] = 1.2f;
|
||||
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;
|
||||
}
|
||||
|
||||
float ReferenceTexel(int x, int y, float reference, const Sampler& sampler) {
|
||||
const std::uint32_t clamp = sampler.clamp;
|
||||
const int size = static_cast<int>(Side);
|
||||
if (clamp == ClampBorder && (x < 0 || x >= size || y < 0 || y >= size)) {
|
||||
const float border = sampler.border == BorderWhite ? 1.0f : 0.0f;
|
||||
return reference <= border ? 1.0f : 0.0f;
|
||||
}
|
||||
const auto address = [&](int value) {
|
||||
if (clamp == ClampEdge) return std::clamp(value, 0, static_cast<int>(Side) - 1);
|
||||
const int size = static_cast<int>(Side);
|
||||
return ((value % size) + size) % size;
|
||||
};
|
||||
const float red = static_cast<float>(Red[address(y) * Side + address(x)]) / 255.0f;
|
||||
return reference <= red ? 1.0f : 0.0f;
|
||||
}
|
||||
|
||||
float Expected(std::uint32_t tid, const Sampler& sampler) {
|
||||
const float u = Input[tid * 3u + 1u] * static_cast<float>(Side);
|
||||
const float v = Input[tid * 3u + 2u] * static_cast<float>(Side);
|
||||
const float reference = std::clamp(Input[tid * 3u + 0u], 0.0f, 1.0f);
|
||||
if (sampler.filter == FilterPoint) return ReferenceTexel(static_cast<int>(std::floor(u)), static_cast<int>(std::floor(v)), reference, sampler);
|
||||
const float cu = u - 0.5f;
|
||||
const float cv = v - 0.5f;
|
||||
const int x = static_cast<int>(std::floor(cu));
|
||||
const int y = static_cast<int>(std::floor(cv));
|
||||
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;
|
||||
const float bottom = ReferenceTexel(x, y + 1, reference, sampler) * (1.0f - a) + ReferenceTexel(x + 1, y + 1, reference, sampler) * a;
|
||||
return top * (1.0f - b) + bottom * b;
|
||||
}
|
||||
|
||||
ShaderRecompiler::RecompileResult Compile(AgcDriver::VulkanDevice& device, std::uint32_t format, const Sampler& sampler) {
|
||||
std::vector<std::uint32_t> userData(20, 0u);
|
||||
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(sizeof(Input)));
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(sizeof(Output)));
|
||||
const auto texture = TextureDescriptor(format);
|
||||
const auto samplerWords = SamplerDescriptor(sampler);
|
||||
std::copy(input.begin(), input.end(), userData.begin());
|
||||
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<const std::uint32_t> code(Code);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(code.data()), code, 0, {}},
|
||||
{32, 0, userData, compute, std::nullopt, std::nullopt, memory},
|
||||
device.Target(),
|
||||
{0, 0, 0, 128}
|
||||
};
|
||||
request.useCache = false;
|
||||
return ShaderRecompiler::Recompile(request);
|
||||
}
|
||||
|
||||
void Run(AgcDriver::VulkanDevice& device, const Sampler& sampler, const char* name) {
|
||||
Output.fill(-1.0f);
|
||||
const auto result = Compile(device, Format8888UNorm, sampler);
|
||||
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(Code.data()));
|
||||
device.WaitIdle();
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
|
||||
const float expected = Expected(tid, sampler);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
bool BindsDepthCompare(const ShaderRecompiler::RecompileResult& result) {
|
||||
return std::any_of(result.bindings.begin(), result.bindings.end(), [](const ShaderRecompiler::DescriptorBinding& binding) {
|
||||
return std::any_of(binding.imageDepthCompare.begin(), binding.imageDepthCompare.end(), [](bool compare) { return compare; });
|
||||
});
|
||||
}
|
||||
|
||||
void Reject(AgcDriver::VulkanDevice& device, std::uint32_t format, const Sampler& sampler, std::string_view reason) {
|
||||
try {
|
||||
static_cast<void>(Compile(device, format, sampler));
|
||||
} catch (const std::exception& error) {
|
||||
Require(std::string_view(error.what()).find(reason) != std::string_view::npos, std::string("unexpected rejection: ") + error.what());
|
||||
return;
|
||||
}
|
||||
Require(false, std::string("expected rejection: ") + std::string(reason));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
FillTexels();
|
||||
FillInput();
|
||||
Require(BindsDepthCompare(Compile(*device, Format32Float, {ClampEdge, FilterBilinear})), "an R32 float texture left the native comparison path");
|
||||
Require(!BindsDepthCompare(Compile(*device, Format8888UNorm, {ClampEdge, FilterBilinear})), "a color texture kept a depth-compare binding");
|
||||
Run(*device, {ClampEdge, FilterPoint}, "point, clamp to edge");
|
||||
Run(*device, {ClampEdge, FilterBilinear}, "bilinear, clamp to edge");
|
||||
Run(*device, {ClampWrap, FilterBilinear}, "bilinear, wrap");
|
||||
Run(*device, {ClampBorder, FilterPoint, BorderWhite}, "point, white border");
|
||||
Run(*device, {ClampBorder, FilterPoint, BorderBlack}, "point, black border");
|
||||
Run(*device, {ClampBorder, FilterBilinear, BorderWhite}, "bilinear, white border");
|
||||
Run(*device, {ClampBorder, FilterBilinear, BorderBlack}, "bilinear, black border");
|
||||
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");
|
||||
Reject(*device, Format8888UNorm, {ClampBorder, FilterPoint, BorderTable}, "border color table");
|
||||
Reject(*device, Format8888UNorm, {ClampEdge, FilterAnisoBilinear}, "point or bilinear");
|
||||
std::puts("emulated color compare tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+22
@@ -32,6 +32,27 @@ struct BufferResource {
|
||||
|
||||
enum class ImageMipMode { None, DynamicStorage };
|
||||
|
||||
namespace EmulatedCompare {
|
||||
inline constexpr std::uint32_t Enabled = 1u << 0u;
|
||||
inline constexpr std::uint32_t FunctionShift = 1u;
|
||||
inline constexpr std::uint32_t Linear = 1u << 4u;
|
||||
inline constexpr std::uint32_t ClampXShift = 5u;
|
||||
inline constexpr std::uint32_t ClampYShift = 7u;
|
||||
inline constexpr std::uint32_t ReferenceShift = 9u;
|
||||
inline constexpr std::uint32_t SingleLevel = 1u << 11u;
|
||||
inline constexpr std::uint32_t BorderWhite = 1u << 12u;
|
||||
inline constexpr std::uint32_t AddressWrap = 0u;
|
||||
inline constexpr std::uint32_t AddressEdge = 1u;
|
||||
inline constexpr std::uint32_t AddressBorder = 2u;
|
||||
inline constexpr std::uint32_t ReferenceFloat = 0u;
|
||||
inline constexpr std::uint32_t ReferenceUnorm = 1u;
|
||||
inline constexpr std::uint32_t ReferenceSnorm = 2u;
|
||||
[[nodiscard]] inline std::uint32_t Function(std::uint32_t state) { return (state >> FunctionShift) & 0x7u; }
|
||||
[[nodiscard]] inline std::uint32_t AddressX(std::uint32_t state) { return (state >> ClampXShift) & 0x3u; }
|
||||
[[nodiscard]] inline std::uint32_t AddressY(std::uint32_t state) { return (state >> ClampYShift) & 0x3u; }
|
||||
[[nodiscard]] inline std::uint32_t Reference(std::uint32_t state) { return (state >> ReferenceShift) & 0x3u; }
|
||||
}
|
||||
|
||||
struct ImageResource {
|
||||
static constexpr std::uint32_t NoIndirectImage = std::numeric_limits<std::uint32_t>::max();
|
||||
|
||||
@@ -54,6 +75,7 @@ struct ImageResource {
|
||||
bool depthUnorm16 = false;
|
||||
bool packed = false;
|
||||
IrBufferFormat packedFormat = IrBufferFormat::Invalid;
|
||||
std::uint32_t emulatedCompare = 0;
|
||||
std::uint32_t indirectRoot = NoIndirectImage;
|
||||
std::uint32_t indirectMappingOffset = 0;
|
||||
std::uint32_t indirectSearchIterations = 0;
|
||||
|
||||
@@ -32,6 +32,7 @@ struct ResourceSpecialization {
|
||||
bool depthBits = false;
|
||||
bool depthUnorm16 = false;
|
||||
IrBufferFormat packedFormat = IrBufferFormat::Invalid;
|
||||
std::uint32_t emulatedCompare = 0;
|
||||
|
||||
bool operator==(const Image& other) const;
|
||||
};
|
||||
|
||||
@@ -499,6 +499,69 @@ void materializeSnapshot(const IrResourcePlan& plan, const SrtRuntime& runtime,
|
||||
snapshot.samplers.assign(values.begin() + cursor, values.begin() + cursor + plan.info.samplers.size());
|
||||
}
|
||||
|
||||
std::uint32_t colorCompareReference(IrBufferFormat format) {
|
||||
switch (format) {
|
||||
case IrBufferFormat::Format8UNorm: case IrBufferFormat::Format8_8UNorm: case IrBufferFormat::Format16_16UNorm:
|
||||
case IrBufferFormat::Format11_11_10UNorm: case IrBufferFormat::Format10_11_11UNorm: case IrBufferFormat::Format2_10_10_10UNorm:
|
||||
case IrBufferFormat::Format10_10_10_2UNorm: case IrBufferFormat::Format8_8_8_8UNorm: case IrBufferFormat::Format16_16_16_16UNorm:
|
||||
return EmulatedCompare::ReferenceUnorm;
|
||||
case IrBufferFormat::Format8SNorm: case IrBufferFormat::Format16SNorm: case IrBufferFormat::Format8_8SNorm: case IrBufferFormat::Format16_16SNorm:
|
||||
case IrBufferFormat::Format11_11_10SNorm: case IrBufferFormat::Format10_11_11SNorm: case IrBufferFormat::Format2_10_10_10SNorm:
|
||||
case IrBufferFormat::Format10_10_10_2SNorm: case IrBufferFormat::Format8_8_8_8SNorm: case IrBufferFormat::Format16_16_16_16SNorm:
|
||||
return EmulatedCompare::ReferenceSnorm;
|
||||
case IrBufferFormat::Format16Float: case IrBufferFormat::Format16_16Float: case IrBufferFormat::Format11_11_10Float:
|
||||
case IrBufferFormat::Format10_11_11Float: case IrBufferFormat::Format32_32Float: case IrBufferFormat::Format16_16_16_16Float:
|
||||
return EmulatedCompare::ReferenceFloat;
|
||||
default:
|
||||
throw std::runtime_error("comparison sampling of a color texture is implemented only for float, unorm and snorm formats (format " + std::to_string(static_cast<std::uint32_t>(format)) + ")");
|
||||
}
|
||||
}
|
||||
|
||||
std::uint32_t emulatedCompareState(const IrResourcePlan& plan, const ResourceSnapshot& snapshot, std::uint32_t index) {
|
||||
const auto& image = plan.info.images[index];
|
||||
const auto& descriptor = snapshot.images[index];
|
||||
if (!image.depthCompare || descriptor.dwordCount != 8u || nullImageDescriptor(descriptor)) return 0u;
|
||||
const auto format = rawImageFormat(descriptor);
|
||||
if (format == IrBufferFormat::Format32Float || format == IrBufferFormat::Format16UNorm || IsDepthBitsTexture(descriptor.dwords[1], descriptor.dwords[3])) return 0u;
|
||||
const auto reference = colorCompareReference(format);
|
||||
const auto type = rawImageType(descriptor);
|
||||
if (type != ImageType::Color2D && type != ImageType::Color2DArray) throw std::runtime_error("comparison sampling of a color texture is implemented only for 2D and 2D array views");
|
||||
if ((descriptorImageSwizzle(descriptor) & 0x7u) != 4u) throw std::runtime_error("comparison sampling of a color texture is implemented only when the view's X channel is red");
|
||||
std::optional<std::uint32_t> samplerState;
|
||||
for (const auto& pair : plan.info.sampledPairs) {
|
||||
if (pair.image != index) continue;
|
||||
if (pair.sampler >= snapshot.samplers.size() || snapshot.samplers[pair.sampler].dwordCount != 4u) throw std::runtime_error("comparison sampling of a color texture has no sampler descriptor");
|
||||
const auto& words = snapshot.samplers[pair.sampler].dwords;
|
||||
const auto clampX = words[0] & 0x7u;
|
||||
const auto clampY = (words[0] >> 3u) & 0x7u;
|
||||
const auto function = (words[0] >> 12u) & 0x7u;
|
||||
const bool unnormalized = ((words[0] >> 15u) & 0x1u) != 0u;
|
||||
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) {
|
||||
if (clamp == 0u) return EmulatedCompare::AddressWrap;
|
||||
if (clamp == 2u) return EmulatedCompare::AddressEdge;
|
||||
if (clamp == 6u) return EmulatedCompare::AddressBorder;
|
||||
throw std::runtime_error("comparison sampling of a color texture is implemented only with wrap, clamp-to-edge or clamp-to-border addressing");
|
||||
};
|
||||
const auto addressX = addressMode(clampX);
|
||||
const auto addressY = addressMode(clampY);
|
||||
const auto borderType = (words[3] >> 30u) & 0x3u;
|
||||
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");
|
||||
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");
|
||||
samplerState = state;
|
||||
}
|
||||
if (!samplerState.has_value()) throw std::runtime_error("comparison sampling of a color texture has no paired sampler");
|
||||
const bool singleLevel = ((descriptor.dwords[3] >> 12u) & 0xfu) == ((descriptor.dwords[3] >> 16u) & 0xfu);
|
||||
return *samplerState | (reference << EmulatedCompare::ReferenceShift) | (singleLevel ? EmulatedCompare::SingleLevel : 0u);
|
||||
}
|
||||
|
||||
void buildResourceSpecialization(const IrResourcePlan& plan, ResourceSnapshot& snapshot, const std::vector<TableResolution>& tables, ResourceSpecialization& specialization) {
|
||||
ResourceSpecialization result;
|
||||
result.buffers.reserve(plan.info.buffers.size());
|
||||
@@ -545,6 +608,7 @@ void buildResourceSpecialization(const IrResourcePlan& plan, ResourceSnapshot& s
|
||||
entry.depthBits = decoded.depthBits;
|
||||
entry.depthUnorm16 = decoded.depthUnorm16;
|
||||
entry.packedFormat = decoded.packedFormat;
|
||||
entry.emulatedCompare = emulatedCompareState(plan, snapshot, i);
|
||||
result.images.push_back(entry);
|
||||
}
|
||||
|
||||
@@ -632,6 +696,8 @@ void ResourceMaterializer::Apply(IrProgram& program, const ResourceSpecializatio
|
||||
image.depthBits = source.depthBits;
|
||||
image.depthUnorm16 = source.depthUnorm16;
|
||||
image.packedFormat = source.packedFormat;
|
||||
image.emulatedCompare = source.emulatedCompare;
|
||||
if ((source.emulatedCompare & EmulatedCompare::Enabled) != 0u) image.depthCompare = false;
|
||||
image.indirectResources.clear();
|
||||
}
|
||||
for (std::uint32_t index = 0; index < images.size(); index++) {
|
||||
@@ -960,7 +1026,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 && packedFormat == other.packedFormat;
|
||||
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 && emulatedCompare == other.emulatedCompare;
|
||||
}
|
||||
|
||||
bool ResourceSpecialization::operator==(const ResourceSpecialization& other) const {
|
||||
|
||||
@@ -70,7 +70,7 @@ static_assert(sizeof(FragmentParameter) == 12, "FragmentParameter changed: updat
|
||||
static_assert(sizeof(CompiledShaderInfo) == 304, "CompiledShaderInfo changed: update the info encoder");
|
||||
static_assert(sizeof(ShaderInfo) == 200, "ShaderInfo changed: update the info encoder");
|
||||
static_assert(sizeof(BufferResource) == 36, "BufferResource changed: update the info encoder");
|
||||
static_assert(sizeof(ImageResource) == 88, "ImageResource changed: update the info encoder");
|
||||
static_assert(sizeof(ImageResource) == 96, "ImageResource changed: update the info encoder");
|
||||
static_assert(sizeof(SamplerResource) == 12, "SamplerResource changed: update the info encoder");
|
||||
static_assert(sizeof(SampledResourcePair) == 12, "SampledResourcePair changed: update the info encoder");
|
||||
static_assert(sizeof(StageInput) == 56, "StageInput changed: update the info encoder");
|
||||
@@ -80,7 +80,7 @@ static_assert(sizeof(IrDescriptorBinding) == 32, "IrDescriptorBinding changed: u
|
||||
static_assert(sizeof(BindingAllocationResult) == 120, "BindingAllocationResult changed: update the allocation encoder");
|
||||
static_assert(sizeof(ResourceSpecialization) == 72, "ResourceSpecialization changed: update BuildKey");
|
||||
static_assert(sizeof(ResourceSpecialization::Buffer) == 16, "ResourceSpecialization::Buffer changed: update BuildKey");
|
||||
static_assert(sizeof(ResourceSpecialization::Image) == 40, "ResourceSpecialization::Image changed: update BuildKey");
|
||||
static_assert(sizeof(ResourceSpecialization::Image) == 44, "ResourceSpecialization::Image changed: update BuildKey");
|
||||
static_assert(sizeof(BindingLayout) == 16, "BindingLayout changed: update BuildKey");
|
||||
#endif
|
||||
|
||||
@@ -408,6 +408,7 @@ void encodeInfo(Writer& writer, const CompiledShaderInfo& compiled) {
|
||||
out.Value(image.depthUnorm16);
|
||||
out.Value(image.packed);
|
||||
out.Value(image.packedFormat);
|
||||
out.Value(image.emulatedCompare);
|
||||
out.Value(image.indirectRoot);
|
||||
out.Value(image.indirectMappingOffset);
|
||||
out.Value(image.indirectSearchIterations);
|
||||
@@ -497,6 +498,7 @@ void decodeInfo(Reader& reader, CompiledShaderInfo& compiled) {
|
||||
in.Value(image.depthUnorm16);
|
||||
in.Value(image.packed);
|
||||
in.Value(image.packedFormat);
|
||||
in.Value(image.emulatedCompare);
|
||||
in.Value(image.indirectRoot);
|
||||
in.Value(image.indirectMappingOffset);
|
||||
in.Value(image.indirectSearchIterations);
|
||||
@@ -812,6 +814,7 @@ void BuildKey(const RecompileRequest& request, std::uint32_t hostSubgroupSize, c
|
||||
out.Value(image.depthBits);
|
||||
out.Value(image.depthUnorm16);
|
||||
out.Value(image.packedFormat);
|
||||
out.Value(image.emulatedCompare);
|
||||
});
|
||||
writer.Values(std::span<const std::uint32_t>(specialization.boundDescriptors));
|
||||
const auto& switches = switchKey();
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "SpirvBackend/SpirvEmitterInstructions.hpp"
|
||||
#include "RdnaDecoder/RdnaDescriptorFormat.hpp"
|
||||
#include "RdnaDecoder/RdnaImageOpDecoder.hpp"
|
||||
#include <span>
|
||||
#include <initializer_list>
|
||||
#include <algorithm>
|
||||
#include <spirv/unified1/GLSL.std.450.h>
|
||||
#include <spirv/unified1/spirv.hpp>
|
||||
@@ -804,6 +806,9 @@ void EmitGatherOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, con
|
||||
if (setup.layout.clamp != NoImageComponent) {
|
||||
ctx.Fail(access.inst, "is a gather with an LOD clamp, which is not implemented");
|
||||
}
|
||||
if (setup.dref && (access.image.emulatedCompare & EmulatedCompare::Enabled) != 0u) {
|
||||
ctx.Fail(access.inst, "is a comparison gather of a color texture, which is not implemented");
|
||||
}
|
||||
if (dimension == RdnaImageDimension::Dim1D) {
|
||||
if (setup.dref || !HasFlag(mem, RdnaImageSampleFlagLevelZero) || HasFlag(mem, RdnaImageSampleFlagOffset) || HasFlag(mem, RdnaImageSampleFlagGatherHorizontal)) {
|
||||
ctx.Fail(access.inst, "has an unsupported 1D gather variant");
|
||||
@@ -892,10 +897,121 @@ TableSelection EmitIndirectImageSelector(SpirvValueEmitContext& ctx, const Image
|
||||
return {selected, mapped};
|
||||
}
|
||||
|
||||
void EmitEmulatedCompareSample(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, const SampleSetup& setup) {
|
||||
auto& state = ctx.state;
|
||||
const auto& mem = access.mem;
|
||||
const auto& image = access.image;
|
||||
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");
|
||||
const auto f32 = TypeF32(state);
|
||||
const auto i32 = TypeI32(state);
|
||||
const auto u32 = TypeU32(state);
|
||||
const auto ext = [&](std::uint32_t type, std::uint32_t op, std::initializer_list<std::uint32_t> args) {
|
||||
const auto value = state.module.AllocateId();
|
||||
std::vector<std::uint32_t> words{spv::OpExtInst, type, value, GlslStd450(state), op};
|
||||
words.insert(words.end(), args.begin(), args.end());
|
||||
state.module.AddFunction(std::span<const std::uint32_t>(words));
|
||||
return value;
|
||||
};
|
||||
const auto extract = [&](std::uint32_t type, std::uint32_t composite, std::uint32_t index) {
|
||||
const auto value = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpCompositeExtract, type, value, composite, index);
|
||||
return value;
|
||||
};
|
||||
const auto f32Constant = [&](float value) { return ConstantF32(state, std::bit_cast<std::uint32_t>(value)); };
|
||||
const auto descriptor = LoadSampledImageDescriptor(state, mem.resource, access.slot);
|
||||
state.module.EmitCapability(spv::CapabilityImageQuery);
|
||||
const auto size = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpImageQuerySizeLod, ImageViewSizeType(state, image.dimension), size, descriptor, ConstantU32(state, 0u));
|
||||
const auto width = Unary(state, spv::OpBitcast, i32, extract(u32, size, 0u));
|
||||
const auto height = Unary(state, spv::OpBitcast, i32, extract(u32, size, 1u));
|
||||
auto layer = ConstantU32(state, 0u);
|
||||
if (arrayed) {
|
||||
const auto layers = Unary(state, spv::OpBitcast, i32, extract(u32, size, 2u));
|
||||
const auto rounded = Unary(state, spv::OpConvertFToS, i32, ext(f32, GLSLstd450Floor, {Binary(state, spv::OpFAdd, f32, extract(f32, setup.coord, 2u), f32Constant(0.5f))}));
|
||||
layer = Unary(state, spv::OpBitcast, u32, ext(i32, GLSLstd450SClamp, {rounded, ConstantI32(state, 0), Binary(state, spv::OpISub, i32, layers, ConstantI32(state, 1))}));
|
||||
}
|
||||
auto reference = DrefValueF32(ctx, access, setup.layout);
|
||||
if (EmulatedCompare::Reference(compare) == EmulatedCompare::ReferenceUnorm) reference = ext(f32, GLSLstd450FClamp, {reference, f32Constant(0.0f), f32Constant(1.0f)});
|
||||
if (EmulatedCompare::Reference(compare) == EmulatedCompare::ReferenceSnorm) reference = ext(f32, GLSLstd450FClamp, {reference, f32Constant(-1.0f), f32Constant(1.0f)});
|
||||
const auto address = [&](std::uint32_t index, std::uint32_t extent, std::uint32_t mode) {
|
||||
if (mode == EmulatedCompare::AddressWrap) return Binary(state, spv::OpSMod, i32, index, extent);
|
||||
return ext(i32, GLSLstd450SClamp, {index, ConstantI32(state, 0), Binary(state, spv::OpISub, i32, extent, ConstantI32(state, 1))});
|
||||
};
|
||||
const auto inside = [&](std::uint32_t index, std::uint32_t extent) {
|
||||
const auto notBelow = Binary(state, spv::OpSGreaterThanEqual, TypeBool(state), index, ConstantI32(state, 0));
|
||||
const auto below = Binary(state, spv::OpSLessThan, TypeBool(state), index, extent);
|
||||
return Binary(state, spv::OpLogicalAnd, TypeBool(state), notBelow, below);
|
||||
};
|
||||
const auto one = f32Constant(1.0f);
|
||||
const auto zero = f32Constant(0.0f);
|
||||
const auto borderRed = (compare & EmulatedCompare::BorderWhite) != 0u ? one : zero;
|
||||
const auto compareTexel = [&](std::uint32_t x, std::uint32_t y) {
|
||||
const auto modeX = EmulatedCompare::AddressX(compare);
|
||||
const auto modeY = EmulatedCompare::AddressY(compare);
|
||||
const auto ux = Unary(state, spv::OpBitcast, u32, address(x, width, modeX));
|
||||
const auto uy = Unary(state, spv::OpBitcast, u32, address(y, height, modeY));
|
||||
const auto coord = state.module.AllocateId();
|
||||
if (arrayed) state.module.AddFunction(spv::OpCompositeConstruct, TypeU32Vector(state, 3), coord, ux, uy, layer);
|
||||
else state.module.AddFunction(spv::OpCompositeConstruct, TypeU32Vector(state, 2), coord, ux, uy);
|
||||
const auto texel = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpImageFetch, ImageVectorType(state, IrTextureNumericClass::Float, 4), texel, descriptor, coord, spv::ImageOperandsLodMask, ConstantU32(state, 0u));
|
||||
auto red = extract(f32, texel, 0u);
|
||||
std::uint32_t inBorder = 0u;
|
||||
if (modeX == EmulatedCompare::AddressBorder) inBorder = Unary(state, spv::OpLogicalNot, TypeBool(state), inside(x, width));
|
||||
if (modeY == EmulatedCompare::AddressBorder) {
|
||||
const auto outsideY = Unary(state, spv::OpLogicalNot, TypeBool(state), inside(y, height));
|
||||
inBorder = inBorder == 0u ? outsideY : Binary(state, spv::OpLogicalOr, TypeBool(state), inBorder, outsideY);
|
||||
}
|
||||
if (inBorder != 0u) red = Select(state, f32, inBorder, borderRed, red);
|
||||
switch (EmulatedCompare::Function(compare)) {
|
||||
case 0u: return zero;
|
||||
case 1u: return Select(state, f32, Binary(state, spv::OpFOrdLessThan, TypeBool(state), reference, red), one, zero);
|
||||
case 2u: return Select(state, f32, Binary(state, spv::OpFOrdEqual, TypeBool(state), reference, red), one, zero);
|
||||
case 3u: return Select(state, f32, Binary(state, spv::OpFOrdLessThanEqual, TypeBool(state), reference, red), one, zero);
|
||||
case 4u: return Select(state, f32, Binary(state, spv::OpFOrdGreaterThan, TypeBool(state), reference, red), one, zero);
|
||||
case 5u: return Select(state, f32, Binary(state, spv::OpFOrdNotEqual, TypeBool(state), reference, red), one, zero);
|
||||
case 6u: return Select(state, f32, Binary(state, spv::OpFOrdGreaterThanEqual, TypeBool(state), reference, red), one, zero);
|
||||
default: return one;
|
||||
}
|
||||
};
|
||||
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));
|
||||
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})));
|
||||
} else {
|
||||
const auto half = f32Constant(0.5f);
|
||||
const auto centreU = Binary(state, spv::OpFSub, f32, scaledU, half);
|
||||
const auto centreV = Binary(state, spv::OpFSub, f32, scaledV, half);
|
||||
const auto floorU = ext(f32, GLSLstd450Floor, {centreU});
|
||||
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 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});
|
||||
const auto bottom = ext(f32, GLSLstd450FMix, {compareTexel(x0, y1), compareTexel(x1, y1), weightU});
|
||||
result = ext(f32, GLSLstd450FMix, {top, bottom, weightV});
|
||||
}
|
||||
ctx.Define(access.inst, TableResult(ctx, access, ResultVector(ctx, access, result, setup.numericClass, true, false)));
|
||||
}
|
||||
|
||||
void EmitSampleOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, const SampleSetup& setup) {
|
||||
auto& state = ctx.state;
|
||||
const auto& mem = access.mem;
|
||||
const auto& image = access.image;
|
||||
if (setup.dref && (image.emulatedCompare & EmulatedCompare::Enabled) != 0u) {
|
||||
EmitEmulatedCompareSample(ctx, access, setup);
|
||||
return;
|
||||
}
|
||||
const bool explicitLod = HasFlag(mem, RdnaImageSampleFlagDerivative) || HasFlag(mem, RdnaImageSampleFlagLod) || HasFlag(mem, RdnaImageSampleFlagLevelZero) || state.program.Resources().stage != IrShaderStage::Pixel;
|
||||
std::uint32_t opcode = spv::OpImageSampleImplicitLod;
|
||||
if (explicitLod) {
|
||||
|
||||
@@ -149,6 +149,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
|
||||
- [libScePlayerInvitationDialog](../../core/libs/prx/libScePlayerInvitationDialog/libScePlayerInvitationDialog.cpp) simulates dialog completion without displaying UI or sending invitations; its parameter ABI remains unverified.
|
||||
- [64-bit LDS atomics](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) (`ds_*_u64`, `ds_*_b64`, `ds_*_f64`) read and write the two dwords under a workgroup lock stored after the guest LDS, as LDS is a 32-bit array without 64-bit atomics. They are atomic with respect to each other only: 32-bit accesses to the same dwords issued concurrently are not ordered by the lock. 64-bit GDS atomics throw. `ds_min/max(_rtn)_f64` and `ds_cmpst(_rtn)_f64` follow the NaN and signed-zero rules measured for the f32 forms; the f64 forms were not measured on hardware.
|
||||
- [wcsrtombs_s](../../core/libs/prx/libc/src/Multibyte.cpp) (libc) follows the console's `libc.prx`, but a runtime-constraint violation only returns its error: the default handler there writes the message and a newline to `stderr` before returning, which is not done here. Conversion uses the C locale of `wcrtomb` (`MB_CUR_MAX` 1), so the path where a multibyte character does not fit the remaining space is not reached
|
||||
|
||||
Reference in New Issue
Block a user