Skip to content

Commit

Permalink
Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 11, 2024
1 parent e040a09 commit d90460b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"dunge",
"dunge_macros",
"dunge_shader",
"dunge_tools",
"examples/triangle",
"helpers",
]
Expand Down
4 changes: 2 additions & 2 deletions dunge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V>(&self, format: Format, shader: &Shader<V>) -> Layer<V> {
Expand Down
4 changes: 2 additions & 2 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {

pub struct Shader<V> {
inner: Inner,
ty: PhantomData<V>,
vert: PhantomData<V>,
}

impl<V> Shader<V> {
Expand All @@ -21,7 +21,7 @@ impl<V> Shader<V> {
{
Self {
inner: Inner::new(state, module.into_module()),
ty: PhantomData,
vert: PhantomData,
}
}

Expand Down
88 changes: 65 additions & 23 deletions dunge/src/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V> {
buf: Arc<Buffer>,
ty: PhantomData<V>,
buf: Buffer,
vert: PhantomData<V>,
}

impl<V> Uniform<V> {
pub(crate) fn new(state: &State, data: &Data) -> Self {
pub(crate) fn new(state: &State, contents: &[u8]) -> Self {
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
BufferUsages,
Expand All @@ -24,16 +23,16 @@ impl<V> Uniform<V> {
let buf = {
let desc = BufferInitDescriptor {
label: None,
contents: data.as_slice(),
contents,
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
};

state.device().create_buffer_init(&desc)
};

Self {
buf: Arc::new(buf),
ty: PhantomData,
buf,
vert: PhantomData,
}
}

Expand All @@ -43,7 +42,7 @@ impl<V> Uniform<V> {
{
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 {
Expand All @@ -54,16 +53,18 @@ impl<V> Uniform<V> {
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 {}

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.])
}
}
Expand All @@ -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<f32>;
type Data = Data;

fn value(self) -> Data {
fn value(self) -> Self::Data {
let [x, y] = self;
Data([x, y, 0., 0.])
}
Expand All @@ -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<f32>;
type Data = Data;

fn value(self) -> Data {
fn value(self) -> Self::Data {
let [x, y, z] = self;
Data([x, y, z, 0.])
}
Expand All @@ -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<f32>;
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)
}
}

Expand All @@ -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<f32>;
type Data = Data;

fn value(self) -> Data {
fn value(self) -> Self::Data {
self.to_array().value()
}
}
Expand All @@ -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<f32>;
type Data = Data;

fn value(self) -> Data {
fn value(self) -> Self::Data {
self.to_array().value()
}
}
Expand All @@ -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<f32>;
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<const N: usize = 4>([f32; N]);

impl<const N: usize> AsRef<[u8]> for Data<N> {
fn as_ref(&self) -> &[u8] {
bytemuck::cast_slice(&self.0)
}
}
Expand Down
14 changes: 14 additions & 0 deletions dunge_tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions dunge_tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit d90460b

Please sign in to comment.