Skip to main content

pebble/wgpu/
material_instance.rs

1use crate::{assets::storage::RawAssetHandle, wgpu::samplers::SamplerKind};
2
3/// The small, closed vocabulary of value kinds a material instance
4/// parameter can be. Arrays instead of a math-library type so this
5/// module has no dependency beyond wgpu itself — convert to/from your
6/// own vector type at the call site.
7pub enum ParamValue {
8    Texture(RawAssetHandle),
9    /// Handle to a texture array asset — bound as one `texture_2d_array<f32>`
10    /// resource. Pairs with [`BindingEntry::texture_array`](crate::wgpu::BindingEntry::texture_array).
11    TextureArray(RawAssetHandle),
12    Cubemap(RawAssetHandle),
13    Sampler(SamplerKind),
14    Float(f32),
15    Vec4([f32; 4]),
16    /// Escape hatch: raw bytes for anything not covered above (e.g. a
17    /// custom struct of several packed values).
18    Bytes(Vec<u8>),
19}
20
21/// One instance's full set of parameter values, by name. Resolve each
22/// name against the base material's `MaterialDescriptor::binding_index`
23/// when building the actual bind group, in your own `Asset::upload`.
24pub struct MaterialInstanceDescriptor {
25    pub material: RawAssetHandle,
26    pub params: Vec<(&'static str, ParamValue)>,
27}
28
29impl MaterialInstanceDescriptor {
30    pub fn new(material: RawAssetHandle) -> Self {
31        Self {
32            material,
33            params: Vec::new(),
34        }
35    }
36
37    pub fn with_texture(mut self, name: &'static str, handle: RawAssetHandle) -> Self {
38        self.params.push((name, ParamValue::Texture(handle)));
39        self
40    }
41    pub fn with_texture_array(mut self, name: &'static str, handle: RawAssetHandle) -> Self {
42        self.params.push((name, ParamValue::TextureArray(handle)));
43        self
44    }
45    pub fn with_cubemap(mut self, name: &'static str, handle: RawAssetHandle) -> Self {
46        self.params.push((name, ParamValue::Cubemap(handle)));
47        self
48    }
49    pub fn with_sampler(mut self, name: &'static str, kind: SamplerKind) -> Self {
50        self.params.push((name, ParamValue::Sampler(kind)));
51        self
52    }
53    pub fn with_float(mut self, name: &'static str, value: f32) -> Self {
54        self.params.push((name, ParamValue::Float(value)));
55        self
56    }
57    pub fn with_vec4(mut self, name: &'static str, value: [f32; 4]) -> Self {
58        self.params.push((name, ParamValue::Vec4(value)));
59        self
60    }
61
62    pub fn with_bytes(mut self, name: &'static str, bytes: Vec<u8>) -> Self {
63        self.params.push((name, ParamValue::Bytes(bytes)));
64        self
65    }
66}