uniforms: make bounds more general

This commit is contained in:
chyyran 2023-01-15 12:16:57 -05:00
parent 19e8da7d85
commit ed3b61a931

View file

@ -1,3 +1,4 @@
use librashader_common::Size;
use librashader_reflect::reflect::semantics::MemberOffset;
use std::marker::PhantomData;
@ -71,14 +72,7 @@ pub struct UniformStorage<H = NoUniformBinder, C = Option<()>> {
_c: PhantomData<C>,
}
impl<H, C> UniformStorage<H, C>
where
H: BindUniform<C, f32>,
H: BindUniform<C, u32>,
H: BindUniform<C, i32>,
H: for<'a> BindUniform<C, &'a [f32; 4]>,
H: for<'a> BindUniform<C, &'a [f32; 16]>,
{
impl<H, C> UniformStorage<H, C> {
/// Create a new `UniformStorage` with the given size for UBO and Push Constant Buffer sizes.
pub fn new(ubo_size: usize, push_size: usize) -> Self {
UniformStorage {
@ -100,51 +94,8 @@ where
};
}
#[inline(always)]
fn write_mat4_inner(buffer: &mut [u8], mat4: &[f32; 16], ctx: C) {
if H::bind_uniform(mat4, ctx).is_none() {
let mat4 = bytemuck::cast_slice(mat4);
buffer.copy_from_slice(mat4);
}
}
#[inline(always)]
fn write_vec4_inner(buffer: &mut [u8], vec4: impl Into<[f32; 4]>, ctx: C) {
let vec4 = vec4.into();
if H::bind_uniform(&vec4, ctx).is_none() {
let vec4 = bytemuck::cast_slice(&vec4);
buffer.copy_from_slice(vec4);
}
}
/// Bind a `mat4` to the given offset.
pub fn bind_mat4(&mut self, offset: MemberOffset, value: &[f32; 16], ctx: C) {
let (buffer, offset) = match offset {
MemberOffset::Ubo(offset) => (&mut self.ubo, offset),
MemberOffset::PushConstant(offset) => (&mut self.push, offset),
};
Self::write_mat4_inner(
&mut buffer[offset..][..16 * std::mem::size_of::<f32>()],
value,
ctx,
);
}
/// Bind a `vec4` to the given offset.
pub fn bind_vec4(&mut self, offset: MemberOffset, value: impl Into<[f32; 4]>, ctx: C) {
let (buffer, offset) = match offset {
MemberOffset::Ubo(offset) => (&mut self.ubo, offset),
MemberOffset::PushConstant(offset) => (&mut self.push, offset),
};
Self::write_vec4_inner(
&mut buffer[offset..][..4 * std::mem::size_of::<f32>()],
value,
ctx,
);
}
/// Bind a scalar to the given offset.
#[inline(always)]
pub fn bind_scalar<T: UniformScalar>(&mut self, offset: MemberOffset, value: T, ctx: C)
where
H: BindUniform<C, T>,
@ -161,3 +112,58 @@ where
)
}
}
impl<H, C> UniformStorage<H, C>
where
H: for<'a> BindUniform<C, &'a [f32; 4]>,
{
#[inline(always)]
fn write_vec4_inner(buffer: &mut [u8], vec4: impl Into<[f32; 4]>, ctx: C) {
let vec4 = vec4.into();
if H::bind_uniform(&vec4, ctx).is_none() {
let vec4 = bytemuck::cast_slice(&vec4);
buffer.copy_from_slice(vec4);
}
}
/// Bind a `vec4` to the given offset.
#[inline(always)]
pub fn bind_vec4(&mut self, offset: MemberOffset, value: impl Into<[f32; 4]>, ctx: C) {
let (buffer, offset) = match offset {
MemberOffset::Ubo(offset) => (&mut self.ubo, offset),
MemberOffset::PushConstant(offset) => (&mut self.push, offset),
};
Self::write_vec4_inner(
&mut buffer[offset..][..4 * std::mem::size_of::<f32>()],
value,
ctx,
);
}
}
impl<H, C> UniformStorage<H, C>
where
H: for<'a> BindUniform<C, &'a [f32; 16]>,
{
#[inline(always)]
fn write_mat4_inner(buffer: &mut [u8], mat4: &[f32; 16], ctx: C) {
if H::bind_uniform(mat4, ctx).is_none() {
let mat4 = bytemuck::cast_slice(mat4);
buffer.copy_from_slice(mat4);
}
}
/// Bind a `mat4` to the given offset.
#[inline(always)]
pub fn bind_mat4(&mut self, offset: MemberOffset, value: &[f32; 16], ctx: C) {
let (buffer, offset) = match offset {
MemberOffset::Ubo(offset) => (&mut self.ubo, offset),
MemberOffset::PushConstant(offset) => (&mut self.push, offset),
};
Self::write_mat4_inner(
&mut buffer[offset..][..16 * std::mem::size_of::<f32>()],
value,
ctx,
);
}
}