use crate::pso::{graphics::StencilValue, State};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Comparison {
Never = 0,
Less = 1,
Equal = 2,
LessEqual = 3,
Greater = 4,
NotEqual = 5,
GreaterEqual = 6,
Always = 7,
}
bitflags!(
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ColorMask: u8 {
const RED = 0x1;
const GREEN = 0x2;
const BLUE = 0x4;
const ALPHA = 0x8;
const COLOR = 0x7;
const ALL = 0xF;
const NONE = 0x0;
}
);
impl Default for ColorMask {
fn default() -> Self {
Self::ALL
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Factor {
Zero = 0,
One = 1,
SrcColor = 2,
OneMinusSrcColor = 3,
DstColor = 4,
OneMinusDstColor = 5,
SrcAlpha = 6,
OneMinusSrcAlpha = 7,
DstAlpha = 8,
OneMinusDstAlpha = 9,
ConstColor = 10,
OneMinusConstColor = 11,
ConstAlpha = 12,
OneMinusConstAlpha = 13,
SrcAlphaSaturate = 14,
Src1Color = 15,
OneMinusSrc1Color = 16,
Src1Alpha = 17,
OneMinusSrc1Alpha = 18,
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BlendOp {
Add { src: Factor, dst: Factor },
Sub { src: Factor, dst: Factor },
RevSub { src: Factor, dst: Factor },
Min,
Max,
}
impl BlendOp {
pub const REPLACE: Self = BlendOp::Add {
src: Factor::One,
dst: Factor::Zero,
};
pub const ADD: Self = BlendOp::Add {
src: Factor::One,
dst: Factor::One,
};
pub const ALPHA: Self = BlendOp::Add {
src: Factor::SrcAlpha,
dst: Factor::OneMinusSrcAlpha,
};
pub const PREMULTIPLIED_ALPHA: Self = BlendOp::Add {
src: Factor::One,
dst: Factor::OneMinusSrcAlpha,
};
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BlendState {
pub color: BlendOp,
pub alpha: BlendOp,
}
impl BlendState {
pub const REPLACE: Self = BlendState {
color: BlendOp::REPLACE,
alpha: BlendOp::REPLACE,
};
pub const ADD: Self = BlendState {
color: BlendOp::ADD,
alpha: BlendOp::ADD,
};
pub const MULTIPLY: Self = BlendState {
color: BlendOp::Add {
src: Factor::Zero,
dst: Factor::SrcColor,
},
alpha: BlendOp::Add {
src: Factor::Zero,
dst: Factor::SrcAlpha,
},
};
pub const ALPHA: Self = BlendState {
color: BlendOp::ALPHA,
alpha: BlendOp::PREMULTIPLIED_ALPHA,
};
pub const PREMULTIPLIED_ALPHA: Self = BlendState {
color: BlendOp::PREMULTIPLIED_ALPHA,
alpha: BlendOp::PREMULTIPLIED_ALPHA,
};
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ColorBlendDesc {
pub mask: ColorMask,
pub blend: Option<BlendState>,
}
impl ColorBlendDesc {
pub const EMPTY: Self = ColorBlendDesc {
mask: ColorMask::ALL,
blend: None,
};
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DepthTest {
pub fun: Comparison,
pub write: bool,
}
impl DepthTest {
pub const FAIL: Self = DepthTest {
fun: Comparison::Never,
write: false,
};
pub const PASS_TEST: Self = DepthTest {
fun: Comparison::Always,
write: false,
};
pub const PASS_WRITE: Self = DepthTest {
fun: Comparison::Always,
write: true,
};
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum StencilOp {
Keep = 0,
Zero = 1,
Replace = 2,
IncrementClamp = 3,
DecrementClamp = 4,
Invert = 5,
IncrementWrap = 6,
DecrementWrap = 7,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StencilFace {
pub fun: Comparison,
pub op_fail: StencilOp,
pub op_depth_fail: StencilOp,
pub op_pass: StencilOp,
}
impl Default for StencilFace {
fn default() -> StencilFace {
StencilFace {
fun: Comparison::Never,
op_fail: StencilOp::Keep,
op_depth_fail: StencilOp::Keep,
op_pass: StencilOp::Keep,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Sided<T> {
pub front: T,
pub back: T,
}
impl<T: Copy> Sided<T> {
pub fn new(value: T) -> Self {
Sided {
front: value,
back: value,
}
}
}
pub type StencilValues = State<Sided<StencilValue>>;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StencilTest {
pub faces: Sided<StencilFace>,
pub read_masks: StencilValues,
pub write_masks: StencilValues,
pub reference_values: StencilValues,
}
impl Default for StencilTest {
fn default() -> Self {
StencilTest {
faces: Sided::default(),
read_masks: State::Static(Sided::new(!0)),
write_masks: State::Static(Sided::new(!0)),
reference_values: State::Static(Sided::new(0)),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DepthStencilDesc {
pub depth: Option<DepthTest>,
pub depth_bounds: bool,
pub stencil: Option<StencilTest>,
}
impl DepthStencilDesc {
pub fn uses_depth(&self) -> bool {
self.depth.is_some() || self.depth_bounds
}
pub fn uses_stencil(&self) -> bool {
self.stencil.is_some()
}
}
bitflags!(
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Face: u32 {
const NONE = 0x0;
const FRONT = 0x1;
const BACK = 0x2;
}
);