use crate::wgpu;
#[macro_export]
macro_rules! buffer {
{ @searchbinding $struct:ident ; } => {};
{ @searchbinding $struct:ident ; $first:ident $( $rest:ident )* } => {
$crate::buffer! { @trybinding $struct ; $first }
$crate::buffer! { @searchbinding $struct ; $( $rest )* }
};
{ @trybinding $struct:ident ; STORAGE } => {
impl $crate::buffer::MewBufferBinding for $struct {
fn binding_type() -> $crate::wgpu::BindingType {
$crate::wgpu::BindingType::Buffer {
ty: $crate::wgpu::BufferBindingType::Storage { read_only: true, },
has_dynamic_offset: false,
min_binding_size: None,
}
}
fn as_binding(&self) -> $crate::wgpu::BindingResource<'_> {
self.buffer.as_entire_binding()
}
}
};
{ @trybinding $struct:ident ; UNIFORM } => {
impl $crate::buffer::MewBufferBinding for $struct {
fn binding_type() -> $crate::wgpu::BindingType {
$crate::wgpu::BindingType::Buffer {
ty: $crate::wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
}
}
fn as_binding<'a>(&'a self) -> $crate::wgpu::BindingResource<'a> {
self.buffer.as_entire_binding()
}
}
};
{ @trybinding $struct:ident ; $other:ident } => {};
{ $struct:ident as $($usage:ident)|+ } => {
$crate::buffer! { $struct <u8> as $($usage)|+ }
};
{ $struct:ident <$inner:ty> as $($usage:ident)|+ } => {
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $struct {
buffer: $crate::wgpu::Buffer,
}
impl ::std::ops::Deref for $struct {
type Target = $crate::wgpu::Buffer;
fn deref(&self) -> &Self::Target {
&self.buffer
}
}
impl $crate::buffer::MewBuffer for $struct {
type Inner = $inner;
fn new(buffer: $crate::wgpu::Buffer) -> Self {
Self {
buffer,
}
}
fn buffer_desc(inner_size: u64) -> $crate::wgpu::BufferDescriptor<'static> {
$crate::wgpu::BufferDescriptor {
label: Some(concat!(stringify!($struct), "<", stringify!($inner), "> Buffer")),
size: inner_size * ::std::mem::size_of::<Self::Inner>() as u64,
usage: $( $crate::wgpu::BufferUsages::$usage )|+,
mapped_at_creation: false,
}
}
}
$crate::buffer! { @searchbinding $struct ; $( $usage )+ }
}
}
pub use buffer;
pub trait MewBuffer {
type Inner;
fn new(buffer: wgpu::Buffer) -> Self;
fn buffer_desc(inner_size: u64) -> wgpu::BufferDescriptor<'static>;
}
pub trait MewBufferBinding: MewBuffer {
fn binding_type() -> wgpu::BindingType;
fn as_binding(&self) -> wgpu::BindingResource<'_>;
}