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