Skip to main content

rotex_vulkan/backend/vulkan/graphics_pipeline/
state.rs

1use ash::vk;
2
3pub struct Viewport {
4    pub x: f32,
5    pub y: f32,
6    pub width: u32,
7    pub height: u32,
8    pub min_depth: f32,
9    pub max_depth: f32,
10}
11
12impl Viewport {
13    pub fn default() -> Self {
14        Self {
15            x: 0.0,
16            y: 0.0,
17            width: 0,
18            height: 0,
19            min_depth: 0.0,
20            max_depth: 1.0,
21        }
22    }
23
24    pub fn with_position(mut self, x: f32, y: f32) -> Self {
25        self.x = x;
26        self.y = y;
27        self
28    }
29
30    pub fn with_extent(mut self, width: u32, height: u32) -> Self {
31        self.width = width;
32        self.height = height;
33        self
34    }
35
36    pub fn with_depth_range(mut self, min_depth: f32, max_depth: f32) -> Self {
37        self.min_depth = min_depth;
38        self.max_depth = max_depth;
39        self
40    }
41
42    pub fn to_vk_viewport(&self) -> vk::Viewport {
43        vk::Viewport {
44            x: self.x,
45            y: self.y,
46            width: self.width as f32,
47            height: self.height as f32,
48            min_depth: self.min_depth,
49            max_depth: self.max_depth,
50        }
51    }
52}
53
54pub struct RasterizationState {
55    pub depth_clamp_enable: bool,
56    pub rasterizer_discard_enable: bool,
57    pub polygon_mode: vk::PolygonMode,
58    pub cull_mode: vk::CullModeFlags,
59    pub front_face: vk::FrontFace,
60    pub depth_bias_enable: bool,
61    pub depth_bias_constant_factor: f32,
62    pub depth_bias_clamp: f32,
63    pub depth_bias_slope_factor: f32,
64    pub flags: vk::PipelineRasterizationStateCreateFlags,
65    pub line_width: f32,
66}
67
68impl RasterizationState {
69    pub fn default() -> Self {
70        Self {
71            depth_clamp_enable: false,
72            rasterizer_discard_enable: false,
73            polygon_mode: vk::PolygonMode::FILL,
74            cull_mode: vk::CullModeFlags::BACK,
75            front_face: vk::FrontFace::CLOCKWISE,
76            depth_bias_enable: false,
77            depth_bias_constant_factor: 0.0,
78            depth_bias_clamp: 0.0,
79            depth_bias_slope_factor: 0.0,
80            flags: vk::PipelineRasterizationStateCreateFlags::empty(),
81            line_width: 1.0,
82        }
83    }
84
85    pub fn with_depth_clamp_enable(mut self, enable: bool) -> Self {
86        self.depth_clamp_enable = enable;
87        self
88    }
89
90    pub fn with_rasterizer_discard_enable(mut self, enable: bool) -> Self {
91        self.rasterizer_discard_enable = enable;
92        self
93    }
94
95    pub fn with_polygon_mode(mut self, mode: vk::PolygonMode) -> Self {
96        self.polygon_mode = mode;
97        self
98    }
99
100    pub fn with_cull_mode(mut self, mode: vk::CullModeFlags) -> Self {
101        self.cull_mode = mode;
102        self
103    }
104
105    pub fn with_front_face(mut self, face: vk::FrontFace) -> Self {
106        self.front_face = face;
107        self
108    }
109
110    pub fn with_depth_bias_enable(mut self, enable: bool) -> Self {
111        self.depth_bias_enable = enable;
112        self
113    }
114
115    pub fn with_depth_bias(mut self, constant_factor: f32, clamp: f32, slope_factor: f32) -> Self {
116        self.depth_bias_constant_factor = constant_factor;
117        self.depth_bias_clamp = clamp;
118        self.depth_bias_slope_factor = slope_factor;
119        self
120    }
121
122    pub fn with_flags(mut self, flags: vk::PipelineRasterizationStateCreateFlags) -> Self {
123        self.flags |= flags;
124        self
125    }
126
127    pub fn with_line_width(mut self, width: f32) -> Self {
128        self.line_width = width;
129        self
130    }
131
132    pub fn to_vk_rasterization_state(&self) -> vk::PipelineRasterizationStateCreateInfo<'_> {
133        vk::PipelineRasterizationStateCreateInfo::default()
134            .depth_clamp_enable(self.depth_clamp_enable)
135            .rasterizer_discard_enable(self.rasterizer_discard_enable)
136            .polygon_mode(self.polygon_mode)
137            .cull_mode(self.cull_mode)
138            .front_face(self.front_face)
139            .depth_bias_enable(self.depth_bias_enable)
140            .depth_bias_constant_factor(self.depth_bias_constant_factor)
141            .depth_bias_clamp(self.depth_bias_clamp)
142            .depth_bias_slope_factor(self.depth_bias_slope_factor)
143            .flags(self.flags)
144            .line_width(self.line_width)
145    }
146}
147
148pub struct DepthStencilState {
149    pub depth_test_enable: bool,
150    pub depth_write_enable: bool,
151    pub depth_compare_op: vk::CompareOp,
152    pub depth_bounds_test_enable: bool,
153    pub stencil_test_enable: bool,
154    pub min_depth_bounds: f32,
155    pub max_depth_bounds: f32,
156}
157
158impl DepthStencilState {
159    pub fn default() -> Self {
160        Self {
161            depth_test_enable: false,
162            depth_write_enable: false,
163            depth_compare_op: vk::CompareOp::LESS,
164            depth_bounds_test_enable: false,
165            stencil_test_enable: false,
166            min_depth_bounds: 0.0,
167            max_depth_bounds: 1.0,
168        }
169    }
170
171    pub fn with_depth_test_enable(mut self, enable: bool) -> Self {
172        self.depth_test_enable = enable;
173        self
174    }
175
176    pub fn with_depth_write_enable(mut self, enable: bool) -> Self {
177        self.depth_write_enable = enable;
178        self
179    }
180
181    pub fn with_depth_compare_op(mut self, op: vk::CompareOp) -> Self {
182        self.depth_compare_op = op;
183        self
184    }
185
186    pub fn with_depth_bounds_test_enable(mut self, enable: bool) -> Self {
187        self.depth_bounds_test_enable = enable;
188        self
189    }
190
191    pub fn with_stencil_test_enable(mut self, enable: bool) -> Self {
192        self.stencil_test_enable = enable;
193        self
194    }
195
196    pub fn with_depth_bounds(mut self, min: f32, max: f32) -> Self {
197        self.min_depth_bounds = min;
198        self.max_depth_bounds = max;
199        self
200    }
201
202    pub fn to_vk_depth_stencil_state(&self) -> vk::PipelineDepthStencilStateCreateInfo<'_> {
203        vk::PipelineDepthStencilStateCreateInfo::default()
204            .depth_test_enable(self.depth_test_enable)
205            .depth_write_enable(self.depth_write_enable)
206            .depth_compare_op(self.depth_compare_op)
207            .depth_bounds_test_enable(self.depth_bounds_test_enable)
208            .stencil_test_enable(self.stencil_test_enable)
209            .min_depth_bounds(self.min_depth_bounds)
210            .max_depth_bounds(self.max_depth_bounds)
211    }
212}
213
214pub struct MultisampleState {
215    pub sample_shading_enable: bool,
216    pub rasterization_samples: vk::SampleCountFlags,
217    pub min_sample_shading: f32,
218    pub sample_mask: Option<Vec<u32>>,
219    pub alpha_to_coverage_enable: bool,
220    pub alpha_to_one_enable: bool,
221    pub flags: vk::PipelineMultisampleStateCreateFlags,
222}
223
224impl MultisampleState {
225    pub fn default() -> Self {
226        Self {
227            sample_shading_enable: false,
228            rasterization_samples: vk::SampleCountFlags::TYPE_1,
229            min_sample_shading: 1.0,
230            sample_mask: None,
231            alpha_to_coverage_enable: false,
232            alpha_to_one_enable: false,
233            flags: vk::PipelineMultisampleStateCreateFlags::empty(),
234        }
235    }
236
237    pub fn with_sample_shading_enable(mut self, enable: bool) -> Self {
238        self.sample_shading_enable = enable;
239        self
240    }
241
242    pub fn with_rasterization_samples(mut self, samples: vk::SampleCountFlags) -> Self {
243        self.rasterization_samples = samples;
244        self
245    }
246
247    pub fn with_min_sample_shading(mut self, min_shading: f32) -> Self {
248        self.min_sample_shading = min_shading;
249        self
250    }
251
252    pub fn with_sample_mask(mut self, mask: Vec<u32>) -> Self {
253        self.sample_mask = Some(mask);
254        self
255    }
256
257    pub fn with_alpha_to_coverage_enable(mut self, enable: bool) -> Self {
258        self.alpha_to_coverage_enable = enable;
259        self
260    }
261
262    pub fn with_alpha_to_one_enable(mut self, enable: bool) -> Self {
263        self.alpha_to_one_enable = enable;
264        self
265    }
266
267    pub fn with_flags(mut self, flags: vk::PipelineMultisampleStateCreateFlags) -> Self {
268        self.flags |= flags;
269        self
270    }
271}
272
273pub struct ColorBlendAttachmentState {
274    pub blend_enable: bool,
275    pub src_color_blend_factor: vk::BlendFactor,
276    pub dst_color_blend_factor: vk::BlendFactor,
277    pub color_blend_op: vk::BlendOp,
278    pub src_alpha_blend_factor: vk::BlendFactor,
279    pub dst_alpha_blend_factor: vk::BlendFactor,
280    pub alpha_blend_op: vk::BlendOp,
281    pub color_write_mask: vk::ColorComponentFlags,
282}
283
284impl ColorBlendAttachmentState {
285    pub fn default() -> Self {
286        Self {
287            blend_enable: false,
288            src_color_blend_factor: vk::BlendFactor::ONE,
289            dst_color_blend_factor: vk::BlendFactor::ZERO,
290            color_blend_op: vk::BlendOp::ADD,
291            src_alpha_blend_factor: vk::BlendFactor::ONE,
292            dst_alpha_blend_factor: vk::BlendFactor::ZERO,
293            alpha_blend_op: vk::BlendOp::ADD,
294            color_write_mask: vk::ColorComponentFlags::R
295                | vk::ColorComponentFlags::G
296                | vk::ColorComponentFlags::B
297                | vk::ColorComponentFlags::A,
298        }
299    }
300
301    pub fn with_blend_enable(mut self, enable: bool) -> Self {
302        self.blend_enable = enable;
303        self
304    }
305
306    pub fn with_src_color_blend_factor(mut self, factor: vk::BlendFactor) -> Self {
307        self.src_color_blend_factor = factor;
308        self
309    }
310
311    pub fn with_dst_color_blend_factor(mut self, factor: vk::BlendFactor) -> Self {
312        self.dst_color_blend_factor = factor;
313        self
314    }
315
316    pub fn with_color_blend_op(mut self, op: vk::BlendOp) -> Self {
317        self.color_blend_op = op;
318        self
319    }
320
321    pub fn with_src_alpha_blend_factor(mut self, factor: vk::BlendFactor) -> Self {
322        self.src_alpha_blend_factor = factor;
323        self
324    }
325
326    pub fn with_dst_alpha_blend_factor(mut self, factor: vk::BlendFactor) -> Self {
327        self.dst_alpha_blend_factor = factor;
328        self
329    }
330
331    pub fn with_alpha_blend_op(mut self, op: vk::BlendOp) -> Self {
332        self.alpha_blend_op = op;
333        self
334    }
335
336    pub fn with_color_write_mask(mut self, mask: vk::ColorComponentFlags) -> Self {
337        self.color_write_mask = mask;
338        self
339    }
340
341    pub(crate) fn to_vk_color_blend_attachment_state(
342        &self,
343    ) -> vk::PipelineColorBlendAttachmentState {
344        vk::PipelineColorBlendAttachmentState::default()
345            .blend_enable(self.blend_enable)
346            .src_color_blend_factor(self.src_color_blend_factor)
347            .dst_color_blend_factor(self.dst_color_blend_factor)
348            .color_blend_op(self.color_blend_op)
349            .src_alpha_blend_factor(self.src_alpha_blend_factor)
350            .dst_alpha_blend_factor(self.dst_alpha_blend_factor)
351            .alpha_blend_op(self.alpha_blend_op)
352            .color_write_mask(self.color_write_mask)
353    }
354}
355
356pub struct ColorBlendState {
357    pub logic_op_enable: bool,
358    pub logic_op: vk::LogicOp,
359    pub attachments: Vec<ColorBlendAttachmentState>,
360    pub blend_constants: [f32; 4],
361    pub flags: vk::PipelineColorBlendStateCreateFlags,
362}
363
364impl ColorBlendState {
365    pub fn default() -> Self {
366        Self {
367            logic_op_enable: false,
368            logic_op: vk::LogicOp::CLEAR,
369            attachments: Vec::new(),
370            blend_constants: [0.0; 4],
371            flags: vk::PipelineColorBlendStateCreateFlags::empty(),
372        }
373    }
374
375    pub fn with_logic_op_enable(mut self, enable: bool) -> Self {
376        self.logic_op_enable = enable;
377        self
378    }
379
380    pub fn with_logic_op(mut self, op: vk::LogicOp) -> Self {
381        self.logic_op = op;
382        self
383    }
384
385    pub fn with_attachment(mut self, attachment: ColorBlendAttachmentState) -> Self {
386        self.attachments.push(attachment);
387        self
388    }
389
390    pub fn with_blend_constants(mut self, constants: [f32; 4]) -> Self {
391        self.blend_constants = constants;
392        self
393    }
394
395    pub fn with_flags(mut self, flags: vk::PipelineColorBlendStateCreateFlags) -> Self {
396        self.flags |= flags;
397        self
398    }
399}