1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
//! Configures how primitives should be converted into collections of fragments.
use crate::{
device::Device, macros::vulkan_enum, Requires, RequiresAllOf, RequiresOneOf, ValidationError,
};
/// The state in a graphics pipeline describing how the rasterization stage should behave.
#[derive(Clone, Debug)]
pub struct RasterizationState {
/// If true, then the depth value of the vertices will be clamped to the range [0.0, 1.0]. If
/// false, fragments whose depth is outside of this range will be discarded.
///
/// If enabled, the [`depth_clamp`](crate::device::Features::depth_clamp) feature must be
/// enabled on the device.
///
/// The default value is `false`.
pub depth_clamp_enable: bool,
/// If true, all the fragments will be discarded, and the fragment shader will not be run. This
/// is usually used when your vertex shader has some side effects and you don't need to run the
/// fragment shader.
///
/// The default value is `false`.
pub rasterizer_discard_enable: bool,
/// This setting can ask the rasterizer to downgrade triangles into lines or points, or lines
/// into points.
///
/// If set to a value other than `Fill`, the
/// [`fill_mode_non_solid`](crate::device::Features::fill_mode_non_solid) feature must be
/// enabled on the device.
///
/// The default value is [`PolygonMode::Fill`].
pub polygon_mode: PolygonMode,
/// Specifies whether front faces or back faces should be discarded, or none, or both.
///
/// The default value is [`CullMode::None`].
pub cull_mode: CullMode,
/// Specifies which triangle orientation is considered to be the front of the triangle.
///
/// The default value is [`FrontFace::CounterClockwise`].
pub front_face: FrontFace,
/// Sets how to modify depth values in the rasterization stage.
///
/// If set to `None`, depth biasing is disabled, the depth values will pass to the fragment
/// shader unmodified.
///
/// The default value is `None`.
pub depth_bias: Option<DepthBiasState>,
/// Width, in pixels, of lines when drawing lines.
///
/// Setting this to a value other than 1.0 requires the
/// [`wide_lines`](crate::device::Features::wide_lines) feature to be enabled on
/// the device.
///
/// The default value is `1.0`.
pub line_width: f32,
/// The rasterization mode for lines.
///
/// If this is not set to `Default`, the
/// [`ext_line_rasterization`](crate::device::DeviceExtensions::ext_line_rasterization)
/// extension and an additional feature must be enabled on the device.
///
/// The default value is [`LineRasterizationMode::Default`].
pub line_rasterization_mode: LineRasterizationMode,
/// Enables and sets the parameters for line stippling.
///
/// If this is set to `Some`, the
/// [`ext_line_rasterization`](crate::device::DeviceExtensions::ext_line_rasterization)
/// extension and an additional feature must be enabled on the device.
///
/// The default value is `None`.
pub line_stipple: Option<LineStipple>,
pub _ne: crate::NonExhaustive,
}
impl Default for RasterizationState {
#[inline]
fn default() -> Self {
Self {
depth_clamp_enable: false,
rasterizer_discard_enable: false,
polygon_mode: Default::default(),
cull_mode: Default::default(),
front_face: Default::default(),
depth_bias: None,
line_width: 1.0,
line_rasterization_mode: Default::default(),
line_stipple: None,
_ne: crate::NonExhaustive(()),
}
}
}
impl RasterizationState {
/// Creates a `RasterizationState` with depth clamping, discard, depth biasing and line
/// stippling disabled, filled polygons, no culling, counterclockwise front face, and the
/// default line width and line rasterization mode.
#[inline]
#[deprecated(since = "0.34.0", note = "use `RasterizationState::default` instead")]
pub fn new() -> Self {
Self::default()
}
/// Sets the polygon mode.
#[inline]
#[deprecated(since = "0.34.0")]
pub fn polygon_mode(mut self, polygon_mode: PolygonMode) -> Self {
self.polygon_mode = polygon_mode;
self
}
/// Sets the cull mode.
#[inline]
#[deprecated(since = "0.34.0")]
pub fn cull_mode(mut self, cull_mode: CullMode) -> Self {
self.cull_mode = cull_mode;
self
}
/// Sets the front face.
#[inline]
#[deprecated(since = "0.34.0")]
pub fn front_face(mut self, front_face: FrontFace) -> Self {
self.front_face = front_face;
self
}
pub(crate) fn validate(&self, device: &Device) -> Result<(), Box<ValidationError>> {
let &Self {
depth_clamp_enable,
rasterizer_discard_enable,
polygon_mode,
cull_mode,
front_face,
depth_bias: _,
line_width: _,
line_rasterization_mode,
ref line_stipple,
_ne: _,
} = self;
let properties = device.physical_device().properties();
polygon_mode.validate_device(device).map_err(|err| {
err.add_context("polygon_mode")
.set_vuids(&["VUID-VkPipelineRasterizationStateCreateInfo-polygonMode-parameter"])
})?;
line_rasterization_mode
.validate_device(device)
.map_err(|err| err.add_context("line_rasterization_mode").set_vuids(&["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-lineRasterizationMode-parameter"]))?;
if depth_clamp_enable && !device.enabled_features().depth_clamp {
return Err(Box::new(ValidationError {
context: "depth_clamp_enable".into(),
problem: "is `true`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"depth_clamp",
)])]),
vuids: &["VUID-VkPipelineRasterizationStateCreateInfo-depthClampEnable-00782"],
}));
}
if polygon_mode != PolygonMode::Fill && !device.enabled_features().fill_mode_non_solid {
return Err(Box::new(ValidationError {
context: "polygon_mode".into(),
problem: "is not `PolygonMode::Fill`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"fill_mode_non_solid",
)])]),
vuids: &["VUID-VkPipelineRasterizationStateCreateInfo-polygonMode-01507"],
}));
}
if device.enabled_extensions().khr_portability_subset
&& !device.enabled_features().point_polygons
&& !rasterizer_discard_enable
&& polygon_mode == PolygonMode::Point
{
return Err(Box::new(ValidationError {
problem: "this device is a portability subset device, \
`rasterizer_discard_enable` is `false`, and \
`polygon_mode` is `PolygonMode::Point`"
.into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"point_polygons",
)])]),
vuids: &["VUID-VkPipelineRasterizationStateCreateInfo-pointPolygons-04458"],
..Default::default()
}));
}
cull_mode.validate_device(device).map_err(|err| {
err.add_context("cull_mode")
.set_vuids(&["VUID-VkPipelineRasterizationStateCreateInfo-cullMode-parameter"])
})?;
front_face.validate_device(device).map_err(|err| {
err.add_context("front_face")
.set_vuids(&["VUID-VkPipelineRasterizationStateCreateInfo-frontFace-parameter"])
})?;
if line_rasterization_mode != LineRasterizationMode::Default {
if !device.enabled_extensions().ext_line_rasterization {
return Err(Box::new(ValidationError {
context: "line_rasterization_mode".into(),
problem: "`is not `LineRasterizationMode::Default`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"ext_line_rasterization",
)])]),
..Default::default()
}));
}
match line_rasterization_mode {
LineRasterizationMode::Default => (),
LineRasterizationMode::Rectangular => {
if !device.enabled_features().rectangular_lines {
return Err(Box::new(ValidationError {
context: "line_rasterization_mode".into(),
problem: "is `LineRasterizationMode::Rectangular`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"rectangular_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-lineRasterizationMode-02768"],
}));
}
}
LineRasterizationMode::Bresenham => {
if !device.enabled_features().bresenham_lines {
return Err(Box::new(ValidationError {
context: "line_rasterization_mode".into(),
problem: "is `LineRasterizationMode::Bresenham`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"bresenham_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-lineRasterizationMode-02769"],
}));
}
}
LineRasterizationMode::RectangularSmooth => {
if !device.enabled_features().smooth_lines {
return Err(Box::new(ValidationError {
context: "line_rasterization_mode".into(),
problem: "is `LineRasterizationMode::RectangularSmooth`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"smooth_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-lineRasterizationMode-02770"],
}));
}
}
}
}
if line_stipple.is_some() {
if !device.enabled_extensions().ext_line_rasterization {
return Err(Box::new(ValidationError {
context: "line_stipple".into(),
problem: "is `Some`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"ext_line_rasterization",
)])]),
..Default::default()
}));
}
match line_rasterization_mode {
LineRasterizationMode::Default => {
if !device.enabled_features().stippled_rectangular_lines {
return Err(Box::new(ValidationError {
problem: "`line_stipple` is `Some`, and \
`line_rasterization_mode` is \
`LineRasterizationMode::Default`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"stippled_rectangular_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-stippledLineEnable-02774"],
..Default::default()
}));
}
if !properties.strict_lines {
return Err(Box::new(ValidationError {
problem: "`line_stipple` is `Some`, and \
`line_rasterization_mode` is \
`LineRasterizationMode::Default`, \
but the `strict_lines` property is `false`".into(),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-stippledLineEnable-02774"],
..Default::default()
}));
}
}
LineRasterizationMode::Rectangular => {
if !device.enabled_features().stippled_rectangular_lines {
return Err(Box::new(ValidationError {
problem: "`line_stipple` is `Some`, and \
`line_rasterization_mode` is \
`LineRasterizationMode::Rectangular`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"stippled_rectangular_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-stippledLineEnable-02771"],
..Default::default()
}));
}
}
LineRasterizationMode::Bresenham => {
if !device.enabled_features().stippled_bresenham_lines {
return Err(Box::new(ValidationError {
problem: "`line_stipple` is `Some`, and \
`line_rasterization_mode` is \
`LineRasterizationMode::Bresenham`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"stippled_bresenham_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-stippledLineEnable-02772"],
..Default::default()
}));
}
}
LineRasterizationMode::RectangularSmooth => {
if !device.enabled_features().stippled_smooth_lines {
return Err(Box::new(ValidationError {
problem: "`line_stipple` is `Some`, and \
`line_rasterization_mode` is \
`LineRasterizationMode::RectangularSmooth`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"stippled_smooth_lines",
)])]),
vuids: &["VUID-VkPipelineRasterizationLineStateCreateInfoEXT-stippledLineEnable-02773"],
..Default::default()
}));
}
}
}
}
Ok(())
}
}
/// The values to use for depth biasing.
#[derive(Clone, Copy, Debug)]
pub struct DepthBiasState {
/// Specifies a constant factor to be multiplied to every depth value.
///
/// The default value is `1.0`.
pub constant_factor: f32,
/// The maximum (or minimum) depth bias of a fragment.
///
/// Setting this to a value other than 0.0 requires the
/// [`depth_bias_clamp`](crate::device::Features::depth_bias_clamp) feature to be enabled on
/// the device.
///
/// The default value is `0.0`.
pub clamp: f32,
/// A scalar factor to multiply with a fragment's slope in depth bias calculations.
///
/// The default value is `1.0`.
pub slope_factor: f32,
}
impl Default for DepthBiasState {
#[inline]
fn default() -> Self {
Self {
constant_factor: 1.0,
clamp: 0.0,
slope_factor: 1.0,
}
}
}
vulkan_enum! {
#[non_exhaustive]
/// Specifies the culling mode.
///
/// This setting works in pair with `front_face`. The `front_face` setting tells the GPU whether
/// clockwise or counter-clockwise correspond to the front and the back of each triangle. Then
/// `cull_mode` lets you specify whether front faces should be discarded, back faces should be
/// discarded, or none, or both.
CullMode = CullModeFlags(u32);
/// No culling.
None = NONE,
/// The faces facing the front of the screen (ie. facing the user) will be removed.
Front = FRONT,
/// The faces facing the back of the screen will be removed.
Back = BACK,
/// All faces will be removed.
FrontAndBack = FRONT_AND_BACK,
}
impl Default for CullMode {
#[inline]
fn default() -> CullMode {
CullMode::None
}
}
vulkan_enum! {
#[non_exhaustive]
/// Specifies which triangle orientation corresponds to the front or the triangle.
FrontFace = FrontFace(i32);
/// Triangles whose vertices are oriented counter-clockwise on the screen will be considered
/// as facing their front. Otherwise they will be considered as facing their back.
CounterClockwise = COUNTER_CLOCKWISE,
/// Triangles whose vertices are oriented clockwise on the screen will be considered
/// as facing their front. Otherwise they will be considered as facing their back.
Clockwise = CLOCKWISE,
}
impl Default for FrontFace {
#[inline]
fn default() -> FrontFace {
FrontFace::CounterClockwise
}
}
vulkan_enum! {
#[non_exhaustive]
// TODO: document
PolygonMode = PolygonMode(i32);
// TODO: document
Fill = FILL,
// TODO: document
Line = LINE,
// TODO: document further
/// On [portability subset](crate::instance#portability-subset-devices-and-the-enumerate_portability-flag)
/// devices, unless `rasterizer_discard_enable` is active, the
/// [`point_polygons`](crate::device::Features::point_polygons)
/// feature must be enabled on the device.
Point = POINT,
/* TODO: enable
// TODO: document
FillRectangle = FILL_RECTANGLE_NV
RequiresOneOf([
RequiresAllOf([DeviceExtension(nv_fill_rectangle)]),
]),*/
}
impl Default for PolygonMode {
#[inline]
fn default() -> PolygonMode {
PolygonMode::Fill
}
}
vulkan_enum! {
#[non_exhaustive]
/// The rasterization mode to use for lines.
LineRasterizationMode = LineRasterizationModeEXT(i32);
/// If the [`strict_lines`](crate::device::Properties::strict_lines) device property is `true`,
/// then this is the same as `Rectangular`. Otherwise, lines are drawn as parallelograms.
///
/// If [`RasterizationState::line_stipple`] is `Some`, then the
/// [`strict_lines`](crate::device::Properties::strict_lines) property must be `true` and the
/// [`stippled_rectangular_lines`](crate::device::Features::stippled_rectangular_lines) feature
/// must be enabled on the device.
Default = DEFAULT,
/// Lines are drawn as if they were rectangles extruded from the line.
///
/// The [`rectangular_lines`](crate::device::Features::rectangular_lines) feature must be
/// enabled on the device. If [`RasterizationState::line_stipple`] is `Some`, then the
/// [`stippled_rectangular_lines`](crate::device::Features::stippled_rectangular_lines) must
/// also be enabled.
Rectangular = RECTANGULAR,
/// Lines are drawn by determining which pixel diamonds the line intersects and exits.
///
/// The [`bresenham_lines`](crate::device::Features::bresenham_lines) feature must be
/// enabled on the device. If [`RasterizationState::line_stipple`] is `Some`, then the
/// [`stippled_bresenham_lines`](crate::device::Features::stippled_bresenham_lines) must
/// also be enabled.
Bresenham = BRESENHAM,
/// As `Rectangular`, but with alpha falloff.
///
/// The [`smooth_lines`](crate::device::Features::smooth_lines) feature must be
/// enabled on the device. If [`RasterizationState::line_stipple`] is `Some`, then the
/// [`stippled_smooth_lines`](crate::device::Features::stippled_smooth_lines) must
/// also be enabled.
RectangularSmooth = RECTANGULAR_SMOOTH,
}
impl Default for LineRasterizationMode {
/// Returns `LineRasterizationMode::Default`.
#[inline]
fn default() -> Self {
Self::Default
}
}
/// The parameters of a stippled line.
#[derive(Clone, Copy, Debug)]
pub struct LineStipple {
/// The repeat factor used in stippled line rasterization. Must be between 1 and 256 inclusive.
pub factor: u32,
/// The bit pattern used in stippled line rasterization.
pub pattern: u16,
}