Skip to content
Merged
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
332 changes: 332 additions & 0 deletions core/cpu/agc/AgcDriverLeafFixture.cpp

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions core/cpu/agc/AgcRegisterDefaults.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "SceShaders.hpp"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <vector>

// Upstream sceAgcGetRegisterDefaults2 returns a host table of host pointers. The
// guest needs the same table at guest addresses, so it is laid out again here from
// the upstream compact data (the same header upstream builds its table from).
namespace Upstream {
struct CompactBank { ShaderRegister* registers; std::uint32_t registerCount; const std::uint16_t* offsets; std::uint32_t pointerCount; };
struct CompactRegisterDefaults { CompactBank banks[4]; std::uint32_t* types; std::uint32_t count; };
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-braces"
#include "prx/libSceAgc/Command/include/RegisterDefaultsData.hpp"
#pragma clang diagnostic pop
}

namespace Cpu {
namespace {
// Guest RegisterDefaults: tables[4] (ShaderRegister**), registerCounts[4], types, count.
struct GuestRegisterDefaults {
std::uint64_t Tables[4];
std::uint32_t RegisterCounts[4];
std::uint64_t Types;
std::uint32_t Count;
};
static_assert(sizeof(GuestRegisterDefaults) == 64 && offsetof(GuestRegisterDefaults, Count) == 0x38);
std::size_t align8(std::size_t value) { return (value + 7) & ~std::size_t{7}; }
}

std::vector<std::byte> AgcRegisterDefaultsGuestImage(std::uint32_t version, bool internal, std::uint64_t base) {
if (version >= 14) throw std::runtime_error("SCE AGC: unsupported register defaults version");
const auto* source = (internal ? Upstream::agcInternalRegDefaultsByVersion : Upstream::agcPublicRegDefaultsByVersion)[version];
std::vector<std::byte> image(sizeof(GuestRegisterDefaults));
const auto append = [&](const void* data, std::size_t bytes) {
const auto at = align8(image.size());
image.resize(at + bytes);
if (bytes) std::memcpy(image.data() + at, data, bytes);
return base + at;
};
GuestRegisterDefaults header{};
for (std::size_t bank = 0; bank < 4; ++bank) {
const auto& input = source->banks[bank];
header.RegisterCounts[bank] = input.registerCount;
if (!input.pointerCount) continue;
const auto registers = append(input.registers, std::size_t{input.registerCount} * sizeof(ShaderRegister));
std::vector<std::uint64_t> pointers;
for (std::uint32_t index = 0; index < input.pointerCount; ++index) {
if (input.offsets[index] >= input.registerCount) throw std::runtime_error("SCE AGC: register defaults pointer exceeds bank");
pointers.push_back(registers + std::uint64_t{input.offsets[index]} * sizeof(ShaderRegister));
}
header.Tables[bank] = append(pointers.data(), pointers.size() * sizeof(std::uint64_t));
}
header.Count = source->count;
if (source->count) header.Types = append(source->types, std::size_t{source->count} * sizeof(std::uint32_t));
std::memcpy(image.data(), &header, sizeof(header));
return image;
}
}
19 changes: 19 additions & 0 deletions core/cpu/agc/AgcUpstreamBuilders.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "SceTypes.hpp"
#include "SceShaders.hpp"
#include "prx/libSceAgc/Misc/include/PacketInfo.hpp"
#include <cstdint>

