2019-05-04 14:28:21 +00:00
|
|
|
use super::{gfx_backend, mesh::Mesh, Pipeline, RenderError};
|
|
|
|
use gfx::{
|
|
|
|
buffer::Role,
|
|
|
|
memory::{Bind, Usage},
|
|
|
|
traits::FactoryExt,
|
|
|
|
Factory,
|
|
|
|
};
|
|
|
|
use std::ops::Range;
|
2019-01-07 21:10:31 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Represents a mesh that has been sent to the GPU.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub struct Model<P: Pipeline> {
|
2019-01-11 20:14:37 +00:00
|
|
|
pub vbuf: gfx::handle::Buffer<gfx_backend::Resources, P::Vertex>,
|
2019-08-19 21:54:16 +00:00
|
|
|
pub vertex_range: Range<u32>,
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pipeline> Model<P> {
|
2019-04-29 20:37:19 +00:00
|
|
|
pub fn new(factory: &mut gfx_backend::Factory, mesh: &Mesh<P>) -> Self {
|
2019-01-07 21:10:31 +00:00
|
|
|
Self {
|
2019-01-11 20:14:37 +00:00
|
|
|
vbuf: factory.create_vertex_buffer(mesh.vertices()),
|
2019-08-19 21:54:16 +00:00
|
|
|
vertex_range: 0..mesh.vertices().len() as u32,
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-19 21:54:16 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn vertex_range(&self) -> Range<u32> { self.vertex_range.clone() }
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
2019-05-04 14:28:21 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Represents a mesh on the GPU which can be updated dynamically.
|
2019-05-04 14:28:21 +00:00
|
|
|
pub struct DynamicModel<P: Pipeline> {
|
|
|
|
pub vbuf: gfx::handle::Buffer<gfx_backend::Resources, P::Vertex>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pipeline> DynamicModel<P> {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::redundant_closure)] // TODO: Pending review in #587
|
2019-05-04 14:28:21 +00:00
|
|
|
pub fn new(factory: &mut gfx_backend::Factory, size: usize) -> Result<Self, RenderError> {
|
|
|
|
Ok(Self {
|
|
|
|
vbuf: factory
|
|
|
|
.create_buffer(size, Role::Vertex, Usage::Dynamic, Bind::empty())
|
|
|
|
.map_err(|err| RenderError::BufferCreationError(err))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Create a model with a slice of a portion of this model to send to the
|
|
|
|
/// renderer.
|
2019-05-04 14:28:21 +00:00
|
|
|
pub fn submodel(&self, range: Range<usize>) -> Model<P> {
|
|
|
|
Model {
|
|
|
|
vbuf: self.vbuf.clone(),
|
2019-08-19 21:54:16 +00:00
|
|
|
vertex_range: range.start as u32..range.end as u32,
|
2019-05-04 14:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::redundant_closure)] // TODO: Pending review in #587
|
2019-05-04 14:28:21 +00:00
|
|
|
pub fn update(
|
|
|
|
&self,
|
|
|
|
encoder: &mut gfx::Encoder<gfx_backend::Resources, gfx_backend::CommandBuffer>,
|
|
|
|
mesh: &Mesh<P>,
|
|
|
|
offset: usize,
|
|
|
|
) -> Result<(), RenderError> {
|
|
|
|
encoder
|
|
|
|
.update_buffer(&self.vbuf, mesh.vertices(), offset)
|
|
|
|
.map_err(|err| RenderError::UpdateError(err))
|
|
|
|
}
|
|
|
|
}
|