sync
This commit is contained in:
parent
371c1699be
commit
8721daed30
@ -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
|
||||
}
|
||||
|
||||
@ -37,15 +37,16 @@ pub struct Snippet {
|
||||
name: &'static str,
|
||||
parsed: CodeBlock,
|
||||
link: Option<Box<Snippet>>,
|
||||
alias: HashMap<String, String>,
|
||||
alias: Option<HashMap<String, String>>,
|
||||
chained: String,
|
||||
main: String,
|
||||
main: Option<String>,
|
||||
}
|
||||
|
||||
impl Snippet {
|
||||
pub fn new<S: std::borrow::Borrow<str>>(
|
||||
name: &'static str,
|
||||
code: CodeType<S>,
|
||||
mangling: bool,
|
||||
main: Option<String>,
|
||||
) -> Result<Self> {
|
||||
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 = 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>) -> String {
|
||||
pub fn call(&self, paras: &Vec<String>) -> Option<String> {
|
||||
(self.main.as_ref()).map(|name| {
|
||||
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);
|
||||
format!("{}({})", call_name, c)
|
||||
if let Some(c) = c {
|
||||
return format!("{}({})", call_name, c);
|
||||
} 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(", "))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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<RefCell<ShareVariable>>> {
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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<Path>,
|
||||
buffer: Vec<Point>,
|
||||
}
|
||||
|
||||
impl AggFastPath {
|
||||
@ -28,7 +28,22 @@ impl AggFastPath {
|
||||
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, "");
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -15,6 +15,7 @@ impl Polar {
|
||||
let snippets = Snippet::new(
|
||||
"polar",
|
||||
CodeType::<&'static str>::Path("transform/polar.glsl".into()),
|
||||
true,
|
||||
Some("forward".to_string()),
|
||||
)?;
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ impl Position {
|
||||
let snippets = Snippet::new(
|
||||
"position",
|
||||
CodeType::<&'static str>::Path("transform/position.glsl".into()),
|
||||
true,
|
||||
None,
|
||||
)?;
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ impl Trackball {
|
||||
let snippets = Snippet::new(
|
||||
"trackball",
|
||||
CodeType::<&'static str>::Path("transform/trackball.glsl".into()),
|
||||
true,
|
||||
None,
|
||||
)?;
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ impl Viewport {
|
||||
let snippets = Snippet::new(
|
||||
"viewport",
|
||||
CodeType::<&'static str>::Path("transform/viewport.glsl".into()),
|
||||
true,
|
||||
None,
|
||||
)?;
|
||||
|
||||
|
||||
@ -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()
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user