Skip to content

Commit

Permalink
allow configuring MSAA
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 21, 2024
1 parent 5e7ff16 commit eaefcff
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 35 deletions.
4 changes: 4 additions & 0 deletions assets/shaders/fog.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ struct FragmentOutput {

@group(0) @binding(0) var<uniform> params: RenderParams;

#ifdef MSAA
@group(1) @binding(0) var t_depth: texture_multisampled_2d<f32>;
#else
@group(1) @binding(0) var t_depth: texture_2d<f32>;
#endif
@group(1) @binding(1) var s_depth: sampler;

fn uv2s(uv: vec2<f32>) -> vec2<f32> {
Expand Down
5 changes: 5 additions & 0 deletions assets/shaders/ssao.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ struct FragmentOutput {

@group(0) @binding(0) var<uniform> params: RenderParams;

#ifdef MSAA
@group(1) @binding(0) var t_depth: texture_multisampled_2d<f32>;
#else
@group(1) @binding(0) var t_depth: texture_2d<f32>;
#endif

@group(1) @binding(1) var s_depth: sampler;


Expand Down
4 changes: 4 additions & 0 deletions assets/shaders/water.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ struct FragmentOutput {

@group(0) @binding(0) var<uniform> params: RenderParams;

#ifdef MSAA
@group(1) @binding(0) var t_depth: texture_multisampled_2d<f32>;
#else
@group(1) @binding(0) var t_depth: texture_2d<f32>;
#endif
@group(1) @binding(1) var s_depth: sampler;

@group(2) @binding(0) var t_wavy: texture_2d<f32>;
Expand Down
5 changes: 4 additions & 1 deletion engine/src/drawables/lit_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ impl PipelineKey for MeshPipeline {
false => gfx.sc_desc.format,
},
)
.with_samples(gfx.samples);
.with_samples(match self.offscreen_render {
true => 4,
false => gfx.samples,
});

if self.offscreen_render {
builder = builder.with_depth_write();
Expand Down
59 changes: 44 additions & 15 deletions engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct GfxSettings {
pub pbr_enabled: bool,
pub fog_shader_debug: bool,
pub parallel_render: bool,
pub msaa: bool,
}

impl Default for GfxSettings {
Expand All @@ -160,6 +161,7 @@ impl Default for GfxSettings {
pbr_enabled: true,
fog_shader_debug: false,
parallel_render: false,
msaa: false,
}
}
}
Expand Down Expand Up @@ -314,7 +316,8 @@ impl GfxContext {
alpha_mode: CompositeAlphaMode::Auto,
view_formats: vec![],
};
let samples = if cfg!(target_arch = "wasm32") { 1 } else { 4 };
// let samples = if cfg!(target_arch = "wasm32") { 1 } else { 4 };
let samples = 1;
let fbos = Self::create_textures(&device, &sc_desc, samples);
surface.configure(&device, &sc_desc);

Expand Down Expand Up @@ -611,12 +614,25 @@ impl GfxContext {
}
}

let samples = match settings.msaa {
true => 4,
false => 1,
};

if self.samples != samples {
self.samples = samples;
self.pipelines.write().unwrap().invalidate_all();
self.fbos = Self::create_textures(&self.device, &self.sc_desc, samples);
self.update_simplelit_bg();
}

self.set_define_flag("FOG", settings.fog);
self.set_define_flag("SSAO", settings.ssao);
self.set_define_flag("TERRAIN_GRID", settings.terrain_grid);
self.set_define_flag("DEBUG", settings.shader_debug);
self.set_define_flag("FOG_DEBUG", settings.fog_shader_debug);
self.set_define_flag("PBR_ENABLED", settings.pbr_enabled);
self.set_define_flag("MSAA", settings.msaa);

self.settings = Some(settings);
}
Expand Down Expand Up @@ -789,19 +805,31 @@ impl GfxContext {
let mut main_enc = self
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("shadow map encoder"),
label: Some("main pass encoder"),
});

