Merge pull request #538 from DotDebian/feat/ampr-command-buffer-markers

feat(ampr): implement the command buffer markers
This commit is contained in:
Dean
2026-10-05 10:23:03 +13:00
committed by GitHub
6 changed files with 248 additions and 30 deletions
+21
View File
@@ -0,0 +1,21 @@
add_library(libSceAmpr SHARED EXCLUDE_FROM_ALL Export.cpp)
target_include_directories(libSceAmpr PRIVATE ${LIBS_INCLUDE_DIR})
configure_windows_unwind(libSceAmpr)
if(MINGW)
foreach(runtimeLinkFlag IN ITEMS -static-libgcc -static-libstdc++ -static)
string(REPLACE "${runtimeLinkFlag}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
endforeach()
endif()
if(BUILD_TESTING)
add_test_executable(guest_ampr_tests ${LIBS_INCLUDE_DIR}/tests/GuestAmpr.cpp)
target_include_directories(guest_ampr_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_ampr_tests PRIVATE libSceAmpr libkernel libc)
configure_windows_unwind(guest_ampr_tests)
add_test(NAME guest_ampr_markers COMMAND guest_ampr_tests)
if(WIN32)
set_tests_properties(guest_ampr_markers PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAmpr>")
endif()
endif()
+48 -30
View File
@@ -6,6 +6,7 @@
#include <cstring>
static constexpr int SCE_AMPR_ERROR_BUFFER_FULL = 0x8002001C;
static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016;
static int Append(Apr::CommandBufferObject* buffer, const void* command, uint32_t bytes) {
if (!buffer->base || bytes > buffer->size - buffer->offset) return SCE_AMPR_ERROR_BUFFER_FULL;
@@ -22,6 +23,29 @@ static int AppendCommand(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, T
return Append(buffer, &command, sizeof(command));
}
static std::uint64_t MarkerBytes(const char* text) {
return sizeof(Apr::MarkerCommand) + ((std::strlen(text) + 8) & ~std::uint64_t{7});
}
static std::uint64_t MeasureMarker(const char* text) {
if (!text) return static_cast<std::uint32_t>(SCE_KERNEL_ERROR_EINVAL);
return MarkerBytes(text);
}
static int AppendMarker(Apr::CommandBufferObject* buffer, Apr::Opcode opcode, const char* text) {
if (!buffer || !text) return SCE_KERNEL_ERROR_EINVAL;
const std::uint64_t bytes = MarkerBytes(text);
if (!buffer->base || bytes > buffer->size - buffer->offset) return SCE_AMPR_ERROR_BUFFER_FULL;
const Apr::MarkerCommand command{{opcode, static_cast<std::uint32_t>(bytes)}};
std::uint8_t* destination = buffer->base + buffer->offset;
std::memset(destination, 0, bytes);
std::memcpy(destination, &command, sizeof(command));
std::memcpy(destination + sizeof(command), text, std::strlen(text));
buffer->offset += static_cast<std::uint32_t>(bytes);
++buffer->numCommands;
return 0;
}
extern "C" {
int APS5_VABI sceAmprCommandBufferWriteAddressOnCompletion(Apr::CommandBufferObject* buffer, volatile std::uint64_t* address, std::uint64_t value) {
@@ -182,19 +206,17 @@ int APS5_VABI sceAmprCommandBufferNopWithData() {
return 0;
}
int APS5_VABI sceAmprCommandBufferPopMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceAmprCommandBufferPopMarker(Apr::CommandBufferObject* buffer) {
return AppendCommand(buffer, Apr::Opcode::PopMarker, Apr::MarkerCommand{});
}
int APS5_VABI sceAmprCommandBufferPushMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceAmprCommandBufferPushMarker(Apr::CommandBufferObject* buffer, const char* text) {
return AppendMarker(buffer, Apr::Opcode::PushMarker, text);
}
int APS5_VABI sceAmprCommandBufferPushMarkerWithColor() {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceAmprCommandBufferPushMarkerWithColor(Apr::CommandBufferObject* buffer, const char* text, std::uint32_t color) {
(void)color;
return AppendMarker(buffer, Apr::Opcode::PushMarker, text);
}
int APS5_VABI sceAmprCommandBufferReset(Apr::CommandBufferObject* buffer) {
@@ -211,14 +233,13 @@ int APS5_VABI sceAmprCommandBufferSetBuffer(Apr::CommandBufferObject* buffer, vo
return 0;
}
int APS5_VABI sceAmprCommandBufferSetMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceAmprCommandBufferSetMarker(Apr::CommandBufferObject* buffer, const char* text) {
return AppendMarker(buffer, Apr::Opcode::SetMarker, text);
}
int APS5_VABI sceAmprCommandBufferSetMarkerWithColor() {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceAmprCommandBufferSetMarkerWithColor(Apr::CommandBufferObject* buffer, const char* text, const std::uint32_t* color) {
if (!color) return SCE_KERNEL_ERROR_EINVAL;
return AppendMarker(buffer, Apr::Opcode::SetMarker, text);
}
int APS5_VABI sceAmprCommandBufferWaitOnAddress_04_00() {
@@ -290,19 +311,17 @@ int APS5_VABI sceAmprMeasureCommandSizeNopWithData() {
return 0;
}
int APS5_VABI sceAmprMeasureCommandSizePopMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePopMarker() {
return sizeof(Apr::MarkerCommand);
}
int APS5_VABI sceAmprMeasureCommandSizePushMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePushMarker(const char* text) {
return MeasureMarker(text);
}
int APS5_VABI sceAmprMeasureCommandSizePushMarkerWithColor() {
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePushMarkerWithColor(const char* text, std::uint32_t color) {
(void)color;
return MeasureMarker(text);
}
uint32_t APS5_VABI sceAmprMeasureCommandSizeReadFile(void) {
@@ -329,14 +348,13 @@ int APS5_VABI sceAmprMeasureCommandSizeResetGatherScatterState() {
return 0;
}
int APS5_VABI sceAmprMeasureCommandSizeSetMarker() {
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeSetMarker(const char* text) {
return MeasureMarker(text);
}
int APS5_VABI sceAmprMeasureCommandSizeSetMarkerWithColor() {
NotImplemented_nid_no_patch(__func__);
return 0;
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeSetMarkerWithColor(const char* text, std::uint32_t color) {
(void)color;
return MeasureMarker(text);
}
int APS5_VABI sceAmprMeasureCommandSizeWaitOnAddress_04_00() {
@@ -33,6 +33,9 @@ enum class Opcode : std::uint32_t {
WriteAddressFromTimeCounter = 7,
WriteAddressFromCounter = 8,
WriteAddressFromCounterPair = 9,
PushMarker = 10,
PopMarker = 11,
SetMarker = 12,
};
struct CommandHeader {
@@ -87,6 +90,10 @@ struct WriteAddressFromCounterCommand {
std::uint32_t counter1;
};
struct MarkerCommand {
CommandHeader header;
};
}
#endif
+3
View File
@@ -194,6 +194,9 @@ void _execute(const Apr::CommandBufferObject& buffer) {
if (header.bytes < sizeof(header) || cursor + header.bytes > buffer.offset) throw std::runtime_error("APR: malformed command");
switch (header.opcode) {
case Apr::Opcode::Nop:
case Apr::Opcode::PushMarker:
case Apr::Opcode::PopMarker:
case Apr::Opcode::SetMarker:
break;
case Apr::Opcode::ReadFile: {
Apr::ReadFileCommand command;
+168
View File
@@ -0,0 +1,168 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/Apr/include/AprCommandBuffer.hpp"
#include <array>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
extern "C" {
int APS5_VABI sceAmprCommandBufferConstructor(Apr::CommandBufferObject*);
int APS5_VABI sceAmprAprCommandBufferConstructor(Apr::CommandBufferObject*, std::uint64_t*, std::uint64_t*);
int APS5_VABI sceAmprCommandBufferSetBuffer(Apr::CommandBufferObject*, void*, std::uint32_t);
std::uint32_t APS5_VABI sceAmprCommandBufferGetCurrentOffset(const Apr::CommandBufferObject*);
std::uint32_t APS5_VABI sceAmprCommandBufferGetNumCommands(const Apr::CommandBufferObject*);
int APS5_VABI sceAmprCommandBufferWriteAddressOnCompletion(Apr::CommandBufferObject*, volatile std::uint64_t*, std::uint64_t);
int APS5_VABI sceAmprCommandBufferPushMarker(Apr::CommandBufferObject*, const char*);
int APS5_VABI sceAmprCommandBufferPushMarkerWithColor(Apr::CommandBufferObject*, const char*, std::uint32_t);
int APS5_VABI sceAmprCommandBufferPopMarker(Apr::CommandBufferObject*);
int APS5_VABI sceAmprCommandBufferSetMarker(Apr::CommandBufferObject*, const char*);
int APS5_VABI sceAmprCommandBufferSetMarkerWithColor(Apr::CommandBufferObject*, const char*, const std::uint32_t*);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePushMarker(const char*);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePushMarkerWithColor(const char*, std::uint32_t);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizePopMarker();
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeSetMarker(const char*);
std::uint64_t APS5_VABI sceAmprMeasureCommandSizeSetMarkerWithColor(const char*, std::uint32_t);
int APS5_VABI sceKernelAprSubmitCommandBuffer(const Apr::CommandBufferObject*, std::uint32_t);
}
static void Require(bool value) { if (!value) std::abort(); }
namespace {
constexpr int invalidArgument = static_cast<int>(0x80020016);
constexpr int bufferFull = static_cast<int>(0x8002001C);
constexpr std::uint32_t color = 0xFF8040u;
struct Recorder {
alignas(8) std::array<std::uint8_t, 4096> memory{};
Apr::CommandBufferObject buffer{};
std::uint64_t gatherState = 0;
std::uint64_t scatterState = 0;
explicit Recorder(std::uint32_t size = 4096) {
Require(sceAmprCommandBufferConstructor(&buffer) == 0);
Require(sceAmprAprCommandBufferConstructor(&buffer, &gatherState, &scatterState) == 0);
Require(sceAmprCommandBufferSetBuffer(&buffer, memory.data(), size) == 0);
}
std::uint32_t Offset() const { return sceAmprCommandBufferGetCurrentOffset(&buffer); }
std::uint32_t Commands() const { return sceAmprCommandBufferGetNumCommands(&buffer); }
};
void RequireAppended(const Recorder& recorder, std::uint32_t offset, std::uint32_t commands, Apr::Opcode opcode, std::uint64_t measured) {
Require(recorder.Offset() == offset + measured);
Require(recorder.Commands() == commands + 1);
Apr::CommandHeader header;
std::memcpy(&header, recorder.memory.data() + offset, sizeof(header));
Require(header.opcode == opcode && header.bytes == measured);
}
void RequireRecorded(const Recorder& recorder, std::uint32_t offset, std::uint32_t commands, Apr::Opcode opcode, std::uint64_t measured, const std::string& text) {
RequireAppended(recorder, offset, commands, opcode, measured);
Require(measured >= sizeof(Apr::MarkerCommand) + text.size() + 1);
Require(std::memcmp(recorder.memory.data() + offset + sizeof(Apr::MarkerCommand), text.c_str(), text.size() + 1) == 0);
}
void TestRecording(const std::string& text) {
Recorder recorder;
const char* marker = text.c_str();
auto offset = recorder.Offset();
auto commands = recorder.Commands();
Require(sceAmprCommandBufferPushMarker(&recorder.buffer, marker) == 0);
RequireRecorded(recorder, offset, commands, Apr::Opcode::PushMarker, sceAmprMeasureCommandSizePushMarker(marker), text);
offset = recorder.Offset();
commands = recorder.Commands();
Require(sceAmprCommandBufferPushMarkerWithColor(&recorder.buffer, marker, color) == 0);
RequireRecorded(recorder, offset, commands, Apr::Opcode::PushMarker, sceAmprMeasureCommandSizePushMarkerWithColor(marker, color), text);
offset = recorder.Offset();
commands = recorder.Commands();
Require(sceAmprCommandBufferSetMarker(&recorder.buffer, marker) == 0);
RequireRecorded(recorder, offset, commands, Apr::Opcode::SetMarker, sceAmprMeasureCommandSizeSetMarker(marker), text);
offset = recorder.Offset();
commands = recorder.Commands();
Require(sceAmprCommandBufferSetMarkerWithColor(&recorder.buffer, marker, &color) == 0);
RequireRecorded(recorder, offset, commands, Apr::Opcode::SetMarker, sceAmprMeasureCommandSizeSetMarkerWithColor(marker, color), text);
offset = recorder.Offset();
commands = recorder.Commands();
Require(sceAmprCommandBufferPopMarker(&recorder.buffer) == 0);
RequireAppended(recorder, offset, commands, Apr::Opcode::PopMarker, sceAmprMeasureCommandSizePopMarker());
}
void TestRejectedArguments() {
Recorder recorder;
Require(sceAmprCommandBufferPushMarker(&recorder.buffer, nullptr) == invalidArgument);
Require(sceAmprCommandBufferPushMarkerWithColor(&recorder.buffer, nullptr, color) == invalidArgument);
Require(sceAmprCommandBufferSetMarker(&recorder.buffer, nullptr) == invalidArgument);
Require(sceAmprCommandBufferSetMarkerWithColor(&recorder.buffer, nullptr, &color) == invalidArgument);
Require(sceAmprCommandBufferSetMarkerWithColor(&recorder.buffer, "frame", nullptr) == invalidArgument);
Require(sceAmprCommandBufferPushMarker(nullptr, "frame") == invalidArgument);
Require(sceAmprCommandBufferPushMarkerWithColor(nullptr, "frame", color) == invalidArgument);
Require(sceAmprCommandBufferSetMarker(nullptr, "frame") == invalidArgument);
Require(sceAmprCommandBufferSetMarkerWithColor(nullptr, "frame", &color) == invalidArgument);
Require(sceAmprCommandBufferPopMarker(nullptr) == invalidArgument);
Require(recorder.Offset() == 0 && recorder.Commands() == 0);
const auto rejected = static_cast<std::uint64_t>(static_cast<std::uint32_t>(invalidArgument));
Require(sceAmprMeasureCommandSizePushMarker(nullptr) == rejected);
Require(sceAmprMeasureCommandSizePushMarkerWithColor(nullptr, color) == rejected);
Require(sceAmprMeasureCommandSizeSetMarker(nullptr) == rejected);
Require(sceAmprMeasureCommandSizeSetMarkerWithColor(nullptr, color) == rejected);
}
void TestFullBuffer() {
const char* marker = "streaming";
const auto measured = static_cast<std::uint32_t>(sceAmprMeasureCommandSizePushMarker(marker));
Recorder exact(measured);
Require(sceAmprCommandBufferPushMarker(&exact.buffer, marker) == 0);
Require(exact.Offset() == measured && exact.Commands() == 1);
Require(sceAmprCommandBufferPopMarker(&exact.buffer) == bufferFull);
Require(sceAmprCommandBufferSetMarker(&exact.buffer, "") == bufferFull);
Require(exact.Offset() == measured && exact.Commands() == 1);
Recorder small(measured - 4);
Require(sceAmprCommandBufferPushMarker(&small.buffer, marker) == bufferFull);
Require(sceAmprCommandBufferSetMarkerWithColor(&small.buffer, marker, &color) == bufferFull);
Require(small.Offset() == 0 && small.Commands() == 0);
Apr::CommandBufferObject unbound{};
Require(sceAmprCommandBufferConstructor(&unbound) == 0);
Require(sceAmprCommandBufferPushMarker(&unbound, marker) == bufferFull);
Require(sceAmprCommandBufferPopMarker(&unbound) == bufferFull);
}
void TestSubmission() {
Recorder recorder;
std::uint64_t first = 0;
std::uint64_t second = 0;
Require(sceAmprCommandBufferPushMarker(&recorder.buffer, "level") == 0);
Require(sceAmprCommandBufferWriteAddressOnCompletion(&recorder.buffer, &first, 0x1111) == 0);
Require(sceAmprCommandBufferSetMarkerWithColor(&recorder.buffer, "textures", &color) == 0);
Require(sceAmprCommandBufferPushMarkerWithColor(&recorder.buffer, std::string(200, 'm').c_str(), color) == 0);
Require(sceAmprCommandBufferSetMarker(&recorder.buffer, "") == 0);
Require(sceAmprCommandBufferPopMarker(&recorder.buffer) == 0);
Require(sceAmprCommandBufferPopMarker(&recorder.buffer) == 0);
Require(sceAmprCommandBufferWriteAddressOnCompletion(&recorder.buffer, &second, 0x2222) == 0);
Require(recorder.Commands() == 8);
Require(sceKernelAprSubmitCommandBuffer(&recorder.buffer, 0) == 0);
Require(first == 0x1111 && second == 0x2222);
}
}
int main() {
TestRecording("frame");
TestRecording("");
TestRecording("1234567");
TestRecording("12345678");
TestRecording(std::string(300, 'a'));
TestRejectedArguments();
TestFullBuffer();
TestSubmission();
return 0;
}
+1
View File
@@ -24,6 +24,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceVideoOutOpen](../../core/libs/prx/libSceVideoOut/src/Output.cpp) (libSceVideoOut) - the open param's first word is unknown (PPSA21564 passes 16; it is not the byte size, since the affinity mask is at offset 16); only 16 is accepted. Whether the param continues past offset 24 is also unknown
- [AMPR WaitOnAddress / WaitOnCounter](../../core/libs/prx/libkernel/Apr/src/Apr.cpp) (libSceAmpr) - compare encoding assumed to be the WAIT_REG_MEM one (0 always, 1 <, 2 <=, 3 ==, 4 !=, 5 >=, 6 >)
- [AMPR markers](../../core/libs/prx/libSceAmpr/Export.cpp) (libSceAmpr) - signatures taken from the [ampr_emu](https://github.com/drakmor/ampr_emu) reimplementation, not confirmed on a title: `sceAmprCommandBufferSetMarkerWithColor` takes the color by pointer, the push and measure variants by value. Colors are not recorded
- [sceAgcSetSubmitMode](../../core/libs/prx/libSceAgc/Misc/src/Suspend.cpp) (libSceAgc) - mode values unknown; only 0 is accepted
- [zARR5aCmkoY](../../core/libs/prx/libSceAgc/DcbFlow/src/Control.cpp) (libSceAgc) - unknown name, signature
- [qj7QZpgr9Uw](../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - unknown name