Fix GPU abstraction

This commit is contained in:
Patrick Walton 2019-03-05 13:22:11 -08:00
parent 8b4c06fe2a
commit 6b9adcb6fc
3 changed files with 42 additions and 13 deletions

View File

@ -48,15 +48,25 @@ impl GLDevice {
BlendState::Off => {
gl::Disable(gl::BLEND); ck();
}
BlendState::RGBOneAlphaOne => {
gl::BlendEquation(gl::FUNC_ADD); ck();
gl::BlendFunc(gl::ONE, gl::ONE); ck();
gl::Enable(gl::BLEND); ck();
}
BlendState::RGBOneAlphaOneMinusSrcAlpha => {
gl::BlendEquation(gl::FUNC_ADD); ck();
gl::BlendFuncSeparate(gl::ONE,
gl::ONE_MINUS_SRC_ALPHA,
gl::ONE,
gl::ONE); ck();
gl::Enable(gl::BLEND); ck();
}
BlendState::RGBOneAlphaOne => {
gl::BlendFunc(gl::ONE, gl::ONE); ck();
BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha => {
gl::BlendEquation(gl::FUNC_ADD); ck();
gl::BlendFuncSeparate(gl::SRC_ALPHA,
gl::ONE_MINUS_SRC_ALPHA,
gl::ONE,
gl::ONE); ck();
gl::Enable(gl::BLEND); ck();
}
}
@ -67,9 +77,9 @@ impl GLDevice {
gl::Disable(gl::DEPTH_TEST); ck();
}
Some(ref state) => {
gl::Enable(gl::DEPTH_TEST); ck();
gl::DepthFunc(state.func.to_gl_depth_func()); ck();
gl::DepthMask(state.write as GLboolean); ck();
gl::Enable(gl::DEPTH_TEST); ck();
}
}
@ -82,8 +92,13 @@ impl GLDevice {
gl::StencilFunc(state.func.to_gl_stencil_func(),
state.reference as GLint,
state.mask); ck();
let pass_action = if state.pass_replace { gl::REPLACE } else { gl::KEEP };
let (pass_action, write_mask) = if state.write {
(gl::REPLACE, state.mask)
} else {
(gl::KEEP, 0)
};
gl::StencilOp(gl::KEEP, gl::KEEP, pass_action); ck();
gl::StencilMask(write_mask);
gl::Enable(gl::STENCIL_TEST); ck();
}
}
@ -98,7 +113,9 @@ impl GLDevice {
unsafe {
match render_state.blend {
BlendState::Off => {}
BlendState::RGBOneAlphaOneMinusSrcAlpha | BlendState::RGBOneAlphaOne => {
BlendState::RGBOneAlphaOneMinusSrcAlpha |
BlendState::RGBOneAlphaOne |
BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha => {
gl::Disable(gl::BLEND); ck();
}
}
@ -108,6 +125,7 @@ impl GLDevice {
}
if render_state.stencil.is_some() {
gl::StencilMask(!0); ck();
gl::Disable(gl::STENCIL_TEST); ck();
}
@ -436,14 +454,17 @@ impl Device for GLDevice {
unsafe {
let mut flags = 0;
if let Some(color) = color {
gl::ColorMask(gl::TRUE, gl::TRUE, gl::TRUE, gl::TRUE); ck();
gl::ClearColor(color.x(), color.y(), color.z(), color.w()); ck();
flags |= gl::COLOR_BUFFER_BIT;
}
if let Some(depth) = depth {
gl::DepthMask(gl::TRUE); ck();
gl::ClearDepth(depth as GLdouble); ck();
flags |= gl::DEPTH_BUFFER_BIT;
}
if let Some(stencil) = stencil {
gl::StencilMask(!0); ck();
gl::ClearStencil(stencil as GLint); ck();
flags |= gl::STENCIL_BUFFER_BIT;
}

View File

@ -323,7 +323,7 @@ impl<D> Renderer<D> where D: Device {
self.device.set_uniform(&self.mask_tile_program.view_box_origin_uniform,
UniformData::Vec2(F32x4::default()));
let render_state = RenderState {
blend: BlendState::RGBOneAlphaOneMinusSrcAlpha,
blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha,
stencil: self.stencil_state(),
..RenderState::default()
};
@ -400,7 +400,7 @@ impl<D> Renderer<D> where D: Device {
}
}
self.device.draw_arrays(Primitive::TriangleFan, 4, &RenderState {
blend: BlendState::RGBOneAlphaOneMinusSrcAlpha,
blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha,
..RenderState::default()
});
}
@ -421,7 +421,7 @@ impl<D> Renderer<D> where D: Device {
func: StencilFunc::Always,
reference: 1,
mask: 1,
pass_replace: true,
write: true,
}),
color_mask: false,
..RenderState::default()
@ -467,7 +467,7 @@ impl<D> Renderer<D> where D: Device {
return None;
}
Some(StencilState { func: StencilFunc::Equal, reference: 1, mask: 1, pass_replace: false })
Some(StencilState { func: StencilFunc::Equal, reference: 1, mask: 1, write: false })
}
}

View File

@ -165,7 +165,7 @@ pub enum Primitive {
Lines,
}
#[derive(Clone, Default, Debug)]
#[derive(Clone, Debug)]
pub struct RenderState {
pub blend: BlendState,
pub depth: Option<DepthState>,
@ -176,8 +176,9 @@ pub struct RenderState {
#[derive(Clone, Copy, Debug)]
pub enum BlendState {
Off,
RGBOneAlphaOneMinusSrcAlpha,
RGBOneAlphaOne,
RGBOneAlphaOneMinusSrcAlpha,
RGBSrcAlphaAlphaOneMinusSrcAlpha,
}
#[derive(Clone, Copy, Default, Debug)]
@ -197,7 +198,7 @@ pub struct StencilState {
pub func: StencilFunc,
pub reference: u32,
pub mask: u32,
pub pass_replace: bool,
pub write: bool,
}
#[derive(Clone, Copy, Debug)]
@ -207,6 +208,13 @@ pub enum StencilFunc {
NotEqual,
}
impl Default for RenderState {
#[inline]
fn default() -> RenderState {
RenderState { blend: BlendState::default(), depth: None, stencil: None, color_mask: true }
}
}
impl Default for BlendState {
#[inline]
fn default() -> BlendState {
@ -217,7 +225,7 @@ impl Default for BlendState {
impl Default for StencilState {
#[inline]
fn default() -> StencilState {
StencilState { func: StencilFunc::default(), reference: 0, mask: !0, pass_replace: false }
StencilState { func: StencilFunc::default(), reference: 0, mask: !0, write: false }
}
}