♻️ refactor

main
_ 2021-12-18 22:22:20 +00:00
parent ac85cf2bd1
commit 47e835005b
4 changed files with 81 additions and 72 deletions

View File

@ -172,65 +172,23 @@ async fn main () -> Result <()> {
let passes = vec![ let passes = vec![
// Clear everything // Clear everything
Pass { Pass::default ()
iso: IsoGlState { .color_mask ([1, 1, 1, 1])
shader_id: None, .depth_mask (1)
flags: Default::default (), .clone (),
front_face: None,
stencil: None,
depth_func: None,
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (255),
},
},
// Draw world // Draw world
Pass { Pass::default ()
iso: IsoGlState { .shader (&shaders [0])
shader_id: Some (shaders [0].get_id ()), .flags ([
flags: hashmap! { (gl::CULL_FACE, true),
gl::CULL_FACE => true, (gl::DEPTH_TEST, true),
gl::DEPTH_TEST => true, (gl::TEXTURE_2D, true),
gl::TEXTURE_2D => true, (gl::STENCIL_TEST, false),
gl::STENCIL_TEST => false, ].into_iter ())
}, .front_face (FrontFace::Cw)
front_face: Some (FrontFace::Cw), .color_mask ([1, 1, 1, 1])
stencil: Some (StencilState { .depth_mask (1)
func: StencilFuncState { .clone (),
func: StencilFunc::Always,
reference: 0,
mask: 0,
},
op: StencilOpState {
sfail: StencilOp::Keep,
dpfail: StencilOp::Keep,
dppass: StencilOp::Keep,
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (0),
},
},
// Draw UI
Pass {
iso: IsoGlState {
shader_id: Some (shaders [0].get_id ()),
flags: hashmap! {
gl::CULL_FACE => false,
gl::DEPTH_TEST => false,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => false,
},
front_face: None,
stencil: None,
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (0),
},
},
]; ];
let text_stream = TriangleStream { let text_stream = TriangleStream {

View File

@ -613,7 +613,7 @@ impl GameGraphics {
front_face: None, front_face: None,
stencil: None, stencil: None,
depth_func: None, depth_func: None,
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (255), stencil_mask: Some (255),
}, },
@ -642,7 +642,7 @@ impl GameGraphics {
}, },
}), }),
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (0), stencil_mask: Some (0),
}, },
@ -671,7 +671,7 @@ impl GameGraphics {
}, },
}), }),
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((0, 0, 0, 0)), color_mask: Some ([0, 0, 0, 0]),
depth_mask: Some (0), depth_mask: Some (0),
stencil_mask: Some (255), stencil_mask: Some (255),
}, },
@ -700,7 +700,7 @@ impl GameGraphics {
}, },
}), }),
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (0), stencil_mask: Some (0),
}, },
@ -729,7 +729,7 @@ impl GameGraphics {
}, },
}), }),
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (0), stencil_mask: Some (0),
}, },
@ -771,7 +771,7 @@ impl GameGraphics {
}, },
}), }),
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (0), stencil_mask: Some (0),
}, },
@ -802,7 +802,7 @@ impl GameGraphics {
front_face: None, front_face: None,
stencil: None, stencil: None,
depth_func: Some (DepthFunc::Less), depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)), color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1), depth_mask: Some (1),
stencil_mask: Some (0), stencil_mask: Some (0),
}, },
@ -1162,8 +1162,6 @@ impl GameGraphics {
0.0, 256.0, 0.0, 256.0,
]; ];
use std::convert::TryInto;
let eps = 0.0 / 256.0; let eps = 0.0 / 256.0;
let uv: Vec <f32> = vec! [ let uv: Vec <f32> = vec! [

View File

@ -1,4 +1,7 @@
use opengl_rust::prelude::*; use opengl_rust::{
prelude::*,
shader,
};
struct GameState { struct GameState {
logic_frames: u64, logic_frames: u64,

View File

@ -138,6 +138,7 @@ pub struct StencilState {
// These are POD where no extra data is needed // These are POD where no extra data is needed
// to safely use the flags / numbers // to safely use the flags / numbers
#[derive (Clone)]
pub struct IsoGlState { pub struct IsoGlState {
pub shader_id: Option <u32>, pub shader_id: Option <u32>,
@ -146,12 +147,12 @@ pub struct IsoGlState {
pub stencil: Option <StencilState>, pub stencil: Option <StencilState>,
pub depth_func: Option <DepthFunc>, pub depth_func: Option <DepthFunc>,
pub color_mask: Option <(u8, u8, u8, u8)>, pub color_mask: Option <[u8; 4]>,
pub depth_mask: Option <u8>, pub depth_mask: Option <u8>,
pub stencil_mask: Option <u32>, pub stencil_mask: Option <u32>,
} }
impl std::default::Default for IsoGlState { impl Default for IsoGlState {
fn default () -> Self { fn default () -> Self {
Self { Self {
shader_id: None, shader_id: None,
@ -171,7 +172,7 @@ impl std::default::Default for IsoGlState {
pub struct NonIsoGlState { pub struct NonIsoGlState {
} }
impl std::default::Default for NonIsoGlState { impl Default for NonIsoGlState {
fn default () -> Self { fn default () -> Self {
Self { Self {
} }
@ -192,12 +193,61 @@ impl std::default::Default for GlState {
} }
} }
#[derive (Clone, Default)]
pub struct Pass { pub struct Pass {
// In the context of a Pass, "None" means "Don't care" // In the context of a Pass, "None" means "Don't care"
pub iso: IsoGlState, pub iso: IsoGlState,
} }
// Builder methods
impl Pass {
pub fn shader (&mut self, x: &crate::shader_closure::ShaderClosure) -> &mut Self {
self.iso.shader_id = Some (x.get_id ());
self
}
pub fn shader_id <T: Into <Option <u32>>> (&mut self, x: T) -> &mut Self {
self.iso.shader_id = x.into ();
self
}
pub fn color_mask <T: Into <Option <[u8; 4]>>> (&mut self, x: T) -> &mut Self {
self.iso.color_mask = x.into ();
self
}
pub fn depth_mask <T: Into <Option <u8>>> (&mut self, x: T) -> &mut Self {
self.iso.depth_mask = x.into ();
self
}
pub fn stencil_mask <T: Into <Option <u32>>> (&mut self, x: T) -> &mut Self {
self.iso.stencil_mask = x.into ();
self
}
pub fn flag (&mut self, k: u32, v: bool) -> &mut Self {
self.iso.flags.insert (k, v);
self
}
pub fn flags <I: Iterator <Item=(u32, bool)>> (&mut self, i: I) -> &mut Self {
for (k, v) in i {
self.flag (k, v);
}
self
}
pub fn front_face <T: Into <Option <FrontFace>>> (&mut self, x: T) -> &mut Self {
self.iso.front_face = x.into ();
self
}
}
// State-diffing stuff
impl Pass { impl Pass {
pub fn apply_diff (&self, old_state: &mut IsoGlState) { pub fn apply_diff (&self, old_state: &mut IsoGlState) {
let state = &self.iso; let state = &self.iso;
@ -273,7 +323,7 @@ impl Pass {
} }
} }
if let Some ((r, g, b, a)) = &state.color_mask { if let Some ([r, g, b, a]) = &state.color_mask {
if old_state.color_mask != state.color_mask { if old_state.color_mask != state.color_mask {
glezz::color_mask (*r, *g, *b, *a); glezz::color_mask (*r, *g, *b, *a);
old_state.color_mask = state.color_mask; old_state.color_mask = state.color_mask;