2023-01-29 17:57:09 +11:00
|
|
|
use librashader_reflect::reflect::semantics::{MemberOffset, UniformMemberBlock};
|
2022-11-30 17:38:05 +11:00
|
|
|
use std::marker::PhantomData;
|
2023-01-16 16:16:31 +11:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2022-11-29 14:56:20 +11:00
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// A scalar value that is valid as a uniform member
|
2022-11-29 14:56:20 +11:00
|
|
|
pub trait UniformScalar: Copy + bytemuck::Pod {}
|
|
|
|
impl UniformScalar for f32 {}
|
|
|
|
impl UniformScalar for i32 {}
|
|
|
|
impl UniformScalar for u32 {}
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// A trait for a binder that binds the given value and context into the uniform for a shader pass.
|
2024-03-04 18:10:08 +11:00
|
|
|
pub trait BindUniform<C, T, D> {
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Bind the given value to the shader uniforms given the input context.
|
|
|
|
///
|
2024-09-07 12:32:02 +10:00
|
|
|
/// A `BindUniform` implementation should not write to a backing buffer from a [`UniformStorage`].
|
2022-12-01 14:50:57 +11:00
|
|
|
/// If the binding is successful and no writes to a backing buffer is necessary, this function should return `Some(())`.
|
|
|
|
/// If this function returns `None`, then the value will instead be written to the backing buffer.
|
2024-03-04 18:10:08 +11:00
|
|
|
fn bind_uniform(block: UniformMemberBlock, value: T, ctx: C, device: &D) -> Option<()>;
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// A trait to access the raw pointer to a backing uniform storage.
|
2022-11-30 15:56:10 +11:00
|
|
|
pub trait UniformStorageAccess {
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Get a pointer to the backing UBO storage. This pointer must be valid for the lifetime
|
|
|
|
/// of the implementing struct.
|
2022-11-30 15:56:10 +11:00
|
|
|
fn ubo_pointer(&self) -> *const u8;
|
2022-12-01 14:50:57 +11:00
|
|
|
|
2022-12-29 16:50:48 +11:00
|
|
|
/// Get a pointer to the backing UBO storage. This pointer must be valid for the lifetime
|
|
|
|
/// of the implementing struct.
|
|
|
|
fn ubo_slice(&self) -> &[u8];
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Get a pointer to the backing Push Constant buffer storage.
|
|
|
|
/// This pointer must be valid for the lifetime of the implementing struct.
|
2022-11-30 15:56:10 +11:00
|
|
|
fn push_pointer(&self) -> *const u8;
|
2022-12-29 16:50:48 +11:00
|
|
|
|
|
|
|
/// Get a slice to the backing Push Constant buffer storage.
|
|
|
|
/// This pointer must be valid for the lifetime of the implementing struct.
|
|
|
|
fn push_slice(&self) -> &[u8];
|
2022-11-30 15:56:10 +11:00
|
|
|
}
|
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<D, T, H, U, P> UniformStorageAccess for UniformStorage<T, H, U, P, D>
|
2023-01-16 16:16:31 +11:00
|
|
|
where
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2023-01-16 16:16:31 +11:00
|
|
|
{
|
2022-11-30 15:56:10 +11:00
|
|
|
fn ubo_pointer(&self) -> *const u8 {
|
|
|
|
self.ubo.as_ptr()
|
|
|
|
}
|
|
|
|
|
2022-12-29 16:50:48 +11:00
|
|
|
fn ubo_slice(&self) -> &[u8] {
|
|
|
|
&self.ubo
|
|
|
|
}
|
|
|
|
|
2022-11-30 15:56:10 +11:00
|
|
|
fn push_pointer(&self) -> *const u8 {
|
|
|
|
self.push.as_ptr()
|
|
|
|
}
|
2022-12-29 16:50:48 +11:00
|
|
|
|
|
|
|
fn push_slice(&self) -> &[u8] {
|
|
|
|
&self.push
|
|
|
|
}
|
2022-11-30 15:56:10 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
/// A uniform binder that always returns `None`, and does not do any binding of uniforms.
|
|
|
|
/// All uniform data is thus written into the backing buffer storage.
|
|
|
|
pub struct NoUniformBinder;
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<T, D> BindUniform<Option<()>, T, D> for NoUniformBinder {
|
|
|
|
fn bind_uniform(_: UniformMemberBlock, _: T, _: Option<()>, _: &D) -> Option<()> {
|
2022-12-01 14:50:57 +11:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A helper to bind uniform variables to UBO or Push Constant Buffers.
|
2024-03-03 17:07:07 +11:00
|
|
|
pub struct UniformStorage<H = NoUniformBinder, C = Option<()>, U = Box<[u8]>, P = Box<[u8]>, D = ()>
|
2023-01-16 16:16:31 +11:00
|
|
|
where
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2023-01-16 16:16:31 +11:00
|
|
|
{
|
2023-02-06 14:24:58 +11:00
|
|
|
ubo: U,
|
|
|
|
push: P,
|
2022-11-29 14:56:20 +11:00
|
|
|
_h: PhantomData<H>,
|
2022-11-30 17:38:05 +11:00
|
|
|
_c: PhantomData<C>,
|
2024-03-03 17:07:07 +11:00
|
|
|
_d: PhantomData<D>,
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, U, P, D> UniformStorage<H, C, U, P, D>
|
2023-01-16 16:16:31 +11:00
|
|
|
where
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2023-01-16 16:16:31 +11:00
|
|
|
{
|
2023-01-16 17:09:38 +11:00
|
|
|
/// Access the backing storage for the UBO.
|
2023-02-06 14:24:58 +11:00
|
|
|
pub fn inner_ubo(&self) -> &U {
|
2023-01-16 16:16:31 +11:00
|
|
|
&self.ubo
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
2023-01-29 17:57:09 +11:00
|
|
|
|
2023-02-06 14:24:58 +11:00
|
|
|
/// Access the backing storage for the Push storage.
|
|
|
|
pub fn inner_push(&self) -> &P {
|
|
|
|
&self.push
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:57:09 +11:00
|
|
|
pub(crate) fn buffer(&mut self, ty: UniformMemberBlock) -> &mut [u8] {
|
|
|
|
match ty {
|
2023-01-29 18:30:58 +11:00
|
|
|
UniformMemberBlock::Ubo => self.ubo.deref_mut(),
|
|
|
|
UniformMemberBlock::PushConstant => self.push.deref_mut(),
|
2023-01-29 17:57:09 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 16:16:31 +11:00
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, U, P, D> UniformStorage<H, C, U, P, D>
|
2023-01-16 16:16:31 +11:00
|
|
|
where
|
2023-01-29 17:57:09 +11:00
|
|
|
C: Copy,
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2023-01-16 16:16:31 +11:00
|
|
|
{
|
2022-11-29 14:56:20 +11:00
|
|
|
#[inline(always)]
|
2023-01-29 18:30:58 +11:00
|
|
|
fn write_scalar_inner<T: UniformScalar>(buffer: &mut [u8], value: T) {
|
2023-01-29 17:23:54 +11:00
|
|
|
let buffer = bytemuck::cast_slice_mut(buffer);
|
|
|
|
buffer[0] = value;
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:16:57 +11:00
|
|
|
/// Bind a scalar to the given offset.
|
2022-12-01 14:50:57 +11:00
|
|
|
#[inline(always)]
|
2024-03-03 17:07:07 +11:00
|
|
|
pub fn bind_scalar<T: UniformScalar>(
|
|
|
|
&mut self,
|
|
|
|
offset: MemberOffset,
|
|
|
|
value: T,
|
|
|
|
ctx: C,
|
|
|
|
device: &D,
|
|
|
|
) where
|
2024-03-04 18:10:08 +11:00
|
|
|
H: BindUniform<C, T, D>,
|
2023-01-16 04:16:57 +11:00
|
|
|
{
|
2023-01-29 17:57:09 +11:00
|
|
|
for ty in UniformMemberBlock::TYPES {
|
2024-03-04 18:10:08 +11:00
|
|
|
if H::bind_uniform(ty, value, ctx, device).is_some() {
|
2023-01-29 17:57:09 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(offset) = offset.offset(ty) {
|
|
|
|
let buffer = self.buffer(ty);
|
2023-01-29 18:30:58 +11:00
|
|
|
Self::write_scalar_inner(&mut buffer[offset..][..std::mem::size_of::<T>()], value)
|
2023-01-29 17:57:09 +11:00
|
|
|
}
|
2023-01-29 17:23:54 +11:00
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
2023-01-16 16:16:31 +11:00
|
|
|
|
|
|
|
/// Create a new `UniformStorage` with the given backing storage
|
2024-03-04 18:10:08 +11:00
|
|
|
pub fn new_with_storage(ubo: U, push: P) -> UniformStorage<H, C, U, P, D> {
|
2023-02-06 14:24:58 +11:00
|
|
|
UniformStorage {
|
|
|
|
ubo,
|
|
|
|
push,
|
|
|
|
_h: Default::default(),
|
|
|
|
_c: Default::default(),
|
2024-03-04 18:10:08 +11:00
|
|
|
_d: Default::default(),
|
2023-02-06 14:24:58 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, U, D> UniformStorage<H, C, U, Box<[u8]>, D>
|
2023-02-07 13:56:30 +11:00
|
|
|
where
|
|
|
|
C: Copy,
|
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
2023-02-06 14:24:58 +11:00
|
|
|
{
|
|
|
|
/// Create a new `UniformStorage` with the given backing storage
|
2023-02-07 13:56:30 +11:00
|
|
|
pub fn new_with_ubo_storage(
|
|
|
|
storage: U,
|
|
|
|
push_size: usize,
|
2024-03-04 18:10:08 +11:00
|
|
|
) -> UniformStorage<H, C, U, Box<[u8]>, D> {
|
2023-01-16 16:16:31 +11:00
|
|
|
UniformStorage {
|
|
|
|
ubo: storage,
|
|
|
|
push: vec![0u8; push_size].into_boxed_slice(),
|
|
|
|
_h: Default::default(),
|
|
|
|
_c: Default::default(),
|
2024-03-04 18:10:08 +11:00
|
|
|
_d: Default::default(),
|
2023-01-16 16:16:31 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, D> UniformStorage<H, C, Box<[u8]>, Box<[u8]>, D> {
|
2023-01-16 16:16:31 +11:00
|
|
|
/// Create a new `UniformStorage` with the given size for UBO and Push Constant Buffer sizes.
|
2024-03-04 18:10:08 +11:00
|
|
|
pub fn new(ubo_size: usize, push_size: usize) -> UniformStorage<H, C, Box<[u8]>, Box<[u8]>, D> {
|
2023-01-16 16:16:31 +11:00
|
|
|
UniformStorage {
|
|
|
|
ubo: vec![0u8; ubo_size].into_boxed_slice(),
|
|
|
|
push: vec![0u8; push_size].into_boxed_slice(),
|
|
|
|
_h: Default::default(),
|
|
|
|
_c: Default::default(),
|
2024-03-04 18:10:08 +11:00
|
|
|
_d: Default::default(),
|
2023-01-16 16:16:31 +11:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 04:16:57 +11:00
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, U, P, D> UniformStorage<H, C, U, P, D>
|
2023-01-16 04:16:57 +11:00
|
|
|
where
|
2023-01-29 17:57:09 +11:00
|
|
|
C: Copy,
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2024-03-04 18:10:08 +11:00
|
|
|
H: for<'a> BindUniform<C, &'a [f32; 4], D>,
|
2023-01-16 04:16:57 +11:00
|
|
|
{
|
2022-12-01 14:50:57 +11:00
|
|
|
#[inline(always)]
|
2023-01-29 17:23:54 +11:00
|
|
|
fn write_vec4_inner(buffer: &mut [u8], vec4: &[f32; 4]) {
|
|
|
|
let vec4 = bytemuck::cast_slice(vec4);
|
|
|
|
buffer.copy_from_slice(vec4);
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
2022-12-01 14:50:57 +11:00
|
|
|
/// Bind a `vec4` to the given offset.
|
2023-01-16 04:16:57 +11:00
|
|
|
#[inline(always)]
|
2024-03-03 17:07:07 +11:00
|
|
|
pub fn bind_vec4(
|
|
|
|
&mut self,
|
|
|
|
offset: MemberOffset,
|
|
|
|
value: impl Into<[f32; 4]>,
|
|
|
|
ctx: C,
|
|
|
|
device: &D,
|
|
|
|
) {
|
2023-01-29 17:23:54 +11:00
|
|
|
let vec4 = value.into();
|
|
|
|
|
2023-01-29 17:57:09 +11:00
|
|
|
for ty in UniformMemberBlock::TYPES {
|
2024-03-04 18:10:08 +11:00
|
|
|
if H::bind_uniform(ty, &vec4, ctx, device).is_some() {
|
2023-01-29 17:57:09 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(offset) = offset.offset(ty) {
|
|
|
|
let buffer = self.buffer(ty);
|
|
|
|
Self::write_vec4_inner(
|
|
|
|
&mut buffer[offset..][..4 * std::mem::size_of::<f32>()],
|
|
|
|
&vec4,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
2023-01-16 04:16:57 +11:00
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
|
2024-03-04 18:10:08 +11:00
|
|
|
impl<H, C, U, P, D> UniformStorage<H, C, U, P, D>
|
2023-01-16 04:16:57 +11:00
|
|
|
where
|
2023-01-29 17:57:09 +11:00
|
|
|
C: Copy,
|
2023-02-06 14:24:58 +11:00
|
|
|
U: Deref<Target = [u8]> + DerefMut,
|
|
|
|
P: Deref<Target = [u8]> + DerefMut,
|
2024-03-04 18:10:08 +11:00
|
|
|
H: for<'a> BindUniform<C, &'a [f32; 16], D>,
|
2023-01-16 04:16:57 +11:00
|
|
|
{
|
|
|
|
#[inline(always)]
|
2023-01-29 17:23:54 +11:00
|
|
|
fn write_mat4_inner(buffer: &mut [u8], mat4: &[f32; 16]) {
|
|
|
|
let mat4 = bytemuck::cast_slice(mat4);
|
|
|
|
buffer.copy_from_slice(mat4);
|
2023-01-16 04:16:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Bind a `mat4` to the given offset.
|
|
|
|
#[inline(always)]
|
2024-03-04 18:10:08 +11:00
|
|
|
pub fn bind_mat4(&mut self, offset: MemberOffset, value: &[f32; 16], ctx: C, device: &D) {
|
2023-01-29 17:57:09 +11:00
|
|
|
for ty in UniformMemberBlock::TYPES {
|
2024-03-04 18:10:08 +11:00
|
|
|
if H::bind_uniform(ty, value, ctx, device).is_some() {
|
2023-01-29 17:57:09 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(offset) = offset.offset(ty) {
|
|
|
|
let buffer = self.buffer(ty);
|
|
|
|
Self::write_mat4_inner(
|
|
|
|
&mut buffer[offset..][..16 * std::mem::size_of::<f32>()],
|
|
|
|
value,
|
|
|
|
);
|
|
|
|
}
|
2023-01-29 17:23:54 +11:00
|
|
|
}
|
2022-11-29 14:56:20 +11:00
|
|
|
}
|
2022-11-30 17:38:05 +11:00
|
|
|
}
|