Cleanup OpenXR on initialisation failure

This commit is contained in:
Bastiaan Olij 2022-03-24 22:10:43 +11:00
parent d250f12243
commit c78876f977
5 changed files with 25 additions and 28 deletions

View File

@ -99,17 +99,7 @@ bool OpenXRAPI::openxr_is_enabled() {
} }
OpenXRAPI *OpenXRAPI::get_singleton() { OpenXRAPI *OpenXRAPI::get_singleton() {
if (singleton != nullptr) { return singleton;
// already constructed, return our singleton
return singleton;
} else if (openxr_is_enabled()) {
// construct our singleton and return it
singleton = memnew(OpenXRAPI);
return singleton;
} else {
// not enabled, don't instantiate, return nullptr
return nullptr;
}
} }
String OpenXRAPI::get_default_action_map_resource_name() { String OpenXRAPI::get_default_action_map_resource_name() {
@ -145,7 +135,7 @@ String OpenXRAPI::get_swapchain_format_name(int64_t p_swapchain_format) const {
} }
bool OpenXRAPI::load_layer_properties() { bool OpenXRAPI::load_layer_properties() {
// This queries additional layers that are available and can be initialised when we create our OpenXR instance // This queries additional layers that are available and can be initialized when we create our OpenXR instance
if (layer_properties != nullptr) { if (layer_properties != nullptr) {
// already retrieved this // already retrieved this
return true; return true;
@ -175,7 +165,7 @@ bool OpenXRAPI::load_layer_properties() {
} }
bool OpenXRAPI::load_supported_extensions() { bool OpenXRAPI::load_supported_extensions() {
// This queries supported extensions that are available and can be initialised when we create our OpenXR instance // This queries supported extensions that are available and can be initialized when we create our OpenXR instance
if (supported_extensions != nullptr) { if (supported_extensions != nullptr) {
// already retrieved this // already retrieved this
@ -1010,7 +1000,7 @@ bool OpenXRAPI::is_running() {
return running; return running;
} }
bool OpenXRAPI::initialise(const String &p_rendering_driver) { bool OpenXRAPI::initialize(const String &p_rendering_driver) {
ERR_FAIL_COND_V_MSG(instance != XR_NULL_HANDLE, false, "OpenXR instance was already created"); ERR_FAIL_COND_V_MSG(instance != XR_NULL_HANDLE, false, "OpenXR instance was already created");
if (p_rendering_driver == "vulkan") { if (p_rendering_driver == "vulkan") {
@ -1034,7 +1024,7 @@ bool OpenXRAPI::initialise(const String &p_rendering_driver) {
ERR_FAIL_V_MSG(false, "OpenXR: Unsupported rendering device."); ERR_FAIL_V_MSG(false, "OpenXR: Unsupported rendering device.");
} }
// initialise // initialize
if (!load_layer_properties()) { if (!load_layer_properties()) {
destroy_instance(); destroy_instance();
return false; return false;
@ -1068,7 +1058,7 @@ bool OpenXRAPI::initialise(const String &p_rendering_driver) {
return true; return true;
} }
bool OpenXRAPI::initialise_session() { bool OpenXRAPI::initialize_session() {
if (!create_session()) { if (!create_session()) {
destroy_session(); destroy_session();
return false; return false;
@ -1599,7 +1589,7 @@ void OpenXRAPI::end_frame() {
OpenXRAPI::OpenXRAPI() { OpenXRAPI::OpenXRAPI() {
// OpenXRAPI is only constructed if OpenXR is enabled. // OpenXRAPI is only constructed if OpenXR is enabled.
// It will be constructed when the rendering device first accesses OpenXR (be it the Vulkan or OpenGL rendering system) singleton = this;
if (Engine::get_singleton()->is_editor_hint()) { if (Engine::get_singleton()->is_editor_hint()) {
// Enabled OpenXR in the editor? Adjust our settings for the editor // Enabled OpenXR in the editor? Adjust our settings for the editor
@ -1656,7 +1646,7 @@ OpenXRAPI::OpenXRAPI() {
frame_state.predictedDisplayPeriod = 0; frame_state.predictedDisplayPeriod = 0;
#ifdef ANDROID_ENABLED #ifdef ANDROID_ENABLED
// our android wrapper will initialise our android loader at this point // our android wrapper will initialize our android loader at this point
register_extension_wrapper(memnew(OpenXRAndroidExtension(this))); register_extension_wrapper(memnew(OpenXRAndroidExtension(this)));
#endif #endif
} }
@ -1683,6 +1673,8 @@ OpenXRAPI::~OpenXRAPI() {
memfree(layer_properties); memfree(layer_properties);
layer_properties = nullptr; layer_properties = nullptr;
} }
singleton = nullptr;
} }
Transform3D OpenXRAPI::transform_from_pose(const XrPosef &p_pose) { Transform3D OpenXRAPI::transform_from_pose(const XrPosef &p_pose) {

View File

@ -234,8 +234,8 @@ public:
bool is_initialized(); bool is_initialized();
bool is_running(); bool is_running();
bool initialise(const String &p_rendering_driver); bool initialize(const String &p_rendering_driver);
bool initialise_session(); bool initialize_session();
void finish(); void finish();
XrTime get_next_frame_time() { return frame_state.predictedDisplayTime + frame_state.predictedDisplayPeriod; }; XrTime get_next_frame_time() { return frame_state.predictedDisplayTime + frame_state.predictedDisplayPeriod; };

View File

@ -466,7 +466,7 @@ void OpenXRInterface::free_interaction_profiles() {
interaction_profiles.clear(); interaction_profiles.clear();
} }
bool OpenXRInterface::initialise_on_startup() const { bool OpenXRInterface::initialize_on_startup() const {
if (openxr_api == nullptr) { if (openxr_api == nullptr) {
return false; return false;
} else if (!openxr_api->is_initialized()) { } else if (!openxr_api->is_initialized()) {
@ -495,7 +495,7 @@ bool OpenXRInterface::initialize() {
// load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding // load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding
_load_action_map(); _load_action_map();
if (!openxr_api->initialise_session()) { if (!openxr_api->initialize_session()) {
return false; return false;
} }

View File

@ -106,7 +106,7 @@ public:
virtual PackedStringArray get_suggested_tracker_names() const override; virtual PackedStringArray get_suggested_tracker_names() const override;
virtual TrackingStatus get_tracking_status() const override; virtual TrackingStatus get_tracking_status() const override;
bool initialise_on_startup() const; bool initialize_on_startup() const;
virtual bool is_initialized() const override; virtual bool is_initialized() const override;
virtual bool initialize() override; virtual bool initialize() override;
virtual void uninitialize() override; virtual void uninitialize() override;

View File

@ -45,9 +45,13 @@ void preregister_openxr_types() {
// For now we create our openxr device here. If we merge it with openxr_interface we'll create that here soon. // For now we create our openxr device here. If we merge it with openxr_interface we'll create that here soon.
OpenXRAPI::setup_global_defs(); OpenXRAPI::setup_global_defs();
openxr_api = OpenXRAPI::get_singleton(); if (OpenXRAPI::openxr_is_enabled()) {
if (openxr_api) { openxr_api = memnew(OpenXRAPI);
if (!openxr_api->initialise(Main::get_rendering_driver_name())) { ERR_FAIL_NULL(openxr_api);
if (!openxr_api->initialize(Main::get_rendering_driver_name())) {
memdelete(openxr_api);
openxr_api = nullptr;
return; return;
} }
} }
@ -67,7 +71,7 @@ void register_openxr_types() {
openxr_interface.instantiate(); openxr_interface.instantiate();
xr_server->add_interface(openxr_interface); xr_server->add_interface(openxr_interface);
if (openxr_interface->initialise_on_startup()) { if (openxr_interface->initialize_on_startup()) {
openxr_interface->initialize(); openxr_interface->initialize();
} }
} }
@ -75,7 +79,7 @@ void register_openxr_types() {
void unregister_openxr_types() { void unregister_openxr_types() {
if (openxr_interface.is_valid()) { if (openxr_interface.is_valid()) {
// uninitialise just in case // uninitialize just in case
if (openxr_interface->is_initialized()) { if (openxr_interface->is_initialized()) {
openxr_interface->uninitialize(); openxr_interface->uninitialize();
} }
@ -96,5 +100,6 @@ void unregister_openxr_types() {
if (openxr_api) { if (openxr_api) {
openxr_api->finish(); openxr_api->finish();
memdelete(openxr_api); memdelete(openxr_api);
openxr_api = nullptr;
} }
} }