Skip to main content

bevy_material/
descriptor.rs

1use alloc::borrow::Cow;
2use bevy_asset::Handle;
3use bevy_derive::Deref;
4use bevy_mesh::VertexBufferLayout;
5use bevy_shader::{CachedPipelineId, Shader, ShaderDefVal};
6use core::iter;
7use thiserror::Error;
8use wgpu_types::{
9    BindGroupLayoutEntry, ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState,
10};
11
12#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
13pub struct BindGroupLayoutDescriptor {
14    /// Debug label of the bind group layout descriptor. This will show up in graphics debuggers for easy identification.
15    pub label: Cow<'static, str>,
16    pub entries: Vec<BindGroupLayoutEntry>,
17}
18
19impl BindGroupLayoutDescriptor {
20    pub fn new(label: impl Into<Cow<'static, str>>, entries: &[BindGroupLayoutEntry]) -> Self {
21        Self {
22            label: label.into(),
23            entries: entries.into(),
24        }
25    }
26}
27
28/// Describes a render (graphics) pipeline.
29#[derive(Clone, Debug, PartialEq, Default)]
30pub struct RenderPipelineDescriptor {
31    /// Debug label of the pipeline. This will show up in graphics debuggers for easy identification.
32    pub label: Option<Cow<'static, str>>,
33    /// The layout of bind groups for this pipeline.
34    pub layout: Vec<BindGroupLayoutDescriptor>,
35    /// The immediate size for this pipeline.
36    /// Supply 0 if the pipeline doesn't use push constants/immediates.
37    pub immediate_size: u32,
38    /// The compiled vertex stage, its entry point, and the input buffers layout.
39    pub vertex: VertexState,
40    /// The properties of the pipeline at the primitive assembly and rasterization level.
41    pub primitive: PrimitiveState,
42    /// The effect of draw calls on the depth and stencil aspects of the output target, if any.
43    pub depth_stencil: Option<DepthStencilState>,
44    /// The multi-sampling properties of the pipeline.
45    pub multisample: MultisampleState,
46    /// The compiled fragment stage, its entry point, and the color targets.
47    pub fragment: Option<FragmentState>,
48    /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true.
49    /// If this is false, reading from workgroup variables before writing to them will result in garbage values.
50    pub zero_initialize_workgroup_memory: bool,
51}
52
53#[derive(Copy, Clone, Debug, Error)]
54#[error("RenderPipelineDescriptor has no FragmentState configured")]
55pub struct NoFragmentStateError;
56
57impl RenderPipelineDescriptor {
58    pub fn fragment_mut(&mut self) -> Result<&mut FragmentState, NoFragmentStateError> {
59        self.fragment.as_mut().ok_or(NoFragmentStateError)
60    }
61
62    pub fn set_layout(&mut self, index: usize, layout: BindGroupLayoutDescriptor) {
63        filling_set_at(&mut self.layout, index, bevy_utils::default(), layout);
64    }
65}
66
67#[derive(Clone, Debug, Eq, PartialEq, Default)]
68pub struct VertexState {
69    /// The compiled shader module for this stage.
70    pub shader: Handle<Shader>,
71    pub shader_defs: Vec<ShaderDefVal>,
72    /// The name of the entry point in the compiled shader, or `None` if the default entry point
73    /// is used.
74    pub entry_point: Option<Cow<'static, str>>,
75    /// The format of any vertex buffers used with this pipeline.
76    pub buffers: Vec<VertexBufferLayout>,
77}
78
79/// Describes the fragment process in a render pipeline.
80#[derive(Clone, Debug, PartialEq, Eq, Default)]
81pub struct FragmentState {
82    /// The compiled shader module for this stage.
83    pub shader: Handle<Shader>,
84    pub shader_defs: Vec<ShaderDefVal>,
85    /// The name of the entry point in the compiled shader, or `None` if the default entry point
86    /// is used.
87    pub entry_point: Option<Cow<'static, str>>,
88    /// The color state of the render targets.
89    pub targets: Vec<Option<ColorTargetState>>,
90}
91
92impl FragmentState {
93    pub fn set_target(&mut self, index: usize, target: ColorTargetState) {
94        filling_set_at(&mut self.targets, index, None, Some(target));
95    }
96}
97
98/// Describes a compute pipeline.
99#[derive(Clone, Debug, PartialEq, Eq, Default)]
100pub struct ComputePipelineDescriptor {
101    pub label: Option<Cow<'static, str>>,
102    pub layout: Vec<BindGroupLayoutDescriptor>,
103    pub immediate_size: u32,
104    /// The compiled shader module for this stage.
105    pub shader: Handle<Shader>,
106    pub shader_defs: Vec<ShaderDefVal>,
107    /// The name of the entry point in the compiled shader, or `None` if the default entry point
108    /// is used.
109    pub entry_point: Option<Cow<'static, str>>,
110    /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true.
111    /// If this is false, reading from workgroup variables before writing to them will result in garbage values.
112    pub zero_initialize_workgroup_memory: bool,
113}
114
115// utility function to set a value at the specified index, extending with
116// a filler value if the index is out of bounds.
117fn filling_set_at<T: Clone>(vec: &mut Vec<T>, index: usize, filler: T, value: T) {
118    let num_to_fill = (index + 1).saturating_sub(vec.len());
119    vec.extend(iter::repeat_n(filler, num_to_fill));
120    vec[index] = value;
121}
122
123/// A descriptor for a [`Pipeline`](https://docs.rs/bevy/latest/bevy/render/render_resource/enum.Pipeline.html).
124///
125/// Used to store a heterogenous collection of render and compute pipeline descriptors together.
126#[derive(Debug)]
127pub enum PipelineDescriptor {
128    RenderPipelineDescriptor(Box<RenderPipelineDescriptor>),
129    ComputePipelineDescriptor(Box<ComputePipelineDescriptor>),
130}
131
132/// Index of a cached render pipeline in a `PipelineCache`.
133#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Deref)]
134pub struct CachedRenderPipelineId(CachedPipelineId);
135
136impl CachedRenderPipelineId {
137    /// An invalid cached render pipeline index, often used to initialize a variable.
138    pub const INVALID: Self = CachedRenderPipelineId(usize::MAX);
139
140    #[inline]
141    pub fn new(id: usize) -> Self {
142        Self(id)
143    }
144
145    #[inline]
146    pub fn id(&self) -> usize {
147        self.0
148    }
149}
150
151/// Index of a cached compute pipeline in a `PipelineCache`.
152#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
153pub struct CachedComputePipelineId(CachedPipelineId);
154
155impl CachedComputePipelineId {
156    /// An invalid cached compute pipeline index, often used to initialize a variable.
157    pub const INVALID: Self = CachedComputePipelineId(usize::MAX);
158
159    #[inline]
160    pub fn new(id: usize) -> Self {
161        Self(id)
162    }
163
164    #[inline]
165    pub fn id(&self) -> usize {
166        self.0
167    }
168}