Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Headset.h
1#pragma once
2#include <functional>
3#include <glm/ext/matrix_float4x4.hpp>
4#include <glm/gtc/quaternion.hpp>
5#include <memory>
6#include <openxr/openxr.h>
7#include <vector>
8#include <vulkan/vulkan_core.h>
9
14
15class NamedThreadPool;
16
17namespace Engine::Core
18{
20} // namespace Engine::Core
21
22namespace Engine::Rendering
23{
24 class Renderer;
25
33 {
34 VkImage image = nullptr;
35 VkImageView imageView = nullptr;
36 VkFramebuffer framebuffer = nullptr;
37
42 void cleanup( const Core::ApplicationContext * context );
43
44 private:
45 VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
46 };
47
54 class Headset
55 {
56 public:
62
67
71 void cleanup();
72
84
94
99
107 WaitFrameResult waitForXrFrame( Rendering::Renderer * renderer );
108
116 BeginFrameResult beginXrFrameAfterWait( uint32_t & swapchainImageIndex );
117
124 BeginFrameResult beginXrFrame( uint32_t & swapchainImageIndex, Renderer * renderer );
125
130
135
139 void beginXrSession() const;
140
145 void setSessionStateCallback( std::function<void( XrSessionState )> callback );
146
151 void setInteractionProfileChangedCallback( std::function<void()> callback );
152
158 [[nodiscard]] VkImage getSwapchainImage( size_t swapchainImageIndex ) const;
159
164 std::vector<RenderTarget> getSwapchainRenderTargets();
165
169 void endXrSession() const;
170
177
182 [[nodiscard]] uint32_t getEyeCount() const;
183
189 [[nodiscard]] VkExtent2D getEyeResolution( size_t eyeIndex ) const;
190
195 [[nodiscard]] VkRenderPass getRenderPass() const;
196
202 [[nodiscard]] glm::mat4 getEyeViewMatrix( size_t eyeIndex ) const;
203
209 [[nodiscard]] glm::mat4 getRawEyeViewMatrix( size_t eyeIndex ) const;
210
215 void setPlayerPosition( const glm::vec3 & position )
216 {
217 playerPosition_ = position;
218 }
219
224 [[nodiscard]] glm::vec3 getPlayerPosition() const
225 {
226 return playerPosition_;
227 }
228
233 void setPlayerRotation( const glm::quat & rotation );
234
239 [[nodiscard]] glm::quat getPlayerRotation() const
240 {
241 return playerRotation_;
242 }
243
249 [[nodiscard]] glm::vec3 transformPlaySpacePosition( const glm::vec3 & position ) const;
250
256 [[nodiscard]] glm::quat transformPlaySpaceOrientation( const glm::quat & orientation ) const;
257
262 [[nodiscard]] glm::vec3 getHeadsetLocalPosition() const;
263
268 [[nodiscard]] glm::quat getHeadsetLocalOrientation() const;
269
274 [[nodiscard]] glm::vec3 getHeadsetWorldPosition() const;
275
280 [[nodiscard]] glm::quat getHeadsetWorldOrientation() const;
281
287 [[nodiscard]] glm::mat4 getEyeProjectionMatrix( size_t eyeIndex ) const;
288
294 [[nodiscard]] glm::mat4 getViewProjectionMatrix( size_t eyeIndex ) const;
295
300 [[nodiscard]] XrSpace getReferenceSpace() const;
301
306 [[nodiscard]] XrSession getSession() const;
307
312 [[nodiscard]] bool getIsExitRequested() const;
313
318 [[nodiscard]] XrFrameState getXrFrameState() const;
319
320 private:
321 XrViewState xrViewState = {};
322 XrFrameState xrFrameState = {};
323
324 XrSessionState xrSessionState = XR_SESSION_STATE_UNKNOWN;
325
326 bool isExistRequested = false;
327
329
330 VkRenderPass vkRenderPass = VK_NULL_HANDLE;
331
332 XrSession xrSession = XR_NULL_HANDLE;
333
334 XrSpace xrReferenceSpace = XR_NULL_HANDLE;
335 XrReferenceSpaceType xrReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
336
337 uint32_t eyeCount = 0u;
338
339 std::vector<XrViewConfigurationView> eyeImageInfos;
340
341 std::vector<XrView> eyePoses;
342
347 {
348 VkImage image = nullptr;
349 VkDeviceMemory deviceMemory = nullptr;
350 VkImageView imageView = nullptr;
351
357 };
358
362
363 XrSwapchain xrSwapchain = nullptr;
364
365 std::unique_ptr<NamedThreadPool> xrWaitThreadPool_;
366
367 std::function<void( XrSessionState )> sessionStateCallback_;
369
370 private:
371 std::vector<RenderTarget> swapchainRenderTargets;
372
373 std::vector<XrCompositionLayerProjectionView> eyeRenderInfos;
374
378 struct Eye
379 {
380 glm::mat4 eyeViewMatrix{};
382 };
383
384 std::vector<Eye> eyes = {};
385
386 XrTime lastUpdateTime = 0;
387
388 glm::vec3 playerPosition_{ 0.0f, 0.0f, 0.0f };
389 glm::quat playerRotation_{ 1.0f, 0.0f, 0.0f, 0.0f };
390
391 public:
397 const RenderTarget * getRenderTarget( size_t swapchainImageIndex ) const;
398
403 [[nodiscard]] VkImage getDepthBufferImage() const
404 {
405 return depthBuffer.image;
406 }
407
412 [[nodiscard]] VkImageView getDepthBufferView() const
413 {
414 return depthBuffer.imageView;
415 }
416
421 [[nodiscard]] VkImage getColorBufferImage() const
422 {
423 return colorBuffer.image;
424 }
425
430 [[nodiscard]] VkImageView getColorBufferView() const
431 {
432 return colorBuffer.imageView;
433 }
434
439 [[nodiscard]] VkImage getResolveBufferImage() const
440 {
441 return resolveBuffer.image;
442 }
443
448 [[nodiscard]] VkImageView getResolveBufferView() const
449 {
450 return resolveBuffer.imageView;
451 }
452
457 [[nodiscard]] VkFormat getColorFormat() const
458 {
459 return colorFormat_;
460 }
461
466 [[nodiscard]] VkFormat getSceneColorFormat() const
467 {
468 return sceneColorFormat_;
469 }
470
471 private:
472 VkFormat swapchainFormat_ = VK_FORMAT_R8G8B8A8_UNORM;
473 VkFormat colorFormat_ = VK_FORMAT_R8G8B8A8_UNORM;
474 VkFormat sceneColorFormat_ = VK_FORMAT_R16G16B16A16_SFLOAT;
476 };
477} // 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
Render target wrappers for OpenXR swapchain images.
Definition Headset.h:371
void endXrFrameNoRender()
Ends an OpenXR frame without releasing a swapchain image or submitting projection layers.
void beginXrSession() const
Begins the OpenXR session for the configured primary view type.
bool isExistRequested
True once OpenXR or the application requests session exit.
Definition Headset.h:326
glm::quat transformPlaySpaceOrientation(const glm::quat &orientation) const
Converts an OpenXR play-space orientation into world space.
void setSessionStateCallback(std::function< void(XrSessionState)> callback)
Registers a callback invoked whenever the OpenXR session state changes.
std::vector< XrViewConfigurationView > eyeImageInfos
Runtime-recommended image settings per eye.
Definition Headset.h:339
XrSpace getReferenceSpace() const
Returns the OpenXR reference space used for view location and layer submission.
std::vector< XrView > eyePoses
Eye poses and fields of view from the latest xrLocateViews() call.
Definition Headset.h:341
std::vector< XrCompositionLayerProjectionView > eyeRenderInfos
Projection-layer views submitted to xrEndFrame().
Definition Headset.h:373
VkImage getDepthBufferImage() const
Returns the depth image used by headset rendering.
Definition Headset.h:403
glm::quat getHeadsetLocalOrientation() const
Gets the headset's local orientation relative to the play space.
XrFrameState getXrFrameState() const
Returns the latest OpenXR frame state.
VkImage getColorBufferImage() const
Returns the multisampled color image used by headset rendering.
Definition Headset.h:421
VkRenderPass getRenderPass() const
Returns the legacy multiview render pass.
VkImage getSwapchainImage(size_t swapchainImageIndex) const
Returns a borrowed OpenXR swapchain image handle.
VkImageView getDepthBufferView() const
Returns the depth image view used by headset rendering.
Definition Headset.h:412
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)
Runs waitForXrFrame() and beginXrFrameAfterWait() as a single legacy call.
glm::vec3 getPlayerPosition() const
Returns the current player/play-space world position.
Definition Headset.h:224
std::function< void(XrSessionState)> sessionStateCallback_
Invoked on OpenXR session state changes.
Definition Headset.h:367
XrSpace xrReferenceSpace
Reference space used to locate views and submit layers.
Definition Headset.h:334
XrReferenceSpaceType xrReferenceSpaceType
Configured reference-space type.
Definition Headset.h:335
XrSessionState xrSessionState
Current OpenXR session lifecycle state.
Definition Headset.h:324
VkFormat getColorFormat() const
Returns the format used by XR swapchain color attachment views.
Definition Headset.h:457
void endXrSession() const
Ends the active OpenXR session.
uint32_t getEyeCount() const
Returns the number of configured XR views.
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
Returns the HDR scene color format used for engine-owned color and resolve buffers.
Definition Headset.h:466
VkRenderPass vkRenderPass
Legacy multiview render pass used by framebuffer rendering.
Definition Headset.h:330
ImageBuffer depthBuffer
Multisampled depth attachment, also sampled by Hi-Z generation.
Definition Headset.h:360
VkImage getResolveBufferImage() const
Returns the single-sample resolve image used before XR swapchain tonemapping.
Definition Headset.h:439
XrViewState xrViewState
Validity flags for the latest xrLocateViews() result.
Definition Headset.h:321
ImageBuffer resolveBuffer
Single-sample HDR resolve target before tonemapping to the XR swapchain.
Definition Headset.h:361
VkFormat colorFormat_
VkFormat used for OpenXR swapchain image views.
Definition Headset.h:473
glm::mat4 getRawEyeViewMatrix(size_t eyeIndex) const
Returns the raw OpenXR view matrix for an eye.
VkFormat swapchainFormat_
VkFormat used when creating the OpenXR swapchain.
Definition Headset.h:472
Headset(const Core::ApplicationContext *context=nullptr)
Creates OpenXR session, reference space, render pass, images, swapchain, and eye state.
const Core::ApplicationContext * context
Application context that owns Vulkan and OpenXR handles.
Definition Headset.h:328
glm::vec3 playerPosition_
World-space player/play-space offset.
Definition Headset.h:388
glm::quat playerRotation_
World-space player/play-space rotation.
Definition Headset.h:389
XrFrameState xrFrameState
State returned by the latest xrWaitFrame() call.
Definition Headset.h:322
~Headset()
Does not release GPU or OpenXR resources; call cleanup() before destruction.
void endXrFrame()
Releases the acquired swapchain image and submits projection layers to xrEndFrame().
XrSwapchain xrSwapchain
OpenXR color swapchain submitted to the runtime.
Definition Headset.h:363
XrSession getSession() const
Returns the OpenXR session handle.
std::unique_ptr< NamedThreadPool > xrWaitThreadPool_
Thread pool used to timeout xrWaitFrame() safely.
Definition Headset.h:365
void setInteractionProfileChangedCallback(std::function< void()> callback)
Registers a callback invoked when the runtime reports an interaction profile change.
XrSession xrSession
OpenXR session created for the application context's system id.
Definition Headset.h:332
ImageBuffer colorBuffer
Multisampled HDR color attachment for multiview rendering.
Definition Headset.h:359
BeginFrameResult
Frame-begin outcome returned by XR frame lifecycle functions.
Definition Headset.h:77
@ ComputeOnly
Run compute work but skip XR and mesh rendering.
Definition Headset.h:82
@ RenderFully
Render this frame normally.
Definition Headset.h:79
@ SkipFully
Skip processing the frame without calling xrEndFrame().
Definition Headset.h:81
@ SkipRender
Skip rendering but still end the OpenXR frame.
Definition Headset.h:80
@ Error
An OpenXR error occurred.
Definition Headset.h:78
std::vector< Eye > eyes
Cached eye matrices indexed by zero-based eye index.
Definition Headset.h:384
void setPlayerPosition(const glm::vec3 &position)
Sets the player/play-space world position.
Definition Headset.h:215
bool useMutableSwapchainFormat_
True when swapchain views use a different color format.
Definition Headset.h:475
void updateViewMatrix()
Updates cached eye view and projection matrices from the latest OpenXR eye poses.
uint32_t eyeCount
Number of views reported by the OpenXR runtime.
Definition Headset.h:337
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...
void cleanup()
Releases owned Vulkan image buffers, swapchain render targets, render pass, reference space,...
std::vector< RenderTarget > getSwapchainRenderTargets()
Returns a copy of the swapchain render-target list.
glm::quat getHeadsetWorldOrientation() const
Gets the headset orientation after player rotation is applied.
std::function< void()> interactionProfileChangedCallback_
Invoked on OpenXR interaction profile changes.
Definition Headset.h:368
bool getIsExitRequested() const
Returns whether OpenXR requested application exit or session shutdown.
VkFormat sceneColorFormat_
HDR format for engine-owned scene buffers.
Definition Headset.h:474
glm::mat4 getEyeViewMatrix(size_t eyeIndex) const
Returns the world-adjusted view matrix for an eye.
glm::mat4 getViewProjectionMatrix(size_t eyeIndex) const
Gets the projection-view matrix for an eye.
glm::quat getPlayerRotation() const
Returns the player/play-space rotation.
Definition Headset.h:239
glm::vec3 getHeadsetWorldPosition() const
Gets the headset position after player position/rotation are applied.
XrTime lastUpdateTime
Reserved timestamp for future pose update tracking.
Definition Headset.h:386
VkExtent2D getEyeResolution(size_t eyeIndex) const
Returns the recommended render resolution for an eye.
const RenderTarget * getRenderTarget(size_t swapchainImageIndex) const
Returns the render target for a swapchain image.
VkImageView getColorBufferView() const
Returns the multisampled color image view used by headset rendering.
Definition Headset.h:430
VkImageView getResolveBufferView() const
Returns the single-sample resolve image view used before XR swapchain tonemapping.
Definition Headset.h:448
Cached matrices for one eye.
Definition Headset.h:379
glm::mat4 eyeViewMatrix
Raw OpenXR eye view matrix before player transform.
Definition Headset.h:380
glm::mat4 eyeProjectionMatrix
Projection matrix built from the OpenXR eye field of view.
Definition Headset.h:381
Engine-owned Vulkan image, memory, and view tuple.
Definition Headset.h:347
void cleanup(const Core::ApplicationContext *context)
Destroys image view, image, and device memory.
VkImageView imageView
Image view for rendering or sampling.
Definition Headset.h:350
VkImage image
Vulkan image owned by Headset.
Definition Headset.h:348
VkDeviceMemory deviceMemory
Device-local memory bound to image.
Definition Headset.h:349
Result from waitForXrFrame() indicating how the main loop should continue.
Definition Headset.h:89
bool shouldCallBeginFrame
True when beginXrFrameAfterWait() must be called before ending the frame.
Definition Headset.h:91
BeginFrameResult result
Recommended frame action after xrWaitFrame() and event polling.
Definition Headset.h:90
bool needsGpuSyncForStateTransition
True when the main loop must perform deferred GPU sync.
Definition Headset.h:92
Render target wrapper for one OpenXR swapchain image.
Definition Headset.h:33
VkImageView imageView
View used when sampling or copying the swapchain image.
Definition Headset.h:35
VkImageLayout imageLayout
Tracked layout for legacy render-target paths.
Definition Headset.h:45
VkImage image
Borrowed swapchain image for this render target.
Definition Headset.h:34
VkFramebuffer framebuffer
Legacy render-pass framebuffer for this swapchain image.
Definition Headset.h:36
void cleanup(const Core::ApplicationContext *context)
Destroys framebuffer and image view resources for this render target.