Skip to content
Open
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 @@ -643,6 +643,7 @@ DrawVerdict Driver::draw(QueueState& queue, std::span<const std::uint32_t> packe
vertexInfos[i]->paClVsOutCntl = PositionExportLayout(program.snapshot.get(), program.codeOffset, program.binary.code, readRegister(queue.context, 0x207), program.tailContinuation.get()) | graphics.layerExports | graphics.viewportExports;
vertexInfos[i]->depthRemapMode = graphics.depthRemap.mode;
Graphics::ApplyPointSize(graphics, *vertexInfos[i]);
Graphics::ApplyInstanceStepRate(graphics, *vertexInfos[i]);
}
};
if (!registerKey) {
Expand Down Expand Up @@ -905,6 +906,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) {
vertexInfos[i]->paClVsOutCntl = PositionExportLayout(program.snapshot.get(), program.codeOffset, program.binary.code, tail.reg207, program.tailContinuation.get()) | graphics.layerExports | graphics.viewportExports;
vertexInfos[i]->depthRemapMode = graphics.depthRemap.mode;
Graphics::ApplyPointSize(graphics, *vertexInfos[i]);
Graphics::ApplyInstanceStepRate(graphics, *vertexInfos[i]);
}
};
const auto fold = [&](const ShaderRecompiler::RecompileResult& main, Pm4::DrawParameters& parameters) { FoldDrawOffsets(main, programs.front(), parameters); };
Expand Down Expand Up @@ -1525,6 +1527,7 @@ void Driver::restartFinishedDraw(DrawTail& tail) {
tail.vertexInfos[i]->paClVsOutCntl = PositionExportLayout(program.snapshot.get(), program.codeOffset, program.binary.code, tail.reg207, program.tailContinuation.get()) | graphics.layerExports | graphics.viewportExports;
tail.vertexInfos[i]->depthRemapMode = graphics.depthRemap.mode;
Graphics::ApplyPointSize(graphics, *tail.vertexInfos[i]);
Graphics::ApplyInstanceStepRate(graphics, *tail.vertexInfos[i]);
}
tail.shaderMemory = std::make_unique<ShaderMemory>(tail.memory, &queryPendingWrite, &observePendingWrite, hookWaitCounter(), &QueryPendingSpan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void Driver::lookupDraw(const Submission& submission, const std::shared_ptr<Vulk
mix(std::bit_cast<std::uint32_t>(vertex.pointSize));
mix(std::bit_cast<std::uint32_t>(vertex.pointSizeMin));
mix(std::bit_cast<std::uint32_t>(vertex.pointSizeMax));
mix(vertex.instanceStepRate);
for (std::uint32_t r = 0; r < vertex.resourcesNum; ++r) {
for (const auto field : vertex.resources[r].fields) mix(field);
const auto& destination = vertex.resourcesDst[r];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void Driver::traceDrawKeyLookup(const std::array<std::uint64_t, 5>& components,
}

bool Driver::sameVertexInfo(const ShaderRecompiler::ShaderVertexStageInfo& a, const ShaderRecompiler::ShaderVertexStageInfo& b) {
if (a.resourcesNum != b.resourcesNum || a.fetchAttribReg != b.fetchAttribReg || a.fetchBufferReg != b.fetchBufferReg || a.fetchEmbedded != b.fetchEmbedded || a.paClVsOutCntl != b.paClVsOutCntl || a.pointList != b.pointList || a.pointSize != b.pointSize || a.pointSizeMin != b.pointSizeMin || a.pointSizeMax != b.pointSizeMax) return false;
if (a.resourcesNum != b.resourcesNum || a.fetchAttribReg != b.fetchAttribReg || a.fetchBufferReg != b.fetchBufferReg || a.fetchEmbedded != b.fetchEmbedded || a.paClVsOutCntl != b.paClVsOutCntl || a.pointList != b.pointList || a.pointSize != b.pointSize || a.pointSizeMin != b.pointSizeMin || a.pointSizeMax != b.pointSizeMax || a.instanceStepRate != b.instanceStepRate) return false;
if (a.depthRemapMode != b.depthRemapMode) return false;
for (std::uint32_t i = 0; i < a.resourcesNum && i < a.resources.size(); ++i) {
if (a.resources[i].fields != b.resources[i].fields) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,7 @@ std::vector<PreparedGraphicsStage> PrepareGraphicsStages(const DrawDecode& decod
vertex->paClVsOutCntl = PositionExportLayout(program.snapshot.get(), program.codeOffset, program.binary.code, decoded.state.paClVsOutCntl, program.tailContinuation.get()) | decoded.state.layerExports | decoded.state.viewportExports;
vertex->depthRemapMode = decoded.state.depthRemap.mode;
Graphics::ApplyPointSize(decoded.state, *vertex);
Graphics::ApplyInstanceStepRate(decoded.state, *vertex);
}
ShaderRecompiler::RecompileRequest request{program.binary, {wave, program.firstUserSgpr, program.userData, {}, fragment ? std::optional(decoded.pixel) : std::nullopt, vertex, memory, RegisteredFloatMode(*program.snapshot)}, target, {0, 0, offset, room}, ShaderRecompiler::GraphicsCompileContext{program.firstUserSgpr, linked, Graphics::StageMeshConfiguration(decoded.state.stages, program.binary.stage), decoded.state.stages.tessellation, {}}};
std::vector<std::uint64_t> key;
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct ShaderStages {
std::uint32_t hullWaveSize = 64;
std::optional<ShaderRecompiler::MeshConfiguration> tessellationGeometry;
bool legacyVertex = false;
std::uint32_t instanceStepRate = 1;
};

inline constexpr std::uint16_t UnclampedBlendMask = 0xffffu;
Expand Down Expand Up @@ -245,6 +246,7 @@ VkLogicOp LogicOpFromRop(std::uint8_t rop);

ShaderStages DecodeShaderStages(const QueueState& queue, bool vertexWorkgroup = false);
void ApplyPointSize(const State& state, ShaderRecompiler::ShaderVertexStageInfo& vertex);
void ApplyInstanceStepRate(const State& state, ShaderRecompiler::ShaderVertexStageInfo& vertex);
State DecodeState(const QueueState& queue, bool vertexWorkgroup = false);
void ApplyDepthRemap(const Context& context, State& state);
void ApplyViewportIndex(const Context& context, State& state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static_assert(sizeof(VkVertexInputBindingDescription) == 12, "VkVertexInputBindi
static_assert(sizeof(VkVertexInputAttributeDescription) == 16, "VkVertexInputAttributeDescription changed: update the recipe codec");
#if defined(__linux__) && defined(__x86_64__) && defined(__GLIBCXX__)
static_assert(sizeof(State) == 1008, "State changed: update CanonicalPipelineState and the recipe codec");
static_assert(sizeof(ShaderStages) == 164, "ShaderStages changed: update the recipe codec");
static_assert(sizeof(ShaderStages) == 168, "ShaderStages changed: update the recipe codec");
static_assert(sizeof(ShaderRecompiler::MeshConfiguration) == 40, "MeshConfiguration changed: update the recipe codec");
static_assert(sizeof(ShaderRecompiler::TessellationConfiguration) == 40, "TessellationConfiguration changed: update the recipe codec");
#endif
Expand Down
14 changes: 11 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ std::array<std::uint8_t, 32> PixelSampleLocations(const State& state, const Shad
return locations;
}

void ApplyInstanceStepRate(const State& state, ShaderRecompiler::ShaderVertexStageInfo& vertex) {
vertex.instanceStepRate = state.stages.legacyVertex ? state.stages.instanceStepRate : 1u;
}

void ApplyPointSize(const State& state, ShaderRecompiler::ShaderVertexStageInfo& vertex) {
if (!state.vertexPoints && !(state.stages.mesh.has_value() && state.stages.mesh->outputPrimitiveSize == 1u)) return;
vertex.paClVsOutCntl |= state.paClVsOutCntl & (1u << 16u);
Expand Down Expand Up @@ -1390,12 +1394,16 @@ ShaderStages DecodeShaderStages(const QueueState& queue, bool vertexWorkgroup) {
};
if (path == ShaderPath::Vertex && ShaderRecompiler::GcnGuestShaders() && (value & 0x2000u) == 0) {
validate(value == 0, "GCN vertex routing other than a plain VS is unsupported");
const auto unitStepRate = [&](std::uint32_t offset) {
const auto stepRate = [&](std::uint32_t offset) {
const auto rate = find(queue.context, offset);
return rate == queue.context.end() || rate->second == 1u;
return rate == queue.context.end() ? 1u : rate->second;
};
const auto components = (read(queue.shader, 0x4a, RegisterBank::Shader) >> 24u) & 3u;
validate(components == 0 || (unitStepRate(0x2a8) && (components == 1 || unitStepRate(0x2a9))), "GCN vertex instance step rates other than 1 are not implemented");
if (components != 0) {
result.instanceStepRate = stepRate(0x2a8);
validate(result.instanceStepRate != 0, "GCN vertex instance step rate 0 of VGT_INSTANCE_STEP_RATE_0 is not implemented");
}
validate(components < 2 || stepRate(0x2a9) == 1u, "GCN vertex instance step rates other than 1 in VGT_INSTANCE_STEP_RATE_1 are not implemented");
result.legacyVertex = true;
} else if (path == ShaderPath::Vertex) {
validate((value & 0x2000u) != 0, "legacy vertex routing without PRIMGEN_EN is unsupported");
Expand Down
62 changes: 62 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4019,6 +4019,67 @@ void gcnColorSliceTests() {
expectFailure([&] { static_cast<void>(DecodeColorBuffer(queue.context, 0)); }, "the color view ends before its first slice");
}

void gcnInstanceStepRateTests() {
using AgcDriver::Graphics::DecodeShaderStages;
ShaderRecompiler::SetGcnGuestShaders(true);
struct Restore {
~Restore() { ShaderRecompiler::SetGcnGuestShaders(false); }
} restore;
const auto legacy = [](std::uint32_t components) {
auto queue = makeState();
queue.context[0x2d5] = 0;
queue.shader[0x4a] = components << 24u;
return queue;
};
Require(DecodeShaderStages(legacy(1)).legacyVertex && DecodeShaderStages(legacy(1)).instanceStepRate == 1u, "a PS4 vertex program without VGT_INSTANCE_STEP_RATE_0 does not step once per instance");
for (const std::uint32_t rate : {1u, 2u, 3u, 0xffffffffu}) {
for (const std::uint32_t components : {1u, 2u, 3u}) {
auto queue = legacy(components);
queue.context[0x2a8] = rate;
const auto stages = DecodeShaderStages(queue);
Require(stages.legacyVertex && stages.instanceStepRate == rate, "a PS4 vertex program that reads its instance does not take VGT_INSTANCE_STEP_RATE_0");
AgcDriver::Graphics::State state{};
state.stages = stages;
ShaderRecompiler::ShaderVertexStageInfo vertex{};
AgcDriver::Graphics::ApplyInstanceStepRate(state, vertex);
Require(vertex.instanceStepRate == rate, "the vertex stage info does not carry VGT_INSTANCE_STEP_RATE_0");
}
}
auto unread = legacy(0);
unread.context[0x2a8] = 0;
unread.context[0x2a9] = 5;
Require(DecodeShaderStages(unread).instanceStepRate == 1u, "a PS4 vertex program that reads no instance takes the instance step rates");
auto zero = legacy(1);
zero.context[0x2a8] = 0;
expectFailure([&] { static_cast<void>(DecodeShaderStages(zero)); }, "GCN vertex instance step rate 0 of VGT_INSTANCE_STEP_RATE_0 is not implemented");
auto second = legacy(1);
second.context[0x2a9] = 4;
Require(DecodeShaderStages(second).instanceStepRate == 1u, "a PS4 vertex program that reads one instance VGPR refuses VGT_INSTANCE_STEP_RATE_1");
for (const std::uint32_t components : {2u, 3u}) {
auto queue = legacy(components);
queue.context[0x2a8] = 2;
queue.context[0x2a9] = 4;
expectFailure([&] { static_cast<void>(DecodeShaderStages(queue)); }, "GCN vertex instance step rates other than 1 in VGT_INSTANCE_STEP_RATE_1 are not implemented");
}
AgcDriver::Graphics::State agc{};
agc.stages.instanceStepRate = 2;
ShaderRecompiler::ShaderVertexStageInfo vertex{};
vertex.instanceStepRate = 7;
AgcDriver::Graphics::ApplyInstanceStepRate(agc, vertex);
Require(vertex.instanceStepRate == 1u, "a vertex program outside the PS4 legacy path takes an instance step rate");
ShaderRecompiler::RecompileRequest request{};
const std::array<std::uint32_t, 1> code{0xbf810000u};
request.shader = {ShaderRecompiler::ShaderStage::Vertex, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.vertex = ShaderRecompiler::ShaderVertexStageInfo{};
const auto unitHash = ShaderRecompiler::RecompileCacheKey::ContextHash(request);
request.context.vertex->instanceStepRate = 3;
Require(ShaderRecompiler::RecompileCacheKey::ContextHash(request) != unitHash, "VGT_INSTANCE_STEP_RATE_0 does not key the vertex program's variant");
const ShaderRecompiler::RequestSerializer serializer;
const auto back = serializer.Deserialize(serializer.Serialize(request));
Require(back.request.context.vertex->instanceStepRate == 3u && back.vertex->instanceStepRate == 3u, "VGT_INSTANCE_STEP_RATE_0 did not survive request serialization");
}

void cmaskTests() {
using AgcDriver::Graphics::DecodeColorBuffer;
using AgcDriver::Graphics::CmaskLayout;
Expand Down Expand Up @@ -16151,6 +16212,7 @@ int main(int argc, char** argv) {
metadataPassTests();
depthCopyPassTests();
cmaskTests();
gcnInstanceStepRateTests();
gcnColorSliceTests();
uint16ExportTests();
uint8x2TargetTests();
Expand Down
21 changes: 11 additions & 10 deletions core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8957,7 +8957,7 @@ void verifyPixelRequestSerialization() {
minimal.context.pixel->layerExported = true;
minimal.context.pixel->ditheredAlphaToCoverage = true;
const auto encoded = serializer.Serialize(minimal);
require(requestPrefix(encoded, 8u) == "NVNQQT8AAAA=", "new requests did not use serialization version 63");
require(requestPrefix(encoded, 8u) == "NVNQQUAAAAA=", "new requests did not use serialization version 64");
auto withBarycentric = minimal;
withBarycentric.target.fragmentBarycentricBuiltins = true;
require(serializer.Deserialize(serializer.Serialize(withBarycentric)).request.target.fragmentBarycentricBuiltins && !serializer.Deserialize(encoded).request.target.fragmentBarycentricBuiltins, "barycentric built-ins were lost in serialization");
Expand Down Expand Up @@ -9078,7 +9078,7 @@ void verifyPixelRequestSerialization() {
for (std::size_t bytes = 0; bytes < 45u; ++bytes) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(requestPrefix(encoded, mappingOffset + bytes))); }, "truncated data", "a truncated pixel mapping, packing, dual-source, alpha-to-coverage, stencil reference export flag, primitive class, layer export, dither or front-face flag, quad pixel mask or quad sample mask was accepted");
}
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQTIAAAA=", "NVNQQToAAAA=", "NVNQQT0AAAA=", "NVNQQUAAAAA="}) {
for (const auto unsupported : {"NVNQQQAAAAA=", "NVNQQTIAAAA=", "NVNQQToAAAA=", "NVNQQT0AAAA=", "NVNQQUEAAAA="}) {
expectFailure([&] { static_cast<void>(serializer.Deserialize(unsupported)); }, "serialization version", "an unsupported request version was accepted");
}
static constexpr std::string_view version18Request{
Expand Down Expand Up @@ -9135,13 +9135,6 @@ std::vector<std::uint8_t> decodeBase64(std::string_view text) {
return bytes;
}

std::vector<std::uint8_t> version56Bytes(std::vector<std::uint8_t> bytes) {
require(bytes.size() > 14u && bytes[4] == 63u, "a legacy fixture did not start from serialization version 63");
bytes.resize(bytes.size() - 6u);
bytes[4] = 56u;
return bytes;
}

std::string encodeBase64(const std::vector<std::uint8_t>& bytes) {
constexpr std::string_view alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string text;
Expand All @@ -9157,6 +9150,14 @@ std::string encodeBase64(const std::vector<std::uint8_t>& bytes) {
return text;
}

std::vector<std::uint8_t> version56Bytes(std::vector<std::uint8_t> bytes) {
require(bytes.size() > 14u && bytes[4] == 64u, "a legacy fixture did not start from serialization version 64");
const bool vertex = ShaderRecompiler::RequestSerializer{}.Deserialize(encodeBase64(bytes)).request.context.vertex.has_value();
bytes.resize(bytes.size() - 6u - (vertex ? 4u : 0u));
bytes[4] = 56u;
return bytes;
}

void verifyEmulatedBarycentricRequests() {
using namespace ShaderRecompiler;
const std::array<std::uint32_t, 1> code{0xbf810000u};
Expand Down Expand Up @@ -9198,7 +9199,7 @@ void verifyEmulatedBarycentricRequests() {
require(replay.request.graphics.has_value() && replay.request.graphics->emulateBarycentrics && serializer.Serialize(replay.request) == encoded, "emulated barycentrics were lost in serialization");
auto bytes = decodeBase64(serializer.Serialize(request));
auto flagged = decodeBase64(encoded);
require(bytes[4] == 63u && flagged.size() == bytes.size() + 1u && flagged.back() == 1u && std::equal(bytes.begin(), bytes.end(), flagged.begin()), "emulated barycentrics are not one trailing byte after an otherwise identical request");
require(bytes[4] == 64u && flagged.size() == bytes.size() + 1u && flagged.back() == 1u && std::equal(bytes.begin(), bytes.end(), flagged.begin()), "emulated barycentrics are not one trailing byte after an otherwise identical request");
auto legacyBytes = bytes;
legacyBytes[4] = 62u;
const auto legacy = serializer.Deserialize(encodeBase64(legacyBytes));
Expand Down
Loading
Loading