pub struct Material { /* private fields */ }Expand description
A render pipeline asset, plus the bind group values (textures/samplers/
uniforms/storage buffers) it renders with — WGSL shader source and
fixed-function state (vertex layouts, cull mode, depth, targets) compile
into a wgpu::RenderPipeline; many Materials sharing the same shader
and fixed-function state automatically share one compiled pipeline (see
MaterialPipelineCache), so “the same shader, several different
uniform-value combinations” is just several Materials, not a separate
instance concept.
Bind group 0 is this material’s own — build it with the streamlined
per-binding calls (.texture(...)/.sampler(...)/.uniform_value(...)/etc.,
each declaring the entry and providing its value in one call, visible
to the fragment stage, binding index auto-assigned) for the common case,
or .with_entry(...)/.with_entry_at(...) plus the matching .with_texture(...)/etc.
value-only call when you need to override visibility, sample type, or the
binding index. .with_extra_group(...) appends group 1 and up — a
shared/global layout, most often.
Implementations§
Source§impl Material
impl Material
pub fn new(shader_source: &'static str) -> Self
Sourcepub fn standard(shader_source: &'static str) -> Self
pub fn standard(shader_source: &'static str) -> Self
Like new, but pre-filled with the common opaque-3D-geometry
defaults instead of leaving them empty: Vertex::layout() for
.with_vertex_layouts, a single opaque target in the real surface
format for .with_targets (rendering straight to the screen is the
common case this saves you from getting wrong — the surface format
varies by platform/backend, e.g. Bgra8Unorm is common on
Windows/DX12, not the Rgba8Unorm [DEFAULT_TARGET] assumes), and
DepthStencilState::DEFAULT for .with_depth. The surface format
is resolved against Backend at upload time, not here — standard
itself needs no Backend reference, same as new. Still a plain
builder — chain .with_vertex_layouts(...)/.with_targets(...)/
.with_depth(...)/.without_depth()/etc. afterwards to override
any of these for a material that doesn’t fit the common case (a
custom vertex type, an offscreen target with a different/blended
format, no depth test).
pub fn with_label(self, label: &'static str) -> Self
pub fn with_vertex_entry(self, entry: &'static str) -> Self
pub fn without_vertex_entry(self) -> Self
pub fn with_fragment_entry(self, entry: &'static str) -> Self
pub fn without_fragment_entry(self) -> Self
pub fn with_vertex_layouts(self, layouts: Vec<VertexBufferLayout>) -> Self
Sourcepub fn with_entry(self, name: &'static str, kind: BindingKind) -> Self
pub fn with_entry(self, name: &'static str, kind: BindingKind) -> Self
Declares one of this material’s own (group 0) bind group entries,
at the next auto-assigned binding index — the low-level counterpart
to the streamlined .texture(...)/.sampler(...)/etc. calls, for
when you need a kind one of those doesn’t produce (vertex/compute
visibility, a non-default sample type, a dynamic-offset buffer).
Pair it with the matching value-only .with_texture(...)/.with_sampler(...)/etc.
call.
Sourcepub fn with_entry_at(
self,
name: &'static str,
binding: u32,
kind: BindingKind,
) -> Self
pub fn with_entry_at( self, name: &'static str, binding: u32, kind: BindingKind, ) -> Self
Same as with_entry, pinning an explicit
binding index instead of auto-assigning the next one.
Sourcepub fn with_extra_group(self, group: GroupEntry) -> Self
pub fn with_extra_group(self, group: GroupEntry) -> Self
Appends a bind group beyond this material’s own (group 0) —
typically GroupEntry::Global, a layout shared with other
materials/computes via GlobalLayoutPool. Groups append in call
order, starting at group 1.
pub fn with_cull_mode(self, mode: Face) -> Self
pub fn without_cull_mode(self) -> Self
pub fn with_depth(self, depth: DepthStencilState) -> Self
pub fn without_depth(self) -> Self
pub fn with_targets(self, targets: Vec<ColorTargetState>) -> Self
pub fn with_polygon_mode(self, mode: PolygonMode) -> Self
pub fn with_sample_count(self, count: u32) -> Self
Sourcepub fn with_texture(self, name: &'static str, handle: Handle<Texture>) -> Self
pub fn with_texture(self, name: &'static str, handle: Handle<Texture>) -> Self
Binds a texture value against an entry declared separately (via
.with_entry(...)/.with_entry_at(...)) — for when .texture(...)’s
fragment-visible/filterable-float default isn’t right. Most
materials want .texture(...) instead.
Sourcepub fn with_texture_array(
self,
name: &'static str,
handle: Handle<TextureArray>,
) -> Self
pub fn with_texture_array( self, name: &'static str, handle: Handle<TextureArray>, ) -> Self
Value-only counterpart to .texture_array(...) — see .with_texture.
Sourcepub fn with_cubemap(self, name: &'static str, handle: Handle<Cubemap>) -> Self
pub fn with_cubemap(self, name: &'static str, handle: Handle<Cubemap>) -> Self
Value-only counterpart to .cubemap(...) — see .with_texture.
Sourcepub fn with_texture_view(self, name: &'static str, view: TextureView) -> Self
pub fn with_texture_view(self, name: &'static str, view: TextureView) -> Self
Binds an already-built TextureView directly — e.g. one mip level
from GPUTexture::get_view,
or a standalone render target from
Texture::empty. Unlike
.with_texture/.with_texture_array/.with_cubemap, no Handle
lookup happens at upload time — view must already exist.
Sourcepub fn with_sampler(self, name: &'static str, kind: SamplerKind) -> Self
pub fn with_sampler(self, name: &'static str, kind: SamplerKind) -> Self
Value-only counterpart to .sampler(...) — see .with_texture.
Sourcepub fn with_uniform(self, name: &'static str, data: Vec<u8>) -> Self
pub fn with_uniform(self, name: &'static str, data: Vec<u8>) -> Self
Value-only counterpart to .uniform(...) — see .with_texture.
Sourcepub fn with_storage(self, name: &'static str, data: Vec<u8>) -> Self
pub fn with_storage(self, name: &'static str, data: Vec<u8>) -> Self
Value-only counterpart to .storage(...) — see .with_texture.
Declares a read-write entry via .with_entry(...) if you need one;
.storage(...)’s streamlined default is read-only.
Sourcepub fn with_uniform_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
pub fn with_uniform_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
Same as with_uniform, but takes a typed
value instead of pre-packed bytes — uses encase to lay it out with
correct WGSL uniform (std140) alignment. Value-only counterpart to
.uniform_value(...).
Sourcepub fn with_storage_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
pub fn with_storage_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
Same as with_storage, but takes a typed
value instead of pre-packed bytes — uses encase to lay it out with
correct WGSL storage (std430) alignment. Value-only counterpart to
.storage_value(...).
Sourcepub fn texture(self, name: &'static str, handle: Handle<Texture>) -> Self
pub fn texture(self, name: &'static str, handle: Handle<Texture>) -> Self
Declares a fragment-visible texture_2d<f32> entry at the next
auto-assigned binding index and binds handle to it — the
streamlined one-call form of .with_entry(name, BindingKind::texture_2d(FRAGMENT))
followed by .with_texture(name, handle). Reach for those two
directly instead when you need vertex/compute visibility, a
non-default sample type, or an explicit binding index.
Sourcepub fn texture_array(
self,
name: &'static str,
handle: Handle<TextureArray>,
) -> Self
pub fn texture_array( self, name: &'static str, handle: Handle<TextureArray>, ) -> Self
Streamlined form of .texture_array(...) — see texture.
Sourcepub fn cubemap(self, name: &'static str, handle: Handle<Cubemap>) -> Self
pub fn cubemap(self, name: &'static str, handle: Handle<Cubemap>) -> Self
Streamlined form of .cubemap(...) — see texture.
Sourcepub fn sampler(self, name: &'static str, kind: SamplerKind) -> Self
pub fn sampler(self, name: &'static str, kind: SamplerKind) -> Self
Streamlined form of .sampler(...) — see texture.
Sourcepub fn uniform(self, name: &'static str, data: Vec<u8>) -> Self
pub fn uniform(self, name: &'static str, data: Vec<u8>) -> Self
Streamlined form of .uniform(...) — see texture.
Sourcepub fn storage(self, name: &'static str, data: Vec<u8>) -> Self
pub fn storage(self, name: &'static str, data: Vec<u8>) -> Self
Streamlined form of .storage(...) (read-only) — see texture.
Sourcepub fn uniform_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
pub fn uniform_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
Streamlined, typed form of .uniform(...) — declares the entry and
binds an encase-laid-out value in one call. See texture.
Sourcepub fn storage_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
pub fn storage_value<T>(self, name: &'static str, value: &T) -> Selfwhere
T: ShaderType + WriteInto,
Streamlined, typed form of .storage(...) (read-only) — see texture.
Sourcepub fn with_buffer(self, name: &'static str, buffer: Buffer) -> Self
pub fn with_buffer(self, name: &'static str, buffer: Buffer) -> Self
Binds an existing Buffer instead of uploading raw bytes — for a
buffer you already built yourself (e.g. one a compute pass writes
to, then this material reads from). Unlike .with_uniform/.with_storage,
no buffer is created here; buffer must already carry the usage
flags this binding needs.
Sourcepub fn with_dynamic_buffer(
self,
name: &'static str,
buffer: DynamicBuffer,
) -> Self
pub fn with_dynamic_buffer( self, name: &'static str, buffer: DynamicBuffer, ) -> Self
Binds an existing DynamicBuffer — the dynamic-offset counterpart
to .with_buffer.
pub fn with_param(self, name: &'static str, entry: BindingValue) -> Self
pub fn build_asset( self, name: &str, assets: &mut Assets<Material>, ) -> Handle<Material>
Trait Implementations§
Source§impl Asset<Backend> for Material
impl Asset<Backend> for Material
type Deps<'a> = (Read<'a, GlobalLayoutPool>, Read<'a, MaterialPipelineCache>, Read<'a, Assets<Texture>>, Read<'a, Assets<TextureArray>>, Read<'a, Assets<Cubemap>>, Read<'a, GlobalSamplers>)
fn upload<'a>( &self, backend: &Backend, deps: &Self::Deps<'a>, ) -> Option<GPUMaterial>
Source§impl AssetSource for Material
impl AssetSource for Material
type Processed = GPUMaterial
Auto Trait Implementations§
impl !RefUnwindSafe for Material
impl !UnwindSafe for Material
impl Freeze for Material
impl Send for Material
impl Sync for Material
impl Unpin for Material
impl UnsafeUnpin for Material
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> Component for 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.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().