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
impl SwapchainSk
Sourcepub fn new(
format: TexFormat,
width: u32,
height: u32,
xr_comp_layers: Option<XrCompLayers>,
) -> Option<Self>
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.
Sourcepub fn get_render_target(&self) -> Option<&Tex>
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");
}
Sourcepub fn wrap(
handle: Swapchain,
format: TexFormat,
width: u32,
height: u32,
xr_comp_layers: Option<XrCompLayers>,
) -> Option<Self>
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.
Sourcepub fn acquire_image(
&mut self,
timeout_ns: Option<i64>,
) -> Result<u32, XrResult>
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. IfNone
, 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),
}
Sourcepub fn release_image(&mut self) -> Result<(), XrResult>
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");
}
Auto Trait Implementations§
impl Freeze for SwapchainSk
impl RefUnwindSafe for SwapchainSk
impl !Send for SwapchainSk
impl !Sync for SwapchainSk
impl Unpin for SwapchainSk
impl UnwindSafe for SwapchainSk
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.