Expand Down Expand Up @@ -192,4 +193,22 @@ int APS5_VABI sceAgcSetShRegIndirectPatchSetAddress(std::uint32_t* cmd, const vo
int APS5_VABI sceAgcWaitRegMemPatchReference(std::uint32_t* cmd, std::uint64_t reference);
int APS5_VABI sceAgcDmaDataPatchSetDstAddressOrOffset(std::uint32_t* cmd, std::uint64_t address);
int APS5_VABI sceAgcSetShRegIndirectPatchSetNumRegisters(std::uint32_t* cmd, std::uint32_t numRegs);
int APS5_VABI sceAgcBranchPatchSetThenTarget_0300(std::uint32_t* cmd, std::uint8_t cachePolicy, const volatile std::uint32_t* target, std::uint32_t sizeInDwords);
int APS5_VABI sceAgcBranchPatchSetElseTarget_0300(std::uint32_t* cmd, std::uint8_t cachePolicy, const volatile std::uint32_t* target, std::uint32_t sizeInDwords);
int APS5_VABI sceAgcAcquireMemSetEngine(std::uint32_t* cmd, std::uint32_t engine);
// Marker spans: the text is `length` bytes, not NUL-terminated.
std::uint32_t* APS5_VABI sceAgcDcbSetMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color);
std::uint32_t* APS5_VABI sceAgcDcbPushMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color);
std::uint32_t* APS5_VABI sceAgcAcbSetMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color);
std::uint32_t* APS5_VABI sceAgcAcbPushMarkerSpan(CommandBuffer* buf, const char* str, std::uint32_t length, std::uint32_t color);
std::uint32_t APS5_VABI sceAgcDcbBeginOcclusionQueryGetSize(std::uint8_t type);
std::uint32_t APS5_VABI sceAgcDcbEndOcclusionQueryGetSize();
int APS5_VABI sceAgcSetShaderInstrumentation(std::uint32_t flags);
std::uint32_t APS5_VABI sceAgcGetShaderInstrumentation();
// Shader helpers: they read Shader/special-register structs and write register
// pairs, so the bridge passes host copies (see SceAgcImports.cpp).
void* APS5_VABI sceAgcGetRegisterDefaults2(std::uint32_t version);
void* APS5_VABI sceAgcGetRegisterDefaults2Internal(std::uint32_t version);
int APS5_VABI sceAgcCreatePrimState(ShaderRegister* cxRegs, ShaderRegister* ucRegs, const Shader* hs, const Shader* gs, std::uint32_t primType);
int APS5_VABI sceAgcUpdatePrimState(ShaderRegister* cxRegs, ShaderRegister* ucRegs, std::uint32_t primType);
}
13 changes: 11 additions & 2 deletions core/cpu/agc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if(TARGET anyps5_cpu AND TARGET anyps5_metal_native_execution)
set(agcUpstream "${PROJECT_SOURCE_DIR}/core/libs/prx/libSceAgc")
add_library(anyps5_cpu_agc STATIC
"${PROJECT_SOURCE_DIR}/core/cpu/src/SceAgcImports.cpp"
"${CMAKE_CURRENT_LIST_DIR}/NativeAgcBackend.cpp"
"${CMAKE_CURRENT_LIST_DIR}/NativeAgcBackend.cpp" "${CMAKE_CURRENT_LIST_DIR}/AgcRegisterDefaults.cpp"
"${agcUpstream}/Shader/src/ShaderUtils.cpp"
"${agcUpstream}/Command/src/Packet.cpp" "${agcUpstream}/Command/src/Memory.cpp"
"${agcUpstream}/Command/src/Control.cpp" "${agcUpstream}/DcbFlow/src/Memory.cpp"
Expand All @@ -22,12 +22,15 @@ if(TARGET anyps5_cpu AND TARGET anyps5_metal_native_execution)
"${agcUpstream}/Predication/src/Predication.cpp" "${agcUpstream}/Command/src/RegisterDefaults.cpp"
"${agcUpstream}/Patch/src/Branch.cpp" "${agcUpstream}/Patch/src/CondExec.cpp" "${agcUpstream}/Patch/src/Dma.cpp"
"${agcUpstream}/Patch/src/IndirectRegisters.cpp" "${agcUpstream}/Patch/src/Jump.cpp" "${agcUpstream}/Patch/src/Nop.cpp"
"${agcUpstream}/Patch/src/QueueEndOfPipeAction.cpp" "${agcUpstream}/Patch/src/Rewind.cpp" "${agcUpstream}/Patch/src/WaitRegMem.cpp")
"${agcUpstream}/Patch/src/QueueEndOfPipeAction.cpp" "${agcUpstream}/Patch/src/Rewind.cpp" "${agcUpstream}/Patch/src/WaitRegMem.cpp"
"${agcUpstream}/Misc/src/RegisterDefaults.cpp" "${agcUpstream}/Shader/src/PrimState.cpp"
"${PROJECT_SOURCE_DIR}/core/cpu/src/upstream-prx/AgcDriver.cpp")
target_compile_features(anyps5_cpu_agc PUBLIC cxx_std_20)
target_compile_options(anyps5_cpu_agc PRIVATE -Wall -Wextra -Wpedantic)
target_include_directories(anyps5_cpu_agc PUBLIC
"${PROJECT_SOURCE_DIR}/core/cpu/include"
"${CMAKE_CURRENT_LIST_DIR}")
target_include_directories(anyps5_cpu_agc PRIVATE "${PROJECT_SOURCE_DIR}/core/libs/nid/include")
target_link_libraries(anyps5_cpu_agc PUBLIC anyps5_cpu anyps5_metal_native_execution)
if(BUILD_TESTING)
find_program(agcGuestClang clang HINTS /opt/homebrew/opt/llvm/bin REQUIRED)
Expand Down Expand Up @@ -69,6 +72,12 @@ if(TARGET anyps5_cpu AND TARGET anyps5_metal_native_execution)
target_link_libraries(anyps5_cpu_agc_builder_fixture PRIVATE anyps5_cpu_agc)
add_test(NAME anyps5_cpu_agc_builder COMMAND anyps5_cpu_agc_builder_fixture)
set_tests_properties(anyps5_cpu_agc_builder PROPERTIES TIMEOUT 30)
add_executable(anyps5_cpu_agc_driver_leaf_fixture "${CMAKE_CURRENT_LIST_DIR}/AgcDriverLeafFixture.cpp")
target_compile_features(anyps5_cpu_agc_driver_leaf_fixture PRIVATE cxx_std_20)
target_compile_options(anyps5_cpu_agc_driver_leaf_fixture PRIVATE -Wall -Wextra -Wpedantic)
target_link_libraries(anyps5_cpu_agc_driver_leaf_fixture PRIVATE anyps5_cpu_agc)
add_test(NAME anyps5_cpu_agc_driver_leaf COMMAND anyps5_cpu_agc_driver_leaf_fixture)
set_tests_properties(anyps5_cpu_agc_driver_leaf PROPERTIES TIMEOUT 30)
# The direct CPU+Metal build does not define platform components. Use
# the actual existing event provider source in this focused fixture,
# preserving both direct-build support and wrapper target ordering.
Expand Down
9 changes: 8 additions & 1 deletion core/cpu/include/cpu/SceAgcImports.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ enum class AgcAbiContract {
// pointers are never dereferenced by the host; the bridge marshals every
// memory operand and runs the upstream encoder on host-side copies.
UpstreamPacketSizes, UpstreamPatches, UpstreamMarkers, UpstreamRegisters, UpstreamDraw,
UpstreamAcb, UpstreamCb, UpstreamDcb, UpstreamPacketInfo
UpstreamAcb, UpstreamCb, UpstreamDcb, UpstreamPacketInfo,
// Shader helpers run on host copies of guest Shader structs; register
// defaults are published as guest tables.
UpstreamShader,
// libSceAgcDriver leaf exports through prx bridge descriptors.
UpstreamDriver,
// Compute-queue flip and rendering wait: the Metal driver's packets.
AcbSetFlipCommandBuffer56, AcbWaitUntilSafeForRenderingCommandBuffer56
};

// These policies constrain the admitted target use, independently of the source
Expand Down
Loading
Loading