From 5dde94cdf9f26e20440a62363fcbba2b8fb24553 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 22 Feb 2020 01:37:47 -0800 Subject: [PATCH] working on additive blending for determination of winding order. Saving here because this bug is very pretty --- resources/shaders/color-passthrough.frag | 7 ++- resources/shaders/color-passthrough.vert | 2 + resources/shaders/simple_text.frag | 10 +++- src/canvas/canvas_state.rs | 66 +++++++++++++++++------- src/canvas/managed/shader/text_shader.rs | 35 +++++++++---- src/drawables/text.rs | 10 ++-- src/util/vertex.rs | 2 +- 7 files changed, 97 insertions(+), 35 deletions(-) diff --git a/resources/shaders/color-passthrough.frag b/resources/shaders/color-passthrough.frag index 28779dd1..010d3de1 100644 --- a/resources/shaders/color-passthrough.frag +++ b/resources/shaders/color-passthrough.frag @@ -7,7 +7,12 @@ layout(location = 0) in vec4 out_color; layout(location = 0) out vec4 f_color; void main() { - f_color = out_color; + if (int(out_color.w) % 2 == 0) { + + f_color = out_color; + f_color.w = 255.0; + } + } diff --git a/resources/shaders/color-passthrough.vert b/resources/shaders/color-passthrough.vert index 12ee3717..3d787649 100644 --- a/resources/shaders/color-passthrough.vert +++ b/resources/shaders/color-passthrough.vert @@ -10,6 +10,8 @@ layout(location = 0) out vec4 out_color; void main() { out_color = color; gl_Position = vec4(v_position, 1.0); + + } diff --git a/resources/shaders/simple_text.frag b/resources/shaders/simple_text.frag index c6a39e9f..298485ea 100644 --- a/resources/shaders/simple_text.frag +++ b/resources/shaders/simple_text.frag @@ -8,5 +8,13 @@ layout(location = 0) in vec4 out_color; layout(location = 0) out vec4 f_color; void main() { - f_color = out_color; + + + if (int(out_color.w) % 2 == 0) { + + f_color = out_color; + f_color.w = 255.0; + } + + //f_color = out_color; } diff --git a/src/canvas/canvas_state.rs b/src/canvas/canvas_state.rs index a2fe1592..530a5ade 100644 --- a/src/canvas/canvas_state.rs +++ b/src/canvas/canvas_state.rs @@ -392,7 +392,7 @@ impl CanvasState { let mut image_vertex_buffer: HashMap, Vec> = HashMap::new(); let mut text_instances: HashMap, Vec> = HashMap::new(); - let mut text_vertex_buffer: Vec = Vec::new(); + let mut text_vertex_buffer: Vec = Vec::new(); for value in canvas_frame.map { match value { @@ -542,29 +542,58 @@ impl CanvasState { } } - self.dynamic_state = DynamicState { - line_width: None, - viewports: self.dynamic_state.viewports.clone(), - scissors: None, - compare_mask: Some(DynamicStencilValue { - face: StencilFaceFlags::StencilFrontAndBack, - value: 0xFF, - }), - write_mask: Some(DynamicStencilValue { - face: StencilFaceFlags::StencilFrontAndBack, - value: 0xFF, - }), - reference: Some(DynamicStencilValue { - face: StencilFaceFlags::StencilFrontAndBack, - value: 0xFF, - }), - }; + // Text let mut shader = self.shader_buffers.get( self.get_shader_handle(String::from("simple_text")) .unwrap().clone().get_handle() as usize ).unwrap(); +// +// self.dynamic_state = DynamicState { +// line_width: None, +// viewports: self.dynamic_state.viewports.clone(), +// scissors: None, +// compare_mask: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0x00, +// }), +// write_mask: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0xFF, +// }), +// reference: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0x00, +// }), +// }; +// +// if !allocated_buffers.text_vertex_buffer.is_empty() { +// command_buffer = command_buffer.draw( +// shader.get_pipeline().clone(), +// &self.dynamic_state.clone(), +// allocated_buffers.text_vertex_buffer.clone(), +// (), (), +// ).unwrap(); +// } +// +// self.dynamic_state = DynamicState { +// line_width: None, +// viewports: self.dynamic_state.viewports.clone(), +// scissors: None, +// compare_mask: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0xFF, +// }), +// write_mask: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0x00, +// }), +// reference: Some(DynamicStencilValue { +// face: StencilFaceFlags::StencilFrontAndBack, +// value: 0x00, +// }), +// }; if !allocated_buffers.text_vertex_buffer.is_empty() { command_buffer = command_buffer.draw( @@ -575,6 +604,7 @@ impl CanvasState { ).unwrap(); } + command_buffer .end_render_pass() .unwrap() diff --git a/src/canvas/managed/shader/text_shader.rs b/src/canvas/managed/shader/text_shader.rs index 15b3231a..85e54451 100644 --- a/src/canvas/managed/shader/text_shader.rs +++ b/src/canvas/managed/shader/text_shader.rs @@ -17,6 +17,7 @@ use crate::canvas::managed::shader::generic_shader::GenericShader; use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef; use crate::canvas::managed::ShaderSpecializationConstants; use crate::util::vertex::ColorVertex3D; +use vulkano::pipeline::blend::{LogicOp, AttachmentBlend, BlendOp, BlendFactor}; /// CanvasShader holds the pipeline and render pass for the input shader source @@ -80,19 +81,19 @@ impl CompiledShader for TextShader { depth_write: true, depth_bounds_test: DepthBounds::Disabled, stencil_front: Stencil { - compare: Compare::Equal, - pass_op: StencilOp::IncrementAndWrap, - fail_op: StencilOp::DecrementAndClamp, + compare: Compare::NotEqual, + pass_op: StencilOp::Keep, + fail_op: StencilOp::IncrementAndWrap, depth_fail_op: StencilOp::Keep, compare_mask: None, write_mask: None, reference: None, }, stencil_back: Stencil { - compare: Compare::Equal, - pass_op: StencilOp::Invert, - fail_op: StencilOp::Zero, - depth_fail_op: StencilOp::Zero, + compare: Compare::NotEqual, + pass_op: StencilOp::Keep, + fail_op: StencilOp::DecrementAndWrap, + depth_fail_op: StencilOp::Keep, compare_mask: None, write_mask: None, reference: None, @@ -101,6 +102,21 @@ impl CompiledShader for TextShader { let vertex_definition = RuntimeVertexDef::from_primitive(0); + + let blend = AttachmentBlend { + enabled: true, + color_op: BlendOp::Add, + color_source: BlendFactor::SrcAlpha, + color_destination: BlendFactor::OneMinusSrcAlpha, + alpha_op: BlendOp::Add, + alpha_source: BlendFactor::SrcAlpha, + alpha_destination: BlendFactor::OneMinusSrcAlpha, + mask_red: true, + mask_green: true, + mask_blue: true, + mask_alpha: true, + }; + TextShader { graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() @@ -124,8 +140,9 @@ impl CompiledShader for TextShader { third_constant: 0.0, }) - .depth_stencil(stencil) - + .depth_stencil_simple_depth() + .blend_collective(blend) + .blend_logic_op(LogicOp::And) // We have to indicate which subpass of which render pass this pipeline is going to be used // in. The pipeline will only be usable from this particular subpass. .render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) diff --git a/src/drawables/text.rs b/src/drawables/text.rs index d32be563..02b1c04f 100644 --- a/src/drawables/text.rs +++ b/src/drawables/text.rs @@ -26,7 +26,7 @@ impl Text { }, ColorVertex3D { v_position: [-1.0, 1.0, normalized_depth], - color: [1.0, 1.0, 1.0, 1.0], + color: [1.0, 0.8, 1.0, 1.0], }, ColorVertex3D { v_position: [-0.25, 0.0, normalized_depth], @@ -34,7 +34,7 @@ impl Text { }, ColorVertex3D { v_position: [-0.25, 0.0, normalized_depth], - color: [1.0, 1.0, 1.0, 1.0], + color: [0.8, 1.0, 1.0, 1.0], }, ColorVertex3D { v_position: [-1.0, 1.0, normalized_depth], @@ -42,7 +42,7 @@ impl Text { }, ColorVertex3D { v_position: [0.0, 0.5, normalized_depth], - color: [1.0, 1.0, 1.0, 1.0], + color: [1.0, 1.0, 0.8, 1.0], }, ColorVertex3D { v_position: [0.25, 0.0, normalized_depth], @@ -50,7 +50,7 @@ impl Text { }, ColorVertex3D { v_position: [-1.0, 1.0, normalized_depth], - color: [1.0, 1.0, 1.0, 1.0], + color: [1.0, 0.8, 1.0, 1.0], }, ColorVertex3D { v_position: [0.0, 0.5, normalized_depth], @@ -119,7 +119,7 @@ impl Text { }; Text { - verts: VertexTypes::ColorType(verts), + verts: VertexTypes::TextType(verts), position: position, size: size, } diff --git a/src/util/vertex.rs b/src/util/vertex.rs index 567c7f93..a6871ef3 100644 --- a/src/util/vertex.rs +++ b/src/util/vertex.rs @@ -56,7 +56,7 @@ pub enum VertexTypes { ImageType(Vec, Arc), ColorType(Vec), ThreeDType(Vec), - TextType(Vec), + TextType(Vec), }