2019-08-19 17:23:47 +00:00
|
|
|
use super::{gfx_backend, RenderError};
|
|
|
|
use gfx::{
|
|
|
|
self,
|
|
|
|
buffer::Role,
|
|
|
|
memory::{Bind, Usage},
|
|
|
|
Factory,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Represents a mesh that has been sent to the GPU.
|
|
|
|
pub struct Instances<T: Copy + gfx::traits::Pod> {
|
|
|
|
pub ibuf: gfx::handle::Buffer<gfx_backend::Resources, T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy + gfx::traits::Pod> Instances<T> {
|
|
|
|
pub fn new(factory: &mut gfx_backend::Factory, len: usize) -> Result<Self, RenderError> {
|
|
|
|
Ok(Self {
|
|
|
|
ibuf: factory
|
2019-08-19 20:09:35 +00:00
|
|
|
.create_buffer(len, Role::Vertex, Usage::Dynamic, Bind::TRANSFER_DST)
|
2020-06-11 18:44:03 +00:00
|
|
|
.map_err(RenderError::BufferCreationError)?,
|
2019-08-19 17:23:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn count(&self) -> usize { self.ibuf.len() }
|
2019-08-19 21:54:16 +00:00
|
|
|
|
2019-08-19 17:23:47 +00:00
|
|
|
pub fn update(
|
|
|
|
&mut self,
|
|
|
|
encoder: &mut gfx::Encoder<gfx_backend::Resources, gfx_backend::CommandBuffer>,
|
|
|
|
instances: &[T],
|
|
|
|
) -> Result<(), RenderError> {
|
|
|
|
encoder
|
|
|
|
.update_buffer(&self.ibuf, instances, 0)
|
2020-06-11 18:44:03 +00:00
|
|
|
.map_err(RenderError::UpdateError)
|
2019-08-19 17:23:47 +00:00
|
|
|
}
|
|
|
|
}
|