Skip to content

Commit

Permalink
Fixing include paths, update logging logic, boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
jherico committed Sep 19, 2019
1 parent d406d55 commit 4efd719
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 54 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ set(NAME OpenXRExamples)
include(${CMAKE_SOURCE_DIR}/cmake/ezvcpkg/ezvcpkg.cmake)

ezvcpkg_fetch(
COMMIT 1e7303d4c52468fbb24a0d3418ff033d2585d18d
REPO jherico/vcpkg
COMMIT cfb3b69551964c583c4917509ea37c76efc49a4d
PACKAGES assimp basisu fmt imgui glad glfw3 gli glm openxr-loader vulkan
UPDATE_TOOLCHAIN
USE_HOST_VCPKG
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* `cd build`
* Configure using cmake (force 64-bit architecture)
* `cmake .. -A x64`
* This step can take a while the first time, since it will automatically download and build all the dependencies
* Build the example
* `cmake --build . --config Debug --target gl_single_file_example`

Expand Down
3 changes: 1 addition & 2 deletions cmake/macros/TargetOpenXR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ macro(TARGET_OPENXR)
find_library(OPENXR_LIBRARY_DEBUG openxr_loader PATHS ${EZVCPKG_DIR}/debug/lib NO_DEFAULT_PATH)
include(SelectLibraryConfigurations)
select_library_configurations(OPENXR)
target_link_libraries(${TARGET_NAME} PRIVATE ${OPENXR_LIBRARIES})
target_include_directories(${TARGET_NAME} PRIVATE "C:\\Users\\bdavi\\git\\OpenXR-SDK-Source\\include")
target_link_libraries(${TARGET_NAME} PUBLIC ${OPENXR_LIBRARIES})
endmacro()


6 changes: 6 additions & 0 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//
// Created by Bradley Austin Davis
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "common.hpp"

#if defined(__ANDROID__)
Expand Down
6 changes: 6 additions & 0 deletions src/common/common.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//
// Created by Bradley Austin Davis
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once

#include <cassert>
Expand Down
6 changes: 6 additions & 0 deletions src/common/gl.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//
// Created by Bradley Austin Davis
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once

#if !defined(__ANDROID__)
Expand Down
9 changes: 7 additions & 2 deletions src/common/glfw.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//
// Created by Bradley Austin Davis
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once

#include <functional>
Expand All @@ -19,8 +25,7 @@ namespace glfw {
using NativeContext = HGLRC;
using NativeWindow = HWND;
} // namespace glfw

#elif defined()
#else
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_EXPOSE_NATIVE_GLX
namespace glfw {
Expand Down
61 changes: 53 additions & 8 deletions src/common/logging.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,70 @@
//
// Created by Bradley Austin Davis on 2019/09/18
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once

#include <string>
#include <chrono>
#include <ctime>
#include <fmt/format.h>
#include <openxr/openxr.h>

namespace logging {
using Time = std::chrono::time_point<std::chrono::system_clock>;
}

template <>
struct fmt::formatter<logging::Time> {
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx) {
return ctx.begin();
}

template <typename FormatContext>
auto format(const logging::Time& t, FormatContext& ctx) {
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(t);
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now_ms.time_since_epoch()).count() % 1000;
time_t tt = std::chrono::system_clock::to_time_t(t);
tm local_tm = *localtime(&tt);
return format_to(ctx.out(), "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}", local_tm.tm_year + 1900,
local_tm.tm_mon + 1, local_tm.tm_mday, local_tm.tm_hour, local_tm.tm_min, local_tm.tm_sec,
duration_ms);
}
};

enum class Level : uint64_t
namespace logging {

// Values picked to match the OpenXR XrDebugUtilsMessageSeverityFlagBitsEXT values
enum class Level : uint32_t
{
Debug = XR_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
Info = XR_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,
Warning = XR_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,
Error = XR_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
Debug = 0x00000001,
Info = 0x00000010,
Warning = 0x00000100,
Error = 0x00001000,
};

inline std::string to_string(Level level) {
uint32_t levelRaw = reinterpret_cast<uint32_t&>(level);
if (0x00001000 == (levelRaw & 0x00001000)) {
return "ERROR";
} else if (0x00000100 == (levelRaw & 0x00000100)) {
return "WARNING";
} else if (0x00000010 == (levelRaw & 0x00000010)) {
return "INFO";
}
return "DEBUG";
}

inline void log(Level level, const std::string& message) {
//auto output = fmt::format("{} {}: {}", std::chrono::system_clock::now(), to_string(level), message);
auto output = fmt::format("{} {}: {}", std::chrono::system_clock::now(), to_string(level), message);
#ifdef WIN32
OutputDebugStringA(message.c_str());
OutputDebugStringA(output.c_str());
OutputDebugStringA("\n");
#endif
std::cout << message << std::endl;
std::cout << output << std::endl;
}

} // namespace logging
Expand Down
84 changes: 43 additions & 41 deletions src/examples/gl_single_file_example.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//
// Created by Bradley Austin Davis on 2019/09/18
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

