SwapchainSk

Struct SwapchainSk 

Source
pub struct SwapchainSk {
    pub xr_comp_layers: XrCompLayers,
    pub handle: Swapchain,
    pub width: u32,
    pub height: u32,
    pub acquired: u32,
    /* private fields */
}
Expand description

High-level wrapper around an OpenXR swapchain. Manages creation of render-target textures, image acquisition and release.

SwapchainSk provides a convenient interface for working with OpenXR swapchains in StereoKit applications. It handles the complexity of swapchain image management and provides StereoKit Tex objects for rendering.

§Examples

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::* };

// Create a swapchain
if let Some(mut swapchain) = SwapchainSk::new(TexFormat::RGBA32, 512, 512, None) {
    
    // Set up rendering
    let mut render_list = RenderList::new();
    let material = Material::default().copy();
    render_list.add_mesh(
        Mesh::cube(),
        material,
        Matrix::IDENTITY,
        named_colors::RED,
        None
    );
    
    // Render to swapchain
    if let Ok(_) = swapchain.acquire_image(None) {
        if let Some(render_target) = swapchain.get_render_target() {
            render_list.draw_now(
                render_target,
                Matrix::look_at(Vec3::angle_xy(Time::get_totalf() * 90.0, 0.0), Vec3::ZERO, None),
                Matrix::orthographic(1.0, 1.0, 0.1, 10.0),
                None,
                Some(RenderClear::All),
                Rect::new(0.0, 0.0, 1.0, 1.0),
                None,
            );
        }
        swapchain.release_image().expect("Failed to release image");
    }
    
    // Clean up
    swapchain.destroy();
}

Fields§

§xr_comp_layers: XrCompLayers§handle: Swapchain§width: u32§height: u32§acquired: u32

Implementations§

Source§

impl SwapchainSk

Source

pub fn new( format: TexFormat, width: u32, height: u32, xr_comp_layers: Option<XrCompLayers>, ) -> Option<Self>

Create a new SwapchainSk for rendering into an OpenXR quad layer. Returns Some<Self> if the XR runtime and swapchain creation succeed.

Source

pub fn get_render_target(&self) -> Option<&Tex>

Return a reference to the currently acquired render-target texture, if any.

This method provides access to the StereoKit Tex object that represents the currently acquired swapchain image. The texture can be used as a render target for drawing operations.

§Returns
  • Some(&Tex): Reference to the current render target texture.
  • None: No image is currently acquired or swapchain is empty.
§Example
if let Ok(_) = swapchain.acquire_image(None) {
    if let Some(render_target) = swapchain.get_render_target() {
        // Use render_target for drawing operations
        println!("Render target size: {}x{}",
                 render_target.get_width().unwrap_or(0),
                 render_target.get_height().unwrap_or(0));
         
        // ... perform rendering to render_target ...
    }
    swapchain.release_image().expect("Failed to release");
}
Source

pub fn wrap( handle: Swapchain, format: TexFormat, width: u32, height: u32, xr_comp_layers: Option<XrCompLayers>, ) -> Option<Self>

Wrap OpenGL ES swapchain images into Tex objects for unix platforms.

Source

pub fn acquire_image( &mut self, timeout_ns: Option<i64>, ) -> Result<u32, XrResult>

Acquire the next image from the swapchain, waiting up to timeout_ns nanoseconds.

This method must be called before rendering to the swapchain. It acquires an available image from the swapchain and waits for it to be ready for rendering.

§Parameters
  • timeout_ns: Optional timeout in nanoseconds. If None, waits indefinitely.
§Returns
  • Ok(image_index): The index of the acquired image on success.
  • Err(XrResult): OpenXR error code if acquisition fails.
§Example
// Acquire with default timeout
match swapchain.acquire_image(None) {
    Ok(image_index) => {
        println!("Acquired image {}", image_index);
        // Render to swapchain.get_render_target()
        // ... rendering code ...
        swapchain.release_image().expect("Failed to release");
    }
    Err(e) => eprintln!("Failed to acquire image: {:?}", e),
}
Source

pub fn release_image(&mut self) -> Result<(), XrResult>

Release the currently held image back to the swapchain.

This method must be called after finishing rendering to the acquired image. It signals to the OpenXR runtime that rendering is complete and the image can be used for composition.

§Returns
  • Ok(()): Successfully released the image.
  • Err(XrResult): OpenXR error code if release fails.
§Example
// After acquiring and rendering to the image
if let Ok(_) = swapchain.acquire_image(None) {
    // ... render to swapchain.get_render_target() ...
     
    // Must release the image when done
    swapchain.release_image().expect("Failed to release image");
}
Source

pub fn destroy(&mut self)

Destroy the swapchain and all associated resources.

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> 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