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 const fn block_info(&self) -> super::TexelBlockInfo {
46        const fn uncompressed(size: u8) -> super::TexelBlockInfo {
47            super::TexelBlockInfo {
48                dimensions: (1, 1),
49                size,
50            }
51        }
52        const 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::Depth32FloatStencil8Uint => uncompressed(5),
78            Self::Stencil8Uint => uncompressed(1),
79            Self::Bc1Unorm => cx_bc(8),
80            Self::Bc1UnormSrgb => cx_bc(8),
81            Self::Bc2Unorm => cx_bc(16),
82            Self::Bc2UnormSrgb => cx_bc(16),
83            Self::Bc3Unorm => cx_bc(16),
84            Self::Bc3UnormSrgb => cx_bc(16),
85            Self::Bc4Unorm => cx_bc(8),
86            Self::Bc4Snorm => cx_bc(8),
87            Self::Bc5Unorm => cx_bc(16),
88            Self::Bc5Snorm => cx_bc(16),
89            Self::Bc6hUfloat => cx_bc(16),
90            Self::Bc6hFloat => cx_bc(16),
91            Self::Bc7Unorm => cx_bc(16),
92            Self::Bc7UnormSrgb => cx_bc(16),
93            Self::Rgb10a2Unorm => uncompressed(4),
94            Self::Rg11b10Ufloat => uncompressed(4),
95            Self::Rgb9e5Ufloat => uncompressed(4),
96        }
97    }
98
99    pub fn aspects(&self) -> super::TexelAspects {
100        match *self {
101            Self::Depth32Float => super::TexelAspects::DEPTH,
102
103            Self::Depth32FloatStencil8Uint => {
104                super::TexelAspects::DEPTH | super::TexelAspects::STENCIL
105            }
106
107            Self::Stencil8Uint => super::TexelAspects::STENCIL,
108
109            _ => super::TexelAspects::COLOR,
110        }
111    }
112}
113
114impl super::TextureColor {
115    pub const fn stencil_clear_value(&self) -> u32 {
116        match self {
117            crate::TextureColor::TransparentBlack => 0,
118            crate::TextureColor::OpaqueBlack => !0,
119            crate::TextureColor::White => !0,
120        }
121    }
122
123    pub const fn depth_clear_value(&self) -> f32 {
124        match self {
125            crate::TextureColor::TransparentBlack => 0.0,
126            crate::TextureColor::OpaqueBlack => 0.0,
127            crate::TextureColor::White => 1.0,
128        }
129    }
130}
131
132impl super::ComputePipeline {
133    /// Return the dispatch group counts sufficient to cover the given extent.
134    pub fn get_dispatch_for(&self, extent: super::Extent) -> [u32; 3] {
135        let wg_size = self.get_workgroup_size();
136        [
137            (extent.width + wg_size[0] - 1) / wg_size[0],
138            (extent.height + wg_size[1] - 1) / wg_size[1],
139            (extent.depth + wg_size[2] - 1) / wg_size[2],
140        ]
141    }
142}