This commit is contained in:
tsuki 2024-07-10 22:58:03 +08:00
parent 371c1699be
commit 8721daed30
8 changed files with 166 additions and 31 deletions

View File

@ -191,7 +191,7 @@ mod utils {
body.push_str("}\n\n"); body.push_str("}\n\n");
let input = header + &body; 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 result
} }

View File

@ -37,15 +37,16 @@ pub struct Snippet {
name: &'static str, name: &'static str,
parsed: CodeBlock, parsed: CodeBlock,
link: Option<Box<Snippet>>, link: Option<Box<Snippet>>,
alias: HashMap<String, String>, alias: Option<HashMap<String, String>>,
chained: String, chained: String,
main: String, main: Option<String>,
} }
impl Snippet { impl Snippet {
pub fn new<S: std::borrow::Borrow<str>>( pub fn new<S: std::borrow::Borrow<str>>(
name: &'static str, name: &'static str,
code: CodeType<S>, code: CodeType<S>,
mangling: bool,
main: Option<String>, main: Option<String>,
) -> Result<Self> { ) -> Result<Self> {
let code = match code { let code = match code {
@ -71,15 +72,20 @@ impl Snippet {
let mut parsed_code = CodeBlock::new(&merged_code)?; let mut parsed_code = CodeBlock::new(&merged_code)?;
let call = if let Some(main) = main { let call = if let Some(main) = main {
main Some(main)
} else { } else {
parsed_code parsed_code
.all_functions() .all_functions()
.first() .first()
.map(|f| f.borrow().name.clone()) .map(|f| f.borrow().name.clone())
.unwrap()
}; };
let alias = if mangling {
let alias = parsed_code.mangling(id.to_string()); let alias = parsed_code.mangling(id.to_string());
Some(alias)
} else {
None
};
let chained = format!( let chained = format!(
"// START {} //\n{}\n// END {} //\n", "// START {} //\n{}\n// END {} //\n",
@ -111,15 +117,22 @@ impl Snippet {
self self
} }
pub fn call(&self, paras: &Vec<String>) -> String { pub fn call(&self, paras: &Vec<String>) -> Option<String> {
(self.main.as_ref()).map(|name| {
if let Some(link) = &self.link { if let Some(link) = &self.link {
let call_name = self.alias.get(&self.main).unwrap(); let call_name = self.alias.as_ref().map(|a| a.get(name).unwrap()).unwrap_or(name);
let c = link.call(paras); let c = link.call(paras);
format!("{}({})", call_name, c) if let Some(c) = c {
return format!("{}({})", call_name, c);
} else { } else {
let call_name = self.alias.get(&self.main).unwrap(); 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(", ")) format!("{}({})", call_name, paras.join(", "))
} }
})
} }
pub fn prepare_code(&self) -> &str { pub fn prepare_code(&self) -> &str {
@ -127,7 +140,7 @@ impl Snippet {
} }
pub fn find_symbol(&self, name: &str) -> Option<&String> { 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<RefCell<ShareVariable>>> { pub fn find_variable(&self, name: &str) -> Option<&Rc<RefCell<ShareVariable>>> {
@ -151,7 +164,7 @@ impl Add for Snippet {
let code = rhs.parsed.to_string(); let code = rhs.parsed.to_string();
raw_code.push_str(&code); 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(); let snippet3 = snippet.clone() + snippet2.clone();

View File

@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut};
use bytemuck::{Pod, Zeroable}; 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::errors::*;
use crate::graphics::transforms::viewport::Viewport; use crate::graphics::transforms::viewport::Viewport;
use crate::graphics::transforms::Transform; use crate::graphics::transforms::Transform;
@ -13,7 +13,7 @@ use super::Colletion;
struct AggFastPath { struct AggFastPath {
program: Program, program: Program,
buffer: Vec<Path>, buffer: Vec<Point>,
} }
impl AggFastPath { impl AggFastPath {
@ -28,7 +28,22 @@ impl AggFastPath {
CodeType::<String>::Path("agg-fast-path.frag".into()), CodeType::<String>::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, ""); let mut program = Program::new(vertex, fragment, None, "");
@ -37,7 +52,7 @@ impl AggFastPath {
Ok(Self { Ok(Self {
program, program,
buffer: Vec::with_capacity(500), buffer: Vec::with_capacity(128),
}) })
} }
} }
@ -46,15 +61,16 @@ impl Colletion for AggFastPath {
type Item = Path; type Item = Path;
fn append(&mut self, item: Self::Item) { fn append(&mut self, item: Self::Item) {
self.buffer.push(item); self.buffer.extend(item.points);
} }
} }
impl Graphics for AggFastPath { impl Graphics for AggFastPath {
fn draw(&self) -> Result<()> { 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(()) Ok(())
} }
} }
@ -76,6 +92,52 @@ pub struct Path {
is_empty: bool, 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 { impl Path {
pub fn new(is_closed: bool) -> Self { pub fn new(is_closed: bool) -> Self {
Self { Self {
@ -85,6 +147,11 @@ impl Path {
} }
} }
pub fn builder() -> PathBuilder {
PathBuilder::default()
}
pub fn push(&mut self, point: [f32; 3]) { pub fn push(&mut self, point: [f32; 3]) {
if self.is_empty { if self.is_empty {
self.points.push(Point { self.points.push(Point {
@ -107,21 +174,33 @@ impl Path {
let next = point; let next = point;
self.points.push(Point { prev, curr, next }); self.points.push(Point { prev, curr, next });
self.points[len - 1].next = curr; self.points[len - 1].next = curr;
if len == 2 {
self.points[0].next = curr;
}
} }
} }
pub fn finish(&mut self) { pub fn finish(&mut self) {
if self.is_closed { if self.is_closed {
let len = self.points.len(); let len = self.points.len();
let curr = self.points.first().unwrap().curr;
let prev = self.points[len - 1].curr; let prev = self.points[len - 1].curr;
let curr = self.points[0].curr; let next = self.points[1].next;
let next = self.points[1].curr;
self.points.push(Point { prev, curr, next }); self.points.push(Point { prev, curr, next });
self.points[len - 1].next = curr; 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 { impl Deref for Path {
type Target = [u8]; type Target = [u8];
@ -137,3 +216,42 @@ impl DerefMut for Path {
cast_slice_mut(&mut self.points) 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();
}
}

View File

@ -15,6 +15,7 @@ impl Polar {
let snippets = Snippet::new( let snippets = Snippet::new(
"polar", "polar",
CodeType::<&'static str>::Path("transform/polar.glsl".into()), CodeType::<&'static str>::Path("transform/polar.glsl".into()),
true,
Some("forward".to_string()), Some("forward".to_string()),
)?; )?;

View File

@ -12,6 +12,7 @@ impl Position {
let snippets = Snippet::new( let snippets = Snippet::new(
"position", "position",
CodeType::<&'static str>::Path("transform/position.glsl".into()), CodeType::<&'static str>::Path("transform/position.glsl".into()),
true,
None, None,
)?; )?;

View File

@ -12,6 +12,7 @@ impl Trackball {
let snippets = Snippet::new( let snippets = Snippet::new(
"trackball", "trackball",
CodeType::<&'static str>::Path("transform/trackball.glsl".into()), CodeType::<&'static str>::Path("transform/trackball.glsl".into()),
true,
None, None,
)?; )?;

View File

@ -11,6 +11,7 @@ impl Viewport {
let snippets = Snippet::new( let snippets = Snippet::new(
"viewport", "viewport",
CodeType::<&'static str>::Path("transform/viewport.glsl".into()), CodeType::<&'static str>::Path("transform/viewport.glsl".into()),
true,
None, None,
)?; )?;

View File

@ -1525,7 +1525,7 @@ impl SnippetCode {
let paras = paras.join(", "); let paras = paras.join(", ");
format!("{}({})", call_name, paras) format!("{}({})", call_name, paras)
} else { } else {
snippet.call(paras) snippet.call(paras).unwrap()
} }
} else { } else {
if ow_subhooks { if ow_subhooks {
@ -1538,7 +1538,7 @@ impl SnippetCode {
var.borrow().name.to_owned() var.borrow().name.to_owned()
} }
} else { } else {
snippet.call(&vec![]) snippet.call(&vec![]).unwrap()
} }
}; };