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
57 changes: 43 additions & 14 deletions core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,36 +384,58 @@ constexpr std::array<std::array<std::int8_t, 2>, 2> StandardLocations2{{{4, 4},
constexpr std::array<std::array<std::int8_t, 2>, 4> StandardLocations4{{{-2, -6}, {6, -2}, {-6, 2}, {2, 6}}};
constexpr std::array<std::array<std::int8_t, 2>, 8> StandardLocations8{{{1, -3}, {-1, 3}, {5, 1}, {-3, -5}, {-5, 5}, {-7, -1}, {3, 7}, {7, -7}}};

std::string singleSampleMsaaRejection(const QueueState& queue) {
const auto& cx = queue.context;
std::string nonTriangleRejection(const QueueState& queue, const char* message, bool rectangles = false) {
const auto word = [](const Registers& registers, std::uint32_t offset, RegisterBank bank = RegisterBank::Context) -> std::optional<std::uint32_t> {
const auto it = find(registers, offset, bank);
if (it == registers.end()) return std::nullopt;
return it->second;
};
for (std::uint32_t pixel = 0; pixel < 4; ++pixel) {
const auto offset = 0x2feu + pixel * 4u;
if (const auto locations = word(cx, offset); locations && (*locations & 0xffu) != 0) return zeroMessage(offset, *locations, "MSAA_ENABLE on a single-sample target whose sample is off the pixel center");
}
if (const auto alphaToMask = word(cx, 0x2dc); alphaToMask && (*alphaToMask & 1u) != 0 && !(*alphaToMask == AlphaToCoverageDithered && alphaToCoverageDisabled(cx))) return zeroMessage(0x2dc, *alphaToMask, "MSAA_ENABLE on a single-sample target with alpha-to-coverage");
if (const auto conservative = word(cx, 0x313); conservative && (*conservative & ~0x001f6000u) != 0) return zeroMessage(0x313, *conservative, "MSAA_ENABLE on a single-sample target with conservative rasterization");
constexpr const char* primitives = "MSAA_ENABLE on a single-sample target for primitives other than triangles";
const auto& cx = queue.context;
const auto stages = word(cx, 0x2d5);
if (!stages) return {};
if ((*stages & 0x20u) != 0) {
if (const auto output = word(cx, 0x29b); output && ((*output & 0x80000000u) != 0 || (*output & 0x3fu) != 2u)) return zeroMessage(0x29b, *output, primitives);
if (const auto output = word(cx, 0x29b); output && ((*output & 0x80000000u) != 0 || (*output & 0x3fu) != 2u)) return zeroMessage(0x29b, *output, message);
} else if ((*stages & 4u) != 0) {
if (const auto parameters = word(cx, 0x2db)) {
const auto domain = *parameters & 3u;
const auto topology = (*parameters >> 5u) & 7u;
if ((domain != 1u && domain != 2u) || (topology != 2u && topology != 3u)) return zeroMessage(0x2db, *parameters, primitives);
if ((domain != 1u && domain != 2u) || (topology != 2u && topology != 3u)) return zeroMessage(0x2db, *parameters, message);
}
} else if (const auto primitive = word(queue.userConfig, 0x242, RegisterBank::UserConfig); primitive && *primitive != 4u && *primitive != 5u && *primitive != 6u) {
return zeroMessage(0x242, *primitive, primitives);
} else if (const auto primitive = word(queue.userConfig, 0x242, RegisterBank::UserConfig); primitive && *primitive != 4u && *primitive != 5u && *primitive != 6u && !(rectangles && (*primitive == 7u || *primitive == 17u))) {
return zeroMessage(0x242, *primitive, message);
}
return {};
}

std::string singleSampleMsaaRejection(const QueueState& queue) {
const auto& cx = queue.context;
const auto word = [](const Registers& registers, std::uint32_t offset, RegisterBank bank = RegisterBank::Context) -> std::optional<std::uint32_t> {
const auto it = find(registers, offset, bank);
if (it == registers.end()) return std::nullopt;
return it->second;
};
for (std::uint32_t pixel = 0; pixel < 4; ++pixel) {
const auto offset = 0x2feu + pixel * 4u;
if (const auto locations = word(cx, offset); locations && (*locations & 0xffu) != 0) return zeroMessage(offset, *locations, "MSAA_ENABLE on a single-sample target whose sample is off the pixel center");
}
if (const auto alphaToMask = word(cx, 0x2dc); alphaToMask && (*alphaToMask & 1u) != 0 && !(*alphaToMask == AlphaToCoverageDithered && alphaToCoverageDisabled(cx))) return zeroMessage(0x2dc, *alphaToMask, "MSAA_ENABLE on a single-sample target with alpha-to-coverage");
if (const auto conservative = word(cx, 0x313); conservative && (*conservative & ~0x001f6000u) != 0) return zeroMessage(0x313, *conservative, "MSAA_ENABLE on a single-sample target with conservative rasterization");
return nonTriangleRejection(queue, "MSAA_ENABLE on a single-sample target for primitives other than triangles");
}

std::string singleSampleDepthScanRejection(const QueueState& queue) {
if (auto reason = nonTriangleRejection(queue, "a single-sample scan into a multisampled depth target for primitives other than triangles and rectangles", true); !reason.empty()) return reason;
const auto& cx = queue.context;
if (const auto eqaa = find(cx, 0x201); eqaa != cx.end() && (eqaa->second & 0x0f0c0000u) != 0) return zeroMessage(0x201, eqaa->second, "a single-sample scan into a multisampled depth target with Z interpolation or overrasterization (DB_EQAA)");
if (PixelProgramSkipped(queue)) return {};
constexpr std::uint32_t sampleIndex = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary);
const auto ena = find(cx, 0x1b3);
const auto addr = find(cx, 0x1b4);
if (ena != cx.end() && addr != cx.end() && (ena->second & addr->second & sampleIndex) != 0) return zeroMessage(0x1b3, ena->second, "a single-sample scan into a multisampled depth target with a pixel program reading the sample index");
if (const auto control = find(cx, 0x203); control != cx.end() && (control->second & 0x100u) != 0) return zeroMessage(0x203, control->second, "a single-sample scan into a multisampled depth target with a sample-mask export");
return {};
}

constexpr std::uint32_t CoveredCentroidIsCenter = 1u << 29u;
constexpr std::uint32_t SampleZeroRefusedInputs = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCentroid) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::LinearCentroid) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::SampleCoverage);
constexpr std::uint32_t PixelCenterPositionInputs = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY);
Expand Down Expand Up @@ -1055,7 +1077,6 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) {
Require(!result.customSampleLocations, "dithered alpha-to-coverage with custom sample locations is unsupported");
}
const bool depthKeepsSampleZero = result.depth && result.depth->samples == 1 && result.samples > 1;
Require(!result.depth || depthKeepsSampleZero || result.depth->samples == result.samples, "the depth target's sample count differs from the rasterizer's");
zero(cx, 0x292, ScanModeMask & ~1u, "scan conversion mode");
if (result.samples > 1 && (read(cx, 0x292) & 1u) == 0) {
const bool filled = result.topology != VK_PRIMITIVE_TOPOLOGY_POINT_LIST && result.topology != VK_PRIMITIVE_TOPOLOGY_LINE_LIST && result.topology != VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
Expand All @@ -1074,6 +1095,14 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) {
Require(rasterizesSampleZeroOnly(cx, result), "the depth target's sample count differs from the rasterizer's");
rasterizeSampleZero(result);
}
if (result.depth && result.depth->samples > 1 && result.samples == 1 && (read(cx, 0x292) & 1u) == 0) {
Require(!result.alphaToCoverage, "a single-sample scan into a multisampled depth target with alpha-to-coverage is unsupported");
if (auto reason = singleSampleDepthScanRejection(queue); !reason.empty()) throw std::runtime_error(reason);
result.samples = result.depth->samples;
result.customSampleLocations = true;
result.sampleLocations.fill(0);
}
Require(!result.depth || depthKeepsSampleZero || result.depth->samples == result.samples, "the depth target's sample count differs from the rasterizer's");
zero(cx, 0x80, ~0u, "window offset");
zero(cx, 0x8d, ScreenOffsetMask, "reserved PA_SU_HARDWARE_SCREEN_OFFSET bits");
Require(read(cx, 0x83) == 0xffffu, "clip rectangles are unsupported");
Expand Down
98 changes: 98 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3173,6 +3173,103 @@ void multisampleDccStateTests() {
}
}

