Skip to main content

pebble/wgpu/
mod.rs

1pub mod cubemap;
2pub mod material;
3pub mod material_instance;
4pub mod samplers;
5pub mod textures;
6
7pub struct BindingEntry {
8    pub name: &'static str,
9    pub kind: BindingKind,
10}
11
12pub enum BindingKind {
13    Texture,
14    /// `texture_2d_array<f32>` in WGSL — a texture with multiple layers
15    /// sampled by index, e.g. a texture atlas or a per-instance layer
16    /// lookup. Bind the whole array as one resource; select the layer in
17    /// the shader.
18    TextureArray,
19    Sampler,
20    /// `sampler_comparison` in WGSL — a sampler used with
21    /// `textureSampleCompare`, e.g. shadow-map testing. Distinct from a
22    /// regular [`Sampler`](BindingKind::Sampler) binding at the wgpu level
23    /// (`SamplerBindingType::Comparison` vs `Filtering`); pair with a
24    /// [`SamplerKind`](crate::wgpu::samplers::SamplerKind) comparison
25    /// variant (e.g. `CompareLess`) when building the actual sampler.
26    ComparisonSampler,
27    Buffer,
28    TextureCubemap,
29}
30
31impl BindingEntry {
32    pub fn texture(name: &'static str) -> Self {
33        Self {
34            name,
35            kind: BindingKind::Texture,
36        }
37    }
38
39    pub fn sampler(name: &'static str) -> Self {
40        Self {
41            name,
42            kind: BindingKind::Sampler,
43        }
44    }
45
46    pub fn buffer(name: &'static str) -> Self {
47        Self {
48            name,
49            kind: BindingKind::Buffer,
50        }
51    }
52
53    pub fn cubemap(name: &'static str) -> Self {
54        Self {
55            name,
56            kind: BindingKind::TextureCubemap,
57        }
58    }
59
60    pub fn texture_array(name: &'static str) -> Self {
61        Self {
62            name,
63            kind: BindingKind::TextureArray,
64        }
65    }
66
67    pub fn comparison_sampler(name: &'static str) -> Self {
68        Self {
69            name,
70            kind: BindingKind::ComparisonSampler,
71        }
72    }
73
74    pub fn layout_entry(&self, binding: u32) -> wgpu::BindGroupLayoutEntry {
75        match self.kind {
76            BindingKind::Texture => wgpu::BindGroupLayoutEntry {
77                binding,
78                visibility: wgpu::ShaderStages::FRAGMENT,
79                ty: wgpu::BindingType::Texture {
80                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
81                    view_dimension: wgpu::TextureViewDimension::D2,
82                    multisampled: false,
83                },
84                count: None,
85            },
86            BindingKind::Sampler => wgpu::BindGroupLayoutEntry {
87                binding,
88                visibility: wgpu::ShaderStages::FRAGMENT,
89                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
90                count: None,
91            },
92            BindingKind::Buffer => wgpu::BindGroupLayoutEntry {
93                binding,
94                visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
95                ty: wgpu::BindingType::Buffer {
96                    ty: wgpu::BufferBindingType::Uniform,
97                    has_dynamic_offset: false,
98                    min_binding_size: None,
99                },
100                count: None,
101            },
102            BindingKind::TextureCubemap => wgpu::BindGroupLayoutEntry {
103                binding,
104                visibility: wgpu::ShaderStages::FRAGMENT,
105                ty: wgpu::BindingType::Texture {
106                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
107                    view_dimension: wgpu::TextureViewDimension::Cube,
108                    multisampled: false,
109                },
110                count: None,
111            },
112            BindingKind::TextureArray => wgpu::BindGroupLayoutEntry {
113                binding,
114                visibility: wgpu::ShaderStages::FRAGMENT,
115                ty: wgpu::BindingType::Texture {
116                    sample_type: wgpu::TextureSampleType::Float { filterable: true },
117                    view_dimension: wgpu::TextureViewDimension::D2Array,
118                    multisampled: false,
119                },
120                count: None,
121            },
122            BindingKind::ComparisonSampler => wgpu::BindGroupLayoutEntry {
123                binding,
124                visibility: wgpu::ShaderStages::FRAGMENT,
125                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
126                count: None,
127            },
128        }
129    }
130}