feat(agc): add new context state op impl

This commit is contained in:
boykopovar
2026-09-22 19:44:17 +03:00
parent 7fa0e0c469
commit bf7e41bb8b
4 changed files with 127 additions and 5 deletions
@@ -11,6 +11,7 @@ void Require(bool condition, const char* function, const char* reason);
void CheckBits(std::uint64_t value, std::uint64_t mask, const char* function);
void CheckAddress(std::uint64_t address, std::uint32_t alignment, const char* function);
std::uint32_t Header(std::uint32_t opcode, std::uint32_t count, std::uint32_t flags = 0);
void Reserve(CommandBuffer* buffer, std::uint32_t count, const char* function);
std::uint32_t* Allocate(CommandBuffer* buffer, std::uint32_t count, const char* function);
std::uint32_t* Emit(CommandBuffer* buffer, std::uint32_t opcode, std::initializer_list<std::uint32_t> payload, const char* function);
void ValidatePacket(const std::uint32_t* packet, std::uint32_t opcode, std::uint32_t count, const char* function);
@@ -45,7 +45,7 @@ std::uint32_t available(const CommandBuffer& buffer, const char* function) {
}
std::uint32_t* Allocate(CommandBuffer* buffer, std::uint32_t count, const char* function) {
void Reserve(CommandBuffer* buffer, std::uint32_t count, const char* function) {
Require(buffer != nullptr && count != 0, function, "null command buffer or empty allocation");
if (available(*buffer, function) < count) {
Require(buffer->callback != nullptr, function, "command buffer exhausted");
@@ -53,6 +53,10 @@ std::uint32_t* Allocate(CommandBuffer* buffer, std::uint32_t count, const char*
Require(buffer->callback(buffer, count + buffer->reserved_dw, buffer->user_data), function, "command buffer allocation callback failed");
Require(available(*buffer, function) >= count, function, "command buffer allocation callback returned insufficient space");
}
}
std::uint32_t* Allocate(CommandBuffer* buffer, std::uint32_t count, const char* function) {
Reserve(buffer, count, function);
auto* result = buffer->cursor_up;
buffer->cursor_up += count;
return result;
@@ -23,10 +23,37 @@ uint64_t APS5_VABI sceAgcDcbContextStateOpGetSize(uint32_t operation) {
APS5_EXPORT("qj7QZpgr9Uw", sceAgcDcbContextStateAnotherOp);
uint32_t* APS5_VABI sceAgcDcbContextStateAnotherOp(CommandBuffer* buf, uint32_t operation) {
(void)buf;
(void)operation;
NotImplemented_nid_no_patch(__func__);
return nullptr;
Agc::Command::Require(buf != nullptr, __func__, "null command buffer");
Agc::Command::Require(operation <= 3, __func__, "invalid context state operation");
const auto* function = __func__;
std::uint32_t* first = nullptr;
const auto append = [&](std::uint32_t count) {
auto* packet = Agc::Command::WriteNop(buf, count, function);
if (first == nullptr) {
first = packet;
packet[0] = Agc::Command::Header(0x10u, count, 0x68u);
packet[1] = operation;
}
};
if (operation == 0) {
append(5);
return first;
}
if (operation == 2) {
append(3);
}
Agc::Command::Reserve(buf, 22, function);
append(5);
append(8);
append(9);
if (operation != 2) {
append(3);
}
append(2);
if (operation == 3) {
append(5);
}
return first;
}
std::uint32_t APS5_VABI sceAgcDcbSetBaseDispatchIndirectArgsGetSize() {
@@ -16,6 +16,7 @@ extern "C" std::uint32_t* APS5_VABI sceAgcDcbDrawIndexAuto(CommandBuffer* buf, s
extern "C" int APS5_VABI sceAgcWaitRegMemPatchReference(std::uint32_t* cmd, std::uint64_t reference);
extern "C" int APS5_VABI sceAgcGetDataPacketPayloadAddressUnk(std::uint32_t** addr, std::uint32_t* cmd, int type);
extern "C" std::uint32_t* APS5_VABI sceAgcCbSetShRegisterRangeDirect(CommandBuffer* buf, std::uint32_t offset, const std::uint32_t* values, std::uint32_t numValues);
extern "C" std::uint32_t* APS5_VABI sceAgcDcbContextStateAnotherOp(CommandBuffer* buf, std::uint32_t operation);
namespace {
@@ -71,6 +72,94 @@ void testPackets() {
check(exhausted.buffer.cursor_up == exhausted.words.data(), "failed allocation advanced cursor");
}
struct ContextGrowth {
Storage destination;
std::uint32_t* expectedCursor;
std::uint32_t expectedCount;
std::uint32_t calls = 0;
bool success = true;
};
bool APS5_VABI growContext(CommandBuffer* buffer, std::uint32_t count, void* userData) {
auto& growth = *static_cast<ContextGrowth*>(userData);
check(++growth.calls == 1, "unexpected repeated context allocation callback");
check(buffer->cursor_up == growth.expectedCursor, "context callback at wrong packet boundary");
check(count == growth.expectedCount, "incorrect context reservation size");
if (!growth.success) {
return false;
}
buffer->bottom = growth.destination.buffer.bottom;
buffer->top = growth.destination.buffer.top;
buffer->cursor_up = growth.destination.buffer.cursor_up;
buffer->cursor_down = growth.destination.buffer.cursor_down;
return true;
}
void testContextState() {
const std::array<std::array<std::uint32_t, 6>, 4> sizes{{{5}, {5, 8, 9, 3, 2}, {3, 5, 8, 9, 2}, {5, 8, 9, 3, 2, 5}}};
const std::array<std::array<std::uint32_t, 4>, 4> reservations{{{5}, {22, 3, 2}, {3, 22, 2}, {22, 3, 2, 5}}};
const std::array<std::uint32_t, 4> totals{5, 27, 27, 32};
for (std::uint32_t operation = 0; operation < sizes.size(); ++operation) {
for (std::uint32_t capacity = 0; capacity <= totals[operation]; ++capacity) {
Storage source;
source.words.fill(0xdeadbeefu);
std::uint32_t split = 0;
std::uint32_t requested = 0;
for (const auto count : reservations[operation]) {
if (count > capacity - split) {
requested = count;
break;
}
split += count;
}
ContextGrowth growth{{}, source.words.data() + split, requested + 2};
growth.destination.words.fill(0xdeadbeefu);
source.buffer.cursor_down = source.words.data() + capacity + 2;
source.buffer.reserved_dw = 2;
source.buffer.callback = growContext;
source.buffer.user_data = &growth;
auto* first = sceAgcDcbContextStateAnotherOp(&source.buffer, operation);
check(first == (split == 0 ? growth.destination.words.data() : source.words.data()), "incorrect first context packet address");
check(growth.calls == (requested == 0 ? 0u : 1u), "incorrect context callback count");
auto* end = requested == 0 ? source.words.data() + totals[operation] : growth.destination.words.data() + totals[operation] - split;
check(source.buffer.cursor_up == end, "incorrect context cursor advance");
check(*end == 0xdeadbeefu && source.words[split] == 0xdeadbeefu, "context allocation overwrote adjacent memory");
std::uint32_t offset = 0;
for (const auto count : sizes[operation]) {
if (count == 0) {
break;
}
const auto* packet = offset < split ? source.words.data() + offset : growth.destination.words.data() + offset - split;
const auto header = 0xc0001000u | ((count - 2u) << 16u) | (offset == 0 ? 0x68u : 0u);
check(packet[0] == header, "incorrect context packet header");
for (std::uint32_t i = 1; i < count; ++i) {
check(packet[i] == (offset == 0 && i == 1 ? operation : 0u), "incorrect context packet payload");
}
offset += count;
}
}
}
expectFailure([] { sceAgcDcbContextStateAnotherOp(nullptr, 0); });
Storage invalid;
expectFailure([&] { sceAgcDcbContextStateAnotherOp(&invalid.buffer, 4); });
check(invalid.buffer.cursor_up == invalid.words.data(), "invalid context operation advanced cursor");
invalid.buffer.cursor_up = invalid.words.data() + 1;
invalid.buffer.cursor_down = invalid.words.data();
expectFailure([&] { sceAgcDcbContextStateAnotherOp(&invalid.buffer, 0); });
invalid.buffer.cursor_up = invalid.words.data();
expectFailure([&] { sceAgcDcbContextStateAnotherOp(&invalid.buffer, 1); });
ContextGrowth growth{{}, invalid.words.data(), 22};
invalid.buffer.callback = growContext;
invalid.buffer.user_data = &growth;
growth.success = false;
expectFailure([&] { sceAgcDcbContextStateAnotherOp(&invalid.buffer, 1); });
growth.calls = 0;
growth.success = true;
growth.destination.buffer.cursor_down = growth.destination.words.data() + 21;
expectFailure([&] { sceAgcDcbContextStateAnotherOp(&invalid.buffer, 1); });
check(invalid.buffer.cursor_up == growth.destination.words.data(), "failed reservation advanced cursor");
}
void testFlip() {
Storage storage;
storage.words.fill(0xdeadbeefu);
@@ -178,6 +267,7 @@ void testDefaults() {
int main() {
try {
testPackets();
testContextState();
testFlip();
testRegisters();
testRegisterRange();