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(&mut writer.lock(), &config, &files, &diagnostic).expect("cannot write error");
42}
43
44impl super::TextureFormat {
45    pub fn block_info(&self) -> super::TexelBlockInfo {
46        fn uncompressed(size: u8) -> super::TexelBlockInfo {
47            super::TexelBlockInfo {
48                dimensions: (1, 1),
49                size,
50            }
51        }
52        fn cx_bc(size: u8) -> super::TexelBlockInfo {
53            super::TexelBlockInfo {
54                dimensions: (4, 4),
55                size,
56            }
57        }
58        match *self {
59            Self::R8Unorm => uncompressed(1),
60            Self::Rg8Unorm => uncompressed(2),
61            Self::Rg8Snorm => uncompressed(2),
62            Self::Rgba8Unorm => uncompressed(4),
63            Self::Rgba8UnormSrgb => uncompressed(4),
64            Self::Bgra8Unorm => uncompressed(4),
65            Self::Bgra8UnormSrgb => uncompressed(4),
66            Self::Rgba8Snorm => uncompressed(4),
67            Self::R16Float => uncompressed(2),
68            Self::Rg16Float => uncompressed(4),
69            Self::Rgba16Float => uncompressed(8),
70            Self::R32Float => uncompressed(4),
71            Self::Rg32Float => uncompressed(8),
72            Self::Rgba32Float => uncompressed(16),
73            Self::R32Uint => uncompressed(4),
74            Self::Rg32Uint => uncompressed(8),
75            Self::Rgba32Uint => uncompressed(16),
76            Self::Depth32Float => uncompressed(4),
77            Self::Bc1Unorm => cx_bc(8),
78            Self::Bc1UnormSrgb => cx_bc(8),
79            Self::Bc2Unorm => cx_bc(16),
80            Self::Bc2UnormSrgb => cx_bc(16),
81            Self::Bc3Unorm => cx_bc(16),
82            Self::Bc3UnormSrgb => cx_bc(16),
83            Self::Bc4Unorm => cx_bc(8),
84            Self::Bc4Snorm => cx_bc(8),
85            Self::Bc5Unorm => cx_bc(16),
86            Self::Bc5Snorm => cx_bc(16),
87            Self::Bc6hUfloat => cx_bc(16),
88            Self::Bc6hFloat => cx_bc(16),
89            Self::Bc7Unorm => cx_bc(16),
90            Self::Bc7UnormSrgb => cx_bc(16),
91            Self::Rgb10a2Unorm => uncompressed(4),
92            Self::Rg11b10Ufloat => uncompressed(4),
93            Self::Rgb9e5Ufloat => uncompressed(4),
94        }
95    }
96
97    pub fn aspects(&self) -> super::TexelAspects {
98        match *self {
99            Self::Depth32Float => super::TexelAspects::DEPTH,
100            _ => super::TexelAspects::COLOR,
101        }
102    }
103}
104
105impl super::ComputePipeline {
106    /// Return the dispatch group counts sufficient to cover the given extent.
107    pub fn get_dispatch_for(&self, extent: super::Extent) -> [u32; 3] {
108        let wg_size = self.get_workgroup_size();
109        [
110            (extent.width + wg_size[0] - 1) / wg_size[0],
111            (extent.height + wg_size[1] - 1) / wg_size[1],
112            (extent.depth + wg_size[2] - 1) / wg_size[2],
113        ]
114    }
115}