use attrib::IntSubType;
use std::default::Default;
use std::fmt;
use state;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum SurfaceError {
UnsupportedSurfaceFormat,
}
#[derive(Copy, Clone, PartialEq)]
pub enum TextureError {
UnsupportedTextureFormat,
UnsupportedTextureSampling,
InvalidTextureInfo(TextureInfo),
IncorrectTextureSize(usize),
}
impl fmt::Debug for TextureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&TextureError::UnsupportedTextureFormat =>
write!(f, "Failed to map a given format to the device"),
&TextureError::UnsupportedTextureSampling =>
write!(
f,
"Failed to map a given multisampled kind to the device"
),
&TextureError::InvalidTextureInfo(info) =>
write!(
f,
"Invalid TextureInfo (width, height, and levels must not \
be zero): {:?}\n",
info
),
&TextureError::IncorrectTextureSize(expected) =>
write!(
f,
"Invalid data size provided to update the texture, \
expected size {:?}",
expected
),
}
}
}
pub type Bits = u8;
pub type NumSamples = u8;
pub type NumFragments = u8;
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum AaMode {
Msaa(NumSamples),
Eqaa(NumSamples, NumFragments),
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
#[repr(u8)]
pub enum Components {
R,
RG,
RGB,
RGBA,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum Format {
Float(Components, ::attrib::FloatSize),
Integer(Components, Bits, ::attrib::IntSubType),
Unsigned(Components, Bits, ::attrib::IntSubType),
Compressed(Compression),
R3G3B2,
RGB5A1,
RGB10A2,
RGB10A2UI,
R11FG11FB10F,
RGB9E5,
DEPTH24STENCIL8,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum Compression {
ETC2_RGB,
ETC2_SRGB,
ETC2_EAC_RGBA8,
}
impl Format {
pub fn get_components(&self) -> Option<Components> {
Some(match *self {
Format::Float(c, _) => c,
Format::Integer(c, _, _) => c,
Format::Unsigned(c, _, _) => c,
Format::Compressed(_) => panic!("Tried to get components of compressed texel!"),
Format::R3G3B2 | Format::R11FG11FB10F | Format::RGB9E5 => Components::RGB,
Format::RGB5A1 | Format::RGB10A2 | Format::RGB10A2UI => Components::RGBA,
Format::DEPTH24STENCIL8 => return None,
})
}
pub fn is_compressed(&self) -> bool {
match *self {
Format::Compressed(_) => true,
_ => false
}
}
}
pub static RGBA8: Format = Format::Unsigned(Components::RGBA, 8, IntSubType::Normalized);
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct SurfaceInfo {
pub width: u16,
pub height: u16,
pub format: Format,
pub aa_mode: Option<AaMode>,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum FilterMethod {
Scale,
Mipmap,
Bilinear,
Trilinear,
Anisotropic(u8)
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum TextureKind {
Texture1D,
Texture1DArray,
Texture2D,
Texture2DArray,
Texture2DMultiSample(AaMode),
Texture2DMultiSampleArray(AaMode),
TextureCube(CubeFace),
Texture3D,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
#[allow(missing_docs)]
pub enum CubeFace {
PosZ,
NegZ,
PosX,
NegX,
PosY,
NegY
}
impl TextureKind {
pub fn get_aa_mode(&self) -> Option<AaMode> {
match *self {
TextureKind::Texture2DMultiSample(aa) => Some(aa),
TextureKind::Texture2DMultiSampleArray(aa) => Some(aa),
_ => None,
}
}
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct TextureInfo {
pub width: u16,
pub height: u16,
pub depth: u16,
pub levels: u8,
pub kind: TextureKind,
pub format: Format,
}
#[allow(missing_docs)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub struct ImageInfo {
pub xoffset: u16,
pub yoffset: u16,
pub zoffset: u16,
pub width: u16,
pub height: u16,
pub depth: u16,
pub format: Format,
pub mipmap: u8,
}
impl Default for ImageInfo {
fn default() -> ImageInfo {
ImageInfo {
xoffset: 0,
yoffset: 0,
zoffset: 0,
width: 0,
height: 1,
depth: 1,
format: RGBA8,
mipmap: 0
}
}
}
impl Default for TextureInfo {
fn default() -> TextureInfo {
TextureInfo {
width: 0,
height: 1,
depth: 1,
levels: -1,
kind: TextureKind::Texture2D,
format: RGBA8,
}
}
}
impl TextureInfo {
pub fn new() -> TextureInfo {
Default::default()
}
pub fn to_image_info(&self) -> ImageInfo {
ImageInfo {
xoffset: 0,
yoffset: 0,
zoffset: 0,
width: self.width,
height: self.height,
depth: self.depth,
format: self.format,
mipmap: 0,
}
}
pub fn to_surface_info(&self) -> SurfaceInfo {
SurfaceInfo {
width: self.width,
height: self.height,
format: self.format,
aa_mode: self.kind.get_aa_mode(),
}
}
pub fn contains(&self, img: &ImageInfo) -> bool {
self.width <= img.xoffset + img.width &&
self.height <= img.yoffset + img.height &&
self.depth <= img.zoffset + img.depth &&
self.format == img.format &&
img.mipmap < self.levels &&
self.kind.get_aa_mode().is_none()
}
}
impl ImageInfo {
pub fn new() -> ImageInfo { Default::default() }
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum WrapMode {
Tile,
Mirror,
Clamp,
}
#[derive(Eq, Ord, PartialEq, PartialOrd, Hash, Copy, Clone, Debug)]
pub enum ComparisonMode {
NoComparison,
CompareRefToTexture(state::Comparison)
}
#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
pub struct SamplerInfo {
pub filtering: FilterMethod,
pub wrap_mode: (WrapMode, WrapMode, WrapMode),
pub lod_bias: f32,
pub lod_range: (f32, f32),
pub comparison: ComparisonMode
}
impl SamplerInfo {
pub fn new(filtering: FilterMethod, wrap: WrapMode) -> SamplerInfo {
SamplerInfo {
filtering: filtering,
wrap_mode: (wrap, wrap, wrap),
lod_bias: 0.0,
lod_range: (-1000.0, 1000.0),
comparison: ComparisonMode::NoComparison
}
}
}