From 9f7246efba4fc8289e04554f63b2c1653e1e5330 Mon Sep 17 00:00:00 2001 From: Jared Roberts Date: Thu, 15 Sep 2016 00:21:13 -0700 Subject: [PATCH] depth testing --- src/bin/gl.rs | 11 ++++++++--- src/vr.rs | 29 ++++++++++++++++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/bin/gl.rs b/src/bin/gl.rs index 1db408d..75a278d 100644 --- a/src/bin/gl.rs +++ b/src/bin/gl.rs @@ -17,7 +17,7 @@ use num_traits::identities::One; use piston_window::{PistonWindow, Window, WindowSettings}; pub type ColorFormat = gfx::format::Srgba8; -//pub type DepthFormat = gfx::format::DepthStencil; +pub type DepthFormat = gfx::format::DepthStencil; const NEAR: f32 = 0.01; const FAR: f32 = 1000.0; @@ -36,6 +36,7 @@ gfx_defines!{ vbuf: gfx::VertexBuffer = (), trans: gfx::ConstantBuffer = "b_trans", pixcolor: gfx::RenderTarget = "pixcolor", + depth: gfx::DepthTarget = gfx::preset::depth::LESS_EQUAL_WRITE, } } @@ -63,9 +64,9 @@ fn main() { pipe::new()) .expect("create pipeline"); - let (tex_left, tgt_left) = vr::create_eyebuffer(&mut window.factory, render_size) + let (tex_left, tgt_left, depth_left) = vr::create_eyebuffer(&mut window.factory, render_size) .expect("create left renderbuffer"); - let (tex_right, tgt_right) = vr::create_eyebuffer(&mut window.factory, render_size) + let (tex_right, tgt_right, depth_right) = vr::create_eyebuffer(&mut window.factory, render_size) .expect("create right renderbuffer"); let (vertex_buffer, slice) = window.factory.create_vertex_buffer_with_slice(&POLYGON, POLYGON_IDX); @@ -73,16 +74,19 @@ fn main() { vbuf: vertex_buffer.clone(), trans: window.factory.create_constant_buffer(1), pixcolor: window.output_color.clone(), + depth: window.output_stencil.clone(), }; let pipe_left = pipe::Data { vbuf: vertex_buffer.clone(), trans: window.factory.create_constant_buffer(1), pixcolor: tgt_left, + depth: depth_left, }; let pipe_right = pipe::Data { vbuf: vertex_buffer.clone(), trans: window.factory.create_constant_buffer(1), pixcolor: tgt_right, + depth: depth_right, }; window.encoder.update_constant_buffer( &pipe_monitor.trans, @@ -128,6 +132,7 @@ fn main() { .into_iter() { info!("\tpass for eye: {:?}", pass.0); window.encoder.clear(&pass.1.pixcolor, [0.1, 0.2, 0.3, 1.0]); + window.encoder.clear_depth(&pass.1.depth, 1.0); // Submit eye textures if let Some((eye, tex)) = pass.0 { diff --git a/src/vr.rs b/src/vr.rs index f24e607..1ed6dd4 100644 --- a/src/vr.rs +++ b/src/vr.rs @@ -5,10 +5,11 @@ extern crate num_traits; extern crate openvr as vr; extern crate openvr_sys; -use self::gfx::{tex, Factory, Typed}; - pub use self::vr::Eye; pub use self::vr::tracking::TrackedDeviceClass; + +use self::gfx::{tex, Factory, Typed}; +use self::gfx_device_gl::Resources as GLResources; use self::na::Inverse; use self::num_traits::identities::Zero; use self::num_traits::identities::One; @@ -16,7 +17,7 @@ use self::num_traits::identities::One; pub struct VR { system: vr::IVRSystem, compositor: vr::IVRCompositor, - gfx_handles: gfx::handle::Manager, + gfx_handles: gfx::handle::Manager, } impl VR { @@ -33,7 +34,7 @@ impl VR { self.compositor.wait_get_poses() } - pub fn submit(&mut self, eye: Eye, tex: &gfx::handle::Texture) { + pub fn submit(&mut self, eye: Eye, tex: &gfx::handle::Texture) { let tex_id = match self.gfx_handles.ref_texture(tex.raw()) { &gfx_device_gl::NewTexture::Surface(id) => id, _ => panic!("Not a surface") @@ -86,14 +87,14 @@ impl AsMatrix4 for [[N; 4]; 4] { } } -pub fn create_eyebuffer(factory: &mut gfx_device_gl::Factory, - size: vr::common::Size) - -> Result<(gfx::handle::Texture, - gfx::handle::RenderTargetView), - gfx::CombinedError> - where T: gfx::format::RenderFormat + gfx::format::TextureFormat { +pub fn create_eyebuffer(factory: &mut gfx_device_gl::Factory, + size: vr::common::Size) + -> Result<(gfx::handle::Texture, + gfx::handle::RenderTargetView, + gfx::handle::DepthStencilView), + gfx::CombinedError> + where T: gfx::format::RenderFormat + gfx::format::TextureFormat, + D: gfx::format::DepthFormat + gfx::format::TextureFormat { let tex = try!(factory.create_texture( tex::Kind::D2(size.width as tex::Size, size.height as tex::Size, tex::AaMode::Single), 1, // levels @@ -101,5 +102,7 @@ pub fn create_eyebuffer(factory: &mut gfx_device_gl::Factory, gfx::Usage::GpuOnly, // Usage Some(::get_channel_type()))); // hint: format::ChannelType? let tgt = try!(factory.view_texture_as_render_target(&tex, 0, None)); - Ok((tex, tgt)) + let depth = try!(factory.create_depth_stencil_view_only(size.width as tex::Size, + size.height as tex::Size)); + Ok((tex, tgt, depth)) }