#define XR_USE_GRAPHICS_API_OPENGL
#define SUPPRESS_DEBUG_UTILS
#define _CRT_SECURE_NO_WARNINGS
Expand Down Expand Up @@ -37,38 +44,33 @@

namespace xrs {

namespace debug {
namespace DebugUtilsEXT {

using SevBits = xr::DebugUtilsMessageSeverityFlagBitsEXT;
using TypeBits = xr::DebugUtilsMessageTypeFlagBitsEXT;
using SevFlags = xr::DebugUtilsMessageSeverityFlagsEXT;
using TypeFlags = xr::DebugUtilsMessageTypeFlagsEXT;
using MessageSeverityFlagBits = xr::DebugUtilsMessageSeverityFlagBitsEXT;
using MessageTypeFlagBits = xr::DebugUtilsMessageTypeFlagBitsEXT;
using MessageSeverityFlags = xr::DebugUtilsMessageSeverityFlagsEXT;
using MessageTypeFlags = xr::DebugUtilsMessageTypeFlagsEXT;
using CallbackData = xr::DebugUtilsMessengerCallbackDataEXT;
using Messenger = xr::DebugUtilsMessengerEXT;

static Messenger messenger;

// Raw C callback
static XrBool32 debugCallback(XrDebugUtilsMessageSeverityFlagsEXT sev_,
XrDebugUtilsMessageTypeFlagsEXT type_,
const XrDebugUtilsMessengerCallbackDataEXT* data_,
void* userData) {
LOG_FORMATTED((logging::Level)sev_, "{}: message", data_->functionName, data_->message);
return XR_TRUE;
}

void startup(const xr::Instance& instance,
const SevFlags& severityFlags = SevBits::AllBits,
const TypeFlags& typeFlags = TypeBits::AllBits,
void* userData = nullptr) {
messenger = instance.createDebugUtilsMessengerEXT({ severityFlags, typeFlags, debugCallback, userData },
xr::DispatchLoaderDynamic{ instance });
}

// Clear debug callback
inline void shutdown(const xr::Instance& instance) {
messenger.destroy(xr::DispatchLoaderDynamic{ instance });
Messenger create(const xr::Instance& instance,
const MessageSeverityFlags& severityFlags = MessageSeverityFlagBits::AllBits,
const MessageTypeFlags& typeFlags = MessageTypeFlagBits::AllBits,
void* userData = nullptr) {
return instance.createDebugUtilsMessengerEXT({ severityFlags, typeFlags, debugCallback, userData },
xr::DispatchLoaderDynamic{ instance });
}

} // namespace debug
} // namespace DebugUtilsEXT

inline XrFovf toTanFovf(const XrFovf& fov) {
return { tanf(fov.angleLeft), tanf(fov.angleRight), tanf(fov.angleUp), tanf(fov.angleDown) };
Expand Down Expand Up @@ -127,7 +129,6 @@ inline glm::mat4 toGlm(const XrPosef& p) {

} // namespace xrs


struct FrameCounter {
using time_point = std::chrono::time_point<std::chrono::steady_clock>;
int64_t counter{ 0 };
Expand Down Expand Up @@ -204,6 +205,7 @@ struct OpenXrExample {
xr::SystemId systemId;
xr::DispatchLoaderDynamic dispatch;
glm::uvec2 renderTargetSize;
xrs::DebugUtilsEXT::Messenger messenger;
xr::GraphicsRequirementsOpenGLKHR graphicsRequirements;
void prepareXrInstance() {
std::unordered_map<std::string, xr::ExtensionProperties> discoveredExtensions;
Expand Down Expand Up @@ -243,7 +245,7 @@ struct OpenXrExample {
dumci.messageSeverities = xr::DebugUtilsMessageSeverityFlagBitsEXT::AllBits;
dumci.messageTypes = xr::DebugUtilsMessageTypeFlagBitsEXT::AllBits;
dumci.userData = this;
dumci.userCallback = &xrs::debug::debugCallback;
dumci.userCallback = &xrs::DebugUtilsEXT::debugCallback;
ici.next = &dumci;
}

Expand All @@ -252,7 +254,7 @@ struct OpenXrExample {

// Turn on debug logging
if (enableDebug) {
xrs::debug::startup(instance);
messenger = xrs::DebugUtilsEXT::create(instance);
}
}

Expand All @@ -264,10 +266,8 @@ struct OpenXrExample {
// Now we want to fetch the instance properties
xr::InstanceProperties instanceProperties = instance.getInstanceProperties();
LOG_INFO("OpenXR Runtime {} version {}.{}.{}", //
(const char*)instanceProperties.runtimeName,
(uint32_t)instanceProperties.runtimeVersion.major,
(uint32_t)instanceProperties.runtimeVersion.minor,
(uint32_t)instanceProperties.runtimeVersion.patch);
(const char*)instanceProperties.runtimeName, (uint32_t)instanceProperties.runtimeVersion.major,
(uint32_t)instanceProperties.runtimeVersion.minor, (uint32_t)instanceProperties.runtimeVersion.patch);
}

// We want to create an HMD example, so we ask for a runtime that supposts that form factor
Expand All @@ -278,10 +278,9 @@ struct OpenXrExample {
{
xr::SystemProperties systemProperties = instance.getSystemProperties(systemId);
LOG_INFO("OpenXR System {} max layers {} max swapchain image size {}x{}", //
(const char*)systemProperties.systemName,
(uint32_t)systemProperties.graphicsProperties.maxLayerCount,
(uint32_t)systemProperties.graphicsProperties.maxSwapchainImageWidth,
(uint32_t)systemProperties.graphicsProperties.maxSwapchainImageHeight);
(const char*)systemProperties.systemName, (uint32_t)systemProperties.graphicsProperties.maxLayerCount,
(uint32_t)systemProperties.graphicsProperties.maxSwapchainImageWidth,
(uint32_t)systemProperties.graphicsProperties.maxSwapchainImageHeight);
}

// Find out what view configurations we have available
Expand All @@ -303,17 +302,17 @@ struct OpenXrExample {
// Even preferable would be to create a swapchain texture array with one layer per eye, so that we could use the
// VK_KHR_multiview to render both eyes with a single set of draws, but sadly the Oculus runtime doesn't currently
// support texture array swapchains
if (viewConfigViews.size() != 2) {
throw std::runtime_error("Unexpected number of view configurations");
}
if (viewConfigViews.size() != 2) {
throw std::runtime_error("Unexpected number of view configurations");
}

if (viewConfigViews[0].recommendedImageRectHeight != viewConfigViews[1].recommendedImageRectHeight) {
throw std::runtime_error("Per-eye images have different recommended heights");
}
if (viewConfigViews[0].recommendedImageRectHeight != viewConfigViews[1].recommendedImageRectHeight) {
throw std::runtime_error("Per-eye images have different recommended heights");
}

renderTargetSize = { viewConfigViews[0].recommendedImageRectWidth * 2, viewConfigViews[0].recommendedImageRectHeight };
graphicsRequirements = instance.getOpenGLGraphicsRequirementsKHR(systemId, dispatch);

graphicsRequirements = instance.getOpenGLGraphicsRequirementsKHR(systemId, dispatch);
}

glfw::Window window;
Expand All @@ -332,9 +331,9 @@ struct OpenXrExample {
window.makeCurrent();
window.setSwapInterval(0);

// Initialize GLAD
// Initialize GLAD
gl::init();
gl::report();
gl::report();
// Make sure we get GL errors reported
gl::setupDebugLogging();
}
Expand Down Expand Up @@ -543,7 +542,7 @@ struct OpenXrExample {
glFramebufferTexture(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 0, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

swapchain.releaseSwapchainImage(xr::SwapchainImageReleaseInfo{});
swapchain.releaseSwapchainImage(xr::SwapchainImageReleaseInfo{});

window.swapBuffers();
}
Expand All @@ -570,6 +569,9 @@ struct OpenXrExample {
session.destroy();
session = nullptr;
}
if (messenger) {
messenger.destroy(dispatch);
}
if (instance) {
instance.destroy();
instance = nullptr;
Expand Down

0 comments on commit 4efd719

Please sign in to comment.