Skip to content

Commit

Permalink
Merge pull request #70694 from BastiaanOlij/change_openxr_extensions
Browse files Browse the repository at this point in the history
Various improvements to OpenXR extension wrappers
  • Loading branch information
akien-mga committed Jan 5, 2023
2 parents ac0c757 + b6550c4 commit 5dfefc6
Show file tree
Hide file tree
Showing 31 changed files with 883 additions and 536 deletions.
3 changes: 3 additions & 0 deletions modules/openxr/SCsub
Expand Up @@ -95,10 +95,13 @@ if env["opengl3"]:

env_openxr.add_source_files(module_obj, "extensions/openxr_palm_pose_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_composition_layer_depth_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_htc_controller_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_htc_vive_tracker_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_huawei_controller_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_hand_tracking_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_fb_passthrough_extension_wrapper.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_fb_display_refresh_rate_extension.cpp")
env_openxr.add_source_files(module_obj, "extensions/openxr_wmr_controller_extension.cpp")

env.modules_sources += module_obj

Expand Down
320 changes: 2 additions & 318 deletions modules/openxr/action_map/openxr_interaction_profile_meta_data.cpp

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions modules/openxr/extensions/openxr_android_extension.cpp
Expand Up @@ -44,11 +44,17 @@ OpenXRAndroidExtension *OpenXRAndroidExtension::get_singleton() {
return singleton;
}

OpenXRAndroidExtension::OpenXRAndroidExtension(OpenXRAPI *p_openxr_api) :
OpenXRExtensionWrapper(p_openxr_api) {
OpenXRAndroidExtension::OpenXRAndroidExtension() {
singleton = this;
}

HashMap<String, bool *> OpenXRAndroidExtension::get_requested_extensions() {
HashMap<String, bool *> request_extensions;

request_extensions[XR_KHR_LOADER_INIT_ANDROID_EXTENSION_NAME] = &loader_init_extension_available;
request_extensions[XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME] = &create_instance_extension_available;

return request_extensions;
}

void OpenXRAndroidExtension::on_before_instance_created() {
Expand Down
3 changes: 2 additions & 1 deletion modules/openxr/extensions/openxr_android_extension.h
Expand Up @@ -38,8 +38,9 @@ class OpenXRAndroidExtension : public OpenXRExtensionWrapper {
public:
static OpenXRAndroidExtension *get_singleton();

OpenXRAndroidExtension(OpenXRAPI *p_openxr_api);
OpenXRAndroidExtension();

virtual HashMap<String, bool *> get_requested_extensions() override;
virtual void on_before_instance_created() override;
virtual void *set_instance_create_info_and_get_next_pointer(void *p_next_pointer) override;

Expand Down
Expand Up @@ -36,17 +36,22 @@ OpenXRCompositionLayerDepthExtension *OpenXRCompositionLayerDepthExtension::get_
return singleton;
}

OpenXRCompositionLayerDepthExtension::OpenXRCompositionLayerDepthExtension(OpenXRAPI *p_openxr_api) :
OpenXRExtensionWrapper(p_openxr_api) {
OpenXRCompositionLayerDepthExtension::OpenXRCompositionLayerDepthExtension() {
singleton = this;

request_extensions[XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME] = &available;
}

OpenXRCompositionLayerDepthExtension::~OpenXRCompositionLayerDepthExtension() {
singleton = nullptr;
}

HashMap<String, bool *> OpenXRCompositionLayerDepthExtension::get_requested_extensions() {
HashMap<String, bool *> request_extensions;

request_extensions[XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME] = &available;

return request_extensions;
}

bool OpenXRCompositionLayerDepthExtension::is_available() {
return available;
}
Expand Down
Expand Up @@ -38,9 +38,10 @@ class OpenXRCompositionLayerDepthExtension : public OpenXRExtensionWrapper, publ
public:
static OpenXRCompositionLayerDepthExtension *get_singleton();

OpenXRCompositionLayerDepthExtension(OpenXRAPI *p_openxr_api);
OpenXRCompositionLayerDepthExtension();
virtual ~OpenXRCompositionLayerDepthExtension() override;

virtual HashMap<String, bool *> get_requested_extensions() override;
bool is_available();
virtual XrCompositionLayerBaseHeader *get_composition_layer() override;

Expand Down
88 changes: 47 additions & 41 deletions modules/openxr/extensions/openxr_extension_wrapper.h
Expand Up @@ -42,69 +42,75 @@
class OpenXRAPI;
class OpenXRActionMap;

// `OpenXRExtensionWrapper` allows us to implement OpenXR extensions.
class OpenXRExtensionWrapper {
protected:
OpenXRAPI *openxr_api = nullptr;

// Store extension we require.
// If bool pointer is a nullptr this means this extension is mandatory and initialisation will fail if it is not available
// If bool pointer is set, value will be set to true or false depending on whether extension is available
HashMap<String, bool *> request_extensions;

public:
virtual HashMap<String, bool *> get_request_extensions() {
return request_extensions;
}
// `get_requested_extensions` should return a list of OpenXR extensions related to this extension.
// If the bool * is a nullptr this extension is mandatory
// If the bool * points to a boolean, the boolean will be updated
// to true if the extension is enabled.
virtual HashMap<String, bool *> get_requested_extensions() = 0;

// These functions allow an extension to add entries to a struct chain.
// `p_next_pointer` points to the last struct that was created for this chain
// and should be used as the value for the `pNext` pointer in the first struct you add.
// You should return the pointer to the last struct you define as your result.
// If you are not adding any structs, just return `p_next_pointer`.
// See existing extensions for examples of this implementation.
virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; }
virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; }
virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; }
virtual void *set_instance_create_info_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; }
virtual void *set_system_properties_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when we interogate OpenXRS system abilities.
virtual void *set_instance_create_info_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when we create our OpenXR instance.
virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when we create our OpenXR session.
virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when creating OpenXR swap chains.

