Path: blob/master/modules/openxr/openxr_api_extension.cpp
20951 views
/**************************************************************************/1/* openxr_api_extension.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_api_extension.h"3132#include "extensions/openxr_extension_wrapper.h"33#include "openxr_api_extension.compat.inc"3435void OpenXRAPIExtension::_bind_methods() {36ClassDB::bind_method(D_METHOD("get_openxr_version"), &OpenXRAPIExtension::get_openxr_version);37ClassDB::bind_method(D_METHOD("get_instance"), &OpenXRAPIExtension::get_instance);38ClassDB::bind_method(D_METHOD("get_system_id"), &OpenXRAPIExtension::get_system_id);39ClassDB::bind_method(D_METHOD("get_session"), &OpenXRAPIExtension::get_session);4041ClassDB::bind_method(D_METHOD("transform_from_pose", "pose"), &OpenXRAPIExtension::transform_from_pose);42ClassDB::bind_method(D_METHOD("xr_result", "result", "format", "args"), &OpenXRAPIExtension::xr_result);43ClassDB::bind_static_method("OpenXRAPIExtension", D_METHOD("openxr_is_enabled", "check_run_in_editor"), &OpenXRAPIExtension::openxr_is_enabled);44ClassDB::bind_method(D_METHOD("get_instance_proc_addr", "name"), &OpenXRAPIExtension::get_instance_proc_addr);45ClassDB::bind_method(D_METHOD("get_error_string", "result"), &OpenXRAPIExtension::get_error_string);46ClassDB::bind_method(D_METHOD("get_swapchain_format_name", "swapchain_format"), &OpenXRAPIExtension::get_swapchain_format_name);47ClassDB::bind_method(D_METHOD("set_object_name", "object_type", "object_handle", "object_name"), &OpenXRAPIExtension::set_object_name);48ClassDB::bind_method(D_METHOD("begin_debug_label_region", "label_name"), &OpenXRAPIExtension::begin_debug_label_region);49ClassDB::bind_method(D_METHOD("end_debug_label_region"), &OpenXRAPIExtension::end_debug_label_region);50ClassDB::bind_method(D_METHOD("insert_debug_label", "label_name"), &OpenXRAPIExtension::insert_debug_label);5152ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);53ClassDB::bind_method(D_METHOD("is_running"), &OpenXRAPIExtension::is_running);5455ClassDB::bind_method(D_METHOD("set_custom_play_space", "space"), &OpenXRAPIExtension::set_custom_play_space);56ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space);57ClassDB::bind_method(D_METHOD("get_predicted_display_time"), &OpenXRAPIExtension::get_predicted_display_time);58ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);59ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);6061ClassDB::bind_method(D_METHOD("find_action", "name", "action_set"), &OpenXRAPIExtension::find_action);62ClassDB::bind_method(D_METHOD("action_get_handle", "action"), &OpenXRAPIExtension::action_get_handle);6364ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);6566ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);67ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);6869ClassDB::bind_method(D_METHOD("register_projection_views_extension", "extension"), &OpenXRAPIExtension::register_projection_views_extension);70ClassDB::bind_method(D_METHOD("unregister_projection_views_extension", "extension"), &OpenXRAPIExtension::unregister_projection_views_extension);7172ClassDB::bind_method(D_METHOD("register_frame_info_extension", "extension"), &OpenXRAPIExtension::register_frame_info_extension);73ClassDB::bind_method(D_METHOD("unregister_frame_info_extension", "extension"), &OpenXRAPIExtension::unregister_frame_info_extension);7475ClassDB::bind_method(D_METHOD("get_render_state_z_near"), &OpenXRAPIExtension::get_render_state_z_near);76ClassDB::bind_method(D_METHOD("get_render_state_z_far"), &OpenXRAPIExtension::get_render_state_z_far);7778ClassDB::bind_method(D_METHOD("set_velocity_texture", "render_target"), &OpenXRAPIExtension::set_velocity_texture);79ClassDB::bind_method(D_METHOD("set_velocity_depth_texture", "render_target"), &OpenXRAPIExtension::set_velocity_depth_texture);80ClassDB::bind_method(D_METHOD("set_velocity_target_size", "target_size"), &OpenXRAPIExtension::set_velocity_target_size);8182ClassDB::bind_method(D_METHOD("get_supported_swapchain_formats"), &OpenXRAPIExtension::get_supported_swapchain_formats);8384ClassDB::bind_method(D_METHOD("openxr_swapchain_create", "create_flags", "usage_flags", "swapchain_format", "width", "height", "sample_count", "array_size"), &OpenXRAPIExtension::openxr_swapchain_create);85ClassDB::bind_method(D_METHOD("openxr_swapchain_free", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_free);86ClassDB::bind_method(D_METHOD("openxr_swapchain_get_swapchain", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_swapchain);87ClassDB::bind_method(D_METHOD("openxr_swapchain_acquire", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_acquire);88ClassDB::bind_method(D_METHOD("openxr_swapchain_get_image", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_image);89ClassDB::bind_method(D_METHOD("openxr_swapchain_release", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_release);9091ClassDB::bind_method(D_METHOD("get_projection_layer"), &OpenXRAPIExtension::get_projection_layer);9293ClassDB::bind_method(D_METHOD("set_render_region", "render_region"), &OpenXRAPIExtension::set_render_region);9495ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);96ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);9798ClassDB::bind_method(D_METHOD("update_main_swapchain_size"), &OpenXRAPIExtension::update_main_swapchain_size);99100BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);101BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);102BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);103}104105uint64_t OpenXRAPIExtension::get_openxr_version() {106ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);107return (uint64_t)OpenXRAPI::get_singleton()->get_openxr_version();108}109110uint64_t OpenXRAPIExtension::get_instance() {111ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);112return (uint64_t)OpenXRAPI::get_singleton()->get_instance();113}114115uint64_t OpenXRAPIExtension::get_system_id() {116ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);117return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();118}119120uint64_t OpenXRAPIExtension::get_session() {121ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);122return (uint64_t)OpenXRAPI::get_singleton()->get_session();123}124125Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {126ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());127return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);128}129130bool OpenXRAPIExtension::xr_result(uint64_t p_result, const String &p_format, const Array &p_args) {131ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);132return OpenXRAPI::get_singleton()->xr_result((XrResult)p_result, p_format.utf8().get_data(), p_args);133}134135bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {136ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);137return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);138}139140uint64_t OpenXRAPIExtension::get_instance_proc_addr(const String &p_name) {141ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);142CharString str = p_name.utf8();143PFN_xrVoidFunction addr = nullptr;144XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);145if (result != XR_SUCCESS) {146return 0;147}148return reinterpret_cast<uint64_t>(addr);149}150151String OpenXRAPIExtension::get_error_string(uint64_t result) {152ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());153return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);154}155156String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {157ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());158return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);159}160161void OpenXRAPIExtension::set_object_name(int64_t p_object_type, uint64_t p_object_handle, const String &p_object_name) {162ERR_FAIL_NULL(OpenXRAPI::get_singleton());163164OpenXRAPI::get_singleton()->set_object_name(XrObjectType(p_object_type), p_object_handle, p_object_name);165}166167void OpenXRAPIExtension::begin_debug_label_region(const String &p_label_name) {168ERR_FAIL_NULL(OpenXRAPI::get_singleton());169170OpenXRAPI::get_singleton()->begin_debug_label_region(p_label_name);171}172173void OpenXRAPIExtension::end_debug_label_region() {174ERR_FAIL_NULL(OpenXRAPI::get_singleton());175176OpenXRAPI::get_singleton()->end_debug_label_region();177}178179void OpenXRAPIExtension::insert_debug_label(const String &p_label_name) {180ERR_FAIL_NULL(OpenXRAPI::get_singleton());181182OpenXRAPI::get_singleton()->insert_debug_label(p_label_name);183}184185bool OpenXRAPIExtension::is_initialized() {186ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);187return OpenXRAPI::get_singleton()->is_initialized();188}189190bool OpenXRAPIExtension::is_running() {191ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);192return OpenXRAPI::get_singleton()->is_running();193}194195void OpenXRAPIExtension::set_custom_play_space(GDExtensionConstPtr<const void> p_custom_space) {196ERR_FAIL_NULL(OpenXRAPI::get_singleton());197OpenXRAPI::get_singleton()->set_custom_play_space(*(XrSpace *)p_custom_space.data);198}199200uint64_t OpenXRAPIExtension::get_play_space() {201ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);202return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();203}204205int64_t OpenXRAPIExtension::get_predicted_display_time() {206ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);207return (XrTime)OpenXRAPI::get_singleton()->get_predicted_display_time();208}209210int64_t OpenXRAPIExtension::get_next_frame_time() {211ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);212213// In the past we needed to look a frame ahead, may be calling this unintentionally so lets warn the dev.214WARN_PRINT_ONCE("OpenXR: Next frame timing called, verify this is intended.");215216return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();217}218219bool OpenXRAPIExtension::can_render() {220ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);221return OpenXRAPI::get_singleton()->can_render();222}223224RID OpenXRAPIExtension::find_action(const String &p_name, const RID &p_action_set) {225ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());226return OpenXRAPI::get_singleton()->find_action(p_name, p_action_set);227}228229uint64_t OpenXRAPIExtension::action_get_handle(RID p_action) {230ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);231XrAction action_handle = OpenXRAPI::get_singleton()->action_get_handle(p_action);232return (uint64_t)action_handle;233}234235uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {236ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);237return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);238}239240void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {241ERR_FAIL_NULL(OpenXRAPI::get_singleton());242OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);243}244245void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {246ERR_FAIL_NULL(OpenXRAPI::get_singleton());247OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);248}249250void OpenXRAPIExtension::register_projection_views_extension(OpenXRExtensionWrapper *p_extension) {251ERR_FAIL_NULL(OpenXRAPI::get_singleton());252OpenXRAPI::get_singleton()->register_projection_views_extension(p_extension);253}254255void OpenXRAPIExtension::unregister_projection_views_extension(OpenXRExtensionWrapper *p_extension) {256ERR_FAIL_NULL(OpenXRAPI::get_singleton());257OpenXRAPI::get_singleton()->unregister_projection_views_extension(p_extension);258}259260void OpenXRAPIExtension::register_frame_info_extension(OpenXRExtensionWrapper *p_extension) {261ERR_FAIL_NULL(OpenXRAPI::get_singleton());262OpenXRAPI::get_singleton()->register_frame_info_extension(p_extension);263}264265void OpenXRAPIExtension::unregister_frame_info_extension(OpenXRExtensionWrapper *p_extension) {266ERR_FAIL_NULL(OpenXRAPI::get_singleton());267OpenXRAPI::get_singleton()->unregister_frame_info_extension(p_extension);268}269270double OpenXRAPIExtension::get_render_state_z_near() {271ERR_NOT_ON_RENDER_THREAD_V(0.0);272ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);273return OpenXRAPI::get_singleton()->get_render_state_z_near();274}275276double OpenXRAPIExtension::get_render_state_z_far() {277ERR_NOT_ON_RENDER_THREAD_V(0.0);278ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);279return OpenXRAPI::get_singleton()->get_render_state_z_far();280}281282void OpenXRAPIExtension::set_velocity_texture(RID p_render_target) {283ERR_FAIL_NULL(OpenXRAPI::get_singleton());284OpenXRAPI::get_singleton()->set_velocity_texture(p_render_target);285}286287void OpenXRAPIExtension::set_velocity_depth_texture(RID p_render_target) {288ERR_FAIL_NULL(OpenXRAPI::get_singleton());289OpenXRAPI::get_singleton()->set_velocity_depth_texture(p_render_target);290}291292void OpenXRAPIExtension::set_velocity_target_size(const Size2i &p_target_size) {293ERR_FAIL_NULL(OpenXRAPI::get_singleton());294OpenXRAPI::get_singleton()->set_velocity_target_size(p_target_size);295}296297PackedInt64Array OpenXRAPIExtension::get_supported_swapchain_formats() {298ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), PackedInt64Array());299return OpenXRAPI::get_singleton()->get_supported_swapchain_formats();300}301302uint64_t OpenXRAPIExtension::openxr_swapchain_create(XrSwapchainCreateFlags p_create_flags, XrSwapchainUsageFlags p_usage_flags, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size) {303ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);304305OpenXRAPI::OpenXRSwapChainInfo *new_swapchain_info = memnew(OpenXRAPI::OpenXRSwapChainInfo);306new_swapchain_info->create(p_create_flags, p_usage_flags, p_swapchain_format, p_width, p_height, p_sample_count, p_array_size);307return reinterpret_cast<uint64_t>(new_swapchain_info);308}309310void OpenXRAPIExtension::openxr_swapchain_free(uint64_t p_swapchain_info) {311ERR_FAIL_NULL(OpenXRAPI::get_singleton());312313OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);314swapchain_info->free();315memfree(swapchain_info);316}317318uint64_t OpenXRAPIExtension::openxr_swapchain_get_swapchain(uint64_t p_swapchain_info) {319ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);320321OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);322XrSwapchain swapchain = swapchain_info->get_swapchain();323return (uint64_t)swapchain;324}325326void OpenXRAPIExtension::openxr_swapchain_acquire(uint64_t p_swapchain_info) {327ERR_FAIL_NULL(OpenXRAPI::get_singleton());328329OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);330bool should_render = true; // Can ignore should_render.331swapchain_info->acquire(should_render);332}333334RID OpenXRAPIExtension::openxr_swapchain_get_image(uint64_t p_swapchain_info) {335ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());336337OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);338return swapchain_info->get_image();339}340341void OpenXRAPIExtension::openxr_swapchain_release(uint64_t p_swapchain_info) {342ERR_FAIL_NULL(OpenXRAPI::get_singleton());343344OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);345swapchain_info->release();346}347348uint64_t OpenXRAPIExtension::get_projection_layer() {349ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);350return (uint64_t)OpenXRAPI::get_singleton()->get_projection_layer();351}352353void OpenXRAPIExtension::set_render_region(const Rect2i &p_render_region) {354ERR_FAIL_NULL(OpenXRAPI::get_singleton());355OpenXRAPI::get_singleton()->set_render_region(p_render_region);356}357358void OpenXRAPIExtension::update_main_swapchain_size() {359ERR_FAIL_NULL(OpenXRAPI::get_singleton());360OpenXRAPI::get_singleton()->update_main_swapchain_size();361}362363void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {364ERR_FAIL_NULL(OpenXRAPI::get_singleton());365OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);366}367368OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {369ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);370return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();371}372373OpenXRAPIExtension::OpenXRAPIExtension() {374}375376377