evan 2026.1.4
Evan
Loading...
Searching...
No Matches
evan::XrSwapchainContext Class Reference

The XrSwapchainContext class manages the swapchain context for an OpenXR application using Vulkan. It inherits from ASwapchainContext and provides implementations for acquiring images from the swapchain, waiting for images to be available, and updating projection layer views based on the current view configurations. The class also maintains a vector of XrCompositionLayerProjectionView structures that represent the projection layer views for each view configuration, allowing for efficient management of rendering resources in a VR application. More...

#include <headers/evan/openxr/XrSwapchainContext.hpp>

Inheritance diagram for evan::XrSwapchainContext:
Collaboration diagram for evan::XrSwapchainContext:

Public Member Functions

 XrSwapchainContext (const DeviceContext &deviceContext)
 Constructs an XrSwapchainContext instance using the provided DeviceContext.
 
void destroy (VkDevice device) override
 Destroys all swapchain images and clears the swapchain image collection.
 
void recreateSwapchain (const DeviceContext &deviceContext, VkRenderPass renderpass) override
 Recreates the swapchain and associated resources.
 
VkResult aquireImage (uint32_t index, VkDevice device, VkSemaphore imageAvailableSemaphore, VkFence inFlightFence, uint32_t &imageIndex) override
 Acquires an image from the swapchain for rendering.
 
void waitForImage (uint32_t index) override
 Waits for a swapchain image to become available.
 
void updateProjectionLayerViews ()
 Updates the projection layer views based on current swapchain images and view data.
 
const std::vector< XrCompositionLayerProjectionView > & getProjectionLayerViews () const
 Retrieves the projection layer views for the current view configurations.
 
ViewSet & getViewSet () override
 Retrieves the set of views to render (mutable).
 
const ViewSet & getViewSet () const override
 Retrieves the set of views to render (const).
 
VkSampleCountFlagBits getMsaaSamples () const override
 Retrieves the MSAA sample count used by this swapchain context.
 
bool usesMultiview () const override
 Indicates whether all views render into one layered swapchain.
 
void syncViewSet ()
 Rebuilds the ViewSet from the latest OpenXR view state.
 
void setView (std::size_t index, const utility::graphic::ViewF &view) override
 Sets the view matrix for a specific view index.
 
- Public Member Functions inherited from evan::ASwapchainContext
VkRenderPass getRenderPass () const
 Retrieves the render pass associated with the swapchain context.
 
std::size_t getSwapchainCount () const
 Retrieves the number of swapchain image sets in the context.
 
virtual bool usesImageAvailableSemaphore () const
 Indicates whether aquireImage signals the image-available semaphore.
 
virtual bool needsSwapchainRecreation () const
 Indicates whether the swapchain needs to be recreated.
 
utility::graphic::ViewF getView (std::size_t index) const
 Retrieves the view matrix for the specified view index.
 
const utility::graphic::PoseF & getViewOffset (void) const
 Retrieves the persistent world-space view offset.
 
std::size_t getViewCount (void) const
 Retrieves the number of views in the swapchain context.
 
glm::mat4 getProjection (std::size_t index) const
 Retrieves the projection matrix for the specified view index.
 
utility::math::Vector2F getViewportSize () const
 Retrieves the current viewport size in pixels.
 

Public Attributes

std::vector< XrView > _views
 
std::vector< XrViewConfigurationView > _viewsConfigurations
 
- Public Attributes inherited from evan::ASwapchainContext
std::vector< std::shared_ptr< ASwapchainImage > > _swapchainImages
 

Additional Inherited Members

- Static Public Member Functions inherited from evan::ASwapchainContext
static void updateViewForExtent (utility::graphic::ViewF &view, VkExtent2D extent)
 Updates a view's viewport size and perspective for a new swapchain extent.
 
- Protected Member Functions inherited from evan::ASwapchainContext
void createRenderPass (const std::shared_ptr< ADeviceBackend > &deviceBackend, VkSampleCountFlagBits msaaSamples, ColorAttachmentMode mode, uint32_t viewMask=0)
 Creates the Vulkan render pass for the swapchain context.
 
VkFormat selectSwapchainFormat (VkPhysicalDevice physicalDevice, const std::vector< int64_t > &swapchainFormats)
 Selects the appropriate swapchain format from the available formats.
 