let mut render_pass = main_enc.begin_render_pass(&RenderPassDescriptor {
label: Some("main render pass"),
color_attachments: &[Some(RenderPassColorAttachment {
let ops = wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
};

let attachment = if self.samples > 1 {
RenderPassColorAttachment {
view: &self.fbos.color_msaa,
resolve_target: Some(frame),
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
})],
ops,
}
} else {
RenderPassColorAttachment {
view: frame,
resolve_target: None,
ops,
}
};

let mut render_pass = main_enc.begin_render_pass(&RenderPassDescriptor {
label: Some("main render pass"),
color_attachments: &[Some(attachment)],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.fbos.depth.view,
depth_ops: None,
Expand Down Expand Up @@ -927,10 +955,7 @@ impl GfxContext {
);
if self.defines_changed {
self.defines_changed = false;
self.pipelines
.write()
.unwrap()
.invalidate_all(&self.defines, &self.device);
self.pipelines.write().unwrap().invalidate_all();
}
if self.tick % 30 == 0 {
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -977,7 +1002,11 @@ impl GfxContext {
FBOs {
depth,
depth_bg,
color_msaa: Texture::create_color_msaa(device, desc, samples),
color_msaa: if samples > 1 {
Texture::create_color_msaa(device, desc, samples)
} else {
ssao.mip_view(0) // bogus
},
ssao,
fog,
ui_blur,
Expand Down
28 changes: 20 additions & 8 deletions engine/src/passes/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ use wgpu::{

pub fn render_background(gfx: &GfxContext, enc: &mut CommandEncoder, frame: &TextureView) {
profiling::scope!("bg pass");
let mut bg_pass = enc.begin_render_pass(&RenderPassDescriptor {
label: Some("bg pass"),
color_attachments: &[Some(RenderPassColorAttachment {
let ops = wgpu::Operations {
load: wgpu::LoadOp::Load, // Don't clear! We're drawing after main pass
store: wgpu::StoreOp::Store,
};

let attachment = if gfx.samples > 1 {
RenderPassColorAttachment {
view: &gfx.fbos.color_msaa,
resolve_target: Some(frame),
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
})],
ops,
}
} else {
RenderPassColorAttachment {
view: frame,
resolve_target: None,
ops,
}
};

let mut bg_pass = enc.begin_render_pass(&RenderPassDescriptor {
label: Some("bg pass"),
color_attachments: &[Some(attachment)],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &gfx.fbos.depth.view,
depth_ops: Some(wgpu::Operations {
Expand Down
10 changes: 5 additions & 5 deletions engine/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ impl Pipelines {
}
}

pub fn invalidate_all(&mut self, defines: &FastMap<String, String>, device: &Device) {
let shader_names = self.shader_watcher.keys().cloned().collect::<Vec<_>>();
for shader_name in shader_names {
self.invalidate(defines, device, &shader_name);
}
pub fn invalidate_all(&mut self) {
self.pipelines.clear();
self.shader_watcher.clear();
self.shader_cache.clear();
self.pipelines_deps.clear();
}

pub fn invalidate(
Expand Down
23 changes: 17 additions & 6 deletions engine/src/yakui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,28 @@ impl YakuiWrapper {
self.yakui.finish();
}

self.renderer.paint_with_encoder(
&mut self.yakui,
&gfx.gfx.device,
&gfx.gfx.queue,
gfx.encoder,
let surface_info = if gfx.gfx.samples > 1 {
yakui_wgpu::SurfaceInfo {
format: self.format,
sample_count: gfx.gfx.samples,
color_attachment: &gfx.gfx.fbos.color_msaa,
resolve_target: Some(gfx.view),
},
}
} else {
yakui_wgpu::SurfaceInfo {
format: self.format,
sample_count: 1,
color_attachment: gfx.view,
resolve_target: None,
}
};

self.renderer.paint_with_encoder(
&mut self.yakui,
&gfx.gfx.device,
&gfx.gfx.queue,
gfx.encoder,
surface_info,
);
}

Expand Down
5 changes: 5 additions & 0 deletions native_app/src/newgui/hud/windows/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ pub fn settings(uiw: &UiWorld, _: &Simulation, opened: &mut bool) {
on_secondary_container(),
"Ambient Occlusion (SSAO)",
);
checkbox_value(
&mut settings.gfx.msaa,
on_secondary_container(),
"MSAA 4x Anti-aliasing",
);
checkbox_value(&mut settings.gfx.vsync, on_secondary_container(), "VSync");
checkbox_value(
&mut settings.gfx.parallel_render,
Expand Down

0 comments on commit eaefcff

Please sign in to comment.