mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-12-05 17:11:09 +00:00
1187 lines
51 KiB
C++
1187 lines
51 KiB
C++
#include "prx/libSceRazorCpu/libSceRazorCpu.hpp"
|
|
#include "prx/ulobjmgr/ulobjmgr.hpp"
|
|
#include "prx/libkernel/Time/include/Time.hpp"
|
|
#include "prx/libkernel/include/Api.hpp"
|
|
#include "prx/libSceJobManager/include/Api.hpp"
|
|
#include "prx/libSceAgcDriver/Submit/include/Acb.hpp"
|
|
#include "prx/libSceAgcDriver/Eq/include/Event.hpp"
|
|
#include "prx/libSceAgc/Cb/include/Sync.hpp"
|
|
#include "prx/libSceAgc/Cb/include/Registers.hpp"
|
|
#include "prx/libSceAgc/Cb/include/Dispatch.hpp"
|
|
#include "prx/libSceAgc/Acb/include/Sync.hpp"
|
|
#include "prx/libSceAgc/Acb/include/Memory.hpp"
|
|
#include "prx/libSceAgc/Acb/include/Dispatch.hpp"
|
|
#include "prx/libSceAgc/Acb/include/Control.hpp"
|
|
#include <atomic>
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include "prx/libc/include/GuestCpu.hpp"
|
|
#include "prx/libc/include/PreciseWait.hpp"
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <span>
|
|
#include <stdexcept>
|
|
|
|
#include "SceTypes.hpp"
|
|
#include "ComputeProgress.hpp"
|
|
#include "prx/libc/include/General.hpp"
|
|
#include "prx/libc/include/GuestArena.hpp"
|
|
|
|
namespace {
|
|
|
|
constexpr int ManagerInvalidArgument = static_cast<int>(0x81581000u);
|
|
constexpr int ManagerNullMemory = static_cast<int>(0x81581001u);
|
|
constexpr int ManagerMemoryTooSmall = static_cast<int>(0x81581002u);
|
|
constexpr int ManagerNotInitialized = static_cast<int>(0x81581010u);
|
|
constexpr int ManagerAlreadyInitialized = static_cast<int>(0x81581020u);
|
|
constexpr int ManagerMisalignedMemory = static_cast<int>(0x81581030u);
|
|
constexpr int SequenceOutOfRange = static_cast<int>(0x81580000u);
|
|
constexpr int SequenceNullArgument = static_cast<int>(0x81580001u);
|
|
constexpr int SequenceNoNodes = static_cast<int>(0x81580002u);
|
|
constexpr int SequenceLimitExceeded = static_cast<int>(0x81580003u);
|
|
constexpr int SequenceNoneFree = static_cast<int>(0x81580006u);
|
|
constexpr int SequenceInvalid = static_cast<int>(0x81580007u);
|
|
|
|
constexpr std::uint32_t MaxWorkers = 13;
|
|
constexpr std::uint32_t MaxSequences = 32;
|
|
constexpr std::uint32_t MaxInvocations = 0x7fff;
|
|
constexpr std::uint32_t MaxDispatches = 0x3ff;
|
|
constexpr std::uint32_t MaxSyncPoints = 0xff;
|
|
constexpr std::uint32_t MaxWaits = 0x3f;
|
|
constexpr std::uint64_t StateOffset = 0x10;
|
|
constexpr std::uint64_t StateEnd = 0x2d8;
|
|
constexpr std::uint64_t ThreadRecordBytes = 0x48;
|
|
constexpr std::uint64_t SequenceBytes = 0x240;
|
|
constexpr std::uint64_t NodeBytes = 72;
|
|
constexpr std::uint64_t ComputeContextBytes = 0x168;
|
|
constexpr std::uint32_t FirstComputeQueue = 0x20;
|
|
constexpr std::uint32_t UserDataRegister = 0x240;
|
|
constexpr int UserEventFilter = -11;
|
|
|
|
constexpr ConfigParams DefaultConfig{0, 0x00021fff, 0x00010000, 0x2bc, nullptr, 0, 0x1fff, 0x100, 0};
|
|
|
|
struct BlockHeader {
|
|
std::uint16_t magic;
|
|
std::uint16_t stateOffset;
|
|
std::uint32_t version;
|
|
std::uint32_t size;
|
|
std::uint32_t reserved;
|
|
};
|
|
|
|
std::uint64_t AlignUp(std::uint64_t value, std::uint64_t alignment) {
|
|
return (value + alignment - 1) & ~(alignment - 1);
|
|
}
|
|
|
|
struct ComputeCommandSizes {
|
|
std::uint64_t budget;
|
|
std::size_t syncWords;
|
|
std::size_t syncTailWords;
|
|
};
|
|
|
|
ComputeCommandSizes MeasureComputeCommands() {
|
|
std::array<std::uint32_t, 256> words{};
|
|
CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0};
|
|
const ShaderRegister shaderRegister{};
|
|
const std::array<std::uint32_t, 2> userData{};
|
|
bool encoded = sceAgcAcbAcquireMem(&buffer, 0x20380, nullptr, 0, 0x190) != nullptr;
|
|
encoded = encoded && sceAgcCbSetShRegistersDirect(&buffer, &shaderRegister, 1) != nullptr;
|
|
encoded = encoded && sceAgcCbSetShRegisterRangeDirect(&buffer, UserDataRegister, userData.data(), 2) != nullptr;
|
|
encoded = encoded && sceAgcAcbPushMarker(&buffer, "1234567890123456789012345678901", 0) != nullptr;
|
|
encoded = encoded && sceAgcCbDispatch(&buffer, 1, 1, 1, 0) != nullptr;
|
|
encoded = encoded && sceAgcAcbPopMarker(&buffer) != nullptr;
|
|
const auto dispatchBytes = static_cast<std::uint64_t>(buffer.cursor_up - words.data()) * 4;
|
|
buffer.cursor_up = words.data();
|
|
encoded = encoded && sceAgcCbReleaseMem(&buffer, 0x2f, 0, 1, 3, nullptr, 1, 0, 0, 0, 0, 0) != nullptr;
|
|
encoded = encoded && sceAgcAcbWaitRegMem(&buffer, 0, 3, 3, nullptr, 0, ~std::uint64_t{0}, 0x190) != nullptr;
|
|
encoded = encoded && sceAgcAcbRewind(&buffer, 1) != nullptr;
|
|
encoded = encoded && sceAgcCbReleaseMem(&buffer, 0x2f, 0x200, 1, 3, nullptr, 1, 0, 0, 0, 0, 0) != nullptr;
|
|
const auto* tail = buffer.cursor_up;
|
|
encoded = encoded && sceAgcAcbWriteData(&buffer, 2, 3, 0, userData.data(), 1, 1, 0) != nullptr;
|
|
const auto syncBytes = static_cast<std::uint64_t>(buffer.cursor_up - words.data()) * 4;
|
|
const auto syncTailWords = static_cast<std::size_t>(buffer.cursor_up - tail);
|
|
buffer.cursor_up = words.data();
|
|
encoded = encoded && sceAgcCbReleaseMem(&buffer, 0x2f, 0x200, 1, 3, nullptr, 1, 0, 0, 0, 2, 0) != nullptr;
|
|
const auto waitBytes = static_cast<std::uint64_t>(buffer.cursor_up - words.data()) * 4;
|
|
if (!encoded || dispatchBytes > 255 || syncBytes > 255 || waitBytes > 255) throw std::runtime_error("libSceJobManager: compute command size measurement failed");
|
|
return {MaxDispatches * dispatchBytes + MaxSyncPoints * syncBytes + MaxWaits * waitBytes,
|
|
static_cast<std::size_t>(syncBytes / 4), syncTailWords};
|
|
}
|
|
|
|
struct Manager;
|
|
class Sequence;
|
|
|
|
enum class ItemType { Dispatch, SyncPoint, Wait, Compute, ComputeIndirect };
|
|
|
|
struct Item {
|
|
Item* next = nullptr;
|
|
Item* runnableNext = nullptr;
|
|
Sequence* sequence;
|
|
struct Cpu {
|
|
JobEntry entry;
|
|
void* argument;
|
|
std::uint32_t count;
|
|
std::uint32_t handedOut;
|
|
std::uint32_t completed;
|
|
};
|
|
struct Compute {
|
|
const void* shader;
|
|
void* userData;
|
|
union {
|
|
std::array<std::uint32_t, 3> groups;
|
|
const void* indirectArguments;
|
|
};
|
|
};
|
|
union {
|
|
Cpu cpu;
|
|
Compute gpu;
|
|
bool* passed;
|
|
};
|
|
const char* name = nullptr;
|
|
ItemType type;
|
|
bool encoded = false;
|
|
};
|
|
|
|
static_assert(sizeof(Item) == NodeBytes);
|
|
|
|
struct RunQueue {
|
|
Item* first = nullptr;
|
|
Item* last = nullptr;
|
|
|
|
void Push(Item* item) {
|
|
item->runnableNext = nullptr;
|
|
if (last != nullptr) last->runnableNext = item;
|
|
else first = item;
|
|
last = item;
|
|
}
|
|
|
|
void Pop() {
|
|
first = first->runnableNext;
|
|
if (first == nullptr) last = nullptr;
|
|
}
|
|
};
|
|
|
|
struct Counters {
|
|
std::uint32_t invocations = 0;
|
|
std::uint32_t dispatches = 0;
|
|
std::uint32_t syncPoints = 0;
|
|
std::uint32_t waits = 0;
|
|
};
|
|
|
|
class Sequence final : public ISequence {
|
|
public:
|
|
APS5_VABI int Dispatch(JobEntry entry, void* argument, std::uint32_t count, const char* name) override;
|
|
APS5_VABI int SyncPoint() override;
|
|
APS5_VABI int Wait() override;
|
|
APS5_VABI int DispatchCompute(const void* shader, void* userData, std::int32_t x, std::int32_t y, std::int32_t z, const char* name) override;
|
|
APS5_VABI int DispatchComputeIndirect(const void* shader, void* userData, const void* indirectArguments, const char* name) override;
|
|
|
|
bool Open(Manager* owner, const Counters& limit, std::uint32_t runQueue, const char* name);
|
|
int WaitAndClose(std::uint32_t index);
|
|
void Advance();
|
|
void DispatchCompleted(std::uint32_t invocations);
|
|
void CollectCompute();
|
|
void SetComputeStorage(std::uint32_t* commands, const ComputeCommandSizes& sizes, Label* completion);
|
|
void CancelWaits();
|
|
|
|
Manager* manager = nullptr;
|
|
bool open = false;
|
|
bool compute = false;
|
|
std::uint32_t queue = 0;
|
|
std::uint16_t profileId = 0;
|
|
|
|
private:
|
|
int Latch(int code);
|
|
void RequireOpen() const;
|
|
int WaitLocked(std::unique_lock<std::mutex>& guard);
|
|
int AppendCompute(Item item);
|
|
void Append(Item* item);
|
|
void RemoveWait(Item* item);
|
|
void SubmitCompute(const Item& item);
|
|
void SubmitComputeAhead(Item* item);
|
|
void RetireCompute();
|
|
void FlushCompute(bool syncPoint);
|
|
void ReclaimComputeTail();
|
|
void Profile(std::uint16_t event);
|
|
|
|
Counters limits;
|
|
Counters submitted;
|
|
Counters completed;
|
|
std::uint32_t priority = 0;
|
|
int error = 0;
|
|
std::uint32_t outstanding = 0;
|
|
Item* reserved = nullptr;
|
|
Item* first = nullptr;
|
|
Item* last = nullptr;
|
|
Item* cursor = nullptr;
|
|
std::condition_variable passed;
|
|
Label* label = nullptr;
|
|
JobManager::ComputeProgress progress;
|
|
std::uint32_t pendingCompute = 0;
|
|
std::uint32_t activeCompute = 0;
|
|
std::uint32_t availableCompute = 0;
|
|
bool batchStarted = false;
|
|
bool profileActive = false;
|
|
bool profileCpu = false;
|
|
bool profileGpuBarrier = false;
|
|
bool reclaimSyncTail = false;
|
|
const char* profileName = nullptr;
|
|
std::uint32_t* commandBegin = nullptr;
|
|
std::size_t commandWords = 0;
|
|
std::size_t commandUsed = 0;
|
|
std::size_t syncWords = 0;
|
|
std::size_t syncTailWords = 0;
|
|
};
|
|
|
|
static_assert(sizeof(Sequence) < SequenceBytes);
|
|
|
|
struct alignas(64) SequenceSlot {
|
|
Sequence value;
|
|
std::array<std::byte, SequenceBytes - sizeof(Sequence)> padding;
|
|
};
|
|
|
|
static_assert(sizeof(SequenceSlot) == SequenceBytes);
|
|
|
|
struct WorkerSlot {
|
|
Pthread thread = nullptr;
|
|
std::array<std::byte, ThreadRecordBytes - sizeof(Pthread)> padding;
|
|
};
|
|
|
|
static_assert(sizeof(WorkerSlot) == ThreadRecordBytes);
|
|
|
|
class Factory final : public ISequenceFactory {
|
|
public:
|
|
APS5_VABI int Create(ISequence** out, std::uint32_t invocations, std::uint32_t dispatches, std::uint32_t syncPoints, std::uint32_t waits, const char* name) override;
|
|
APS5_VABI int Destroy(ISequence* sequence) override;
|
|
APS5_VABI int CreateCompute(ISequence** out, std::uint32_t invocations, std::uint32_t dispatches, std::uint32_t syncPoints, std::uint32_t waits, const char* name) override;
|
|
APS5_VABI int DestroyCompute(ISequence* sequence) override;
|
|
|
|
Manager* manager = nullptr;
|
|
std::uint32_t priority = 0;
|
|
};
|
|
|
|
struct Manager {
|
|
std::uint32_t objectId = 0;
|
|
std::uint16_t profileId = 0;
|
|
std::mutex lock;
|
|
std::condition_variable work;
|
|
std::condition_variable quiesced;
|
|
bool running = false;
|
|
std::uint32_t blockedWaiters = 0;
|
|
Item* freeNodes = nullptr;
|
|
std::uint32_t freeSequences = 0;
|
|
std::uint32_t persistentThreads = 0;
|
|
std::uint32_t workerCount = 0;
|
|
std::uint32_t sleepingWorkers = 0;
|
|
std::uint32_t wakeTokens = 0;
|
|
std::atomic<std::uint32_t> workEpoch = 0;
|
|
std::uint64_t pendingInvocations = 0;
|
|
std::uint64_t spinTicks = 0;
|
|
std::array<RunQueue, 2> runQueues;
|
|
std::span<SequenceSlot> sequences;
|
|
std::span<SequenceSlot> computeSequences;
|
|
std::uint32_t freeComputeSequences = 0;
|
|
std::uint64_t computeQueues = 0;
|
|
KernelEqueue computeEvents = 0;
|
|
Pthread computeHandler = nullptr;
|
|
std::array<Factory, 2> factories;
|
|
std::array<Factory, 2> unifiedFactories;
|
|
std::span<WorkerSlot> workers;
|
|
std::uint32_t activeWorkers = 0;
|
|
void* stateBegin = nullptr;
|
|
std::size_t stateBytes = 0;
|
|
std::span<Item> nodes;
|
|
};
|
|
|
|
static_assert(sizeof(Manager) <= StateEnd - StateOffset);
|
|
|
|
thread_local Manager* currentWorker = nullptr;
|
|
|
|
void WakeWorkers(Manager& manager, std::uint32_t invocations, bool publisherCanRun = true) {
|
|
manager.workEpoch.fetch_add(1, std::memory_order_release);
|
|
const std::uint32_t available = invocations - (publisherCanRun && currentWorker == &manager ? 1u : 0u);
|
|
manager.wakeTokens += std::min(available, manager.sleepingWorkers - manager.wakeTokens);
|
|
if (manager.wakeTokens != 0) manager.work.notify_all();
|
|
}
|
|
|
|
Manager*& Implementation(void* self) {
|
|
return *static_cast<Manager**>(self);
|
|
}
|
|
|
|
Item* TakeNode(Manager& manager) {
|
|
Item* item = manager.freeNodes;
|
|
if (item != nullptr) {
|
|
manager.freeNodes = item->next;
|
|
*item = {};
|
|
}
|
|
return item;
|
|
}
|
|
|
|
void ReturnNode(Manager& manager, Item* item) {
|
|
item->next = manager.freeNodes;
|
|
manager.freeNodes = item;
|
|
}
|
|
|
|
int Sequence::Latch(int code) {
|
|
if (error == 0) error = code;
|
|
return code;
|
|
}
|
|
|
|
void Sequence::Profile(std::uint16_t event) {
|
|
const auto id = static_cast<std::uint16_t>(profileId - (compute && event <= 2 ? manager->sequences.size() : 0));
|
|
static_cast<void>(sceRazorCpuJobManagerSequence(profileName, manager->profileId, id, event));
|
|
}
|
|
|
|
bool Sequence::Open(Manager* owner, const Counters& limit, std::uint32_t runQueue, const char* name) {
|
|
manager = owner;
|
|
limits = limit;
|
|
submitted = {};
|
|
completed = {};
|
|
priority = runQueue;
|
|
error = 0;
|
|
outstanding = 0;
|
|
cursor = nullptr;
|
|
first = nullptr;
|
|
last = nullptr;
|
|
commandUsed = 0;
|
|
batchStarted = false;
|
|
activeCompute = 0;
|
|
availableCompute = 0;
|
|
profileActive = false;
|
|
profileCpu = false;
|
|
profileGpuBarrier = false;
|
|
reclaimSyncTail = false;
|
|
profileName = name;
|
|
reserved = TakeNode(*owner);
|
|
if (reserved == nullptr) return false;
|
|
open = true;
|
|
Profile(1);
|
|
return true;
|
|
}
|
|
|
|
void Sequence::RequireOpen() const {
|
|
if (!open) throw std::runtime_error("libSceJobManager: a destroyed sequence was used");
|
|
}
|
|
|
|
void Sequence::Append(Item* item) {
|
|
item->next = nullptr;
|
|
if (last != nullptr) last->next = item;
|
|
else first = item;
|
|
last = item;
|
|
if (cursor == nullptr) cursor = item;
|
|
}
|
|
|
|
void Sequence::RemoveWait(Item* item) {
|
|
Item* previous = nullptr;
|
|
for (Item* current = first; current != nullptr; current = current->next) {
|
|
if (current == item) {
|
|
if (previous != nullptr) previous->next = item->next;
|
|
else first = item->next;
|
|
if (last == item) last = previous;
|
|
if (cursor == item) cursor = item->next;
|
|
return;
|
|
}
|
|
previous = current;
|
|
}
|
|
}
|
|
|
|
void Sequence::Advance() {
|
|
while (cursor != nullptr) {
|
|
Item& item = *cursor;
|
|
if (item.type == ItemType::Dispatch) {
|
|
ReclaimComputeTail();
|
|
if (profileGpuBarrier) {
|
|
Profile(4);
|
|
profileActive = false;
|
|
profileGpuBarrier = false;
|
|
}
|
|
if (!profileActive) Profile(3);
|
|
profileActive = true;
|
|
profileCpu = true;
|
|
if (!manager->running) return;
|
|
++outstanding;
|
|
cursor = item.next;
|
|
manager->runQueues[priority].Push(&item);
|
|
manager->pendingInvocations -= item.cpu.count;
|
|
WakeWorkers(*manager, item.cpu.count);
|
|
continue;
|
|
}
|
|
if (item.type == ItemType::Compute || item.type == ItemType::ComputeIndirect) {
|
|
if (!manager->running) return;
|
|
if (!profileActive) Profile(3);
|
|
profileActive = true;
|
|
profileGpuBarrier = false;
|
|
++outstanding;
|
|
++activeCompute;
|
|
cursor = item.next;
|
|
if (!item.encoded) {
|
|
SubmitCompute(item);
|
|
item.encoded = true;
|
|
}
|
|
RetireCompute();
|
|
continue;
|
|
}
|
|
if (!item.encoded) {
|
|
FlushCompute(item.type == ItemType::SyncPoint && !profileCpu);
|
|
item.encoded = true;
|
|
}
|
|
if (outstanding != 0) {
|
|
if (item.type == ItemType::SyncPoint && !profileCpu && manager->running) SubmitComputeAhead(item.next);
|
|
return;
|
|
}
|
|
if (item.type == ItemType::SyncPoint) {
|
|
if (profileCpu) {
|
|
Profile(4);
|
|
profileActive = false;
|
|
profileCpu = false;
|
|
}
|
|
profileGpuBarrier = profileActive;
|
|
++completed.syncPoints;
|
|
cursor = item.next;
|
|
continue;
|
|
}
|
|
if (profileActive) Profile(4);
|
|
profileActive = false;
|
|
profileCpu = false;
|
|
profileGpuBarrier = false;
|
|
bool* const waiter = item.passed;
|
|
while (first != &item) {
|
|
Item* retired = first;
|
|
first = first->next;
|
|
ReturnNode(*manager, retired);
|
|
}
|
|
first = item.next;
|
|
if (last == &item) last = nullptr;
|
|
cursor = first;
|
|
commandUsed = 0;
|
|
batchStarted = false;
|
|
reclaimSyncTail = false;
|
|
if (error == 0) {
|
|
submitted.invocations -= completed.invocations;
|
|
submitted.dispatches -= completed.dispatches;
|
|
submitted.syncPoints -= completed.syncPoints;
|
|
--submitted.waits;
|
|
completed = {};
|
|
}
|
|
*waiter = true;
|
|
passed.notify_all();
|
|
}
|
|
}
|
|
|
|
void Sequence::DispatchCompleted(std::uint32_t invocations) {
|
|
completed.invocations += invocations;
|
|
++completed.dispatches;
|
|
--outstanding;
|
|
if (outstanding == 0) Advance();
|
|
}
|
|
|
|
void Sequence::CancelWaits() {
|
|
for (Item* item = first; item != nullptr; item = item->next) {
|
|
if (item->type == ItemType::Wait) *item->passed = true;
|
|
}
|
|
passed.notify_all();
|
|
}
|
|
|
|
int Sequence::Dispatch(JobEntry entry, void* argument, std::uint32_t count, const char* name) {
|
|
if (entry == nullptr) return SequenceNullArgument;
|
|
if (count == 0 || count > MaxInvocations) return SequenceOutOfRange;
|
|
const std::lock_guard guard(manager->lock);
|
|
RequireOpen();
|
|
if (error != 0) return SequenceInvalid;
|
|
const Counters before = submitted;
|
|
submitted.invocations += count;
|
|
++submitted.dispatches;
|
|
if (before.invocations + count > limits.invocations || before.dispatches >= limits.dispatches) return Latch(SequenceLimitExceeded);
|
|
Item* item = TakeNode(*manager);
|
|
if (item == nullptr) return Latch(SequenceNoNodes);
|
|
item->type = ItemType::Dispatch;
|
|
item->sequence = this;
|
|
item->name = name;
|
|
item->cpu = {entry, argument, count, 0, 0};
|
|
Append(item);
|
|
manager->pendingInvocations += count;
|
|
Advance();
|
|
return 0;
|
|
}
|
|
|
|
void Sequence::SubmitCompute(const Item& item) {
|
|
const auto* bytes = static_cast<const std::uint8_t*>(item.gpu.shader);
|
|
const ShaderRegister* registers = nullptr;
|
|
const std::uint8_t* specials = nullptr;
|
|
std::memcpy(®isters, bytes + 0x20, sizeof(registers));
|
|
std::memcpy(&specials, bytes + 0x28, sizeof(specials));
|
|
const std::uint32_t registerCount = bytes[0x5c];
|
|
std::uint32_t modifier = 0;
|
|
std::memcpy(&modifier, specials + 0x10, sizeof(modifier));
|
|
const auto userData = reinterpret_cast<std::uint64_t>(item.gpu.userData);
|
|
const std::array<std::uint32_t, 2> userDataWords{static_cast<std::uint32_t>(userData), static_cast<std::uint32_t>(userData >> 32)};
|
|
const auto capacity = std::min(commandWords - commandUsed, 64 + 3 * registerCount + (item.name != nullptr ? std::strlen(item.name) / 4 : 0));
|
|
auto* const words = commandBegin + commandUsed;
|
|
CommandBuffer buffer{words, words + capacity, words, words + capacity, nullptr, nullptr, 0};
|
|
{
|
|
const GuestArena::HostWrite destination(words, capacity * 4);
|
|
bool encoded = sceAgcAcbAcquireMem(&buffer, 0x20380u | (batchStarted ? 0u : 1u), nullptr, 0, 0x190) != nullptr;
|
|
encoded = encoded && sceAgcCbSetShRegistersDirect(&buffer, registers, registerCount) != nullptr;
|
|
encoded = encoded && sceAgcCbSetShRegisterRangeDirect(&buffer, UserDataRegister, userDataWords.data(), 2) != nullptr;
|
|
if (item.name != nullptr) encoded = encoded && sceAgcAcbPushMarker(&buffer, item.name, 0) != nullptr;
|
|
if (item.type == ItemType::Compute) {
|
|
encoded = encoded && sceAgcCbDispatch(&buffer, item.gpu.groups[0], item.gpu.groups[1], item.gpu.groups[2], modifier) != nullptr;
|
|
} else {
|
|
encoded = encoded && sceAgcAcbDispatchIndirect(&buffer, item.gpu.indirectArguments, modifier) != nullptr;
|
|
}
|
|
if (item.name != nullptr) encoded = encoded && sceAgcAcbPopMarker(&buffer) != nullptr;
|
|
if (!encoded) throw std::runtime_error("libSceJobManager: a compute dispatch did not fit its command buffer");
|
|
}
|
|
batchStarted = true;
|
|
reclaimSyncTail = false;
|
|
++pendingCompute;
|
|
const Packet packet{words, static_cast<std::uint32_t>(buffer.cursor_up - words), 0, {}};
|
|
commandUsed += packet.dw_num;
|
|
if (sceAgcDriverSubmitAcb(queue, &packet) != 0) throw std::runtime_error("libSceJobManager: sceAgcDriverSubmitAcb failed");
|
|
}
|
|
|
|
void Sequence::SetComputeStorage(std::uint32_t* commands, const ComputeCommandSizes& sizes, Label* completion) {
|
|
commandBegin = commands;
|
|
commandWords = static_cast<std::size_t>(sizes.budget / 4);
|
|
syncWords = sizes.syncWords;
|
|
syncTailWords = sizes.syncTailWords;
|
|
label = completion;
|
|
const GuestArena::HostWrite destination(label, sizeof(*label));
|
|
label->value = 0;
|
|
}
|
|
|
|
void Sequence::SubmitComputeAhead(Item* item) {
|
|
while (item != nullptr) {
|
|
if (item->type == ItemType::Compute || item->type == ItemType::ComputeIndirect) {
|
|
if (!item->encoded) {
|
|
SubmitCompute(*item);
|
|
item->encoded = true;
|
|
}
|
|
} else if (item->type == ItemType::Dispatch) {
|
|
ReclaimComputeTail();
|
|
return;
|
|
} else {
|
|
if (!item->encoded) {
|
|
FlushCompute(item->type == ItemType::SyncPoint);
|
|
item->encoded = true;
|
|
}
|
|
if (item->type != ItemType::SyncPoint) return;
|
|
}
|
|
item = item->next;
|
|
}
|
|
}
|
|
|
|
void Sequence::ReclaimComputeTail() {
|
|
if (!reclaimSyncTail) return;
|
|
commandUsed -= syncTailWords;
|
|
reclaimSyncTail = false;
|
|
}
|
|
|
|
void Sequence::FlushCompute(bool syncPoint) {
|
|
if (!syncPoint) ReclaimComputeTail();
|
|
if (pendingCompute == 0) return;
|
|
const auto reservedWords = syncPoint ? syncWords : std::size_t{8};
|
|
if (commandWords - commandUsed < reservedWords) throw std::runtime_error("libSceJobManager: a compute completion did not fit its command buffer");
|
|
auto* const words = commandBegin + commandUsed;
|
|
CommandBuffer buffer{words, words + 8, words, words + 8, nullptr, nullptr, 0};
|
|
{
|
|
const GuestArena::HostWrite destination(words, 32);
|
|
if (sceAgcCbReleaseMem(&buffer, 0x2f, 0x200, 1, 3, label, 1, progress.Publish(pendingCompute), 0, 0, 2, 0) == nullptr) {
|
|
throw std::runtime_error("libSceJobManager: a compute completion could not be encoded");
|
|
}
|
|
}
|
|
pendingCompute = 0;
|
|
const Packet packet{words, static_cast<std::uint32_t>(buffer.cursor_up - words), 0, {}};
|
|
commandUsed += reservedWords;
|
|
reclaimSyncTail = syncPoint;
|
|
if (sceAgcDriverSubmitAcb(queue, &packet) != 0) throw std::runtime_error("libSceJobManager: sceAgcDriverSubmitAcb failed for a completion");
|
|
}
|
|
|
|
void Sequence::RetireCompute() {
|
|
const auto finished = std::min(activeCompute, availableCompute);
|
|
activeCompute -= finished;
|
|
availableCompute -= finished;
|
|
completed.dispatches += finished;
|
|
outstanding -= finished;
|
|
}
|
|
|
|
void Sequence::CollectCompute() {
|
|
const auto finished = progress.Collect(label->value);
|
|
if (finished == 0) return;
|
|
availableCompute += finished;
|
|
RetireCompute();
|
|
if (outstanding == 0) Advance();
|
|
}
|
|
|
|
int Sequence::AppendCompute(Item item) {
|
|
const std::lock_guard guard(manager->lock);
|
|
RequireOpen();
|
|
if (!compute) throw std::runtime_error("libSceJobManager: a compute dispatch was made on a CPU sequence");
|
|
if (error != 0) return SequenceInvalid;
|
|
if (submitted.dispatches++ >= limits.dispatches) return Latch(SequenceLimitExceeded);
|
|
Item* stored = TakeNode(*manager);
|
|
if (stored == nullptr) return Latch(SequenceNoNodes);
|
|
*stored = item;
|
|
Append(stored);
|
|
Advance();
|
|
return 0;
|
|
}
|
|
|
|
int Sequence::DispatchCompute(const void* shader, void* userData, std::int32_t x, std::int32_t y, std::int32_t z, const char* name) {
|
|
if (shader == nullptr) return SequenceNullArgument;
|
|
if (x <= 0 || y <= 0 || z <= 0) return SequenceOutOfRange;
|
|
Item item{};
|
|
item.type = ItemType::Compute;
|
|
item.sequence = this;
|
|
item.gpu.shader = shader;
|
|
item.gpu.userData = userData;
|
|
item.gpu.groups = {static_cast<std::uint32_t>(x), static_cast<std::uint32_t>(y), static_cast<std::uint32_t>(z)};
|
|
item.name = name;
|
|
return AppendCompute(item);
|
|
}
|
|
|
|
int Sequence::DispatchComputeIndirect(const void* shader, void* userData, const void* indirectArguments, const char* name) {
|
|
if (shader == nullptr || indirectArguments == nullptr || (reinterpret_cast<std::uint64_t>(indirectArguments) & 0x1f) != 0) return SequenceNullArgument;
|
|
Item item{};
|
|
item.type = ItemType::ComputeIndirect;
|
|
item.sequence = this;
|
|
item.gpu.shader = shader;
|
|
item.gpu.userData = userData;
|
|
item.gpu.indirectArguments = indirectArguments;
|
|
item.name = name;
|
|
return AppendCompute(item);
|
|
}
|
|
|
|
int Sequence::SyncPoint() {
|
|
const std::lock_guard guard(manager->lock);
|
|
RequireOpen();
|
|
if (error != 0) return SequenceInvalid;
|
|
const std::uint32_t before = submitted.syncPoints++;
|
|
if (before >= limits.syncPoints) return Latch(SequenceLimitExceeded);
|
|
Item* item = TakeNode(*manager);
|
|
if (item == nullptr) return Latch(SequenceNoNodes);
|
|
item->type = ItemType::SyncPoint;
|
|
item->sequence = this;
|
|
Append(item);
|
|
Advance();
|
|
return 0;
|
|
}
|
|
|
|
int Sequence::Wait() {
|
|
std::unique_lock guard(manager->lock);
|
|
return WaitLocked(guard);
|
|
}
|
|
|
|
int Sequence::WaitAndClose(std::uint32_t index) {
|
|
std::unique_lock guard(manager->lock);
|
|
const int result = WaitLocked(guard);
|
|
while (first != nullptr) {
|
|
Item* item = first;
|
|
first = item->next;
|
|
if (item->type != ItemType::Wait) ReturnNode(*manager, item);
|
|
}
|
|
ReturnNode(*manager, reserved);
|
|
reserved = nullptr;
|
|
last = nullptr;
|
|
commandUsed = 0;
|
|
cursor = nullptr;
|
|
outstanding = 0;
|
|
open = false;
|
|
Profile(2);
|
|
(compute ? manager->freeComputeSequences : manager->freeSequences) |= std::uint32_t{1} << index;
|
|
return result;
|
|
}
|
|
|
|
int Sequence::WaitLocked(std::unique_lock<std::mutex>& guard) {
|
|
RequireOpen();
|
|
int result = 0;
|
|
if (error != 0) {
|
|
result = SequenceInvalid;
|
|
} else if (submitted.waits++ >= limits.waits) {
|
|
result = Latch(SequenceLimitExceeded);
|
|
}
|
|
++manager->blockedWaiters;
|
|
bool done = false;
|
|
Item waiter{};
|
|
waiter.type = ItemType::Wait;
|
|
waiter.sequence = this;
|
|
waiter.passed = &done;
|
|
Append(&waiter);
|
|
Advance();
|
|
if (!done && currentWorker == manager) {
|
|
std::uint64_t runnable = 0;
|
|
for (const auto& queue : manager->runQueues) {
|
|
for (const Item* item = queue.first; item != nullptr; item = item->runnableNext) runnable += item->cpu.count - item->cpu.handedOut;
|
|
}
|
|
WakeWorkers(*manager, static_cast<std::uint32_t>(std::min(runnable, std::uint64_t{manager->workerCount})), false);
|
|
}
|
|
passed.wait(guard, [&done] { return done; });
|
|
RemoveWait(&waiter);
|
|
--manager->blockedWaiters;
|
|
manager->quiesced.notify_all();
|
|
return result;
|
|
}
|
|
|
|
bool ValidLimits(std::uint32_t invocations, std::uint32_t dispatches, std::uint32_t syncPoints, std::uint32_t waits) {
|
|
if (invocations > MaxInvocations || dispatches > MaxDispatches || syncPoints > MaxSyncPoints || waits > MaxWaits) return false;
|
|
return invocations != 0 || dispatches != 0 || syncPoints != 0 || waits != 0;
|
|
}
|
|
|
|
template <typename TSequences>
|
|
int CreateIn(Manager* manager, TSequences& sequences, std::uint32_t& free, std::uint32_t priority, ISequence** out, const Counters& limits, const char* name) {
|
|
if (out == nullptr) return SequenceNullArgument;
|
|
if (!ValidLimits(limits.invocations, limits.dispatches, limits.syncPoints, limits.waits)) return SequenceOutOfRange;
|
|
const std::lock_guard guard(manager->lock);
|
|
if (free == 0) return SequenceNoneFree;
|
|
const int index = std::countr_zero(free);
|
|
Sequence& sequence = sequences[static_cast<std::size_t>(index)].value;
|
|
if (!sequence.Open(manager, limits, priority, name)) return SequenceNoNodes;
|
|
free &= ~(std::uint32_t{1} << index);
|
|
*out = &sequence;
|
|
return 0;
|
|
}
|
|
|
|
template <typename TSequences>
|
|
int DestroyIn(Manager* manager, TSequences& sequences, const std::uint32_t& free, ISequence* sequence) {
|
|
Sequence* found = nullptr;
|
|
std::uint32_t index = 0;
|
|
{
|
|
const std::lock_guard guard(manager->lock);
|
|
for (; index < sequences.size(); ++index) {
|
|
if (&sequences[index].value == sequence) break;
|
|
}
|
|
if (index == sequences.size() || (free & (std::uint32_t{1} << index)) != 0) return SequenceInvalid;
|
|
found = &sequences[index].value;
|
|
}
|
|
static_cast<void>(found->WaitAndClose(index));
|
|
return 0;
|
|
}
|
|
|
|
int Factory::Create(ISequence** out, std::uint32_t invocations, std::uint32_t dispatches, std::uint32_t syncPoints, std::uint32_t waits, const char* name) {
|
|
return CreateIn(manager, manager->sequences, manager->freeSequences, priority, out, {invocations, dispatches, syncPoints, waits}, name);
|
|
}
|
|
|
|
int Factory::Destroy(ISequence* sequence) {
|
|
return DestroyIn(manager, manager->sequences, manager->freeSequences, sequence);
|
|
}
|
|
|
|
int Factory::CreateCompute(ISequence** out, std::uint32_t invocations, std::uint32_t dispatches, std::uint32_t syncPoints, std::uint32_t waits, const char* name) {
|
|
return CreateIn(manager, manager->computeSequences, manager->freeComputeSequences, priority, out, {invocations, dispatches, syncPoints, waits}, name);
|
|
}
|
|
|
|
int Factory::DestroyCompute(ISequence* sequence) {
|
|
return DestroyIn(manager, manager->computeSequences, manager->freeComputeSequences, sequence);
|
|
}
|
|
|
|
void* APS5_VABI ComputeHandlerEntry(void* argument) {
|
|
Manager& manager = *static_cast<Manager*>(argument);
|
|
for (;;) {
|
|
KernelEvent event{};
|
|
int count = 0;
|
|
if (sceKernelWaitEqueue(manager.computeEvents, &event, 1, &count, nullptr) != 0) throw std::runtime_error("libSceJobManager: sceKernelWaitEqueue failed in the compute handler");
|
|
if (count <= 0) continue;
|
|
if (sceKernelGetEventFilter(&event) == UserEventFilter && sceKernelGetEventId(&event) == 0) return nullptr;
|
|
const std::lock_guard guard(manager.lock);
|
|
for (SequenceSlot& slot : manager.computeSequences) slot.value.CollectCompute();
|
|
}
|
|
}
|
|
|
|
bool RunOne(Manager& manager, std::unique_lock<std::mutex>& guard) {
|
|
Item* item = nullptr;
|
|
for (const std::size_t queue : {std::size_t{1}, std::size_t{0}}) {
|
|
auto& pending = manager.runQueues[queue];
|
|
if (pending.first == nullptr) continue;
|
|
item = pending.first;
|
|
if (item->cpu.handedOut + 1 == item->cpu.count) pending.Pop();
|
|
break;
|
|
}
|
|
if (item == nullptr) return false;
|
|
const std::uint32_t index = item->cpu.handedOut++;
|
|
const JobEntry entry = item->cpu.entry;
|
|
void* const argument = item->cpu.argument;
|
|
const auto dispatchId = static_cast<std::uint16_t>(item - manager.nodes.data());
|
|
guard.unlock();
|
|
static_cast<void>(sceRazorCpuJobManagerJob(manager.profileId, dispatchId, static_cast<std::uint16_t>(index), 16));
|
|
entry(argument, index);
|
|
static_cast<void>(sceRazorCpuJobManagerJob(manager.profileId, dispatchId, static_cast<std::uint16_t>(index), 32));
|
|
guard.lock();
|
|
if (++item->cpu.completed == item->cpu.count) {
|
|
static_cast<void>(sceRazorCpuJobManagerDispatch(item->name, manager.profileId, item->sequence->profileId, dispatchId, static_cast<std::uint16_t>(item->cpu.count), reinterpret_cast<std::uint64_t>(entry), reinterpret_cast<std::uint64_t>(argument)));
|
|
item->sequence->DispatchCompleted(item->cpu.count);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
constexpr std::uint32_t SpinPauses = 1024;
|
|
|
|
void* APS5_VABI WorkerEntry(void* argument) {
|
|
Manager& manager = *static_cast<Manager*>(argument);
|
|
currentWorker = &manager;
|
|
std::uint64_t lastWork = GuestCpu::ReadTsc();
|
|
std::unique_lock guard(manager.lock);
|
|
while (manager.running) {
|
|
if (RunOne(manager, guard)) {
|
|
lastWork = GuestCpu::ReadTsc();
|
|
continue;
|
|
}
|
|
const bool spinning = manager.pendingInvocations != 0 && GuestCpu::ReadTsc() - lastWork < manager.spinTicks;
|
|
if (spinning) {
|
|
const std::uint32_t epoch = manager.workEpoch.load(std::memory_order_acquire);
|
|
guard.unlock();
|
|
for (std::uint32_t pause = 0; pause != SpinPauses && manager.workEpoch.load(std::memory_order_acquire) == epoch; ++pause) PreciseSpinPause();
|
|
guard.lock();
|
|
continue;
|
|
}
|
|
if (manager.sleepingWorkers >= manager.workerCount - manager.persistentThreads) {
|
|
const std::uint32_t epoch = manager.workEpoch.load(std::memory_order_acquire);
|
|
guard.unlock();
|
|
while (manager.workEpoch.load(std::memory_order_acquire) == epoch) {
|
|
for (std::uint32_t pause = 0; pause != SpinPauses; ++pause) PreciseSpinPause();
|
|
}
|
|
guard.lock();
|
|
continue;
|
|
}
|
|
++manager.sleepingWorkers;
|
|
manager.work.wait(guard, [&manager] { return !manager.running || manager.wakeTokens != 0; });
|
|
if (manager.wakeTokens != 0) --manager.wakeTokens;
|
|
--manager.sleepingWorkers;
|
|
}
|
|
currentWorker = nullptr;
|
|
return nullptr;
|
|
}
|
|
|
|
struct WorkLayout {
|
|
std::uint64_t workers = 0;
|
|
std::uint64_t sequences = 0;
|
|
std::uint64_t stateEnd = 0;
|
|
std::uint64_t pool = 0;
|
|
std::uint64_t commands = 0;
|
|
ComputeCommandSizes commandSizes{};
|
|
std::uint64_t labels = 0;
|
|
std::uint32_t nodes = 0;
|
|
};
|
|
|
|
WorkLayout LayoutFor(std::uint64_t memory, std::uint64_t size, std::uint32_t workers, std::uint32_t sequences, std::uint32_t computeQueues) {
|
|
WorkLayout layout;
|
|
const std::uint64_t end = memory + size;
|
|
layout.workers = AlignUp(memory + StateEnd, 8);
|
|
std::uint64_t address = layout.workers + workers * ThreadRecordBytes;
|
|
for (const std::uint64_t bytes : {std::uint64_t{0x100}, std::uint64_t{0x80}, std::uint64_t{0x80}, std::uint64_t{0x80}}) address = AlignUp(address, 64) + bytes;
|
|
layout.sequences = AlignUp(address, 64);
|
|
address = layout.sequences + (sequences + computeQueues) * SequenceBytes;
|
|
layout.stateEnd = address;
|
|
if (computeQueues != 0) {
|
|
layout.commandSizes = MeasureComputeCommands();
|
|
layout.commands = AlignUp(address + ComputeContextBytes, 0x100);
|
|
layout.labels = AlignUp(layout.commands + layout.commandSizes.budget * computeQueues + 4 * std::uint64_t{computeQueues}, 8);
|
|
address = layout.labels + 8 * std::uint64_t{computeQueues};
|
|
}
|
|
address = AlignUp(address, 64);
|
|
layout.pool = address;
|
|
if (address <= end) layout.nodes = static_cast<std::uint32_t>(((end - address) / NodeBytes) & 0xffff);
|
|
return layout;
|
|
}
|
|
|
|
int ValidateWorkers(const ConfigParams& config, std::uint32_t workers) {
|
|
const std::uint32_t mode = config.threadFlags & 0x30000;
|
|
if (config.reserved != 0 || config.priority < 0x100 || config.priority > 0x2ff || config.stackSize < 0x4000) return ManagerInvalidArgument;
|
|
if ((config.threadFlags & 0xfffce000) != 0 || static_cast<std::uint32_t>(std::popcount(config.threadFlags & 0x1fff)) < workers) return ManagerInvalidArgument;
|
|
if ((mode != 0x10000 && mode != 0x20000) || config.spinMicroseconds > 0x32) return ManagerInvalidArgument;
|
|
return 0;
|
|
}
|
|
|
|
class ThreadAttributes {
|
|
public:
|
|
ThreadAttributes() {
|
|
if (scePthreadAttrInit(&value) != 0) throw std::runtime_error("libSceJobManager: scePthreadAttrInit failed");
|
|
}
|
|
~ThreadAttributes() { static_cast<void>(scePthreadAttrDestroy(&value)); }
|
|
ThreadAttributes(const ThreadAttributes&) = delete;
|
|
ThreadAttributes& operator=(const ThreadAttributes&) = delete;
|
|
PthreadAttr value = nullptr;
|
|
};
|
|
|
|
void StartWorkers(Manager& manager, const ConfigParams& config, std::uint32_t workers) {
|
|
std::uint32_t remaining = config.threadFlags & 0x1fff;
|
|
for (std::uint32_t index = 0; index < workers; ++index) {
|
|
KernelCpumask affinity = config.threadFlags & 0x1fff;
|
|
if ((config.threadFlags & 0x30000) == 0x10000) {
|
|
affinity = remaining & (~remaining + 1);
|
|
remaining &= remaining - 1;
|
|
}
|
|
std::array<char, 32> name{};
|
|
if (config.threadName != nullptr) std::snprintf(name.data(), name.size(), "%.31s", config.threadName);
|
|
else std::snprintf(name.data(), name.size(), "SceJobManagerWorker%u", index);
|
|
const KernelSchedParam schedule{static_cast<int>(config.priority)};
|
|
ThreadAttributes attributes;
|
|
if (scePthreadAttrSetinheritsched(&attributes.value, 0) != 0 || scePthreadAttrSetschedparam(&attributes.value, &schedule) != 0 ||
|
|
scePthreadAttrSetstacksize(&attributes.value, config.stackSize) != 0 || scePthreadAttrSetaffinity(&attributes.value, affinity) != 0) {
|
|
throw std::runtime_error("libSceJobManager: a worker thread attribute was rejected");
|
|
}
|
|
Pthread thread = nullptr;
|
|
const int created = scePthreadCreate(&thread, &attributes.value, WorkerEntry, &manager, name.data());
|
|
if (created != 0) throw std::runtime_error("libSceJobManager: scePthreadCreate failed for a worker");
|
|
manager.workers[manager.activeWorkers++].thread = thread;
|
|
}
|
|
}
|
|
|
|
void StartCompute(Manager& manager, const WorkLayout& layout, std::uint32_t computeSequences, std::uint64_t computeQueueMask, std::uint32_t priority, std::uint32_t affinity) {
|
|
if (sceKernelCreateEqueue(&manager.computeEvents, "SceJobManagerComputeEventQueue") != 0) throw std::runtime_error("libSceJobManager: sceKernelCreateEqueue failed");
|
|
for (std::uint64_t mask = computeQueueMask; mask != 0; mask &= mask - 1) {
|
|
const std::uint32_t queue = FirstComputeQueue + static_cast<std::uint32_t>(std::countr_zero(mask));
|
|
if (sceAgcDriverAddEqEvent(manager.computeEvents, static_cast<int>(queue), nullptr) != 0) throw std::runtime_error("libSceJobManager: sceAgcDriverAddEqEvent failed");
|
|
manager.computeQueues |= std::uint64_t{1} << (queue - FirstComputeQueue);
|
|
}
|
|
std::uint64_t availableQueues = computeQueueMask;
|
|
for (std::uint32_t index = 0; index < computeSequences; ++index) {
|
|
Sequence& sequence = manager.computeSequences[index].value;
|
|
sequence.compute = true;
|
|
sequence.queue = FirstComputeQueue + static_cast<std::uint32_t>(std::countr_zero(availableQueues));
|
|
availableQueues &= availableQueues - 1;
|
|
sequence.SetComputeStorage(reinterpret_cast<std::uint32_t*>(layout.commands + index * layout.commandSizes.budget),
|
|
layout.commandSizes, reinterpret_cast<Label*>(layout.labels + index * sizeof(Label)));
|
|
}
|
|
manager.freeComputeSequences = computeSequences == MaxSequences ? ~std::uint32_t{0} : (std::uint32_t{1} << computeSequences) - 1;
|
|
const KernelSchedParam schedule{static_cast<int>(priority)};
|
|
ThreadAttributes attributes;
|
|
if (scePthreadAttrSetinheritsched(&attributes.value, 0) != 0 || scePthreadAttrSetschedparam(&attributes.value, &schedule) != 0 ||
|
|
scePthreadAttrSetstacksize(&attributes.value, 0x4000) != 0 || scePthreadAttrSetaffinity(&attributes.value, affinity) != 0) {
|
|
throw std::runtime_error("libSceJobManager: a compute handler thread attribute was rejected");
|
|
}
|
|
const int created = scePthreadCreate(&manager.computeHandler, &attributes.value, ComputeHandlerEntry, &manager, "SceJobManagerComputeEventHdlr");
|
|
if (created != 0) throw std::runtime_error("libSceJobManager: scePthreadCreate failed for the compute handler");
|
|
}
|
|
|
|
void StopManager(Manager& manager) {
|
|
{
|
|
const std::lock_guard guard(manager.lock);
|
|
manager.running = false;
|
|
manager.workEpoch.fetch_add(1, std::memory_order_release);
|
|
manager.work.notify_all();
|
|
}
|
|
while (manager.activeWorkers != 0) {
|
|
if (scePthreadJoin(manager.workers[manager.activeWorkers - 1].thread, nullptr) != 0) throw std::runtime_error("sce::Job::JobManager::shutdown: scePthreadJoin failed");
|
|
manager.workers[--manager.activeWorkers].thread = nullptr;
|
|
}
|
|
if (manager.computeHandler != nullptr) {
|
|
if (sceKernelAddUserEventEdge(manager.computeEvents, 0) != 0 || sceKernelTriggerUserEvent(manager.computeEvents, 0, nullptr) != 0) {
|
|
throw std::runtime_error("sce::Job::JobManager::shutdown: the compute handler could not be signalled");
|
|
}
|
|
if (scePthreadJoin(manager.computeHandler, nullptr) != 0) throw std::runtime_error("sce::Job::JobManager::shutdown: scePthreadJoin failed");
|
|
manager.computeHandler = nullptr;
|
|
}
|
|
if (manager.computeEvents != 0) {
|
|
for (std::uint64_t mask = manager.computeQueues; mask != 0; mask &= mask - 1) {
|
|
const auto queue = FirstComputeQueue + static_cast<std::uint32_t>(std::countr_zero(mask));
|
|
static_cast<void>(sceAgcDriverDeleteEqEvent(manager.computeEvents, static_cast<int>(queue)));
|
|
}
|
|
if (sceKernelDeleteEqueue(manager.computeEvents) != 0) throw std::runtime_error("sce::Job::JobManager::shutdown: sceKernelDeleteEqueue failed");
|
|
manager.computeEvents = 0;
|
|
manager.computeQueues = 0;
|
|
}
|
|
std::unique_lock guard(manager.lock);
|
|
for (SequenceSlot& slot : manager.sequences) slot.value.CancelWaits();
|
|
for (SequenceSlot& slot : manager.computeSequences) slot.value.CancelWaits();
|
|
manager.quiesced.wait(guard, [&manager] { return manager.blockedWaiters == 0; });
|
|
}
|
|
|
|
void DestroyManager(Manager* manager) {
|
|
const GuestArena::HostWrite memory(manager->stateBegin, manager->stateBytes);
|
|
for (SequenceSlot& slot : manager->sequences) std::destroy_at(&slot);
|
|
for (SequenceSlot& slot : manager->computeSequences) std::destroy_at(&slot);
|
|
std::destroy_at(manager);
|
|
}
|
|
|
|
int Initialize(void* self, void* memory, std::uint64_t size, std::uint32_t workers, std::uint32_t sequences, std::uint32_t computeSequences, std::uint64_t computeQueueMask, const ConfigParams* config, bool unified) {
|
|
if (Implementation(self) != nullptr) return ManagerAlreadyInitialized;
|
|
if (memory == nullptr) return ManagerNullMemory;
|
|
const auto base = reinterpret_cast<std::uint64_t>(memory);
|
|
if ((base & 0xff) != 0) return ManagerMisalignedMemory;
|
|
if (size == 0 || workers == 0 || workers > MaxWorkers) return ManagerInvalidArgument;
|
|
if (unified) {
|
|
if ((sequences == 0 && computeSequences == 0) || std::uint64_t{sequences} + computeSequences > MaxSequences || (computeQueueMask >> 56) != 0 || static_cast<std::uint32_t>(std::popcount(computeQueueMask)) < computeSequences) return ManagerInvalidArgument;
|
|
} else if (sequences == 0 || sequences > MaxSequences) {
|
|
return ManagerInvalidArgument;
|
|
}
|
|
if (size < StateOffset) return ManagerMemoryTooSmall;
|
|
if (size < StateEnd) throw std::runtime_error("sce::Job::JobManager::initialize: a memory block smaller than 0x2d8 bytes is not supported");
|
|
{
|
|
const GuestArena::HostWrite destination(memory, StateEnd);
|
|
std::memset(static_cast<std::uint8_t*>(memory) + StateOffset, 0, StateEnd - StateOffset);
|
|
const BlockHeader header{0x4d4a, static_cast<std::uint16_t>(StateOffset), 0x05000001, static_cast<std::uint32_t>(size), 0};
|
|
std::memcpy(memory, &header, sizeof(header));
|
|
}
|
|
if (config != nullptr && config->version > 1) return ManagerInvalidArgument;
|
|
const ConfigParams& settings = config != nullptr ? *config : DefaultConfig;
|
|
const auto computeQueues = computeSequences != 0 ? static_cast<std::uint32_t>(std::popcount(computeQueueMask)) : 0;
|
|
const auto layout = LayoutFor(base, size, workers, sequences, computeQueues);
|
|
if (layout.nodes == 0) return ManagerMemoryTooSmall;
|
|
if (const int invalid = ValidateWorkers(settings, workers); invalid != 0) return invalid;
|
|
const bool ownHandlerSettings = (settings.version & 1) != 0;
|
|
const std::uint32_t handlerPriority = ownHandlerSettings ? settings.computePriority : 0x100;
|
|
const std::uint32_t handlerAffinity = ownHandlerSettings ? settings.computeAffinity : 0x1fff;
|
|
if (computeSequences != 0 && (handlerPriority < 0x100 || handlerPriority > 0x2ff || handlerAffinity == 0 || handlerAffinity > 0x1fff)) return ManagerInvalidArgument;
|
|
const GuestArena::HostWrite stateMemory(memory, static_cast<std::size_t>(layout.stateEnd - base));
|
|
const GuestArena::HostWrite nodeMemory(reinterpret_cast<void*>(layout.pool), layout.nodes * sizeof(Item));
|
|
Manager* manager = std::construct_at(reinterpret_cast<Manager*>(base + StateOffset));
|
|
manager->stateBegin = memory;
|
|
manager->stateBytes = static_cast<std::size_t>(layout.stateEnd - base);
|
|
manager->nodes = {reinterpret_cast<Item*>(layout.pool), layout.nodes};
|
|
std::unique_ptr<Manager, decltype(&DestroyManager)> owner(manager, DestroyManager);
|
|
manager->running = true;
|
|
manager->workerCount = workers;
|
|
manager->spinTicks = HostTscFrequency_nid_no_patch() / 1000000 * settings.spinMicroseconds;
|
|
for (std::uint32_t index = 0; index < layout.nodes; ++index) std::construct_at(&manager->nodes[index]);
|
|
for (std::uint32_t index = layout.nodes; index > 1; --index) ReturnNode(*manager, &manager->nodes[index - 1]);
|
|
manager->freeSequences = sequences == MaxSequences ? ~std::uint32_t{0} : (std::uint32_t{1} << sequences) - 1;
|
|
manager->workers = {reinterpret_cast<WorkerSlot*>(layout.workers), workers};
|
|
for (WorkerSlot& worker : manager->workers) std::construct_at(&worker);
|
|
auto* cpuSlots = reinterpret_cast<SequenceSlot*>(layout.sequences);
|
|
auto* computeSlots = cpuSlots + sequences;
|
|
for (std::uint32_t index = 0; index < sequences; ++index) {
|
|
std::construct_at(cpuSlots + index);
|
|
cpuSlots[index].value.profileId = static_cast<std::uint16_t>(index);
|
|
manager->sequences = {cpuSlots, index + 1};
|
|
}
|
|
for (std::uint32_t index = 0; index < computeSequences; ++index) {
|
|
std::construct_at(computeSlots + index);
|
|
computeSlots[index].value.profileId = static_cast<std::uint16_t>(sequences + index);
|
|
manager->computeSequences = {computeSlots, index + 1};
|
|
}
|
|
for (std::uint32_t priority = 0; priority < 2; ++priority) {
|
|
manager->factories[priority].manager = manager;
|
|
manager->factories[priority].priority = priority;
|
|
manager->unifiedFactories[priority].manager = manager;
|
|
manager->unifiedFactories[priority].priority = priority;
|
|
}
|
|
try {
|
|
if (computeSequences != 0) StartCompute(*manager, layout, computeSequences, computeQueueMask, handlerPriority, handlerAffinity);
|
|
StartWorkers(*manager, settings, workers);
|
|
} catch (...) {
|
|
try {
|
|
StopManager(*manager);
|
|
} catch (...) {
|
|
Implementation(self) = owner.release();
|
|
throw;
|
|
}
|
|
throw;
|
|
}
|
|
Implementation(self) = owner.release();
|
|
const int result = _sceUlobjmgrRegisterObject_nid_postfix(base, 0x21, &manager->objectId);
|
|
std::uint32_t crc = 0;
|
|
for (std::uint32_t byte = 0; byte < sizeof(base); ++byte) {
|
|
crc ^= static_cast<std::uint8_t>(base >> (byte * 8));
|
|
for (std::uint32_t bit = 0; bit < 8; ++bit) crc = (crc >> 1) ^ (0x82f63b78u & (0u - (crc & 1)));
|
|
}
|
|
manager->profileId = static_cast<std::uint16_t>(crc >> 16);
|
|
return result;
|
|
}
|
|
|
|
ISequenceFactory* FactoryFor(void* self, std::uint32_t priority, bool unified) {
|
|
Manager* manager = Implementation(self);
|
|
if (manager == nullptr) return nullptr;
|
|
priority &= 0xffff;
|
|
if (priority > 1) throw std::runtime_error("sce::Job::JobManager: sequence factory priority " + std::to_string(priority) + " does not exist");
|
|
return unified ? &manager->unifiedFactories[priority] : &manager->factories[priority];
|
|
}
|
|
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
void APS5_VABI _ZN3sce3Job10JobManagerC1Ev(void* self) {
|
|
Implementation(self) = nullptr;
|
|
}
|
|
|
|
void APS5_VABI _ZN3sce3Job10JobManagerC2Ev(void* self) {
|
|
Implementation(self) = nullptr;
|
|
}
|
|
|
|
void APS5_VABI _ZN3sce3Job10JobManagerD1Ev(void* self) {
|
|
if (Implementation(self) != nullptr) {
|
|
static_cast<void>(_ZN3sce3Job10JobManager8shutdownEv(self));
|
|
}
|
|
}
|
|
|
|
void APS5_VABI _ZN3sce3Job10JobManagerD2Ev(void* self) {
|
|
_ZN3sce3Job10JobManagerD1Ev(self);
|
|
|
|
}
|
|
|
|
std::uint64_t APS5_VABI _ZN3sce3Job10JobManager27calculateRequiredMemorySizeEPKNS1_21MemorySizeQueryParamsE(const MemorySizeQueryParams* params) {
|
|
if (params == nullptr || params->version > 1 || params->workers == 0 || params->workers > MaxWorkers || params->sequences > MaxSequences) return 0;
|
|
const bool unified = params->version == 1;
|
|
const std::uint32_t computeSequences = unified ? params->computeSequences : 0;
|
|
if (unified) {
|
|
if (computeSequences > MaxSequences || params->sequences + computeSequences == 0 || params->sequences + computeSequences > MaxSequences) return 0;
|
|
if ((params->computeQueueMask >> 56) != 0 || static_cast<std::uint32_t>(std::popcount(params->computeQueueMask)) < computeSequences) return 0;
|
|
} else if (params->sequences == 0) {
|
|
return 0;
|
|
}
|
|
const std::uint64_t total = params->sequences + computeSequences;
|
|
if (params->invocations > total * MaxInvocations || params->dispatches > total * MaxDispatches || params->syncPoints > total * MaxSyncPoints || params->waits > total * MaxWaits) return 0;
|
|
std::uint64_t bytes = (NodeBytes * params->workers + 0x317) & 0xfc0;
|
|
bytes = (bytes + 0x13f) & ~std::uint64_t{0x3f};
|
|
bytes = (bytes + 0x0bf) & ~std::uint64_t{0x3f};
|
|
bytes = (bytes + 0x13f) & ~std::uint64_t{0x3f};
|
|
if (computeSequences != 0) {
|
|
const auto queues = static_cast<std::uint64_t>(std::popcount(params->computeQueueMask));
|
|
bytes = (bytes + (params->sequences + queues) * SequenceBytes + 0x267) & ~std::uint64_t{0xff};
|
|
bytes = (bytes + MeasureComputeCommands().budget * queues + 3) & ~std::uint64_t{3};
|
|
bytes = (bytes + 4 * queues + 7) & ~std::uint64_t{7};
|
|
bytes += 8 * queues;
|
|
} else {
|
|
bytes += params->sequences * SequenceBytes;
|
|
}
|
|
const std::uint32_t nodes = params->dispatches + params->syncPoints + params->waits + static_cast<std::uint32_t>(total) + 1;
|
|
return AlignUp(bytes, 64) + std::uint64_t{nodes} * NodeBytes + 0x800;
|
|
}
|
|
|
|
int APS5_VABI _ZN3sce3Job10JobManager10initializeEPvmjjjmPKNS1_12ConfigParamsE(void* self, void* memory, std::uint64_t memorySize, std::uint32_t workers, std::uint32_t sequences, std::uint32_t computeSequences, std::uint64_t computeQueueMask, const ConfigParams* config) {
|
|
return Initialize(self, memory, memorySize, workers, sequences, computeSequences, computeQueueMask, config, true);
|
|
}
|
|
|
|
int APS5_VABI _ZN3sce3Job10JobManager10initializeEPvmjjPKNS1_12ConfigParamsE(void* self, void* memory, std::uint64_t memorySize, std::uint32_t workers, std::uint32_t sequences, const ConfigParams* config) {
|
|
return Initialize(self, memory, memorySize, workers, sequences, 0, 0, config, false);
|
|
}
|
|
|
|
int APS5_VABI _ZN3sce3Job10JobManager8shutdownEv(void* self) {
|
|
Manager* manager = Implementation(self);
|
|
if (manager == nullptr) return ManagerNotInitialized;
|
|
if (currentWorker == manager) throw std::runtime_error("sce::Job::JobManager::shutdown: a worker cannot join itself");
|
|
{
|
|
const std::lock_guard guard(manager->lock);
|
|
manager->running = false;
|
|
}
|
|
StopManager(*manager);
|
|
static_cast<void>(_sceUlobjmgrUnregisterObject_nid_postfix(manager->objectId));
|
|
Implementation(self) = nullptr;
|
|
DestroyManager(manager);
|
|
return 0;
|
|
}
|
|
|
|
ISequenceFactory* APS5_VABI _ZN3sce3Job10JobManager27getSequenceFactoryInterfaceENS1_8PriorityE(void* self, std::uint32_t priority) {
|
|
return FactoryFor(self, priority, false);
|
|
}
|
|
|
|
ISequenceFactory* APS5_VABI _ZN3sce3Job10JobManager34getUnifiedSequenceFactoryInterfaceENS1_8PriorityE(void* self, std::uint32_t priority) {
|
|
return FactoryFor(self, priority, true);
|
|
}
|
|
|
|
int APS5_VABI _ZN3sce3Job10JobManager24setPersistentThreadCountEj(void* self, std::uint32_t count) {
|
|
Manager* manager = Implementation(self);
|
|
if (manager == nullptr) return ManagerNotInitialized;
|
|
if (count > manager->workerCount) return ManagerInvalidArgument;
|
|
const std::lock_guard guard(manager->lock);
|
|
manager->persistentThreads = count;
|
|
manager->workEpoch.fetch_add(1, std::memory_order_release);
|
|
return 0;
|
|
}
|
|
|
|
}
|