From d8c87984d54aca5407580e0d33c30e3dcb6af313 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Fri, 11 Jan 2019 17:30:13 +0000 Subject: [PATCH] Switch back to old gfx backend --- voxygen/Cargo.toml | 13 +- voxygen/src/main.rs | 9 +- voxygen/src/menu/title.rs | 3 +- voxygen/src/render/mod.rs | 19 +- voxygen/src/render/pipelines/character.rs | 39 ++++ voxygen/src/render/pipelines/mod.rs | 1 + voxygen/src/render/renderer.rs | 209 +++------------------- voxygen/src/window.rs | 54 ++++-- 8 files changed, 125 insertions(+), 222 deletions(-) create mode 100644 voxygen/src/render/pipelines/character.rs create mode 100644 voxygen/src/render/pipelines/mod.rs diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index a31f5ee603..4b58ce03ff 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -5,19 +5,18 @@ authors = ["Joshua Barretto "] edition = "2018" [features] -empty = ["rendy/empty"] -dx12 = ["rendy/dx12"] -metal = ["rendy/metal"] -vulkan = ["rendy/vulkan"] +gl = ["gfx_device_gl"] -default = ["empty"] +default = [] [dependencies] common = { path = "../common" } # Graphics -winit = "0.18" -rendy = { git = "https://github.com/omni-viral/rendy" } +gfx = "0.17" +gfx_device_gl = { version = "0.15", optional = true } +gfx_window_glutin = "0.28" +glutin = "0.19" # Mathematics vek = "0.9" diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 209bfbe69e..60ae4fe4d3 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -3,10 +3,13 @@ mod render; mod window; // Standard -use std::mem; +use std::{ + any, + mem, +}; // Library -use winit; +use glutin; use failure; // Crate @@ -17,7 +20,7 @@ use crate::{ #[derive(Debug)] pub enum VoxygenErr { - WinitCreationErr(winit::CreationError), + BackendErr(Box), Other(failure::Error), } diff --git a/voxygen/src/menu/title.rs b/voxygen/src/menu/title.rs index ac4eb506ec..adbe185975 100644 --- a/voxygen/src/menu/title.rs +++ b/voxygen/src/menu/title.rs @@ -34,7 +34,8 @@ impl PlayState for TitleState { 1.0, )); global_state.window.renderer_mut().flush(); - global_state.window.display(); + global_state.window.display() + .expect("Failed to display window"); } } } diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index db1f83f747..0e4c0badf1 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -1,6 +1,7 @@ mod mesh; mod model; mod renderer; +mod pipelines; mod shader_set; // Reexports @@ -8,23 +9,11 @@ pub use self::{ mesh::Mesh, model::Model, shader_set::ShaderSet, - renderer::Renderer, + renderer::{Renderer, TgtColorFmt, TgtDepthFmt}, }; -// Library -use rendy; - -#[cfg(not(any(feature = "dx12", feature = "metal", feature = "vulkan")))] -type Backend = rendy::empty::Backend; - -#[cfg(feature = "dx12")] -type Backend = rendy::dx12::Backend; - -#[cfg(feature = "metal")] -type Backend = rendy::metal::Backend; - -#[cfg(feature = "vulkan")] -type Backend = rendy::vulkan::Backend; +#[cfg(feature = "gl")] +use gfx_device_gl as gfx_backend; /// Used to represent one of many possible errors that may be omitted by the rendering code #[derive(Debug)] diff --git a/voxygen/src/render/pipelines/character.rs b/voxygen/src/render/pipelines/character.rs new file mode 100644 index 0000000000..72aa4d19a4 --- /dev/null +++ b/voxygen/src/render/pipelines/character.rs @@ -0,0 +1,39 @@ +// Library +use gfx::{ + self, + VertexBuffer, + ConstantBuffer, + RenderTarget, + DepthTarget, + preset::depth::LESS_EQUAL_WRITE, + // Macros + gfx_defines, + gfx_vertex_struct_meta, + gfx_constant_struct_meta, + gfx_impl_struct_meta, + gfx_pipeline, + gfx_pipeline_inner, +}; + +// Local +use super::super::{ + renderer::{TgtColorFmt, TgtDepthFmt}, +}; + +gfx_defines! { + vertex Vertex { + pos: [f32; 3] = "v_pos", + bone: u8 = "v_bone", + } + + constant Locals { + model: [[f32; 4]; 4] = "u_model", + } + + pipeline pipe { + vbuf: VertexBuffer = (), + locals: ConstantBuffer = "locals", + tgt_color: RenderTarget = "tgt", + tgt_depth: DepthTarget = LESS_EQUAL_WRITE, + } +} diff --git a/voxygen/src/render/pipelines/mod.rs b/voxygen/src/render/pipelines/mod.rs new file mode 100644 index 0000000000..771e7198e1 --- /dev/null +++ b/voxygen/src/render/pipelines/mod.rs @@ -0,0 +1 @@ +mod character; diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index 9c8b6cade5..d3da83f0c3 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -1,24 +1,8 @@ // Library use vek::*; -use winit; -use lazy_static::lazy_static; -use rendy::{ - hal, - command::RenderPassInlineEncoder, - wsi::Surface, - graph::{ - Graph, - GraphBuilder, - NodeBuffer, - NodeImage, - present::PresentNode, - render::RenderPass, - }, - factory::{Config, Factory}, - memory::MemoryUsageValue, - resource::buffer::Buffer, - mesh::AsVertex, - shader::{Shader, ShaderKind, SourceLanguage, StaticShaderInfo}, +use gfx::{ + self, + traits::Device, }; // Crate @@ -31,188 +15,47 @@ use super::{ shader_set::ShaderSet, Pipeline, RenderErr, - Backend, + gfx_backend, }; -lazy_static! { - static ref VS: StaticShaderInfo = StaticShaderInfo::new( - concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/test/shader.vert"), - ShaderKind::Vertex, - SourceLanguage::GLSL, - "main", - ); +pub type TgtColorFmt = gfx::format::Srgba8; +pub type TgtDepthFmt = gfx::format::DepthStencil; - static ref FS: StaticShaderInfo = StaticShaderInfo::new( - concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/test/shader.frag"), - ShaderKind::Fragment, - SourceLanguage::GLSL, - "main", - ); -} - -#[derive(Debug)] -struct TriangleRenderPass { - vertex: Option>, -} - -impl RenderPass for TriangleRenderPass { - fn name() -> &'static str { "triangle" } - - fn vertices() -> Vec<( - Vec>, - hal::pso::ElemStride, - )> { vec![rendy::mesh::PosColor::VERTEX.gfx_vertex_input_desc()] } - - fn load_shader_sets<'a>( - storage: &'a mut Vec, - factory: &mut Factory, - _aux: &mut T, - ) -> Vec> { - storage.clear(); - storage.push(VS.module(factory).unwrap()); - storage.push(FS.module(factory).unwrap()); - - vec![hal::pso::GraphicsShaderSet { - vertex: hal::pso::EntryPoint { - entry: "main", - module: &storage[0], - specialization: hal::pso::Specialization::default(), - }, - fragment: Some(hal::pso::EntryPoint { - entry: "main", - module: &storage[1], - specialization: hal::pso::Specialization::default(), - }), - hull: None, - domain: None, - geometry: None, - }] - } - - fn build<'a>( - _factory: &mut Factory, - _aux: &mut T, - buffers: &mut [NodeBuffer<'a, B>], - images: &mut [NodeImage<'a, B>], - sets: &[impl AsRef<[B::DescriptorSetLayout]>], - ) -> Self { - assert!(buffers.is_empty()); - assert!(images.is_empty()); - assert_eq!(sets.len(), 1); - assert!(sets[0].as_ref().is_empty()); - - Self { - vertex: None, - } - } - - fn prepare(&mut self, factory: &mut Factory, _aux: &T) -> bool { - if self.vertex.is_some() { - return false; - } else { - let mut vbuf = factory.create_buffer( - 512, - rendy::mesh::PosColor::VERTEX.stride as u64 * 3, - (hal::buffer::Usage::VERTEX, MemoryUsageValue::Dynamic) - ).unwrap(); - - unsafe { - factory.upload_visible_buffer(&mut vbuf, 0, &[ - rendy::mesh::PosColor { - position: [0.0, -1.0, 0.0].into(), - color: [1.0, 0.0, 0.0, 1.0].into(), - }, - rendy::mesh::PosColor { - position: [1.0, 1.0, 0.0].into(), - color: [0.0, 1.0, 0.0, 1.0].into(), - }, - rendy::mesh::PosColor { - position: [-1.0, 1.0, 0.0].into(), - color: [0.0, 0.0, 1.0, 1.0].into(), - }, - ]).unwrap(); - } - - self.vertex = Some(vbuf); - - true - } - } - - fn draw( - &mut self, - _layouts: &[B::PipelineLayout], - pipelines: &[B::GraphicsPipeline], - mut encoder: RenderPassInlineEncoder<'_, B>, - _index: usize, - _aux: &T, - ) { - let vbuf = self.vertex.as_ref().unwrap(); - encoder.bind_graphics_pipeline(&pipelines[0]); - encoder.bind_vertex_buffers(0, Some((vbuf.raw(), 0))); - encoder.draw(0..3, 0..1); - } - - fn dispose(self, _factory: &mut Factory, _aux: &mut T) {} -} +pub type TgtColorView = gfx::handle::RenderTargetView; +pub type TgtDepthView = gfx::handle::DepthStencilView; pub struct Renderer { - //surface: Surface, - graph: Graph, - factory: Factory, + device: gfx_backend::Device, + encoder: gfx::Encoder, + factory: gfx_backend::Factory, + + tgt_color_view: TgtColorView, + tgt_depth_view: TgtDepthView, } impl Renderer { pub fn new( - window: winit::Window, + device: gfx_backend::Device, + mut factory: gfx_backend::Factory, + tgt_color_view: TgtColorView, + tgt_depth_view: TgtDepthView, ) -> Result { - let config: Config = Config::default(); - - let mut factory = Factory::::new(config) - .map_err(|err| VoxygenErr::Other(err))?; - - let surface = factory.create_surface(window); - - let mut graph_builder = GraphBuilder::::new(); - - let color_img = graph_builder.create_image( - surface.kind(), - 1, - hal::format::Format::Rgba8Unorm, - MemoryUsageValue::Data, - Some(hal::command::ClearValue::Color([1.0; 4].into())), - ); - - graph_builder.add_node( - TriangleRenderPass::builder() - .with_image(color_img) - ); - - graph_builder.add_node( - PresentNode::builder(surface) - .with_image(color_img) - ); - - let graph = graph_builder.build(&mut factory, &mut ()) - .map_err(|err| VoxygenErr::Other(err))?; - Ok(Self { - graph, + device, + encoder: factory.create_command_buffer().into(), factory, + tgt_color_view, + tgt_depth_view, }) } pub fn clear(&mut self, col: Rgba) { + self.encoder.clear(&self.tgt_color_view, col.into_array()); + self.encoder.clear_depth(&self.tgt_depth_view, 1.0); } pub fn flush(&mut self) { - self.graph.run(&mut self.factory, &mut ()); - } -} - -impl Drop for Renderer { - fn drop(&mut self) { - //self.graph.dispose(&mut self.factory, &mut ()); - //self.factory.dispose(); + self.encoder.flush(&mut self.device); + self.device.cleanup(); } } diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index fcc36efaab..4698be36c0 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -1,32 +1,59 @@ // Library -use winit; +use glutin; +use gfx_window_glutin; // Crate use crate::{ VoxygenErr, - render::Renderer, + render::{ + Renderer, + TgtColorFmt, + TgtDepthFmt, + }, }; pub struct Window { - events_loop: winit::EventsLoop, + events_loop: glutin::EventsLoop, + window: glutin::GlWindow, renderer: Renderer, } impl Window { pub fn new() -> Result { - let events_loop = winit::EventsLoop::new(); + let events_loop = glutin::EventsLoop::new(); - let window = winit::WindowBuilder::new() + let win_builder = glutin::WindowBuilder::new() .with_title("Veloren (Voxygen)") - .with_dimensions(winit::dpi::LogicalSize::new(800.0, 500.0)) + .with_dimensions(glutin::dpi::LogicalSize::new(800.0, 500.0)) .with_maximized(false) - .build(&events_loop) - .map_err(|err| VoxygenErr::WinitCreationErr(err))?; + ; + + let ctx_builder = glutin::ContextBuilder::new() + .with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 2))) + .with_vsync(true); + + let ( + window, + device, + factory, + tgt_color_view, + tgt_depth_view, + ) = gfx_window_glutin::init::( + win_builder, + ctx_builder, + &events_loop, + ).map_err(|err| VoxygenErr::BackendErr(Box::new(err)))?; let tmp = Ok(Self { events_loop, - renderer: Renderer::new(window)?, + window, + renderer: Renderer::new( + device, + factory, + tgt_color_view, + tgt_depth_view, + )?, }); tmp } @@ -37,8 +64,8 @@ impl Window { pub fn fetch_events(&mut self) -> Vec { let mut events = vec![]; self.events_loop.poll_events(|event| match event { - winit::Event::WindowEvent { event, .. } => match event { - winit::WindowEvent::CloseRequested => events.push(Event::Close), + glutin::Event::WindowEvent { event, .. } => match event { + glutin::WindowEvent::CloseRequested => events.push(Event::Close), _ => {}, }, _ => {}, @@ -46,8 +73,9 @@ impl Window { events } - pub fn display(&self) { - // TODO + pub fn display(&self) -> Result<(), VoxygenErr> { + self.window.swap_buffers() + .map_err(|err| VoxygenErr::BackendErr(Box::new(err))) } }