From 6b9adcb6fc226ec21e76ffa913947fc64a04f76c Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 5 Mar 2019 13:22:11 -0800 Subject: [PATCH] Fix GPU abstraction --- gl/src/device.rs | 31 ++++++++++++++++++++++++++----- gl/src/renderer.rs | 8 ++++---- gpu/src/lib.rs | 16 ++++++++++++---- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/gl/src/device.rs b/gl/src/device.rs index f2c314a5..0906ac14 100644 --- a/gl/src/device.rs +++ b/gl/src/device.rs @@ -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; } diff --git a/gl/src/renderer.rs b/gl/src/renderer.rs index c5df17e8..f1af2e20 100644 --- a/gl/src/renderer.rs +++ b/gl/src/renderer.rs @@ -323,7 +323,7 @@ impl Renderer 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 Renderer where D: Device { } } self.device.draw_arrays(Primitive::TriangleFan, 4, &RenderState { - blend: BlendState::RGBOneAlphaOneMinusSrcAlpha, + blend: BlendState::RGBSrcAlphaAlphaOneMinusSrcAlpha, ..RenderState::default() }); } @@ -421,7 +421,7 @@ impl Renderer 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 Renderer 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 }) } } diff --git a/gpu/src/lib.rs b/gpu/src/lib.rs index a81ab8f9..ff0b8fdb 100644 --- a/gpu/src/lib.rs +++ b/gpu/src/lib.rs @@ -165,7 +165,7 @@ pub enum Primitive { Lines, } -#[derive(Clone, Default, Debug)] +#[derive(Clone, Debug)] pub struct RenderState { pub blend: BlendState, pub depth: Option, @@ -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 } } }