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 @@ -12,6 +12,14 @@ struct RegisterStateKeyParts {
std::uint64_t auxiliaryRegisters = 0;
};

struct RegisterFingerprints {
std::uint64_t context = 0;
std::uint64_t shader = 0;
std::uint64_t userConfig = 0;
bool operator==(const RegisterFingerprints&) const = default;
};

RegisterFingerprints DrawKeyFingerprints(const QueueState& queue);
std::uint64_t RegisterStateKey(const QueueState& queue, bool allUserWords = false, bool incremental = true, std::uint64_t* shapeKey = nullptr, RegisterStateKeyParts* parts = nullptr);
std::uint64_t DrawShaderSetKey(const QueueState& queue, const ShaderRegistry& registry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ inline std::uint32_t ThreadDimensionGroups(std::uint32_t threads, std::uint32_t
struct DrawTail;
struct SplitTableCallParts;

struct PrecheckMemoCounts {
std::uint64_t hits = 0;
std::uint64_t records = 0;
};

class Driver {
public:
static Driver& Get();
Expand Down Expand Up @@ -252,6 +257,8 @@ class Driver {
static void recordStageDataPositions(DispatchVariant& variant, const ShaderRecompiler::ResourceCapture& capture, const std::vector<Graphics::DecodeRead>& decodeReads);
void cacheDrawStages(bool useDrawEntries, bool drawHit, const Pm4::DrawParameters& drawParameters, const std::optional<Graphics::IndirectDrawPath>& indirectCpu, const std::vector<DrawProgram>& programs, const std::vector<StageCapture>& stageCaptures, const std::vector<std::optional<ShaderRecompiler::ShaderVertexStageInfo>>& vertexInfos, const std::vector<std::vector<Graphics::DecodeRead>>& decodeReads, bool verifyHit, const std::vector<std::shared_ptr<DispatchVariant>>& matched, const std::vector<StageDataHit>& stageData, std::vector<std::shared_ptr<DispatchVariant>>& fresh, std::uint64_t drawKey, bool registerKey, const std::shared_ptr<const DrawDecode>& decode, DrawPhaseTiming& phaseTiming, const std::vector<bool>& reused, const std::shared_ptr<DrawEntry>& entry, std::uint64_t shapeKey, bool shapeNeedsEntry = false);
static bool drawPrecheck();
static PrecheckMemoCounts precheckMemoCounts();
static void resetPrecheckMemo();
std::optional<DrawVerdict> precheckDraw(const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::span<const std::uint32_t> packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect);
static std::uint32_t drawUserWord(const DrawProgram& program, std::int32_t sgpr);
DrawVerdict draw(QueueState& queue, std::span<const std::uint32_t> packet, const Submission& submission, std::size_t packetOffset, std::string& rejected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawRegisterKey.hpp"
#include "prx/libc/include/HostThreadLocal.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp"
#include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp"
Expand Down Expand Up @@ -46,15 +48,78 @@ QueueState DepthCopyDraw(const QueueState& queue, const Graphics::DepthCopyPass&
return draw;
}

struct RestartInputs {
std::uint32_t indexSize = 0;
std::uint32_t resetIndex = 0;
bool resetPresent = false;
bool operator==(const RestartInputs&) const = default;
};

struct PrecheckMemo {
RegisterFingerprints state;
RestartInputs restart;
bool restartKnown = false;
bool valid = false;
PrecheckMemoCounts counts;
};

struct PrecheckMemoStorage {};

PrecheckMemo& precheckMemo() {
return HostThreadLocal<PrecheckMemo, PrecheckMemoStorage>();
}

bool precheckMemoEnabled() {
static const bool enabled = std::getenv("APS5_NO_PRECHECK_MEMO") == nullptr;
return enabled;
}

bool primitiveRestartEnabled(const QueueState& queue) {
const auto restart = queue.userConfig.find(0x24b);
return restart != queue.userConfig.end() && restart->second != 0;
}

RestartInputs restartInputs(const QueueState& queue, const Pm4::DrawParameters& drawParameters) {
const auto reset = queue.context.find(0x103);
const bool present = reset != queue.context.end();
return {drawParameters.indexSize, present ? reset->second : 0u, present};
}

}

bool Driver::drawPrecheck() {
static const bool precheck = std::getenv("APS5_NO_DRAW_PRECHECK") == nullptr;
return precheck;
}

PrecheckMemoCounts Driver::precheckMemoCounts() {
return precheckMemo().counts;
}

void Driver::resetPrecheckMemo() {
precheckMemo() = {};
}

std::optional<DrawVerdict> Driver::precheckDraw(const QueueState& queue, const Submission& submission, std::size_t packetOffset, std::span<const std::uint32_t> packet, const Pm4::DrawParameters& drawParameters, std::string& rejected, bool& traceIndirect) {
if (!drawParameters.indirect && !drawParameters.indexed && (drawParameters.indexCount == 0 || drawParameters.instanceCount == 0)) return DrawVerdict::Nothing;
static const bool traceIndirectEnabled = std::getenv("APS5_TRACE_INDIRECT_DRAWS") != nullptr;
const auto reached = [&] {
traceIndirect = traceIndirectEnabled;
if (traceIndirect && drawParameters.indirect) std::fprintf(stderr, "[draw] indirect packet %s args 0x%llx count %u reached\n", Pm4::Name(packet[0]).c_str(), static_cast<unsigned long long>(drawParameters.indirect->arguments), drawParameters.indirect->count);
};
PrecheckMemo* const memo = precheckMemoEnabled() ? &precheckMemo() : nullptr;
RegisterFingerprints fingerprints;
bool restartDecides = false;
if (memo != nullptr) {
fingerprints = DrawKeyFingerprints(queue);
restartDecides = drawParameters.indexed && primitiveRestartEnabled(queue);
if (memo->valid && memo->state == fingerprints && (!restartDecides || (memo->restartKnown && memo->restart == restartInputs(queue, drawParameters)))) {
++memo->counts.hits;
rejected.clear();
reached();
return std::nullopt;
}
}
const auto metadata = Graphics::DecodeColorMetadataPass(queue);
std::optional<Graphics::DepthExpandPass> expand;
if (!metadata) expand = Graphics::DecodeDepthExpandPass(queue);
Expand Down Expand Up @@ -99,16 +164,15 @@ std::optional<DrawVerdict> Driver::precheckDraw(const QueueState& queue, const S
auto copyDraw = DepthCopyDraw(queue, *copy);
return draw(copyDraw, packet, submission, packetOffset, rejected);
}
if (const auto control = Graphics::DecodeDepthRenderControl(queue); control && Graphics::DepthRenderControlDrains(*control)) {
const auto control = Graphics::DecodeDepthRenderControl(queue);
if (control && Graphics::DepthRenderControlDrains(*control)) {
if (DrawPipeline::Active()) DrawPipeline::Queue0().Drain(DrawPipeline::DrainReason::Packet, (packet[0] >> 8u) & 0xffu, "depth_render_control");
Graphics::NoteDepthRenderControl(*control);
}
if (Graphics::DepthResummarizeDrawsNothing(queue)) return DrawVerdict::Nothing;
rejected = Graphics::DepthMaintenanceRejection(queue);
if (!rejected.empty()) return DrawVerdict::Rejected;
static const bool traceIndirectEnabled = std::getenv("APS5_TRACE_INDIRECT_DRAWS") != nullptr;
traceIndirect = traceIndirectEnabled;
if (traceIndirect && drawParameters.indirect) std::fprintf(stderr, "[draw] indirect packet %s args 0x%llx count %u reached\n", Pm4::Name(packet[0]).c_str(), static_cast<unsigned long long>(drawParameters.indirect->arguments), drawParameters.indirect->count);
reached();
{
const auto colorControl = queue.context.find(0x202);
const bool colorDisabled = colorControl != queue.context.end() && ((colorControl->second >> 4u) & 7u) == 0u;
Expand Down Expand Up @@ -143,6 +207,13 @@ std::optional<DrawVerdict> Driver::precheckDraw(const QueueState& queue, const S
rejected = Graphics::DrawRejection(queue, drawParameters.indexed);
if (!rejected.empty()) return DrawVerdict::Rejected;
}
if (memo != nullptr && !control) {
memo->state = fingerprints;
memo->restartKnown = restartDecides;
memo->restart = restartDecides ? restartInputs(queue, drawParameters) : RestartInputs{};
memo->valid = true;
++memo->counts.records;
}
return std::nullopt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ void forEachKeyedUserWord(const QueueState& queue, bool allUserWords, TVisit&& v

}

RegisterFingerprints DrawKeyFingerprints(const QueueState& queue) {
const auto& masks = registerMasks();
return {
queue.context.Fingerprint(masks[static_cast<std::size_t>(Graphics::RegisterBank::Context)]),
queue.shader.Fingerprint(masks[static_cast<std::size_t>(Graphics::RegisterBank::Shader)]),
queue.userConfig.Fingerprint(masks[static_cast<std::size_t>(Graphics::RegisterBank::UserConfig)]),
};
}

std::uint64_t RegisterStateKey(const QueueState& queue, bool allUserWords, bool incremental, std::uint64_t* shapeKey, RegisterStateKeyParts* parts) {
const auto& masks = registerMasks();
const auto fingerprint = [&](const Registers& registers, Graphics::RegisterBank bank) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if(BUILD_TESTING AND TARGET agc_driver_precheck_memo_tests)
set_tests_properties(agc_driver_precheck_memo PROPERTIES TIMEOUT 180)
set_property(TEST agc_driver_precheck_memo APPEND PROPERTY ENVIRONMENT_MODIFICATION "APS5_NO_PRECHECK_MEMO=unset:")
add_test(NAME agc_driver_precheck_memo_off COMMAND agc_driver_precheck_memo_tests)
set_tests_properties(agc_driver_precheck_memo_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PRECHECK_MEMO=1" SKIP_RETURN_CODE 77 TIMEOUT 180 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
if(WIN32)
set_tests_properties(agc_driver_precheck_memo_off PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
Loading
Loading