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
impl TextureManager
Sourcepub fn allocate_texture(&self, texture_id: u64, texture_dimensions: (u32, u32))
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.
Sourcepub fn allocate_texture_with_data(
&self,
texture_id: u64,
texture_dimensions: (u32, u32),
texture_data: &[u8],
)
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).
Sourcepub fn load_data_into_texture(
&self,
texture_id: u64,
texture_dimensions: (u32, u32),
texture_data: &[u8],
) -> Result<(), TextureManagerError>
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 apremultiply_rgba8_srgb_inplacehelper 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.
pub fn is_texture_loaded(&self, texture_id: u64) -> bool
Trait Implementations§
Source§impl Clone for TextureManager
impl Clone for TextureManager
Source§fn clone(&self) -> TextureManager
fn clone(&self) -> TextureManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more