virtual void on_before_instance_created() {}
virtual void on_instance_created(const XrInstance p_instance) {}
virtual void on_instance_destroyed() {}
virtual void on_session_created(const XrSession p_instance) {}
// `on_register_metadata` allows extensions to register additional controller metadata.
// This function is called even when OpenXRApi is not constructured as the metadata
// needs to be available to the editor.
// Also extensions should provide metadata regardless of whether they are supported
// on the host system as the controller data is used to setup action maps for users
// who may have access to the relevant hardware.
virtual void on_register_metadata() {}

virtual void on_before_instance_created() {} // `on_before_instance_created` is called before we create our OpenXR instance.
virtual void on_instance_created(const XrInstance p_instance) {} // `on_instance_created` is called right after we've successfully created our OpenXR instance.
virtual void on_instance_destroyed() {} // `on_instance_destroyed` is called right before we destroy our OpenXR instance.
virtual void on_session_created(const XrSession p_instance) {} // `on_session_created` is called right after we've successsfully created our OpenXR session.
virtual void on_session_destroyed() {} // `on_session_destroyed` is called right before we destroy our OpenXR session.

// `on_process` is called as part of our OpenXR process handling,
// this happens right before physics process and normal processing is run.
// This is when controller data is queried and made available to game logic.
virtual void on_process() {}
virtual void on_pre_render() {}
virtual void on_session_destroyed() {}
virtual void on_pre_render() {} // `on_pre_render` is called right before we start rendering our XR viewport.

virtual void on_state_idle() {}
virtual void on_state_ready() {}
virtual void on_state_synchronized() {}
virtual void on_state_visible() {}
virtual void on_state_focused() {}
virtual void on_state_stopping() {}
virtual void on_state_loss_pending() {}
virtual void on_state_exiting() {}
virtual void on_state_idle() {} // `on_state_idle` is called when the OpenXR session state is changed to idle.
virtual 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.
virtual 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.
virtual 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.
virtual 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.
virtual void on_state_stopping() {} // `on_state_stopping` is called when the OpenXR session state is changed to stopping.
virtual void on_state_loss_pending() {} // `on_state_loss_pending` is called when the OpenXR session state is changed to loss pending.
virtual void on_state_exiting() {} // `on_state_exiting` is called when the OpenXR session state is changed to exiting.

// Returns true if the event was handled, false otherwise.
// `on_event_polled` is called when there is an OpenXR event to process.
// Should return true if the event was handled, false otherwise.
virtual bool on_event_polled(const XrEventDataBuffer &event) {
return false;
}

OpenXRExtensionWrapper(OpenXRAPI *p_openxr_api) { openxr_api = p_openxr_api; };
OpenXRExtensionWrapper() = default;
virtual ~OpenXRExtensionWrapper() = default;
};

// `OpenXRGraphicsExtensionWrapper` implements specific logic for each supported graphics API.
class OpenXRGraphicsExtensionWrapper : public OpenXRExtensionWrapper {
public:
virtual void get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) = 0;
virtual void get_usable_depth_formats(Vector<int64_t> &p_usable_swap_chains) = 0;
virtual String get_swapchain_format_name(int64_t p_swapchain_format) const = 0;
virtual 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;
virtual void cleanup_swapchain_graphics_data(void **p_swapchain_graphics_data) = 0;
virtual bool create_projection_fov(const XrFovf p_fov, double p_z_near, double p_z_far, Projection &r_camera_matrix) = 0;
virtual RID get_texture(void *p_swapchain_graphics_data, int p_image_index) = 0;

OpenXRGraphicsExtensionWrapper(OpenXRAPI *p_openxr_api) :
OpenXRExtensionWrapper(p_openxr_api){};
virtual 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.
virtual 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.
virtual 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.
virtual 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.
virtual 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.
virtual 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.
virtual 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.
};

#endif // OPENXR_EXTENSION_WRAPPER_H
Expand Up @@ -36,18 +36,22 @@ OpenXRDisplayRefreshRateExtension *OpenXRDisplayRefreshRateExtension::get_single
return singleton;
}