VkFormat findSupportedFormat (VkPhysicalDevice physicalDevice, const std::vector< VkFormat > &candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
 Finds a supported format from a list of candidates based on the specified tiling and feature requirements.
 
- Protected Attributes inherited from evan::ASwapchainContext
VkRenderPass _renderPass
 
utility::graphic::PoseF _viewOffset {}
 Persistent world-space offset applied on top of the tracked poses.
 

Detailed Description

The XrSwapchainContext class manages the swapchain context for an OpenXR application using Vulkan. It inherits from ASwapchainContext and provides implementations for acquiring images from the swapchain, waiting for images to be available, and updating projection layer views based on the current view configurations. The class also maintains a vector of XrCompositionLayerProjectionView structures that represent the projection layer views for each view configuration, allowing for efficient management of rendering resources in a VR application.

Note
The XrSwapchainContext class is designed to work with the DeviceContext class to manage Vulkan resources and ensure proper synchronization when acquiring images from the swapchain. It also provides methods to retrieve projection and view matrices for rendering operations, making it an essential component of the rendering system in an OpenXR application.

Definition at line 40 of file XrSwapchainContext.hpp.

Constructor & Destructor Documentation

◆ XrSwapchainContext()

evan::XrSwapchainContext::XrSwapchainContext ( const DeviceContext &  deviceContext)

Constructs an XrSwapchainContext instance using the provided DeviceContext.

This constructor initializes the XrSwapchainContext by setting up the necessary Vulkan resources and configurations based on the provided DeviceContext. It may involve querying the swapchain support details, creating projection layer views, and preparing for image acquisition from the swapchain. Implement this constructor to ensure that the XrSwapchainContext is properly initialized and ready for use in rendering operations within an OpenXR application.

Parameters
deviceContextA reference to a DeviceContext instance that provides access to Vulkan resources and configurations needed for initializing the XrSwapchainContext. This context is essential for managing the swapchain and ensuring proper synchronization when acquiring images for rendering.
Note
The constructor should handle any necessary setup for the swapchain context, including querying swapchain support details, creating projection layer views based on the current view configurations, and preparing for image acquisition from the swapchain. Ensure that all Vulkan resources are properly initialized and that the XrSwapchainContext is ready for use in rendering operations within an OpenXR application.

Definition at line 46 of file XrSwapchainContext.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ aquireImage()

VkResult evan::XrSwapchainContext::aquireImage ( uint32_t  index,
VkDevice  device,
VkSemaphore  imageAvailableSemaphore,
VkFence  inFlightFence,
uint32_t &  imageIndex 
)
overridevirtual

Acquires an image from the swapchain for rendering.

This method is responsible for acquiring an image from the swapchain, which can then be used for rendering operations. It takes the index of the swapchain image to acquire, the Vulkan device, a semaphore to signal when the image is available, a fence to ensure synchronization, and a reference to store the acquired image index. Implement this method to properly acquire images from the swapchain and ensure synchronization with rendering operations in an OpenXR application.

Parameters
indexThe index of the swapchain image to acquire.
deviceThe Vulkan device used for acquiring the image.
imageAvailableSemaphoreA Vulkan semaphore that will be signaled when the image is available for rendering.
inFlightFenceA Vulkan fence used to ensure synchronization when acquiring the image.
imageIndexA reference to store the index of the acquired image from the swapchain.
Returns
VkResult The result of the image acquisition operation, indicating success or failure. Implement this method to return appropriate Vulkan result codes based on the outcome of the image acquisition process.

Implements evan::ASwapchainContext.

Definition at line 358 of file XrSwapchainContext.cpp.

◆ destroy()

void evan::XrSwapchainContext::destroy ( VkDevice  device)
overridevirtual

Destroys all swapchain images and clears the swapchain image collection.

This function iterates through all swapchain images stored in the context, destroys each image using the provided Vulkan device, and then clears the swapchain image collection.

Parameters
deviceThe Vulkan device handle used to destroy the swapchain images.
Note
This function should be called during cleanup to ensure proper resource deallocation of all swapchain images.
See also
evan::XrSwapchainContext

Implements evan::ASwapchainContext.

Definition at line 303 of file XrSwapchainContext.cpp.

◆ getMsaaSamples()

VkSampleCountFlagBits evan::XrSwapchainContext::getMsaaSamples ( ) const
overridevirtual

Retrieves the MSAA sample count used by this swapchain context.

The count is derived from the OpenXR runtime's recommended swapchain sample count.

Returns
The MSAA sample count used by this swapchain context.

Implements evan::ASwapchainContext.

Definition at line 466 of file XrSwapchainContext.cpp.

◆ getProjectionLayerViews()

const std::vector< XrCompositionLayerProjectionView > & evan::XrSwapchainContext::getProjectionLayerViews ( ) const

Retrieves the projection layer views for the current view configurations.

This method returns a reference to a vector of XrCompositionLayerProjectionView structures that represent the projection layer views for the current view configurations. These views are essential for rendering the scene accurately in an OpenXR application, as they provide the necessary information for projecting the scene onto the display. Implement this method to return the appropriate projection layer views based on the current view configurations.

Returns
const std::vector<XrCompositionLayerProjectionView>& A reference to a vector of XrCompositionLayerProjectionView structures that represent the projection layer views for the current view configurations. This vector should be updated based on any changes in the view configurations to ensure accurate rendering of the scene in an OpenXR application.

Definition at line 451 of file XrSwapchainContext.cpp.

◆ getViewSet() [1/2]

const evan::ViewSet & evan::XrSwapchainContext::getViewSet ( ) const
overridevirtual

Retrieves the set of views to render (const).

Returns
A const reference to the ViewSet.

Implements evan::ASwapchainContext.

Definition at line 461 of file XrSwapchainContext.cpp.

◆ getViewSet() [2/2]

evan::ViewSet & evan::XrSwapchainContext::getViewSet ( )
overridevirtual

Retrieves the set of views to render (mutable).

Returns
A mutable reference to the ViewSet.

Implements evan::ASwapchainContext.

Definition at line 456 of file XrSwapchainContext.cpp.

◆ recreateSwapchain()

void evan::XrSwapchainContext::recreateSwapchain ( const DeviceContext &  deviceContext,
VkRenderPass  renderpass 
)
overridevirtual

Recreates the swapchain and associated resources.

This function is responsible for recreating the swapchain and any associated resources when the swapchain becomes out of date or needs to be recreated due to changes in the window size or other factors. It takes a reference to the DeviceContext instance, which provides access to Vulkan resources and synchronization mechanisms, and the Vulkan render pass used for rendering operations. Implement this function to ensure that the swapchain is properly recreated and that all necessary resources are reinitialized for rendering operations.

Parameters
deviceContextA reference to the DeviceContext instance that provides access to Vulkan resources and synchronization mechanisms.
renderpassThe Vulkan render pass that will be used with the swapchain images during rendering operations.

Implements evan::ASwapchainContext.

Definition at line 316 of file XrSwapchainContext.cpp.

Here is the call graph for this function:

◆ setView()

void evan::XrSwapchainContext::setView ( std::size_t  index,
const utility::graphic::ViewF &  view 
)
overridevirtual

Sets the view matrix for a specific view index.

For OpenXR this method updates the near and far clipping planes used to build the per-eye projection matrices. The eye pose and field of view are refreshed by syncViewSet each frame. The requested pose is reduced to a persistent world-space offset relative to the tracked head center so locomotion survives the per-frame pose refresh.

Parameters
indexThe index of the view for which to set the view matrix.
viewThe view matrix to set for the specified view index.

Reimplemented from evan::ASwapchainContext.

Definition at line 558 of file XrSwapchainContext.cpp.

◆ syncViewSet()

void evan::XrSwapchainContext::syncViewSet ( )

Rebuilds the ViewSet from the latest OpenXR view state.

Must be called after xrLocateViews (done in preprocessFrame) so the ViewSet reflects the current eye poses, fields of view and clipping planes before the renderer iterates over it.

Definition at line 476 of file XrSwapchainContext.cpp.

◆ updateProjectionLayerViews()

void evan::XrSwapchainContext::updateProjectionLayerViews ( )

Updates the projection layer views based on current swapchain images and view data.

This function synchronizes the projection layer views with the available swapchain images and view information. It resizes the projection layer views container to match the smaller of the two collections (views or swapchain images) and populates each view with:

  • Composition layer type information
  • View pose and field of view from the corresponding view
  • Swapchain reference and image rectangle dimensions from the corresponding swapchain image

Each projection layer view is initialized with zero extent and offset (0, 0), and the image rectangle extent is set to the swapchain image dimensions. Dynamic cast is used to safely retrieve XrSwapchainImage instances; if a cast fails, that index is skipped.

Note
This function should be called whenever the swapchain images or view data changes to ensure the projection layer views remain synchronized.
See also
XrSwapchainImage
XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW

Definition at line 398 of file XrSwapchainContext.cpp.

◆ usesMultiview()

bool evan::XrSwapchainContext::usesMultiview ( ) const
overridevirtual

Indicates whether all views render into one layered swapchain.

True when the device supports multiview and the runtime accepted a swapchain with one array layer per view, so the renderer records a single pass covering every eye. False keeps the two-pass fallback (one swapchain per view).

Returns
True when the renderer must record a single multiview pass.

Reimplemented from evan::ASwapchainContext.

Definition at line 471 of file XrSwapchainContext.cpp.

◆ waitForImage()

void evan::XrSwapchainContext::waitForImage ( uint32_t  index)
overridevirtual

Waits for a swapchain image to become available.

Blocks until the swapchain image at the specified index is ready for rendering. Uses an infinite timeout duration to wait indefinitely for the image to be available.

Parameters
indexThe index of the swapchain image to wait for.
Note
If the wait operation fails, an error message is printed to stderr. The function does not throw exceptions on failure.
See also
xrWaitSwapchainImage

Implements evan::ASwapchainContext.

Definition at line 379 of file XrSwapchainContext.cpp.

Member Data Documentation

◆ _views

std::vector<XrView> evan::XrSwapchainContext::_views

Vector of XrView structures for each view configuration.

Definition at line 270 of file XrSwapchainContext.hpp.

◆ _viewsConfigurations

std::vector<XrViewConfigurationView> evan::XrSwapchainContext::_viewsConfigurations

Vector of XrViewConfigurationView structures for each view configuration, containing details about the recommended image rect dimensions and swapchain sample counts for each view.

Definition at line 277 of file XrSwapchainContext.hpp.


The documentation for this class was generated from the following files: