From 0679f06fbfc8553b720cfc85e89337bd39df5cc6 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Fri, 2 Feb 2024 20:27:21 +0600 Subject: [PATCH] Refactor --- dunge/src/bind.rs | 2 +- dunge/src/context.rs | 2 +- dunge/src/draw.rs | 14 +++++++------- dunge/src/el.rs | 6 +++--- dunge/src/group.rs | 6 +----- dunge/src/lib.rs | 20 +++++++++++++------- dunge/src/mesh.rs | 16 ++++++++-------- dunge/src/state.rs | 2 +- dunge/src/texture.rs | 14 +++++++------- dunge/src/update.rs | 16 ++++++++-------- dunge/tests/triangle_group.rs | 22 +++++++++------------- dunge/tests/triangle_index.rs | 11 ++++++----- dunge/tests/triangle_instance.rs | 11 ++++++----- dunge/tests/triangle_vertex.rs | 16 ++++++++-------- examples/cube/src/main.rs | 21 +++++++++------------ examples/triangle/src/main.rs | 2 +- 16 files changed, 89 insertions(+), 92 deletions(-) diff --git a/dunge/src/bind.rs b/dunge/src/bind.rs index 0497b84..62ee6d8 100644 --- a/dunge/src/bind.rs +++ b/dunge/src/bind.rs @@ -36,7 +36,7 @@ impl<'a, V> VisitMember<'a> for &'a Uniform { impl<'a> VisitMember<'a> for BoundTexture<'a> { fn visit_member(self, visitor: &mut Visitor<'a>) { - visitor.push(BindingResource::TextureView(self.get().view())); + visitor.push(BindingResource::TextureView(self.0.view())); } } diff --git a/dunge/src/context.rs b/dunge/src/context.rs index 837fcd5..f447498 100644 --- a/dunge/src/context.rs +++ b/dunge/src/context.rs @@ -56,7 +56,7 @@ impl Context { Layer::new(&self.0, shader, &opts) } - pub fn make_mesh(&self, data: &mesh::Data) -> Mesh + pub fn make_mesh(&self, data: &mesh::MeshData) -> Mesh where V: Vertex, { diff --git a/dunge/src/draw.rs b/dunge/src/draw.rs index 1c44fbb..703d1aa 100644 --- a/dunge/src/draw.rs +++ b/dunge/src/draw.rs @@ -1,29 +1,29 @@ use crate::state::Frame; pub trait Draw { - fn draw(&mut self, frame: Frame); + fn draw(&self, frame: Frame); } -impl Draw for &mut D +impl Draw for &D where D: Draw + ?Sized, { - fn draw(&mut self, frame: Frame) { + fn draw(&self, frame: Frame) { (**self).draw(frame); } } -pub fn from_fn(draw: D) -> impl Draw +pub fn draw(draw: D) -> impl Draw where - D: FnMut(Frame), + D: Fn(Frame), { struct Func(D); impl Draw for Func where - D: FnMut(Frame), + D: Fn(Frame), { - fn draw(&mut self, frame: Frame) { + fn draw(&self, frame: Frame) { (self.0)(frame); } } diff --git a/dunge/src/el.rs b/dunge/src/el.rs index b25b55e..31f28ea 100644 --- a/dunge/src/el.rs +++ b/dunge/src/el.rs @@ -224,7 +224,7 @@ where match ctrl.view.output() { Ok(output) => { let rv = output.render_view(); - cx.state().draw(rv, &mut upd); + cx.state().draw(rv, &upd); output.present(); } Err(SurfaceError::Timeout) => log::info!("suface error: timeout"), @@ -338,7 +338,7 @@ macro_rules! then_try { match $e { ::std::result::Result::Ok(v) => _ = v, ::std::result::Result::Err(e) => { - return $crate::el::Then::Fail(::std::boxed::Box::from(e)); + return $crate::Then::Fail(::std::boxed::Box::from(e)); } } }; @@ -347,7 +347,7 @@ macro_rules! then_try { match $e { ::std::result::Result::Ok(v) => v, ::std::result::Result::Err(e) => { - return $crate::el::Then::Fail(::std::boxed::Box::from(e)); + return $crate::Then::Fail(::std::boxed::Box::from(e)); } } }; diff --git a/dunge/src/group.rs b/dunge/src/group.rs index bfb8529..dbf7d00 100644 --- a/dunge/src/group.rs +++ b/dunge/src/group.rs @@ -8,7 +8,7 @@ use crate::{ pub use dunge_shader::group::Projection; #[derive(Clone, Copy)] -pub struct BoundTexture<'a>(&'a Texture); +pub struct BoundTexture<'a>(pub(crate) &'a Texture); impl<'a> BoundTexture<'a> { pub fn new(texture: &'a T) -> Self @@ -17,10 +17,6 @@ impl<'a> BoundTexture<'a> { { Self(texture.bind_texture()) } - - pub(crate) fn get(self) -> &'a Texture { - self.0 - } } /// Describes a group member type projection. diff --git a/dunge/src/lib.rs b/dunge/src/lib.rs index f884c1a..64b8ec3 100644 --- a/dunge/src/lib.rs +++ b/dunge/src/lib.rs @@ -1,30 +1,30 @@ pub mod bind; pub mod color; -pub mod context; -pub mod draw; +mod context; +mod draw; mod format; pub mod group; mod init; pub mod instance; pub mod layer; pub mod mesh; -pub mod shader; +mod shader; mod state; pub mod texture; pub mod uniform; pub mod vertex; #[cfg(feature = "winit")] -pub mod el; +mod el; #[cfg(feature = "winit")] mod time; #[cfg(feature = "winit")] -pub mod update; +mod update; #[cfg(feature = "winit")] pub mod window; pub mod prelude { - pub use crate::{context::Context, draw, sl, types, update, Frame, Group, Instance, Vertex}; + pub use crate::{context::Context, shader::Shader, sl, types, Frame, Group, Instance, Vertex}; #[cfg(feature = "winit")] pub use crate::el::{Control, KeyCode, Then}; @@ -32,6 +32,8 @@ pub mod prelude { pub use { crate::{ + context::{Context, Error}, + draw::{draw, Draw}, format::Format, init::context, state::{Frame, Options}, @@ -42,4 +44,8 @@ pub use { }; #[cfg(feature = "winit")] -pub use crate::init::window; +pub use crate::{ + el::{Control, Flow, Key, KeyCode, LoopError, SmolStr, Then}, + init::window, + update::{update, update_with_state, Update}, +}; diff --git a/dunge/src/mesh.rs b/dunge/src/mesh.rs index 39ea4e2..396d6c1 100644 --- a/dunge/src/mesh.rs +++ b/dunge/src/mesh.rs @@ -7,18 +7,18 @@ use { type Face = [u16; 3]; #[derive(Clone)] -pub struct Data<'a, V> { +pub struct MeshData<'a, V> { verts: &'a [V], indxs: Option>, } -impl<'a, V> Data<'a, V> { - /// Creates a [mesh data](crate::mesh::Data) from given vertices. +impl<'a, V> MeshData<'a, V> { + /// Creates a [mesh data](crate::mesh::MeshData) from given vertices. pub fn from_verts(verts: &'a [V]) -> Self { Self { verts, indxs: None } } - /// Creates a [mesh data](crate::mesh::Data) from given vertices and indices. + /// Creates a [mesh data](crate::mesh::MeshData) from given vertices and indices. /// /// # Errors /// Returns an [error](crate::mesh::Error) if the passed data is incorrect. @@ -32,7 +32,7 @@ impl<'a, V> Data<'a, V> { Ok(Self { verts, indxs }) } - /// Creates a [mesh data](crate::mesh::Data) from given quadrilaterals. + /// Creates a [mesh data](crate::mesh::MeshData) from given quadrilaterals. /// /// # Errors /// Returns an [error](crate::mesh::TooManyVertices) if too many vertices are passed. @@ -58,7 +58,7 @@ impl<'a, V> Data<'a, V> { } } -/// An error returned from the [mesh data](crate::mesh::Data) constructors. +/// An error returned from the [mesh data](crate::mesh::MeshData) constructors. #[derive(Debug)] pub enum Error { /// Vertices length doesn't fit in [`u16`](std::u16) integer. @@ -98,7 +98,7 @@ pub struct Mesh { } impl Mesh { - pub(crate) fn new(state: &State, data: &Data) -> Self + pub(crate) fn new(state: &State, data: &MeshData) -> Self where V: Vertex, { @@ -164,7 +164,7 @@ mod tests { #[test] fn from_quads() { let verts = [[0, 1, 2, 3], [4, 5, 6, 7]]; - let data = Data::from_quads(&verts).expect("mesh data"); + let data = MeshData::from_quads(&verts).expect("mesh data"); let indxs = data.indxs.expect("indices"); assert_eq!(data.verts.len(), 8); assert_eq!(indxs.len(), 4); diff --git a/dunge/src/state.rs b/dunge/src/state.rs index 1be84d7..87c53e1 100644 --- a/dunge/src/state.rs +++ b/dunge/src/state.rs @@ -87,7 +87,7 @@ impl State { self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed) } - pub fn draw(&self, view: RenderView, mut draw: D) + pub fn draw(&self, view: RenderView, draw: D) where D: Draw, { diff --git a/dunge/src/texture.rs b/dunge/src/texture.rs index e89846a..79a1595 100644 --- a/dunge/src/texture.rs +++ b/dunge/src/texture.rs @@ -11,13 +11,13 @@ use { }; #[derive(Clone, Copy)] -pub struct Data<'a> { +pub struct TextureData<'a> { data: &'a [u8], size: (u32, u32), format: Format, } -impl<'a> Data<'a> { +impl<'a> TextureData<'a> { pub const fn empty(size: (u32, u32), format: Format) -> Result { let (width, height) = size; if width == 0 || height == 0 { @@ -64,7 +64,7 @@ impl<'a> Data<'a> { } } -/// The [texture data](crate::texture::Data) error. +/// The [texture data](crate::texture::TextureData) error. #[derive(Debug)] pub enum Error { /// The texture data is zero sized. @@ -85,7 +85,7 @@ impl fmt::Display for Error { impl error::Error for Error {} -/// The [texture data](crate::texture::Data) is zero sized. +/// The [texture data](crate::texture::TextureData) is zero sized. #[derive(Debug)] pub struct ZeroSized; @@ -103,7 +103,7 @@ pub struct Texture { } impl Texture { - fn new(state: &State, mut usage: TextureUsages, data: Data) -> Self { + fn new(state: &State, mut usage: TextureUsages, data: TextureData) -> Self { use wgpu::*; let (width, height) = data.size; @@ -446,9 +446,9 @@ pub trait Make: private::Sealed { fn make(self, maker: Maker) -> Self::Out; } -impl private::Sealed for Data<'_> {} +impl private::Sealed for TextureData<'_> {} -impl Make for Data<'_> { +impl Make for TextureData<'_> { type Out = Texture; fn make(self, Maker { state, usage }: Maker) -> Self::Out { diff --git a/dunge/src/update.rs b/dunge/src/update.rs index d9b2839..aafe0c0 100644 --- a/dunge/src/update.rs +++ b/dunge/src/update.rs @@ -9,28 +9,28 @@ pub trait Update: Draw { fn update(&mut self, ctrl: &Control) -> Self::Flow; } -pub fn from_fn(mut upd: U, mut draw: D) -> impl Update +pub fn update(mut upd: U, draw: D) -> impl Update where U: FnMut(&Control) -> F, F: Flow, - D: FnMut(Frame), + D: Fn(Frame), { - with_state((), move |(), ctrl| upd(ctrl), move |(), frame| draw(frame)) + update_with_state((), move |(), ctrl| upd(ctrl), move |(), frame| draw(frame)) } -pub fn with_state(state: S, upd: U, draw: D) -> impl Update +pub fn update_with_state(state: S, upd: U, draw: D) -> impl Update where U: FnMut(&mut S, &Control) -> F, F: Flow, - D: FnMut(&S, Frame), + D: Fn(&S, Frame), { struct Func(S, U, D); impl Draw for Func where - D: FnMut(&S, Frame), + D: Fn(&S, Frame), { - fn draw(&mut self, frame: Frame) { + fn draw(&self, frame: Frame) { (self.2)(&self.0, frame); } } @@ -39,7 +39,7 @@ where where U: FnMut(&mut S, &Control) -> F, F: Flow, - D: FnMut(&S, Frame), + D: Fn(&S, Frame), { type Flow = F; diff --git a/dunge/tests/triangle_group.rs b/dunge/tests/triangle_group.rs index 87481ac..7a6bdaa 100644 --- a/dunge/tests/triangle_group.rs +++ b/dunge/tests/triangle_group.rs @@ -7,11 +7,10 @@ fn render() -> Result<(), Error> { use { dunge::{ color::Rgba, - draw, group::BoundTexture, - mesh, + mesh::MeshData, sl::{self, Groups, InVertex, Out}, - texture::{self, Filter, Sampler}, + texture::{Filter, Sampler, TextureData}, Format, Group, Vertex, }, glam::Vec2, @@ -45,10 +44,9 @@ fn render() -> Result<(), Error> { let map = { let texture = { - use texture::Data; - let gradient = Image::decode(include_bytes!("gradient.png")); - let data = Data::new(&gradient.data, gradient.size, Format::RgbAlpha)?.with_bind(); + let data = + TextureData::new(&gradient.data, gradient.size, Format::RgbAlpha)?.with_bind(); cx.make_texture(data) }; @@ -65,15 +63,13 @@ fn render() -> Result<(), Error> { let layer = cx.make_layer(&shader, Format::RgbAlpha); let view = { - use texture::Data; - - let data = Data::empty(SIZE, Format::RgbAlpha)?.with_draw().with_copy(); + let data = TextureData::empty(SIZE, Format::RgbAlpha)? + .with_draw() + .with_copy(); cx.make_texture(data) }; let mesh = { - use mesh::Data; - const VERTS: [Vert; 3] = [ Vert { pos: [0., -0.75], @@ -89,13 +85,13 @@ fn render() -> Result<(), Error> { }, ]; - let data = Data::from_verts(&VERTS); + let data = MeshData::from_verts(&VERTS); cx.make_mesh(&data) }; let buffer = cx.make_copy_buffer(SIZE); let opts = Rgba::from_standard([0., 0., 0., 1.]); - let draw = draw::from_fn(|mut frame| { + let draw = dunge::draw(|mut frame| { frame.layer(&layer, opts).bind(&map).draw(&mesh); frame.copy_texture(&buffer, &view); }); diff --git a/dunge/tests/triangle_index.rs b/dunge/tests/triangle_index.rs index 0864787..7bf29c4 100644 --- a/dunge/tests/triangle_index.rs +++ b/dunge/tests/triangle_index.rs @@ -7,9 +7,9 @@ fn render() -> Result<(), Error> { use { dunge::{ color::Rgba, - draw, sl::{self, Index, Out}, - texture, Format, + texture::TextureData, + Format, }, glam::Vec4, helpers::Image, @@ -36,15 +36,16 @@ fn render() -> Result<(), Error> { let layer = cx.make_layer(&shader, Format::RgbAlpha); let view = { - use texture::Data; + let data = TextureData::empty(SIZE, Format::RgbAlpha)? + .with_draw() + .with_copy(); - 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| { + let draw = dunge::draw(|mut frame| { frame.layer(&layer, opts).bind_empty().draw_points(3); frame.copy_texture(&buffer, &view); }); diff --git a/dunge/tests/triangle_instance.rs b/dunge/tests/triangle_instance.rs index fdc2804..d382b03 100644 --- a/dunge/tests/triangle_instance.rs +++ b/dunge/tests/triangle_instance.rs @@ -7,10 +7,10 @@ fn render() -> Result<(), Error> { use { dunge::{ color::Rgba, - draw, instance::Row, sl::{self, InInstance, Index, Out}, - texture, Format, Instance, + texture::TextureData, + Format, Instance, }, glam::Vec2, helpers::Image, @@ -40,9 +40,10 @@ fn render() -> Result<(), Error> { let layer = cx.make_layer(&shader, Format::RgbAlpha); let view = { - use texture::Data; + let data = TextureData::empty(SIZE, Format::RgbAlpha)? + .with_draw() + .with_copy(); - let data = Data::empty(SIZE, Format::RgbAlpha)?.with_draw().with_copy(); cx.make_texture(data) }; @@ -55,7 +56,7 @@ fn render() -> Result<(), Error> { let buffer = cx.make_copy_buffer(SIZE); let opts = Rgba::from_standard([0., 0., 0., 1.]); - let draw = draw::from_fn(|mut frame| { + let draw = dunge::draw(|mut frame| { frame .layer(&layer, opts) .bind_empty() diff --git a/dunge/tests/triangle_vertex.rs b/dunge/tests/triangle_vertex.rs index 9907735..d07bbea 100644 --- a/dunge/tests/triangle_vertex.rs +++ b/dunge/tests/triangle_vertex.rs @@ -7,9 +7,10 @@ fn render() -> Result<(), Error> { use { dunge::{ color::Rgba, - draw, mesh, + mesh::MeshData, sl::{self, InVertex, Out}, - texture, Format, Vertex, + texture::TextureData, + Format, Vertex, }, glam::Vec2, helpers::Image, @@ -33,28 +34,27 @@ fn render() -> Result<(), Error> { let layer = cx.make_layer(&shader, Format::RgbAlpha); let view = { - use texture::Data; + let data = TextureData::empty(SIZE, Format::RgbAlpha)? + .with_draw() + .with_copy(); - 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([0., -0.75], [1., 0., 0.]), Vert([0.866, 0.75], [0., 1., 0.]), Vert([-0.866, 0.75], [0., 0., 1.]), ]; - let data = Data::from_verts(&VERTS); + let data = MeshData::from_verts(&VERTS); cx.make_mesh(&data) }; let buffer = cx.make_copy_buffer(SIZE); let opts = Rgba::from_standard([0., 0., 0., 1.]); - let draw = draw::from_fn(|mut frame| { + let draw = dunge::draw(|mut frame| { frame.layer(&layer, opts).bind_empty().draw(&mesh); frame.copy_texture(&buffer, &view); }); diff --git a/examples/cube/src/main.rs b/examples/cube/src/main.rs index ff39bc8..66eef5b 100644 --- a/examples/cube/src/main.rs +++ b/examples/cube/src/main.rs @@ -13,9 +13,10 @@ async fn run() -> Result<(), Error> { color::Rgba, glam::{Mat4, Quat, Vec2, Vec3}, group::BoundTexture, + mesh::MeshData, prelude::*, sl::{Groups, InVertex, Out}, - texture::{self, Filter, Sampler, Texture, ZeroSized}, + texture::{self, Filter, Sampler, Texture, TextureData, ZeroSized}, uniform::Uniform, Format, Options, }; @@ -83,9 +84,9 @@ async fn run() -> Result<(), Error> { }; let make_screen_tex = |cx: &Context, size| -> Result<_, ZeroSized> { - use dunge::texture::Data; - - let data = Data::empty(size, Format::RgbAlpha)?.with_bind().with_draw(); + let data = TextureData::empty(size, Format::RgbAlpha)? + .with_bind() + .with_draw(); Ok(cx.make_texture(data)) }; @@ -103,8 +104,6 @@ async fn run() -> Result<(), Error> { }; let mesh = { - use dunge::mesh::Data; - const P: f32 = 0.5; const VERTS: [Vert; 8] = [ Vert { @@ -152,13 +151,11 @@ async fn run() -> Result<(), Error> { [7, 6, 1], // +z ]; - let data = Data::new(&VERTS, &INDXS)?; + let data = MeshData::new(&VERTS, &INDXS)?; cx.make_mesh(&data) }; let screen_mesh = { - use dunge::mesh::Data; - const VERTS: [Screen; 4] = [ Screen([-1., -1.], [0., 1.]), Screen([1., -1.], [1., 1.]), @@ -166,7 +163,7 @@ async fn run() -> Result<(), Error> { Screen([-1., 1.], [0., 0.]), ]; - let data = Data::from_quads(&[VERTS])?; + let data = MeshData::from_quads(&[VERTS])?; cx.make_mesh(&data) }; @@ -208,7 +205,7 @@ async fn run() -> Result<(), Error> { .draw(&mesh); }; - cx.draw_to(state.tex, draw::from_fn(main)); + cx.draw_to(state.tex, dunge::draw(main)); frame .layer(&screen_layer, Options::default()) @@ -226,6 +223,6 @@ async fn run() -> Result<(), Error> { bind_map, }; - let handle = update::with_state(state, upd, draw); + let handle = dunge::update_with_state(state, upd, draw); window.run(handle).map_err(Box::from) } diff --git a/examples/triangle/src/main.rs b/examples/triangle/src/main.rs index 5948961..f0ed081 100644 --- a/examples/triangle/src/main.rs +++ b/examples/triangle/src/main.rs @@ -63,6 +63,6 @@ async fn run() -> Result<(), Error> { frame.layer(&layer, clear).bind(&bind).draw_points(3); }; - window.run(update::from_fn(upd, draw))?; + window.run(dunge::update(upd, draw))?; Ok(()) }