#[derive(Debug, Clone, Copy)]
pub struct ClearState {
pub color: Option<[f32; 4]>,
pub depth: Option<f32>,
pub stencil: Option<u32>,
}
impl ClearState {
pub fn color(color: [f32; 4]) -> Self {
Self {
color: Some(color),
depth: None,
stencil: None,
}
}
pub fn depth(depth: f32) -> Self {
Self {
color: None,
depth: Some(depth),
stencil: None,
}
}
pub fn color_and_depth(color: [f32; 4], depth: f32) -> Self {
Self {
color: Some(color),
depth: Some(depth),
stencil: None,
}
}
pub fn none() -> Self {
Self {
color: None,
depth: None,
stencil: None,
}
}
pub fn color_load_op(&self) -> wgpu::LoadOp<wgpu::Color> {
match self.color {
Some([r, g, b, a]) => wgpu::LoadOp::Clear(wgpu::Color {
r: r as f64,
g: g as f64,
b: b as f64,
a: a as f64,
}),
None => wgpu::LoadOp::Load,
}
}
pub fn depth_load_op(&self) -> wgpu::LoadOp<f32> {
match self.depth {
Some(d) => wgpu::LoadOp::Clear(d),
None => wgpu::LoadOp::Load,
}
}
pub fn stencil_load_op(&self) -> wgpu::LoadOp<u32> {
match self.stencil {
Some(s) => wgpu::LoadOp::Clear(s),
None => wgpu::LoadOp::Load,
}
}
}
impl Default for ClearState {
fn default() -> Self {
Self::color_and_depth([0.0, 0.0, 0.0, 1.0], 1.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BlendState {
#[default]
Opaque,
Alpha,
Additive,
PremultipliedAlpha,
}
impl BlendState {
pub fn to_wgpu(&self) -> Option<wgpu::BlendState> {
match self {
BlendState::Opaque => None,
BlendState::Alpha => Some(wgpu::BlendState::ALPHA_BLENDING),
BlendState::Additive => Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
}),
BlendState::PremultipliedAlpha => Some(wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct DepthState {
pub write: bool,
pub compare: wgpu::CompareFunction,
}
impl DepthState {
pub fn read_write() -> Self {
Self {
write: true,
compare: wgpu::CompareFunction::Less,
}
}
pub fn read_only() -> Self {
Self {
write: false,
compare: wgpu::CompareFunction::Less,
}
}
pub fn disabled() -> Self {
Self {
write: false,
compare: wgpu::CompareFunction::Always,
}
}
pub fn none() -> Self {
Self::disabled()
}
pub fn to_wgpu(&self, format: wgpu::TextureFormat) -> wgpu::DepthStencilState {
wgpu::DepthStencilState {
format,
depth_write_enabled: self.write,
depth_compare: self.compare,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}
}
}
impl Default for DepthState {
fn default() -> Self {
Self::read_write()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CullState {
None,
Front,
#[default]
Back,
}
impl CullState {
pub fn to_wgpu(&self) -> Option<wgpu::Face> {
match self {
CullState::None => None,
CullState::Front => Some(wgpu::Face::Front),
CullState::Back => Some(wgpu::Face::Back),
}
}
}