Skip to content

Commit

Permalink
Add IntoValue trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 21, 2024
1 parent 57aa536 commit e6747a7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
7 changes: 4 additions & 3 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
sl::IntoModule,
state::State,
texture::{self, CopyBuffer, CopyBufferView, Filter, Make, MapResult, Mapped, Sampler},
uniform::{Uniform, Value},
uniform::{IntoValue, Uniform, Value},
Vertex,
},
std::{error, fmt, future::IntoFuture, sync::Arc},
Expand Down Expand Up @@ -37,10 +37,11 @@ impl Context {
Binder::new(&self.0, shader)
}

pub fn make_uniform<U>(&self, val: U) -> Uniform<U>
pub fn make_uniform<U>(&self, val: U) -> Uniform<U::Value>
where
U: Value,
U: IntoValue,
{
let val = val.into_value();
Uniform::new(&self.0, val.value().as_ref())
}

Expand Down
79 changes: 72 additions & 7 deletions dunge/src/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ impl<U> Uniform<U> {
}
}

pub fn update(&self, cx: &Context, val: U)
pub fn update<V>(&self, cx: &Context, val: V)
where
V: IntoValue<Value = U>,
U: Value,
{
let queue = cx.state().queue();
let data = val.value();
queue.write_buffer(&self.buf, 0, data.as_ref());
let val = val.into_value();
queue.write_buffer(&self.buf, 0, val.value().as_ref());
}

pub(crate) fn buffer(&self) -> &Buffer {
Expand All @@ -57,6 +58,14 @@ pub trait Value: private::Sealed {
fn value(self) -> Self::Data;
}

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)
}
}

pub(crate) fn values_as_bytes<U>(values: &[U]) -> &[u8]
where
U: Value,
Expand Down Expand Up @@ -152,11 +161,67 @@ impl Value for [[f32; 4]; 4] {
}
}

pub struct Data<const N: usize = 4>([f32; N]);
pub trait IntoValue {
type Value: Value;
fn into_value(self) -> Self::Value;
}

impl<const N: usize> AsRef<[u8]> for Data<N> {
fn as_ref(&self) -> &[u8] {
bytemuck::cast_slice(&self.0)
impl<U> IntoValue for U
where
U: Value,
{
type Value = Self;

fn into_value(self) -> Self {
self
}
}

impl IntoValue for glam::Vec2 {
type Value = [f32; 2];

fn into_value(self) -> Self::Value {
self.to_array()
}
}

impl IntoValue for glam::Vec3 {
type Value = [f32; 3];

fn into_value(self) -> Self::Value {
self.to_array()
}
}

impl IntoValue for glam::Vec4 {
type Value = [f32; 4];

fn into_value(self) -> Self::Value {
self.to_array()
}
}

impl IntoValue for glam::Mat2 {
type Value = [[f32; 2]; 2];

fn into_value(self) -> Self::Value {
self.to_cols_array_2d()
}
}

impl IntoValue for glam::Mat3 {
type Value = [[f32; 3]; 3];

fn into_value(self) -> Self::Value {
self.to_cols_array_2d()
}
}

impl IntoValue for glam::Mat4 {
type Value = [[f32; 4]; 4];

fn into_value(self) -> Self::Value {
self.to_cols_array_2d()
}
}

Expand Down
5 changes: 2 additions & 3 deletions examples/cube/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ async fn run() -> Result<(), Error> {
let mut r = 0.;
let uniform = {
let mat = transform(r, window.size());
// TODO: `IntoValue` trait
cx.make_uniform(mat.to_cols_array_2d())
cx.make_uniform(mat)
};

let bind = {
Expand Down Expand Up @@ -128,7 +127,7 @@ async fn run() -> Result<(), Error> {

r += ctrl.delta_time().as_secs_f32();
let mat = transform(r, ctrl.size());
uniform.update(&cx, mat.to_cols_array_2d());
uniform.update(&cx, mat);
Then::Run
};

Expand Down

0 comments on commit e6747a7

Please sign in to comment.