Skip to content

Instantly share code, notes, and snippets.

@veryjos
Created December 10, 2018 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/d1c59b75800fe067e9ab91ab9bb906ca to your computer and use it in GitHub Desktop.
Save veryjos/d1c59b75800fe067e9ab91ab9bb906ca to your computer and use it in GitHub Desktop.
use ash::vk::{ Format };
use
#[derive(Copy, Clone, Debug)]
struct ComponentMetadata {
size_bits: usize,
channel: char
}
#[derive(Copy, Clone, Debug)]
struct PixelFormatMetadata {
components: &'static [ComponentMetadata],
vulkan_format: Format
}
#[derive(Clone, Copy, Debug)]
pub enum PixelFormat {
BGRA8,
BGR8,
Depth,
DepthStencil
}
impl PixelFormat {
fn get_metadata(&self) -> PixelFormatMetadata {
match self {
// Color formats
PixelFormat::BGRA8 => PixelFormatMetadata {
components: &'static [
ComponentMetadata { size_bits: 8, channel: 'b' },
ComponentMetadata { size_bits: 8, channel: 'g' },
ComponentMetadata { size_bits: 8, channel: 'r' },
ComponentMetadata { size_bits: 8, channel: 'a' }
],
vulkan_format: Format::B8G8R8A8_UNORM
},
PixelFormat::BGR8 => PixelFormatMetadata {
components: &'static [
ComponentMetadata { size_bits: 8, channel: 'b' },
ComponentMetadata { size_bits: 8, channel: 'g' },
ComponentMetadata { size_bits: 8, channel: 'r' }
],
vulkan_format: Format::B8G8R8_UNORM
},
PixelFormat::Depth => PixelFormatMetadata {
components: &'static [
ComponentMetadata { size_bits: 16, channel: 'd' }
],
vulkan_format: Format::D16_UNORM
},
PixelFormat::DepthStencil => PixelFormatMeta {
components: &'static [
ComponentMetadata { size_bits: 16, channel: 'd' },
ComponentMetadata { size_bits: 8, channel: 'a' }
],
vulkan_format: Format::D16_S8_UNORM
}
}
}
fn has_alpha(&self) -> bool {
self.get_metadata().components.iter().find(|&c| { c.channel == 'a' }).is_some()
}
fn has_stencil(&self) -> bool {
self.get_metadata().components.iter().find(|&c| { c.channel == 's' }).is_some()
}
fn has_depth(&self) -> bool {
self.get_metadata().components.iter().find(|&c| { c.channel == 'd' }).is_some()
}
}
impl Into<Format> for PixelFormat {
fn into(self) -> Format {
self.get_metadata().vulkan_format
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment