From 432e8285175a7eef896b0b6b4f3b166c775e82a2 Mon Sep 17 00:00:00 2001 From: Imbris Date: Wed, 25 Sep 2019 23:19:45 -0400 Subject: [PATCH] Add MSAAx4 --- assets/voxygen/shaders/postprocess-frag.glsl | 11 +- voxygen/src/render/error.rs | 94 +++++++++++++++ voxygen/src/render/mod.rs | 15 +-- voxygen/src/render/renderer.rs | 120 ++++++++++--------- 4 files changed, 167 insertions(+), 73 deletions(-) create mode 100644 voxygen/src/render/error.rs diff --git a/assets/voxygen/shaders/postprocess-frag.glsl b/assets/voxygen/shaders/postprocess-frag.glsl index 1a5ba3113a..fe1aa3cf81 100644 --- a/assets/voxygen/shaders/postprocess-frag.glsl +++ b/assets/voxygen/shaders/postprocess-frag.glsl @@ -2,7 +2,7 @@ #include -uniform sampler2D src_color; +uniform sampler2DMS src_color; in vec2 f_pos; @@ -169,8 +169,15 @@ void main() { uv = clamp(uv + vec2(sin(uv.y * 16.0 + tick.x), sin(uv.x * 24.0 + tick.x)) * 0.005, 0, 1); } - vec4 fxaa_color = fxaa_apply(src_color, uv * screen_res.xy * FXAA_SCALE, screen_res.xy * FXAA_SCALE); + + //vec4 fxaa_color = fxaa_apply(src_color, uv * screen_res.xy * FXAA_SCALE, screen_res.xy * FXAA_SCALE); //vec4 fxaa_color = texture(src_color, uv); + ivec2 uv_int = ivec2(uv.x * screen_res.x, uv.y * screen_res.y); + vec4 sample1 = texelFetch(src_color, uv_int, 0); + vec4 sample2 = texelFetch(src_color, uv_int, 1); + vec4 sample3 = texelFetch(src_color, uv_int, 2); + vec4 sample4 = texelFetch(src_color, uv_int, 3); + vec4 fxaa_color = (sample1 + sample2 + sample3 + sample4) / 4.0; vec4 hsva_color = vec4(rgb2hsv(fxaa_color.rgb), fxaa_color.a); hsva_color.y *= 1.45; diff --git a/voxygen/src/render/error.rs b/voxygen/src/render/error.rs new file mode 100644 index 0000000000..a7a38d784d --- /dev/null +++ b/voxygen/src/render/error.rs @@ -0,0 +1,94 @@ +/// Used to represent one of many possible errors that may be omitted by the rendering subsystem. +#[derive(Debug)] +pub enum RenderError { + PipelineError(gfx::PipelineStateError), + UpdateError(gfx::UpdateError), + TexUpdateError(gfx::UpdateError<[u16; 3]>), + CombinedError(gfx::CombinedError), + BufferCreationError(gfx::buffer::CreationError), + IncludeError(glsl_include::Error), + MappingError(gfx::mapping::Error), + CopyError(gfx::CopyError<[u16; 3], usize>), +} + +impl From> for RenderError { + fn from(err: gfx::PipelineStateError) -> Self { + Self::PipelineError(err) + } +} + +impl From> for RenderError { + fn from(err: gfx::PipelineStateError<&str>) -> Self { + match err { + gfx::PipelineStateError::DescriptorInit(err) => { + gfx::PipelineStateError::DescriptorInit(err.into()) + } + err => err, + } + .into() + } +} +impl From for RenderError { + fn from(err: gfx::shade::ProgramError) -> Self { + gfx::PipelineStateError::::Program(err).into() + } +} +impl From> for RenderError { + fn from(err: gfx::UpdateError) -> Self { + Self::UpdateError(err) + } +} + +impl From> for RenderError { + fn from(err: gfx::UpdateError<[u16; 3]>) -> Self { + Self::TexUpdateError(err) + } +} + +impl From for RenderError { + fn from(err: gfx::CombinedError) -> Self { + Self::CombinedError(err) + } +} + +impl From for RenderError { + fn from(err: gfx::TargetViewError) -> Self { + Self::CombinedError(err.into()) + } +} + +impl From for RenderError { + fn from(err: gfx::ResourceViewError) -> Self { + Self::CombinedError(err.into()) + } +} + +impl From for RenderError { + fn from(err: gfx::texture::CreationError) -> Self { + Self::CombinedError(err.into()) + } +} + +impl From for RenderError { + fn from(err: gfx::buffer::CreationError) -> Self { + Self::BufferCreationError(err) + } +} + +impl From for RenderError { + fn from(err: glsl_include::Error) -> Self { + Self::IncludeError(err) + } +} + +impl From for RenderError { + fn from(err: gfx::mapping::Error) -> Self { + Self::MappingError(err) + } +} + +impl From> for RenderError { + fn from(err: gfx::CopyError<[u16; 3], usize>) -> Self { + Self::CopyError(err) + } +} diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index b7cd14b426..182e604b6d 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -1,4 +1,5 @@ pub mod consts; +mod error; pub mod instances; pub mod mesh; pub mod model; @@ -10,6 +11,7 @@ mod util; // Reexports pub use self::{ consts::Consts, + error::RenderError, instances::Instances, mesh::{Mesh, Quad, Tri}, model::{DynamicModel, Model}, @@ -37,19 +39,6 @@ use gfx_device_gl as gfx_backend; use gfx; -/// Used to represent one of many possible errors that may be omitted by the rendering subsystem. -#[derive(Debug)] -pub enum RenderError { - PipelineError(gfx::PipelineStateError), - UpdateError(gfx::UpdateError), - TexUpdateError(gfx::UpdateError<[u16; 3]>), - CombinedError(gfx::CombinedError), - BufferCreationError(gfx::buffer::CreationError), - IncludeError(glsl_include::Error), - MappingError(gfx::mapping::Error), - CopyError(gfx::CopyError<[u16; 3], usize>), -} - /// Used to represent a specific rendering configuration. /// /// Note that pipelines are tied to the diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index 25eb0a4bc5..33e48acafd 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -169,12 +169,35 @@ impl Renderer { factory: &mut gfx_device_gl::Factory, size: (u16, u16), ) -> Result<(TgtColorView, TgtDepthView, TgtColorRes), RenderError> { - let (_, tgt_color_res, tgt_color_view) = factory - .create_render_target::(size.0, size.1) - .map_err(RenderError::CombinedError)?;; - let tgt_depth_view = factory - .create_depth_stencil_view_only::(size.0, size.1) - .map_err(RenderError::CombinedError)?;; + let kind = gfx::texture::Kind::D2(size.0, size.1, gfx::texture::AaMode::Multi(4)); + let levels = 1; + + let color_cty = <::Channel as gfx::format::ChannelTyped + >::get_channel_type(); + let tgt_color_tex = factory.create_texture( + kind, + levels, + gfx::memory::Bind::SHADER_RESOURCE | gfx::memory::Bind::RENDER_TARGET, + gfx::memory::Usage::Data, + Some(color_cty), + )?; + let tgt_color_res = factory.view_texture_as_shader_resource::( + &tgt_color_tex, + (0, levels - 1), + gfx::format::Swizzle::new(), + )?; + let tgt_color_view = factory.view_texture_as_render_target(&tgt_color_tex, 0, None)?; + + let depth_cty = <::Channel as gfx::format::ChannelTyped>::get_channel_type(); + let tgt_depth_tex = factory.create_texture( + kind, + levels, + gfx::memory::Bind::DEPTH_STENCIL, + gfx::memory::Usage::Data, + Some(depth_cty), + )?; + let tgt_depth_view = factory.view_texture_as_depth_stencil_trivial(&tgt_depth_tex)?; + Ok((tgt_color_view, tgt_depth_view, tgt_color_res)) } @@ -319,33 +342,29 @@ impl Renderer { type WinSurfaceData = <::Surface as SurfaceTyped>::DataType; let download = self .factory - .create_download_buffer::(width as usize * height as usize) - .map_err(|err| RenderError::BufferCreationError(err))?; - self.encoder - .copy_texture_to_buffer_raw( - self.win_color_view.raw().get_texture(), - None, - gfx::texture::RawImageInfo { - xoffset: 0, - yoffset: 0, - zoffset: 0, - width, - height, - depth: 0, - format: WinColorFmt::get_format(), - mipmap: 0, - }, - download.raw(), - 0, - ) - .map_err(|err| RenderError::CopyError(err))?; + .create_download_buffer::(width as usize * height as usize)?; + self.encoder.copy_texture_to_buffer_raw( + self.win_color_view.raw().get_texture(), + None, + gfx::texture::RawImageInfo { + xoffset: 0, + yoffset: 0, + zoffset: 0, + width, + height, + depth: 0, + format: WinColorFmt::get_format(), + mipmap: 0, + }, + download.raw(), + 0, + )?; self.flush(); // Assumes that the format is Rgba8. let raw_data = self .factory - .read_mapping(&download) - .map_err(|err| RenderError::MappingError(err))? + .read_mapping(&download)? .chunks_exact(width as usize) .rev() .flatten() @@ -722,38 +741,23 @@ fn create_pipeline<'a, P: gfx::pso::PipelineInit>( ctx: &IncludeContext, cull_face: gfx::state::CullFace, ) -> Result, RenderError> { - let vs = ctx.expand(vs).map_err(RenderError::IncludeError)?; - let fs = ctx.expand(fs).map_err(RenderError::IncludeError)?; + let vs = ctx.expand(vs)?; + let fs = ctx.expand(fs)?; - let program = factory - .link_program(vs.as_bytes(), fs.as_bytes()) - .map_err(|err| RenderError::PipelineError(gfx::PipelineStateError::Program(err)))?; + let program = factory.link_program(vs.as_bytes(), fs.as_bytes())?; Ok(GfxPipeline { - pso: factory - .create_pipeline_from_program( - &program, - gfx::Primitive::TriangleList, - gfx::state::Rasterizer { - front_face: gfx::state::FrontFace::CounterClockwise, - cull_face, - 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 { - 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) - } - }) - })?, + pso: factory.create_pipeline_from_program( + &program, + gfx::Primitive::TriangleList, + gfx::state::Rasterizer { + front_face: gfx::state::FrontFace::CounterClockwise, + cull_face, + 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. }) }