OpenXRDisplayRefreshRateExtension::OpenXRDisplayRefreshRateExtension(OpenXRAPI *p_openxr_api) :
OpenXRExtensionWrapper(p_openxr_api) {
OpenXRDisplayRefreshRateExtension::OpenXRDisplayRefreshRateExtension() {
singleton = this;

// Extensions we use for our hand tracking.
request_extensions[XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME] = &display_refresh_rate_ext;
}

OpenXRDisplayRefreshRateExtension::~OpenXRDisplayRefreshRateExtension() {
display_refresh_rate_ext = false;
}

HashMap<String, bool *> OpenXRDisplayRefreshRateExtension::get_requested_extensions() {
HashMap<String, bool *> request_extensions;

request_extensions[XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME] = &display_refresh_rate_ext;

return request_extensions;
}

void OpenXRDisplayRefreshRateExtension::on_instance_created(const XrInstance p_instance) {
if (display_refresh_rate_ext) {
EXT_INIT_XR_FUNC(xrEnumerateDisplayRefreshRatesFB);
Expand All @@ -65,9 +69,9 @@ float OpenXRDisplayRefreshRateExtension::get_refresh_rate() const {

if (display_refresh_rate_ext) {
float rate;
XrResult result = xrGetDisplayRefreshRateFB(openxr_api->get_session(), &rate);
XrResult result = xrGetDisplayRefreshRateFB(OpenXRAPI::get_singleton()->get_session(), &rate);
if (XR_FAILED(result)) {
print_line("OpenXR: Failed to obtain refresh rate [", openxr_api->get_error_string(result), "]");
print_line("OpenXR: Failed to obtain refresh rate [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
} else {
refresh_rate = rate;
}
Expand All @@ -78,9 +82,9 @@ float OpenXRDisplayRefreshRateExtension::get_refresh_rate() const {

void OpenXRDisplayRefreshRateExtension::set_refresh_rate(float p_refresh_rate) {
if (display_refresh_rate_ext) {
XrResult result = xrRequestDisplayRefreshRateFB(openxr_api->get_session(), p_refresh_rate);
XrResult result = xrRequestDisplayRefreshRateFB(OpenXRAPI::get_singleton()->get_session(), p_refresh_rate);
if (XR_FAILED(result)) {
print_line("OpenXR: Failed to set refresh rate [", openxr_api->get_error_string(result), "]");
print_line("OpenXR: Failed to set refresh rate [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
}
}
}
Expand All @@ -91,21 +95,21 @@ Array OpenXRDisplayRefreshRateExtension::get_available_refresh_rates() const {

if (display_refresh_rate_ext) {
uint32_t display_refresh_rate_count = 0;
result = xrEnumerateDisplayRefreshRatesFB(openxr_api->get_session(), 0, &display_refresh_rate_count, nullptr);
result = xrEnumerateDisplayRefreshRatesFB(OpenXRAPI::get_singleton()->get_session(), 0, &display_refresh_rate_count, nullptr);
if (XR_FAILED(result)) {
print_line("OpenXR: Failed to obtain refresh rates count [", openxr_api->get_error_string(result), "]");
print_line("OpenXR: Failed to obtain refresh rates count [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
}

if (display_refresh_rate_count > 0) {
float *display_refresh_rates = (float *)memalloc(sizeof(float) * display_refresh_rate_count);
if (display_refresh_rates == nullptr) {
print_line("OpenXR: Failed to obtain refresh rates memory buffer [", openxr_api->get_error_string(result), "]");
print_line("OpenXR: Failed to obtain refresh rates memory buffer [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
return arr;
}

result = xrEnumerateDisplayRefreshRatesFB(openxr_api->get_session(), display_refresh_rate_count, &display_refresh_rate_count, display_refresh_rates);
result = xrEnumerateDisplayRefreshRatesFB(OpenXRAPI::get_singleton()->get_session(), display_refresh_rate_count, &display_refresh_rate_count, display_refresh_rates);
if (XR_FAILED(result)) {
print_line("OpenXR: Failed to obtain refresh rates count [", openxr_api->get_error_string(result), "]");
print_line("OpenXR: Failed to obtain refresh rates count [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
memfree(display_refresh_rates);
return arr;
}
Expand Down
Expand Up @@ -45,9 +45,11 @@ class OpenXRDisplayRefreshRateExtension : public OpenXRExtensionWrapper {
public:
static OpenXRDisplayRefreshRateExtension *get_singleton();

OpenXRDisplayRefreshRateExtension(OpenXRAPI *p_openxr_api);
OpenXRDisplayRefreshRateExtension();
virtual ~OpenXRDisplayRefreshRateExtension() override;

virtual HashMap<String, bool *> get_requested_extensions() override;

virtual void on_instance_created(const XrInstance p_instance) override;
virtual void on_instance_destroyed() override;

Expand Down

0 comments on commit 5dfefc6

Please sign in to comment.