mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-22 20:06:54 +02:00
vr: add OpenXR headset output, VR tracker and VR controller input behind --vr (#1726)
This commit is contained in:
+215
-4
@@ -1,15 +1,20 @@
|
||||
#include "common/assert.h"
|
||||
#include "common/emulatorConfig.h"
|
||||
#include "graphics/guest_gpu/tile.h"
|
||||
#include "graphics/host_gpu/renderer/image/imageInfo.h"
|
||||
#include "graphics/shader/shaderBindings.h"
|
||||
#include "kernel/pthread.h"
|
||||
#include "libs/agc.h"
|
||||
#include "libs/errno.h"
|
||||
#include "libs/hmd2.h"
|
||||
#include "libs/libs.h"
|
||||
#include "loader/symbolDatabase.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
|
||||
@@ -21,10 +26,19 @@ namespace Hmd2 {
|
||||
|
||||
constexpr int32_t ERROR_ALREADY_INITIALIZED = -1972240383; // 0x8a720001
|
||||
constexpr int32_t ERROR_NOT_INITIALIZED = -1972240382; // 0x8a720002
|
||||
constexpr int32_t ERROR_HANDLE_INVALID = -1972240381; // 0x8a720003
|
||||
constexpr int32_t ERROR_PARAMETER_NULL = -1972240376; // 0x8a720008
|
||||
constexpr int32_t ERROR_PARAMETER_INVALID = -1972240375; // 0x8a720009
|
||||
constexpr int32_t ERROR_REPROJECTION_ALREADY_INITIALIZED = -1972240372; // 0x8a72000c
|
||||
constexpr int32_t ERROR_REPROJECTION_IN_VR_MODE = -1972240356; // 0x8a72001c
|
||||
|
||||
// Any non-negative value is a valid headset handle.
|
||||
constexpr int32_t HMD_HANDLE = 0x0f000001;
|
||||
constexpr uint32_t REPROJECTION_LAYER_TYPE_VR3D = 0x10;
|
||||
constexpr uint32_t REPROJECTION_LAYER_TYPE_VR2D = 0x12;
|
||||
constexpr uint32_t REPROJECTION_MAX_SOURCE_COUNT = 5;
|
||||
constexpr int32_t GAZE_TRACKING_STATUS_DISABLE = -1;
|
||||
|
||||
struct SceHmd2InitializeParam {
|
||||
void* reserved0;
|
||||
uint8_t reserved[8];
|
||||
@@ -69,6 +83,58 @@ struct SceHmd2ReprojectionInitializeParam {
|
||||
uint32_t reserved[5];
|
||||
};
|
||||
|
||||
struct SceHmd2ReprojectionSource {
|
||||
uint32_t layerType;
|
||||
uint32_t data[73]; // VR3D: srcL texture, srcR texture, ...; VR2D: src texture, ...
|
||||
const void* option;
|
||||
};
|
||||
|
||||
struct SceHmd2ReprojectionBuffer {
|
||||
const SceHmd2ReprojectionSource* src;
|
||||
uint32_t srcCount;
|
||||
};
|
||||
|
||||
struct SceHmd2ReprojectionRenderConfig {
|
||||
uint32_t syncMode;
|
||||
uint32_t phase;
|
||||
uint32_t reserved[2];
|
||||
};
|
||||
|
||||
struct SceHmd2ReprojectionParam {
|
||||
const void* settings;
|
||||
int32_t settingCount;
|
||||
const void* option;
|
||||
};
|
||||
|
||||
struct SceHmd2GazeGetResultParam {
|
||||
uint32_t sizeOfThis;
|
||||
int32_t handle;
|
||||
int64_t flipArg;
|
||||
};
|
||||
|
||||
struct SceHmd2GazeEyeResult {
|
||||
int32_t isPupilDiameterValid;
|
||||
float pupilDiameter;
|
||||
int32_t isBlink;
|
||||
};
|
||||
|
||||
struct SceHmd2GazeResult {
|
||||
uint32_t sizeOfThis;
|
||||
int32_t status;
|
||||
uint64_t timestamp;
|
||||
int32_t isGazeValid;
|
||||
SceFVector3 gazeDirection;
|
||||
SceHmd2GazeEyeResult left;
|
||||
SceHmd2GazeEyeResult right;
|
||||
};
|
||||
|
||||
struct SceHmd2GazeResultForFoveatedRendering {
|
||||
uint32_t sizeOfThis;
|
||||
int32_t status;
|
||||
SceFVector3 gazeDirection;
|
||||
uint32_t reserved;
|
||||
};
|
||||
|
||||
static_assert(sizeof(SceHmd2InitializeParam) == 16);
|
||||
static_assert(offsetof(SceHmd2InitializeParam, reserved) == 8);
|
||||
static_assert(sizeof(SceHmd2DeviceInformation) == 28);
|
||||
@@ -84,6 +150,19 @@ 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_assert(sizeof(SceHmd2ReprojectionSource) == 304);
|
||||
static_assert(offsetof(SceHmd2ReprojectionSource, option) == 296);
|
||||
static_assert(sizeof(SceHmd2ReprojectionBuffer) == 16);
|
||||
static_assert(sizeof(SceHmd2ReprojectionRenderConfig) == 16);
|
||||
static_assert(sizeof(SceHmd2ReprojectionParam) == 24);
|
||||
static_assert(offsetof(SceHmd2ReprojectionParam, option) == 16);
|
||||
static_assert(sizeof(SceHmd2GazeGetResultParam) == 16);
|
||||
static_assert(offsetof(SceHmd2GazeGetResultParam, flipArg) == 8);
|
||||
static_assert(sizeof(SceHmd2GazeResult) == 56);
|
||||
static_assert(offsetof(SceHmd2GazeResult, gazeDirection) == 20);
|
||||
static_assert(offsetof(SceHmd2GazeResult, right) == 44);
|
||||
static_assert(sizeof(SceHmd2GazeResultForFoveatedRendering) == 24);
|
||||
static_assert(offsetof(SceHmd2GazeResultForFoveatedRendering, gazeDirection) == 8);
|
||||
|
||||
static std::atomic<bool> g_initialized = false;
|
||||
static std::mutex g_reprojection_mutex;
|
||||
@@ -127,7 +206,7 @@ static int32_t KYTY_SYSV_ABI Hmd2GetDeviceInformation(SceHmd2DeviceInformation*
|
||||
if (Config::VrEnabled()) {
|
||||
// Virtual headset defaults: READY, MOUNT, 63 mm lens separation.
|
||||
info->hmuMount = 1;
|
||||
info->lensSeparationDistance = 63;
|
||||
info->lensSeparationDistance = LENS_SEPARATION_MM;
|
||||
// Headset characteristics: 4000x2040 panel and 2 m virtual image distance.
|
||||
info->deviceInfo.panelResolution = {PANEL_WIDTH, PANEL_HEIGHT};
|
||||
info->virtualImageDistance = 2.0f;
|
||||
@@ -149,9 +228,7 @@ static int32_t KYTY_SYSV_ABI Hmd2GetFieldOfViewWithoutHandle(SceHmd2FieldOfView*
|
||||
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};
|
||||
*fov = {FOV_TAN_OUT, FOV_TAN_IN, FOV_TAN_TOP, FOV_TAN_BOTTOM};
|
||||
LOGF("Hmd2: FOV tangents out=%f in=%f top=%f bottom=%f\n", fov->tanOut, fov->tanIn,
|
||||
fov->tanTop, fov->tanBottom);
|
||||
return OK;
|
||||
@@ -212,6 +289,133 @@ static int32_t KYTY_SYSV_ABI Hmd2ReprojectionEnableVrMode(uint64_t output_mode)
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2Open(int32_t user_id, int32_t type, int32_t index,
|
||||
const void* param) {
|
||||
PRINT_NAME();
|
||||
if (!g_initialized.load()) {
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
if (user_id != Config::GetUserId() || type != 0 || index != 0 || param != nullptr) {
|
||||
return ERROR_PARAMETER_INVALID;
|
||||
}
|
||||
return HMD_HANDLE;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionBeginFrame(int64_t flip_arg) {
|
||||
PRINT_NAME();
|
||||
LOGF("\t flip_arg = %" PRId64 "\n", flip_arg);
|
||||
return OK;
|
||||
}
|
||||
|
||||
// Microseconds of process time, the clock VrTracker2 predicts poses for. The headset shows a frame
|
||||
// one refresh after it begins.
|
||||
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionGetPredictedDisplayTime(int64_t flip_arg,
|
||||
uint64_t* timestamp) {
|
||||
PRINT_NAME();
|
||||
if (timestamp == nullptr) {
|
||||
return ERROR_PARAMETER_NULL;
|
||||
}
|
||||
const auto* state = GetReprojectionState();
|
||||
const bool fast = state != nullptr && state->output_mode.load() == OUTPUT_MODE_119_88HZ;
|
||||
*timestamp = LibKernel::KernelGetProcessTime() + (fast ? 8342 : 11122);
|
||||
LOGF("\t flip_arg = %" PRId64 ", time = %" PRIu64 "\n", flip_arg, *timestamp);
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionSetRenderConfig(SceHmd2ReprojectionRenderConfig config,
|
||||
const void* option) {
|
||||
PRINT_NAME();
|
||||
LOGF("\t sync_mode = %u, phase = %u\n", config.syncMode, config.phase);
|
||||
if (GetReprojectionState() == nullptr) {
|
||||
return ERROR_REPROJECTION_NOT_INITIALIZED;
|
||||
}
|
||||
return option != nullptr ? ERROR_PARAMETER_INVALID : OK;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionSetParam(int32_t index, int64_t flip_arg,
|
||||
const SceHmd2ReprojectionParam* param,
|
||||
const void* option) {
|
||||
PRINT_NAME();
|
||||
if (GetReprojectionState() == nullptr) {
|
||||
return ERROR_REPROJECTION_NOT_INITIALIZED;
|
||||
}
|
||||
if (param == nullptr || param->settings == nullptr) {
|
||||
return ERROR_PARAMETER_NULL;
|
||||
}
|
||||
LOGF("\t index = %d, flip_arg = %" PRId64 ", layers = %d\n", index, flip_arg,
|
||||
param->settingCount);
|
||||
return option != nullptr ? ERROR_PARAMETER_INVALID : OK;
|
||||
}
|
||||
|
||||
// No eye tracker: report DISABLE, which a PS5 also returns when the user turned eye tracking off.
|
||||
template <typename Result>
|
||||
static int32_t GazeResult(const SceHmd2GazeGetResultParam* param, Result* result) {
|
||||
if (param == nullptr || result == nullptr) {
|
||||
return ERROR_PARAMETER_NULL;
|
||||
}
|
||||
if (param->sizeOfThis != sizeof(*param) || result->sizeOfThis != sizeof(*result)) {
|
||||
return ERROR_PARAMETER_INVALID;
|
||||
}
|
||||
if (param->handle != HMD_HANDLE) {
|
||||
return ERROR_HANDLE_INVALID;
|
||||
}
|
||||
*result = {};
|
||||
result->sizeOfThis = sizeof(*result);
|
||||
result->status = GAZE_TRACKING_STATUS_DISABLE;
|
||||
result->gazeDirection = {0.0f, 0.0f,
|
||||
-1.0f}; // The display center; +z points away from the lens.
|
||||
return OK;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2GazeGetResult(const SceHmd2GazeGetResultParam* param,
|
||||
SceHmd2GazeResult* result) {
|
||||
PRINT_NAME();
|
||||
const auto error = GazeResult(param, result);
|
||||
if (error == OK) {
|
||||
result->timestamp = LibKernel::KernelGetProcessTime();
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2GazeGetResultForFoveatedRendering(
|
||||
const SceHmd2GazeGetResultParam* param, SceHmd2GazeResultForFoveatedRendering* result) {
|
||||
PRINT_NAME();
|
||||
return GazeResult(param, result);
|
||||
}
|
||||
|
||||
bool GetReprojectionSurface(const void* reprojection_buffer, ReprojectionSurface& out) {
|
||||
const auto* buffer = static_cast<const SceHmd2ReprojectionBuffer*>(reprojection_buffer);
|
||||
if (buffer == nullptr || buffer->src == nullptr || buffer->srcCount == 0 ||
|
||||
buffer->srcCount > REPROJECTION_MAX_SOURCE_COUNT ||
|
||||
(buffer->src->layerType != REPROJECTION_LAYER_TYPE_VR3D &&
|
||||
buffer->src->layerType != REPROJECTION_LAYER_TYPE_VR2D)) {
|
||||
return false;
|
||||
}
|
||||
Graphics::ShaderTextureResource texture;
|
||||
std::memcpy(texture.fields, buffer->src->data, sizeof(texture.fields));
|
||||
// Eye textures are RGBA ordered render targets; flip them through the matching SDR format.
|
||||
const auto& policies = Graphics::VIDEO_OUT_FORMAT_POLICIES;
|
||||
const auto policy = std::find_if(policies.begin(), policies.end(), [&](const auto& p) {
|
||||
return p.info.guest_format == texture.Format() &&
|
||||
(p.pixel_format & 0xff000000u) == 0x22000000u &&
|
||||
!Graphics::IsHdrVideoOutPixelFormat(p.pixel_format);
|
||||
});
|
||||
if (texture.IsNull() || texture.TileMode() != Graphics::Prospero::TileMode::kRenderTarget ||
|
||||
policy == policies.end()) {
|
||||
LOGF("Hmd2: unsupported eye texture format=%u tile=%u\n",
|
||||
static_cast<uint32_t>(texture.Format()), static_cast<uint32_t>(texture.TileMode()));
|
||||
return false;
|
||||
}
|
||||
if (buffer->src->layerType == REPROJECTION_LAYER_TYPE_VR3D &&
|
||||
std::memcmp(buffer->src->data, buffer->src->data + std::size(texture.fields),
|
||||
sizeof(texture.fields)) != 0) {
|
||||
Log::WriteToConsoleAndLog(
|
||||
"Hmd2: separate eye textures are not supported; showing the left one\n");
|
||||
}
|
||||
out = {texture.Base40(), texture.Width5() + 1u, texture.Height5() + 1u, policy->pixel_format};
|
||||
return true;
|
||||
}
|
||||
|
||||
static int32_t KYTY_SYSV_ABI Hmd2ReprojectionSetTiming(int32_t timing) {
|
||||
auto* state = GetReprojectionState();
|
||||
if (state == nullptr) {
|
||||
@@ -235,6 +439,13 @@ LIB_DEFINE(InitHmd2_1) {
|
||||
LIB_FUNC("C0rPwER-yxg", Hmd2::Hmd2ReprojectionInitialize);
|
||||
LIB_FUNC("VVvFh51o20s", Hmd2::Hmd2ReprojectionEnableVrMode);
|
||||
LIB_FUNC("FkQX7rjFomk", Hmd2::Hmd2ReprojectionSetTiming);
|
||||
LIB_FUNC("f3kPeoTZnIE", Hmd2::Hmd2Open);
|
||||
LIB_FUNC("Ocf081WpBpA", Hmd2::Hmd2ReprojectionBeginFrame);
|
||||
LIB_FUNC("SVEG+1D7qHA", Hmd2::Hmd2ReprojectionGetPredictedDisplayTime);
|
||||
LIB_FUNC("hA9LshbSkzw", Hmd2::Hmd2ReprojectionSetRenderConfig);
|
||||
LIB_FUNC("xMo9ENEu2E0", Hmd2::Hmd2ReprojectionSetParam);
|
||||
LIB_FUNC("lAoFUedcfqA", Hmd2::Hmd2GazeGetResult);
|
||||
LIB_FUNC("retc+-uRMhk", Hmd2::Hmd2GazeGetResultForFoveatedRendering);
|
||||
}
|
||||
|
||||
} // namespace Libs
|
||||
|
||||
Reference in New Issue
Block a user