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
impl XrCompLayers
Sourcepub fn new() -> Option<Self>
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.
Sourcepub fn to_native_format(format: TexFormat) -> i64
pub fn to_native_format(format: TexFormat) -> i64
Convert a StereoKit TexFormat
into the corresponding native OpenXR format value.
Sourcepub 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>,
)
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).
Sourcepub fn try_make_swapchain(
&self,
width: u32,
height: u32,
format: TexFormat,
usage: SwapchainUsageFlags,
single_image: bool,
) -> Option<Swapchain>
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.
Sourcepub fn destroy_swapchain(swapchain: Swapchain)
pub fn destroy_swapchain(swapchain: Swapchain)
Destroy the given XR swapchain handle.
Trait Implementations§
Source§impl Debug for XrCompLayers
impl Debug for XrCompLayers
Auto Trait Implementations§
impl Freeze for XrCompLayers
impl RefUnwindSafe for XrCompLayers
impl Send for XrCompLayers
impl Sync for XrCompLayers
impl Unpin for XrCompLayers
impl UnwindSafe for XrCompLayers
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.