Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 12, 2024
1 parent fa551f2 commit 3be719e
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 124 deletions.
2 changes: 1 addition & 1 deletion dunge/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
uniform::{Uniform, Value},
};

pub use dunge_shader::group::{DeclareGroup, Projection};
pub use dunge_shader::group::Projection;

#[derive(Clone, Copy)]
pub struct BoundTexture<'a>(&'a Texture);
Expand Down
10 changes: 5 additions & 5 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
bind::TypedGroup,
sl::{InputInfo, IntoModule, Module, Stages},
sl::{Info, IntoModule, Module, Stages},
state::State,
types::{MemberType, VectorType},
},
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Inner {
let mut groups = vec![];
for info in cx.groups() {
entries.clear();
for (binding, member) in iter::zip(0.., info.decl) {
for (binding, member) in iter::zip(0.., info.def) {
let entry = match member {
MemberType::Scalar(_) | MemberType::Vector(_) | MemberType::Matrix(_) => {
BindGroupLayoutEntry {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Inner {
};

let vertex = {
let vert = |info: InputInfo| {
let vert = |info: Info| {
let mut offset = 0;
let mut shader_location = 0;
let attr = |vecty| {
Expand Down Expand Up @@ -182,11 +182,11 @@ impl Inner {

Vertex {
size: info.size,
attributes: info.decl.into_iter().map(attr).collect(),
attributes: info.def.into_iter().map(attr).collect(),
}
};

cx.inputs().map(vert).collect()
cx.verts().map(vert).collect()
};

Self {
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
types::{self, VectorType},
};

pub use dunge_shader::vertex::{verts_as_bytes, DeclareInput, Projection};
pub use dunge_shader::vertex::{verts_as_bytes, Projection};

/// Describes an input type projection.
///
Expand Down
6 changes: 3 additions & 3 deletions dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
group::BoundTexture,
mesh,
sl::{self, Groups, InVertex, Out},
state::{Options, Render},
state::Render,
texture::{self, Filter, Sampler},
Group, Vertex,
},
Expand Down Expand Up @@ -94,9 +94,9 @@ fn render() -> Result<(), Error> {
};

let buffer = cx.make_copy_buffer(SIZE);
let options = Options::default().with_clear(Rgba::from_standard([0., 0., 0., 1.]));
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = draw::from_fn(|mut frame| {
frame.layer(&layer, options).bind(&map).draw(&mesh);
frame.layer(&layer, opts).bind(&map).draw(&mesh);
frame.copy_texture(&buffer, &view);
});

Expand Down
6 changes: 3 additions & 3 deletions dunge/tests/triangle_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
draw,
format::Format,
sl::{self, Index, Out},
state::{Options, Render},
state::Render,
texture,
},
glam::Vec4,
Expand Down Expand Up @@ -43,9 +43,9 @@ fn render() -> Result<(), Error> {
};

let buffer = cx.make_copy_buffer(SIZE);
let options = Options::default().with_clear(Rgba::from_standard([0., 0., 0., 1.]));
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = draw::from_fn(|mut frame| {
frame.layer(&layer, options).bind_empty().draw_triangles(1);
frame.layer(&layer, opts).bind_empty().draw_triangles(1);
frame.copy_texture(&buffer, &view);
});

Expand Down
Binary file added dunge/tests/triangle_instance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions dunge/tests/triangle_instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![cfg(not(target_family = "wasm"))]

use {
dunge::{
color::Rgba,
draw,
format::Format,
sl::{self, Index, Out},
state::Render,
texture,
},
glam::Vec4,
helpers::Image,
std::{error, f32::consts, fs},
};

type Error = Box<dyn error::Error>;

#[test]
fn render() -> Result<(), Error> {
const SIZE: (u32, u32) = (300, 300);
const COLOR: Vec4 = Vec4::new(1., 0., 0., 1.);
const THIRD: f32 = consts::TAU / 3.;
const R_OFFSET: f32 = -consts::TAU / 4.;
const Y_OFFSET: f32 = 0.25;

let triangle = |Index(index): Index| {
let [x, y] = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
Out {
place: sl::vec4(sl::cos(x), sl::sin(y) + Y_OFFSET, 0., 1.),
color: COLOR,
}
};

let cx = helpers::block_on(dunge::context())?;
let shader = cx.make_shader(triangle);
let layer = cx.make_layer(Format::RgbAlpha, &shader);
let view = {
use texture::Data;

let data = Data::empty(SIZE, Format::RgbAlpha)?.with_draw().with_copy();
cx.make_texture(data)
};

let buffer = cx.make_copy_buffer(SIZE);
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = draw::from_fn(|mut frame| {
frame.layer(&layer, opts).bind_empty().draw_triangles(1);
frame.copy_texture(&buffer, &view);
});

let mut render = Render::default();
cx.draw_to_texture(&mut render, &view, draw);

let mapped = helpers::block_on({
let (tx, rx) = helpers::oneshot();
cx.map_view(buffer.view(), tx, rx)
});

let data = mapped.data();
let image = Image::from_fn(SIZE, |x, y| {
let (width, _) = buffer.size();
let idx = x + y * width;
data[idx as usize]
});

fs::write("tests/triangle_instance.png", image.encode())?;
Ok(())
}
6 changes: 3 additions & 3 deletions dunge/tests/triangle_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
format::Format,
mesh,
sl::{self, InVertex, Out},
state::{Options, Render},
state::Render,
texture, Vertex,
},
glam::{Vec2, Vec3},
Expand Down Expand Up @@ -66,9 +66,9 @@ fn render() -> Result<(), Error> {
};

let buffer = cx.make_copy_buffer(SIZE);
let options = Options::default().with_clear(Rgba::from_standard([0., 0., 0., 1.]));
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = draw::from_fn(|mut frame| {
frame.layer(&layer, options).bind_empty().draw(&mesh);
frame.layer(&layer, opts).bind_empty().draw(&mesh);
frame.copy_texture(&buffer, &view);
});

Expand Down
4 changes: 2 additions & 2 deletions dunge_macros/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) fn derive(input: DeriveInput) -> TokenStream {
quote::quote! {
impl<#(#lts),*> ::dunge::Group for #name<#(#lts),*> {
type Projection = #projection_name<#(#static_lts),*>;
const DECL: ::dunge::group::DeclareGroup = ::dunge::group::DeclareGroup::new(&[
const DEF: ::dunge::sl::Define<::dunge::types::MemberType> = ::dunge::sl::Define::new(&[
#(#group_types),*,
]);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ mod tests {
let expected = quote::quote! {
impl<'a> ::dunge::Group for Map<'a> {
type Projection = MapProjection<'static>;
const DECL: ::dunge::group::DeclareGroup = ::dunge::group::DeclareGroup::new(&[
const DEF: ::dunge::sl::Define<::dunge::types::MemberType> = ::dunge::sl::Define::new(&[
<BoundTexture<'a> as ::dunge::group::MemberProjection>::TYPE,
<&'a Sampler as ::dunge::group::MemberProjection>::TYPE,
]);
Expand Down
4 changes: 2 additions & 2 deletions dunge_macros/src/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn derive(input: DeriveInput) -> TokenStream {
quote::quote! {
unsafe impl ::dunge::Vertex for #name {
type Projection = #projection_name;
const DECL: ::dunge::vertex::DeclareInput = ::dunge::vertex::DeclareInput::new(&[
const DEF: ::dunge::sl::Define<::dunge::types::VectorType> = ::dunge::sl::Define::new(&[
#(#vector_types),*,
]);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ mod tests {
let expected = quote::quote! {
unsafe impl ::dunge::Vertex for Vert {
type Projection = VertProjection;
const DECL: ::dunge::vertex::DeclareInput = ::dunge::vertex::DeclareInput::new(&[
const DEF: ::dunge::sl::Define<::dunge::types::VectorType> = ::dunge::sl::Define::new(&[
<[f32; 2] as ::dunge::vertex::InputProjection>::TYPE,
<[f32; 3] as ::dunge::vertex::InputProjection>::TYPE,
]);
Expand Down
Loading

0 comments on commit 3be719e

Please sign in to comment.