• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Razakhel / RaZ / 18062694977

27 Sep 2025 04:20PM UTC coverage: 74.093% (+0.04%) from 74.05%
18062694977

push

github

Razakhel
[Utils/Logger] Added formatted logging overloads

- Formatted calls to logging functions and made use of std::format() in several other places

100 of 170 new or added lines in 36 files covered. (58.82%)

4 existing lines in 2 files now uncovered.

8334 of 11248 relevant lines covered (74.09%)

1757.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/RaZ/XR/XrSystem.cpp
1
#include "RaZ/Utils/Logger.hpp"
2
#include "RaZ/XR/XrSystem.hpp"
3

4
#include "openxr/openxr.h"
5

6
#include <array>
7

8
namespace Raz {
9

10
namespace {
11

12
const char* getResultStr(XrInstance instance, XrResult result) {
×
13
  static std::array<char, XR_MAX_RESULT_STRING_SIZE> errorStr {};
14
  xrResultToString(instance, result, errorStr.data());
×
15
  return errorStr.data();
×
16
}
17

18
std::string getErrorStr(const std::string& errorMsg, XrResult result, XrInstance instance) {
×
19
  return "[XrSystem] " + errorMsg + ": " + getResultStr(instance, result) + " (" + std::to_string(result) + ')';
×
20
}
21

22
void checkLog(XrResult result, const std::string& errorMsg, XrInstance instance) {
×
23
  if (XR_SUCCEEDED(result))
×
24
    return;
×
25

26
  Logger::error(getErrorStr(errorMsg, result, instance));
×
27
}
28

29
bool pollNextEvent(XrInstance instance, XrEventDataBuffer& eventData) {
×
30
  eventData      = {};
×
31
  eventData.type = XR_TYPE_EVENT_DATA_BUFFER;
×
32

33
  return (xrPollEvent(instance, &eventData) == XR_SUCCESS);
×
34
}
35

36
void processEventData(const XrEventDataEventsLost& eventsLost) {
×
NEW
37
  Logger::info("[XrSystem] {} events lost", eventsLost.lostEventCount);
×
38
}
×
39

40
void processEventData(const XrEventDataInstanceLossPending& instanceLossPending) {
×
41
  // After the period of time specified by lossTime, the application can try recreating an instance again
42
  // See: https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrEventDataInstanceLossPending.html#_description
NEW
43
  Logger::info("[XrSystem] Instance loss pending at: {}", instanceLossPending.lossTime);
×
44
}
×
45

46
void processEventData(const XrEventDataInteractionProfileChanged& interactionProfileChanged, ::XrSession session) {
×
NEW
47
  Logger::info("[XrSystem] Interaction profile changed for {} session", (interactionProfileChanged.session != session ? "unknown" : "current"));
×
48
}
×
49

50
void processEventData(const XrEventDataReferenceSpaceChangePending& referenceSpaceChangePending, ::XrSession session) {
×
NEW
51
  Logger::info("[XrSystem] Reference space changed pending for {} session", (referenceSpaceChangePending.session != session ? "unknown" : "current"));
×
52
}
×
53

54
} // namespace
55

56
XrSystem::XrSystem(const std::string& appName) : m_context(appName), m_session(m_context) {
×
57
  recoverViewConfigurations();
×
58

59
  m_optimalViewWidth  = m_viewConfigViews.front().recommendedImageRectWidth;
×
60
  m_optimalViewHeight = m_viewConfigViews.front().recommendedImageRectHeight;
×
61

62
  if (m_viewConfigViews.size() > 1) {
×
63
    for (const XrViewConfigurationView& viewConfigView : m_viewConfigViews) {
×
64
      if (viewConfigView.recommendedImageRectWidth != m_optimalViewWidth || viewConfigView.recommendedImageRectHeight != m_optimalViewHeight)
×
65
        Logger::warn("[XrSystem] The optimal configuration view size is not the same for all views; rendering may be altered");
×
66
    }
67
  }
68

69
  recoverEnvironmentBlendModes();
×
70
}
×
71

72
bool XrSystem::update(const FrameTimeInfo&) {
×
73
  XrEventDataBuffer eventData {};
×
74

75
  while (pollNextEvent(m_context.m_instance, eventData)) {
×
76
    switch (eventData.type) {
×
77
      case XR_TYPE_EVENT_DATA_EVENTS_LOST:
×
78
        processEventData(*reinterpret_cast<XrEventDataEventsLost*>(&eventData));
×
79
        break;
×
80

81
      case XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING:
×
82
        processEventData(*reinterpret_cast<XrEventDataInstanceLossPending*>(&eventData));
×
83
        m_session.m_isRunning = false;
×
84
        return false;
×
85

86
      case XR_TYPE_EVENT_DATA_INTERACTION_PROFILE_CHANGED:
×
87
        processEventData(*reinterpret_cast<XrEventDataInteractionProfileChanged*>(&eventData), m_session.m_handle);
×
88
        break;
×
89

90
      case XR_TYPE_EVENT_DATA_REFERENCE_SPACE_CHANGE_PENDING:
×
91
        processEventData(*reinterpret_cast<XrEventDataReferenceSpaceChangePending*>(&eventData), m_session.m_handle);
×
92
        break;
×
93

94
      case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED:
×
95
        processSessionStateChanged(*reinterpret_cast<XrEventDataSessionStateChanged*>(&eventData));
×
96
        break;
×
97

98
      default:
×
99
        break;
×
100
    }
101
  }
102

103
  return true;
×
104
}
105

106
XrSystem::~XrSystem() = default;
×
107

108
void XrSystem::recoverViewConfigurations() {
×
109
  uint32_t viewConfigCount {};
×
110
  checkLog(xrEnumerateViewConfigurations(m_context.m_instance, m_context.m_systemId, 0, &viewConfigCount, nullptr),
×
111
           "Failed to get view configuration count",
112
           m_context.m_instance);
113
  m_viewConfigTypes.resize(viewConfigCount);
×
114
  checkLog(xrEnumerateViewConfigurations(m_context.m_instance, m_context.m_systemId, viewConfigCount, &viewConfigCount,
×
115
                                         reinterpret_cast<XrViewConfigurationType*>(m_viewConfigTypes.data())),
×
116
           "Failed to enumerate view configurations",
117
           m_context.m_instance);
118

119
  for (const XrViewConfigurationType viewConfigType : { XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_MONO }) {
×
NEW
120
    if (std::ranges::find(m_viewConfigTypes, viewConfigType) == m_viewConfigTypes.cend())
×
121
      continue;
×
122

123
    m_viewConfigType = viewConfigType;
×
124
    break;
×
125
  }
126

127
  if (m_viewConfigType == 0) {
×
128
    Logger::warn("[XrSystem] Failed to find a view configuration type; defaulting to XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO");
×
129
    m_viewConfigType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
×
130
  }
131

132
  uint32_t viewConfigViewCount {};
×
133
  checkLog(xrEnumerateViewConfigurationViews(m_context.m_instance, m_context.m_systemId, static_cast<XrViewConfigurationType>(m_viewConfigType),
×
134
                                             0, &viewConfigViewCount, nullptr),
135
           "Failed to get view configuration view count",
136
           m_context.m_instance);
137
  m_viewConfigViews.resize(viewConfigViewCount, { XR_TYPE_VIEW_CONFIGURATION_VIEW });
×
138
  checkLog(xrEnumerateViewConfigurationViews(m_context.m_instance, m_context.m_systemId, static_cast<XrViewConfigurationType>(m_viewConfigType),
×
139
                                             viewConfigViewCount, &viewConfigViewCount,
140
                                             m_viewConfigViews.data()),
141
           "Failed to enumerate view configuration views",
142
           m_context.m_instance);
143

144
#if !defined(NDEBUG) || defined(RAZ_FORCE_DEBUG_LOG)
145
  {
146
    std::string viewConfigViewsMsg = "[XrSystem] View configuration views:";
×
147
    for (const XrViewConfigurationView& viewConfigView : m_viewConfigViews) {
×
NEW
148
      viewConfigViewsMsg += std::format(
×
149
        "\n    View:"
150
        "\n        Recom. image rect width:       {}"
151
        "\n        Max. image rect width:         {}"
152
        "\n        Recom. image rect height:      {}"
153
        "\n        Max. image rect height:        {}"
154
        "\n        Recom. swapchain sample count: {}"
155
        "\n        Max. swapchain sample count:   {}",
NEW
156
        viewConfigView.recommendedImageRectWidth, viewConfigView.maxImageRectWidth, viewConfigView.recommendedImageRectHeight,
×
NEW
157
        viewConfigView.maxImageRectHeight, viewConfigView.recommendedSwapchainSampleCount, viewConfigView.maxSwapchainSampleCount);
×
158
    }
159
    Logger::debug(viewConfigViewsMsg);
×
160
  }
×
161
#endif
162
}
×
163

164
void XrSystem::recoverEnvironmentBlendModes() {
×
165
  uint32_t environmentBlendModeCount {};
×
166
  checkLog(xrEnumerateEnvironmentBlendModes(m_context.m_instance, m_context.m_systemId, static_cast<XrViewConfigurationType>(m_viewConfigType),
×
167
                                            0, &environmentBlendModeCount, nullptr),
168
           "Failed to get environment blend mode count",
169
           m_context.m_instance);
170
  m_environmentBlendModes.resize(environmentBlendModeCount);
×
171
  checkLog(xrEnumerateEnvironmentBlendModes(m_context.m_instance, m_context.m_systemId, static_cast<XrViewConfigurationType>(m_viewConfigType),
×
172
                                            environmentBlendModeCount, &environmentBlendModeCount,
173
                                            reinterpret_cast<XrEnvironmentBlendMode*>(m_environmentBlendModes.data())),
×
174
           "Failed to enumerate environment blend modes",
175
           m_context.m_instance);
176

177
  for (const XrEnvironmentBlendMode environmentBlendMode : { XR_ENVIRONMENT_BLEND_MODE_OPAQUE, XR_ENVIRONMENT_BLEND_MODE_ADDITIVE }) {
×
NEW
178
    if (std::ranges::find(m_environmentBlendModes, environmentBlendMode) == m_environmentBlendModes.cend())
×
179
      continue;
×
180

181
    m_environmentBlendMode = environmentBlendMode;
×
182
    break;
×
183
  }
184

185
  if (m_environmentBlendMode == 0) {
×
186
    Logger::warn("Failed to find a compatible blend mode; defaulting to XR_ENVIRONMENT_BLEND_MODE_OPAQUE");
×
187
    m_environmentBlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
×
188
  }
189
}
×
190

191
void XrSystem::initializeSession() {
×
192
  m_session.initialize(m_context.m_systemId);
×
193
  m_session.createSwapchains(m_viewConfigViews);
×
194
}
×
195

196
bool XrSystem::renderFrame(const ViewRenderFunc& viewRenderFunc) const {
×
197
  return m_session.renderFrame(m_viewConfigViews, m_viewConfigType, m_environmentBlendMode, viewRenderFunc);
×
198
}
199

200
bool XrSystem::processSessionStateChanged(const XrEventDataSessionStateChanged& sessionStateChanged) {
×
201
  if (sessionStateChanged.session != m_session.m_handle) {
×
202
    Logger::info("[XrSystem] Data session state changed for unknown session");
×
203
    return true;
×
204
  }
205

206
  switch (sessionStateChanged.state) {
×
207
    case XR_SESSION_STATE_READY:
×
208
      m_session.begin(m_viewConfigType);
×
209
      m_session.m_isRunning = true;
×
210
      break;
×
211

212
    case XR_SESSION_STATE_STOPPING:
×
213
      m_session.end();
×
214
      m_session.m_isRunning = false;
×
215
      break;
×
216

217
    case XR_SESSION_STATE_LOSS_PENDING:
×
218
      // TODO: "It's possible to try to reestablish an XrInstance and XrSession"
219
      m_session.m_isRunning = false;
×
220
      return false;
×
221

222
    case XR_SESSION_STATE_EXITING:
×
223
      m_session.m_isRunning = false;
×
224
      return false;
×
225

226
    default:
×
227
      break;
×
228
  }
229

230
  m_session.m_state = sessionStateChanged.state;
×
231

232
  return true;
×
233
}
234

235
} // namespace Raz
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc