From ed3b61a9312f6cc1a71857e61e4c62f8ed18b71e Mon Sep 17 00:00:00 2001 From: chyyran Date: Sun, 15 Jan 2023 12:16:57 -0500 Subject: [PATCH] uniforms: make bounds more general --- librashader-runtime/src/uniforms.rs | 110 +++++++++++++++------------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/librashader-runtime/src/uniforms.rs b/librashader-runtime/src/uniforms.rs index c664226..6c2802d 100644 --- a/librashader-runtime/src/uniforms.rs +++ b/librashader-runtime/src/uniforms.rs @@ -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> { _c: PhantomData, } -impl UniformStorage -where - H: BindUniform, - H: BindUniform, - H: BindUniform, - H: for<'a> BindUniform, - H: for<'a> BindUniform, -{ +impl UniformStorage { /// 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::()], - 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::()], - value, - ctx, - ); - } - /// Bind a scalar to the given offset. + #[inline(always)] pub fn bind_scalar(&mut self, offset: MemberOffset, value: T, ctx: C) where H: BindUniform, @@ -161,3 +112,58 @@ where ) } } + +impl UniformStorage +where + H: for<'a> BindUniform, +{ + #[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::()], + value, + ctx, + ); + } +} + +impl UniformStorage +where + H: for<'a> BindUniform, +{ + #[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::()], + value, + ctx, + ); + } +}