mod private
{
use crate::*;
#[ derive( Clone ) ]
pub struct VertexBufferLayout
{
array_stride : Option< f64 >,
attributes : Vec< web_sys::GpuVertexAttribute >,
step_mode : GpuVertexStepMode,
compute_offsets : bool,
}
impl VertexBufferLayout
{
pub fn new() -> Self
{
let array_stride = None;
let step_mode = GpuVertexStepMode::Vertex;
let attributes = Vec::new();
let compute_offsets = false;
VertexBufferLayout
{
array_stride,
step_mode,
attributes,
compute_offsets
}
}
pub fn stride< T >( mut self ) -> Self
{
self.array_stride = Some( std::mem::size_of::< T >() as f64 );
self
}
pub fn stride_from_value( mut self, stride : f64 ) -> Self
{
self.array_stride = Some( stride );
self
}
pub fn vertex( mut self ) -> Self
{
self.step_mode = GpuVertexStepMode::Vertex;
self
}
pub fn instance( mut self) -> Self
{
self.step_mode = GpuVertexStepMode::Instance;
self
}
pub fn attribute( mut self, attribute : impl Into< web_sys::GpuVertexAttribute > ) -> Self
{
self.attributes.push( attribute.into() );
self
}
pub fn compute_offsets( mut self ) -> Self
{
self.compute_offsets = true;
self
}
}
impl From< VertexBufferLayout > for web_sys::GpuVertexBufferLayout
{
fn from( mut value: VertexBufferLayout ) -> Self {
let mut offset : f64 = 0.0;
for a in value.attributes.iter_mut()
{
let a_offset = a.get_offset();
offset = offset.max( a_offset );
if value.compute_offsets
{
a.set_offset( offset );
}
let size = layout::vertex_attribute::format_to_size( a.get_format() ) as f64;
offset += size;
}
if value.array_stride.is_none() { value.array_stride = Some( offset ); }
let layout = web_sys::GpuVertexBufferLayout::new
(
value.array_stride.unwrap(),
&value.attributes.into()
);
layout.set_step_mode( value.step_mode );
layout
}
}
}
crate::mod_interface!
{
exposed use
{
VertexBufferLayout
};
}