From e7af860764d248f3cdbffd4ed0cd28b40c93453b Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Sun, 25 Apr 2021 17:31:26 +0100 Subject: [PATCH] Bad impl of volumetric point lights --- assets/voxygen/shaders/figure-frag.glsl | 2 +- assets/voxygen/shaders/fluid-frag/shiny.glsl | 4 +- .../shaders/include/cloud/regular.glsl | 27 ++++ assets/voxygen/shaders/lod-terrain-frag.glsl | 4 +- assets/voxygen/shaders/postprocess-frag.glsl | 1 - voxygen/src/render/pipelines/clouds.rs | 8 +- voxygen/src/render/renderer.rs | 152 ++++++++++++------ voxygen/src/scene/mod.rs | 1 + voxygen/src/scene/simple.rs | 1 + 9 files changed, 140 insertions(+), 60 deletions(-) diff --git a/assets/voxygen/shaders/figure-frag.glsl b/assets/voxygen/shaders/figure-frag.glsl index 0bfb9a5cad..73cdc66b61 100644 --- a/assets/voxygen/shaders/figure-frag.glsl +++ b/assets/voxygen/shaders/figure-frag.glsl @@ -70,7 +70,7 @@ uniform u_bones { BoneData bones[16]; }; -#include +//#include #include #include diff --git a/assets/voxygen/shaders/fluid-frag/shiny.glsl b/assets/voxygen/shaders/fluid-frag/shiny.glsl index 1b409c2a53..a873a3531f 100644 --- a/assets/voxygen/shaders/fluid-frag/shiny.glsl +++ b/assets/voxygen/shaders/fluid-frag/shiny.glsl @@ -50,7 +50,7 @@ uniform sampler2D t_waves; out vec4 tgt_color; -#include +//#include #include #include @@ -179,7 +179,7 @@ void main() { // Squared to account for prior saturation. float f_light = 1.0;// pow(f_light, 1.5); vec3 reflect_color = get_sky_color(/*reflect_ray_dir*/beam_view_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true); - reflect_color = get_cloud_color(reflect_color, reflect_ray_dir, cam_pos.xyz, time_of_day.x, 100000.0, 0.1); + //reflect_color = get_cloud_color(reflect_color, reflect_ray_dir, cam_pos.xyz, time_of_day.x, 100000.0, 0.1); reflect_color *= f_light; // /*const */vec3 water_color = srgb_to_linear(vec3(0.2, 0.5, 1.0)); // /*const */vec3 water_color = srgb_to_linear(vec3(0.8, 0.9, 1.0)); diff --git a/assets/voxygen/shaders/include/cloud/regular.glsl b/assets/voxygen/shaders/include/cloud/regular.glsl index ae2d50ce53..676c4d2905 100644 --- a/assets/voxygen/shaders/include/cloud/regular.glsl +++ b/assets/voxygen/shaders/include/cloud/regular.glsl @@ -1,5 +1,6 @@ #include #include +#include float falloff(float x) { return pow(max(x > 0.577 ? (0.3849 / x - 0.1) : (0.9 - x * x), 0.0), 4); @@ -234,5 +235,31 @@ vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, const float time_of emission * density_integrals.y; } + for (uint i = 0u; i < light_shadow_count.x; i ++) { + // Only access the array once + Light L = lights[i]; + + vec3 light_pos = L.light_pos.xyz;// - focus_off.xyz; + + float dist = max_dist; + float len_sq = pow(dist, 2); + float t = (dot(light_pos - origin, dir * dist) / len_sq); + t = clamp(t, 0, 1); + + vec3 nearest = origin.xyz + dir * dist * t; + + // Pre-calculate difference between light and fragment + vec3 difference = light_pos - nearest; + vec3 emission; + float vapor = cloud_at(nearest, dist * t, emission).z; + + float strength = attenuation_strength(difference) * 10.0 * vapor; + + // Multiply the vec3 only once + vec3 color = srgb_to_linear(L.light_col.rgb) * (strength * L.light_col.a); + + surf_color += color; + } + return surf_color; } diff --git a/assets/voxygen/shaders/lod-terrain-frag.glsl b/assets/voxygen/shaders/lod-terrain-frag.glsl index 6d8d6b2695..9348bc8ae7 100644 --- a/assets/voxygen/shaders/lod-terrain-frag.glsl +++ b/assets/voxygen/shaders/lod-terrain-frag.glsl @@ -20,7 +20,7 @@ #define HAS_LOD_FULL_INFO #include -#include +//#include #include in vec3 f_pos; @@ -648,7 +648,7 @@ void main() { float passthrough = dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag); vec3 reflect_color = get_sky_color(reflect_ray, time_of_day.x, f_pos, vec3(-100000), 0.125, true); - reflect_color = get_cloud_color(reflect_color, reflect_ray, cam_pos.xyz, time_of_day.x, 100000.0, 0.1); + //reflect_color = get_cloud_color(reflect_color, reflect_ray, cam_pos.xyz, time_of_day.x, 100000.0, 0.1); const float REFLECTANCE = 0.5; surf_color = illuminate(max_light, view_dir, f_col * emitted_light, reflect_color * REFLECTANCE + water_color * reflected_light); diff --git a/assets/voxygen/shaders/postprocess-frag.glsl b/assets/voxygen/shaders/postprocess-frag.glsl index 89ae9c1b59..804decb53c 100644 --- a/assets/voxygen/shaders/postprocess-frag.glsl +++ b/assets/voxygen/shaders/postprocess-frag.glsl @@ -20,7 +20,6 @@ // Note: The sampler uniform is declared here because it differs for MSAA #include #include -#include //uniform sampler2D src_depth; diff --git a/voxygen/src/render/pipelines/clouds.rs b/voxygen/src/render/pipelines/clouds.rs index 7ad0c782b9..a88af303e2 100644 --- a/voxygen/src/render/pipelines/clouds.rs +++ b/voxygen/src/render/pipelines/clouds.rs @@ -1,6 +1,6 @@ use super::{ super::{Mesh, Pipeline, TgtColorFmt, TgtDepthStencilFmt, Tri}, - Globals, + Globals, shadow, Light, Shadow, }; use gfx::{ self, gfx_constant_struct_meta, gfx_defines, gfx_impl_struct_meta, gfx_pipeline, @@ -23,6 +23,11 @@ gfx_defines! { locals: gfx::ConstantBuffer = "u_locals", globals: gfx::ConstantBuffer = "u_globals", + lights: gfx::ConstantBuffer = "u_lights", + shadows: gfx::ConstantBuffer = "u_shadows", + + point_shadow_maps: gfx::TextureSampler = "t_point_shadow_maps", + directed_shadow_maps: gfx::TextureSampler = "t_directed_shadow_maps", map: gfx::TextureSampler<[f32; 4]> = "t_map", alt: gfx::TextureSampler<[f32; 2]> = "t_alt", @@ -32,6 +37,7 @@ gfx_defines! { depth_sampler: gfx::TextureSampler<::View> = "src_depth", noise: gfx::TextureSampler = "t_noise", + light_shadows: gfx::ConstantBuffer = "u_light_shadows", tgt_color: gfx::RenderTarget = "tgt_color", } diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index d83048baa7..62dbcb39df 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -165,57 +165,65 @@ impl assets::Compound for Shaders { _: &assets::AssetCache, _: &str, ) -> Result { + let load = |name| match AssetExt::load(name) { + Ok(asset) => Ok(asset), + Err(err) => { + println!("When loading {}...", name); + Err(err) + }, + }; + Ok(Shaders { - constants: AssetExt::load("voxygen.shaders.include.constants")?, - globals: AssetExt::load("voxygen.shaders.include.globals")?, - sky: AssetExt::load("voxygen.shaders.include.sky")?, - light: AssetExt::load("voxygen.shaders.include.light")?, - srgb: AssetExt::load("voxygen.shaders.include.srgb")?, - random: AssetExt::load("voxygen.shaders.include.random")?, - lod: AssetExt::load("voxygen.shaders.include.lod")?, - shadows: AssetExt::load("voxygen.shaders.include.shadows")?, + constants: load("voxygen.shaders.include.constants")?, + globals: load("voxygen.shaders.include.globals")?, + sky: load("voxygen.shaders.include.sky")?, + light: load("voxygen.shaders.include.light")?, + srgb: load("voxygen.shaders.include.srgb")?, + random: load("voxygen.shaders.include.random")?, + lod: load("voxygen.shaders.include.lod")?, + shadows: load("voxygen.shaders.include.shadows")?, - anti_alias_none: AssetExt::load("voxygen.shaders.antialias.none")?, - anti_alias_fxaa: AssetExt::load("voxygen.shaders.antialias.fxaa")?, - anti_alias_msaa_x4: AssetExt::load("voxygen.shaders.antialias.msaa-x4")?, - anti_alias_msaa_x8: AssetExt::load("voxygen.shaders.antialias.msaa-x8")?, - anti_alias_msaa_x16: AssetExt::load("voxygen.shaders.antialias.msaa-x16")?, - cloud_none: AssetExt::load("voxygen.shaders.include.cloud.none")?, - cloud_regular: AssetExt::load("voxygen.shaders.include.cloud.regular")?, - figure_vert: AssetExt::load("voxygen.shaders.figure-vert")?, + anti_alias_none: load("voxygen.shaders.antialias.none")?, + anti_alias_fxaa: load("voxygen.shaders.antialias.fxaa")?, + anti_alias_msaa_x4: load("voxygen.shaders.antialias.msaa-x4")?, + anti_alias_msaa_x8: load("voxygen.shaders.antialias.msaa-x8")?, + anti_alias_msaa_x16: load("voxygen.shaders.antialias.msaa-x16")?, + cloud_none: load("voxygen.shaders.include.cloud.none")?, + cloud_regular: load("voxygen.shaders.include.cloud.regular")?, + figure_vert: load("voxygen.shaders.figure-vert")?, - terrain_point_shadow_vert: AssetExt::load("voxygen.shaders.light-shadows-vert")?, - terrain_directed_shadow_vert: AssetExt::load( + terrain_point_shadow_vert: load("voxygen.shaders.light-shadows-vert")?, + terrain_directed_shadow_vert: load( "voxygen.shaders.light-shadows-directed-vert", )?, - figure_directed_shadow_vert: AssetExt::load( + figure_directed_shadow_vert: load( "voxygen.shaders.light-shadows-figure-vert", )?, - directed_shadow_frag: AssetExt::load("voxygen.shaders.light-shadows-directed-frag")?, + directed_shadow_frag: load("voxygen.shaders.light-shadows-directed-frag")?, - skybox_vert: AssetExt::load("voxygen.shaders.skybox-vert")?, - skybox_frag: AssetExt::load("voxygen.shaders.skybox-frag")?, - figure_frag: AssetExt::load("voxygen.shaders.figure-frag")?, - terrain_vert: AssetExt::load("voxygen.shaders.terrain-vert")?, - terrain_frag: AssetExt::load("voxygen.shaders.terrain-frag")?, - fluid_vert: AssetExt::load("voxygen.shaders.fluid-vert")?, - fluid_frag_cheap: AssetExt::load("voxygen.shaders.fluid-frag.cheap")?, - fluid_frag_shiny: AssetExt::load("voxygen.shaders.fluid-frag.shiny")?, - sprite_vert: AssetExt::load("voxygen.shaders.sprite-vert")?, - sprite_frag: AssetExt::load("voxygen.shaders.sprite-frag")?, - particle_vert: AssetExt::load("voxygen.shaders.particle-vert")?, - particle_frag: AssetExt::load("voxygen.shaders.particle-frag")?, - ui_vert: AssetExt::load("voxygen.shaders.ui-vert")?, - ui_frag: AssetExt::load("voxygen.shaders.ui-frag")?, - lod_terrain_vert: AssetExt::load("voxygen.shaders.lod-terrain-vert")?, - lod_terrain_frag: AssetExt::load("voxygen.shaders.lod-terrain-frag")?, - clouds_vert: AssetExt::load("voxygen.shaders.clouds-vert")?, - clouds_frag: AssetExt::load("voxygen.shaders.clouds-frag")?, - postprocess_vert: AssetExt::load("voxygen.shaders.postprocess-vert")?, - postprocess_frag: AssetExt::load("voxygen.shaders.postprocess-frag")?, - player_shadow_frag: AssetExt::load("voxygen.shaders.player-shadow-frag")?, - light_shadows_geom: AssetExt::load("voxygen.shaders.light-shadows-geom")?, - light_shadows_frag: AssetExt::load("voxygen.shaders.light-shadows-frag")?, + skybox_vert: load("voxygen.shaders.skybox-vert")?, + skybox_frag: load("voxygen.shaders.skybox-frag")?, + figure_frag: load("voxygen.shaders.figure-frag")?, + terrain_vert: load("voxygen.shaders.terrain-vert")?, + terrain_frag: load("voxygen.shaders.terrain-frag")?, + fluid_vert: load("voxygen.shaders.fluid-vert")?, + fluid_frag_cheap: load("voxygen.shaders.fluid-frag.cheap")?, + fluid_frag_shiny: load("voxygen.shaders.fluid-frag.shiny")?, + sprite_vert: load("voxygen.shaders.sprite-vert")?, + sprite_frag: load("voxygen.shaders.sprite-frag")?, + particle_vert: load("voxygen.shaders.particle-vert")?, + particle_frag: load("voxygen.shaders.particle-frag")?, + ui_vert: load("voxygen.shaders.ui-vert")?, + ui_frag: load("voxygen.shaders.ui-frag")?, + lod_terrain_vert: load("voxygen.shaders.lod-terrain-vert")?, + lod_terrain_frag: load("voxygen.shaders.lod-terrain-frag")?, + clouds_vert: load("voxygen.shaders.clouds-vert")?, + clouds_frag: load("voxygen.shaders.clouds-frag")?, + postprocess_vert: load("voxygen.shaders.postprocess-vert")?, + postprocess_frag: load("voxygen.shaders.postprocess-frag")?, + player_shadow_frag: load("voxygen.shaders.player-shadow-frag")?, + light_shadows_geom: load("voxygen.shaders.light-shadows-geom")?, + light_shadows_frag: load("voxygen.shaders.light-shadows-frag")?, }) } } @@ -1803,9 +1811,28 @@ impl Renderer { &mut self, model: &Model, globals: &Consts, + global: &GlobalModel, locals: &Consts, lod: &lod_terrain::LodData, ) { + let (point_shadow_maps, directed_shadow_maps) = + if let Some(shadow_map) = &mut self.shadow_map { + ( + ( + shadow_map.point_res.clone(), + shadow_map.point_sampler.clone(), + ), + ( + shadow_map.directed_res.clone(), + shadow_map.directed_sampler.clone(), + ), + ) + } else { + ( + (self.noise_tex.srv.clone(), self.noise_tex.sampler.clone()), + (self.noise_tex.srv.clone(), self.noise_tex.sampler.clone()), + ) + }; self.encoder.draw( &gfx::Slice { start: model.vertex_range().start, @@ -1819,6 +1846,9 @@ impl Renderer { vbuf: model.vbuf.clone(), locals: locals.buf.clone(), globals: globals.buf.clone(), + lights: global.lights.buf.clone(), + shadows: global.shadows.buf.clone(), + light_shadows: global.shadow_mats.buf.clone(), map: (lod.map.srv.clone(), lod.map.sampler.clone()), alt: (lod.alt.srv.clone(), lod.alt.sampler.clone()), horizon: (lod.horizon.srv.clone(), lod.horizon.sampler.clone()), @@ -1826,6 +1856,8 @@ impl Renderer { depth_sampler: (self.tgt_depth_res.clone(), self.sampler.clone()), noise: (self.noise_tex.srv.clone(), self.noise_tex.sampler.clone()), tgt_color: self.tgt_color_view_pp.clone(), + point_shadow_maps, + directed_shadow_maps, }, ) } @@ -1944,17 +1976,31 @@ fn create_pipelines( _ => shaders.cloud_regular, }; + let mut include_ctx = IncludeContext::new(); - include_ctx.include("constants.glsl", &constants); - include_ctx.include("globals.glsl", &shaders.globals.read().0); - include_ctx.include("shadows.glsl", &shaders.shadows.read().0); - include_ctx.include("sky.glsl", &shaders.sky.read().0); - include_ctx.include("light.glsl", &shaders.light.read().0); - include_ctx.include("srgb.glsl", &shaders.srgb.read().0); - include_ctx.include("random.glsl", &shaders.random.read().0); - include_ctx.include("lod.glsl", &shaders.lod.read().0); - include_ctx.include("anti-aliasing.glsl", &anti_alias.read().0); - include_ctx.include("cloud.glsl", &cloud.read().0); + + let mut guard = |name: &str, code: &str| { + let define = name + .chars() + .map(|c| match c { + c if c.is_ascii_alphanumeric() => c, + _ => '_', + }) + .collect::(); + let code = format!("#ifndef {}\n#define {}\n{}\n#endif", define, define, code); + include_ctx.include(name, &code); + }; + + guard("constants.glsl", &constants); + guard("globals.glsl", &shaders.globals.read().0); + guard("shadows.glsl", &shaders.shadows.read().0); + guard("sky.glsl", &shaders.sky.read().0); + guard("light.glsl", &shaders.light.read().0); + guard("srgb.glsl", &shaders.srgb.read().0); + guard("random.glsl", &shaders.random.read().0); + guard("lod.glsl", &shaders.lod.read().0); + guard("anti-aliasing.glsl", &anti_alias.read().0); + guard("cloud.glsl", &cloud.read().0); // Construct a pipeline for rendering skyboxes let skybox_pipeline = create_pipeline( diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index daff82a064..a4815132c5 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -1097,6 +1097,7 @@ impl Scene { renderer.render_clouds( &self.clouds.model, &global.globals, + global, &self.clouds.locals, self.lod.get_data(), ); diff --git a/voxygen/src/scene/simple.rs b/voxygen/src/scene/simple.rs index 4901a5972c..62052b8783 100644 --- a/voxygen/src/scene/simple.rs +++ b/voxygen/src/scene/simple.rs @@ -434,6 +434,7 @@ impl Scene { renderer.render_clouds( &self.clouds.model, &self.data.globals, + &self.data, &self.clouds.locals, &self.lod, );