Skip to content

Commit

Permalink
Add fxaa compute shader, improves visual quality of renders
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Oct 5, 2020
1 parent 5c09c01 commit b6873c0
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 60 deletions.
5 changes: 3 additions & 2 deletions crates/render/build.rs
Expand Up @@ -27,8 +27,9 @@ fn visit_files(dir: &Path, f: &mut dyn FnMut(&Path) -> io::Result<()>) -> io::Re

fn main() -> io::Result<()> {
let mut compiler = shaderc::Compiler::new().expect("failed to initialize glsl compiler");
let mut compile_options = shaderc::CompileOptions::new().expect("failed to initialize compiler options");

let mut compile_options =
shaderc::CompileOptions::new().expect("failed to initialize compiler options");

if env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
compile_options.add_macro_definition("TARGET_WASM", None);
}
Expand Down
8 changes: 3 additions & 5 deletions crates/render/shaders/fxaa.comp
Expand Up @@ -21,13 +21,11 @@ void main(void) {
const vec2 global_coord = vec2(gl_GlobalInvocationID.xy);
const vec2 inverse_size = 1.0 / vec2(gl_NumWorkGroups.xy * gl_WorkGroupSize.xy);

// Grab the pixel we're sitting directly on
// this fills in the center of the `loaded_texels` matrix.
const vec4 color_center = texture(sampler2D(color_input, linear_sampler), global_coord * inverse_size);
const vec3 color_nw = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(1.0, 1.0)) * inverse_size).rgb;
const vec3 color_nw = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(-1.0, -1.0)) * inverse_size).rgb;
const vec3 color_ne = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(1.0, -1.0)) * inverse_size).rgb;
const vec3 color_sw = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(-1.0, 1.0)) * inverse_size).rgb;
const vec3 color_se = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(-1.0, -1.0)) * inverse_size).rgb;
const vec3 color_se = texture(sampler2D(color_input, linear_sampler), (global_coord + vec2(1.0, 1.0)) * inverse_size).rgb;

const vec3 luma = vec3(0.299, 0.587, 0.114);
const float luma_nw = dot(color_nw, luma);
Expand All @@ -39,7 +37,7 @@ void main(void) {
float luma_min = min(luma_center, min(min(luma_nw, luma_ne), min(luma_sw, luma_se)));
float luma_max = max(luma_center, max(max(luma_nw, luma_ne), max(luma_sw, luma_se)));

vec2 dir = vec2(
mediump vec2 dir = vec2(
-((luma_nw + luma_ne) - (luma_sw + luma_se)),
(luma_nw + luma_sw) - (luma_ne + luma_se)
);
Expand Down
25 changes: 19 additions & 6 deletions crates/render/src/atoms.rs
Expand Up @@ -101,12 +101,25 @@ impl Atoms {
}
}

pub fn copy_new(&self, gpu_resources: &GlobalGpuResources, fragment_id: FragmentId, encoder: &mut wgpu::CommandEncoder) -> Self {
let buffer = self.buffer.copy_new(gpu_resources, |len, src_buffer, dest_buffer| {
let header_size = mem::size_of::<AtomBufferHeader>() as u64;
encoder.copy_buffer_to_buffer(src_buffer, header_size, dest_buffer, header_size, self.buffer.len());
len
});
pub fn copy_new(
&self,
gpu_resources: &GlobalGpuResources,
fragment_id: FragmentId,
encoder: &mut wgpu::CommandEncoder,
) -> Self {
let buffer = self
.buffer
.copy_new(gpu_resources, |len, src_buffer, dest_buffer| {
let header_size = mem::size_of::<AtomBufferHeader>() as u64;
encoder.copy_buffer_to_buffer(
src_buffer,
header_size,
dest_buffer,
header_size,
self.buffer.len(),
);
len
});

let bind_group = gpu_resources
.device
Expand Down
40 changes: 35 additions & 5 deletions crates/render/src/bind_groups.rs
Expand Up @@ -6,6 +6,7 @@ pub struct BindGroupLayouts {
pub global: wgpu::BindGroupLayout,
pub atoms: wgpu::BindGroupLayout,
pub blit: wgpu::BindGroupLayout,
pub fxaa: wgpu::BindGroupLayout,
}

impl BindGroupLayouts {
Expand Down Expand Up @@ -56,9 +57,7 @@ impl BindGroupLayouts {
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler {
comparison: false,
},
ty: wgpu::BindingType::Sampler { comparison: false },
count: None,
},
wgpu::BindGroupLayoutEntry {
Expand All @@ -71,8 +70,39 @@ impl BindGroupLayouts {
},
count: None,
},
]
})
],
}),
fxaa: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::Sampler { comparison: false },
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::SampledTexture {
dimension: wgpu::TextureViewDimension::D2,
component_type: wgpu::TextureComponentType::Float,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
dimension: wgpu::TextureViewDimension::D2,
format: crate::STORAGE_TEXTURE_FORMAT,
readonly: false,
},
count: None,
},
],
}),
}
}
}
9 changes: 5 additions & 4 deletions crates/render/src/buffer_vec.rs
Expand Up @@ -34,7 +34,6 @@ impl BufferVec {
where
F: FnOnce(&mut [u8]),
{

#[cfg(not(target_arch = "wasm32"))]
let buffer = {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
Expand All @@ -53,7 +52,7 @@ impl BufferVec {
};
#[cfg(target_arch = "wasm32")]
let buffer = {
use wgpu::util::{DeviceExt as _, BufferInitDescriptor};
use wgpu::util::{BufferInitDescriptor, DeviceExt as _};
let mut vec = vec![0; size as usize];
fill(&mut vec);
device.create_buffer_init(&BufferInitDescriptor {
Expand Down Expand Up @@ -136,7 +135,9 @@ impl BufferVec {
usage: self.usage,
mapped_at_creation: false,
});
gpu_resources.queue.write_buffer(&new_buffer, inner.len, data);
gpu_resources
.queue
.write_buffer(&new_buffer, inner.len, data);
new_buffer
};

Expand Down Expand Up @@ -217,7 +218,7 @@ impl BufferVec {

pub fn copy_new<F>(&self, gpu_resources: &GlobalGpuResources, f: F) -> Self
where
F: FnOnce(u64, &wgpu::Buffer /* from */, &wgpu::Buffer /* to */) -> u64
F: FnOnce(u64, &wgpu::Buffer /* from */, &wgpu::Buffer /* to */) -> u64,
{
if let Some(inner) = self.inner.as_ref() {
let copied_buffer = gpu_resources.device.create_buffer(&wgpu::BufferDescriptor {
Expand Down

0 comments on commit b6873c0

Please sign in to comment.