From 8721daed3097bd7a3b032bfe76d906d78c060ba8 Mon Sep 17 00:00:00 2001 From: tsuki Date: Wed, 10 Jul 2024 22:58:03 +0800 Subject: [PATCH] sync --- src/components/shader.rs | 2 +- src/components/snippets/mod.rs | 49 +++++--- src/graphics/collections/agg_fast_path.rs | 138 ++++++++++++++++++++-- src/graphics/transforms/polar.rs | 1 + src/graphics/transforms/position.rs | 1 + src/graphics/transforms/trackball.rs | 1 + src/graphics/transforms/viewport.rs | 1 + src/utils/parser.rs | 4 +- 8 files changed, 166 insertions(+), 31 deletions(-) diff --git a/src/components/shader.rs b/src/components/shader.rs index fb0c72d..27008b4 100644 --- a/src/components/shader.rs +++ b/src/components/shader.rs @@ -191,7 +191,7 @@ mod utils { body.push_str("}\n\n"); let input = header + &body; - let result = Snippet::new("fetch_uniform", CodeType::Code(input), None).unwrap(); + let result = Snippet::new("fetch_uniform", CodeType::Code(input), false, None).unwrap(); result } diff --git a/src/components/snippets/mod.rs b/src/components/snippets/mod.rs index 5f36fa2..3ef013f 100644 --- a/src/components/snippets/mod.rs +++ b/src/components/snippets/mod.rs @@ -37,15 +37,16 @@ pub struct Snippet { name: &'static str, parsed: CodeBlock, link: Option>, - alias: HashMap, + alias: Option>, chained: String, - main: String, + main: Option, } impl Snippet { pub fn new>( name: &'static str, code: CodeType, + mangling: bool, main: Option, ) -> Result { let code = match code { @@ -71,15 +72,20 @@ impl Snippet { let mut parsed_code = CodeBlock::new(&merged_code)?; let call = if let Some(main) = main { - main + Some(main) } else { parsed_code .all_functions() .first() .map(|f| f.borrow().name.clone()) - .unwrap() }; - let alias = parsed_code.mangling(id.to_string()); + + let alias = if mangling { + let alias = parsed_code.mangling(id.to_string()); + Some(alias) + } else { + None + }; let chained = format!( "// START {} //\n{}\n// END {} //\n", @@ -111,15 +117,22 @@ impl Snippet { self } - pub fn call(&self, paras: &Vec) -> String { - if let Some(link) = &self.link { - let call_name = self.alias.get(&self.main).unwrap(); - let c = link.call(paras); - format!("{}({})", call_name, c) - } else { - let call_name = self.alias.get(&self.main).unwrap(); - format!("{}({})", call_name, paras.join(", ")) - } + pub fn call(&self, paras: &Vec) -> Option { + (self.main.as_ref()).map(|name| { + if let Some(link) = &self.link { + let call_name = self.alias.as_ref().map(|a| a.get(name).unwrap()).unwrap_or(name); + let c = link.call(paras); + if let Some(c) = c { + return format!("{}({})", call_name, c); + } else { + return format!("{}()", call_name); + } + } else { + let call_name = self.alias.as_ref().map(|a| a.get(name).unwrap()).unwrap_or(name); + // let call_name = self.alias.get(name).unwrap(); + format!("{}({})", call_name, paras.join(", ")) + } + }) } pub fn prepare_code(&self) -> &str { @@ -127,7 +140,7 @@ impl Snippet { } pub fn find_symbol(&self, name: &str) -> Option<&String> { - self.alias.get(name) + self.alias.as_ref().map(|a| a.get(name)).flatten() } pub fn find_variable(&self, name: &str) -> Option<&Rc>> { @@ -151,7 +164,7 @@ impl Add for Snippet { let code = rhs.parsed.to_string(); raw_code.push_str(&code); - Snippet::new(self.name, CodeType::Code(raw_code), None).unwrap() + Snippet::new(self.name, CodeType::Code(raw_code),false, None).unwrap() } } @@ -179,9 +192,9 @@ mod tests { } "#; - let snippet = Snippet::new("polar", CodeType::Code(code), None).unwrap(); + let snippet = Snippet::new("polar", CodeType::Code(code), true, None).unwrap(); - let snippet2 = Snippet::new("polar2", CodeType::Code(code), None).unwrap(); + let snippet2 = Snippet::new("polar2", CodeType::Code(code),true, None).unwrap(); let snippet3 = snippet.clone() + snippet2.clone(); diff --git a/src/graphics/collections/agg_fast_path.rs b/src/graphics/collections/agg_fast_path.rs index 8d62212..2043d8d 100644 --- a/src/graphics/collections/agg_fast_path.rs +++ b/src/graphics/collections/agg_fast_path.rs @@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut}; use bytemuck::{Pod, Zeroable}; -use crate::components::{fetchcode, CodeComponent, CodeType, Program, Shader}; +use crate::components::{fetchcode, CodeComponent, CodeType, Program, Shader, Snippet}; use crate::errors::*; use crate::graphics::transforms::viewport::Viewport; use crate::graphics::transforms::Transform; @@ -13,7 +13,7 @@ use super::Colletion; struct AggFastPath { program: Program, - buffer: Vec, + buffer: Vec, } impl AggFastPath { @@ -28,7 +28,22 @@ impl AggFastPath { CodeType::::Path("agg-fast-path.frag".into()), )?; - let vertex = vertex.add_snippet_after(fetchcode([], "agg_fast")); + let input_snippet = Snippet::new("input",CodeType::Code(" + layout(location = 0) in vec3 prev; + layout(location = 1) in vec3 curr; + layout(location = 2) in vec3 next; + + uniform float linewidth; + + unifrom float antialias; + + uniform vec4 color; + + ") ,false, None)?; + + print!("{}", input_snippet); + + let vertex = vertex.add_snippet_before(input_snippet + fetchcode([], "agg_fast")); let mut program = Program::new(vertex, fragment, None, ""); @@ -37,7 +52,7 @@ impl AggFastPath { Ok(Self { program, - buffer: Vec::with_capacity(500), + buffer: Vec::with_capacity(128), }) } } @@ -46,15 +61,16 @@ impl Colletion for AggFastPath { type Item = Path; fn append(&mut self, item: Self::Item) { - self.buffer.push(item); + self.buffer.extend(item.points); } } impl Graphics for AggFastPath { fn draw(&self) -> Result<()> { - for path in self.buffer.iter() { - let p: &[u8] = &path; - } + + use bytemuck::cast_slice; + let b:&[u8] = cast_slice(&self.buffer); + Ok(()) } } @@ -76,6 +92,52 @@ pub struct Path { is_empty: bool, } +impl Default for Path { + + fn default() -> Self { + Self { + points: Vec::with_capacity(500), + is_closed: false, + is_empty: true + } + } + +} + +pub struct PathBuilder { + is_closed: bool, +} + +impl Default for PathBuilder{ + + fn default() -> Self { + Self { + is_closed: false + } + } + +} + +impl PathBuilder { + + pub fn is_closed(&mut self , is_closed: bool) -> &mut Self { + self.is_closed = is_closed; + self + } + + + pub fn build(&self) -> Path { + + Path { + points: Vec::with_capacity(500), + is_closed: self.is_closed, + is_empty: true, + } + + } + +} + impl Path { pub fn new(is_closed: bool) -> Self { Self { @@ -85,6 +147,11 @@ impl Path { } } + pub fn builder() -> PathBuilder { + PathBuilder::default() + } + + pub fn push(&mut self, point: [f32; 3]) { if self.is_empty { self.points.push(Point { @@ -107,21 +174,33 @@ impl Path { let next = point; self.points.push(Point { prev, curr, next }); self.points[len - 1].next = curr; + + if len == 2 { + self.points[0].next = curr; + } } } pub fn finish(&mut self) { if self.is_closed { let len = self.points.len(); + let curr = self.points.first().unwrap().curr; let prev = self.points[len - 1].curr; - let curr = self.points[0].curr; - let next = self.points[1].curr; + let next = self.points[1].next; self.points.push(Point { prev, curr, next }); self.points[len - 1].next = curr; } + + self.points.push(self.points.last().unwrap().clone()); } + + pub fn len(&self) -> usize { + self.points.len() - 2 + } + } + impl Deref for Path { type Target = [u8]; @@ -137,3 +216,42 @@ impl DerefMut for Path { cast_slice_mut(&mut self.points) } } + + +mod test { + + use crate::graphics::transforms::{polar::Polar, position::Position, trackball::Trackball}; + + use super::*; + + #[test] + fn test_path() { + + let mut path = Path::builder().is_closed(false).build(); + + path.push([9.0, 9.0, 9.0]); + + path.push([9.0, 19.0, 9.0]); + + path.finish(); + + println!("{:?}, len: {}", path, path.len()); + } + + #[test] + fn test_agg() { + + let viewport = Trackball::new().unwrap(); + let trans = Polar::new().unwrap(); + let position = Position::new().unwrap(); + + let transform = viewport.chain(trans.chain(position)); + + let viewport = &Viewport::new().unwrap(); + + let agg_path = AggFastPath::new(&transform, &viewport).unwrap(); + + + + } +} \ No newline at end of file diff --git a/src/graphics/transforms/polar.rs b/src/graphics/transforms/polar.rs index fd63bfa..9b3377d 100644 --- a/src/graphics/transforms/polar.rs +++ b/src/graphics/transforms/polar.rs @@ -15,6 +15,7 @@ impl Polar { let snippets = Snippet::new( "polar", CodeType::<&'static str>::Path("transform/polar.glsl".into()), + true, Some("forward".to_string()), )?; diff --git a/src/graphics/transforms/position.rs b/src/graphics/transforms/position.rs index b1971ea..209774d 100644 --- a/src/graphics/transforms/position.rs +++ b/src/graphics/transforms/position.rs @@ -12,6 +12,7 @@ impl Position { let snippets = Snippet::new( "position", CodeType::<&'static str>::Path("transform/position.glsl".into()), + true, None, )?; diff --git a/src/graphics/transforms/trackball.rs b/src/graphics/transforms/trackball.rs index 36cb8a5..d8478a7 100644 --- a/src/graphics/transforms/trackball.rs +++ b/src/graphics/transforms/trackball.rs @@ -12,6 +12,7 @@ impl Trackball { let snippets = Snippet::new( "trackball", CodeType::<&'static str>::Path("transform/trackball.glsl".into()), + true, None, )?; diff --git a/src/graphics/transforms/viewport.rs b/src/graphics/transforms/viewport.rs index 26f5b7b..432db40 100644 --- a/src/graphics/transforms/viewport.rs +++ b/src/graphics/transforms/viewport.rs @@ -11,6 +11,7 @@ impl Viewport { let snippets = Snippet::new( "viewport", CodeType::<&'static str>::Path("transform/viewport.glsl".into()), + true, None, )?; diff --git a/src/utils/parser.rs b/src/utils/parser.rs index 5b35f35..d499023 100644 --- a/src/utils/parser.rs +++ b/src/utils/parser.rs @@ -1525,7 +1525,7 @@ impl SnippetCode { let paras = paras.join(", "); format!("{}({})", call_name, paras) } else { - snippet.call(paras) + snippet.call(paras).unwrap() } } else { if ow_subhooks { @@ -1538,7 +1538,7 @@ impl SnippetCode { var.borrow().name.to_owned() } } else { - snippet.call(&vec![]) + snippet.call(&vec![]).unwrap() } };