Skip to content

Commit

Permalink
Document the binding types
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 27, 2024
1 parent 9092fdb commit 19383fe
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
20 changes: 19 additions & 1 deletion dunge/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ impl TypedGroup {
}
}

/// The group binder type.
///
/// Can be created using the context's [`make_binder`](crate::Context::make_binder) function.
pub struct Binder<'a> {
shader_id: usize,
device: &'a Device,
Expand All @@ -188,7 +191,17 @@ impl<'a> Binder<'a> {
}
}

pub fn bind<G>(&mut self, group: &G) -> GroupHandler<G::Projection>
/// Adds a group to the associated shader's binding.
///
/// It returns a [group handler](GroupHandler) that can be used to update
/// the data in this binding. If you don't need to update the data, then
/// discard this handler.
///
/// # Panic
/// It checks the group type matches to an associated shader's group at runtime.
/// If it's violated or there are more bindings than in the shader,
/// then this function will panic.
pub fn add<G>(&mut self, group: &G) -> GroupHandler<G::Projection>
where
G: Visit,
{
Expand Down Expand Up @@ -221,6 +234,11 @@ impl<'a> Binder<'a> {
}
}

/// Constructs an object that can be [used](crate::layer::SetLayer::bind)
/// in the draw stage.
///
/// # Panic
/// It will panic if some group bindings is not set.
pub fn into_binding(self) -> UniqueBinding {
assert!(
self.groups.len() == self.layout.len(),
Expand Down
2 changes: 0 additions & 2 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use {

/// Creates the context instance.
///
/// If you need a window call the [`window`] function.
///
/// # Errors
/// Returns an error when the context could not be created.
/// See [`FailedMakeContext`] for details.
Expand Down
3 changes: 3 additions & 0 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use {
},
};

/// The shader type.
///
/// Can be created using the context's [`make_shader`](crate::Context::make_shader) function.
pub struct Shader<V, I> {
inner: Inner,
wgsl: String,
Expand Down
2 changes: 1 addition & 1 deletion dunge/tests/triangle_discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn render() -> Result<(), Error> {
};

let mut binder = cx.make_binder(&shader);
binder.bind(&map);
binder.add(&map);
binder.into_binding()
};

Expand Down
2 changes: 1 addition & 1 deletion dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn render() -> Result<(), Error> {
};

let mut binder = cx.make_binder(&shader);
binder.bind(&map);
binder.add(&map);
binder.into_binding()
};

Expand Down
2 changes: 1 addition & 1 deletion examples/cube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
let bind_transform = {
let tr = Transform(&uniform);
let mut binder = cx.make_binder(&cube_shader);
binder.bind(&tr);
binder.add(&tr);
binder.into_binding()
};

Expand Down
4 changes: 2 additions & 2 deletions examples/ssaa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
let bind = {
let offset = Offset(&uniform);
let mut binder = cx.make_binder(&triangle_shader);
binder.bind(&offset);
binder.add(&offset);
binder.into_binding()
};

Expand Down Expand Up @@ -105,7 +105,7 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
};

let mut binder = cx.make_binder(&screen_shader);
let handler = binder.bind(&map);
let handler = binder.add(&map);
(binder.into_binding(), handler)
};

Expand Down
2 changes: 1 addition & 1 deletion examples/triangle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
let bind = {
let offset = Offset(&uniform);
let mut binder = cx.make_binder(&shader);
binder.bind(&offset);
binder.add(&offset);
binder.into_binding()
};

Expand Down
8 changes: 4 additions & 4 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ fn serve(Opts { module, .. }: Opts) -> Result<(), Error> {
let html = index.render()?.leak();
let prefix = "xtask/web";
let strip = |s: &'static str| -> &'static str { s.strip_prefix(prefix).expect("strip") };
let js_path = format!("{prefix}/{module}/wasm.js").leak();
let wasm_path = format!("{prefix}/{module}/wasm_bg.wasm").leak();
let js = fs::read_to_string(&js_path)?.leak();
let wasm = fs::read(&wasm_path)?.leak();
let js_path: &str = format!("{prefix}/{module}/wasm.js").leak();
let wasm_path: &str = format!("{prefix}/{module}/wasm_bg.wasm").leak();
let js = fs::read_to_string(js_path)?.leak();
let wasm = fs::read(wasm_path)?.leak();
let routes = &[
("/", Page::html(html)),
("/favicon.ico", Page::html("")),
Expand Down

0 comments on commit 19383fe

Please sign in to comment.