Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 6, 2024
1 parent 2a01a9d commit 3f1cf5c
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 67 deletions.
7 changes: 6 additions & 1 deletion dunge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ 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"
instant = { version = "0.1", optional = true }
log = "0.4"
wgpu = { version = "0.18", default-features = false, features = ["naga"] }

[dependencies.winit]
version = "0.29"
default-features = false
features = ["rwh_05", "x11"]
optional = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -36,9 +37,13 @@ features = ["Document", "Window", "Element", "Performance"]
version = "0.29"
default-features = false
features = ["android-native-activity"]
optional = true

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

[features]
winit = ["dep:instant", "dep:winit"]

[lints]
workspace = true
29 changes: 12 additions & 17 deletions dunge/src/bind.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use {
crate::{
group::{BoundTexture, Group},
shader::Shader,
state::State,
texture::Sampler,
},
crate::{group::BoundTexture, shader::Shader, state::State, texture::Sampler, Group},
std::{any::TypeId, fmt, marker::PhantomData, sync::Arc},
wgpu::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindingResource, Device,
Expand Down Expand Up @@ -74,12 +69,12 @@ pub struct Bind<'a> {
}

#[derive(Clone)]
pub struct GroupBinding {
pub struct SharedBinding {
shader_id: usize,
groups: Arc<[BindGroup]>,
}

impl GroupBinding {
impl SharedBinding {
fn new(shader_id: usize, groups: Vec<BindGroup>) -> Self {
Self {
shader_id,
Expand All @@ -88,7 +83,7 @@ impl GroupBinding {
}
}

impl Binding for GroupBinding {
impl Binding for SharedBinding {
fn binding(&self) -> Bind {
Bind {
shader_id: self.shader_id,
Expand All @@ -101,7 +96,7 @@ pub type Update = Result<(), ForeignShader>;

pub(crate) fn update<G>(
state: &State,
uni: &mut UniqueGroupBinding,
uni: &mut UniqueBinding,
handler: GroupHandler<G>,
group: &G,
) -> Update
Expand All @@ -125,10 +120,10 @@ where
Ok(())
}

pub struct UniqueGroupBinding(GroupBinding);
pub struct UniqueBinding(SharedBinding);

impl UniqueGroupBinding {
pub fn into_inner(self) -> GroupBinding {
impl UniqueBinding {
pub fn shared(self) -> SharedBinding {
self.0
}

Expand All @@ -137,7 +132,7 @@ impl UniqueGroupBinding {
}
}

impl Binding for UniqueGroupBinding {
impl Binding for UniqueBinding {
fn binding(&self) -> Bind {
self.0.binding()
}
Expand Down Expand Up @@ -211,12 +206,12 @@ impl<'a> Binder<'a> {
}
}

pub fn into_binding(self) -> UniqueGroupBinding {
pub fn into_binding(self) -> UniqueBinding {
if self.groups.len() != self.layout.len() {
panic!("some group bindings is not set");
}

let binding = GroupBinding::new(self.shader_id, self.groups);
UniqueGroupBinding(binding)
let binding = SharedBinding::new(self.shader_id, self.groups);
UniqueBinding(binding)
}
}
10 changes: 5 additions & 5 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use {
crate::{
bind::{self, Binder, GroupHandler, UniqueGroupBinding, Update, Visit},
bind::{self, Binder, GroupHandler, UniqueBinding, Update, Visit},
draw::Draw,
layer::Layer,
mesh::{self, Mesh},
shader::Shader,
sl::IntoModule,
state::{Render, State},
state::{Render, State, View},
texture::{
self, CopyBuffer, CopyBufferView, DrawTexture, Format, Make, MapResult, Mapped, Sampler,
},
vertex::Vertex,
Vertex,
},
std::{error, fmt, future::IntoFuture, sync::Arc},
};
Expand Down Expand Up @@ -83,13 +83,13 @@ impl Context {
T: DrawTexture,
D: Draw,
{
let view = texture.draw_texture();
let view = View::from_texture(texture.draw_texture());
self.0.draw(render, view, draw)
}

pub fn update_group<G>(
&self,
uni: &mut UniqueGroupBinding,
uni: &mut UniqueBinding,
handler: GroupHandler<G>,
group: &G,
) -> Update
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
types::{self, GroupMemberType},
};

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

#[derive(Clone, Copy)]
pub struct BoundTexture<'a>(&'a Texture);
Expand Down
5 changes: 3 additions & 2 deletions dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ pub mod mesh;
pub mod shader;
pub mod state;
pub mod texture;
#[allow(dead_code)]
mod time;
pub mod vertex;

#[cfg(feature = "winit")]
mod time;

pub use {
dunge_macros::{Group, Vertex},
dunge_shader::{group::Group, sl, types, vertex::Vertex},
Expand Down
5 changes: 1 addition & 4 deletions dunge/src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
state::State,
vertex::{self, Vertex},
},
crate::{state::State, vertex, Vertex},
std::{borrow::Cow, error, fmt, marker::PhantomData, mem, slice},
wgpu::{Buffer, RenderPass},
};
Expand Down
30 changes: 13 additions & 17 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
context::Error,
draw::Draw,
layer::{Layer, SetLayer},
texture::{CopyBuffer, CopyTexture, Format, Texture},
texture::{CopyBuffer, CopyTexture, DrawTexture, Format, Texture},
},
std::sync::atomic::{self, AtomicUsize},
wgpu::{Adapter, Color, CommandEncoder, Device, Instance, LoadOp, Queue, TextureView},
Expand Down Expand Up @@ -76,12 +76,11 @@ impl State {
self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn draw<V, D>(&self, render: &mut Render, view: &V, draw: D)
pub fn draw<D>(&self, render: &mut Render, view: View, draw: D)
where
V: AsView,
D: Draw,
{
draw.draw(render.0.make(&self.device, view.as_view()));
draw.draw(render.0.make(&self.device, view));
let buffers = render.0.drain().map(CommandEncoder::finish);
self.queue.submit(buffers);
}
Expand Down Expand Up @@ -117,11 +116,12 @@ pub struct Frame<'v, 'e> {
}

impl Frame<'_, '_> {
pub fn subframe<'e, 'v, V>(&'e mut self, view: &'v V) -> Frame<'v, 'e>
pub fn subframe<'e, 'v, T>(&'e mut self, texture: &'v T) -> Frame<'v, 'e>
where
V: AsView,
T: DrawTexture,
{
self.encoders.make(self.device, view.as_view())
let view = View::from_texture(texture.draw_texture());
self.encoders.make(self.device, view)
}

pub fn layer<'p, V>(&'p mut self, layer: &'p Layer<V>, opts: Options) -> SetLayer<'p, V> {
Expand Down Expand Up @@ -190,21 +190,17 @@ impl Encoders {
}
}

pub trait AsView {
fn as_view(&self) -> View;
}

#[derive(Clone, Copy)]
pub struct View<'v> {
pub(crate) struct View<'v> {
txview: &'v TextureView,
format: Format,
}

impl AsView for Texture {
fn as_view(&self) -> View {
View {
txview: self.view(),
format: self.format(),
impl<'v> View<'v> {
pub fn from_texture(texture: &'v Texture) -> Self {
Self {
txview: texture.view(),
format: texture.format(),
}
}
}
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::*;
pub use dunge_shader::vertex::{verts_as_bytes, DeclareInput, Projection};

/// Describes an input type projection.
///
Expand Down
9 changes: 3 additions & 6 deletions dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ fn render() -> Result<(), Error> {
sam: &'a Sampler,
}

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 triangle = |vert: Input<Vert>, Groups(map): Groups<Map>| 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
12 changes: 6 additions & 6 deletions dunge_macros/src/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::utils,
crate::ident,
proc_macro2::{Span, TokenStream},
syn::{spanned::Spanned, Data, DataStruct, DeriveInput, GenericParam, Ident, Lifetime},
};
Expand Down Expand Up @@ -66,24 +66,24 @@ pub(crate) fn derive(input: DeriveInput) -> TokenStream {
});

let group_visit_members = iter::zip(0.., &fields).map(|(index, field)| {
let ident = utils::make_ident(index, field.ident.as_ref());
let ident = ident::make(index, field.ident.as_ref());
quote::quote! { ::dunge::bind::VisitMember::visit_member(self.#ident, visitor) }
});

let group_fields = iter::zip(0.., &fields).map(|(index, field)| {
let ident = utils::make_ident(index, field.ident.as_ref());
let ident = ident::make(index, field.ident.as_ref());
let ty = &field.ty;
quote::quote! { #ident: <#ty as ::dunge::group::MemberProjection>::Field }
});

let group_member_projections = iter::zip(0.., &fields).map(|(index, field)| {
let ident = utils::make_ident(index, field.ident.as_ref());
let ident = ident::make(index, field.ident.as_ref());
let ty = &field.ty;
quote::quote! { #ident: <#ty as ::dunge::group::MemberProjection>::member_projection(id, #index, out.clone()) }
});

quote::quote! {
impl<#(#lts),*> ::dunge::group::Group for #name<#(#lts),*> {
impl<#(#lts),*> ::dunge::Group for #name<#(#lts),*> {
type Projection = #projection_name<#(#static_lts),*>;
const DECL: ::dunge::group::DeclareGroup = ::dunge::group::DeclareGroup::new(&[
#(#group_types),*,
Expand Down Expand Up @@ -126,7 +126,7 @@ mod tests {
let input = syn::parse2(input).expect("parse input");
let actual = derive(input);
let expected = quote::quote! {
impl<'a> ::dunge::group::Group for Map<'a> {
impl<'a> ::dunge::Group for Map<'a> {
type Projection = MapProjection<'static>;
const DECL: ::dunge::group::DeclareGroup = ::dunge::group::DeclareGroup::new(&[
<BoundTexture<'a> as ::dunge::group::MemberProjection>::TYPE,
Expand Down
2 changes: 1 addition & 1 deletion dunge_macros/src/utils.rs → dunge_macros/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {proc_macro2::Ident, std::borrow::Cow};

pub(crate) fn make_ident(index: u32, ident: Option<&Ident>) -> Cow<Ident> {
pub(crate) fn make(index: u32, ident: Option<&Ident>) -> Cow<Ident> {
match ident {
Some(ident) => Cow::Borrowed(ident),
None => Cow::Owned(quote::format_ident!("{index}")),
Expand Down
2 changes: 1 addition & 1 deletion dunge_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod group;
mod utils;
mod ident;
mod vertex;

use proc_macro::TokenStream;
Expand Down
10 changes: 5 additions & 5 deletions dunge_macros/src/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::utils,
crate::ident,
proc_macro2::TokenStream,
syn::{meta::ParseNestedMeta, spanned::Spanned, Attribute, Data, DataStruct, DeriveInput},
};
Expand Down Expand Up @@ -39,19 +39,19 @@ pub(crate) fn derive(input: DeriveInput) -> TokenStream {
});

let projection_fields = iter::zip(0.., &fields).map(|(index, field)| {
let ident = utils::make_ident(index, field.ident.as_ref());
let ident = ident::make(index, field.ident.as_ref());
let ty = &field.ty;
quote::quote! { #ident: <#ty as ::dunge::vertex::InputProjection>::Field }
});

let projection_inputs = iter::zip(0.., &fields).map(|(index, field)| {
let ident = utils::make_ident(index, field.ident.as_ref());
let ident = ident::make(index, field.ident.as_ref());
let ty = &field.ty;
quote::quote! { #ident: <#ty as ::dunge::vertex::InputProjection>::input_projection(id, #index) }
});

quote::quote! {
unsafe impl ::dunge::vertex::Vertex for #name {
unsafe impl ::dunge::Vertex for #name {
type Projection = #projection_name;
const DECL: ::dunge::vertex::DeclareInput = ::dunge::vertex::DeclareInput::new(&[
#(#vector_types),*,
Expand Down Expand Up @@ -101,7 +101,7 @@ mod tests {
let input = syn::parse2(input).expect("parse input");
let actual = derive(input);
let expected = quote::quote! {
unsafe impl ::dunge::vertex::Vertex for Vert {
unsafe impl ::dunge::Vertex for Vert {
type Projection = VertProjection;
const DECL: ::dunge::vertex::DeclareInput = ::dunge::vertex::DeclareInput::new(&[
<[f32; 2] as ::dunge::vertex::InputProjection>::TYPE,
Expand Down

0 comments on commit 3f1cf5c

Please sign in to comment.