Skip to main content

wde_renderer/passes/
pipeline_types.rs

1use bevy::{asset::Handle, ecs::prelude::*};
2use wde_wgpu::{
3    bind_group::BindGroupLayout,
4    pipelines::{BlendState, ColorWrites},
5    render_pipeline::{DepthDescriptor, Face, RenderTopology, ShaderStages},
6    texture::TextureFormat
7};
8
9use crate::assets::Shader;
10
11/// Describes a push constant that will be available to a shader.
12/// Note: the size of the push constant must be a multiple of 4 and must not exceed 128 bytes.
13#[derive(Clone)]
14pub struct PushConstantDescriptor {
15    /// Shader stages that can read the push constant.
16    pub stages: ShaderStages,
17    /// Byte offset from the start of the push constant buffer.
18    pub offset: u32,
19    /// Size in bytes (multiple of 4, up to 128).
20    pub size: u32
21}
22
23/// Describes a render pipeline, including its shaders, resources and states.
24#[derive(Resource, Clone)]
25pub struct RenderPipelineDescriptor {
26    pub label: &'static str,
27    pub vert: Option<Handle<Shader>>,
28    pub frag: Option<Handle<Shader>>,
29
30    /// Depth/stencil state for the pipeline.
31    pub depth: DepthDescriptor,
32    /// Formats of the render targets this pipeline will render to. If `None`, the pipeline will render to the swapchain format.
33    pub render_targets: Option<Vec<TextureFormat>>,
34
35    /// Bind group layouts describing all resource bindings.
36    pub bind_group_layouts: Vec<Option<BindGroupLayout>>,
37    /// Push constant ranges exposed to shaders.
38    pub push_constants: Vec<PushConstantDescriptor>,
39
40    /// Primitive topology (default: TriangleList).
41    pub topology: RenderTopology,
42    /// Face culling mode (default: Back). `None` disables culling.
43    pub cull_mode: Option<Face>,
44    /// Blend state for the fragment shader (default: None, i.e. no blending).
45    pub fragment_blend: Option<BlendState>,
46    /// Color write mask for fragment outputs (default: all channels). This enable you to disable writing to certain color channels.
47    pub color_write: ColorWrites,
48    /// The sample count for multisampling (default: 1).
49    pub sample_count: u32,
50    /// Whether the pipeline should expect a vertex buffer. Useful for pipelines that render fullscreen quads without vertex buffers (default: true).
51    pub vertex_buffer: bool
52}
53impl Default for RenderPipelineDescriptor {
54    fn default() -> Self {
55        Self {
56            label: "Unknown Render Pipeline",
57            vert: None,
58            frag: None,
59            depth: DepthDescriptor::default(),
60            render_targets: None,
61            bind_group_layouts: vec![],
62            push_constants: vec![],
63            topology: RenderTopology::TriangleList,
64            cull_mode: Some(Face::Back),
65            fragment_blend: Some(BlendState::REPLACE),
66            color_write: ColorWrites::ALL,
67            sample_count: 1,
68            vertex_buffer: true
69        }
70    }
71}
72
73/// Describes a compute pipeline, including its shader and resources.
74#[derive(Resource, Clone)]
75pub struct ComputePipelineDescriptor {
76    pub label: &'static str,
77    pub comp: Option<Handle<Shader>>,
78
79    /// Bind group layouts describing all resource bindings.
80    pub bind_group_layouts: Vec<Option<BindGroupLayout>>,
81    /// Push constant ranges exposed to the compute shader.
82    pub push_constants: Vec<PushConstantDescriptor>
83}
84impl Default for ComputePipelineDescriptor {
85    fn default() -> Self {
86        Self {
87            label: "Unknown Compute Pipeline",
88            comp: None,
89            bind_group_layouts: vec![],
90            push_constants: vec![]
91        }
92    }
93}