TextureManager

Struct TextureManager 

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

A manager for textures providing granular control over texture handling.

This manager allows for:

  • Loading textures from different threads while keeping usage safe in the rendering thread.
  • Allocating textures and subsequently loading image data into them.
  • Updating the data in an existing texture.

§Examples

Allocate a texture and then load data into it:

let texture_manager = renderer.texture_manager();
let texture_id = 42;
let texture_dimensions = (256, 256);
let data = vec![255u8; 256 * 256 * 4];

// Allocate texture and load data
texture_manager.allocate_texture_with_data(texture_id, texture_dimensions, &data);
// Update data in the texture
texture_manager.load_data_into_texture(texture_id, texture_dimensions, &data).unwrap();
// Check if the texture is loaded
assert!(texture_manager.is_texture_loaded(texture_id));
// Clone the texture manager to pass to another thread
let texture_manager_clone = texture_manager.clone();

The texture manager internally uses an Arc<RwLock<_>> to manage its bind group layout and texture storage.

Implementations§

Source§

impl TextureManager

Source

pub fn allocate_texture(&self, texture_id: u64, texture_dimensions: (u32, u32))

Allocates a new RGBA8 texture with the given dimensions without providing any data.

If you want to allocate and load data into the texture at the same time, use TextureManager::allocate_texture_with_data instead. You can then load data into the texture later using TextureManager::load_data_into_texture.

§Parameters
  • texture_id: Unique identifier for the texture.
  • texture_dimensions: A tuple (width, height) representing the dimensions of the texture.
Source

pub fn allocate_texture_with_data( &self, texture_id: u64, texture_dimensions: (u32, u32), texture_data: &[u8], )

Allocates a texture and immediately loads image data into it.

This function will first allocate the texture, then attempt to load the provided data.

If you are seeing fringes when sampling/minifying near transparent edges, ensure that your texture data is in a premultiplied alpha format. You can use the premultiply_rgba8_srgb_inplace helper function provided in this crate to convert your RGBA8 sRGB data to premultiplied alpha.

§Parameters
  • texture_id: Unique identifier for the texture.
  • texture_dimensions: A tuple (width, height) representing the dimensions of the texture.
  • texture_data: A byte slice containing the image data. The data length is expected to match the texture dimensions and pixel format (RGBA8 with premultiplied alpha).
Source

pub fn load_data_into_texture( &self, texture_id: u64, texture_dimensions: (u32, u32), texture_data: &[u8], ) -> Result<(), TextureManagerError>

Loads image data into an already allocated texture. If you are seeing fringes when sampling/minifying near transparent edges, ensure that your texture data is in a premultiplied alpha format. You can use the premultiply_rgba8_srgb_inplace helper function provided in this crate to convert your RGBA8 sRGB data to premultiplied alpha.

§Parameters
  • texture_id: Unique identifier for the texture.
  • texture_dimensions: A tuple (width, height) representing the dimensions of the texture.
  • texture_data: A byte slice containing the image data in an RGBA8 format with premultiplied alpha. If your texture isn’t premultiplied, consider using a premultiply_rgba8_srgb_inplace helper function provided in this crate. This is needed to avoid fringes when sampling/minifying near transparent edges.
§Returns
  • Ok(()) if the operation succeeds.
  • Err(TextureManagerError::TextureNotFound(texture_id)) if the texture does not exist.
Source

pub fn is_texture_loaded(&self, texture_id: u64) -> bool

Trait Implementations§

Source§

impl Clone for TextureManager

Source§

fn clone(&self) -> TextureManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,