Path: blob/master/modules/openxr/extensions/openxr_extension_wrapper.h
11353 views
/**************************************************************************/1/* openxr_extension_wrapper.h */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#pragma once3132#include "core/error/error_macros.h"33#include "core/math/projection.h"34#include "core/object/class_db.h"35#include "core/object/gdvirtual.gen.inc"36#include "core/templates/hash_map.h"37#include "core/templates/rid.h"38#include "core/variant/native_ptr.h"39#include "core/variant/typed_array.h"40#include "core/variant/variant.h"4142#include <openxr/openxr.h>4344class OpenXRAPI;45class OpenXRAPIExtension;46class OpenXRActionMap;4748// `OpenXRExtensionWrapper` allows us to implement OpenXR extensions.49class OpenXRExtensionWrapper : public Object {50GDCLASS(OpenXRExtensionWrapper, Object);5152Ref<OpenXRAPIExtension> _gdextension_get_openxr_api();53void _gdextension_register_extension_wrapper();5455protected:56static void _bind_methods();5758Ref<OpenXRAPIExtension> openxr_api_extension;5960public:61// `get_requested_extensions` should return a list of OpenXR extensions related to this extension.62// If the bool * is a nullptr this extension is mandatory63// If the bool * points to a boolean, the boolean will be updated64// to true if the extension is enabled.65virtual HashMap<String, bool *> get_requested_extensions();6667GDVIRTUAL0R(Dictionary, _get_requested_extensions);6869// These functions allow an extension to add entries to a struct chain.70// `p_next_pointer` points to the last struct that was created for this chain71// and should be used as the value for the `pNext` pointer in the first struct you add.72// You should return the pointer to the last struct you define as your result.73// If you are not adding any structs, just return `p_next_pointer`.74// See existing extensions for examples of this implementation.75virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when we interrogate OpenXR's system abilities.76virtual void *set_instance_create_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when we create our OpenXR instance.77virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when we create our OpenXR session.78virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when creating OpenXR swap chains.79virtual void *set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer);80virtual void *set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer);81virtual void *set_reference_space_create_info_and_get_next_pointer(int p_reference_space_type, void *p_next_pointer);82// These will only be called for extensions registered via OpenXRApi::register_frame_info_extension().83virtual void *set_frame_wait_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrWaitFrame84virtual void *set_view_locate_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrLocateViews85virtual void *set_frame_end_info_and_get_next_pointer(void *p_next_pointer); // Add additional data structures when calling xrEndFrame8687//TODO workaround as GDExtensionPtr<void> return type results in build error in godot-cpp88GDVIRTUAL1R(uint64_t, _set_system_properties_and_get_next_pointer, GDExtensionPtr<void>);89GDVIRTUAL1R(uint64_t, _set_instance_create_info_and_get_next_pointer, GDExtensionPtr<void>);90GDVIRTUAL1R(uint64_t, _set_session_create_and_get_next_pointer, GDExtensionPtr<void>);91GDVIRTUAL1R(uint64_t, _set_swapchain_create_info_and_get_next_pointer, GDExtensionPtr<void>);92GDVIRTUAL2R(uint64_t, _set_hand_joint_locations_and_get_next_pointer, int, GDExtensionPtr<void>);93GDVIRTUAL2R(uint64_t, _set_projection_views_and_get_next_pointer, int, GDExtensionPtr<void>);94GDVIRTUAL1R(uint64_t, _set_frame_wait_info_and_get_next_pointer, GDExtensionPtr<void>);95GDVIRTUAL1R(uint64_t, _set_frame_end_info_and_get_next_pointer, GDExtensionPtr<void>);96GDVIRTUAL1R(uint64_t, _set_view_locate_info_and_get_next_pointer, GDExtensionPtr<void>);97GDVIRTUAL2R(uint64_t, _set_reference_space_create_info_and_get_next_pointer, int, GDExtensionPtr<void>);98GDVIRTUAL0R(int, _get_composition_layer_count);99GDVIRTUAL1R(uint64_t, _get_composition_layer, int);100GDVIRTUAL1R(int, _get_composition_layer_order, int);101102virtual PackedStringArray get_suggested_tracker_names();103104GDVIRTUAL0R(PackedStringArray, _get_suggested_tracker_names);105106// `on_register_metadata` allows extensions to register additional controller metadata.107// This function is called even when OpenXRApi is not constructured as the metadata108// needs to be available to the editor.109// Also extensions should provide metadata regardless of whether they are supported110// on the host system as the controller data is used to setup action maps for users111// who may have access to the relevant hardware.112virtual void on_register_metadata();113114virtual void on_before_instance_created(); // `on_before_instance_created` is called before we create our OpenXR instance.115virtual void on_instance_created(const XrInstance p_instance); // `on_instance_created` is called right after we've successfully created our OpenXR instance.116virtual void on_instance_destroyed(); // `on_instance_destroyed` is called right before we destroy our OpenXR instance.117virtual void on_session_created(const XrSession p_session); // `on_session_created` is called right after we've successfully created our OpenXR session.118virtual void on_session_destroyed(); // `on_session_destroyed` is called right before we destroy our OpenXR session.119120// `on_process` is called as part of our OpenXR process handling,121// this happens right before physics process and normal processing is run.122// This is when controller data is queried and made available to game logic.123virtual void on_process();124virtual void on_sync_actions(); // `on_sync_actions` is called right after we sync our action sets.125virtual void on_pre_render(); // `on_pre_render` is called right before we start rendering our XR viewports.126virtual void on_main_swapchains_created(); // `on_main_swapchains_created` is called right after our main swapchains are (re)created.127virtual void on_pre_draw_viewport(RID p_render_target); // `on_pre_draw_viewport` is called right before we start rendering this viewport128virtual void on_post_draw_viewport(RID p_render_target); // `on_port_draw_viewport` is called right after we start rendering this viewport (note that on Vulkan draw commands may only be queued)129130GDVIRTUAL0(_on_register_metadata);131GDVIRTUAL0(_on_before_instance_created);132GDVIRTUAL1(_on_instance_created, uint64_t);133GDVIRTUAL0(_on_instance_destroyed);134GDVIRTUAL1(_on_session_created, uint64_t);135GDVIRTUAL0(_on_process);136GDVIRTUAL0(_on_sync_actions);137GDVIRTUAL0(_on_pre_render);138GDVIRTUAL0(_on_main_swapchains_created);139GDVIRTUAL0(_on_session_destroyed);140GDVIRTUAL1(_on_pre_draw_viewport, RID);141GDVIRTUAL1(_on_post_draw_viewport, RID);142143virtual void on_state_idle(); // `on_state_idle` is called when the OpenXR session state is changed to idle.144virtual void on_state_ready(); // `on_state_ready` is called when the OpenXR session state is changed to ready, this means OpenXR is ready to setup our session.145virtual void on_state_synchronized(); // `on_state_synchronized` is called when the OpenXR session state is changed to synchronized, note that OpenXR also returns to this state when our application looses focus.146virtual void on_state_visible(); // `on_state_visible` is called when the OpenXR session state is changed to visible, OpenXR is now ready to receive frames.147virtual void on_state_focused(); // `on_state_focused` is called when the OpenXR session state is changed to focused, this state is the active state when our game runs.148virtual void on_state_stopping(); // `on_state_stopping` is called when the OpenXR session state is changed to stopping.149virtual void on_state_loss_pending(); // `on_state_loss_pending` is called when the OpenXR session state is changed to loss pending.150virtual void on_state_exiting(); // `on_state_exiting` is called when the OpenXR session state is changed to exiting.151152GDVIRTUAL0(_on_state_idle);153GDVIRTUAL0(_on_state_ready);154GDVIRTUAL0(_on_state_synchronized);155GDVIRTUAL0(_on_state_visible);156GDVIRTUAL0(_on_state_focused);157GDVIRTUAL0(_on_state_stopping);158GDVIRTUAL0(_on_state_loss_pending);159GDVIRTUAL0(_on_state_exiting);160161// These will only be called on extensions registered via OpenXRAPI::register_composition_layer_provider().162virtual int get_composition_layer_count();163virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index);164virtual int get_composition_layer_order(int p_index);165166virtual void *set_viewport_composition_layer_and_get_next_pointer(const XrCompositionLayerBaseHeader *p_layer, const Dictionary &p_property_values, void *p_next_pointer); // Add additional data structures to composition layers created via OpenXRCompositionLayer.167virtual void on_viewport_composition_layer_destroyed(const XrCompositionLayerBaseHeader *p_layer); // `on_viewport_composition_layer_destroyed` is called when a composition layer created via OpenXRCompositionLayer is destroyed.168virtual void get_viewport_composition_layer_extension_properties(List<PropertyInfo> *p_property_list); // Get additional property definitions for OpenXRCompositionLayer.169virtual Dictionary get_viewport_composition_layer_extension_property_defaults(); // Get the default values for the additional property definitions for OpenXRCompositionLayer.170virtual void *set_android_surface_swapchain_create_info_and_get_next_pointer(const Dictionary &p_property_values, void *p_next_pointer);171172GDVIRTUAL3R(uint64_t, _set_viewport_composition_layer_and_get_next_pointer, GDExtensionConstPtr<void>, Dictionary, GDExtensionPtr<void>);173GDVIRTUAL1(_on_viewport_composition_layer_destroyed, GDExtensionConstPtr<void>);174GDVIRTUAL0R(TypedArray<Dictionary>, _get_viewport_composition_layer_extension_properties);175GDVIRTUAL0R(Dictionary, _get_viewport_composition_layer_extension_property_defaults);176GDVIRTUAL2R(uint64_t, _set_android_surface_swapchain_create_info_and_get_next_pointer, Dictionary, GDExtensionPtr<void>);177178// `on_event_polled` is called when there is an OpenXR event to process.179// Should return true if the event was handled, false otherwise.180virtual bool on_event_polled(const XrEventDataBuffer &event);181182GDVIRTUAL1R(bool, _on_event_polled, GDExtensionConstPtr<void>);183184OpenXRExtensionWrapper();185virtual ~OpenXRExtensionWrapper() override;186};187188// `OpenXRGraphicsExtensionWrapper` implements specific logic for each supported graphics API.189class OpenXRGraphicsExtensionWrapper : public OpenXRExtensionWrapper {190public:191virtual void get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) = 0; // `get_usable_swapchain_formats` should return a list of usable color formats.192virtual void get_usable_depth_formats(Vector<int64_t> &p_usable_swap_chains) = 0; // `get_usable_depth_formats` should return a list of usable depth formats.193virtual String get_swapchain_format_name(int64_t p_swapchain_format) const = 0; // `get_swapchain_format_name` should return the constant name of a given format.194virtual bool get_swapchain_image_data(XrSwapchain p_swapchain, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, void **r_swapchain_graphics_data) = 0; // `get_swapchain_image_data` extracts image IDs for the swapchain images and stores there in an implementation dependent data structure.195virtual void cleanup_swapchain_graphics_data(void **p_swapchain_graphics_data) = 0; // `cleanup_swapchain_graphics_data` cleans up the data held in our implementation dependent data structure and should free up its memory.196virtual bool create_projection_fov(const XrFovf p_fov, double p_z_near, double p_z_far, Projection &r_camera_matrix) = 0; // `create_projection_fov` creates a proper projection matrix based on asymmetric FOV data provided by OpenXR.197virtual RID get_texture(void *p_swapchain_graphics_data, int p_image_index) = 0; // `get_texture` returns a Godot texture RID for the current active texture in our swapchain.198virtual RID get_density_map(void *p_swapchain_graphics_data, int p_image_index) = 0; // `get_density_map` returns a Godot texture RID for the current active density map in our swapchain (if any).199};200201202