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
//! Multisampling texture manager.
use crate::texture::RawTexture;
use wgpu::*;
/// Multisampling texture manager.
pub struct Multisampler {
sample_count: u32,
surface_format: TextureFormat,
tex: Option<RawTexture>,
}
impl Multisampler {
/// Create a new multisampler.
///
/// # Arguments
///
/// * `surface_format` - Framebuffer texture format.
/// * `sample_count` - Number of MSAA samples.
pub fn new(surface_format: TextureFormat, sample_count: u32) -> Self {
Self {
sample_count,
surface_format,
tex: None,
}
}
/// Get number of MSAA samples.
pub fn sample_count(&self) -> u32 {
self.sample_count
}
/// Get multisampled framebuffer.
///
/// # Arguments
///
/// * `device` - Device to update the buffer on if dimensions change.
/// * `config` - Current screen configuration.
pub fn target(&mut self, device: &Device, config: &SurfaceConfiguration) -> &TextureView {
let dimensions = (config.width, config.height);
match &self.tex {
Some(tex) if tex.dimensions == dimensions => {}
_ => {
self.tex = Some(RawTexture::create_custom(
device,
Some("msaa"),
dimensions,
self.sample_count,
self.surface_format,
wgpu::TextureUsages::RENDER_ATTACHMENT,
));
}
}
self.tex.as_ref().map(|tex| &tex.view).unwrap()
}
}