2019-01-07 21:10:31 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
2019-01-11 17:30:13 +00:00
|
|
|
use gfx::{
|
|
|
|
self,
|
2019-01-11 20:14:37 +00:00
|
|
|
traits::{Device, FactoryExt},
|
2019-01-07 21:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Local
|
|
|
|
use super::{
|
2019-01-11 20:14:37 +00:00
|
|
|
consts::Consts,
|
2019-01-07 21:10:31 +00:00
|
|
|
model::Model,
|
|
|
|
mesh::Mesh,
|
|
|
|
Pipeline,
|
2019-01-11 23:18:34 +00:00
|
|
|
RenderError,
|
2019-01-11 17:30:13 +00:00
|
|
|
gfx_backend,
|
2019-01-11 20:14:37 +00:00
|
|
|
pipelines::{
|
|
|
|
Globals,
|
|
|
|
character,
|
|
|
|
skybox,
|
|
|
|
},
|
2019-01-07 21:10:31 +00:00
|
|
|
};
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Represents the format of the window's color target.
|
2019-01-11 17:30:13 +00:00
|
|
|
pub type TgtColorFmt = gfx::format::Srgba8;
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Represents the format of the window's depth target.
|
2019-01-11 17:30:13 +00:00
|
|
|
pub type TgtDepthFmt = gfx::format::DepthStencil;
|
2019-01-07 21:10:31 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// A handle to a window color target.
|
2019-01-11 17:30:13 +00:00
|
|
|
pub type TgtColorView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtColorFmt>;
|
2019-01-11 23:18:34 +00:00
|
|
|
/// A handle to a window depth target.
|
2019-01-11 17:30:13 +00:00
|
|
|
pub type TgtDepthView = gfx::handle::DepthStencilView<gfx_backend::Resources, TgtDepthFmt>;
|
2019-01-07 21:10:31 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// A type that encapsulates rendering state. `Renderer` is central to Voxygen's rendering
|
|
|
|
/// subsystem and contains any state necessary to interact with the GPU, along with pipeline state
|
|
|
|
/// objects (PSOs) needed to renderer different kinds of model to the screen.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub struct Renderer {
|
2019-01-11 17:30:13 +00:00
|
|
|
device: gfx_backend::Device,
|
|
|
|
encoder: gfx::Encoder<gfx_backend::Resources, gfx_backend::CommandBuffer>,
|
|
|
|
factory: gfx_backend::Factory,
|
|
|
|
|
|
|
|
tgt_color_view: TgtColorView,
|
|
|
|
tgt_depth_view: TgtDepthView,
|
2019-01-11 20:14:37 +00:00
|
|
|
|
|
|
|
skybox_pipeline: GfxPipeline<skybox::pipe::Init<'static>>,
|
2019-01-11 23:18:34 +00:00
|
|
|
character_pipeline: GfxPipeline<character::pipe::Init<'static>>,
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Renderer {
|
2019-01-12 01:14:58 +00:00
|
|
|
/// Create a new `Renderer` from a variety of backend-specific components and the window
|
|
|
|
/// targets.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub fn new(
|
2019-01-11 17:30:13 +00:00
|
|
|
device: gfx_backend::Device,
|
|
|
|
mut factory: gfx_backend::Factory,
|
|
|
|
tgt_color_view: TgtColorView,
|
|
|
|
tgt_depth_view: TgtDepthView,
|
2019-01-11 23:18:34 +00:00
|
|
|
) -> Result<Self, RenderError> {
|
2019-01-11 20:14:37 +00:00
|
|
|
// Construct a pipeline for rendering skyboxes
|
2019-01-11 23:18:34 +00:00
|
|
|
let skybox_pipeline = create_pipeline(
|
2019-01-11 20:14:37 +00:00
|
|
|
&mut factory,
|
|
|
|
skybox::pipe::new(),
|
|
|
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/skybox.vert")),
|
|
|
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/skybox.frag")),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Construct a pipeline for rendering characters
|
2019-01-11 23:18:34 +00:00
|
|
|
let character_pipeline = create_pipeline(
|
2019-01-11 20:14:37 +00:00
|
|
|
&mut factory,
|
|
|
|
character::pipe::new(),
|
2019-01-11 23:18:34 +00:00
|
|
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/character.vert")),
|
|
|
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/character.frag")),
|
2019-01-11 20:14:37 +00:00
|
|
|
)?;
|
|
|
|
|
2019-01-07 21:10:31 +00:00
|
|
|
Ok(Self {
|
2019-01-11 17:30:13 +00:00
|
|
|
device,
|
|
|
|
encoder: factory.create_command_buffer().into(),
|
2019-01-07 21:10:31 +00:00
|
|
|
factory,
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-01-11 17:30:13 +00:00
|
|
|
tgt_color_view,
|
|
|
|
tgt_depth_view,
|
2019-01-11 20:14:37 +00:00
|
|
|
|
|
|
|
skybox_pipeline,
|
2019-01-11 23:18:34 +00:00
|
|
|
character_pipeline,
|
2019-01-07 21:10:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Queue the clearing of the color and depth targets ready for a new frame to be rendered.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub fn clear(&mut self, col: Rgba<f32>) {
|
2019-01-11 17:30:13 +00:00
|
|
|
self.encoder.clear(&self.tgt_color_view, col.into_array());
|
|
|
|
self.encoder.clear_depth(&self.tgt_depth_view, 1.0);
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Perform all queued draw calls for this frame and clean up discarded items.
|
2019-01-07 21:10:31 +00:00
|
|
|
pub fn flush(&mut self) {
|
2019-01-11 17:30:13 +00:00
|
|
|
self.encoder.flush(&mut self.device);
|
|
|
|
self.device.cleanup();
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Create a new set of constants.
|
|
|
|
pub fn create_consts<T: Copy + gfx::traits::Pod>(&mut self) -> Result<Consts<T>, RenderError> {
|
|
|
|
Ok(Consts::new(&mut self.factory))
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:14:58 +00:00
|
|
|
/// Create a new set of constants with a value.
|
|
|
|
pub fn create_consts_with<T: Copy + gfx::traits::Pod>(
|
|
|
|
&mut self,
|
|
|
|
val: T
|
|
|
|
) -> Result<Consts<T>, RenderError> {
|
2019-01-11 23:18:34 +00:00
|
|
|
let mut consts = self.create_consts()?;
|
|
|
|
consts.update(&mut self.encoder, val)?;
|
|
|
|
Ok(consts)
|
2019-01-11 20:14:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 01:14:58 +00:00
|
|
|
/// Update a set of constants with a new value.
|
|
|
|
pub fn update_consts<T: Copy + gfx::traits::Pod>(
|
|
|
|
&mut self,
|
|
|
|
consts: &mut Consts<T>,
|
|
|
|
val: T
|
|
|
|
) -> Result<(), RenderError> {
|
|
|
|
consts.update(&mut self.encoder, val)
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Create a new model from the provided mesh.
|
|
|
|
pub fn create_model<P: Pipeline>(&mut self, mesh: &Mesh<P>) -> Result<Model<P>, RenderError> {
|
2019-01-11 20:14:37 +00:00
|
|
|
Ok(Model::new(
|
|
|
|
&mut self.factory,
|
|
|
|
mesh,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Queue the rendering of the provided skybox model in the upcoming frame
|
|
|
|
pub fn render_skybox(
|
|
|
|
&mut self,
|
|
|
|
model: &Model<skybox::SkyboxPipeline>,
|
|
|
|
locals: &Consts<skybox::Locals>,
|
|
|
|
globals: &Consts<Globals>,
|
|
|
|
) {
|
|
|
|
self.encoder.draw(
|
|
|
|
&model.slice,
|
|
|
|
&self.skybox_pipeline.pso,
|
|
|
|
&skybox::pipe::Data {
|
|
|
|
vbuf: model.vbuf.clone(),
|
|
|
|
locals: locals.buf.clone(),
|
|
|
|
globals: globals.buf.clone(),
|
|
|
|
tgt_color: self.tgt_color_view.clone(),
|
|
|
|
tgt_depth: self.tgt_depth_view.clone(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
struct GfxPipeline<P: gfx::pso::PipelineInit> {
|
2019-01-11 20:14:37 +00:00
|
|
|
pso: gfx::pso::PipelineState<gfx_backend::Resources, P::Meta>,
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
|
|
|
/// Create a new pipeline from the provided vertex shader and fragment shader
|
|
|
|
fn create_pipeline<'a, P: gfx::pso::PipelineInit>(
|
|
|
|
factory: &mut gfx_backend::Factory,
|
|
|
|
pipe: P,
|
|
|
|
vs: &[u8],
|
|
|
|
fs: &[u8],
|
|
|
|
) -> Result<GfxPipeline<P>, RenderError> {
|
|
|
|
let program = factory
|
|
|
|
.link_program(vs, fs)
|
|
|
|
.map_err(|err| RenderError::PipelineError(gfx::PipelineStateError::Program(err)))?;
|
|
|
|
|
|
|
|
Ok(GfxPipeline {
|
|
|
|
pso: factory.create_pipeline_from_program(
|
|
|
|
&program,
|
|
|
|
gfx::Primitive::TriangleList,
|
|
|
|
gfx::state::Rasterizer {
|
|
|
|
front_face: gfx::state::FrontFace::CounterClockwise,
|
|
|
|
cull_face: gfx::state::CullFace::Back,
|
|
|
|
method: gfx::state::RasterMethod::Fill,
|
|
|
|
offset: None,
|
|
|
|
samples: Some(gfx::state::MultiSample),
|
|
|
|
},
|
|
|
|
pipe,
|
|
|
|
)
|
|
|
|
// Do some funky things to work around an oddity in gfx's error ownership rules
|
|
|
|
.map_err(|err| RenderError::PipelineError(match err {
|
2019-01-12 01:14:58 +00:00
|
|
|
gfx::PipelineStateError::Program(err) =>
|
|
|
|
gfx::PipelineStateError::Program(err),
|
|
|
|
gfx::PipelineStateError::DescriptorInit(err) =>
|
|
|
|
gfx::PipelineStateError::DescriptorInit(err.into()),
|
|
|
|
gfx::PipelineStateError::DeviceCreate(err) =>
|
|
|
|
gfx::PipelineStateError::DeviceCreate(err),
|
2019-01-11 23:18:34 +00:00
|
|
|
}))?,
|
|
|
|
})
|
|
|
|
}
|