Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct GuestSamplerResource {
bool nonSeamlessCube = false;
};

GuestSamplerResource DecodeSamplerResource(std::span<const std::uint32_t> words, bool unnormalizedProven = false, bool forceDegammaPaired = false);
GuestSamplerResource DecodeSamplerResource(std::span<const std::uint32_t> words, bool unnormalizedProven = false, bool forceDegammaPaired = false, bool integerBorder = false);
std::optional<std::array<std::uint32_t, 4>> SingleLevelSamplerWords(std::span<const std::uint32_t, 4> words, bool singleLevelImage, bool mipmappedImage);

}
Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/include/Sampler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SamplerCache {
explicit SamplerCache(std::size_t capacity = 1024);
SamplerCache(const SamplerCache&) = delete;
SamplerCache& operator=(const SamplerCache&) = delete;
std::shared_ptr<Sampler> Get(const Context& context, std::span<const std::uint32_t> words, bool compareEnable, bool unnormalizedProven = false, bool forceDegammaPaired = false);
std::shared_ptr<Sampler> Get(const Context& context, std::span<const std::uint32_t> words, bool compareEnable, bool unnormalizedProven = false, bool forceDegammaPaired = false, bool integerBorder = false);
// APS5_PROFILE_DRAW counters: lookups served by an existing sampler, and samplers created.
std::uint64_t Hits() const { return hits; }
std::uint64_t Misses() const { return misses; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ class ShaderResources {
std::array<std::uint32_t, 4> words{};
bool compareEnable = false;
bool unnormalized = false;
bool integerBorder = false;
bool singleLevelImage = false;
bool mipmappedImage = false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ float toSignedLodBias(std::uint32_t raw) {

}

GuestSamplerResource DecodeSamplerResource(std::span<const std::uint32_t> words, bool unnormalizedProven, bool forceDegammaPaired) {
GuestSamplerResource DecodeSamplerResource(std::span<const std::uint32_t> words, bool unnormalizedProven, bool forceDegammaPaired, bool integerBorder) {
Require(words.size() == 4, "guest sampler descriptor must contain 4 dwords");

const auto clampX = (words[0] >> 0u) & 0x7u;
Expand Down Expand Up @@ -135,12 +135,12 @@ GuestSamplerResource DecodeSamplerResource(std::span<const std::uint32_t> words,

VkBorderColor border;
switch (borderColorType) {
case 0: border = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK; break;
case 1: border = VK_BORDER_COLOR_INT_OPAQUE_BLACK; break;
case 2: border = VK_BORDER_COLOR_INT_OPAQUE_WHITE; break;
case 0: border = integerBorder ? VK_BORDER_COLOR_INT_TRANSPARENT_BLACK : VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; break;
case 1: border = integerBorder ? VK_BORDER_COLOR_INT_OPAQUE_BLACK : VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK; break;
case 2: border = integerBorder ? VK_BORDER_COLOR_INT_OPAQUE_WHITE : VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; break;
default:
if (readsBorderColor(clampX) || readsBorderColor(clampY) || readsBorderColor(clampZ)) throw std::runtime_error("AGC graphics: guest sampler descriptor uses a border color table which is not implemented");
border = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK;
border = integerBorder ? VK_BORDER_COLOR_INT_TRANSPARENT_BLACK : VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
break;
}

Expand Down
6 changes: 3 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ namespace AgcDriver::Graphics {

SamplerCache::SamplerCache(std::size_t capacity) : capacity(std::max<std::size_t>(capacity, 1)) {}

std::shared_ptr<Sampler> SamplerCache::Get(const Context& context, std::span<const std::uint32_t> words, bool compareEnable, bool unnormalizedProven, bool forceDegammaPaired) {
std::shared_ptr<Sampler> SamplerCache::Get(const Context& context, std::span<const std::uint32_t> words, bool compareEnable, bool unnormalizedProven, bool forceDegammaPaired, bool integerBorder) {
Require(words.size() == 4, "guest sampler descriptor must contain 4 dwords");
const std::array<std::uint32_t, 5> key{words[0], words[1], words[2], words[3], (compareEnable ? 1u : 0u) | (unnormalizedProven ? 2u : 0u)};
const std::array<std::uint32_t, 5> key{words[0], words[1], words[2], words[3], (compareEnable ? 1u : 0u) | (unnormalizedProven ? 2u : 0u) | (integerBorder ? 4u : 0u)};
std::lock_guard lock(mutex);
++clock;
if (const auto found = entries.find(key); found != entries.end()) {
Expand All @@ -79,7 +79,7 @@ namespace AgcDriver::Graphics {
return found->second.sampler;
}
++misses;
auto resource = DecodeSamplerResource(words, unnormalizedProven, forceDegammaPaired);
auto resource = DecodeSamplerResource(words, unnormalizedProven, forceDegammaPaired, integerBorder);
resource.compareEnable = compareEnable;
auto sampler = std::make_shared<Sampler>(context, resource);
// The cap keeps live samplers well below the device's limit (NVIDIA: ~4000); a set in flight
Expand Down
12 changes: 7 additions & 5 deletions core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,7 @@ void VisitContentKey(const CompiledShader& shader, bool dataWords, bool movableB
packBits(binding.imageDepthCompare);
packBits(binding.imageAtomic);
packBits(binding.samplerUnnormalized);
packBits(binding.samplerIntegerBorder);
packBits(binding.imageUnnormalized);
// Read-only elements are bound without a write set: an object built for one written set
// must not serve a build with another (the variant implies it, this makes it explicit).
Expand Down Expand Up @@ -2908,14 +2909,15 @@ void ShaderResources::addImageBinding(const ShaderRecompiler::DescriptorBinding&
const auto words = std::span<const std::uint32_t>(binding.guestDescriptor).subspan(static_cast<std::size_t>(element) * elementWords, elementWords);
const bool compareEnable = binding.samplerDepthCompare.at(element);
const bool unnormalized = element < binding.samplerUnnormalized.size() && binding.samplerUnnormalized[element];
const bool integerBorder = element < binding.samplerIntegerBorder.size() && binding.samplerIntegerBorder[element];
if (context.samplerCache != nullptr && !SamplerCacheDisabled()) {
samplers.push_back(context.samplerCache->Get(context, words, compareEnable, unnormalized, true));
samplers.push_back(context.samplerCache->Get(context, words, compareEnable, unnormalized, true, integerBorder));
} else {
auto resource = DecodeSamplerResource(words, unnormalized, true);
auto resource = DecodeSamplerResource(words, unnormalized, true, integerBorder);
resource.compareEnable = compareEnable;
samplers.push_back(std::make_shared<Sampler>(context, resource));
}
samplerSources.push_back({{words[0], words[1], words[2], words[3]}, compareEnable, unnormalized});
samplerSources.push_back({{words[0], words[1], words[2], words[3]}, compareEnable, unnormalized, integerBorder});
item.imageAllocations.push_back(samplers.size() - 1);
}
Require(samplers.size() <= context.limits.maxDescriptorSetSamplers, "pipeline sampler descriptors exceed device limits");
Expand Down Expand Up @@ -3073,9 +3075,9 @@ void ShaderResources::applyAnisoOverride() {
if (!variant.has_value()) continue;
const auto& words = *variant;
if (context.samplerCache != nullptr && !SamplerCacheDisabled()) {
samplers[index] = context.samplerCache->Get(context, words, source.compareEnable, source.unnormalized);
samplers[index] = context.samplerCache->Get(context, words, source.compareEnable, source.unnormalized, false, source.integerBorder);
} else {
auto resource = DecodeSamplerResource(words, source.unnormalized);
auto resource = DecodeSamplerResource(words, source.unnormalized, false, source.integerBorder);
resource.compareEnable = source.compareEnable;
samplers[index] = std::make_shared<Sampler>(context, resource);
}
Expand Down
13 changes: 8 additions & 5 deletions core/libs/prx/libSceAgcDriver/tests/GuestSamplerResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void RunGuestSamplerResourceTests() {
Require(result.addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, "clamp mode 2 must decode to clamp-to-edge");
Require(!result.anisotropyEnable && result.maxAnisotropy == 1.0f, "non-anisotropic filter must leave anisotropy disabled");
Require(nearlyEqual(result.maxLod, 12.0f), "max LOD decoded incorrectly");
Require(result.borderColor == VK_BORDER_COLOR_INT_TRANSPARENT_BLACK, "border color type 0 must decode to transparent black");
Require(result.borderColor == VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, "border color type 0 must decode to transparent black");

Fields nearest = base;
nearest.xyMagFilter = 0;
Expand Down Expand Up @@ -360,7 +360,7 @@ void RunGuestSamplerResourceTests() {
requireUnnormalized(unnormalized, "captured unnormalized S#");
Require(unnormalized.magFilter == VK_FILTER_LINEAR && unnormalized.minFilter == VK_FILTER_LINEAR, "captured unnormalized S# filters decoded incorrectly");
Require(unnormalized.addressModeU == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE && unnormalized.addressModeV == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, "captured unnormalized S# clamp modes decoded incorrectly");
Require(unnormalized.borderColor == VK_BORDER_COLOR_INT_TRANSPARENT_BLACK, "captured unnormalized S# border color decoded incorrectly");
Require(unnormalized.borderColor == VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, "captured unnormalized S# border color decoded incorrectly");
Require(!DecodeSamplerResource(pack(base)).unnormalizedCoordinates, "a normalized S# decoded to unnormalized coordinates");

Fields unnormalizedBase = base;
Expand All @@ -371,7 +371,7 @@ void RunGuestSamplerResourceTests() {
const auto point = DecodeSamplerResource(pack(unnormalizedPoint), true);
requireUnnormalized(point, "unnormalized point S#");
Require(point.magFilter == VK_FILTER_NEAREST && point.minFilter == VK_FILTER_NEAREST, "unnormalized point S# filters decoded incorrectly");
const std::array borders{VK_BORDER_COLOR_INT_TRANSPARENT_BLACK, VK_BORDER_COLOR_INT_OPAQUE_BLACK, VK_BORDER_COLOR_INT_OPAQUE_WHITE};
const std::array borders{VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK, VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE};
for (std::uint32_t type = 0; type < borders.size(); ++type) {
Fields border = unnormalizedBase;
border.clampX = 6;
Expand Down Expand Up @@ -504,10 +504,13 @@ void RunGuestSamplerResourceTests() {

Fields opaqueBlack = base;
opaqueBlack.borderColorType = 1;
Require(DecodeSamplerResource(pack(opaqueBlack)).borderColor == VK_BORDER_COLOR_INT_OPAQUE_BLACK, "border color type 1 must decode to opaque black");
Require(DecodeSamplerResource(pack(opaqueBlack)).borderColor == VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK, "border color type 1 must decode to opaque black");
Require(DecodeSamplerResource(pack(opaqueBlack), false, false, true).borderColor == VK_BORDER_COLOR_INT_OPAQUE_BLACK, "border color type 1 of a sampler for integer images must decode to integer opaque black");
Fields opaqueWhite = base;
opaqueWhite.borderColorType = 2;
Require(DecodeSamplerResource(pack(opaqueWhite)).borderColor == VK_BORDER_COLOR_INT_OPAQUE_WHITE, "border color type 2 must decode to opaque white");
Require(DecodeSamplerResource(pack(opaqueWhite)).borderColor == VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, "border color type 2 must decode to opaque white");
Require(DecodeSamplerResource(pack(opaqueWhite), false, false, true).borderColor == VK_BORDER_COLOR_INT_OPAQUE_WHITE, "border color type 2 of a sampler for integer images must decode to integer opaque white");
Require(DecodeSamplerResource(pack(base), false, false, true).borderColor == VK_BORDER_COLOR_INT_TRANSPARENT_BLACK, "border color type 0 of a sampler for integer images must decode to integer transparent black");

Fields positiveBias = base;
positiveBias.lodBiasRaw = 256;
Expand Down
3 changes: 2 additions & 1 deletion core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void setEnvironment(const char* name, const std::string& value) {
}

bool sameBinding(const DescriptorBinding& left, const DescriptorBinding& right) {
return left.kind == right.kind && left.role == right.role && left.descriptorSet == right.descriptorSet && left.binding == right.binding && left.count == right.count && left.guestDescriptor == right.guestDescriptor && left.readOnly == right.readOnly && left.imageShape == right.imageShape && left.samplerDepthCompare == right.samplerDepthCompare && left.imageWritten == right.imageWritten && left.imageDepthCompare == right.imageDepthCompare && left.imageAtomic == right.imageAtomic && left.bufferAtomic == right.bufferAtomic && left.bufferWritten == right.bufferWritten && left.bufferRead == right.bufferRead && left.samplerUnnormalized == right.samplerUnnormalized && left.imageUnnormalized == right.imageUnnormalized && left.imageSamplers == right.imageSamplers;
return left.kind == right.kind && left.role == right.role && left.descriptorSet == right.descriptorSet && left.binding == right.binding && left.count == right.count && left.guestDescriptor == right.guestDescriptor && left.readOnly == right.readOnly && left.imageShape == right.imageShape && left.samplerDepthCompare == right.samplerDepthCompare && left.imageWritten == right.imageWritten && left.imageDepthCompare == right.imageDepthCompare && left.imageAtomic == right.imageAtomic && left.bufferAtomic == right.bufferAtomic && left.bufferWritten == right.bufferWritten && left.bufferRead == right.bufferRead && left.samplerUnnormalized == right.samplerUnnormalized && left.samplerIntegerBorder == right.samplerIntegerBorder && left.imageUnnormalized == right.imageUnnormalized && left.imageSamplers == right.imageSamplers;
}

bool sameBindings(const std::vector<DescriptorBinding>& left, const std::vector<DescriptorBinding>& right) {
Expand Down Expand Up @@ -119,6 +119,7 @@ DescriptorBinding sampleBinding(std::uint32_t seed) {
binding.bufferAtomic = {true};
binding.bufferWritten = {false, false, true, true, false};
binding.samplerUnnormalized = {false, true, true};
binding.samplerIntegerBorder = {true, false, seed % 2 == 1};
binding.imageUnnormalized = {true, false, seed % 2 == 0};
binding.imageSamplers = {0x5u, 0u, 0x80000000u};
binding.bufferRead = {true, false, false, true, true, false};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp"
#include "Recompiler.hpp"
#include "VulkanTestDevice.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>

namespace {

using AgcDriver::Graphics::Require;
using ShaderRecompiler::ShaderStage;

constexpr std::uint32_t Threads = 32;
constexpr std::uint32_t Width = 4;
constexpr std::uint32_t Type2D = 9;
constexpr std::uint32_t Unorm8_8_8_8 = 56;
constexpr std::uint32_t Uint8_8_8_8 = 60;
constexpr std::uint32_t SwizzleXYZW = 0xfacu;
constexpr std::uint32_t One = 0x3f800000u;
constexpr std::uint32_t ClampBorder = 0x1b6u;
constexpr std::uint32_t AnisoOverride = (1u << 29u) | (2u << 22u) | (2u << 20u);

alignas(256) std::array<std::uint32_t, Threads * 4> Output{};
alignas(256) std::array<std::uint32_t, Width> Texels{};

alignas(256) constexpr std::array<std::uint32_t, 9> SampleOutside{
0x34060084u, 0x7e2802f4u, 0x7e2a02f4u, 0xf09c0f08u, 0x00610a14u, 0xbf8c3f70u, 0xe0781000u, 0x80000a03u,
0xbf810000u,
};

std::string Hex(std::uint32_t value) {
char text[16];
std::snprintf(text, sizeof(text), "0x%08x", value);
return text;
}

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) | (((Width - 1u) & 3u) << 30u),
(Width - 1u) >> 2u,
SwizzleXYZW | (Type2D << 28u),
0u,
0u,
0u,
0u,
};
}

void Run(AgcDriver::VulkanDevice& device, std::uint32_t format, std::uint32_t borderType, std::uint32_t filters) {
Output.fill(0xdeadbeefu);
Texels.fill(0u);
std::vector<std::uint32_t> userData(24, 0u);
const auto outputAddress = reinterpret_cast<std::uintptr_t>(Output.data());
const std::array<std::uint32_t, 4> output{static_cast<std::uint32_t>(outputAddress), static_cast<std::uint32_t>((outputAddress >> 32u) & 0xffffu), static_cast<std::uint32_t>(Output.size() * 4u), 0x31016facu};
const auto texture = TextureDescriptor(format);
const std::array<std::uint32_t, 4> sampler{ClampBorder, 0x00fff000u, filters, borderType << 30u};
std::copy(output.begin(), output.end(), userData.begin());
std::copy(texture.begin(), texture.end(), userData.begin() + 4);
std::copy(sampler.begin(), sampler.end(), userData.begin() + 12);
const std::span<const std::uint32_t> code(SampleOutside);
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, {true, 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;
const auto result = ShaderRecompiler::Recompile(request);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
}

void Check(AgcDriver::VulkanDevice& device, std::uint32_t format, std::uint32_t borderType, const std::array<std::uint32_t, 4>& expected, const std::string& what, std::uint32_t filters = 0u) {
Run(device, format, borderType, filters);
for (std::uint32_t thread = 0; thread < Threads; ++thread) {
for (std::uint32_t component = 0; component < 4u; ++component) {
const auto actual = Output[thread * 4u + component];
Require(actual == expected[component], what + ": thread " + std::to_string(thread) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(expected[component]));
}
}
}

}

int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
Check(*device, Unorm8_8_8_8, 0u, {0u, 0u, 0u, 0u}, "8_8_8_8_UNORM transparent black border");
Check(*device, Unorm8_8_8_8, 1u, {0u, 0u, 0u, One}, "8_8_8_8_UNORM opaque black border");
Check(*device, Unorm8_8_8_8, 2u, {One, One, One, One}, "8_8_8_8_UNORM opaque white border");
Check(*device, Uint8_8_8_8, 0u, {0u, 0u, 0u, 0u}, "8_8_8_8_UINT transparent black border");
Check(*device, Uint8_8_8_8, 1u, {0u, 0u, 0u, 1u}, "8_8_8_8_UINT opaque black border");
Check(*device, Uint8_8_8_8, 2u, {1u, 1u, 1u, 1u}, "8_8_8_8_UINT opaque white border");
Check(*device, Unorm8_8_8_8, 2u, {One, One, One, One}, "8_8_8_8_UNORM opaque white border under ANISO_OVERRIDE", AnisoOverride);
Check(*device, Uint8_8_8_8, 2u, {1u, 1u, 1u, 1u}, "8_8_8_8_UINT opaque white border under ANISO_OVERRIDE", AnisoOverride);
std::puts("image sample border color tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
Loading
Loading