Skip to main content

ursus_core/vulkan/gfx_pipeline/
pipeline.rs

1use crate::render::gfx::types::{BlendState, CompareOp, CullMode, Format, PushConstantRange, VertexLayout};
2use ash::vk;
3
4/// Descriptor for building a graphics pipeline via [`ursus_core::render::gfx::PipelineCache::create_graphics_pipeline`].
5pub struct PipelineDesc<'a> {
6    pub(crate) vert_spv: &'a [u8],
7    pub(crate) frag_spv: &'a [u8],
8    pub(crate) color_formats: &'a [Format],
9    pub(crate) vertex_layout: &'a VertexLayout,
10    pub(crate) push_constant_ranges: &'a [PushConstantRange],
11    pub(crate) depth: DepthState,
12    pub(crate) cull_mode: CullMode,
13    pub(crate) blend_attachments: Option<&'a [BlendState]>,
14}
15
16#[derive(Clone, Copy)]
17pub(crate) struct DepthState {
18    pub format: Option<Format>,
19    pub test: bool,
20    pub write: bool,
21    pub compare: CompareOp,
22}
23
24impl<'a> PipelineDesc<'a> {
25    /// Creates a pipeline descriptor with sensible defaults:
26    /// - `depth_format = NONE`
27    /// - `depth_test = true`
28    /// - `depth_write = true`
29    /// - `depth_compare = LESS`
30    /// - `cull_mode = NONE`
31    /// - `blend_attachments = NONE`
32    pub fn new(
33        vert_spv: &'a [u8],
34        frag_spv: &'a [u8],
35        color_formats: &'a [Format],
36        vertex_layout: &'a VertexLayout,
37        push_constant_ranges: &'a [PushConstantRange],
38    ) -> Self {
39        Self {
40            vert_spv,
41            frag_spv,
42            color_formats,
43            vertex_layout,
44            push_constant_ranges,
45            depth: DepthState { format: None, test: true, write: true, compare: CompareOp::Less },
46            cull_mode: CullMode::None,
47            blend_attachments: None,
48        }
49    }
50
51    pub fn depth_format(mut self, format: Format) -> Self {
52        self.depth.format = Some(format);
53        self
54    }
55
56    pub fn depth_test(mut self, test: bool) -> Self {
57        self.depth.test = test;
58        self
59    }
60
61    pub fn depth_write(mut self, write: bool) -> Self {
62        self.depth.write = write;
63        self
64    }
65
66    pub fn depth_compare(mut self, compare: CompareOp) -> Self {
67        self.depth.compare = compare;
68        self
69    }
70
71    pub fn cull_mode(mut self, cull_mode: CullMode) -> Self {
72        self.cull_mode = cull_mode;
73        self
74    }
75
76    pub fn blend_attachments(mut self, states: &'a [BlendState]) -> Self {
77        self.blend_attachments = Some(states);
78        self
79    }
80    /*
81    pub(crate) fn vert_spv(&self) -> &'a [u8] {
82        self.vert_spv
83    }
84    pub(crate) fn frag_spv(&self) -> &'a [u8] {
85        self.frag_spv
86    }
87    pub(crate) fn color_formats(&self) -> &'a [Format] {
88        self.color_formats
89    }
90    pub(crate) fn vertex_layout(&self) -> &'a VertexLayout {
91        self.vertex_layout
92    }
93    pub(crate) fn push_constant_ranges(&self) -> &'a [PushConstantRange] {
94        self.push_constant_ranges
95    }
96    pub(crate) fn depth_format_value(&self) -> Option<Format> {
97        self.depth.format
98    }
99    pub(crate) fn depth_test_value(&self) -> bool {
100        self.depth.test
101    }
102    pub(crate) fn depth_write_value(&self) -> bool {
103        self.depth.write
104    }
105    pub(crate) fn depth_compare_value(&self) -> CompareOp {
106        self.depth.compare
107    }
108    pub(crate) fn cull_mode_value(&self) -> CullMode {
109        self.cull_mode
110    }
111
112    pub(crate) fn blend_attachments_value(&self) -> Option<&'a [BlendState]> {
113        self.blend_attachments
114    }*/
115}
116
117pub struct Pipeline {
118    pub handle: vk::Pipeline,
119    pub layout: vk::PipelineLayout,
120    device: ash::Device,
121}
122
123impl Drop for Pipeline {
124    fn drop(&mut self) {
125        unsafe {
126            self.device.destroy_pipeline(self.handle, None);
127            self.device.destroy_pipeline_layout(self.layout, None);
128        }
129    }
130}