Skip to main content

molgfx_gpu/descriptors/
texture.rs

1//! Texture descriptors and formats.
2
3/// The formats the engine renders with. A deliberate subset: every format
4/// here is universally supported on the targeted backends, and backends
5/// match on it exhaustively, so growing the set is a deliberate change.
6#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7pub enum TextureFormat {
8    /// 8-bit RGBA, linear.
9    Rgba8Unorm,
10    /// Signed normalized 8-bit RGBA; compact generated direction fields.
11    Rgba8Snorm,
12    /// 8-bit RGBA, sRGB-encoded.
13    Rgba8UnormSrgb,
14    /// 8-bit BGRA, linear (a common swapchain format).
15    Bgra8Unorm,
16    /// 8-bit BGRA, sRGB-encoded (a common swapchain format).
17    Bgra8UnormSrgb,
18    /// 16-bit float RGBA; the linear HDR working format.
19    Rgba16Float,
20    /// Two 16-bit float channels; compact screen-space motion vectors.
21    Rg16Float,
22    /// Two 32-bit float channels; conservative scalar-volume min/max pairs.
23    Rg32Float,
24    /// Single 8-bit channel; ambient-occlusion and masks.
25    R8Unorm,
26    /// Single 32-bit unsigned integer; the entity-id channel.
27    R32Uint,
28    /// Single 32-bit float channel; scientific scalar density grids.
29    R32Float,
30    /// 32-bit float depth, used with reversed depth.
31    Depth32Float,
32}
33
34/// Texture dimensionality supported by the portable renderer.
35#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
36pub enum TextureDimension {
37    /// A conventional image or render target.
38    #[default]
39    D2,
40    /// A volumetric scalar field.
41    D3,
42}
43
44impl TextureFormat {
45    /// Whether this is a depth format.
46    #[must_use]
47    pub fn is_depth(self) -> bool {
48        matches!(self, Self::Depth32Float)
49    }
50}
51
52bitflags::bitflags! {
53    /// How a texture may be used.
54    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
55    pub struct TextureUsage: u32 {
56        /// Rendered to as a color or depth attachment.
57        const RENDER_ATTACHMENT = 1;
58        /// Sampled from shaders.
59        const TEXTURE_BINDING = 1 << 1;
60        /// Written from compute as a storage texture.
61        const STORAGE_BINDING = 1 << 2;
62        /// Source of copies.
63        const COPY_SRC = 1 << 3;
64        /// Destination of copies.
65        const COPY_DST = 1 << 4;
66    }
67}
68
69/// Everything needed to create a texture.
70#[derive(Clone, Copy, PartialEq, Eq, Debug)]
71pub struct TextureDesc {
72    /// Debug label.
73    pub label: &'static str,
74    /// Width in texels.
75    pub width: u32,
76    /// Height in texels.
77    pub height: u32,
78    /// Depth in texels; one for 2-D textures.
79    pub depth: u32,
80    /// Whether the texture is 2-D or 3-D.
81    pub dimension: TextureDimension,
82    /// Texel format.
83    pub format: TextureFormat,
84    /// Permitted usages.
85    pub usage: TextureUsage,
86}
87
88/// View parameters; the default views the whole texture.
89#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
90pub struct TextureViewDesc {}
91
92/// One tightly described host-to-texture upload.
93#[derive(Clone, Copy, Debug)]
94pub struct TextureWrite<'a> {
95    /// Destination origin in texels.
96    pub origin: [u32; 3],
97    /// Written extent in texels.
98    pub size: [u32; 3],
99    /// Byte stride between adjacent rows.
100    pub bytes_per_row: u32,
101    /// Rows between adjacent depth slices.
102    pub rows_per_image: u32,
103    /// Borrowed source allocation.
104    pub data: &'a [u8],
105}