diff --git a/Cargo.toml b/Cargo.toml index 7eae211..4b3d555 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "dunge", "dunge_macros", "dunge_shader", + "dunge_tools", "examples/triangle", "helpers", ] diff --git a/dunge/Cargo.toml b/dunge/Cargo.toml index fb6b559..df8b55a 100644 --- a/dunge/Cargo.toml +++ b/dunge/Cargo.toml @@ -11,8 +11,8 @@ repository = "https://github.com/nanoqsh/dunge" rust-version = "1.71" [dependencies] -dunge_macros = { version = "0.2.3", path = "../dunge_macros" } -dunge_shader = { version = "0.2.3", path = "../dunge_shader" } +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 = { version = "0.1", optional = true } diff --git a/dunge/src/context.rs b/dunge/src/context.rs index 5990e66..e0ab547 100644 --- a/dunge/src/context.rs +++ b/dunge/src/context.rs @@ -44,7 +44,7 @@ impl Context { where V: Value, { - Uniform::new(&self.0, &val.value()) + Uniform::new(&self.0, val.value().as_ref()) } pub fn make_layer(&self, format: Format, shader: &Shader) -> Layer { diff --git a/dunge/src/shader.rs b/dunge/src/shader.rs index 6d85bf5..f19fc58 100644 --- a/dunge/src/shader.rs +++ b/dunge/src/shader.rs @@ -11,7 +11,7 @@ use { pub struct Shader { inner: Inner, - ty: PhantomData, + vert: PhantomData, } impl Shader { @@ -21,7 +21,7 @@ impl Shader { { Self { inner: Inner::new(state, module.into_module()), - ty: PhantomData, + vert: PhantomData, } } diff --git a/dunge/src/uniform.rs b/dunge/src/uniform.rs index 0ae5299..66a7756 100644 --- a/dunge/src/uniform.rs +++ b/dunge/src/uniform.rs @@ -2,20 +2,19 @@ use { crate::{ context::Context, state::State, - types::{self, MemberType, ScalarType, VectorType}, + types::{self, MatrixType, MemberType, ScalarType, VectorType}, }, - std::{marker::PhantomData, sync::Arc}, + std::marker::PhantomData, wgpu::Buffer, }; -#[derive(Clone)] pub struct Uniform { - buf: Arc, - ty: PhantomData, + buf: Buffer, + vert: PhantomData, } impl Uniform { - pub(crate) fn new(state: &State, data: &Data) -> Self { + pub(crate) fn new(state: &State, contents: &[u8]) -> Self { use wgpu::{ util::{BufferInitDescriptor, DeviceExt}, BufferUsages, @@ -24,7 +23,7 @@ impl Uniform { let buf = { let desc = BufferInitDescriptor { label: None, - contents: data.as_slice(), + contents, usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST, }; @@ -32,8 +31,8 @@ impl Uniform { }; Self { - buf: Arc::new(buf), - ty: PhantomData, + buf, + vert: PhantomData, } } @@ -43,7 +42,7 @@ impl Uniform { { let queue = cx.state().queue(); let data = val.value(); - queue.write_buffer(&self.buf, 0, data.as_slice()); + queue.write_buffer(&self.buf, 0, data.as_ref()); } pub(crate) fn buffer(&self) -> &Buffer { @@ -54,7 +53,8 @@ impl Uniform { pub trait Value: private::Sealed { const TYPE: MemberType; type Type; - fn value(self) -> Data; + type Data: AsRef<[u8]>; + fn value(self) -> Self::Data; } impl private::Sealed for f32 {} @@ -62,8 +62,9 @@ impl private::Sealed for f32 {} impl Value for f32 { const TYPE: MemberType = MemberType::Scalar(ScalarType::Float); type Type = Self; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { Data([self, 0., 0., 0.]) } } @@ -73,8 +74,9 @@ impl private::Sealed for [f32; 2] {} impl Value for [f32; 2] { const TYPE: MemberType = MemberType::Vector(VectorType::Vec2f); type Type = types::Vec2; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { let [x, y] = self; Data([x, y, 0., 0.]) } @@ -85,8 +87,9 @@ impl private::Sealed for [f32; 3] {} impl Value for [f32; 3] { const TYPE: MemberType = MemberType::Vector(VectorType::Vec3f); type Type = types::Vec3; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { let [x, y, z] = self; Data([x, y, z, 0.]) } @@ -97,10 +100,10 @@ impl private::Sealed for [f32; 4] {} impl Value for [f32; 4] { const TYPE: MemberType = MemberType::Vector(VectorType::Vec4f); type Type = types::Vec4; + type Data = Data; - fn value(self) -> Data { - let [x, y, z, w] = self; - Data([x, y, z, w]) + fn value(self) -> Self::Data { + Data(self) } } @@ -109,8 +112,9 @@ impl private::Sealed for glam::Vec2 {} impl Value for glam::Vec2 { const TYPE: MemberType = MemberType::Vector(VectorType::Vec2f); type Type = types::Vec2; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { self.to_array().value() } } @@ -120,8 +124,9 @@ impl private::Sealed for glam::Vec3 {} impl Value for glam::Vec3 { const TYPE: MemberType = MemberType::Vector(VectorType::Vec3f); type Type = types::Vec3; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { self.to_array().value() } } @@ -131,16 +136,53 @@ impl private::Sealed for glam::Vec4 {} impl Value for glam::Vec4 { const TYPE: MemberType = MemberType::Vector(VectorType::Vec4f); type Type = types::Vec4; + type Data = Data; - fn value(self) -> Data { + fn value(self) -> Self::Data { self.to_array().value() } } -pub struct Data([f32; 4]); +impl private::Sealed for glam::Mat2 {} -impl Data { - fn as_slice(&self) -> &[u8] { +impl Value for glam::Mat2 { + const TYPE: MemberType = MemberType::Matrix(MatrixType::Mat2); + type Type = types::Mat2; + type Data = Data; + + fn value(self) -> Self::Data { + self.to_cols_array().value() + } +} + +impl private::Sealed for glam::Mat3 {} + +impl Value for glam::Mat3 { + const TYPE: MemberType = MemberType::Matrix(MatrixType::Mat3); + type Type = types::Mat3; + type Data = Data<9>; + + fn value(self) -> Self::Data { + Data(self.to_cols_array()) + } +} + +impl private::Sealed for glam::Mat4 {} + +impl Value for glam::Mat4 { + const TYPE: MemberType = MemberType::Matrix(MatrixType::Mat4); + type Type = types::Mat4; + type Data = Data<16>; + + fn value(self) -> Self::Data { + Data(self.to_cols_array()) + } +} + +pub struct Data([f32; N]); + +impl AsRef<[u8]> for Data { + fn as_ref(&self) -> &[u8] { bytemuck::cast_slice(&self.0) } } diff --git a/dunge_tools/Cargo.toml b/dunge_tools/Cargo.toml new file mode 100644 index 0000000..cb7c378 --- /dev/null +++ b/dunge_tools/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "dunge_tools" +version = "0.2.3" +edition = "2021" +description = "Additional tools for the dunge library" +license = "MIT" +documentation = "https://docs.rs/dunge" +repository = "https://github.com/nanoqsh/dunge" + +[dependencies] +dunge = { version = "=0.2.3", path = "../dunge" } + +[lints] +workspace = true diff --git a/dunge_tools/src/lib.rs b/dunge_tools/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/dunge_tools/src/lib.rs @@ -0,0 +1 @@ +