Skip to main content

blade_graphics/
util.rs

1use codespan_reporting::{
2    diagnostic::{Diagnostic, Label},
3    files::SimpleFile,
4    term::{
5        self,
6        termcolor::{ColorChoice, StandardStream},
7    },
8};
9use std::error::Error;
10
11pub fn print_err(error: &dyn Error) {
12    eprint!("{}", error);
13
14    let mut e = error.source();
15    if e.is_some() {
16        eprintln!(": ");
17    } else {
18        eprintln!();
19    }
20
21    while let Some(source) = e {
22        eprintln!("\t{}", source);
23        e = source.source();
24    }
25}
26
27pub fn emit_annotated_error<E: Error>(ann_err: &naga::WithSpan<E>, filename: &str, source: &str) {
28    let files = SimpleFile::new(filename, source);
29    let config = term::Config::default();
30    let writer = StandardStream::stderr(ColorChoice::Auto);
31
32    let diagnostic = Diagnostic::error().with_labels(
33        ann_err
34            .spans()
35            .map(|&(span, ref desc)| {
36                Label::primary((), span.to_range().unwrap()).with_message(desc.to_owned())
37            })
38            .collect(),
39    );
40
41    term::emit_to_write_style(&mut writer.lock(), &config, &files, &diagnostic)
42        .expect("cannot write error");
43}
44
45impl super::TextureFormat {
46    pub const fn block_info(&self) -> super::TexelBlockInfo {
47        const fn uncompressed(size: u8) -> super::TexelBlockInfo {
48            super::TexelBlockInfo {
49                dimensions: (1, 1),
50                size,
51            }
52        }
53        const fn cx_bc(size: u8) -> super::TexelBlockInfo {
54            super::TexelBlockInfo {
55                dimensions: (4, 4),
56                size,
57            }
58        }
59        match *self {
60            Self::R8Unorm => uncompressed(1),
61            Self::Rg8Unorm => uncompressed(2),
62            Self::Rg8Snorm => uncompressed(2),
63            Self::Rgba8Unorm => uncompressed(4),
64            Self::Rgba8UnormSrgb => uncompressed(4),
65            Self::Bgra8Unorm => uncompressed(4),
66            Self::Bgra8UnormSrgb => uncompressed(4),
67            Self::Rgba8Snorm => uncompressed(4),
68            Self::R16Float => uncompressed(2),
69            Self::Rg16Float => uncompressed(4),
70            Self::Rgba16Float => uncompressed(8),
71            Self::R32Float => uncompressed(4),
72            Self::Rg32Float => uncompressed(8),
73            Self::Rgba32Float => uncompressed(16),
74            Self::R32Uint => uncompressed(4),
75            Self::Rg32Uint => uncompressed(8),
76            Self::Rgba32Uint => uncompressed(16),
77            Self::Depth32Float => uncompressed(4),
78            Self::Depth32FloatStencil8Uint => uncompressed(5),
79            Self::Stencil8Uint => uncompressed(1),
80            Self::Bc1Unorm => cx_bc(8),
81            Self::Bc1UnormSrgb => cx_bc(8),
82            Self::Bc2Unorm => cx_bc(16),
83            Self::Bc2UnormSrgb => cx_bc(16),
84            Self::Bc3Unorm => cx_bc(16),
85            Self::Bc3UnormSrgb => cx_bc(16),
86            Self::Bc4Unorm => cx_bc(8),
87            Self::Bc4Snorm => cx_bc(8),
88            Self::Bc5Unorm => cx_bc(16),
89            Self::Bc5Snorm => cx_bc(16),
90            Self::Bc6hUfloat => cx_bc(16),
91            Self::Bc6hFloat => cx_bc(16),
92            Self::Bc7Unorm => cx_bc(16),
93            Self::Bc7UnormSrgb => cx_bc(16),
94            Self::Rgb10a2Unorm => uncompressed(4),
95            Self::Rg11b10Ufloat => uncompressed(4),
96            Self::Rgb9e5Ufloat => uncompressed(4),
97        }
98    }
99
100    pub fn aspects(&self) -> super::TexelAspects {
101        match *self {
102            Self::Depth32Float => super::TexelAspects::DEPTH,
103
104            Self::Depth32FloatStencil8Uint => {
105                super::TexelAspects::DEPTH | super::TexelAspects::STENCIL
106            }
107
108            Self::Stencil8Uint => super::TexelAspects::STENCIL,
109
110            _ => super::TexelAspects::COLOR,
111        }
112    }
113}
114
115impl super::TextureColor {
116    pub const fn stencil_clear_value(&self) -> u32 {
117        match *self {
118            crate::TextureColor::TransparentBlack => 0,
119            crate::TextureColor::OpaqueBlack => !0,
120            crate::TextureColor::White => !0,
121        }
122    }
123
124    pub const fn depth_clear_value(&self) -> f32 {
125        match *self {
126            crate::TextureColor::TransparentBlack => 0.0,
127            crate::TextureColor::OpaqueBlack => 0.0,
128            crate::TextureColor::White => 1.0,
129        }
130    }
131}
132
133impl super::ComputePipeline {
134    /// Return the dispatch group counts sufficient to cover the given extent.
135    pub fn get_dispatch_for(&self, extent: super::Extent) -> [u32; 3] {
136        let wg_size = self.get_workgroup_size();
137        [
138            extent.width.div_ceil(wg_size[0]),
139            extent.height.div_ceil(wg_size[1]),
140            extent.depth.div_ceil(wg_size[2]),
141        ]
142    }
143}