Added Instances construct

This commit is contained in:
Joshua Barretto 2019-08-19 18:23:47 +01:00
parent e2082088c8
commit 6f35786b84
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,32 @@
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
.create_buffer(len, Role::Vertex, Usage::Data, Bind::TRANSFER_DST)
.map_err(|err| RenderError::BufferCreationError(err))?,
})
}
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)
.map_err(|err| RenderError::UpdateError(err))
}
}

View File

@ -1,4 +1,5 @@
pub mod consts;
pub mod instances;
pub mod mesh;
pub mod model;
pub mod pipelines;

View File

@ -1,6 +1,7 @@
use super::{
consts::Consts,
gfx_backend,
instances::Instances,
mesh::Mesh,
model::{DynamicModel, Model},
pipelines::{figure, fluid, postprocess, skybox, sprite, terrain, ui, Globals, Light},
@ -435,6 +436,28 @@ impl Renderer {
);
}
/// Queue the rendering of the provided terrain chunk model in the upcoming frame.
pub fn render_sprites(
&mut self,
model: &Model<sprite::SpritePipeline>,
globals: &Consts<Globals>,
instances: &Instances<sprite::Instance>,
lights: &Consts<Light>,
) {
self.encoder.draw(
&model.slice,
&self.sprite_pipeline.pso,
&sprite::pipe::Data {
vbuf: model.vbuf.clone(),
ibuf: instances.ibuf.clone(),
globals: globals.buf.clone(),
lights: lights.buf.clone(),
tgt_color: self.tgt_color_view.clone(),
tgt_depth: self.tgt_depth_view.clone(),
},
);
}
/// Queue the rendering of the provided UI element in the upcoming frame.
pub fn render_ui_element(
&mut self,