Skip to content

Commit

Permalink
Update Group
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 5, 2024
1 parent ac55329 commit af140aa
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
6 changes: 3 additions & 3 deletions dunge/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'g> Visitor for VisitGroup<'g> {

fn visit<'g, G>(group: &'g G) -> Vec<BindGroupEntry<'g>>
where
G: Group<Visitor = VisitGroup<'g>>,
G: Group<Visitor<'g> = VisitGroup<'g>>,
{
let mut visit = VisitGroup::default();
group.group(&mut visit);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub(crate) fn update<'g, G>(
group: &'g G,
) -> Update
where
G: Group<Visitor = VisitGroup<'g>>,
G: Group<Visitor<'g> = VisitGroup<'g>>,
{
if handler.shader_id != uni.0.shader_id {
return Err(ForeignShader);
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a> Binder<'a> {

pub fn bind<'g, G>(&mut self, group: &'g G) -> GroupHandler<G>
where
G: Group<Visitor = VisitGroup<'g>>,
G: Group<Visitor<'g> = VisitGroup<'g>>,
{
let id = self.groups.len();
let Some(layout) = self.layout.get(id) else {
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Context {
group: &'g G,
) -> Update
where
G: Group<Visitor = VisitGroup<'g>>,
G: Group<Visitor<'g> = VisitGroup<'g>>,
{
bind::update(&self.0, uni, handler, group)
}
Expand Down
Binary file modified dunge/tests/triangle_group.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 29 additions & 21 deletions dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use {
color::Rgba,
context::Context,
draw,
group::{BoundTexture, DeclareGroup, Group, Projection, Visitor},
group::{BoundTexture, DeclareGroup, Group, MemberProjection, Projection, Visitor},
mesh,
sl::{self, GlobalOut, Groups, Input, Out, ReadGlobal, Ret},
sl::{self, GlobalOut, Groups, Input, Out},
state::{Options, Render},
texture::{self, Format, Sampler},
types::{self, GroupMemberType},
Vertex,
},
glam::Vec2,
Expand All @@ -35,38 +34,47 @@ fn render() -> Result<(), Error> {
sam: &'g Sampler,
}

impl<'g> Group for Map<'g> {
impl Group for Map<'_> {
type Projection = MapProjection;
type Visitor = VisitGroup<'g>;
const DECL: DeclareGroup =
DeclareGroup::new(&[GroupMemberType::Tx2df, GroupMemberType::Sampl]);
type Visitor<'g> = VisitGroup<'g>
where
Self: 'g;

fn group(&self, visit: &mut Self::Visitor) {
const DECL: DeclareGroup = DeclareGroup::new(&[
<BoundTexture<'static> as MemberProjection>::TYPE,
<&'static Sampler as MemberProjection>::TYPE,
]);

fn group<'g>(&'g self, visit: &mut Self::Visitor<'g>) {
visit.visit_texture(self.tex);
visit.visit_sampler(self.sam);
}
}

struct MapProjection {
tex: Ret<ReadGlobal, types::Texture2d<f32>>,
sam: Ret<ReadGlobal, types::Sampler>,
tex: <BoundTexture<'static> as MemberProjection>::Field,
sam: <&'static Sampler as MemberProjection>::Field,
}

impl Projection for MapProjection {
fn projection(id: u32, out: GlobalOut) -> Self {
Self {
tex: ReadGlobal::new(id, 0, out.clone()),
sam: ReadGlobal::new(id, 1, out.clone()),
tex: <BoundTexture<'static> as MemberProjection>::member_projection(
id,
0,
out.clone(),
),
sam: <&'static Sampler as MemberProjection>::member_projection(id, 1, out.clone()),
}
}
}

let triangle = |vert: Input<Vert>, groups: Groups<Map>| Out {
place: sl::concat(vert.pos, Vec2::new(0., 1.)),
color: {
let Groups(map) = groups;
sl::texture_sample(map.tex, map.sam, sl::fragment(vert.tex))
},
let triangle = |vert: Input<Vert>, groups: Groups<Map>| {
let Groups(map) = groups;
Out {
place: sl::concat(vert.pos, Vec2::new(0., 1.)),
color: sl::texture_sample(map.tex, map.sam, sl::fragment(vert.tex)),
}
};

let cx = helpers::block_on(Context::new())?;
Expand Down Expand Up @@ -105,15 +113,15 @@ fn render() -> Result<(), Error> {
const VERTS: [Vert; 3] = [
Vert {
pos: Vec2::new(0., -0.75),
tex: Vec2::new(0., 0.),
tex: Vec2::new(0., 1.),
},
Vert {
pos: Vec2::new(0.866, 0.75),
tex: Vec2::new(0., 1.),
tex: Vec2::new(1., 1.),
},
Vert {
pos: Vec2::new(-0.866, 0.75),
tex: Vec2::new(1., 1.),
tex: Vec2::new(1., 0.),
},
];

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 @@ -104,7 +104,7 @@ mod tests {

let input = syn::parse2(input).expect("parse input");
let actual = impl_vertex(input);
let expectd = quote::quote! {
let expected = quote::quote! {
unsafe impl ::dunge::vertex::Vertex for Vert {
type Projection = VertProjection;
const DECL: ::dunge::vertex::DeclareInput = ::dunge::vertex::DeclareInput::new(&[
Expand All @@ -128,6 +128,6 @@ mod tests {
}
};

assert_eq!(actual.to_string(), expectd.to_string());
assert_eq!(actual.to_string(), expected.to_string());
}
}
7 changes: 5 additions & 2 deletions dunge_shader/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use {
/// The group type description.
pub trait Group {
type Projection: Projection + 'static;
type Visitor: Visitor;
type Visitor<'g>: Visitor
where
Self: 'g;

const DECL: DeclareGroup;

fn group(&self, visit: &mut Self::Visitor);
fn group<'g>(&'g self, visit: &mut Self::Visitor<'g>);
}

pub trait Projection {
Expand Down

0 comments on commit af140aa

Please sign in to comment.