mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-20 02:47:02 +02:00
241 lines
8.2 KiB
C++
241 lines
8.2 KiB
C++
#include "common/assert.h"
|
|
#include "common/emulatorConfig.h"
|
|
#include "graphics/guest_gpu/tile.h"
|
|
#include "libs/agc.h"
|
|
#include "libs/errno.h"
|
|
#include "libs/hmd2.h"
|
|
#include "libs/libs.h"
|
|
#include "loader/symbolDatabase.h"
|
|
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
#include <new>
|
|
|
|
namespace Libs {
|
|
|
|
LIB_VERSION("Hmd2", 1, "Hmd2", 1, 1);
|
|
|
|
namespace Hmd2 {
|
|
|
|
constexpr int32_t ERROR_ALREADY_INITIALIZED = -1972240383; // 0x8a720001
|
|
constexpr int32_t ERROR_NOT_INITIALIZED = -1972240382; // 0x8a720002
|
|
constexpr int32_t ERROR_PARAMETER_NULL = -1972240376; // 0x8a720008
|
|
constexpr int32_t ERROR_REPROJECTION_ALREADY_INITIALIZED = -1972240372; // 0x8a72000c
|
|
constexpr int32_t ERROR_REPROJECTION_IN_VR_MODE = -1972240356; // 0x8a72001c
|
|
|
|
struct SceHmd2InitializeParam {
|
|
void* reserved0;
|
|
uint8_t reserved[8];
|
|
};
|
|
|
|
struct SceHmd2DeviceInformation {
|
|
uint32_t status;
|
|
uint32_t reserve0;
|
|
struct {
|
|
struct {
|
|
uint32_t width;
|
|
uint32_t height;
|
|
} panelResolution;
|
|
struct {
|
|
uint16_t refreshRate90Hz;
|
|
uint16_t refreshRate120Hz;
|
|
} flipToDisplayLatency;
|
|
} deviceInfo;
|
|
uint8_t hmuMount;
|
|
uint8_t lensSeparationDistance;
|
|
uint8_t reserve1[2];
|
|
float virtualImageDistance;
|
|
};
|
|
|
|
struct SceHmd2FieldOfView {
|
|
float tanOut;
|
|
float tanIn;
|
|
float tanTop;
|
|
float tanBottom;
|
|
};
|
|
|
|
struct SceHmd2ReprojectionInitializeParam {
|
|
void* pReprojectionBuff;
|
|
void* pDisplayBuff;
|
|
int32_t threadPriority;
|
|
int32_t cpuAffinityMask;
|
|
uint32_t pipeId;
|
|
uint32_t queueId;
|
|
uint32_t type;
|
|
void* pSeeThroughBuff;
|
|
int32_t reprojectionTiming;
|
|
uint32_t reserved[5];
|
|
};
|
|
|
|
static_assert(sizeof(SceHmd2InitializeParam) == 16);
|
|
static_assert(offsetof(SceHmd2InitializeParam, reserved) == 8);
|
|
static_assert(sizeof(SceHmd2DeviceInformation) == 28);
|
|
static_assert(offsetof(SceHmd2DeviceInformation, deviceInfo) == 8);
|
|
static_assert(offsetof(SceHmd2DeviceInformation, hmuMount) == 20);
|
|
static_assert(offsetof(SceHmd2DeviceInformation, virtualImageDistance) == 24);
|
|
static_assert(sizeof(SceHmd2FieldOfView) == 16);
|
|
static_assert(alignof(SceHmd2FieldOfView) == 4);
|
|
static_assert(sizeof(SceHmd2ReprojectionInitializeParam) == 72);
|
|
static_assert(alignof(SceHmd2ReprojectionInitializeParam) == 8);
|
|
static_assert(offsetof(SceHmd2ReprojectionInitializeParam, threadPriority) == 16);
|
|
static_assert(offsetof(SceHmd2ReprojectionInitializeParam, type) == 32);
|
|
static_assert(offsetof(SceHmd2ReprojectionInitializeParam, pSeeThroughBuff) == 40);
|
|
static_assert(offsetof(SceHmd2ReprojectionInitializeParam, reprojectionTiming) == 48);
|
|
static_assert(offsetof(SceHmd2ReprojectionInitializeParam, reserved) == 52);
|
|
|
|
static std::atomic<bool> g_initialized = false;
|
|
static std::mutex g_reprojection_mutex;
|
|
static std::atomic<ReprojectionState*> g_reprojection_state {nullptr};
|
|
|
|
ReprojectionState* GetReprojectionState() {
|
|
return g_reprojection_state.load(std::memory_order_acquire);
|
|
}
|
|
|
|
static Graphics::SizeAlign ReprojectionDisplaySize() {
|
|
// The host display target is one uncompressed tiled 10-bit panel surface.
|
|
Graphics::TileSizeAlign size {};
|
|
Graphics::TileGetTextureTotalSize(Graphics::Prospero::BufferFormat::k10_10_10_2UNorm,
|
|
PANEL_WIDTH, PANEL_HEIGHT, 1, 1,
|
|
Graphics::Prospero::TileMode::kRenderTarget, false, size);
|
|
EXIT_IF(size.size == 0 || size.align == 0);
|
|
return {size.size, size.align};
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI Hmd2Initialize(const SceHmd2InitializeParam* param) {
|
|
PRINT_NAME();
|
|
if (param == nullptr) {
|
|
return ERROR_PARAMETER_NULL;
|
|
}
|
|
if (g_initialized.exchange(true)) {
|
|
return ERROR_ALREADY_INITIALIZED;
|
|
}
|
|
LOGF("Hmd2: initialized, virtual headset=%s\n", Config::VrEnabled() ? "enabled" : "disabled");
|
|
return OK;
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI Hmd2GetDeviceInformation(SceHmd2DeviceInformation* info) {
|
|
PRINT_NAME();
|
|
if (!g_initialized.load()) {
|
|
return ERROR_NOT_INITIALIZED;
|
|
}
|
|
if (info == nullptr) {
|
|
return ERROR_PARAMETER_NULL;
|
|
}
|
|
*info = {};
|
|
if (Config::VrEnabled()) {
|
|
// Virtual headset defaults: READY, MOUNT, 63 mm lens separation.
|
|
info->hmuMount = 1;
|
|
info->lensSeparationDistance = 63;
|
|
// Headset characteristics: 4000x2040 panel and 2 m virtual image distance.
|
|
info->deviceInfo.panelResolution = {PANEL_WIDTH, PANEL_HEIGHT};
|
|
info->virtualImageDistance = 2.0f;
|
|
// Host presentation has no emulated headset scan-out delay; latency remains zero.
|
|
} else {
|
|
info->status = 2; // SCE_HMD2_DEVICE_STATUS_NOT_DETECTED
|
|
}
|
|
LOGF("Hmd2: device status=%u panel=%ux%u mounted=%u\n", info->status,
|
|
info->deviceInfo.panelResolution.width, info->deviceInfo.panelResolution.height,
|
|
info->hmuMount);
|
|
return OK;
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI Hmd2GetFieldOfViewWithoutHandle(SceHmd2FieldOfView* fov) {
|
|
PRINT_NAME();
|
|
if (!g_initialized.load()) {
|
|
return ERROR_NOT_INITIALIZED;
|
|
}
|
|
if (fov == nullptr) {
|
|
return ERROR_PARAMETER_NULL;
|
|
}
|
|
// Virtual ER15 field of view: 55/42.5/51.2/51.2 degrees.
|
|
// These tangents describe that geometry, not bit-exact firmware output.
|
|
*fov = {1.4281480312f, 0.9163311720f, 1.2437491417f, 1.2437491417f};
|
|
LOGF("Hmd2: FOV tangents out=%f in=%f top=%f bottom=%f\n", fov->tanOut, fov->tanIn,
|
|
fov->tanTop, fov->tanBottom);
|
|
return OK;
|
|
}
|
|
|
|
static Graphics::SizeAlign KYTY_SYSV_ABI Hmd2ReprojectionQueryBufferSizeAlign() {
|
|
const Graphics::SizeAlign size {sizeof(ReprojectionState), alignof(ReprojectionState)};
|
|
LOGF("Hmd2: reprojection work size=%" PRIu64 " alignment=%zu\n", size.m_size, size.m_align);
|
|
return size;
|
|
}
|
|
|
|
static Graphics::SizeAlign KYTY_SYSV_ABI Hmd2ReprojectionQueryDisplayBufferSizeAlign() {
|
|
const auto size = ReprojectionDisplaySize();
|
|
LOGF("Hmd2: reprojection display size=%" PRIu64 " alignment=%zu\n", size.m_size,
|
|
size.m_align);
|
|
return size;
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI
|
|
Hmd2ReprojectionInitialize(const SceHmd2ReprojectionInitializeParam* param, void*) {
|
|
std::scoped_lock lock {g_reprojection_mutex};
|
|
if (GetReprojectionState() != nullptr) {
|
|
return ERROR_REPROJECTION_ALREADY_INITIALIZED;
|
|
}
|
|
if (param == nullptr || param->pReprojectionBuff == nullptr || param->pDisplayBuff == nullptr) {
|
|
return ERROR_PARAMETER_NULL;
|
|
}
|
|
EXIT_NOT_IMPLEMENTED(param->pSeeThroughBuff != nullptr);
|
|
const auto display_size = ReprojectionDisplaySize();
|
|
auto* state = new (param->pReprojectionBuff) ReprojectionState;
|
|
if (param->reprojectionTiming != 0) {
|
|
state->timing_us.store(static_cast<uint32_t>(param->reprojectionTiming),
|
|
std::memory_order_relaxed);
|
|
}
|
|
g_reprojection_state.store(state, std::memory_order_release);
|
|
LOGF("Hmd2: reprojection initialized work=%p display=%p size=%" PRIu64 " timing=%u\n",
|
|
static_cast<void*>(state), param->pDisplayBuff, display_size.m_size,
|
|
state->timing_us.load(std::memory_order_relaxed));
|
|
return OK;
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionEnableVrMode(uint64_t output_mode) {
|
|
auto* state = GetReprojectionState();
|
|
if (state == nullptr) {
|
|
return ERROR_REPROJECTION_NOT_INITIALIZED;
|
|
}
|
|
if (output_mode != OUTPUT_MODE_89_91HZ && output_mode != OUTPUT_MODE_119_88HZ) {
|
|
return VideoOut::VIDEO_OUT_ERROR_INVALID_VALUE;
|
|
}
|
|
if (!Config::VrEnabled()) {
|
|
return VideoOut::VIDEO_OUT_ERROR_NO_DEVICE;
|
|
}
|
|
uint64_t expected = 0;
|
|
if (!state->output_mode.compare_exchange_strong(expected, output_mode)) {
|
|
return ERROR_REPROJECTION_IN_VR_MODE;
|
|
}
|
|
LOGF("Hmd2: VR output mode=0x%" PRIx64 "\n", output_mode);
|
|
return OK;
|
|
}
|
|
|
|
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionSetTiming(int32_t timing) {
|
|
auto* state = GetReprojectionState();
|
|
if (state == nullptr) {
|
|
return ERROR_REPROJECTION_NOT_INITIALIZED;
|
|
}
|
|
EXIT_NOT_IMPLEMENTED(timing < 2000 || timing > 7000);
|
|
const auto rounded = static_cast<uint32_t>((timing + 99) / 100 * 100);
|
|
state->timing_us.store(rounded, std::memory_order_release);
|
|
LOGF("Hmd2: reprojection timing=%u us\n", rounded);
|
|
return OK;
|
|
}
|
|
|
|
} // namespace Hmd2
|
|
|
|
LIB_DEFINE(InitHmd2_1) {
|
|
LIB_FUNC("c812oYs7Vsc", Hmd2::Hmd2Initialize);
|
|
LIB_FUNC("bIi4YUfSRys", Hmd2::Hmd2GetDeviceInformation);
|
|
LIB_FUNC("gF8+lvc7GuQ", Hmd2::Hmd2GetFieldOfViewWithoutHandle);
|
|
LIB_FUNC("U-CnbmeyYaA", Hmd2::Hmd2ReprojectionQueryBufferSizeAlign);
|
|
LIB_FUNC("-C2nkoEYOnU", Hmd2::Hmd2ReprojectionQueryDisplayBufferSizeAlign);
|
|
LIB_FUNC("C0rPwER-yxg", Hmd2::Hmd2ReprojectionInitialize);
|
|
LIB_FUNC("VVvFh51o20s", Hmd2::Hmd2ReprojectionEnableVrMode);
|
|
LIB_FUNC("FkQX7rjFomk", Hmd2::Hmd2ReprojectionSetTiming);
|
|
}
|
|
|
|
} // namespace Libs
|