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 //! Generates multiple fragments per framebuffer pixel when rasterizing. This can be used for
11 //! anti-aliasing.
12 
13 use crate::image::SampleCount;
14 
15 // TODO: handle some weird behaviors with non-floating-point targets
16 
17 /// State of the multisampling.
18 #[derive(Copy, Clone, Debug)]
19 pub struct MultisampleState {
20     /// The number of rasterization samples to take per pixel. The GPU will pick this many different
21     /// locations within each pixel and assign to each of these locations a different depth value.
22     /// The depth and stencil test will then be run for each sample.
23     ///
24     /// The default value is [`SampleCount::Sample1`].
25     pub rasterization_samples: SampleCount,
26 
27     /// Controls the proportion (between 0.0 and 1.0) of the samples that will be run through the
28     /// fragment shader.
29     ///
30     /// If the value is 1.0, then all sub-pixel samples will run
31     /// through the shader and get a different value. If the value is 0.5, about half of the samples
32     /// will run through the shader and the other half will get their values from the ones which
33     /// went through the shader.
34     ///
35     /// If set to `Some`, the [`sample_rate_shading`](crate::device::Features::sample_rate_shading)
36     /// feature must be enabled on the device.
37     pub sample_shading: Option<f32>,
38 
39     /// A mask of bits that is ANDed with the coverage mask of each set of `rasterization_samples`
40     /// samples. Only the first `rasterization_samples / 32` bits are used, the rest is ignored.
41     ///
42     /// The default value is `[0xFFFFFFFF; 2]`.
43     pub sample_mask: [u32; 2], // 64 bits for needed for 64 SampleCount
44 
45     /// Controls whether the alpha value of the fragment will be used in an implementation-defined
46     /// way to determine which samples get disabled or not. For example if the alpha value is 0.5,
47     /// then about half of the samples will be discarded. If you render to a multisample image, this
48     /// means that the color will end up being mixed with whatever color was underneath, which gives
49     /// the same effect as alpha blending.
50     pub alpha_to_coverage_enable: bool,
51 
52     /// Controls whether the alpha value of all the samples will be forced to 1.0 (or the
53     /// maximum possible value) after the effects of `alpha_to_coverage` have been applied.
54     ///
55     /// If set to `true`, the [`alpha_to_one`](crate::device::Features::alpha_to_one)
56     /// feature must be enabled on the device.
57     pub alpha_to_one_enable: bool,
58 }
59 
60 impl MultisampleState {
61     /// Creates a `MultisampleState` with multisampling disabled.
62     #[inline]
new() -> MultisampleState63     pub fn new() -> MultisampleState {
64         MultisampleState {
65             rasterization_samples: SampleCount::Sample1,
66             sample_shading: None,
67             sample_mask: [0xFFFFFFFF; 2],
68             alpha_to_coverage_enable: false,
69             alpha_to_one_enable: false,
70         }
71     }
72 }
73 
74 impl Default for MultisampleState {
75     /// Returns [`MultisampleState::new()`].
76     #[inline]
default() -> Self77     fn default() -> Self {
78         Self::new()
79     }
80 }
81