1 // Copyright (c) 2016 The vulkano developers 2 // Licensed under the Apache License, Version 2.0 3 // <LICENSE-APACHE or 4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT 5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, 6 // at your option. All files in the project carrying such 7 // notice may not be copied, modified, or distributed except 8 // according to those terms. 9 10 //! Stage when triangles are turned into pixels. 11 //! 12 //! The rasterization is the stage when collections of triangles are turned into collections 13 //! of pixels or samples. 14 //! 15 16 /// State of the rasterizer. 17 #[derive(Clone, Debug)] 18 pub struct Rasterization { 19 /// If true, then the depth value of the vertices will be clamped to [0.0 ; 1.0]. If false, 20 /// fragments whose depth is outside of this range will be discarded. 21 pub depth_clamp: bool, 22 23 /// If true, all the fragments will be discarded. This is usually used when your vertex shader 24 /// has some side effects and you don't need to run the fragment shader. 25 pub rasterizer_discard: bool, 26 27 /// This setting can ask the rasterizer to downgrade triangles into lines or points, or lines 28 /// into points. 29 pub polygon_mode: PolygonMode, 30 31 /// Specifies whether front faces or back faces should be discarded, or none, or both. 32 pub cull_mode: CullMode, 33 34 /// Specifies which triangle orientation corresponds to the front or the triangle. 35 pub front_face: FrontFace, 36 37 /// Width, in pixels, of lines when drawing lines. 38 /// 39 /// If you pass `None`, then this state will be considered as dynamic and the line width will 40 /// need to be set when you build the command buffer. 41 pub line_width: Option<f32>, 42 43 pub depth_bias: DepthBiasControl, 44 } 45 46 impl Default for Rasterization { 47 #[inline] default() -> Rasterization48 fn default() -> Rasterization { 49 Rasterization { 50 depth_clamp: false, 51 rasterizer_discard: false, 52 polygon_mode: Default::default(), 53 cull_mode: Default::default(), 54 front_face: Default::default(), 55 line_width: Some(1.0), 56 depth_bias: DepthBiasControl::Disabled, 57 } 58 } 59 } 60 61 #[derive(Copy, Clone, Debug)] 62 pub enum DepthBiasControl { 63 Disabled, 64 Dynamic, 65 Static(DepthBias), 66 } 67 68 impl DepthBiasControl { 69 #[inline] is_dynamic(&self) -> bool70 pub fn is_dynamic(&self) -> bool { 71 match *self { 72 DepthBiasControl::Dynamic => true, 73 _ => false, 74 } 75 } 76 } 77 78 #[derive(Copy, Clone, Debug)] 79 pub struct DepthBias { 80 pub constant_factor: f32, 81 /// Requires the `depth_bias_clamp` feature to be enabled. 82 pub clamp: f32, 83 pub slope_factor: f32, 84 } 85 86 /// Specifies the culling mode. 87 /// 88 /// This setting works in pair with `front_face`. The `front_face` setting tells the GPU whether 89 /// clockwise or counter-clockwise correspond to the front and the back of each triangle. Then 90 /// `cull_mode` lets you specify whether front faces should be discarded, back faces should be 91 /// discarded, or none, or both. 92 #[derive(Copy, Clone, Debug)] 93 #[repr(u32)] 94 pub enum CullMode { 95 /// No culling. 96 None = ash::vk::CullModeFlags::NONE.as_raw(), 97 /// The faces facing the front of the screen (ie. facing the user) will be removed. 98 Front = ash::vk::CullModeFlags::FRONT.as_raw(), 99 /// The faces facing the back of the screen will be removed. 100 Back = ash::vk::CullModeFlags::BACK.as_raw(), 101 /// All faces will be removed. 102 FrontAndBack = ash::vk::CullModeFlags::FRONT_AND_BACK.as_raw(), 103 } 104 105 impl From<CullMode> for ash::vk::CullModeFlags { 106 #[inline] from(val: CullMode) -> Self107 fn from(val: CullMode) -> Self { 108 Self::from_raw(val as u32) 109 } 110 } 111 112 impl Default for CullMode { 113 #[inline] default() -> CullMode114 fn default() -> CullMode { 115 CullMode::None 116 } 117 } 118 119 /// Specifies which triangle orientation corresponds to the front or the triangle. 120 #[derive(Copy, Clone, Debug)] 121 #[repr(i32)] 122 pub enum FrontFace { 123 /// Triangles whose vertices are oriented counter-clockwise on the screen will be considered 124 /// as facing their front. Otherwise they will be considered as facing their back. 125 CounterClockwise = ash::vk::FrontFace::COUNTER_CLOCKWISE.as_raw(), 126 127 /// Triangles whose vertices are oriented clockwise on the screen will be considered 128 /// as facing their front. Otherwise they will be considered as facing their back. 129 Clockwise = ash::vk::FrontFace::CLOCKWISE.as_raw(), 130 } 131 132 impl From<FrontFace> for ash::vk::FrontFace { 133 #[inline] from(val: FrontFace) -> Self134 fn from(val: FrontFace) -> Self { 135 Self::from_raw(val as i32) 136 } 137 } 138 139 impl Default for FrontFace { 140 #[inline] default() -> FrontFace141 fn default() -> FrontFace { 142 FrontFace::CounterClockwise 143 } 144 } 145 146 #[derive(Copy, Clone, Debug, PartialEq, Eq)] 147 #[repr(i32)] 148 pub enum PolygonMode { 149 Fill = ash::vk::PolygonMode::FILL.as_raw(), 150 Line = ash::vk::PolygonMode::LINE.as_raw(), 151 Point = ash::vk::PolygonMode::POINT.as_raw(), 152 } 153 154 impl From<PolygonMode> for ash::vk::PolygonMode { 155 #[inline] from(val: PolygonMode) -> Self156 fn from(val: PolygonMode) -> Self { 157 Self::from_raw(val as i32) 158 } 159 } 160 161 impl Default for PolygonMode { 162 #[inline] default() -> PolygonMode163 fn default() -> PolygonMode { 164 PolygonMode::Fill 165 } 166 } 167