THE BIG MERGE v0.9 - more opti-like

This commit is contained in:
Noah Santschi-Cooney 2020-08-04 01:33:49 +01:00
parent d35781b718
commit ae51b3b01c
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
7 changed files with 111 additions and 24 deletions

View file

@ -29,11 +29,13 @@ impl <'a> MergeViewGenerator<'a> {
let mut last_offset: Vec<String> = Vec::new();
let mut nodes_iter = nodes.iter().peekable();
let first = nodes_iter.next().unwrap().0;
let first_path = self.graph.get_node(first).clone();
self.create_merge_views(nodes_iter, &mut merge_list, &mut last_offset_set, &mut last_offset);
// now we add a view of the remainder of the root file
let offset = *last_offset_set.get(&first_path).unwrap();
merge_list.push_back(&self.sources.get(&first_path).unwrap().as_str()[offset..]);
@ -62,8 +64,6 @@ impl <'a> MergeViewGenerator<'a> {
last_offset.push(parent_path.clone());
}
let offset = *last_offset_set.insert(parent_path.clone(), edge.line).get_or_insert(0);
let source = self.sources.get(&parent_path).unwrap();
let mut char_for_line: usize = 0;
let mut char_following_line: usize = 0;
@ -76,55 +76,61 @@ impl <'a> MergeViewGenerator<'a> {
char_following_line = char_for_line;
}
// TODO: update after finding offset? why set it to 0 on L71 then
last_offset_set.insert(parent_path.clone(), char_following_line);
let offset = *last_offset_set.insert(parent_path.clone(), char_following_line).get_or_insert(0);
merge_list.push_back(&source.as_str()[offset..char_for_line]);
merge_list.push_back(&"#line 1\n"[..]);
match nodes.peek() {
Some(next) => {
let next = (*next).clone();
let next = *next;
// if the next element is not a child of this element, we dump the rest of this elements source
if next.1.unwrap() != child {
let source = self.sources.get(&child_path).unwrap();
merge_list.push_back(&source.as_str()[..]);
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
let line_directive = format!("\n#line {}\n", edge.line+2);
self.line_directives.borrow_mut().push(line_directive);
unsafe {
self.unsafe_get_and_insert(merge_list);
}
self.add_line_directive(edge.line+2, merge_list);
}
self.create_merge_views(nodes, merge_list, last_offset_set, last_offset);
if next.1.unwrap() == child {
let offset = *last_offset_set.get(&child_path).unwrap();
merge_list.push_back(&self.sources.get(&child_path).unwrap().as_str()[offset..]);
let source = self.sources.get(&child_path).unwrap();
if offset <= source.len() {
merge_list.push_back(&source.as_str()[offset..]);
}
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
let line_directive = format!("\n#line {}\n", edge.line+2);
self.line_directives.borrow_mut().push(line_directive);
unsafe {
self.unsafe_get_and_insert(merge_list);
}
self.add_line_directive(edge.line+2, merge_list);
}
},
None => {
let source = self.sources.get(&child_path).unwrap();
merge_list.push_back(&source.as_str()[..]);
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
let line_directive = format!("\n#line {}\n", edge.line+2);
self.line_directives.borrow_mut().push(line_directive);
unsafe {
self.unsafe_get_and_insert(merge_list);
}
self.add_line_directive(edge.line+2, merge_list);
}
}
}
fn add_line_directive(&self, line: usize, merge_list: &mut LinkedList<&str>) {
// Optifine doesn't seem to add a leading newline if the previous line was
// a #line directive
let line_directive = if let Some(l) = merge_list.back() {
if l.starts_with("\n#line") {
format!("#line {}\n", line)
} else {
format!("\n#line {}\n", line)
}
} else {
format!("\n#line {}\n", line)
};
self.line_directives.borrow_mut().push(line_directive);
unsafe {
self.unsafe_get_and_insert(merge_list);
}
}
unsafe fn unsafe_get_and_insert(&self, merge_list: &mut LinkedList<&str>) {
// :^)
let vec_ptr_offset = self.line_directives.borrow().as_ptr().add(self.line_directives.borrow().len()-1);

View file

@ -440,5 +440,43 @@ fn test_generate_merge_list_02() {
assert_that!(total, eq(truth));
server.endpoint.request_shutdown();
}
#[test]
fn test_generate_merge_list_03() {
let mut server = new_temp_server();
let (_tmp_dir, tmp_path) = copy_to_and_set_root("./testdata/03", &mut server);
let final_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/{}", tmp_path, "final.fsh"));
let test_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "test.glsl"));
let burger_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "burger.glsl"));
let sample_idx = server.graph.borrow_mut().add_node(format!("{}/shaders/utils/{}", tmp_path, "sample.glsl"));
server.graph.borrow_mut().add_edge(final_idx, sample_idx, 2, 0, 0);
server.graph.borrow_mut().add_edge(sample_idx, burger_idx, 4, 0, 0);
server.graph.borrow_mut().add_edge(sample_idx, test_idx, 6, 0, 0);
let nodes = server.get_dfs_for_node(final_idx).unwrap();
let mut sources = server.load_sources(&nodes).unwrap();
let graph_borrow = server.graph.borrow();
let mut merger = merge_views::MergeViewGenerator::new(&mut sources, &graph_borrow);
let result = merger.generate_merge_list(&nodes);
let total: String = result
.iter()
.map(|s| &**s)
.collect::<Vec<&str>>()
.join("");
let merge_file = tmp_path + "/shaders/final.fsh.merge";
let truth = String::from_utf8(fs::read::<String>(merge_file).unwrap()).unwrap();
assert_that!(total, eq(truth));
server.endpoint.request_shutdown();
}

7
server/testdata/03/final.fsh vendored Normal file
View file

@ -0,0 +1,7 @@
#version 120
#include "/utils/sample.glsl"
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

23
server/testdata/03/final.fsh.merge vendored Normal file
View file

@ -0,0 +1,23 @@
#version 120
#line 1
int sample() {
return 5;
}
#line 1
void burger() {
// sample text
}
#line 6
#line 1
float test() {
return 3.0;
}
#line 8
#line 4
void main() {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

3
server/testdata/03/utils/burger.glsl vendored Normal file
View file

@ -0,0 +1,3 @@
void burger() {
// sample text
}

7
server/testdata/03/utils/sample.glsl vendored Normal file
View file

@ -0,0 +1,7 @@
int sample() {
return 5;
}
#include "/utils/burger.glsl"
#include "/utils/test.glsl"

3
server/testdata/03/utils/test.glsl vendored Normal file
View file

@ -0,0 +1,3 @@
float test() {
return 3.0;
}