Texture

Struct Texture 

Source
pub struct Texture {
    pub label: String,
    pub texture: Texture,
    pub format: TextureFormat,
    pub view: TextureView,
    pub sampler: Sampler,
    pub size: (u32, u32),
    pub sample_count: u32,
    pub layer_count: u32,
    pub mip_level_count: u32,
}
Expand description

Texture wrapper with a ready-to-use view and sampler.

§Examples

Create a render target and clear it:

use wde_wgpu::{texture::{Texture, TextureFormat, TextureUsages}, instance::RenderInstanceData};

let color = Texture::new(
    instance,
    "color-target",
    (1280, 720),
    TextureFormat::Rgba8Unorm,
    TextureUsages::RENDER_ATTACHMENT | TextureUsages::COPY_SRC,
);

Upload raw pixel data (RGBA8):

use wde_wgpu::{texture::{Texture, TextureFormat, TextureUsages}, instance::RenderInstanceData};

let texture = Texture::new(
    instance,
    "albedo",
    (512, 512),
    TextureFormat::Rgba8Unorm,
    TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
);
texture.copy_from_buffer(instance, TextureFormat::Rgba8Unorm, pixels);
// Or
texture.copy_from_buffer_layered(instance, TextureFormat::Rgba8Unorm, 0, pixels); // For texture arrays

Copy one GPU texture into another:


let dst = Texture::new(
    instance,
    "blit-target",
    (1024, 1024),
    TextureFormat::Rgba8Unorm,
    TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
);
dst.copy_from_texture(instance, src, (1024, 1024));

Fields§

§label: String§texture: Texture§format: TextureFormat§view: TextureView§sampler: Sampler§size: (u32, u32)§sample_count: u32§layer_count: u32§mip_level_count: u32

Implementations§

Source§

impl Texture

Source

pub fn new( instance: &RenderInstanceData<'_>, label: &str, size: (u32, u32), format: TextureFormat, usage: TextureUsages, sample_count: u32, layer_count: u32, mip_level_count: u32, ) -> Self

Create a new texture.

§Arguments
  • instance - Game instance.
  • label - Label of the texture. This is only for debugging purposes.
  • size - Size of the texture (width, height).
  • format - Format of the texture (e.g. Rgba8Unorm, Depth32Float, etc.).
  • usage - Usage of the texture (e.g. RENDER_ATTACHMENT, COPY_SRC, COPY_DST, etc.).
  • sample_count - Sample count of the texture (e.g. 1 for no MSAA, 4 for 4x MSAA, etc.).
  • layer_count - Number of layers in the texture array. Default is 1 (for non-array textures).
  • mip_level_count - Number of mip levels. Default is 1 (no mipmaps). If set to 0, it will be auto-calculated based on the texture size.
Source

pub fn copy_from_buffer( &self, instance: &RenderInstanceData<'_>, texture_format: TextureFormat, buffer: &[u8], )

Copy buffer to texture. It is assumed that the buffer is the same size as the texture. It will be copied on the next queue submit. Note that the buffer must have the COPY_DST usage.

§Arguments
  • instance - Game instance.
  • texture_format - The wgpu texture format.
  • buffer - Image buffer.
Source

pub fn copy_from_buffer_layered( &self, instance: &RenderInstanceData<'_>, texture_format: TextureFormat, array_layer: u32, buffer: &[u8], )

Copy texture to buffer at a given array layer. It is assumed that the buffer is the same size as the texture. It will be copied on the next queue submit. Note that the buffer must have the COPY_DST usage.

§Arguments
  • instance - Game instance.
  • texture_format - The wgpu texture format.
  • array_layer - The array layer to copy from (for texture arrays). Default is 0 for non-array textures.
  • buffer - Image buffer.
Source

pub fn copy_from_texture( &self, instance: &RenderInstanceData<'_>, texture: &Texture, size: (u32, u32), )

Copy texture to texture. It is assumed that the texture is the same size as the source texture. Note that the input texture must have the COPY_SRC usage, and the output texture must have the COPY_DST usage.

§Arguments
  • instance - Game instance.
  • texture - Texture to copy from.
  • size - Size of the texture.
Source

pub fn copy_from_surface_texture( &self, instance: &RenderInstanceData<'_>, surface_texture: &SurfaceTexture, size: (u32, u32), )

Copy texture from a surface texture (e.g. swapchain frame). It is assumed that the texture is the same size as the source texture. Note that the input texture must have the COPY_SRC usage, and the output texture must have the COPY_DST usage.

§Arguments
  • instance - Game instance.
  • surface_texture - Surface texture to copy from.
  • size - Size of the texture.
Source

pub fn copy_from_texture_layered( &self, instance: &RenderInstanceData<'_>, texture: &Texture, array_layer: usize, size: (u32, u32), )

Copy texture to texture at a given array layer. It is assumed that the texture is the same size as the source texture. Note that the input texture must have the COPY_SRC usage, and the output texture must have the COPY_DST usage.

§Arguments
  • instance - Game instance.
  • texture - Texture to copy from.
  • array_layer - The array layer to copy to (for texture arrays). Default is 0 for non-array textures.
  • size - Size of the texture.
Source

pub fn generate_mipmaps(&self, instance: &RenderInstanceData<'_>)

Generate mipmaps for this texture. The texture must have been created with TEXTURE_BINDING | RENDER_ATTACHMENT | COPY_SRC usage. This method uses a simple blit approach to downsample each mip level from the previous one.

§Arguments
  • instance - Game instance.

Trait Implementations§

Source§

impl Debug for Texture

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> Downcast for T
where T: Any,

§

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

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

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

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

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

§

const WITNESS: W = W::MAKE

A constant of the type witness
§

impl<T> Identity for T
where T: ?Sized,

§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoResult<T> for T

§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
§

impl<A> Is for A
where A: Any,

§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. 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.
§

impl<T> Upcast<T> for T

§

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

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

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

§

impl<T> Settings for T
where T: 'static + Send + Sync,

§

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

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

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