HoviTron Video Pipeline
openXRHelp.h
Go to the documentation of this file.
1/* ----------------------
2* Copyright 2023 Université Libre de Bruxelles(ULB), Universidad Politécnica de Madrid(UPM), CREAL, Deutsches Zentrum für Luft - und Raumfahrt(DLR)
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at < http://www.apache.org/licenses/LICENSE-2.0>
7
8* Unless required by applicable law or agreed to in writing, software
9* distributed under the License is distributed on an "AS IS" BASIS,
10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11* See the License for the specific language governing permissionsand
12* limitations under the License.
13---------------------- */
14
15
16
17/*****************************************************************/
26#pragma once
27
29#ifndef __ANDROID__
30#include <cstdarg>
31#endif
32
33#define ENUM_CASE_STR(name, val) case name: return #name;
34#define MAKE_TO_STRING_FUNC(enumType) \
35 inline const char* to_string(enumType e) { \
36 switch (e) { \
37 XR_LIST_ENUM_##enumType(ENUM_CASE_STR) \
38 default: return "Unknown " #enumType; \
39 } \
40 }
41// clang-format on
42
43MAKE_TO_STRING_FUNC(XrReferenceSpaceType);
44MAKE_TO_STRING_FUNC(XrViewConfigurationType);
45MAKE_TO_STRING_FUNC(XrEnvironmentBlendMode);
46MAKE_TO_STRING_FUNC(XrSessionState);
47MAKE_TO_STRING_FUNC(XrResult);
48MAKE_TO_STRING_FUNC(XrFormFactor);
49
50inline std::string Fmt(const char* fmt, ...) {
51 va_list vl;
52 va_start(vl, fmt);
53 int size = std::vsnprintf(nullptr, 0, fmt, vl);
54 va_end(vl);
55
56 if (size != -1) {
57 std::unique_ptr<char[]> buffer(new char[size + 1]);
58
59 va_start(vl, fmt);
60 size = std::vsnprintf(buffer.get(), size + 1, fmt, vl);
61 va_end(vl);
62 if (size != -1) {
63 return std::string(buffer.get(), size);
64 }
65 }
66
67 throw std::runtime_error("Unexpected vsnprintf failure");
68}
69
70//code from check.h (openXr sample)
71#define CHK_STRINGIFY(x) #x
72#define TOSTRING(x) CHK_STRINGIFY(x)
73#define FILE_AND_LINE __FILE__ ":" TOSTRING(__LINE__)
74
75[[noreturn]] inline void Throw(std::string failureMessage, const char* originator = nullptr, const char* sourceLocation = nullptr) {
76 if (originator != nullptr) {
77 failureMessage += Fmt("\n Origin: %s", originator);
78 }
79 if (sourceLocation != nullptr) {
80 failureMessage += Fmt("\n Source: %s", sourceLocation);
81 }
82
83 throw std::logic_error(failureMessage);
84}
85
86#define THROW(msg) Throw(msg, nullptr, FILE_AND_LINE);
87#define CHECK(exp) \
88 { \
89 if (!(exp)) { \
90 Throw("Check failed", #exp, FILE_AND_LINE); \
91 } \
92 }
93#define CHECK_MSG(exp, msg) \
94 { \
95 if (!(exp)) { \
96 Throw(msg, #exp, FILE_AND_LINE); \
97 } \
98 }
99
100[[noreturn]] inline void ThrowXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
101 Throw(Fmt("XrResult failure [%s]", to_string(res)), originator, sourceLocation);
102}
103
104inline XrResult CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
105 if (XR_FAILED(res)) {
106 ThrowXrResult(res, originator, sourceLocation);
107 }
108
109 return res;
110}
111
112#define THROW_XR(xr, cmd) ThrowXrResult(xr, #cmd, FILE_AND_LINE);
113#define CHECK_XRCMD(cmd) CheckXrResult(cmd, #cmd, FILE_AND_LINE);
114#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
115
116inline std::vector<const char*> ParseExtensionString(char* names) {
117 std::vector<const char*> list;
118 while (*names != 0) {
119 list.push_back(names);
120 while (*(++names) != 0) {
121 if (*names == ' ') {
122 *names++ = '\0';
123 break;
124 }
125 }
126 }
127 return list;
128}
129
130
file that contains the common include for the Vulkan part