Struct XrCompLayers

Source
pub struct XrCompLayers { /* private fields */ }
Expand description

Helper for loading and using the OpenXR composition layers extension. Provides function pointers for swapchain management and layer submission.

This is a rust adaptation of https://github.com/StereoKit/StereoKit/blob/master/Examples/StereoKitTest/Tools/XrCompLayers.cs

§Overview

XrCompLayers provides low-level OpenXR composition layer functionality, while SwapchainSk offers a high-level wrapper for creating and managing OpenXR swapchains with StereoKit integration.

§Examples

§Basic Usage with SwapchainSk

use stereokit_rust::{ maths::{Vec3, Matrix, Pose, Vec2, Rect},  render_list::RenderList,
    util::{named_colors, Color128, Time}, tex::TexFormat, material::Material, mesh::Mesh,
    system::{Backend, BackendXRType, RenderClear}, tools::xr_comp_layers::* };

use openxr_sys::SwapchainUsageFlags;

// Check if OpenXR is available
if Backend::xr_type() == BackendXRType::OpenXR {
    // Create XrCompLayers instance
    if let Some(mut swapchain) = SwapchainSk::new(
            TexFormat::RGBA32, 512, 512, None ){
         
        // Set up rendering components
        let mut render_list = RenderList::new();
        let mut material = Material::default().copy();
        let projection = Matrix::orthographic(0.2, 0.2, 0.01, 10.0);
         
        // Add a sphere to the scene
        render_list.add_mesh( Mesh::sphere(), material, Matrix::s(0.05 * Vec3::ONE),
                              named_colors::WHITE, None);
         
        // Render loop example
        test_steps!( // !!!! Get a proper main loop !!!!
            // Acquire the next swapchain image
            if let Ok(_image_index) = swapchain.acquire_image(None) {
                // Get the render target texture
                if let Some(render_tex) = swapchain.get_render_target() {
                    // Render to the swapchain texture
                    render_list.draw_now(
                        render_tex,
                        Matrix::look_at(Vec3::angle_xy(Time::get_totalf() * 90.0, 0.0), Vec3::ZERO, None),
                        projection,
                        Some(Color128::new(0.4, 0.3, 0.2, 1.0)),
                        Some(RenderClear::Color),
                        Rect::new(0.0, 0.0, 1.0, 1.0),
                        None,
                    );
                }
                 
                // Release the image back to the swapchain
                swapchain.release_image().expect("Failed to release image");
                 
                // Submit the quad layer to OpenXR
                XrCompLayers::submit_quad_layer(
                    Pose::new(Vec3::new(0.0, 1.5, -1.0), None), // World position
                    Vec2::new(0.3, 0.3),                        // Quad size
                    swapchain.handle,                           // Swapchain handle
                    Rect::new(0.0, 0.0, 512.0, 512.0),          // Texture rectangle
                    0,                                          // Array index
                    1,                                          // Sort order
                    None,                                       // Eye visibility
                    None,                                       // XR space
                );
            }
        );
         
        // Clean up
        swapchain.destroy();
    }
}

Implementations§

Source§

impl XrCompLayers

Source

pub fn new() -> Option<Self>

Initializes the composition layers helper by loading required OpenXR bindings. Returns Some(Self) if the XR runtime type is OpenXR and all bindings are present.

Source

pub fn to_native_format(format: TexFormat) -> i64

Convert a StereoKit TexFormat into the corresponding native OpenXR format value.

Source

pub fn submit_quad_layer( world_pose: Pose, size: Vec2, swapchain: Swapchain, swapchain_rect: Rect, swapchain_array_index: u32, composition_sort_order: i32, visibility: Option<EyeVisibility>, xr_space: Option<u64>, )

Submit a quad layer to the OpenXR composition.

This method submits a rendered quad layer to the OpenXR compositor, which will be displayed in the XR environment. The quad appears as a 2D surface in 3D space.

§Parameters
  • world_pose: Pose of the quad in world space (position and orientation).
  • size: Dimensions of the quad in meters.
  • swapchain: Swapchain handle to sample the texture from.
  • swapchain_rect: Texture rectangle within the swapchain image (in pixel coordinates).
  • swapchain_array_index: Array slice index for texture arrays (usually 0).
  • composition_sort_order: Ordering for layer submission (higher values render on top).
  • visibility: Optional eye visibility mask (None means both eyes).
  • xr_space: Optional XR space handle (None uses default space).
Source

pub fn try_make_swapchain( &self, width: u32, height: u32, format: TexFormat, usage: SwapchainUsageFlags, single_image: bool, ) -> Option<Swapchain>

Create a standard XR swapchain with the given parameters.

Source

pub fn destroy_swapchain(swapchain: Swapchain)

Destroy the given XR swapchain handle.

Trait Implementations§

Source§

impl Debug for XrCompLayers

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for XrCompLayers

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more