Switch back to old gfx backend

This commit is contained in:
Joshua Barretto 2019-01-11 17:30:13 +00:00
parent 5d6ec363a5
commit d8c87984d5
8 changed files with 125 additions and 222 deletions

View File

@ -5,19 +5,18 @@ authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"]
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"

View File

@ -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<any::Any>),
Other(failure::Error),
}

View File

@ -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");
}
}
}

View File

@ -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)]

View File

@ -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<Vertex> = (),
locals: ConstantBuffer<Locals> = "locals",
tgt_color: RenderTarget<TgtColorFmt> = "tgt",
tgt_depth: DepthTarget<TgtDepthFmt> = LESS_EQUAL_WRITE,
}
}

View File

@ -0,0 +1 @@
mod character;

View File

@ -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<B: hal::Backend> {
vertex: Option<Buffer<B>>,
}
impl<B: hal::Backend, T: ?Sized> RenderPass<B, T> for TriangleRenderPass<B> {
fn name() -> &'static str { "triangle" }
fn vertices() -> Vec<(
Vec<hal::pso::Element<hal::format::Format>>,
hal::pso::ElemStride,
)> { vec![rendy::mesh::PosColor::VERTEX.gfx_vertex_input_desc()] }
fn load_shader_sets<'a>(
storage: &'a mut Vec<B::ShaderModule>,
factory: &mut Factory<B>,
_aux: &mut T,
) -> Vec<hal::pso::GraphicsShaderSet<'a, B>> {
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<B>,
_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<B>, _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<B>, _aux: &mut T) {}
}
pub type TgtColorView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtColorFmt>;
pub type TgtDepthView = gfx::handle::DepthStencilView<gfx_backend::Resources, TgtDepthFmt>;
pub struct Renderer {
//surface: Surface<Backend>,
graph: Graph<Backend, ()>,
factory: Factory<Backend>,
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,
}
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<Self, VoxygenErr> {
let config: Config = Config::default();
let mut factory = Factory::<Backend>::new(config)
.map_err(|err| VoxygenErr::Other(err))?;
let surface = factory.create_surface(window);
let mut graph_builder = GraphBuilder::<Backend, ()>::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<f32>) {
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();
}
}

View File

@ -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<Window, VoxygenErr> {
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::<TgtColorFmt, TgtDepthFmt>(
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<Event> {
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)))
}
}