Skip to content

Commit

Permalink
Updated binding shader generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
facundo-villa committed Feb 22, 2024
1 parent b22c642 commit d2f85fd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
11 changes: 10 additions & 1 deletion jspd/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ impl Node {
})
}

pub fn binding(name: String, set: u32, binding: u32, read: bool, write: bool) -> NodeReference {
pub fn binding(name: String, r#type: BindingTypes, set: u32, binding: u32, read: bool, write: bool) -> NodeReference {
Self::internal_new(Node {
parent: None,
node: Nodes::Binding {
name,
r#type,
set,
binding,
read,
Expand Down Expand Up @@ -294,6 +295,13 @@ impl Node {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BindingTypes {
Buffer,
CombinedImageSampler,
Image,
}

#[derive(Clone, Debug,)]
pub enum Nodes {
Null,
Expand Down Expand Up @@ -329,6 +337,7 @@ pub enum Nodes {
binding: u32,
read: bool,
write: bool,
r#type: BindingTypes,
},
}

Expand Down
1 change: 1 addition & 0 deletions jspd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use lexer::Node;
pub use lexer::Nodes;

pub use crate::lexer::NodeReference;
pub use crate::lexer::BindingTypes;

/// Compiles a BESL source code string into a JSPD.
///
Expand Down
37 changes: 28 additions & 9 deletions resource_management/src/shader_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl ShaderCompilation {
if !self.minified { l_string.push_str(";\n"); } else { l_string.push(';'); }
}

l_string.push_str("}\n");
if self.minified { l_string.push('}') } else { l_string.push_str("}\n"); }

string.insert_str(0, &l_string);

Expand Down Expand Up @@ -176,9 +176,22 @@ impl ShaderCompilation {
}
}
}
jspd::Nodes::Binding { name, set, binding, read, write, .. } => {
jspd::Nodes::Binding { name, set, binding, read, write, r#type, .. } => {
let mut l_string = String::with_capacity(128);
l_string.push_str(&format!("layout(set={}, binding={}) uniform ", set, binding));

let binding_type = match r#type {
jspd::BindingTypes::Buffer => "buffer",
jspd::BindingTypes::Image => "image2D",
jspd::BindingTypes::CombinedImageSampler => "texture2D",
};

l_string.push_str(&format!("layout(set={},binding={}", set, binding));

if r#type == &jspd::BindingTypes::Buffer {
l_string.push_str(",scalar");
}

l_string.push_str(&format!(") {} ", binding_type));
if *read && !*write { l_string.push_str("readonly "); }
if *write && !*read { l_string.push_str("writeonly "); }
l_string.push_str(&name);
Expand Down Expand Up @@ -256,24 +269,30 @@ mod tests {
}

#[test]
fn binding() {
fn bindings() {
let script = r#"
main: fn () -> void {
buffer;
Buffer;
image;
texture;
}
"#;

let root_node = jspd::Node::scope("root".to_string(), vec![jspd::Node::binding("buffer".to_string(), 0, 0, true, false)]);
let root_node = jspd::Node::scope("root".to_string(), vec![
jspd::Node::binding("Buffer".to_string(), jspd::BindingTypes::Buffer, 0, 0, true, true),
jspd::Node::binding("image".to_string(), jspd::BindingTypes::Image, 0, 1, false, true),
jspd::Node::binding("texture".to_string(), jspd::BindingTypes::CombinedImageSampler, 1, 0, true, false),
]);

let script_node = jspd::compile_to_jspd(&script, Some(root_node)).unwrap();

let main = RefCell::borrow(&script_node).get_child("main").unwrap();

let shader_generator = ShaderGenerator::new();
let shader_generator = ShaderGenerator::new().minified(true);

let shader = shader_generator.compilation().generate_shader(&main);

println!("{}", shader);
assert_eq!(shader, "layout(set=1,binding=0) texture2D readonly texture;layout(set=0,binding=1) image2D writeonly image;layout(set=0,binding=0,scalar) buffer Buffer;void main(){Buffer;image;texture;}");
}

#[test]
Expand All @@ -298,7 +317,7 @@ mod tests {

let shader = shader_generator.compilation().generate_shader(&main);

assert_eq!(shader, "void main(){vec3f albedo=vec3f(1.0,0.0,0.0);}\n");
assert_eq!(shader, "void main(){vec3f albedo=vec3f(1.0,0.0,0.0);}");
}

#[test]
Expand Down

0 comments on commit d2f85fd

Please sign in to comment.