bevy_render/render_resource/
pipeline.rs

1use super::ShaderDefVal;
2use crate::mesh::VertexBufferLayout;
3use crate::renderer::WgpuWrapper;
4use crate::{
5    define_atomic_id,
6    render_resource::{BindGroupLayout, Shader},
7};
8use alloc::borrow::Cow;
9use alloc::sync::Arc;
10use bevy_asset::Handle;
11use core::ops::Deref;
12use wgpu::{
13    ColorTargetState, DepthStencilState, MultisampleState, PrimitiveState, PushConstantRange,
14};
15
16define_atomic_id!(RenderPipelineId);
17
18/// A [`RenderPipeline`] represents a graphics pipeline and its stages (shaders), bindings and vertex buffers.
19///
20/// May be converted from and dereferences to a wgpu [`RenderPipeline`](wgpu::RenderPipeline).
21/// Can be created via [`RenderDevice::create_render_pipeline`](crate::renderer::RenderDevice::create_render_pipeline).
22#[derive(Clone, Debug)]
23pub struct RenderPipeline {
24    id: RenderPipelineId,
25    value: Arc<WgpuWrapper<wgpu::RenderPipeline>>,
26}
27
28impl RenderPipeline {
29    #[inline]
30    pub fn id(&self) -> RenderPipelineId {
31        self.id
32    }
33}
34
35impl From<wgpu::RenderPipeline> for RenderPipeline {
36    fn from(value: wgpu::RenderPipeline) -> Self {
37        RenderPipeline {
38            id: RenderPipelineId::new(),
39            value: Arc::new(WgpuWrapper::new(value)),
40        }
41    }
42}
43
44impl Deref for RenderPipeline {
45    type Target = wgpu::RenderPipeline;
46
47    #[inline]
48    fn deref(&self) -> &Self::Target {
49        &self.value
50    }
51}
52
53define_atomic_id!(ComputePipelineId);
54
55/// A [`ComputePipeline`] represents a compute pipeline and its single shader stage.
56///
57/// May be converted from and dereferences to a wgpu [`ComputePipeline`](wgpu::ComputePipeline).
58/// Can be created via [`RenderDevice::create_compute_pipeline`](crate::renderer::RenderDevice::create_compute_pipeline).
59#[derive(Clone, Debug)]
60pub struct ComputePipeline {
61    id: ComputePipelineId,
62    value: Arc<WgpuWrapper<wgpu::ComputePipeline>>,
63}
64
65impl ComputePipeline {
66    /// Returns the [`ComputePipelineId`].
67    #[inline]
68    pub fn id(&self) -> ComputePipelineId {
69        self.id
70    }
71}
72
73impl From<wgpu::ComputePipeline> for ComputePipeline {
74    fn from(value: wgpu::ComputePipeline) -> Self {
75        ComputePipeline {
76            id: ComputePipelineId::new(),
77            value: Arc::new(WgpuWrapper::new(value)),
78        }
79    }
80}
81
82impl Deref for ComputePipeline {
83    type Target = wgpu::ComputePipeline;
84
85    #[inline]
86    fn deref(&self) -> &Self::Target {
87        &self.value
88    }
89}
90
91/// Describes a render (graphics) pipeline.
92#[derive(Clone, Debug, PartialEq)]
93pub struct RenderPipelineDescriptor {
94    /// Debug label of the pipeline. This will show up in graphics debuggers for easy identification.
95    pub label: Option<Cow<'static, str>>,
96    /// The layout of bind groups for this pipeline.
97    pub layout: Vec<BindGroupLayout>,
98    /// The push constant ranges for this pipeline.
99    /// Supply an empty vector if the pipeline doesn't use push constants.
100    pub push_constant_ranges: Vec<PushConstantRange>,
101    /// The compiled vertex stage, its entry point, and the input buffers layout.
102    pub vertex: VertexState,
103    /// The properties of the pipeline at the primitive assembly and rasterization level.
104    pub primitive: PrimitiveState,
105    /// The effect of draw calls on the depth and stencil aspects of the output target, if any.
106    pub depth_stencil: Option<DepthStencilState>,
107    /// The multi-sampling properties of the pipeline.
108    pub multisample: MultisampleState,
109    /// The compiled fragment stage, its entry point, and the color targets.
110    pub fragment: Option<FragmentState>,
111    /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true.
112    /// If this is false, reading from workgroup variables before writing to them will result in garbage values.
113    pub zero_initialize_workgroup_memory: bool,
114}
115
116#[derive(Clone, Debug, Eq, PartialEq)]
117pub struct VertexState {
118    /// The compiled shader module for this stage.
119    pub shader: Handle<Shader>,
120    pub shader_defs: Vec<ShaderDefVal>,
121    /// The name of the entry point in the compiled shader. There must be a
122    /// function with this name in the shader.
123    pub entry_point: Cow<'static, str>,
124    /// The format of any vertex buffers used with this pipeline.
125    pub buffers: Vec<VertexBufferLayout>,
126}
127
128/// Describes the fragment process in a render pipeline.
129#[derive(Clone, Debug, PartialEq, Eq)]
130pub struct FragmentState {
131    /// The compiled shader module for this stage.
132    pub shader: Handle<Shader>,
133    pub shader_defs: Vec<ShaderDefVal>,
134    /// The name of the entry point in the compiled shader. There must be a
135    /// function with this name in the shader.
136    pub entry_point: Cow<'static, str>,
137    /// The color state of the render targets.
138    pub targets: Vec<Option<ColorTargetState>>,
139}
140
141/// Describes a compute pipeline.
142#[derive(Clone, Debug)]
143pub struct ComputePipelineDescriptor {
144    pub label: Option<Cow<'static, str>>,
145    pub layout: Vec<BindGroupLayout>,
146    pub push_constant_ranges: Vec<PushConstantRange>,
147    /// The compiled shader module for this stage.
148    pub shader: Handle<Shader>,
149    pub shader_defs: Vec<ShaderDefVal>,
150    /// The name of the entry point in the compiled shader. There must be a
151    /// function with this name in the shader.
152    pub entry_point: Cow<'static, str>,
153    /// Whether to zero-initialize workgroup memory by default. If you're not sure, set this to true.
154    /// If this is false, reading from workgroup variables before writing to them will result in garbage values.
155    pub zero_initialize_workgroup_memory: bool,
156}