void multisampledDepthTests() {
using AgcDriver::Graphics::DecodeState;
const auto singleSampleScan = [] {
auto queue = makeState();
queue.context[0x2f8] = 0;
queue.context[0x292] = 2;
queue.context[0x31d] = 0x12000;
queue.context[0x1b3] = 2;
queue.context[0x1b4] = 2;
queue.context[0x000] = 0;
queue.context[0x002] = 0;
queue.context[0x007] = (3u << 16u) | 63u;
queue.context[0x00a] = 0;
queue.context[0x00b] = 0;
queue.context[0x010] = 0x2290098b;
queue.context[0x011] = 0x20000180;
for (const auto offset : {0x012u, 0x014u}) queue.context[offset] = 0x100;
queue.context[0x200] = 0x16;
return queue;
};
const auto requireCentered = [](const AgcDriver::Graphics::State& state, const std::string& what) {
Require(state.samples == 4 && state.depth && state.depth->samples == 4 && state.customSampleLocations && !state.sampleLocationGrid && !state.perSampleShading, what + " did not rasterize the depth target's samples");
const auto locations = AgcDriver::Graphics::SampleLocations(state);
for (std::uint32_t sample = 0; sample < state.samples; ++sample) Require(locations[sample].x == 0.5f && locations[sample].y == 0.5f, what + " did not place every sample at the pixel center");
};
const auto depthOnly = [&] {
auto queue = singleSampleScan();
queue.context[0x8e] = 0;
queue.context[0x010] = 0xa290098b;
queue.context[0x200] = 0x777;
queue.context[0x201] = 0;
return queue;
};
auto queue = depthOnly();
auto state = DecodeState(queue);
Require(state.colors.empty(), "a depth-only single-sample scan decoded a color target");
requireCentered(state, "a single-sample scan into a 4x depth target (PPSA18216's registers)");
Require(AgcDriver::Graphics::DrawRejection(queue, false).empty(), "the precheck refused a single-sample scan into a 4x depth target");
for (const auto primitive : {5u, 6u, 7u, 17u}) {
queue = depthOnly();
queue.userConfig[0x242] = primitive;
requireCentered(DecodeState(queue), "a single-sample scan of primitive type " + std::to_string(primitive) + " into a 4x depth target");
}
queue = depthOnly();
queue.context[0x000] = 1;
queue.context[0x00b] = std::bit_cast<std::uint32_t>(0.25f);
state = DecodeState(queue);
requireCentered(state, "a depth clear scanned single-sampled into a 4x depth target");
Require(state.depthWrite && state.depthCompare == VK_COMPARE_OP_ALWAYS, "a depth clear into a 4x depth target did not write every covered sample");
queue = depthOnly();
queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary);
requireCentered(DecodeState(queue), "a single-sample scan whose pixel program is skipped");
queue = depthOnly();
queue.context[0x201] = 0x00040000u;
expectFailure([&] { DecodeState(queue); }, "a single-sample scan into a multisampled depth target with Z interpolation or overrasterization (DB_EQAA)");
queue = singleSampleScan();
state = DecodeState(queue);
Require(state.colors.size() == 1 && state.colors[0].samples == 4, "a single-sample scan into 4x depth and color targets lost its color target");
requireCentered(state, "a single-sample scan into 4x depth and color targets");
queue.context[0x293] = 0x10000u;
Require(!DecodeState(queue).perSampleShading, "PS_ITER_SAMPLE requested sample shading for a single-sample scan");
queue = singleSampleScan();
queue.context[0x31d] = 0;
expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's");
queue = singleSampleScan();
queue.context[0x010] = 0x22900983;
expectFailure([&] { DecodeState(queue); }, "a color target's sample count differs from the rasterizer's");
queue = singleSampleScan();
queue.context[0x2dc] = 0x0000aa01u;
expectFailure([&] { DecodeState(queue); }, "a single-sample scan into a multisampled depth target with alpha-to-coverage");
for (const auto primitive : {2u, 3u}) {
queue = singleSampleScan();
queue.userConfig[0x242] = primitive;
expectFailure([&] { DecodeState(queue); }, "a single-sample scan into a multisampled depth target for primitives other than triangles and rectangles");
}
queue = singleSampleScan();
queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::Ancillary);
expectFailure([&] { DecodeState(queue); }, "a single-sample scan into a multisampled depth target with a pixel program reading the sample index");
queue = singleSampleScan();
queue.context[0x1b3] = queue.context[0x1b4] = 2u | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveSample);
requireCentered(DecodeState(queue), "a single-sample scan with a pixel program reading sample-interpolated inputs");
queue = singleSampleScan();
queue.context[0x1c4] = 9;
queue.context[0x203] |= 0x100u;
expectFailure([&] { DecodeState(queue); }, "a single-sample scan into a multisampled depth target with a sample-mask export");
queue = singleSampleScan();
queue.context[0x292] = 3;
for (std::uint32_t pixel = 0; pixel < 4; ++pixel) queue.context[0x2fe + pixel * 4u] = 0;
expectFailure([&] { DecodeState(queue); }, "the depth target's sample count differs from the rasterizer's");
queue = singleSampleScan();
queue.context[0x2f8] = 0x00100001;
queue.context[0x292] = 3;
for (std::uint32_t pixel = 0; pixel < 4; ++pixel) queue.context[0x2fe + pixel * 4u] = 0xcc44;
queue.context[0x31d] = 0x9000;
expectFailure([&] { DecodeState(queue); }, "the depth target's sample count differs from the rasterizer's");
}

void clipDistanceTests() {
auto queue = makeState();
queue.context[0x207] = 0x0040000f;
Expand Down Expand Up @@ -7965,6 +8062,7 @@ int main(int argc, char** argv) {
uint8x4TargetTests();
multisampleTests();
MsaaEnableSingleSampleTests();
multisampledDepthTests();
clipDistanceTests();
framebufferLayerTests();
UnwrittenSampleLocationTests();
Expand Down
Loading
Loading