Skip to content

Commit

Permalink
Deriving Vertex trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 4, 2024
1 parent f364bbd commit f7534e6
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 146 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[workspace]
members = ["dunge", "dunge_macros", "dunge_shader", "helpers"]
default-members = ["dunge"]
resolver = "2"

[workspace.dependencies]
bytemuck = { version = "1.13" }
bytemuck = "1.13"
glam = "0.25"

[workspace.lints.clippy]
use_self = "deny"
Expand Down
2 changes: 1 addition & 1 deletion dunge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust-version = "1.71"
dunge_macros = { version = "0.2.3", path = "../dunge_macros" }
dunge_shader = { version = "0.2.3", path = "../dunge_shader" }
bytemuck = { workspace = true }
glam = { workspace = true }
instant = "0.1"
log = "0.4"
wgpu = { version = "0.18", default-features = false, features = ["naga"] }
Expand All @@ -38,7 +39,6 @@ features = ["android-native-activity"]

[dev-dependencies]
helpers = { path = "../helpers" }
glam = "0.25"

[lints]
workspace = true
7 changes: 6 additions & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ pub mod state;
pub mod texture;
#[allow(dead_code)]
mod time;
pub mod vertex;

pub use dunge_shader::{sl, vertex};
pub use {
dunge_macros::Vertex,
dunge_shader::{group::Group, sl, types, vertex::Vertex},
glam,
};
3 changes: 2 additions & 1 deletion dunge/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
crate::{
group::TypedGroup,
sl::{GroupMemberType, InputInfo, IntoModule, Module, Stages, VectorType},
sl::{InputInfo, IntoModule, Module, Stages},
state::State,
types::{GroupMemberType, VectorType},
},
std::marker::PhantomData,
wgpu::{PipelineLayout, ShaderModule, VertexAttribute, VertexBufferLayout},
Expand Down
66 changes: 66 additions & 0 deletions dunge/src/vertex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{
sl::{ReadInput, Ret},
types::{self, VectorType},
};

pub use dunge_shader::vertex::*;

pub trait InputProjection {
const TYPE: VectorType;
type Field;
fn input_projection(id: u32, index: u32) -> Self::Field;
}

impl InputProjection for [f32; 2] {
const TYPE: VectorType = VectorType::Vec2f;
type Field = Ret<ReadInput, types::Vec2<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}

impl InputProjection for [f32; 3] {
const TYPE: VectorType = VectorType::Vec3f;
type Field = Ret<ReadInput, types::Vec3<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}

impl InputProjection for [f32; 4] {
const TYPE: VectorType = VectorType::Vec4f;
type Field = Ret<ReadInput, types::Vec4<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}

impl InputProjection for glam::Vec2 {
const TYPE: VectorType = VectorType::Vec2f;
type Field = Ret<ReadInput, types::Vec2<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}

impl InputProjection for glam::Vec3 {
const TYPE: VectorType = VectorType::Vec3f;
type Field = Ret<ReadInput, types::Vec3<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}

impl InputProjection for glam::Vec4 {
const TYPE: VectorType = VectorType::Vec4f;
type Field = Ret<ReadInput, types::Vec4<f32>>;

fn input_projection(id: u32, index: u32) -> Self::Field {
ReadInput::new(id, index)
}
}
4 changes: 3 additions & 1 deletion dunge/tests/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
draw,
sl::{self, Index, Out},
state::{Options, Render},
texture::{Data, Format},
texture::{self, Format},
},
glam::Vec4,
helpers::Image,
Expand Down Expand Up @@ -34,6 +34,8 @@ fn render() -> Result<(), Error> {
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)
};
Expand Down
Binary file added dunge/tests/triangle_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions dunge/tests/triangle_color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use {
dunge::{
color::Rgba,
context::Context,
draw, mesh,
sl::{self, Input, Out},
state::{Options, Render},
texture::{self, Format},
Vertex,
},
glam::{Vec2, Vec3},
helpers::Image,
std::{error, fs},
};

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

#[test]
fn render() -> Result<(), Error> {
const SIZE: (u32, u32) = (300, 300);

#[repr(C)]
#[derive(Vertex)]
struct Vert {
pos: Vec2,
col: Vec3,
}

let triangle = |input: Input<Vert>| Out {
place: sl::compose(input.pos, Vec2::new(0., 1.)),
color: sl::vec4_with(sl::fragment(input.col), 1.),
};

let cx = helpers::block_on(Context::new())?;
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 mesh = {
use mesh::Data;

const VERTS: [Vert; 3] = [
Vert {
pos: Vec2::new(0., -0.75),
col: Vec3::new(1., 0., 0.),
},
Vert {
pos: Vec2::new(0.866, 0.75),
col: Vec3::new(0., 1., 0.),
},
Vert {
pos: Vec2::new(-0.866, 0.75),
col: Vec3::new(0., 0., 1.),
},
];

let data = Data::from_verts(&VERTS);
cx.make_mesh(&data)
};

let buffer = cx.make_copy_buffer(SIZE);
let options = Options::default().with_clear(Rgba::from_standard([0., 0., 0., 1.]));
let draw = draw::from_fn(|mut frame| {
frame.layer(&layer, options).bind_empty().draw(&mesh);
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_color.png", image.encode())?;
Ok(())
}
8 changes: 3 additions & 5 deletions dunge_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ mod vertex;
use proc_macro::TokenStream;

/// Derive implementation for the vector type.
#[proc_macro_derive(Vertex, attributes(position, color, texture))]
#[proc_macro_derive(Vertex)]
pub fn derive_vertex(input: TokenStream) -> TokenStream {
use syn::DeriveInput;

let derive = syn::parse_macro_input!(input as DeriveInput);
vertex::impl_vertex(derive).into()
let input = syn::parse_macro_input!(input);
vertex::impl_vertex(input).into()
}
Loading

0 comments on commit f7534e6

Please sign in to comment.