Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mostly remove the use of the OPENXR ifdef #16344

Merged
merged 7 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ if(WIN32)
endif()

set(CommonVR
Common/VR/OpenXRLoader.cpp
Common/VR/OpenXRLoader.h
Common/VR/PPSSPPVR.cpp
Common/VR/PPSSPPVR.h
Common/VR/VRBase.cpp
Expand Down
4 changes: 3 additions & 1 deletion Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@
<ClInclude Include="UI\UIScreen.h" />
<ClInclude Include="UI\View.h" />
<ClInclude Include="UI\ViewGroup.h" />
<ClInclude Include="VR\OpenXRLoader.h" />
<ClInclude Include="VR\PPSSPPVR.h" />
<ClInclude Include="VR\VRBase.h" />
<ClInclude Include="VR\VRFramebuffer.h" />
Expand Down Expand Up @@ -995,6 +996,7 @@
<ClCompile Include="UI\UIScreen.cpp" />
<ClCompile Include="UI\View.cpp" />
<ClCompile Include="UI\ViewGroup.cpp" />
<ClCompile Include="VR\OpenXRLoader.cpp" />
<ClCompile Include="VR\PPSSPPVR.cpp" />
<ClCompile Include="VR\VRBase.cpp" />
<ClCompile Include="VR\VRFramebuffer.cpp" />
Expand All @@ -1015,4 +1017,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@
<ClInclude Include="VR\VRRenderer.h">
<Filter>VR</Filter>
</ClInclude>
<ClInclude Include="VR\OpenXRLoader.h">
<Filter>VR</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ABI.cpp" />
Expand Down Expand Up @@ -839,6 +842,9 @@
<ClCompile Include="VR\VRRenderer.cpp">
<Filter>VR</Filter>
</ClCompile>
<ClCompile Include="VR\OpenXRLoader.cpp">
<Filter>VR</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Crypto">
Expand Down Expand Up @@ -952,4 +958,4 @@
<Filter>ext\libpng17</Filter>
</Text>
</ItemGroup>
</Project>
</Project>
160 changes: 160 additions & 0 deletions Common/VR/OpenXRLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#include "Common/VR/OpenXRLoader.h"
#ifdef _WIN32
#include "Common/CommonWindows.h"
#endif

#ifdef XR_NO_PROTOTYPES

PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Prefer spaces for alignment, h too...?

-[Unknown]

PFN_xrEnumerateApiLayerProperties xrEnumerateApiLayerProperties;
PFN_xrEnumerateInstanceExtensionProperties xrEnumerateInstanceExtensionProperties;
PFN_xrCreateInstance xrCreateInstance;
PFN_xrDestroyInstance xrDestroyInstance;
PFN_xrGetInstanceProperties xrGetInstanceProperties;
PFN_xrPollEvent xrPollEvent;
PFN_xrResultToString xrResultToString;
PFN_xrStructureTypeToString xrStructureTypeToString;
PFN_xrGetSystem xrGetSystem;
PFN_xrGetSystemProperties xrGetSystemProperties;
PFN_xrEnumerateEnvironmentBlendModes xrEnumerateEnvironmentBlendModes;
PFN_xrCreateSession xrCreateSession;
PFN_xrDestroySession xrDestroySession;
PFN_xrEnumerateReferenceSpaces xrEnumerateReferenceSpaces;
PFN_xrCreateReferenceSpace xrCreateReferenceSpace;
PFN_xrGetReferenceSpaceBoundsRect xrGetReferenceSpaceBoundsRect;
PFN_xrCreateActionSpace xrCreateActionSpace;
PFN_xrLocateSpace xrLocateSpace;
PFN_xrDestroySpace xrDestroySpace;
PFN_xrEnumerateViewConfigurations xrEnumerateViewConfigurations;
PFN_xrGetViewConfigurationProperties xrGetViewConfigurationProperties;
PFN_xrEnumerateViewConfigurationViews xrEnumerateViewConfigurationViews;
PFN_xrEnumerateSwapchainFormats xrEnumerateSwapchainFormats;
PFN_xrCreateSwapchain xrCreateSwapchain;
PFN_xrDestroySwapchain xrDestroySwapchain;
PFN_xrEnumerateSwapchainImages xrEnumerateSwapchainImages;
PFN_xrAcquireSwapchainImage xrAcquireSwapchainImage;
PFN_xrWaitSwapchainImage xrWaitSwapchainImage;
PFN_xrReleaseSwapchainImage xrReleaseSwapchainImage;
PFN_xrBeginSession xrBeginSession;
PFN_xrEndSession xrEndSession;
PFN_xrRequestExitSession xrRequestExitSession;
PFN_xrWaitFrame xrWaitFrame;
PFN_xrBeginFrame xrBeginFrame;
PFN_xrEndFrame xrEndFrame;
PFN_xrLocateViews xrLocateViews;
PFN_xrStringToPath xrStringToPath;
PFN_xrPathToString xrPathToString;
PFN_xrCreateActionSet xrCreateActionSet;
PFN_xrDestroyActionSet xrDestroyActionSet;
PFN_xrCreateAction xrCreateAction;
PFN_xrDestroyAction xrDestroyAction;
PFN_xrSuggestInteractionProfileBindings xrSuggestInteractionProfileBindings;
PFN_xrAttachSessionActionSets xrAttachSessionActionSets;
PFN_xrGetCurrentInteractionProfile xrGetCurrentInteractionProfile;
PFN_xrGetActionStateBoolean xrGetActionStateBoolean;
PFN_xrGetActionStateFloat xrGetActionStateFloat;
PFN_xrGetActionStateVector2f xrGetActionStateVector2f;
PFN_xrGetActionStatePose xrGetActionStatePose;
PFN_xrSyncActions xrSyncActions;
PFN_xrEnumerateBoundSourcesForAction xrEnumerateBoundSourcesForAction;
PFN_xrGetInputSourceLocalizedName xrGetInputSourceLocalizedName;
PFN_xrApplyHapticFeedback xrApplyHapticFeedback;
PFN_xrStopHapticFeedback xrStopHapticFeedback;

#ifdef _WIN32
#define dlsym(x, y) GetProcAddress(x, y)
static HMODULE g_xrLibrary;
#else
#define dlsym(x, y) nullptr
void *xrLibrary;
#endif

#define LOAD_INSTANCE_FUNC(name) (PFN_ ## name)xrGetInstanceProcAddr(instance, #name, (PFN_xrVoidFunction *)(&name))


bool XRLoad() {
if (g_xrLibrary) {
// Already loaded. That's OK.
return true;
}

#ifdef _WIN32
g_xrLibrary = LoadLibrary(L"openxr_loader.dll");
if (!g_xrLibrary) {
return false;
}
#else
void *library = nullptr;
#endif

// Load the three basic functions.
xrGetInstanceProcAddr = (PFN_xrGetInstanceProcAddr)dlsym(g_xrLibrary, "xrGetInstanceProcAddr");
xrEnumerateApiLayerProperties = (PFN_xrEnumerateApiLayerProperties)dlsym(g_xrLibrary, "xrEnumerateApiLayerProperties");
xrEnumerateInstanceExtensionProperties = (PFN_xrEnumerateInstanceExtensionProperties)dlsym(g_xrLibrary, "xrEnumerateInstanceExtensionProperties");

// Load the rest.
return true;
}

void XRLoadInstanceFunctions(XrInstance instance) {
LOAD_INSTANCE_FUNC(xrCreateInstance);
LOAD_INSTANCE_FUNC(xrGetInstanceProcAddr);
LOAD_INSTANCE_FUNC(xrEnumerateApiLayerProperties);
LOAD_INSTANCE_FUNC(xrEnumerateInstanceExtensionProperties);
LOAD_INSTANCE_FUNC(xrCreateInstance);
LOAD_INSTANCE_FUNC(xrDestroyInstance);
LOAD_INSTANCE_FUNC(xrGetInstanceProperties);
LOAD_INSTANCE_FUNC(xrPollEvent);
LOAD_INSTANCE_FUNC(xrResultToString);
LOAD_INSTANCE_FUNC(xrStructureTypeToString);
LOAD_INSTANCE_FUNC(xrGetSystem);
LOAD_INSTANCE_FUNC(xrGetSystemProperties);
LOAD_INSTANCE_FUNC(xrEnumerateEnvironmentBlendModes);
LOAD_INSTANCE_FUNC(xrCreateSession);
LOAD_INSTANCE_FUNC(xrDestroySession);
LOAD_INSTANCE_FUNC(xrEnumerateReferenceSpaces);
LOAD_INSTANCE_FUNC(xrCreateReferenceSpace);
LOAD_INSTANCE_FUNC(xrGetReferenceSpaceBoundsRect);
LOAD_INSTANCE_FUNC(xrCreateActionSpace);
LOAD_INSTANCE_FUNC(xrLocateSpace);
LOAD_INSTANCE_FUNC(xrDestroySpace);
LOAD_INSTANCE_FUNC(xrEnumerateViewConfigurations);
LOAD_INSTANCE_FUNC(xrGetViewConfigurationProperties);
LOAD_INSTANCE_FUNC(xrEnumerateViewConfigurationViews);
LOAD_INSTANCE_FUNC(xrEnumerateSwapchainFormats);
LOAD_INSTANCE_FUNC(xrCreateSwapchain);
LOAD_INSTANCE_FUNC(xrDestroySwapchain);
LOAD_INSTANCE_FUNC(xrEnumerateSwapchainImages);
LOAD_INSTANCE_FUNC(xrAcquireSwapchainImage);
LOAD_INSTANCE_FUNC(xrWaitSwapchainImage);
LOAD_INSTANCE_FUNC(xrReleaseSwapchainImage);
LOAD_INSTANCE_FUNC(xrBeginSession);
LOAD_INSTANCE_FUNC(xrEndSession);
LOAD_INSTANCE_FUNC(xrRequestExitSession);
LOAD_INSTANCE_FUNC(xrWaitFrame);
LOAD_INSTANCE_FUNC(xrBeginFrame);
LOAD_INSTANCE_FUNC(xrEndFrame);
LOAD_INSTANCE_FUNC(xrLocateViews);
LOAD_INSTANCE_FUNC(xrStringToPath);
LOAD_INSTANCE_FUNC(xrPathToString);
LOAD_INSTANCE_FUNC(xrCreateActionSet);
LOAD_INSTANCE_FUNC(xrDestroyActionSet);
LOAD_INSTANCE_FUNC(xrCreateAction);
LOAD_INSTANCE_FUNC(xrDestroyAction);
LOAD_INSTANCE_FUNC(xrSuggestInteractionProfileBindings);
LOAD_INSTANCE_FUNC(xrAttachSessionActionSets);
LOAD_INSTANCE_FUNC(xrGetCurrentInteractionProfile);
LOAD_INSTANCE_FUNC(xrGetActionStateBoolean);
LOAD_INSTANCE_FUNC(xrGetActionStateFloat);
LOAD_INSTANCE_FUNC(xrGetActionStateVector2f);
LOAD_INSTANCE_FUNC(xrGetActionStatePose);
LOAD_INSTANCE_FUNC(xrSyncActions);
LOAD_INSTANCE_FUNC(xrEnumerateBoundSourcesForAction);
LOAD_INSTANCE_FUNC(xrGetInputSourceLocalizedName);
LOAD_INSTANCE_FUNC(xrApplyHapticFeedback);
LOAD_INSTANCE_FUNC(xrStopHapticFeedback);

// TODO: Load any extensions we need, too.
}

#endif
109 changes: 109 additions & 0 deletions Common/VR/OpenXRLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#pragma once

//Vulkan
#ifdef VK_USE_NATIVE_LIB
#include <vulkan/vulkan.h>
#else
#include "Common/GPU/Vulkan/VulkanLoader.h"
using namespace PPSSPP_VK;
#endif

#ifdef ANDROID
//OpenXR
#define XR_USE_PLATFORM_ANDROID 1
#define XR_USE_GRAPHICS_API_OPENGL_ES 1
#define XR_USE_GRAPHICS_API_VULKAN 1
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <jni.h>
#elif defined(_WIN32)
#include "Common/CommonWindows.h"
#include "Common/GPU/OpenGL/GLCommon.h"
#include "Common/VR/OpenXRLoader.h"
#include <unknwn.h>
#define XR_USE_PLATFORM_WIN32 1
#define XR_USE_GRAPHICS_API_OPENGL 1
#define XR_USE_GRAPHICS_API_VULKAN 1
#define XR_NO_PROTOTYPES 1
#else
#define XR_USE_GRAPHICS_API_VULKAN 1
#define XR_NO_PROTOTYPES 1
#endif

#include <openxr.h>
#include <openxr_platform.h>

#ifdef XR_NO_PROTOTYPES

extern PFN_xrGetInstanceProcAddr xrGetInstanceProcAddr;
extern PFN_xrEnumerateApiLayerProperties xrEnumerateApiLayerProperties;
extern PFN_xrEnumerateInstanceExtensionProperties xrEnumerateInstanceExtensionProperties;
extern PFN_xrCreateInstance xrCreateInstance;
extern PFN_xrDestroyInstance xrDestroyInstance;
extern PFN_xrGetInstanceProperties xrGetInstanceProperties;
extern PFN_xrPollEvent xrPollEvent;
extern PFN_xrResultToString xrResultToString;
extern PFN_xrStructureTypeToString xrStructureTypeToString;
extern PFN_xrGetSystem xrGetSystem;
extern PFN_xrGetSystemProperties xrGetSystemProperties;
extern PFN_xrEnumerateEnvironmentBlendModes xrEnumerateEnvironmentBlendModes;
extern PFN_xrCreateSession xrCreateSession;
extern PFN_xrDestroySession xrDestroySession;
extern PFN_xrEnumerateReferenceSpaces xrEnumerateReferenceSpaces;
extern PFN_xrCreateReferenceSpace xrCreateReferenceSpace;
extern PFN_xrGetReferenceSpaceBoundsRect xrGetReferenceSpaceBoundsRect;
extern PFN_xrCreateActionSpace xrCreateActionSpace;
extern PFN_xrLocateSpace xrLocateSpace;
extern PFN_xrDestroySpace xrDestroySpace;
extern PFN_xrEnumerateViewConfigurations xrEnumerateViewConfigurations;
extern PFN_xrGetViewConfigurationProperties xrGetViewConfigurationProperties;
extern PFN_xrEnumerateViewConfigurationViews xrEnumerateViewConfigurationViews;
extern PFN_xrEnumerateSwapchainFormats xrEnumerateSwapchainFormats;
extern PFN_xrCreateSwapchain xrCreateSwapchain;
extern PFN_xrDestroySwapchain xrDestroySwapchain;
extern PFN_xrEnumerateSwapchainImages xrEnumerateSwapchainImages;
extern PFN_xrAcquireSwapchainImage xrAcquireSwapchainImage;
extern PFN_xrWaitSwapchainImage xrWaitSwapchainImage;
extern PFN_xrReleaseSwapchainImage xrReleaseSwapchainImage;
extern PFN_xrBeginSession xrBeginSession;
extern PFN_xrEndSession xrEndSession;
extern PFN_xrRequestExitSession xrRequestExitSession;
extern PFN_xrWaitFrame xrWaitFrame;
extern PFN_xrBeginFrame xrBeginFrame;
extern PFN_xrEndFrame xrEndFrame;
extern PFN_xrLocateViews xrLocateViews;
extern PFN_xrStringToPath xrStringToPath;
extern PFN_xrPathToString xrPathToString;
extern PFN_xrCreateActionSet xrCreateActionSet;
extern PFN_xrDestroyActionSet xrDestroyActionSet;
extern PFN_xrCreateAction xrCreateAction;
extern PFN_xrDestroyAction xrDestroyAction;
extern PFN_xrSuggestInteractionProfileBindings xrSuggestInteractionProfileBindings;
extern PFN_xrAttachSessionActionSets xrAttachSessionActionSets;
extern PFN_xrGetCurrentInteractionProfile xrGetCurrentInteractionProfile;
extern PFN_xrGetActionStateBoolean xrGetActionStateBoolean;
extern PFN_xrGetActionStateFloat xrGetActionStateFloat;
extern PFN_xrGetActionStateVector2f xrGetActionStateVector2f;
extern PFN_xrGetActionStatePose xrGetActionStatePose;
extern PFN_xrSyncActions xrSyncActions;
extern PFN_xrEnumerateBoundSourcesForAction xrEnumerateBoundSourcesForAction;
extern PFN_xrGetInputSourceLocalizedName xrGetInputSourceLocalizedName;
extern PFN_xrApplyHapticFeedback xrApplyHapticFeedback;
extern PFN_xrStopHapticFeedback xrStopHapticFeedback;

// Dynamic loader for OpenXR on desktop platforms.
// On Quest/Pico, we use statically linked loaders provided by the platform owners.
// On Windows (and Linux etc), we're not so lucky - we could link to static libraries, but we really
// don't want to as we still want to function when one is not present.
//
// Similar to our dynamic Vulkan loader.

bool XRLoad();
void XRLoadInstanceFunctions(XrInstance instance);

#else

inline bool XRLoad() { return true; }
inline void XRLoadInstanceFunctions(XrInstance instance) {}

#endif
18 changes: 10 additions & 8 deletions Common/VR/PPSSPPVR.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef OPENXR

#include "Common/GPU/OpenGL/GLRenderManager.h"
#include "Common/GPU/Vulkan/VulkanContext.h"

Expand Down Expand Up @@ -396,6 +394,12 @@ void* BindVRFramebuffer() {
return VR_BindFramebuffer(VR_GetEngine());
}

inline float clampFloat(float x, float minValue, float maxValue) {
if (x < minValue) return minValue;
if (x > maxValue) return maxValue;
return x;
}

bool StartVRRender() {
if (!VR_GetConfig(VR_CONFIG_VIEWPORT_VALID)) {
VR_InitRenderer(VR_GetEngine(), IsMultiviewSupported());
Expand Down Expand Up @@ -430,8 +434,8 @@ bool StartVRRender() {
height = 0;
side = 0;
}
g_Config.fCameraHeight = std::clamp(height, -10.0f, 10.0f);
g_Config.fCameraSide = std::clamp(side, -10.0f, 10.0f);
g_Config.fCameraHeight = clampFloat(height, -10.0f, 10.0f);
g_Config.fCameraSide = clampFloat(side, -10.0f, 10.0f);

//right joystick controls distance and fov
float dst = g_Config.fCameraDistance;
Expand All @@ -445,8 +449,8 @@ bool StartVRRender() {
fov = 100;
dst = 0;
}
g_Config.fCameraDistance = std::clamp(dst, -10.0f, 10.0f);
g_Config.fFieldOfViewPercentage = std::clamp(fov, 100.0f, 200.0f);
g_Config.fCameraDistance = clampFloat(dst, -10.0f, 10.0f);
g_Config.fFieldOfViewPercentage = clampFloat(fov, 100.0f, 200.0f);
}

// Set customizations
Expand Down Expand Up @@ -599,5 +603,3 @@ void UpdateVRView(float* leftEye, float* rightEye) {
memcpy(dst[index], renderView.M, 16 * sizeof(float));
}
}

#endif
Loading