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    Cubemap(RawAssetHandle),
10    Sampler(SamplerKind),
11    Float(f32),
12    Vec4([f32; 4]),
13    /// Escape hatch: raw bytes for anything not covered above (e.g. a
14    /// custom struct of several packed values).
15    Bytes(Vec<u8>),
16}
17
18/// One instance's full set of parameter values, by name. Resolve each
19/// name against the base material's `MaterialDescriptor::binding_index`
20/// when building the actual bind group, in your own `Asset::upload`.
21pub struct MaterialInstanceDescriptor {
22    pub material: RawAssetHandle,
23    pub params: Vec<(&'static str, ParamValue)>,
24}
25
26impl MaterialInstanceDescriptor {
27    pub fn new(material: RawAssetHandle) -> Self {
28        Self {
29            material,
30            params: Vec::new(),
31        }
32    }
33
34    pub fn with_texture(mut self, name: &'static str, handle: RawAssetHandle) -> Self {
35        self.params.push((name, ParamValue::Texture(handle)));
36        self
37    }
38    pub fn with_cubemap(mut self, name: &'static str, handle: RawAssetHandle) -> Self {
39        self.params.push((name, ParamValue::Cubemap(handle)));
40        self
41    }
42    pub fn with_sampler(mut self, name: &'static str, kind: SamplerKind) -> Self {
43        self.params.push((name, ParamValue::Sampler(kind)));
44        self
45    }
46    pub fn with_float(mut self, name: &'static str, value: f32) -> Self {
47        self.params.push((name, ParamValue::Float(value)));
48        self
49    }
50    pub fn with_vec4(mut self, name: &'static str, value: [f32; 4]) -> Self {
51        self.params.push((name, ParamValue::Vec4(value)));
52        self
53    }
54
55    pub fn with_bytes(mut self, name: &'static str, bytes: Vec<u8>) -> Self {
56        self.params.push((name, ParamValue::Bytes(bytes)));
57        self
58    }
59}