You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-20 14:45:44 +00:00
Implement XR_KHR_android_thread_settings
This commit is contained in:
@@ -74,6 +74,7 @@ def get_doc_classes():
|
||||
"OpenXRSpatialAnchorCapability",
|
||||
"OpenXRSpatialPlaneTrackingCapability",
|
||||
"OpenXRSpatialMarkerTrackingCapability",
|
||||
"OpenXRAndroidThreadSettingsExtension",
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<class name="OpenXRAndroidThreadSettingsExtension" inherits="OpenXRExtensionWrapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
|
||||
<brief_description>
|
||||
Wraps the [url=https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_KHR_android_thread_settings]XR_KHR_android_thread_settings[/url] extension.
|
||||
</brief_description>
|
||||
<description>
|
||||
For XR to be comfortable, it is important for applications to deliver frames quickly and consistently. In order to make sure the important application threads get their full share of time, these threads must be identified to the system, which will adjust their scheduling priority accordingly.
|
||||
</description>
|
||||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="set_application_thread_type">
|
||||
<return type="bool" />
|
||||
<param index="0" name="thread_type" type="int" enum="OpenXRAndroidThreadSettingsExtension.ThreadType" />
|
||||
<param index="1" name="thread_id" type="int" default="0" />
|
||||
<description>
|
||||
Sets the thread type of the given thread, so that the XR runtime can adjust its scheduling priority accordingly.
|
||||
[param thread_id] refers to the OS thread id (ie from [code]gettid()[/code]). When [param thread_id] is [code]0[/code], it will set the thread type of the current thread.
|
||||
[b]NOTE:[/b] The id returned by [method Thread.get_id] is incompatible with [param thread_id].
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
<constants>
|
||||
<constant name="THREAD_TYPE_APPLICATION_MAIN" value="0" enum="ThreadType">
|
||||
Hints to the XR runtime that the thread is doing time critical CPU tasks.
|
||||
</constant>
|
||||
<constant name="THREAD_TYPE_APPLICATION_WORKER" value="1" enum="ThreadType">
|
||||
Hints to the XR runtime that the thread is doing background CPU tasks.
|
||||
</constant>
|
||||
<constant name="THREAD_TYPE_RENDERER_MAIN" value="2" enum="ThreadType">
|
||||
Hints to the XR runtime that the thread is doing time critical graphics device tasks.
|
||||
</constant>
|
||||
<constant name="THREAD_TYPE_RENDERER_WORKER" value="3" enum="ThreadType">
|
||||
Hints to the XR runtime that the thread is doing background graphics device tasks.
|
||||
</constant>
|
||||
</constants>
|
||||
</class>
|
||||
@@ -0,0 +1,149 @@
|
||||
/**************************************************************************/
|
||||
/* openxr_android_thread_settings_extension.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "openxr_android_thread_settings_extension.h"
|
||||
|
||||
#include "core/object/callable_method_pointer.h"
|
||||
#include "core/string/print_string.h"
|
||||
#include "servers/rendering/rendering_server.h"
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
#include "../openxr_api.h"
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::singleton = nullptr;
|
||||
|
||||
OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::get_singleton() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
OpenXRAndroidThreadSettingsExtension::OpenXRAndroidThreadSettingsExtension() {
|
||||
singleton = this;
|
||||
}
|
||||
|
||||
OpenXRAndroidThreadSettingsExtension::~OpenXRAndroidThreadSettingsExtension() {
|
||||
singleton = nullptr;
|
||||
}
|
||||
|
||||
void OpenXRAndroidThreadSettingsExtension::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("set_application_thread_type", "thread_type", "thread_id"), &OpenXRAndroidThreadSettingsExtension::set_application_thread_type, DEFVAL(0));
|
||||
|
||||
BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_MAIN);
|
||||
BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_WORKER);
|
||||
BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_MAIN);
|
||||
BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_WORKER);
|
||||
}
|
||||
|
||||
HashMap<String, bool *> OpenXRAndroidThreadSettingsExtension::get_requested_extensions() {
|
||||
HashMap<String, bool *> request_extensions;
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
request_extensions[XR_KHR_ANDROID_THREAD_SETTINGS_EXTENSION_NAME] = &available;
|
||||
#endif
|
||||
|
||||
return request_extensions;
|
||||
}
|
||||
|
||||
void OpenXRAndroidThreadSettingsExtension::on_instance_created(XrInstance p_instance) {
|
||||
if (!available) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_initialize_openxr_android_thread_settings_extension()) {
|
||||
print_error("OpenXR: Failed to initialize android thread settings extension");
|
||||
available = false;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenXRAndroidThreadSettingsExtension::on_session_created(XrSession p_session) {
|
||||
if (!available) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Attempt to mark this thread as the "main thread".
|
||||
set_application_thread_type(THREAD_TYPE_APPLICATION_MAIN);
|
||||
|
||||
// Attempt to mark the render thread too.
|
||||
RenderingServer *rendering_server = RenderingServer::get_singleton();
|
||||
ERR_FAIL_NULL(rendering_server);
|
||||
rendering_server->call_on_render_thread(callable_mp(this, &OpenXRAndroidThreadSettingsExtension::_set_render_thread_type));
|
||||
}
|
||||
|
||||
bool OpenXRAndroidThreadSettingsExtension::set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id) {
|
||||
if (!available) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
XrAndroidThreadTypeKHR thread_type{};
|
||||
switch (p_thread_type) {
|
||||
case THREAD_TYPE_APPLICATION_MAIN:
|
||||
thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_MAIN_KHR;
|
||||
break;
|
||||
case THREAD_TYPE_APPLICATION_WORKER:
|
||||
thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_WORKER_KHR;
|
||||
break;
|
||||
case THREAD_TYPE_RENDERER_MAIN:
|
||||
thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_MAIN_KHR;
|
||||
break;
|
||||
case THREAD_TYPE_RENDERER_WORKER:
|
||||
thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_WORKER_KHR;
|
||||
break;
|
||||
default:
|
||||
print_error(vformat("OpenXR: Failed to set android application thread; invalid thread type %d", p_thread_type));
|
||||
return false;
|
||||
}
|
||||
|
||||
XrResult result = xrSetAndroidApplicationThreadKHR(OpenXRAPI::get_singleton()->get_session(), thread_type, p_thread_id == 0 ? gettid() : p_thread_id);
|
||||
if (result != XR_SUCCESS) {
|
||||
print_error(vformat("OpenXR: Failed to set android application thread; %s", OpenXRAPI::get_singleton()->get_error_string(result)));
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenXRAndroidThreadSettingsExtension::_initialize_openxr_android_thread_settings_extension() {
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
EXT_INIT_XR_FUNC_V(xrSetAndroidApplicationThreadKHR);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenXRAndroidThreadSettingsExtension::_set_render_thread_type() {
|
||||
// Skip when the render thread == the main thread
|
||||
if (Thread::is_main_thread()) {
|
||||
return;
|
||||
}
|
||||
|
||||
set_application_thread_type(THREAD_TYPE_RENDERER_MAIN);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**************************************************************************/
|
||||
/* openxr_android_thread_settings_extension.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../openxr_interface.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
|
||||
#include "openxr_extension_wrapper.h"
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
#include "../util.h"
|
||||
#include <jni.h>
|
||||
#include <openxr/openxr_platform.h>
|
||||
#endif
|
||||
|
||||
class OpenXRAndroidThreadSettingsExtension : public OpenXRExtensionWrapper {
|
||||
GDCLASS(OpenXRAndroidThreadSettingsExtension, OpenXRExtensionWrapper);
|
||||
|
||||
public:
|
||||
static OpenXRAndroidThreadSettingsExtension *get_singleton();
|
||||
|
||||
OpenXRAndroidThreadSettingsExtension();
|
||||
virtual ~OpenXRAndroidThreadSettingsExtension() override;
|
||||
|
||||
virtual HashMap<String, bool *> get_requested_extensions() override;
|
||||
virtual void on_instance_created(XrInstance p_instance) override;
|
||||
virtual void on_session_created(XrSession p_session) override;
|
||||
|
||||
enum ThreadType {
|
||||
THREAD_TYPE_APPLICATION_MAIN,
|
||||
THREAD_TYPE_APPLICATION_WORKER,
|
||||
THREAD_TYPE_RENDERER_MAIN,
|
||||
THREAD_TYPE_RENDERER_WORKER,
|
||||
};
|
||||
bool set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id = 0);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
static OpenXRAndroidThreadSettingsExtension *singleton;
|
||||
|
||||
bool _initialize_openxr_android_thread_settings_extension();
|
||||
void _set_render_thread_type();
|
||||
|
||||
bool available = false;
|
||||
|
||||
#ifdef XR_USE_PLATFORM_ANDROID
|
||||
EXT_PROTO_XRRESULT_FUNC3(xrSetAndroidApplicationThreadKHR, (XrSession), session, (XrAndroidThreadTypeKHR), threadType, (uint32_t), threadId);
|
||||
#endif
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(OpenXRAndroidThreadSettingsExtension::ThreadType)
|
||||
@@ -52,6 +52,7 @@
|
||||
#include "scene/openxr_render_model_manager.h"
|
||||
#include "scene/openxr_visibility_mask.h"
|
||||
|
||||
#include "extensions/openxr_android_thread_settings_extension.h"
|
||||
#include "extensions/openxr_composition_layer_depth_extension.h"
|
||||
#include "extensions/openxr_composition_layer_extension.h"
|
||||
#include "extensions/openxr_debug_utils_extension.h"
|
||||
@@ -133,6 +134,7 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {
|
||||
GDREGISTER_CLASS(OpenXRFutureExtension);
|
||||
GDREGISTER_CLASS(OpenXRAPIExtension);
|
||||
GDREGISTER_CLASS(OpenXRRenderModelExtension);
|
||||
GDREGISTER_CLASS(OpenXRAndroidThreadSettingsExtension);
|
||||
|
||||
// Note, we're not registering all wrapper classes here, there is no point in exposing them
|
||||
// if there isn't specific logic to expose.
|
||||
@@ -197,6 +199,11 @@ void initialize_openxr_module(ModuleInitializationLevel p_level) {
|
||||
OpenXRAPI::register_extension_wrapper(frame_synthesis_extension);
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRFrameSynthesisExtension", frame_synthesis_extension));
|
||||
|
||||
// Register android thread settings extension as a singleton.
|
||||
OpenXRAndroidThreadSettingsExtension *android_thread_settings = memnew(OpenXRAndroidThreadSettingsExtension);
|
||||
OpenXRAPI::register_extension_wrapper(android_thread_settings);
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("OpenXRAndroidThreadSettingsExtension", android_thread_settings));
|
||||
|
||||
// register gated extensions
|
||||
if (int(GLOBAL_GET("xr/openxr/extensions/debug_utils")) > 0) {
|
||||
OpenXRAPI::register_extension_wrapper(memnew(OpenXRDebugUtilsExtension));
|
||||
|
||||
Reference in New Issue
Block a user