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 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#[derive(Clone, Debug, PartialEq, Default)]
30pub struct RenderPipelineDescriptor {
31 pub label: Option<Cow<'static, str>>,
33 pub layout: Vec<BindGroupLayoutDescriptor>,
35 pub immediate_size: u32,
38 pub vertex: VertexState,
40 pub primitive: PrimitiveState,
42 pub depth_stencil: Option<DepthStencilState>,
44 pub multisample: MultisampleState,
46 pub fragment: Option<FragmentState>,
48 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 pub shader: Handle<Shader>,
71 pub shader_defs: Vec<ShaderDefVal>,
72 pub entry_point: Option<Cow<'static, str>>,
75 pub buffers: Vec<VertexBufferLayout>,
77}
78
79#[derive(Clone, Debug, PartialEq, Eq, Default)]
81pub struct FragmentState {
82 pub shader: Handle<Shader>,
84 pub shader_defs: Vec<ShaderDefVal>,
85 pub entry_point: Option<Cow<'static, str>>,
88 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#[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 pub shader: Handle<Shader>,
106 pub shader_defs: Vec<ShaderDefVal>,
107 pub entry_point: Option<Cow<'static, str>>,
110 pub zero_initialize_workgroup_memory: bool,
113}
114
115fn 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#[derive(Debug)]
127pub enum PipelineDescriptor {
128 RenderPipelineDescriptor(Box<RenderPipelineDescriptor>),
129 ComputePipelineDescriptor(Box<ComputePipelineDescriptor>),
130}
131
132#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Deref)]
134pub struct CachedRenderPipelineId(CachedPipelineId);
135
136impl CachedRenderPipelineId {
137 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#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
153pub struct CachedComputePipelineId(CachedPipelineId);
154
155impl CachedComputePipelineId {
156 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}