bevy_render/render_resource/
pipeline.rs1use super::ShaderDefVal;
2use crate::{
3 define_atomic_id,
4 render_resource::{resource_macros::render_resource_wrapper, BindGroupLayout, Shader},
5};
6use bevy_asset::Handle;
7use std::{borrow::Cow, ops::Deref};
8use wgpu::{
9 BufferAddress, ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState,
10 PushConstantRange, VertexAttribute, VertexFormat, VertexStepMode,
11};
12
13define_atomic_id!(RenderPipelineId);
14render_resource_wrapper!(ErasedRenderPipeline, wgpu::RenderPipeline);
15
16#[derive(Clone, Debug)]
21pub struct RenderPipeline {
22 id: RenderPipelineId,
23 value: ErasedRenderPipeline,
24}
25
26impl RenderPipeline {
27 #[inline]
28 pub fn id(&self) -> RenderPipelineId {
29 self.id
30 }
31}
32
33impl From<wgpu::RenderPipeline> for RenderPipeline {
34 fn from(value: wgpu::RenderPipeline) -> Self {
35 RenderPipeline {
36 id: RenderPipelineId::new(),
37 value: ErasedRenderPipeline::new(value),
38 }
39 }
40}
41
42impl Deref for RenderPipeline {
43 type Target = wgpu::RenderPipeline;
44
45 #[inline]
46 fn deref(&self) -> &Self::Target {
47 &self.value
48 }
49}
50
51define_atomic_id!(ComputePipelineId);
52render_resource_wrapper!(ErasedComputePipeline, wgpu::ComputePipeline);
53
54#[derive(Clone, Debug)]
59pub struct ComputePipeline {
60 id: ComputePipelineId,
61 value: ErasedComputePipeline,
62}
63
64impl ComputePipeline {
65 #[inline]
67 pub fn id(&self) -> ComputePipelineId {
68 self.id
69 }
70}
71
72impl From<wgpu::ComputePipeline> for ComputePipeline {
73 fn from(value: wgpu::ComputePipeline) -> Self {
74 ComputePipeline {
75 id: ComputePipelineId::new(),
76 value: ErasedComputePipeline::new(value),
77 }
78 }
79}
80
81impl Deref for ComputePipeline {
82 type Target = wgpu::ComputePipeline;
83
84 #[inline]
85 fn deref(&self) -> &Self::Target {
86 &self.value
87 }
88}
89
90#[derive(Clone, Debug, PartialEq)]
92pub struct RenderPipelineDescriptor {
93 pub label: Option<Cow<'static, str>>,
95 pub layout: Vec<BindGroupLayout>,
97 pub push_constant_ranges: Vec<PushConstantRange>,
100 pub vertex: VertexState,
102 pub primitive: PrimitiveState,
104 pub depth_stencil: Option<DepthStencilState>,
106 pub multisample: MultisampleState,
108 pub fragment: Option<FragmentState>,
110}
111
112#[derive(Clone, Debug, Eq, PartialEq)]
113pub struct VertexState {
114 pub shader: Handle<Shader>,
116 pub shader_defs: Vec<ShaderDefVal>,
117 pub entry_point: Cow<'static, str>,
120 pub buffers: Vec<VertexBufferLayout>,
122}
123
124#[derive(Default, Clone, Debug, Hash, Eq, PartialEq)]
126pub struct VertexBufferLayout {
127 pub array_stride: BufferAddress,
129 pub step_mode: VertexStepMode,
131 pub attributes: Vec<VertexAttribute>,
133}
134
135impl VertexBufferLayout {
136 pub fn from_vertex_formats<T: IntoIterator<Item = VertexFormat>>(
141 step_mode: VertexStepMode,
142 vertex_formats: T,
143 ) -> Self {
144 let mut offset = 0;
145 let mut attributes = Vec::new();
146 for (shader_location, format) in vertex_formats.into_iter().enumerate() {
147 attributes.push(VertexAttribute {
148 format,
149 offset,
150 shader_location: shader_location as u32,
151 });
152 offset += format.size();
153 }
154
155 VertexBufferLayout {
156 array_stride: offset,
157 step_mode,
158 attributes,
159 }
160 }
161}
162
163#[derive(Clone, Debug, PartialEq, Eq)]
165pub struct FragmentState {
166 pub shader: Handle<Shader>,
168 pub shader_defs: Vec<ShaderDefVal>,
169 pub entry_point: Cow<'static, str>,
172 pub targets: Vec<Option<ColorTargetState>>,
174}
175
176#[derive(Clone, Debug)]
178pub struct ComputePipelineDescriptor {
179 pub label: Option<Cow<'static, str>>,
180 pub layout: Vec<BindGroupLayout>,
181 pub push_constant_ranges: Vec<PushConstantRange>,
182 pub shader: Handle<Shader>,
184 pub shader_defs: Vec<ShaderDefVal>,
185 pub entry_point: Cow<'static, str>,
188}