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
2 changes: 2 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/Metal/MetalDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ bool waitFree(const DriverDetail::Submission& submission) {
require(descriptor.flags == 0, "nonzero submission flags are not implemented");
if (descriptor.words != 0) CaptureSubmissionCommands(submission, descriptor.address, descriptor.words);
ValidateSubmission(submission, descriptor.address);
CaptureIndirectRegisterLists(submission);
}
impl->WaitForFlipRoom(submission);
{
Expand Down Expand Up @@ -1071,6 +1072,7 @@ bool waitFree(const DriverDetail::Submission& submission) {
NativeGuestMemory::BorrowedRangesScope current(ranges);
CaptureSubmissionCommands(tail, tailAddress, stalled.rewindWords);
ValidateSubmission(tail, tailAddress);
CaptureIndirectRegisterLists(tail);
}
WaitForFlipRoom(tail);
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,18 +854,16 @@ bool reportSkipCount(std::uint64_t count) {
continue;
}
// Packets the CPU executes that read or store guest memory wait for committed work first.
if (Pm4::IndirectRegisterOpcode(opcode)) {
// Register values loaded from guest memory: wait only for work that may write them.
BeforeCpuRead(static_cast<std::uint64_t>(packet[1]) | (static_cast<std::uint64_t>(packet[2]) << 32u),
std::uint64_t{packet[4]} * (packet[3] != 0x80000000u ? 4u : 8u));
} else if (!RegisterOnlyPacket(opcode)) {
CompletePriorGpuWorkAndCopyBack(opcode);
}
// SET_*_REG_INDIRECT lists were read at submit time (CaptureIndirectRegisterLists), so these
// packets only update registers here.
const bool indirectRegisters = Pm4::IndirectRegisterOpcode(opcode);
if (!indirectRegisters && !RegisterOnlyPacket(opcode)) CompletePriorGpuWorkAndCopyBack(opcode);
// A store the driver performs itself: only its destination stops matching cached images.
// (A label it writes lets the guest go on, but guest writes reach later work of this
// submission only behind a wait, which forgets everything.)
if (const auto store = PacketStoreRange(packet)) ForgetCachedImages(*store);
Pm4::Execute(packet, queue);
if (indirectRegisters) Pm4::ExecuteIndirectRegisters(packet, submission.registerLists.at(cursor), queue);
else Pm4::Execute(packet, queue);
// Remember label values this driver wrote (RELEASE_MEM data, WRITE_DATA), for the wait above.
if (opcode == 0x49 || opcode == 0x37) {
if (const auto store = PacketStoreRange(packet); store && store->bytes != 0 && store->bytes <= 64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ struct Submission;
}

void CaptureSubmissionCommands(DriverDetail::Submission& submission, std::uint64_t guestAddress, std::size_t words);
// Reads the (offset, value) list of every SET_*_REG_INDIRECT packet of the captured commands into
// submission.registerLists, keyed by the packet's DWORD cursor: the lists are taken when the command
// buffer is submitted, as the commands are, not when the queue worker reaches the packet.
void CaptureIndirectRegisterLists(DriverDetail::Submission& submission);

}
16 changes: 16 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/src/SubmissionCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <string>

namespace AgcDriver {
namespace {
Expand Down Expand Up @@ -119,4 +120,19 @@ void CaptureSubmissionCommands(DriverDetail::Submission& submission, std::uint64
CaptureSegment(submission, guestAddress, words, budget);
}

void CaptureIndirectRegisterLists(DriverDetail::Submission& submission) {
submission.registerLists.clear();
for (std::size_t cursor = 0; cursor < submission.commands.size(); cursor += Pm4::PacketWords(submission.commands[cursor])) {
const auto header = submission.commands[cursor];
if ((header >> 30u) != 3u || !Pm4::IndirectRegisterOpcode((header >> 8u) & 0xffu)) continue;
const auto words = Pm4::PacketWords(header);
require(words <= submission.commands.size() - cursor, "indirect register packet extends past the command buffer");
try {
submission.registerLists.emplace(cursor, Pm4::ReadIndirectRegisters(std::span<const std::uint32_t>(submission.commands).subspan(cursor, words)));
} catch (const std::exception& error) {
throw std::runtime_error("AGC driver: " + Pm4::Name(header) + " at DWORD " + std::to_string(cursor) + ": " + error.what());
}
}
}

}
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/Metal/MetalDraw.mm
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ MTLTextureSwizzle channelSwizzle(std::uint8_t value) {
auto renderPass = [MTLRenderPassDescriptor renderPassDescriptor];
renderPass.renderTargetWidth = state.renderExtent.width;
renderPass.renderTargetHeight = state.renderExtent.height;
ConfigureSamplePositions(renderPass, backend.Device(), state);
struct Expansion {
id<MTLTexture> source;
std::uint32_t slot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ namespace AgcDriver::Metal {
void ConfigureRenderPipelineDescriptor(MTLRenderPipelineDescriptor* descriptor, const Graphics::State& state);
void ConfigureRenderPipelineDescriptor(MTLMeshRenderPipelineDescriptor* descriptor, const Graphics::State& state);
[[nodiscard]] id<MTLDepthStencilState> CreateDepthStencilState(id<MTLDevice> device, const Graphics::State& state);
// Sets the pass's programmable sample positions for a custom PA_SC_AA_SAMPLE_LOCS pattern. A standard
// pattern keeps Metal's default positions, which are the same locations. Throws when the device has no
// programmable sample positions.
void ConfigureSamplePositions(MTLRenderPassDescriptor* pass, id<MTLDevice> device, const Graphics::State& state);
void BindRenderState(id<MTLRenderCommandEncoder> encoder, const Graphics::State& state,
id<MTLDepthStencilState> depthStencilState);

Expand Down
11 changes: 11 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/Metal/MetalRenderState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MetalPipelineCache.hpp"

#include <algorithm>
#include <array>
#include <atomic>
#include <cmath>
#include <cstdio>
Expand Down Expand Up @@ -374,4 +375,14 @@ void BindRenderState(id<MTLRenderCommandEncoder> encoder, const Graphics::State&
backReferenceValue:state.stencilTest ? state.stencilBack.reference : 0];
}

void ConfigureSamplePositions(MTLRenderPassDescriptor* pass, id<MTLDevice> device, const Graphics::State& state) {
Require(pass != nil, "render pass descriptor is missing");
if (!state.customSampleLocations || state.rasterSamples == 1) return;
Require(device != nil && device.programmableSamplePositionsSupported, "custom sample locations require programmable sample positions");
const auto locations = Graphics::SampleLocations(state);
std::array<MTLSamplePosition, 8> positions{};
for (std::uint32_t sample = 0; sample < state.rasterSamples; ++sample) positions[sample] = MTLSamplePositionMake(locations[sample].x, locations[sample].y);
[pass setSamplePositions:positions.data() count:state.rasterSamples];
}

}
14 changes: 11 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ struct State {
VkExtent2D renderExtent;
// PA_SC_AA_CONFIG MSAA_NUM_SAMPLES as a count (1 when multisampling is off).
std::uint32_t rasterSamples = 1;
// PA_SC_AA_SAMPLE_LOCS of pixel 0 (one byte a sample: signed x in [3:0], signed y in [7:4], in
// sixteenths of a pixel), uniform over the 2x2 pixel grid; set when the pattern is not the standard one.
std::array<std::uint8_t, 8> sampleLocations{};
bool customSampleLocations = false;
VkPrimitiveTopology topology;
bool primitiveRestart = false;
VkViewport viewport;
Expand All @@ -126,6 +130,9 @@ struct State {

ShaderStages DecodeShaderStages(const QueueState& queue, bool vertexWorkgroup = false);
State DecodeState(const QueueState& queue, bool vertexWorkgroup = false);
// The rasterizer's sample positions in [0, 1) of a pixel, rasterSamples of them: the custom pattern, or
// the standard 2x/4x/8x pattern (the D3D/Vulkan standard, which is also Metal's default).
std::array<VkSampleLocationEXT, 8> SampleLocations(const State& state);
std::array<std::uint8_t, 8> ExportMappings(const State& state);
std::array<ShaderRecompiler::ColorExportPacking, 8> ExportPackings(const State& state);
ColorTarget DecodeColorBuffer(const Registers& context, std::uint32_t slot);
Expand Down Expand Up @@ -180,15 +187,16 @@ struct DrawKeyRange {
std::uint32_t first;
std::uint32_t count;
};
inline constexpr std::array<DrawKeyRange, 48> DrawKeyRegisters{{
inline constexpr std::array<DrawKeyRange, 49> DrawKeyRegisters{{
{RegisterBank::Context, 0x000, 1}, {RegisterBank::Context, 0x002, 1}, {RegisterBank::Context, 0x005, 1}, {RegisterBank::Context, 0x007, 7}, {RegisterBank::Context, 0x010, 6}, {RegisterBank::Context, 0x01a, 5},
{RegisterBank::Context, 0x080, 4}, {RegisterBank::Context, 0x08c, 4}, {RegisterBank::Context, 0x090, 2}, {RegisterBank::Context, 0x094, 2}, {RegisterBank::Context, 0x0b4, 2}, {RegisterBank::Context, 0x105, 4}, {RegisterBank::Context, 0x10b, 3}, {RegisterBank::Context, 0x10f, 6},
// SPI_PS_INPUT_CNTL_0..31, SPI_PS_INPUT_ENA/ADDR, SPI_PS_IN_CONTROL, SPI_SHADER_POS/Z/COL_FORMAT,
// CB_BLEND0..7_CONTROL, GE_MAX_OUTPUT_PER_SUBGROUP.
{RegisterBank::Context, 0x191, 32}, {RegisterBank::Context, 0x1b3, 2}, {RegisterBank::Context, 0x1b6, 1}, {RegisterBank::Context, 0x1c3, 3}, {RegisterBank::Context, 0x1e0, 8}, {RegisterBank::Context, 0x1ff, 1},
// DB_DEPTH_CONTROL .. PA_CL_VS_OUT_CNTL, PA_SC_MODE_CNTL_0/1, VGT_GS_MODE, VGT_GS_VERT_ITEMSIZE,
// PA_SU_VTX_CNTL, the sample masks, PA_SC_CONSERVATIVE_RASTERIZATION_CNTL.
{RegisterBank::Context, 0x200, 8}, {RegisterBank::Context, 0x286, 2}, {RegisterBank::Context, 0x292, 2}, {RegisterBank::Context, 0x29b, 1}, {RegisterBank::Context, 0x2ab, 1}, {RegisterBank::Context, 0x2ce, 1}, {RegisterBank::Context, 0x2d5, 2}, {RegisterBank::Context, 0x2db, 2}, {RegisterBank::Context, 0x2de, 6}, {RegisterBank::Context, 0x2f8, 2}, {RegisterBank::Context, 0x30e, 2}, {RegisterBank::Context, 0x313, 1},
// PA_SU_VTX_CNTL, PA_SC_AA_CONFIG, the sample locations, the sample masks,
// PA_SC_CONSERVATIVE_RASTERIZATION_CNTL.
{RegisterBank::Context, 0x200, 8}, {RegisterBank::Context, 0x286, 2}, {RegisterBank::Context, 0x292, 2}, {RegisterBank::Context, 0x29b, 1}, {RegisterBank::Context, 0x2ab, 1}, {RegisterBank::Context, 0x2ce, 1}, {RegisterBank::Context, 0x2d5, 2}, {RegisterBank::Context, 0x2db, 2}, {RegisterBank::Context, 0x2de, 6}, {RegisterBank::Context, 0x2f8, 2}, {RegisterBank::Context, 0x2fe, 16}, {RegisterBank::Context, 0x30e, 2}, {RegisterBank::Context, 0x313, 1},
// CB_COLOR0..7_BASE .. DCC_BASE (15 words a slot), CB_COLOR0..7_BASE_EXT, DCC_BASE_EXT, ATTRIB2, ATTRIB3.
{RegisterBank::Context, 0x318, 0x78}, {RegisterBank::Context, 0x390, 8}, {RegisterBank::Context, 0x398, 8}, {RegisterBank::Context, 0x3a8, 0x18},
// The pixel program address, RSRC2 and user words; the geometry-back user pointer and program
Expand Down
67 changes: 67 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libc/include/General.hpp"
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cmath>
Expand Down Expand Up @@ -211,6 +212,41 @@ std::uint32_t rasterSamples(const Registers& cx) {
return config == cx.end() ? 1u : 1u << (config->second & 7u);
}

// The standard 2x/4x/8x patterns as signed sixteenths of a pixel from its center (the D3D/Vulkan
// standard locations; Metal's default sample positions are the same).
constexpr std::array<std::array<std::int8_t, 2>, 2> StandardLocations2{{{4, 4}, {-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}}};

const std::array<std::int8_t, 2>& standardLocation(std::uint32_t samples, std::uint32_t sample) {
return samples == 2 ? StandardLocations2[sample] : samples == 4 ? StandardLocations4[sample] : StandardLocations8[sample];
}

// PA_SC_AA_SAMPLE_LOCS_PIXEL_X{0,1}Y{0,1}_{0..3}: four words a pixel of the 2x2 grid, a byte a sample.
// A pattern that differs between the four pixels is refused; a uniform one is kept (as custom when it
// is not the standard pattern for the sample count). A context that never wrote pixel 0's first word
// keeps the standard pattern, as multisampled draws rasterized before these registers were decoded.
void decodeSampleLocations(const Registers& cx, std::uint32_t samples, State& state) {
state.sampleLocations = {};
state.customSampleLocations = false;
if (samples == 1 || find(cx, 0x2fe) == cx.end()) return;
for (std::uint32_t pixel = 0; pixel < 4; ++pixel) {
for (std::uint32_t sample = 0; sample < samples; ++sample) {
const auto word = read(cx, 0x2fe + pixel * 4u + sample / 4u);
const auto bits = static_cast<std::uint8_t>((word >> (8u * (sample % 4u))) & 0xffu);
if (pixel == 0) {
const auto x = static_cast<std::int8_t>(static_cast<std::uint8_t>(bits << 4u)) >> 4;
const auto y = static_cast<std::int8_t>(static_cast<std::uint8_t>(bits & 0xf0u)) >> 4;
const auto& standard = standardLocation(samples, sample);
state.sampleLocations[sample] = bits;
state.customSampleLocations |= x != standard[0] || y != standard[1];
} else {
Require(bits == state.sampleLocations[sample], "nonstandard sample locations varying between pixels are unsupported");
}
}
}
}

// DB_Z_INFO: FORMAT [1:0], NUM_SAMPLES [3:2], SW_MODE [8:4], PARTIALLY_RESIDENT [12], MAXMIP [19:16],
// TILE_SURFACE_ENABLE [29]. Partial residency only governs page faults, and a mipmapped surface is
// rendered through its view's level (DB_DEPTH_VIEW MIP_LEVEL, which must be 0 here).
Expand Down Expand Up @@ -546,6 +582,23 @@ void intersect(VkRect2D& result, const Registers& registers, std::uint32_t offse

}

std::array<VkSampleLocationEXT, 8> SampleLocations(const State& state) {
Require(state.rasterSamples == 1 || state.rasterSamples == 2 || state.rasterSamples == 4 || state.rasterSamples == 8, "invalid sample count");
std::array<VkSampleLocationEXT, 8> locations{};
for (std::uint32_t sample = 0; sample < state.rasterSamples; ++sample) {
std::array<int, 2> position{};
if (state.customSampleLocations) {
const auto bits = state.sampleLocations[sample];
position = {static_cast<int>(bits & 7u) - static_cast<int>(bits & 8u), static_cast<int>((bits >> 4u) & 7u) - static_cast<int>((bits >> 4u) & 8u)};
} else if (state.rasterSamples != 1) {
const auto& standard = standardLocation(state.rasterSamples, sample);
position = {standard[0], standard[1]};
}
locations[sample] = {0.5f + static_cast<float>(position[0]) / 16.0f, 0.5f + static_cast<float>(position[1]) / 16.0f};
}
return locations;
}

ShaderStages DecodeShaderStages(const QueueState& queue, bool vertexWorkgroup) {
const auto value = read(queue.context, 0x2d5);
const auto validate = [&](bool condition, const char* reason) {
Expand Down Expand Up @@ -690,6 +743,15 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) {
if (!result.alphaToCoverage) zero(cx, 0x2dc, AlphaToCoverageMask, "dithered or rounded alpha-to-coverage");
zero(cx, 0x2f8, AaConfigMask, "coverage conversion or over-8x multisampling");
result.rasterSamples = rasterSamples(cx);
const auto guestSamples = result.rasterSamples;
// Multisampling with MSAA_ENABLE clear rasterizes every sample at the pixel center (upstream refuses
// it): a custom pattern with all samples at (0, 0), which suits a pass of any sample count.
if (const auto mode = find(cx, 0x292); guestSamples > 1 && mode != cx.end() && (mode->second & 1u) == 0) {
result.sampleLocations = {};
result.customSampleLocations = true;
} else {
decodeSampleLocations(cx, guestSamples, result);
}
// MSAA_ENABLE on a single-sample scan is accepted when its samples sit at the pixel center (see singleSampleMsaaRejection).
zero(cx, 0x292, ScanModeMask & ~1u, "scan conversion mode");
const auto scanMode = find(cx, 0x292);
Expand Down Expand Up @@ -769,6 +831,11 @@ State DecodeState(const QueueState& queue, bool vertexWorkgroup) {
}
result.rasterSamples = AttachmentSampleCount(result.rasterSamples, result.depth ? result.depth->samples : 0u, result.colors);
Require(!singleSampleMsaa || result.rasterSamples == 1, "MSAA_ENABLE on a single-sample scan into multisampled targets is unsupported");
// The pattern is PA_SC_AA_CONFIG's; a pass whose attachments fix another sample count (EQAA depth,
// see AttachmentSampleCount) rasterizes at that count's standard locations, which a custom pattern
// does not describe.
const bool centered = std::all_of(result.sampleLocations.begin(), result.sampleLocations.end(), [](std::uint8_t location) { return location == 0; });
Require(!result.customSampleLocations || centered || result.rasterSamples == guestSamples, "custom sample locations on a pass whose attachments fix another sample count are unsupported");
if (result.hasColorTarget) {
result.color = result.colors.front();
if (result.depth) result.renderExtent = {std::min(result.renderExtent.width, result.depth->extent.width), std::min(result.renderExtent.height, result.depth->extent.height)};
Expand Down
Loading
Loading