Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Headset.h
Go to the documentation of this file.
1#pragma once
2#include <glm/ext/matrix_float4x4.hpp>
3#include <glm/gtc/quaternion.hpp>
4#include <memory>
5#include <openxr/openxr.h>
6#include <vector>
7#include <vulkan/vulkan_core.h>
8
10
11namespace Engine::Core
12{
14} // namespace Engine::Core
15
16namespace Engine::Rendering
17{
18 class Renderer;
19
24 {
25 VkImage image = nullptr;
26 VkImageView imageView = nullptr;
27 VkFramebuffer framebuffer = nullptr;
28
29 void cleanup( const Core::ApplicationContext * context );
30
31 private:
32 VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
33 };
34
35 class Headset
36 {
37 public:
40
41 void cleanup();
42
44 {
45 Error, // An error occurred
46 RenderFully, // Render this frame normally
47 SkipRender, // Skip rendering the frame but end it
48 SkipFully, // Skip processing this frame entirely without ending it
49 ComputeOnly // Run compute shaders but skip XR/mesh rendering (COMPUTE_DEBUG mode)
50 };
51
56 {
58 bool shouldCallBeginFrame; // Whether beginXrFrameAfterWait should be called
59 bool needsGpuSyncForStateTransition = false; // Deferred GPU sync + Tracy reset needed
60 };
61
66
75
83 BeginFrameResult beginXrFrameAfterWait( uint32_t & swapchainImageIndex );
84
88 BeginFrameResult beginXrFrame( uint32_t & swapchainImageIndex, Renderer * renderer );
89 void endXrFrame();
91
92 void beginXrSession() const;
96 [[nodiscard]] VkImage getSwapchainImage( size_t swapchainImageIndex ) const;
97
98 std::vector<RenderTarget> getSwapchainRenderTargets();
99
103 void endXrSession() const;
104
111
115 [[nodiscard]] uint32_t getEyeCount() const;
116
121 [[nodiscard]] VkExtent2D getEyeResolution( size_t eyeIndex ) const;
122
126 [[nodiscard]] VkRenderPass getRenderPass() const;
127
132 [[nodiscard]] glm::mat4 getEyeViewMatrix( size_t eyeIndex ) const;
133
137 [[nodiscard]] glm::mat4 getRawEyeViewMatrix( size_t eyeIndex ) const;
138
142 void setPlayerPosition( const glm::vec3 & position )
143 {
144 playerPosition_ = position;
145 }
146
150 [[nodiscard]] glm::vec3 getPlayerPosition() const
151 {
152 return playerPosition_;
153 }
154
158 void setPlayerRotation( const glm::quat & rotation );
159
163 [[nodiscard]] glm::quat getPlayerRotation() const
164 {
165 return playerRotation_;
166 }
167
171 [[nodiscard]] glm::vec3 transformPlaySpacePosition( const glm::vec3 & position ) const;
172
176 [[nodiscard]] glm::quat transformPlaySpaceOrientation( const glm::quat & orientation ) const;
177
181 [[nodiscard]] glm::vec3 getHeadsetLocalPosition() const;
182
186 [[nodiscard]] glm::quat getHeadsetLocalOrientation() const;
187
191 [[nodiscard]] glm::vec3 getHeadsetWorldPosition() const;
192
196 [[nodiscard]] glm::quat getHeadsetWorldOrientation() const;
197
202 [[nodiscard]] glm::mat4 getEyeProjectionMatrix( size_t eyeIndex ) const;
203
208 [[nodiscard]] glm::mat4 getViewProjectionMatrix( size_t eyeIndex ) const;
209
213 [[nodiscard]] XrSpace getReferenceSpace() const;
214
218 [[nodiscard]] XrSession getSession() const;
219
223 [[nodiscard]] bool getIsExitRequested() const;
224
228 [[nodiscard]] XrFrameState getXrFrameState() const;
229
230 private:
231 XrViewState xrViewState = {};
232 XrFrameState xrFrameState = {};
233
234 XrSessionState xrSessionState = XR_SESSION_STATE_UNKNOWN;
235
236 bool isExistRequested = false;
237
239
243 VkRenderPass vkRenderPass = VK_NULL_HANDLE;
244
248 XrSession xrSession = XR_NULL_HANDLE;
249
253 XrSpace xrReferenceSpace = XR_NULL_HANDLE;
257 XrReferenceSpaceType xrReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
258
259 uint32_t eyeCount = 0u;
260
261 std::vector<XrViewConfigurationView> eyeImageInfos;
262
263 std::vector<XrView> eyePoses;
264
266 {
267 VkImage image = nullptr;
268 VkDeviceMemory deviceMemory = nullptr;
269 VkImageView imageView = nullptr;
270
272 };
273
277
278 XrSwapchain xrSwapchain = nullptr;
279
280 // Named thread pool for xrWaitFrame timeout handling (visible in Tracy as "XR Wait 1")
281 std::unique_ptr<NamedThreadPool> xrWaitThreadPool_;
282
283 private:
287 std::vector<RenderTarget> swapchainRenderTargets;
288
289 std::vector<XrCompositionLayerProjectionView> eyeRenderInfos;
290
291 struct Eye
292 {
293 glm::mat4 eyeViewMatrix{};
295 };
296
297 std::vector<Eye> eyes = {};
298
299 XrTime lastUpdateTime = 0;
300
301 // Player transform applied to view matrices
302 glm::vec3 playerPosition_{ 0.0f, 0.0f, 0.0f };
303 glm::quat playerRotation_{ 1.0f, 0.0f, 0.0f, 0.0f };
304
305 public:
309 const RenderTarget * getRenderTarget( size_t swapchainImageIndex ) const;
310
314 [[nodiscard]] VkImage getDepthBufferImage() const
315 {
316 return depthBuffer.image;
317 }
318
322 [[nodiscard]] VkImageView getDepthBufferView() const
323 {
324 return depthBuffer.imageView;
325 }
326
330 [[nodiscard]] VkImage getColorBufferImage() const
331 {
332 return colorBuffer.image;
333 }
334
338 [[nodiscard]] VkImageView getColorBufferView() const
339 {
340 return colorBuffer.imageView;
341 }
342
346 [[nodiscard]] VkImage getResolveBufferImage() const
347 {
348 return resolveBuffer.image;
349 }
350
354 [[nodiscard]] VkImageView getResolveBufferView() const
355 {
356 return resolveBuffer.imageView;
357 }
358
362 [[nodiscard]] VkFormat getColorFormat() const
363 {
364 return colorFormat_;
365 }
366
367 [[nodiscard]] VkFormat getSceneColorFormat() const
368 {
369 return sceneColorFormat_;
370 }
371
372 private:
373 VkFormat colorFormat_ = VK_FORMAT_R8G8B8A8_UNORM;
374 VkFormat sceneColorFormat_ = VK_FORMAT_R16G16B16A16_SFLOAT;
375 };
376} // namespace Engine::Rendering
The application context is the core class which stores the basic openxr and vulkan objects.
void requestExitSession()
Requests the XR runtime to exit the session gracefully. This should be called when the application wa...
std::vector< RenderTarget > swapchainRenderTargets
The swapchain render targets. The images get rendered onto those render targets.
Definition Headset.h:287
glm::quat transformPlaySpaceOrientation(const glm::quat &orientation) const
Converts an OpenXR play-space orientation into world space.
std::vector< XrViewConfigurationView > eyeImageInfos
Definition Headset.h:261
XrSpace getReferenceSpace() const
Getter.
std::vector< XrView > eyePoses
Definition Headset.h:263
std::vector< XrCompositionLayerProjectionView > eyeRenderInfos
Definition Headset.h:289
VkImage getDepthBufferImage() const
Getter.
Definition Headset.h:314
glm::quat getHeadsetLocalOrientation() const
Gets the headset's local orientation relative to the play space.
XrFrameState getXrFrameState() const
Getter.
VkImage getColorBufferImage() const
Getter.
Definition Headset.h:330
VkRenderPass getRenderPass() const
Getter.
VkImage getSwapchainImage(size_t swapchainImageIndex) const
Getter.
VkImageView getDepthBufferView() const
Getter.
Definition Headset.h:322
void setPlayerRotation(const glm::quat &rotation)
Setter for the player/play-space rotation applied around the world origin.
BeginFrameResult beginXrFrame(uint32_t &swapchainImageIndex, Renderer *renderer)
Combines waitForXrFrame and beginXrFrameAfterWait.
glm::vec3 getPlayerPosition() const
Getter.
Definition Headset.h:150
XrSpace xrReferenceSpace
The xr reference space.
Definition Headset.h:253
XrReferenceSpaceType xrReferenceSpaceType
Type of the xr reference space, if it is room scale or just sitting vr.
Definition Headset.h:257
XrSessionState xrSessionState
Definition Headset.h:234
VkFormat getColorFormat() const
Getter.
Definition Headset.h:362
void endXrSession() const
Ends the XR session.
uint32_t getEyeCount() const
Getter.
glm::mat4 getEyeProjectionMatrix(size_t eyeIndex) const
Gets the projection matrix for an eye.
glm::vec3 transformPlaySpacePosition(const glm::vec3 &position) const
Converts an OpenXR play-space position into world space.
VkFormat getSceneColorFormat() const
Definition Headset.h:367
VkRenderPass vkRenderPass
The vulkan render pass.
Definition Headset.h:243
VkImage getResolveBufferImage() const
Getter.
Definition Headset.h:346
ImageBuffer resolveBuffer
Definition Headset.h:276
glm::mat4 getRawEyeViewMatrix(size_t eyeIndex) const
Gets the raw eye view matrix without player position offset.
Headset(const Core::ApplicationContext *context=nullptr)
const Core::ApplicationContext * context
Definition Headset.h:238
XrFrameState xrFrameState
Definition Headset.h:232
XrSession getSession() const
Getter.
std::unique_ptr< NamedThreadPool > xrWaitThreadPool_
Definition Headset.h:281
XrSession xrSession
The xr session.
Definition Headset.h:248
std::vector< Eye > eyes
Definition Headset.h:297
void setPlayerPosition(const glm::vec3 &position)
Setter.
Definition Headset.h:142
void updateViewMatrix()
Updates the eye matrix for the headset.
WaitFrameResult waitForXrFrame(Rendering::Renderer *renderer)
First part of frame setup: calls xrWaitFrame. Can be called before previous xrEndFrame completes....
glm::vec3 getHeadsetLocalPosition() const
Gets the headset's local position relative to the play space origin.
BeginFrameResult beginXrFrameAfterWait(uint32_t &swapchainImageIndex)
Second part of frame setup: calls xrBeginFrame, acquires swapchain image. MUST be called after previo...
std::vector< RenderTarget > getSwapchainRenderTargets()
glm::quat getHeadsetWorldOrientation() const
Gets the headset orientation after player rotation is applied.
bool getIsExitRequested() const
Returns true if the headset has requested the application to exit.
glm::mat4 getEyeViewMatrix(size_t eyeIndex) const
Returns the view matrix of the specified eye.
glm::mat4 getViewProjectionMatrix(size_t eyeIndex) const
Gets the multiplied view and projection matrix.
glm::quat getPlayerRotation() const
Getter for the player/play-space rotation.
Definition Headset.h:163
glm::vec3 getHeadsetWorldPosition() const
Gets the headset position after player position/rotation are applied.
VkExtent2D getEyeResolution(size_t eyeIndex) const
Gets eye resolution in x and y for a specified eye index. 0 is left.
const RenderTarget * getRenderTarget(size_t swapchainImageIndex) const
Getter.
VkImageView getColorBufferView() const
Getter.
Definition Headset.h:338
VkImageView getResolveBufferView() const
Getter.
Definition Headset.h:354
The renderer is the main class for rendering. It owns all data which is used any time in any frame....
Definition Renderer.h:71
Tracy-named thread pool using BS_tracy::tracy_thread_pool.
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9
void cleanup(const Core::ApplicationContext *context)
Result from waitForXrFrame indicating what the main loop should do.
Definition Headset.h:56
A render target which contains all resources to access the rendered image.
Definition Headset.h:24
void cleanup(const Core::ApplicationContext *context)