2
0
Эх сурвалжийг харах

OpenXRVk: Use PAL to initialize xr loader on Android (#16)

Signed-off-by: moraaar <[email protected]>
moraaar 2 жил өмнө
parent
commit
9a65f816af

+ 5 - 23
Gems/OpenXRVk/Code/Source/OpenXRVkInstance.cpp

@@ -10,10 +10,7 @@
 #include <OpenXRVk/OpenXRVkUtils.h>
 #include <Atom/RHI.Reflect/Vulkan/XRVkDescriptors.h>
 #include <AzCore/Casting/numeric_cast.h>
-
-#ifdef AZ_PLATFORM_ANDROID
-#include <AzCore/Android/AndroidEnv.h>
-#endif
+#include <OpenXRVk_Traits_Platform.h>
 
 namespace OpenXRVk
 {
@@ -88,26 +85,11 @@ namespace OpenXRVk
 
     AZ::RHI::ResultCode Instance::InitInstanceInternal(AZ::RHI::ValidationMode validationMode)
     {
-#ifdef AZ_PLATFORM_ANDROID
-        // Initialize the loader for Android platform
+#if OPENXRVK_TRAIT_NEEDS_INITIALIZE_XR_LOADER
+        if (!Platform::OpenXRInitializeLoader())
         {
-            AZ::Android::AndroidEnv* androidEnv = AZ::Android::AndroidEnv::Get();
-            AZ_Assert(androidEnv != nullptr, "Invalid android enviroment");
-            
-            PFN_xrInitializeLoaderKHR initializeLoader = nullptr;
-            XrResult resultLoader = xrGetInstanceProcAddr(
-                XR_NULL_HANDLE, "xrInitializeLoaderKHR",
-                reinterpret_cast<PFN_xrVoidFunction*>(&initializeLoader));
-            ASSERT_IF_UNSUCCESSFUL(resultLoader);
-
-            XrLoaderInitInfoAndroidKHR loaderInitInfoAndroid;
-            memset(&loaderInitInfoAndroid, 0, sizeof(loaderInitInfoAndroid));
-            loaderInitInfoAndroid.type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR;
-            loaderInitInfoAndroid.next = nullptr;
-            loaderInitInfoAndroid.applicationVM = androidEnv->GetActivityJavaVM();
-            loaderInitInfoAndroid.applicationContext = androidEnv->GetActivityRef();
-            
-            initializeLoader(reinterpret_cast<const XrLoaderInitInfoBaseHeaderKHR*>(&loaderInitInfoAndroid));
+            AZ_Error("OpenXRVk", false, "Could not initialize xr loader.");
+            return AZ::RHI::ResultCode::Fail;
         }
 #endif
 

+ 45 - 0
Gems/OpenXRVk/Code/Source/Platform/Android/OpenXRInitializeLoader_Android.cpp

@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project.
+ * For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+
+#include <AzCore/Android/AndroidEnv.h>
+
+#include <OpenXRVk/OpenXRVkUtils.h>
+#include <OpenXRInitializeLoader_Android.h>
+
+namespace OpenXRVk::Platform
+{
+    bool OpenXRInitializeLoader()
+    {
+        AZ::Android::AndroidEnv* androidEnv = AZ::Android::AndroidEnv::Get();
+        AZ_Assert(androidEnv != nullptr, "Invalid android environment");
+
+        PFN_xrInitializeLoaderKHR initializeLoader = nullptr;
+        XrResult result = xrGetInstanceProcAddr(
+            XR_NULL_HANDLE, "xrInitializeLoaderKHR",
+            reinterpret_cast<PFN_xrVoidFunction*>(&initializeLoader));
+        if (IsError(result))
+        {
+            return false;
+        }
+
+        XrLoaderInitInfoAndroidKHR loaderInitInfoAndroid;
+        memset(&loaderInitInfoAndroid, 0, sizeof(loaderInitInfoAndroid));
+        loaderInitInfoAndroid.type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR;
+        loaderInitInfoAndroid.next = nullptr;
+        loaderInitInfoAndroid.applicationVM = androidEnv->GetActivityJavaVM();
+        loaderInitInfoAndroid.applicationContext = androidEnv->GetActivityRef();
+
+        result = initializeLoader(reinterpret_cast<const XrLoaderInitInfoBaseHeaderKHR*>(&loaderInitInfoAndroid));
+        if (IsError(result))
+        {
+            return false;
+        }
+
+        return true;
+    }
+}

+ 14 - 0
Gems/OpenXRVk/Code/Source/Platform/Android/OpenXRInitializeLoader_Android.h

@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) Contributors to the Open 3D Engine Project.
+ * For complete copyright and license terms please see the LICENSE at the root of this distribution.
+ *
+ * SPDX-License-Identifier: Apache-2.0 OR MIT
+ *
+ */
+#pragma once
+
+namespace OpenXRVk::Platform
+{
+    // Initializes the XR loader for Android platform
+    bool OpenXRInitializeLoader();
+}

+ 4 - 0
Gems/OpenXRVk/Code/Source/Platform/Android/OpenXRVk_Traits_Android.h

@@ -6,3 +6,7 @@
  *
  */
 #pragma once
+
+#define OPENXRVK_TRAIT_NEEDS_INITIALIZE_XR_LOADER 1
+
+#include <OpenXRInitializeLoader_Android.h>

+ 2 - 0
Gems/OpenXRVk/Code/Source/Platform/Android/platform_private_android_files.cmake

@@ -9,4 +9,6 @@
 set(FILES
     OpenXRVk_Traits_Android.h
     OpenXRVk_Traits_Platform.h
+    OpenXRInitializeLoader_Android.h
+    OpenXRInitializeLoader_Android.cpp
 )

+ 2 - 0
Gems/OpenXRVk/Code/Source/Platform/Linux/OpenXRVk_Traits_Linux.h

@@ -6,3 +6,5 @@
  *
  */
 #pragma once
+
+#define OPENXRVK_TRAIT_NEEDS_INITIALIZE_XR_LOADER 0

+ 2 - 0
Gems/OpenXRVk/Code/Source/Platform/Windows/OpenXRVk_Traits_Windows.h

@@ -6,3 +6,5 @@
  *
  */
 #pragma once
+
+#define OPENXRVK_TRAIT_NEEDS_INITIALIZE_XR_LOADER 0