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 @@ -47,7 +47,12 @@ class DrawPipeline {
static std::size_t Depth();
static std::size_t ResumeDepth();
static std::size_t QueuedItems();
static bool& Active();
class ActiveFlag {
public:
operator bool() const;
ActiveFlag& operator=(bool active);
};
static ActiveFlag Active() { return {}; }
static std::atomic<std::uint64_t>& EpochToken();
static void FollowEpoch(std::uint64_t token);
static void NoteEpochSince(std::uint64_t token, std::uint64_t since);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp"
#include "prx/libSceAgcDriver/Execution/include/TracyZone.hpp"
#include "prx/libSceAgcDriver/Execution/include/Mutex.hpp"
#include "prx/libc/include/HostThreadSlot.hpp"
#include <algorithm>
#include <chrono>
#include <cstddef>
Expand Down Expand Up @@ -239,23 +240,25 @@ class FrameTiming {

class PerformanceContext {
public:
explicit PerformanceContext(FrameTiming* frame) : previous(current) {
current = frame;
explicit PerformanceContext(FrameTiming* frame) : previous(Current()) {
CurrentSlot::Set(frame);
}

PerformanceContext(const PerformanceContext&) = delete;
PerformanceContext& operator=(const PerformanceContext&) = delete;

~PerformanceContext() {
current = previous;
CurrentSlot::Set(previous);
}

static FrameTiming* Current() {
return current;
return CurrentSlot::Get();
}

private:
inline static thread_local FrameTiming* current = nullptr;
struct CurrentTag {};
using CurrentSlot = HostThreadSlot<FrameTiming*, CurrentTag>;

FrameTiming* previous;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Synchronization/DeferredLabels.hpp"
#include "ThreadOwned.hpp"
#include "Optimization/SrtWalker.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include <algorithm>
#include <cstdlib>
#include <cstring>
Expand All @@ -14,6 +14,13 @@ namespace {

enum class QueuedOverlap : std::uint8_t { None, Stored, Unknown };

struct VariantScratch {
std::vector<VouchedRegions> vouched;
std::vector<std::pair<std::uint64_t, std::uint64_t>> checked;
};

struct VariantScratchTag {};

QueuedOverlap QueuedWritesOver(DrawPipeline& pipeline, std::span<const ShaderRecompiler::MemoryRegion> regions) {
auto answer = QueuedOverlap::None;
for (const auto& region : regions) {
Expand Down Expand Up @@ -129,15 +136,14 @@ EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue,
}
const bool masked = live != nullptr && !variant.dataPositions.empty();
const DataMask mask{variant.dataPositions, live};
thread_local std::vector<VouchedRegions>* vouchedSlot = nullptr;
auto& vouched = ShaderRecompiler::ThreadOwned(vouchedSlot);
auto& scratch = ThreadScratch<VariantScratch, VariantScratchTag>();
auto& vouched = scratch.vouched;
vouched.clear();
if (!variant.shared.empty() && vouchedValidation() && !validateLegacy() && variant.runs.size() == regions.size()) VouchSharedRegions(variant.shared, regions.size(), &QueryPendingSpan, vouched, variant.linking != nullptr ? std::span(variant.linking->dependencies) : std::span<const std::shared_ptr<const ShaderRecompiler::SrtGuestWords>>{}, &PreparedShaderState::UserDataLink::Restamp);
PendingView pending;
pending.Load();
std::erase_if(vouched, [&](const VouchedRegions& range) { return pending.Overlaps(range.low, static_cast<std::size_t>(range.high - range.low)); });
thread_local std::vector<std::pair<std::uint64_t, std::uint64_t>>* checkedSlot = nullptr;
auto& checked = ShaderRecompiler::ThreadOwned(checkedSlot);
auto& checked = scratch.checked;
checked.clear();
if (!vouched.empty()) {
for (std::size_t index = 0, range = 0; index < variant.runs.size(); ++index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include "prx/libSceAgcDriver/Execution/include/PerformanceTrace.hpp"
#include "prx/libSceAgcDriver/Execution/include/ProfileCounter.hpp"
#include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include "prx/libSceAgcDriver/Execution/include/WorkerSampler.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "prx/libSceAgcDriver/Graphics/include/RuntimeSwitches.hpp"
#include "prx/libc/include/HostThreadLocal.hpp"
#include "prx/libc/include/HostThreadSlot.hpp"
#include <algorithm>
#include <array>
#include <limits>
Expand Down Expand Up @@ -67,10 +69,23 @@ struct ClearOnExit {
}

namespace {
thread_local bool finisherThread = false;
thread_local bool finisherProducer = false;
thread_local DrawPipeline* committerPipeline = nullptr;

struct DrawThreadFlagsTag {};
using DrawThreadFlags = HostThreadSlot<std::uint8_t, DrawThreadFlagsTag>;
constexpr std::uint8_t ActiveBit = 1u;
constexpr std::uint8_t FinisherThreadBit = 2u;
constexpr std::uint8_t FinisherProducerBit = 4u;

bool DrawThreadFlag(std::uint8_t bit) {
return (DrawThreadFlags::Get() & bit) != 0;
}

void SetDrawThreadFlag(std::uint8_t bit, bool on) {
const auto flags = DrawThreadFlags::Get();
DrawThreadFlags::Set(static_cast<std::uint8_t>(on ? flags | bit : flags & ~bit));
}

std::uint64_t CurrentThreadId() {
#if defined(__linux__)
return static_cast<std::uint64_t>(::syscall(SYS_gettid));
Expand Down Expand Up @@ -114,7 +129,7 @@ bool DrawFinisher::Overlap() {
}

bool DrawFinisher::PendingOverlaps(std::uint64_t begin, std::uint64_t end) {
if (!Busy() || !finisherProducer) return false;
if (!Busy() || !DrawThreadFlag(FinisherProducerBit)) return false;
std::lock_guard lock(mutex);
for (const auto& held : pending) {
if (held.unknown) return true;
Expand All @@ -126,7 +141,7 @@ bool DrawFinisher::PendingOverlaps(std::uint64_t begin, std::uint64_t end) {
}

DrawFinisher::LabelLookup DrawFinisher::PendingLabel(std::uint64_t address, std::size_t bytes, std::uint64_t& value) {
if (!Busy() || !finisherProducer) return LabelLookup::NotHeld;
if (!Busy() || !DrawThreadFlag(FinisherProducerBit)) return LabelLookup::NotHeld;
const auto end = address + bytes;
std::lock_guard lock(mutex);
for (auto held = pending.rbegin(); held != pending.rend(); ++held) {
Expand Down Expand Up @@ -217,7 +232,7 @@ bool DrawFinisher::pendingBlocksClear(std::span<const Range> ranges) const {
}

bool DrawFinisher::PendingOverlapsAny(std::span<const Range> ranges) {
if (!Busy() || !finisherProducer) return false;
if (!Busy() || !DrawThreadFlag(FinisherProducerBit)) return false;
std::lock_guard lock(mutex);
if (PendingWriteBlocks() && pendingBlocksClear(ranges)) {
pendingBlocksAnswered.fetch_add(1, std::memory_order_relaxed);
Expand All @@ -243,7 +258,7 @@ bool DrawFinisher::PendingOverlapsAny(std::span<const Range> ranges) {
}

bool DrawFinisher::PendingKey(std::uint64_t key, std::uint64_t shape) {
if (!Busy() || !finisherProducer) return false;
if (!Busy() || !DrawThreadFlag(FinisherProducerBit)) return false;
std::lock_guard lock(mutex);
return std::any_of(pending.begin(), pending.end(), [&](const Pending& held) { return held.key == key || (shape != 0 && held.shape == shape); });
}
Expand Down Expand Up @@ -289,15 +304,15 @@ std::string DrawFinisher::DrainText() {
}

bool DrawFinisher::OnFinisherThread() {
return finisherThread;
return DrawThreadFlag(FinisherThreadBit);
}

void DrawFinisher::Enqueue(Job job) {
Enqueue(std::move(job), Pending{});
}

void DrawFinisher::Enqueue(Job job, Pending held) {
finisherProducer = true;
SetDrawThreadFlag(FinisherProducerBit, true);
constexpr std::size_t depth = 16;
struct ReclaimStorage {};
auto& reclaim = HostThreadLocal<std::vector<Job>, ReclaimStorage>();
Expand All @@ -314,7 +329,7 @@ void DrawFinisher::Enqueue(Job job, Pending held) {
if (!running) {
running = true;
thread = std::thread([this] {
finisherThread = true;
SetDrawThreadFlag(FinisherThreadBit, true);
run();
});
}
Expand Down Expand Up @@ -402,9 +417,13 @@ std::size_t DrawPipeline::QueuedItems() {
return pipeline.items.size();
}

bool& DrawPipeline::Active() {
static thread_local bool active = false;
return active;
DrawPipeline::ActiveFlag::operator bool() const {
return DrawThreadFlag(ActiveBit);
}

DrawPipeline::ActiveFlag& DrawPipeline::ActiveFlag::operator=(bool active) {
SetDrawThreadFlag(ActiveBit, active);
return *this;
}

std::atomic<std::uint64_t>& DrawPipeline::EpochToken() {
Expand Down Expand Up @@ -461,23 +480,34 @@ void DrawPipeline::Enqueue(CommitKind kind, Commit commit, std::vector<Range> wr
enqueue({std::move(commit), std::move(writes), labelAddress, std::move(labelBytes), std::move(reads), draw, false, kind});
}

namespace {

constexpr std::size_t RecentSlots = 4096;

struct DrawKeyHistory {
std::array<std::uint64_t, RecentSlots> recent{};
std::uint64_t previous = 0;
std::array<std::uint64_t, RecentSlots> recentShapes{};
std::uint64_t previousShape = 0;
};

struct DrawKeyHistoryTag {};

}

void DrawPipeline::NoteDrawKey(std::uint64_t key, std::uint64_t shape) {
constexpr std::size_t RecentSlots = 4096;
thread_local std::array<std::uint64_t, RecentSlots> recent{};
thread_local std::uint64_t previous = 0;
thread_local std::array<std::uint64_t, RecentSlots> recentShapes{};
thread_local std::uint64_t previousShape = 0;
auto& history = ThreadScratch<DrawKeyHistory, DrawKeyHistoryTag>();
const auto shapeSlot = static_cast<std::size_t>((shape ^ (shape >> 29u)) % RecentSlots);
const auto slot = static_cast<std::size_t>((key ^ (key >> 29u)) % RecentSlots);
drawKeys.Add(1);
if (key == previous) drawKeysSameAsPrevious.Add(1);
if (recent[slot] == key) drawKeysSeenRecently.Add(1);
if (shape == previousShape) drawShapesSameAsPrevious.Add(1);
if (recentShapes[shapeSlot] == shape) drawShapesSeenRecently.Add(1);
recent[slot] = key;
previous = key;
recentShapes[shapeSlot] = shape;
previousShape = shape;
if (key == history.previous) drawKeysSameAsPrevious.Add(1);
if (history.recent[slot] == key) drawKeysSeenRecently.Add(1);
if (shape == history.previousShape) drawShapesSameAsPrevious.Add(1);
if (history.recentShapes[shapeSlot] == shape) drawShapesSeenRecently.Add(1);
history.recent[slot] = key;
history.previous = key;
history.recentShapes[shapeSlot] = shape;
history.previousShape = shape;
}

void DrawPipeline::EnqueueStore(Commit commit, std::uint64_t address, std::vector<std::byte> bytes) {
Expand Down Expand Up @@ -860,7 +890,7 @@ bool DrawPipeline::WrittenSince(Watch& watch, const std::vector<Range>& reads) {
}

bool DrawPipeline::Busy() const {
return outstanding.load(std::memory_order_acquire) != 0 || (finisherProducer && DrawFinisher::Busy() && DrawFinisher::Overlap());
return outstanding.load(std::memory_order_acquire) != 0 || (DrawThreadFlag(FinisherProducerBit) && DrawFinisher::Busy() && DrawFinisher::Overlap());
}

bool DrawPipeline::OverlapsAny(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "ThreadOwned.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include <cstdlib>
#include <cstring>

namespace AgcDriver::DriverDetail {

namespace {

struct CaptureScratch {
std::vector<std::byte> known;
std::vector<std::size_t> pending;
std::vector<VouchedRegions> skipped;
std::vector<std::byte> live;
std::vector<ShaderMemory::PendingWrite> policies;
std::vector<std::size_t> knownOffsets;
std::vector<std::byte> knownBytes;
};

struct CaptureScratchTag {};

}

bool Driver::captureStable(std::span<const ShaderRecompiler::MemoryRegion> captured) {
using Policy = ShaderMemory::PendingWrite;
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
Expand All @@ -15,8 +31,7 @@ bool Driver::captureStable(std::span<const ShaderRecompiler::MemoryRegion> captu
double waited = 0;
PendingView pending;
pending.Load();
thread_local std::vector<std::byte>* knownSlot = nullptr;
auto& known = ShaderRecompiler::ThreadOwned(knownSlot);
auto& known = ThreadScratch<CaptureScratch, CaptureScratchTag>().known;
for (const auto& region : captured) {
if (!stable) break;
known.resize(region.bytes.size());
Expand Down Expand Up @@ -60,11 +75,10 @@ bool Driver::captureStable(std::span<const ShaderRecompiler::MemoryRegion> captu
bool Driver::validateCaptured(std::uint64_t program, std::uint32_t queue, std::span<const ShaderRecompiler::MemoryRegion> captured, const ShaderRecompiler::RecompileResult& compiled, bool inPlace, const PendingView& view, bool* unmapped, std::optional<SampledReadScope>* sampling, const DataMask* data, std::span<const VouchedRegions> vouched) {
using Policy = ShaderMemory::PendingWrite;

thread_local std::vector<std::size_t>* pendingSlot = nullptr;
auto& pending = ShaderRecompiler::ThreadOwned(pendingSlot);
auto& scratch = ThreadScratch<CaptureScratch, CaptureScratchTag>();
auto& pending = scratch.pending;
pending.clear();
thread_local std::vector<VouchedRegions>* skippedSlot = nullptr;
auto& skipped = ShaderRecompiler::ThreadOwned(skippedSlot);
auto& skipped = scratch.skipped;
skipped.clear();
for (const auto& range : vouched) {
if (range.begin < range.end && range.end <= captured.size() && (skipped.empty() || skipped.back().end <= range.begin) && !view.Overlaps(range.low, static_cast<std::size_t>(range.high - range.low))) skipped.push_back(range);
Expand All @@ -79,8 +93,7 @@ bool Driver::validateCaptured(std::uint64_t program, std::uint32_t queue, std::s
}

const auto compareMasked = [&](const ShaderRecompiler::MemoryRegion& region, std::size_t first) {
thread_local std::vector<std::byte>* liveSlot = nullptr;
auto& live = ShaderRecompiler::ThreadOwned(liveSlot);
auto& live = scratch.live;
live.resize(region.bytes.size());
const auto copied = GuestMemory::CopyMapped(region.guestAddress, live);
if (copied != GuestMemory::Compare::Equal) return copied;
Expand All @@ -98,12 +111,9 @@ bool Driver::validateCaptured(std::uint64_t program, std::uint32_t queue, std::s
};

static constexpr std::size_t NoKnownValue = std::numeric_limits<std::size_t>::max();
thread_local std::vector<Policy>* policiesSlot = nullptr;
thread_local std::vector<std::size_t>* knownOffsetsSlot = nullptr;
thread_local std::vector<std::byte>* knownBytesSlot = nullptr;
auto& policies = ShaderRecompiler::ThreadOwned(policiesSlot);
auto& knownOffsets = ShaderRecompiler::ThreadOwned(knownOffsetsSlot);
auto& knownBytes = ShaderRecompiler::ThreadOwned(knownBytesSlot);
auto& policies = scratch.policies;
auto& knownOffsets = scratch.knownOffsets;
auto& knownBytes = scratch.knownBytes;
policies.assign(pending.size(), Policy::None);
knownOffsets.assign(pending.size(), NoKnownValue);
knownBytes.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ bool sealsWriteOnly(std::span<const std::uint32_t> packet, std::uint32_t opcode)
}
}

bool& drawnSinceSeal() {
static thread_local bool drawn = false;
struct DrawnSinceSealTag {};
using DrawnSinceSeal = HostThreadSlot<bool, DrawnSinceSealTag>;

bool TakeDrawnSinceSeal() {
const bool drawn = DrawnSinceSeal::Get();
DrawnSinceSeal::Set(false);
return drawn;
}

Expand Down Expand Up @@ -549,7 +553,7 @@ void Driver::execute(Submission& submission) {
} else if (drawPacket) {
landPendingWaits(submission.queue);
packetCounters.CountDraw();
drawnSinceSeal() = true;
DrawnSinceSeal::Set(true);
bool drawn = false;
timed(&WorkerProfile::drawMs, [&] {
static const bool traceDraws = std::getenv("APS5_TRACE_DRAWS") != nullptr;
Expand Down Expand Up @@ -599,7 +603,7 @@ void Driver::execute(Submission& submission) {
} else if (sampleDump && !wroteOnGpu) {
dumpSampleCounters(Pm4::SampleDumpAddress(packet), queue.sampleDumpBlocks);
} else if (submission.queue == 0 && sealsWriteOnly(packet, opcode)) {
if (std::exchange(drawnSinceSeal(), false)) {
if (TakeDrawnSinceSeal()) {
const auto seal = [this] {
GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Flush);
std::lock_guard gpuLock(GuestMemory::GpuMutex());
Expand Down
Loading
Loading