Path: blob/master/modules/openxr/openxr_api_extension.cpp
10277 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_instance"), &OpenXRAPIExtension::get_instance);37ClassDB::bind_method(D_METHOD("get_system_id"), &OpenXRAPIExtension::get_system_id);38ClassDB::bind_method(D_METHOD("get_session"), &OpenXRAPIExtension::get_session);3940ClassDB::bind_method(D_METHOD("transform_from_pose", "pose"), &OpenXRAPIExtension::transform_from_pose);41ClassDB::bind_method(D_METHOD("xr_result", "result", "format", "args"), &OpenXRAPIExtension::xr_result);42ClassDB::bind_static_method("OpenXRAPIExtension", D_METHOD("openxr_is_enabled", "check_run_in_editor"), &OpenXRAPIExtension::openxr_is_enabled);43ClassDB::bind_method(D_METHOD("get_instance_proc_addr", "name"), &OpenXRAPIExtension::get_instance_proc_addr);44ClassDB::bind_method(D_METHOD("get_error_string", "result"), &OpenXRAPIExtension::get_error_string);45ClassDB::bind_method(D_METHOD("get_swapchain_format_name", "swapchain_format"), &OpenXRAPIExtension::get_swapchain_format_name);46ClassDB::bind_method(D_METHOD("set_object_name", "object_type", "object_handle", "object_name"), &OpenXRAPIExtension::set_object_name);47ClassDB::bind_method(D_METHOD("begin_debug_label_region", "label_name"), &OpenXRAPIExtension::begin_debug_label_region);48ClassDB::bind_method(D_METHOD("end_debug_label_region"), &OpenXRAPIExtension::end_debug_label_region);49ClassDB::bind_method(D_METHOD("insert_debug_label", "label_name"), &OpenXRAPIExtension::insert_debug_label);5051ClassDB::bind_method(D_METHOD("is_initialized"), &OpenXRAPIExtension::is_initialized);52ClassDB::bind_method(D_METHOD("is_running"), &OpenXRAPIExtension::is_running);5354ClassDB::bind_method(D_METHOD("set_custom_play_space", "space"), &OpenXRAPIExtension::set_custom_play_space);55ClassDB::bind_method(D_METHOD("get_play_space"), &OpenXRAPIExtension::get_play_space);56ClassDB::bind_method(D_METHOD("get_predicted_display_time"), &OpenXRAPIExtension::get_predicted_display_time);57ClassDB::bind_method(D_METHOD("get_next_frame_time"), &OpenXRAPIExtension::get_next_frame_time);58ClassDB::bind_method(D_METHOD("can_render"), &OpenXRAPIExtension::can_render);5960ClassDB::bind_method(D_METHOD("find_action", "name", "action_set"), &OpenXRAPIExtension::find_action);61ClassDB::bind_method(D_METHOD("action_get_handle", "action"), &OpenXRAPIExtension::action_get_handle);6263ClassDB::bind_method(D_METHOD("get_hand_tracker", "hand_index"), &OpenXRAPIExtension::get_hand_tracker);6465ClassDB::bind_method(D_METHOD("register_composition_layer_provider", "extension"), &OpenXRAPIExtension::register_composition_layer_provider);66ClassDB::bind_method(D_METHOD("unregister_composition_layer_provider", "extension"), &OpenXRAPIExtension::unregister_composition_layer_provider);6768ClassDB::bind_method(D_METHOD("register_projection_views_extension", "extension"), &OpenXRAPIExtension::register_projection_views_extension);69ClassDB::bind_method(D_METHOD("unregister_projection_views_extension", "extension"), &OpenXRAPIExtension::unregister_projection_views_extension);7071ClassDB::bind_method(D_METHOD("register_frame_info_extension", "extension"), &OpenXRAPIExtension::register_frame_info_extension);72ClassDB::bind_method(D_METHOD("unregister_frame_info_extension", "extension"), &OpenXRAPIExtension::unregister_frame_info_extension);7374ClassDB::bind_method(D_METHOD("get_render_state_z_near"), &OpenXRAPIExtension::get_render_state_z_near);75ClassDB::bind_method(D_METHOD("get_render_state_z_far"), &OpenXRAPIExtension::get_render_state_z_far);7677ClassDB::bind_method(D_METHOD("set_velocity_texture", "render_target"), &OpenXRAPIExtension::set_velocity_texture);78ClassDB::bind_method(D_METHOD("set_velocity_depth_texture", "render_target"), &OpenXRAPIExtension::set_velocity_depth_texture);79ClassDB::bind_method(D_METHOD("set_velocity_target_size", "target_size"), &OpenXRAPIExtension::set_velocity_target_size);8081ClassDB::bind_method(D_METHOD("get_supported_swapchain_formats"), &OpenXRAPIExtension::get_supported_swapchain_formats);8283ClassDB::bind_method(D_METHOD("openxr_swapchain_create", "create_flags", "usage_flags", "swapchain_format", "width", "height", "sample_count", "array_size"), &OpenXRAPIExtension::openxr_swapchain_create);84ClassDB::bind_method(D_METHOD("openxr_swapchain_free", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_free);85ClassDB::bind_method(D_METHOD("openxr_swapchain_get_swapchain", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_swapchain);86ClassDB::bind_method(D_METHOD("openxr_swapchain_acquire", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_acquire);87ClassDB::bind_method(D_METHOD("openxr_swapchain_get_image", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_get_image);88ClassDB::bind_method(D_METHOD("openxr_swapchain_release", "swapchain"), &OpenXRAPIExtension::openxr_swapchain_release);8990ClassDB::bind_method(D_METHOD("get_projection_layer"), &OpenXRAPIExtension::get_projection_layer);9192ClassDB::bind_method(D_METHOD("set_render_region", "render_region"), &OpenXRAPIExtension::set_render_region);9394ClassDB::bind_method(D_METHOD("set_emulate_environment_blend_mode_alpha_blend", "enabled"), &OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend);95ClassDB::bind_method(D_METHOD("is_environment_blend_mode_alpha_supported"), &OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported);9697BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);98BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_REAL);99BIND_ENUM_CONSTANT(OPENXR_ALPHA_BLEND_MODE_SUPPORT_EMULATING);100}101102uint64_t OpenXRAPIExtension::get_instance() {103ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);104return (uint64_t)OpenXRAPI::get_singleton()->get_instance();105}106107uint64_t OpenXRAPIExtension::get_system_id() {108ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);109return (uint64_t)OpenXRAPI::get_singleton()->get_system_id();110}111112uint64_t OpenXRAPIExtension::get_session() {113ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);114return (uint64_t)OpenXRAPI::get_singleton()->get_session();115}116117Transform3D OpenXRAPIExtension::transform_from_pose(GDExtensionConstPtr<const void> p_pose) {118ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), Transform3D());119return OpenXRAPI::get_singleton()->transform_from_pose(*(XrPosef *)p_pose.data);120}121122bool OpenXRAPIExtension::xr_result(uint64_t result, String format, Array args) {123ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);124return OpenXRAPI::get_singleton()->xr_result((XrResult)result, format.utf8().get_data(), args);125}126127bool OpenXRAPIExtension::openxr_is_enabled(bool p_check_run_in_editor) {128ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);129return OpenXRAPI::openxr_is_enabled(p_check_run_in_editor);130}131132uint64_t OpenXRAPIExtension::get_instance_proc_addr(String p_name) {133ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);134CharString str = p_name.utf8();135PFN_xrVoidFunction addr = nullptr;136XrResult result = OpenXRAPI::get_singleton()->get_instance_proc_addr(str.get_data(), &addr);137if (result != XR_SUCCESS) {138return 0;139}140return reinterpret_cast<uint64_t>(addr);141}142143String OpenXRAPIExtension::get_error_string(uint64_t result) {144ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());145return OpenXRAPI::get_singleton()->get_error_string((XrResult)result);146}147148String OpenXRAPIExtension::get_swapchain_format_name(int64_t p_swapchain_format) {149ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), String());150return OpenXRAPI::get_singleton()->get_swapchain_format_name(p_swapchain_format);151}152153void OpenXRAPIExtension::set_object_name(int64_t p_object_type, uint64_t p_object_handle, const String &p_object_name) {154ERR_FAIL_NULL(OpenXRAPI::get_singleton());155156OpenXRAPI::get_singleton()->set_object_name(XrObjectType(p_object_type), p_object_handle, p_object_name);157}158159void OpenXRAPIExtension::begin_debug_label_region(const String &p_label_name) {160ERR_FAIL_NULL(OpenXRAPI::get_singleton());161162OpenXRAPI::get_singleton()->begin_debug_label_region(p_label_name);163}164165void OpenXRAPIExtension::end_debug_label_region() {166ERR_FAIL_NULL(OpenXRAPI::get_singleton());167168OpenXRAPI::get_singleton()->end_debug_label_region();169}170171void OpenXRAPIExtension::insert_debug_label(const String &p_label_name) {172ERR_FAIL_NULL(OpenXRAPI::get_singleton());173174OpenXRAPI::get_singleton()->insert_debug_label(p_label_name);175}176177bool OpenXRAPIExtension::is_initialized() {178ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);179return OpenXRAPI::get_singleton()->is_initialized();180}181182bool OpenXRAPIExtension::is_running() {183ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);184return OpenXRAPI::get_singleton()->is_running();185}186187void OpenXRAPIExtension::set_custom_play_space(GDExtensionConstPtr<const void> p_custom_space) {188ERR_FAIL_NULL(OpenXRAPI::get_singleton());189OpenXRAPI::get_singleton()->set_custom_play_space(*(XrSpace *)p_custom_space.data);190}191192uint64_t OpenXRAPIExtension::get_play_space() {193ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);194return (uint64_t)OpenXRAPI::get_singleton()->get_play_space();195}196197int64_t OpenXRAPIExtension::get_predicted_display_time() {198ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);199return (XrTime)OpenXRAPI::get_singleton()->get_predicted_display_time();200}201202int64_t OpenXRAPIExtension::get_next_frame_time() {203ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);204205// In the past we needed to look a frame ahead, may be calling this unintentionally so lets warn the dev.206WARN_PRINT_ONCE("OpenXR: Next frame timing called, verify this is intended.");207208return (XrTime)OpenXRAPI::get_singleton()->get_next_frame_time();209}210211bool OpenXRAPIExtension::can_render() {212ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), false);213return OpenXRAPI::get_singleton()->can_render();214}215216RID OpenXRAPIExtension::find_action(const String &p_name, const RID &p_action_set) {217ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());218return OpenXRAPI::get_singleton()->find_action(p_name, p_action_set);219}220221uint64_t OpenXRAPIExtension::action_get_handle(RID p_action) {222ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);223XrAction action_handle = OpenXRAPI::get_singleton()->action_get_handle(p_action);224return (uint64_t)action_handle;225}226227uint64_t OpenXRAPIExtension::get_hand_tracker(int p_hand_index) {228ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);229return (uint64_t)OpenXRAPI::get_singleton()->get_hand_tracker(p_hand_index);230}231232void OpenXRAPIExtension::register_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {233ERR_FAIL_NULL(OpenXRAPI::get_singleton());234OpenXRAPI::get_singleton()->register_composition_layer_provider(p_extension);235}236237void OpenXRAPIExtension::unregister_composition_layer_provider(OpenXRExtensionWrapper *p_extension) {238ERR_FAIL_NULL(OpenXRAPI::get_singleton());239OpenXRAPI::get_singleton()->unregister_composition_layer_provider(p_extension);240}241242void OpenXRAPIExtension::register_projection_views_extension(OpenXRExtensionWrapper *p_extension) {243ERR_FAIL_NULL(OpenXRAPI::get_singleton());244OpenXRAPI::get_singleton()->register_projection_views_extension(p_extension);245}246247void OpenXRAPIExtension::unregister_projection_views_extension(OpenXRExtensionWrapper *p_extension) {248ERR_FAIL_NULL(OpenXRAPI::get_singleton());249OpenXRAPI::get_singleton()->unregister_projection_views_extension(p_extension);250}251252void OpenXRAPIExtension::register_frame_info_extension(OpenXRExtensionWrapper *p_extension) {253ERR_FAIL_NULL(OpenXRAPI::get_singleton());254OpenXRAPI::get_singleton()->register_frame_info_extension(p_extension);255}256257void OpenXRAPIExtension::unregister_frame_info_extension(OpenXRExtensionWrapper *p_extension) {258ERR_FAIL_NULL(OpenXRAPI::get_singleton());259OpenXRAPI::get_singleton()->unregister_frame_info_extension(p_extension);260}261262double OpenXRAPIExtension::get_render_state_z_near() {263ERR_NOT_ON_RENDER_THREAD_V(0.0);264ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);265return OpenXRAPI::get_singleton()->get_render_state_z_near();266}267268double OpenXRAPIExtension::get_render_state_z_far() {269ERR_NOT_ON_RENDER_THREAD_V(0.0);270ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0.0);271return OpenXRAPI::get_singleton()->get_render_state_z_far();272}273274void OpenXRAPIExtension::set_velocity_texture(RID p_render_target) {275ERR_FAIL_NULL(OpenXRAPI::get_singleton());276OpenXRAPI::get_singleton()->set_velocity_texture(p_render_target);277}278279void OpenXRAPIExtension::set_velocity_depth_texture(RID p_render_target) {280ERR_FAIL_NULL(OpenXRAPI::get_singleton());281OpenXRAPI::get_singleton()->set_velocity_depth_texture(p_render_target);282}283284void OpenXRAPIExtension::set_velocity_target_size(const Size2i &p_target_size) {285ERR_FAIL_NULL(OpenXRAPI::get_singleton());286OpenXRAPI::get_singleton()->set_velocity_target_size(p_target_size);287}288289PackedInt64Array OpenXRAPIExtension::get_supported_swapchain_formats() {290ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), PackedInt64Array());291return OpenXRAPI::get_singleton()->get_supported_swapchain_formats();292}293294uint64_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) {295ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);296297OpenXRAPI::OpenXRSwapChainInfo *new_swapchain_info = memnew(OpenXRAPI::OpenXRSwapChainInfo);298new_swapchain_info->create(p_create_flags, p_usage_flags, p_swapchain_format, p_width, p_height, p_sample_count, p_array_size);299return reinterpret_cast<uint64_t>(new_swapchain_info);300}301302void OpenXRAPIExtension::openxr_swapchain_free(uint64_t p_swapchain_info) {303ERR_FAIL_NULL(OpenXRAPI::get_singleton());304305OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);306swapchain_info->free();307memfree(swapchain_info);308}309310uint64_t OpenXRAPIExtension::openxr_swapchain_get_swapchain(uint64_t p_swapchain_info) {311ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);312313OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);314XrSwapchain swapchain = swapchain_info->get_swapchain();315return (uint64_t)swapchain;316}317318void OpenXRAPIExtension::openxr_swapchain_acquire(uint64_t p_swapchain_info) {319ERR_FAIL_NULL(OpenXRAPI::get_singleton());320321OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);322bool should_render = true; // Can ignore should_render.323swapchain_info->acquire(should_render);324}325326RID OpenXRAPIExtension::openxr_swapchain_get_image(uint64_t p_swapchain_info) {327ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), RID());328329OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);330return swapchain_info->get_image();331}332333void OpenXRAPIExtension::openxr_swapchain_release(uint64_t p_swapchain_info) {334ERR_FAIL_NULL(OpenXRAPI::get_singleton());335336OpenXRAPI::OpenXRSwapChainInfo *swapchain_info = reinterpret_cast<OpenXRAPI::OpenXRSwapChainInfo *>(p_swapchain_info);337swapchain_info->release();338}339340uint64_t OpenXRAPIExtension::get_projection_layer() {341ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), 0);342return (uint64_t)OpenXRAPI::get_singleton()->get_projection_layer();343}344345void OpenXRAPIExtension::set_render_region(const Rect2i &p_render_region) {346ERR_FAIL_NULL(OpenXRAPI::get_singleton());347OpenXRAPI::get_singleton()->set_render_region(p_render_region);348}349350void OpenXRAPIExtension::set_emulate_environment_blend_mode_alpha_blend(bool p_enabled) {351ERR_FAIL_NULL(OpenXRAPI::get_singleton());352OpenXRAPI::get_singleton()->set_emulate_environment_blend_mode_alpha_blend(p_enabled);353}354355OpenXRAPIExtension::OpenXRAlphaBlendModeSupport OpenXRAPIExtension::is_environment_blend_mode_alpha_blend_supported() {356ERR_FAIL_NULL_V(OpenXRAPI::get_singleton(), OPENXR_ALPHA_BLEND_MODE_SUPPORT_NONE);357return (OpenXRAPIExtension::OpenXRAlphaBlendModeSupport)OpenXRAPI::get_singleton()->is_environment_blend_mode_alpha_blend_supported();358}359360OpenXRAPIExtension::OpenXRAPIExtension() {361}362363364