Skip to content

Commit

Permalink
use ifdef for fog, terrain and ssao
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Aug 20, 2023
1 parent 5345f12 commit b7e401d
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 57 deletions.
7 changes: 5 additions & 2 deletions assets/shaders/pbr/render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ fn render(sun: vec3<f32>,
dkD *= 1.0 - vec3(metallic);

let ambient: vec3<f32> = (0.2 * dkD * (0.04 + irradiance_diffuse) * albedo + specular) * ssao;
let atmo: vec3<f32> = atmosphere(-V, sun, depth * 0.2);
var color: vec3<f32> = ambient + Lo;

var color: vec3<f32> = ambient + Lo + atmo;
#ifdef FOG
let atmo: vec3<f32> = atmosphere(-V, sun, depth * 0.2);
color += atmo;
#endif

let autoexposure = 1.0 + smoothstep(0.0, 0.1, -sun.z) * 5.0;

Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/pixel.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ fn frag(@location(0) in_tint: vec4<f32>,

let albedo: vec4<f32> = textureSample(t_albedo, s_albedo, in_uv);
var ssao = 1.0;
if (params.ssao_enabled != 0) {
ssao = textureSample(t_ssao, s_ssao, position.xy / params.viewport).r;
}
#ifdef SSAO
ssao = textureSample(t_ssao, s_ssao, position.xy / params.viewport).r;
#endif

var shadow_v: f32 = 1.0;
if (params.shadow_mapping_resolution != 0) {
Expand Down
2 changes: 0 additions & 2 deletions assets/shaders/render_params.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ struct RenderParams {
viewport: vec2<f32>,
time: f32,
time_always: f32,
ssao_enabled: i32,
shadow_mapping_resolution: i32,
grid_enabled: i32,
}
14 changes: 8 additions & 6 deletions assets/shaders/terrain.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct FragmentOutput {
@group(3) @binding(14) var t_lightdata2: texture_2d<u32>;
@group(3) @binding(15) var s_lightdata2: sampler;

#ifdef TERRAIN_GRID
fn grid(in_wpos: vec3<f32>, wpos_fwidth_x: f32) -> f32 {
let level: f32 = wpos_fwidth_x * 20.0;

Expand Down Expand Up @@ -60,6 +61,7 @@ fn grid(in_wpos: vec3<f32>, wpos_fwidth_x: f32) -> f32 {

return isIn;
}
#endif

fn hash4(p: vec2<f32>) -> vec4<f32> { return fract(sin(vec4( 1.0+dot(p,vec2(37.0,17.0)),
2.0+dot(p,vec2(11.0,47.0)),
Expand Down Expand Up @@ -110,19 +112,19 @@ fn frag(@location(0) in_normal: vec3<f32>,
@location(1) in_wpos: vec3<f32>,
@builtin(position) position: vec4<f32>) -> FragmentOutput {
var ssao = 1.0;
if (params.ssao_enabled != 0) {
ssao = textureSample(t_ssao, s_ssao, position.xy / params.viewport).r;
}
#ifdef SSAO
ssao = textureSample(t_ssao, s_ssao, position.xy / params.viewport).r;
#endif

var shadow_v: f32 = 1.0;
if (params.shadow_mapping_resolution != 0) {
shadow_v = sampleShadow(in_wpos);
}

var c: vec3<f32> = params.grass_col.rgb;
if (params.grid_enabled != 0) {
c.g += grid(in_wpos, fwidth(in_wpos.x)) * 0.015;
}
#ifdef TERRAIN_GRID
c.g += grid(in_wpos, fwidth(in_wpos.x)) * 0.015;
#endif

let grass = (textureNoTile(t_grass, s_grass, in_wpos.xy / 100.0).rgb * 0.3 - c) * 0.5;
c = mix(c + grass, c, smoothstep(0.0, 0.1, in_wpos.z));
Expand Down
11 changes: 9 additions & 2 deletions assets/shaders/water.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,21 @@ fn frag(@location(0) _in_tint: vec4<f32>,
let reflected: vec3<f32> = reflect(-V, normal);

let reflected_atmo = atmosphere(reflected, sun, 1e38);
let view_atmo = atmosphere(-V, sun, depth * 0.2);


let sun_contrib: f32 = clamp(dot(normal, sun), 0.0, 1.0);

let base_color: vec3<f32> = 0.03 * vec3<f32>(0.262, 0.396, 0.508);
let sunpower: f32 = 0.1 * reflect_coeff;

var final_rgb: vec3<f32> = tonemap(base_color + view_atmo + sunpower * reflected_atmo);
var final_rgb: vec3<f32> = base_color + sunpower * reflected_atmo;

#ifdef FOG
let view_atmo = atmosphere(-V, sun, depth * 0.2);
final_rgb += view_atmo;
#endif

final_rgb = tonemap(final_rgb);

return FragmentOutput(
vec4<f32>(final_rgb, 0.9 + reflect_coeff * 0.1),
Expand Down
1 change: 1 addition & 0 deletions engine/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub trait State: 'static {
async fn run<S: State>(el: EventLoop<()>, window: Window) {
let mut ctx = Context::new(window, &el).await;
let mut state = S::new(&mut ctx);
ctx.gfx.defines_changed = false;

let mut frame: Option<_> = None;
let mut new_size: Option<PhysicalSize<u32>> = None;
Expand Down
16 changes: 11 additions & 5 deletions engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct GfxContext {
pub pbr: PBR,
pub lamplights: LampLights,
pub defines: FastMap<String, String>,
pub defines_changed: bool,

pub simplelit_bg: wgpu::BindGroup,
pub bnoise_bg: wgpu::BindGroup,
Expand Down Expand Up @@ -93,9 +94,7 @@ pub struct RenderParams {
pub viewport: Vec2,
pub time: f32,
pub time_always: f32,
pub ssao_enabled: i32,
pub shadow_mapping_resolution: i32,
pub grid_enabled: i32,
pub _pad5: [f32; 3],
}

Expand All @@ -114,9 +113,7 @@ impl Default for RenderParams {
viewport: vec2(1000.0, 1000.0),
time: 0.0,
time_always: 0.0,
ssao_enabled: 1,
shadow_mapping_resolution: 2048,
grid_enabled: 1,
_pad: 0.0,
_pad2: 0.0,
_pad4: 0.0,
Expand Down Expand Up @@ -272,6 +269,7 @@ impl GfxContext {
queue,
pbr,
defines: Default::default(),
defines_changed: false,
};

me.update_simplelit_bg();
Expand Down Expand Up @@ -310,6 +308,7 @@ impl GfxContext {
if self.defines.contains_key(name) == inserted {
return;
}
self.defines_changed = true;
if inserted {
self.defines.insert(name.to_string(), String::new());
} else {
Expand Down Expand Up @@ -565,7 +564,7 @@ impl GfxContext {
*enc_smap_ext = Some(smap_enc.finish());
}

if self.render_params.value().ssao_enabled != 0 {
if self.defines.contains_key("SSAO") {
profiling::scope!("ssao");
let pipeline = self.get_pipeline(SSAOPipeline);
let bg = self
Expand Down Expand Up @@ -657,6 +656,13 @@ impl GfxContext {
.chain(encoder.smap)
.chain(Some(encoder.end.finish())),
);
if self.defines_changed {
self.defines_changed = false;
self.pipelines
.try_borrow_mut()
.unwrap()
.invalidate_all(&self.defines, &self.device);
}
if self.tick % 30 == 0 {
#[cfg(debug_assertions)]
self.pipelines
Expand Down
7 changes: 7 additions & 0 deletions engine/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ 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(
&mut self,
defines: &FastMap<String, String>,
Expand Down
41 changes: 4 additions & 37 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

use winit::event_loop::ControlFlow;
use winit::window::Fullscreen;

use crate::rendering::immediate::{ImmediateDraw, ImmediateSound};
use common::History;
use egregoria::utils::time::GameTime;
use egregoria::Egregoria;
use engine::{Context, FrameContext, GfxContext, Tesselator};
use engine::{Context, FrameContext, Tesselator};
use geom::{vec2, vec3, Camera, LinearColor};

use crate::audio::GameAudio;
use crate::gui::windows::debug::DebugObjs;
use crate::gui::windows::settings::Settings;
use crate::gui::windows::settings::{manage_settings, Settings};
use crate::gui::{ExitState, FollowEntity, Gui, Tool, UiTextures};
use crate::inputmap::{Bindings, InputAction, InputMap};
use crate::rendering::{InstancedRender, MapRenderOptions, MapRenderer, OrbitCamera};
Expand Down Expand Up @@ -71,7 +70,7 @@ impl engine::framework::State for State {

{
let s = uiworld.read::<Settings>();
Self::manage_settings(ctx, &s);
manage_settings(ctx, &s);
}

defer!(log::info!("finished init of game loop"));
Expand Down Expand Up @@ -142,7 +141,7 @@ impl engine::framework::State for State {
.just_act
.contains(&InputAction::HideInterface);

Self::manage_settings(ctx, &self.uiw.read::<Settings>());
manage_settings(ctx, &self.uiw.read::<Settings>());
self.manage_io(ctx);

self.map_renderer.update(&self.goria.read().unwrap(), ctx);
Expand Down Expand Up @@ -305,38 +304,6 @@ impl State {
drop(c);
}

fn manage_settings(ctx: &mut Context, settings: &Settings) {
if settings.fullscreen && ctx.window.fullscreen().is_none() {
ctx.window
.set_fullscreen(Some(Fullscreen::Borderless(ctx.window.current_monitor())))
}
if !settings.fullscreen && ctx.window.fullscreen().is_some() {
ctx.window.set_fullscreen(None);
}

ctx.gfx.set_vsync(settings.vsync);
let params = ctx.gfx.render_params.value_mut();
params.ssao_enabled = settings.ssao as i32;
params.grid_enabled = settings.terrain_grid as i32;
params.shadow_mapping_resolution = settings.shadows.size().unwrap_or(0) as i32;

if let Some(v) = settings.shadows.size() {
if ctx.gfx.sun_shadowmap.extent.width != v {
ctx.gfx.sun_shadowmap = GfxContext::mk_shadowmap(&ctx.gfx.device, v);
ctx.gfx.update_simplelit_bg();
}
}

ctx.egui.pixels_per_point = settings.gui_scale;

ctx.audio.set_settings(
settings.master_volume_percent,
settings.ui_volume_percent,
settings.music_volume_percent,
settings.effects_volume_percent,
);
}

fn manage_io(&mut self, ctx: &mut Context) {
let goria = self.goria.read().unwrap();
let map = goria.map();
Expand Down
40 changes: 40 additions & 0 deletions native_app/src/gui/windows/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use common::saveload::Encoder;
use egregoria::Egregoria;
use egui::{Align2, Context, Widget};
use egui_extras::Column;
use engine::GfxContext;
use std::time::{Duration, Instant};
use winit::window::Fullscreen;

const SETTINGS_SAVE_NAME: &str = "settings";

Expand Down Expand Up @@ -68,6 +70,7 @@ pub struct Settings {
pub ssao: bool,
pub shadows: ShadowQuality,
pub terrain_grid: bool,
pub fog: bool,

pub gui_scale: f32,

Expand Down Expand Up @@ -99,6 +102,7 @@ impl Default for Settings {
camera_smooth_tightness: 1.0,
camera_fov: 60.0,
terrain_grid: true,
fog: true,
gui_scale: 1.0,
}
}
Expand Down Expand Up @@ -210,6 +214,7 @@ pub fn settings(window: egui::Window<'_>, ui: &Context, uiworld: &mut UiWorld, _

ui.checkbox(&mut settings.fullscreen, "Fullscreen");
ui.checkbox(&mut settings.terrain_grid, "Terrain Grid");
ui.checkbox(&mut settings.fog, "Fog");
ui.checkbox(&mut settings.ssao, "Ambient Occlusion (SSAO)");

// shadow quality combobox
Expand Down Expand Up @@ -346,3 +351,38 @@ pub fn settings(window: egui::Window<'_>, ui: &Context, uiworld: &mut UiWorld, _
}
});
}

pub fn manage_settings(ctx: &mut engine::Context, settings: &Settings) {
if settings.fullscreen && ctx.window.fullscreen().is_none() {
ctx.window
.set_fullscreen(Some(Fullscreen::Borderless(ctx.window.current_monitor())))
}
if !settings.fullscreen && ctx.window.fullscreen().is_some() {
ctx.window.set_fullscreen(None);
}

ctx.gfx.set_vsync(settings.vsync);
let params = ctx.gfx.render_params.value_mut();
params.shadow_mapping_resolution = settings.shadows.size().unwrap_or(0) as i32;

if let Some(v) = settings.shadows.size() {
if ctx.gfx.sun_shadowmap.extent.width != v {
ctx.gfx.sun_shadowmap = GfxContext::mk_shadowmap(&ctx.gfx.device, v);
ctx.gfx.update_simplelit_bg();
}
}

ctx.gfx.set_define_flag("FOG", settings.fog);
ctx.gfx.set_define_flag("SSAO", settings.ssao);
ctx.gfx
.set_define_flag("TERRAIN_GRID", settings.terrain_grid);

ctx.egui.pixels_per_point = settings.gui_scale;

ctx.audio.set_settings(
settings.master_volume_percent,
settings.ui_volume_percent,
settings.music_volume_percent,
settings.effects_volume_percent,
);
}

0 comments on commit b7e401d

Please sign in to comment.