Skip to content

Commit

Permalink
OpenXR: Add support for retrieving play area
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Dec 10, 2023
1 parent d76c1d0 commit 69a41b3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
22 changes: 21 additions & 1 deletion modules/openxr/openxr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ String OpenXRAPI::get_default_action_map_resource_name() {
return name;
}

String OpenXRAPI::get_error_string(XrResult result) {
String OpenXRAPI::get_error_string(XrResult result) const {
if (XR_SUCCEEDED(result)) {
return String("Succeeded");
}
Expand Down Expand Up @@ -1261,6 +1261,7 @@ bool OpenXRAPI::resolve_instance_openxr_symbols() {
OPENXR_API_INIT_XR_FUNC_V(xrGetActionStateFloat);
OPENXR_API_INIT_XR_FUNC_V(xrGetActionStateVector2f);
OPENXR_API_INIT_XR_FUNC_V(xrGetCurrentInteractionProfile);
OPENXR_API_INIT_XR_FUNC_V(xrGetReferenceSpaceBoundsRect);
OPENXR_API_INIT_XR_FUNC_V(xrGetSystem);
OPENXR_API_INIT_XR_FUNC_V(xrGetSystemProperties);
OPENXR_API_INIT_XR_FUNC_V(xrLocateViews);
Expand Down Expand Up @@ -2072,6 +2073,25 @@ void OpenXRAPI::set_foveation_dynamic(bool p_foveation_dynamic) {
}
}

Size2 OpenXRAPI::get_play_space_bounds() const {
Size2 ret;

ERR_FAIL_COND_V(session == XR_NULL_HANDLE, Size2());

XrExtent2Df extents;

XrResult result = xrGetReferenceSpaceBoundsRect(session, reference_space, &extents);
if (XR_FAILED(result)) {
print_line("OpenXR: failed to get play space bounds! [", get_error_string(result), "]");
return ret;
}

ret.width = extents.width;
ret.height = extents.height;

return ret;
}

OpenXRAPI::OpenXRAPI() {
// OpenXRAPI is only constructed if OpenXR is enabled.
singleton = this;
Expand Down
6 changes: 5 additions & 1 deletion modules/openxr/openxr_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class OpenXRAPI {
EXT_PROTO_XRRESULT_FUNC3(xrGetActionStateVector2f, (XrSession), session, (const XrActionStateGetInfo *), getInfo, (XrActionStateVector2f *), state)
EXT_PROTO_XRRESULT_FUNC3(xrGetCurrentInteractionProfile, (XrSession), session, (XrPath), topLevelUserPath, (XrInteractionProfileState *), interactionProfile)
EXT_PROTO_XRRESULT_FUNC2(xrGetInstanceProperties, (XrInstance), instance, (XrInstanceProperties *), instanceProperties)
EXT_PROTO_XRRESULT_FUNC3(xrGetReferenceSpaceBoundsRect, (XrSession), session, (XrReferenceSpaceType), referenceSpaceType, (XrExtent2Df *), bounds)
EXT_PROTO_XRRESULT_FUNC3(xrGetSystem, (XrInstance), instance, (const XrSystemGetInfo *), getInfo, (XrSystemId *), systemId)
EXT_PROTO_XRRESULT_FUNC3(xrGetSystemProperties, (XrInstance), instance, (XrSystemId), systemId, (XrSystemProperties *), properties)
EXT_PROTO_XRRESULT_FUNC4(xrLocateSpace, (XrSpace), space, (XrSpace), baseSpace, (XrTime), time, (XrSpaceLocation *), location)
Expand Down Expand Up @@ -317,7 +318,7 @@ class OpenXRAPI {

XrResult try_get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr);
XrResult get_instance_proc_addr(const char *p_name, PFN_xrVoidFunction *p_addr);
String get_error_string(XrResult result);
String get_error_string(XrResult result) const;
String get_swapchain_format_name(int64_t p_swapchain_format) const;

void set_xr_interface(OpenXRInterface *p_xr_interface);
Expand Down Expand Up @@ -380,6 +381,9 @@ class OpenXRAPI {
bool get_foveation_dynamic() const;
void set_foveation_dynamic(bool p_foveation_dynamic);

// Play space.
Size2 get_play_space_bounds() const;

// action map
String get_default_action_map_resource_name();

Expand Down
36 changes: 36 additions & 0 deletions modules/openxr/openxr_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,42 @@ bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
return false;
}

PackedVector3Array OpenXRInterface::get_play_area() const {
XRServer *xr_server = XRServer::get_singleton();
ERR_FAIL_NULL_V(xr_server, PackedVector3Array());
PackedVector3Array arr;

Vector3 sides[4] = {
Vector3(-0.5f, 0.0f, -0.5f),
Vector3(0.5f, 0.0f, -0.5f),
Vector3(0.5f, 0.0f, 0.5f),
Vector3(-0.5f, 0.0f, 0.5f),
};

if (openxr_api != nullptr && openxr_api->is_initialized()) {
Size2 extents = openxr_api->get_play_space_bounds();
if (extents.width != 0.0 && extents.height != 0.0) {
Transform3D reference_frame = xr_server->get_reference_frame();

for (int i = 0; i < 4; i++) {
Vector3 coord = sides[i];

// Scale it up.
coord.x *= extents.width;
coord.z *= extents.height;

// Now apply our reference.
Vector3 out = reference_frame.xform(coord);
arr.push_back(out);
}
} else {
WARN_PRINT_ONCE("OpenXR: No extents available.");
}
}

return arr;
}

float OpenXRInterface::get_display_refresh_rate() const {
if (openxr_api == nullptr) {
return 0.0;
Expand Down
1 change: 1 addition & 0 deletions modules/openxr/openxr_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class OpenXRInterface : public XRInterface {
virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
virtual PackedVector3Array get_play_area() const override;

float get_display_refresh_rate() const;
void set_display_refresh_rate(float p_refresh_rate);
Expand Down

0 comments on commit 69a41b3

Please sign in to comment.