Skip to content

Commit

Permalink
Implementing OpenXR driver
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jan 27, 2022
1 parent 71d1ab5 commit 23be0da
Show file tree
Hide file tree
Showing 44 changed files with 5,478 additions and 108 deletions.
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ opts.Add(BoolVariable("minizip", "Enable ZIP archive support using minizip", Tru
opts.Add(BoolVariable("xaudio2", "Enable the XAudio2 audio driver", False))
opts.Add(BoolVariable("vulkan", "Enable the vulkan video driver", True))
opts.Add(BoolVariable("opengl3", "Enable the OpenGL/GLES3 video driver", True))
opts.Add(BoolVariable("openxr", "Enable the OpenXR driver", True))
opts.Add("custom_modules", "A list of comma-separated directory paths containing custom modules to build.", "")
opts.Add(BoolVariable("custom_modules_recursive", "Detect custom modules recursively for each specified path.", True))
opts.Add(BoolVariable("use_volk", "Use the volk library to load the Vulkan loader dynamically", True))
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1909,5 +1909,14 @@
<member name="rendering/xr/enabled" type="bool" setter="" getter="" default="false">
If [code]true[/code], XR support is enabled in Godot, this ensures required shaders are compiled.
</member>
<member name="xr/openxr/default_action_map" type="String" setter="" getter="" default="&quot;res://default_action_map.tres&quot;">
Action map configuration to load by default.
</member>
<member name="xr/openxr/enabled" type="bool" setter="" getter="" default="false">
If [code]true[/code] Godot will setup and initialise OpenXR on startup.
</member>
<member name="xr/openxr/in_editor" type="bool" setter="" getter="" default="false">
If [code]true[/code] Godot will setup and initialise OpenXR when Godot is run in editor mode enabling editor VR features (experimental).
</member>
</members>
</class>
3 changes: 3 additions & 0 deletions drivers/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ if env["opengl3"]:
SConscript("gl_context/SCsub")
SConscript("gles3/SCsub")

if env["openxr"]:
SConscript("openxr/SCsub")

# Core dependencies
SConscript("png/SCsub")
SConscript("spirv-reflect/SCsub")
Expand Down
104 changes: 104 additions & 0 deletions drivers/openxr/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python

Import("env")

is_supported_on_platform = True
if env["platform"] == "android":
# may need to set OPENXR_ANDROID_VERSION_SUFFIX
env.AppendUnique(CPPDEFINES=["XR_OS_ANDROID", "XR_USE_PLATFORM_ANDROID"])

# may need to include java parts of the openxr loader
elif env["platform"] == "linuxbsd":
env.AppendUnique(CPPDEFINES=["XR_OS_LINUX", "XR_USE_PLATFORM_XLIB"])
elif env["platform"] == "windows":
env.AppendUnique(CPPDEFINES=["XR_OS_WINDOWS", "NOMINMAX", "XR_USE_PLATFORM_WIN32", "HAVE_SECURE_GETENV"])
else:
# don't build anything, OpenXR not supported on this platform
is_supported_on_platform = False

if is_supported_on_platform:
thirdparty_obj = []
thirdparty_dir = "#thirdparty/openxr"

# Use bundled Vulkan headers
env.Prepend(
CPPPATH=[
thirdparty_dir,
thirdparty_dir + "/include",
thirdparty_dir + "/src",
thirdparty_dir + "/src/common",
thirdparty_dir + "/src/external/jsoncpp/include",
thirdparty_dir + "/src/loader",
]
)

# may need to check and set:
# - XR_USE_TIMESPEC

# need to redo this to something more elegant

env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()

env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/xr_generated_dispatch_table.c")

# add in common files (hope these don't clash with us)
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/common/filesystem_utils.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/common/object_info.cpp")

# add in JSON
env_thirdparty.add_source_files(
thirdparty_obj, thirdparty_dir + "/src/external/jsoncpp/src/lib_json/json_reader.cpp"
)
env_thirdparty.add_source_files(
thirdparty_obj, thirdparty_dir + "/src/external/jsoncpp/src/lib_json/json_value.cpp"
)
env_thirdparty.add_source_files(
thirdparty_obj, thirdparty_dir + "/src/external/jsoncpp/src/lib_json/json_writer.cpp"
)

# add in load
if env["platform"] == "android":
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/android_utilities.cpp")

env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/api_layer_interface.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/loader_core.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/loader_instance.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/loader_logger_recorders.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/loader_logger.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/manifest_file.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/runtime_interface.cpp")
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "/src/loader/xr_generated_loader.cpp")

env.drivers_sources += thirdparty_obj

# Godot source files

driver_obj = []

env.add_source_files(driver_obj, "openxr_device.cpp")

if env["platform"] == "android":
env.add_source_files(driver_obj, "openxr_android_extension.cpp")
if env["vulkan"]:
env.add_source_files(driver_obj, "openxr_vulkan_extension.cpp")
# if env["opengl3"]:
# # might need XR_USE_GRAPHICS_API_OPENGL_ES on Android
# env.add_source_files(driver_obj, "openxr_opengl_extension.cpp")

env.drivers_sources += driver_obj

# Needed to force rebuilding the driver files when the thirdparty code is updated.
env.Depends(driver_obj, thirdparty_obj)

else:
# add in dummy driver so we can compile platforms that do not support OpenXR
driver_obj = []

env.AppendUnique(CPPDEFINES=["OPENXR_DUMMY"])

# env.add_source_files(driver_obj, "openxr_device_dummy.cpp")
# env.add_source_files(driver_obj, "openxr_vulkan_extension_dummy.cpp")
# env.add_source_files(driver_obj, "openxr_opengl_extension_dummy.cpp")

# env.drivers_sources += driver_obj
71 changes: 71 additions & 0 deletions drivers/openxr/openxr_android_extension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*************************************************************************/
/* openxr_android_extension.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "openxr_android_extension.h"

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

OpenXRAndroidExtension *OpenXRAndroidExtension::singleton = nullptr;

OpenXRAndroidExtension *OpenXRAndroidExtension::get_singleton() {
return singleton;
}

OpenXRAndroidExtension::OpenXRAndroidExtension(OpenXRDevice *p_openxr_device) :
OpenXRExtensionWrapper(p_openxr_device) {
singleton = this;

request_extensions[XR_KHR_ANDROID_THREAD_SETTINGS_EXTENSION_NAME] = nullptr; // must be available

// Initialize the loader
PFN_xrInitializeLoaderKHR xrInitializeLoaderKHR;
result = xrGetInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR", (PFN_xrVoidFunction *)(&xrInitializeLoaderKHR));
ERR_FAIL_COND_MSG(XR_FAILED(result), "Failed to retrieve pointer to xrInitializeLoaderKHR");

// TODO fix this code, this is still code from GDNative!
JNIEnv *env = android_api->godot_android_get_env();
JavaVM *vm;
env->GetJavaVM(&vm);
jobject activity_object = env->NewGlobalRef(android_api->godot_android_get_activity());

XrLoaderInitInfoAndroidKHR loader_init_info_android = {
.type = XR_TYPE_LOADER_INIT_INFO_ANDROID_KHR,
.next = nullptr,
.applicationVM = vm,
.applicationContext = activity_object
};
xrInitializeLoaderKHR((const XrLoaderInitInfoBaseHeaderKHR *)&loader_init_info_android);
ERR_FAIL_COND_MSG(XR_FAILED(result), "Failed to call xrInitializeLoaderKHR");
}

OpenXRAndroidExtension::~OpenXRAndroidExtension() {
singleton = nullptr;
}
47 changes: 47 additions & 0 deletions drivers/openxr/openxr_android_extension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*************************************************************************/
/* openxr_android_extension.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef OPENXR_ANDROID_EXTENSION_H
#define OPENXR_ANDROID_EXTENSION_H

#include "openxr_extension_wrapper.h"

class OpenXRAndroidExtension : public OpenXRExtensionWrapper {
public:
static OpenXRAndroidExtension *get_singleton();

OpenXRAndroidExtension(OpenXRDevice *p_openxr_device);
virtual ~OpenXRAndroidExtension() override;

private:
static OpenXRAndroidExtension *singleton;
};

#endif // !OPENXR_ANDROID_EXTENSION_H
45 changes: 45 additions & 0 deletions drivers/openxr/openxr_composition_layer_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*************************************************************************/
/* openxr_composition_layer_provider.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef OPENXR_COMPOSITION_LAYER_PROVIDER_H
#define OPENXR_COMPOSITION_LAYER_PROVIDER_H

#include <openxr/openxr.h>

// Interface for OpenXR extensions that provide a composition layer.
class OpenXRCompositionLayerProvider {
public:
// TODO changed to normal method definition for now
// CI complains until we implement this, haven't ported it yet from plugin
// virtual XrCompositionLayerBaseHeader *get_composition_layer() = 0;
XrCompositionLayerBaseHeader *get_composition_layer() { return nullptr; };
};

#endif // OPENXR_COMPOSITION_LAYER_PROVIDER_H
Loading

0 comments on commit 23be0da

Please sign in to comment.