use crate::{
blending::{Blending, BlendingMode},
depth_stencil::{Comparison, StencilOperations, StencilTest, Write},
face_culling::FaceCulling,
scissor::ScissorRegion,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RenderState {
blending: Option<BlendingMode>,
depth_test: Option<Comparison>,
depth_write: Write,
stencil_test: Option<StencilTest>,
stencil_operations: StencilOperations,
face_culling: Option<FaceCulling>,
scissor: Option<ScissorRegion>,
}
impl RenderState {
pub fn set_blending<B>(self, blending: B) -> Self
where
B: Into<Option<Blending>>,
{
RenderState {
blending: blending.into().map(|x| x.into()),
..self
}
}
pub fn set_blending_separate(self, blending_rgb: Blending, blending_alpha: Blending) -> Self {
RenderState {
blending: Some(BlendingMode::Separate {
rgb: blending_rgb,
alpha: blending_alpha,
}),
..self
}
}
pub fn blending(&self) -> Option<BlendingMode> {
self.blending
}
pub fn set_depth_test<D>(self, depth_test: D) -> Self
where
D: Into<Option<Comparison>>,
{
let depth_test = depth_test.into();
RenderState { depth_test, ..self }
}
pub fn depth_test(&self) -> Option<Comparison> {
self.depth_test
}
pub fn set_depth_write(self, depth_write: Write) -> Self {
RenderState {
depth_write,
..self
}
}
pub fn depth_write(&self) -> Write {
self.depth_write
}
pub fn set_stencil_test(self, stencil_test: impl Into<Option<StencilTest>>) -> Self {
let stencil_test = stencil_test.into();
RenderState {
stencil_test,
..self
}
}
pub fn stencil_test(&self) -> Option<&StencilTest> {
self.stencil_test.as_ref()
}
pub fn set_stencil_operations(self, stencil_operations: StencilOperations) -> Self {
RenderState {
stencil_operations,
..self
}
}
pub fn stencil_operations(&self) -> &StencilOperations {
&self.stencil_operations
}
pub fn set_face_culling<FC>(self, face_culling: FC) -> Self
where
FC: Into<Option<FaceCulling>>,
{
RenderState {
face_culling: face_culling.into(),
..self
}
}
pub fn face_culling(&self) -> Option<FaceCulling> {
self.face_culling
}
pub fn set_scissor<SR>(self, scissor: SR) -> Self
where
SR: Into<Option<ScissorRegion>>,
{
RenderState {
scissor: scissor.into(),
..self
}
}
pub fn scissor(&self) -> &Option<ScissorRegion> {
&self.scissor
}
}
impl Default for RenderState {
fn default() -> Self {
RenderState {
blending: None,
depth_test: Some(Comparison::Less),
depth_write: Write::On,
stencil_test: None,
stencil_operations: StencilOperations::default(),
face_culling: None,
scissor: None,
}
}
}