18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 23:14:59 +01:00

Clouds and fog tweaks

This commit is contained in:
2026-11-17 01:02:52 +01:00
parent 9071d3f4d3
commit 91c2d3aba9
10 changed files with 380 additions and 63 deletions

View File

@@ -325,6 +325,8 @@ void weather_state::draw_debug_ui()
ImGui::Checkbox("shadows of the sun and headlights in the fog (gfx.fog.shadows)", &Global.gfx_fog_shadows); ImGui::Checkbox("shadows of the sun and headlights in the fog (gfx.fog.shadows)", &Global.gfx_fog_shadows);
ImGui::Checkbox("fog blended over frames (gfx.fog.temporal)", &Global.gfx_fog_temporal); ImGui::Checkbox("fog blended over frames (gfx.fog.temporal)", &Global.gfx_fog_temporal);
ImGui::SliderFloat("share of a new frame in the fog (gfx.fog.temporal.share)", &Global.gfx_fog_temporal_share, 0.02f, 1.f); ImGui::SliderFloat("share of a new frame in the fog (gfx.fog.temporal.share)", &Global.gfx_fog_temporal_share, 0.02f, 1.f);
ImGui::Checkbox("clouds and fog hidden by the scenery left as they were, vulkan (gfx.weather.occlusion)", &Global.gfx_weather_occlusion);
ImGui::Checkbox("a quarter of the clouds marched a frame, vulkan (gfx.clouds.interleave)", &Global.gfx_clouds_interleave);
char const *cloudlevels[]{"none", "low", "medium", "high", "ultra"}; char const *cloudlevels[]{"none", "low", "medium", "high", "ultra"};
ImGui::Combo("clouds (gfx.clouds)", &Global.gfx_clouds, cloudlevels, IM_ARRAYSIZE(cloudlevels)); ImGui::Combo("clouds (gfx.clouds)", &Global.gfx_clouds, cloudlevels, IM_ARRAYSIZE(cloudlevels));
ImGui::SliderFloat("cloud distance, m (gfx.clouds.distance)", &Global.gfx_clouds_distance, 5000.f, 200000.f, "%.0f", ImGuiSliderFlags_Logarithmic); ImGui::SliderFloat("cloud distance, m (gfx.clouds.distance)", &Global.gfx_clouds_distance, 5000.f, 200000.f, "%.0f", ImGuiSliderFlags_Logarithmic);

View File

@@ -161,8 +161,27 @@ class hiz_pyramid
bool build(command_stream *Commands, VkDevice Device, allocator &Memory, deletion_queue &Deletions, std::uint64_t Value, VkImageView Depth, VkSampler Sampler, int Width, int Height); bool build(command_stream *Commands, VkDevice Device, allocator &Memory, deletion_queue &Deletions, std::uint64_t Value, VkImageView Depth, VkSampler Sampler, int Width, int Height);
// the occlusion test of the pass // the occlusion test of the pass
cull_occlusion occlusion(VkSampler Sampler, glm::mat4 const &Projection) const; cull_occlusion occlusion(VkSampler Sampler, glm::mat4 const &Projection) const;
// the frame the pyramid was made in last from the depth of the main view, and the matrix from the world axes around its camera
// to its clip space: the weather of the next frame leaves out what the scenery hid (gfx.weather.occlusion). a pyramid made
// from another depth is of no frame
void made_in(std::uint64_t const Frame, glm::mat4 const &Projectionview)
{
m_frame = Frame;
m_projectionview = Projectionview;
}
std::uint64_t frame() const
{
return m_frame;
}
glm::mat4 const &projection_view() const
{
return m_projectionview;
}
static constexpr std::uint64_t no_frame{~0ull};
private: private:
std::uint64_t m_frame{no_frame};
glm::mat4 m_projectionview{1.f};
compute_kernel m_kernel; compute_kernel m_kernel;
std::shared_ptr<image> m_pyramid; std::shared_ptr<image> m_pyramid;
std::vector<VkImageView> m_levels; std::vector<VkImageView> m_levels;

View File

@@ -658,6 +658,13 @@ void vulkan14_renderer::render_instances_indirect()
static_cast<int>(targets->extent.height))) static_cast<int>(targets->extent.height)))
{ {
occlusion = scene.cull.hiz.occlusion(ctx.sampling.clamp_nearest, projection); occlusion = scene.cull.hiz.occlusion(ctx.sampling.clamp_nearest, projection);
// the weather of the next frame asks it what the scenery hid, of the main view
auto const mainview{(m_current_viewport != nullptr) && m_current_viewport->main && (m_renderpass.draw_mode == rendermode::color)};
scene.cull.hiz.made_in(mainview ? ctx.framecount : vk14::hiz_pyramid::no_frame, projection * glm::mat4{glm::mat3{OpenGLMatrices.data(GL_MODELVIEW)}});
}
else
{
scene.cull.hiz.made_in(vk14::hiz_pyramid::no_frame, glm::mat4{1.f});
} }
}; };
auto const useocclusion{Global.gfx_gpucull_occlusion && (false == Global.gfx_skippipeline) && (targets != nullptr) && targets->depth}; auto const useocclusion{Global.gfx_gpucull_occlusion && (false == Global.gfx_skippipeline) && (targets != nullptr) && targets->depth};

View File

@@ -20,6 +20,9 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulationenvironment.h" #include "simulation/simulationenvironment.h"
#include "utilities/Globals.h" #include "utilities/Globals.h"
#include <array>
#include <cmath>
namespace namespace
{ {
@@ -82,16 +85,20 @@ struct shadow_constants
struct march_constants struct march_constants
{ {
std::int32_t mode; std::int32_t mode;
std::int32_t pad; std::int32_t occlusion;
glm::ivec2 size; glm::ivec2 size;
glm::mat4 inverse_projection; glm::mat4 inverse_projection;
glm::mat4 view_to_world; glm::mat4 view_to_world;
std::int32_t frame; std::int32_t frame;
std::int32_t pad1[3]; std::int32_t stride;
glm::ivec2 phase;
glm::vec4 quality; glm::vec4 quality;
glm::ivec2 rows; glm::ivec2 rows;
glm::ivec2 pad2;
glm::mat4 occlusion_projection_view;
}; };
static_assert(offsetof(march_constants, quality) == 160, "the layout of the constants of clouds_march.comp"); static_assert(offsetof(march_constants, quality) == 160, "the layout of the constants of clouds_march.comp");
static_assert(offsetof(march_constants, occlusion_projection_view) == 192, "the layout of the constants of clouds_march.comp");
struct temporal_constants struct temporal_constants
{ {
glm::ivec2 size; glm::ivec2 size;
@@ -101,8 +108,12 @@ struct temporal_constants
glm::mat4 previous_projection_view; glm::mat4 previous_projection_view;
glm::vec3 previous_camera; glm::vec3 previous_camera;
float blend; float blend;
std::int32_t stride;
std::int32_t pad1;
glm::ivec2 phase;
}; };
static_assert(sizeof(temporal_constants) == 224, "the layout of the constants of clouds_temporal.comp"); static_assert(sizeof(temporal_constants) == 240, "the layout of the constants of clouds_temporal.comp");
static_assert(offsetof(temporal_constants, phase) == 232, "the layout of the constants of clouds_temporal.comp");
struct inject_block struct inject_block
{ {
glm::ivec3 grid; glm::ivec3 grid;
@@ -118,8 +129,19 @@ struct inject_block
std::int32_t pad2; std::int32_t pad2;
glm::vec2 previous_slicing; glm::vec2 previous_slicing;
glm::vec2 jitter; glm::vec2 jitter;
glm::vec2 pad3;
glm::mat4 occlusion_projection_view;
glm::vec4 occlusion;
}; };
static_assert(offsetof(inject_block, cablamps) == 256, "the layout of the block of fog_inject.comp"); static_assert(offsetof(inject_block, cablamps) == 256, "the layout of the block of fog_inject.comp");
static_assert(offsetof(inject_block, occlusion_projection_view) == 288, "the layout of the block of fog_inject.comp");
// the level of the depth pyramid of the clouds left out behind the scenery (clouds_march.comp: texels of 32 pixels of the scene)
int const CLOUD_OCCLUSION_LEVEL{4};
// gfx.clouds.interleave: one pixel of every block of this many by this many is marched a frame, and how much of the image a pixel
// marched in the frame takes from it: more than with all of them marched (0.15), as its turn comes once in a few frames
int const CLOUD_INTERLEAVE{2};
float const CLOUD_INTERLEAVE_BLEND{0.3f};
struct integrate_constants struct integrate_constants
{ {
glm::ivec3 grid; glm::ivec3 grid;
@@ -452,7 +474,11 @@ void vulkan14_renderer::update_weather()
constants.quality = glm::vec4{static_cast<float>(std::max(level.steps / 2, 24)), static_cast<float>(std::max(level.light_steps - 1, 2)), Global.gfx_clouds_distance, 0.f}; constants.quality = glm::vec4{static_cast<float>(std::max(level.steps / 2, 24)), static_cast<float>(std::max(level.light_steps - 1, 2)), Global.gfx_clouds_distance, 0.f};
constants.rows = glm::ivec2{first, rows}; constants.rows = glm::ivec2{first, rows};
// the depth isn't written in this mode, but something has to be bound // the depth isn't written in this mode, but something has to be bound
weather.march.dispatch(commands, {vk14::storage_image(weather.panorama->view()), vk14::storage_image(weather.depth_dummy->view())}, constants, groups(weather.panorama_size.x), groups(rows), 1); // nor is the depth pyramid read, the noise of the coverage stands for it
weather.march.dispatch(commands,
{vk14::storage_image(weather.panorama->view()), vk14::storage_image(weather.depth_dummy->view()),
vk14::image_descriptor(ctx.sampling.clamp_nearest, weather.coverage_noise->view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)},
constants, groups(weather.panorama_size.x), groups(rows), 1);
vk14::barriers(commands, {vk14::use_of(*weather.panorama, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, sampled_stage, sampled_access)}); vk14::barriers(commands, {vk14::use_of(*weather.panorama, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, sampled_stage, sampled_access)});
} }
} }
@@ -503,20 +529,50 @@ void vulkan14_renderer::render_weather_view(glm::mat4 const &Projection, glm::ma
auto const projectionview{Projection * rotation}; auto const projectionview{Projection * rotation};
auto &previousclouds{m_weatherstate.previous_clouds}; auto &previousclouds{m_weatherstate.previous_clouds};
auto &previousfog{m_weatherstate.previous_fog}; auto &previousfog{m_weatherstate.previous_fog};
// the scenery hid a part of the view in the last frame, which isn't worked out anew (gfx.weather.occlusion): the depth pyramid
// the culling of the main view made in that frame tells it, for a perspective projection with the far plane at infinity (its
// depth is near / w, as the contact shadows take it). the history is kept in the place of the part left out
auto const &pyramid{ctx.scene.cull.hiz};
auto const hidden{pyramid.occlusion(ctx.sampling.clamp_nearest, Projection)};
auto const occlusion{Global.gfx_weather_occlusion && (hidden.pyramid != VK_NULL_HANDLE) && (hidden.levels > 0) && (pyramid.frame() + 1 == ctx.framecount) && (Projection[2][3] != 0.f) && (Projection[2][2] == 0.f) &&
(Projection[3][2] > 0.f)};
// something has to be bound in its place otherwise
auto const pyramiddescriptor{occlusion ? vk14::image_descriptor(hidden.sampler, hidden.pyramid, VK_IMAGE_LAYOUT_GENERAL)
: vk14::image_descriptor(ctx.sampling.clamp_nearest, weather.coverage_noise->view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)};
if (m_weatherstate.clouds_drawn() && weather.clouds_color && weather.clouds_depth && weather.clouds_history[0] && weather.clouds_history[1]) if (m_weatherstate.clouds_drawn() && weather.clouds_color && weather.clouds_depth && weather.clouds_history[0] && weather.clouds_history[1])
{ {
ctx.passes.begin_pass("clouds, fog volume", {vk14::use_of(*weather.clouds_color, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write), vk14::use_of(*weather.clouds_depth, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write)}); ctx.passes.begin_pass("clouds", {vk14::use_of(*weather.clouds_color, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write), vk14::use_of(*weather.clouds_depth, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write)});
bind_compute_set(weather.march); bind_compute_set(weather.march);
auto const cloudscontinue{gfx::weather_state::continuity(previousclouds, Camera)};
march_constants constants{}; march_constants constants{};
constants.mode = 0; constants.mode = 0;
// only with a history to keep where the clouds are left out
constants.occlusion = ((occlusion && cloudscontinue) ? std::min(CLOUD_OCCLUSION_LEVEL, hidden.levels - 1) + 1 : 0);
constants.occlusion_projection_view = pyramid.projection_view();
// a part of the pixels a frame, each block in the order which leaves the pixels marched last the farthest apart; all of them
// when there's no history to make the image of the frame with
// nor when the view turned by a good part of itself since the last frame (a view to the side of the cab, a jump of the free
// camera): the middle of the view was then outside the half of the last view around its middle, much of the history is
// missing, and the image would be made of a quarter of its pixels for the frames it takes to get sharp again
auto const middle{previousclouds.projection_view * glm::vec4{glm::vec3{viewtoworld * glm::vec4{0.f, 0.f, -1.f, 0.f}}, 0.f}};
auto const turned{(middle.w <= 0.f) || (std::max(std::abs(middle.x), std::abs(middle.y)) > 0.5f * middle.w)};
auto const interleaved{Global.gfx_clouds_interleave && cloudscontinue && (false == turned)};
auto const stride{interleaved ? CLOUD_INTERLEAVE : 1};
std::array<glm::ivec2, 4> const phases{glm::ivec2{0, 0}, glm::ivec2{1, 1}, glm::ivec2{1, 0}, glm::ivec2{0, 1}};
auto const phase{interleaved ? phases[static_cast<std::size_t>(m_weatherstate.frame()) % phases.size()] : glm::ivec2{0}};
constants.stride = stride;
constants.phase = phase;
constants.size = weather.view_size; constants.size = weather.view_size;
constants.inverse_projection = inverseprojection; constants.inverse_projection = inverseprojection;
constants.view_to_world = viewtoworld; constants.view_to_world = viewtoworld;
constants.frame = m_weatherstate.frame(); // the turns a pixel had, not the frames: its samples are moved along their steps by the number of its turn, and with a pixel
// marched every fourth frame the frames would move them by nearly the same every time
constants.frame = m_weatherstate.frame() / (stride * stride);
constants.quality = glm::vec4{static_cast<float>(level.steps), static_cast<float>(level.light_steps), Global.gfx_clouds_distance, level.detail ? 1.f : 0.f}; constants.quality = glm::vec4{static_cast<float>(level.steps), static_cast<float>(level.light_steps), Global.gfx_clouds_distance, level.detail ? 1.f : 0.f};
constants.rows = glm::ivec2{0, weather.view_size.y}; constants.rows = glm::ivec2{0, weather.view_size.y};
weather.march.dispatch(commands, {vk14::storage_image(weather.clouds_color->view()), vk14::storage_image(weather.clouds_depth->view())}, constants, groups(weather.view_size.x), groups(weather.view_size.y), 1); weather.march.dispatch(commands, {vk14::storage_image(weather.clouds_color->view()), vk14::storage_image(weather.clouds_depth->view()), pyramiddescriptor}, constants,
groups((weather.view_size.x + stride - 1) / stride), groups((weather.view_size.y + stride - 1) / stride), 1);
auto const next{1 - weather.clouds_current}; auto const next{1 - weather.clouds_current};
auto &history{*weather.clouds_history[weather.clouds_current]}; auto &history{*weather.clouds_history[weather.clouds_current]};
@@ -530,7 +586,17 @@ void vulkan14_renderer::render_weather_view(glm::mat4 const &Projection, glm::ma
blend.view_to_world = viewtoworld; blend.view_to_world = viewtoworld;
blend.previous_projection_view = previousclouds.projection_view; blend.previous_projection_view = previousclouds.projection_view;
blend.previous_camera = glm::vec3{previousclouds.camera - Camera}; blend.previous_camera = glm::vec3{previousclouds.camera - Camera};
blend.blend = gfx::weather_state::continuity(previousclouds, Camera) ? 0.15f : 1.f; blend.blend = 1.f;
if (interleaved)
{
blend.blend = CLOUD_INTERLEAVE_BLEND;
}
else if (cloudscontinue)
{
blend.blend = 0.15f;
}
blend.stride = stride;
blend.phase = phase;
auto const &linear{ctx.sampling.clamp_linear}; auto const &linear{ctx.sampling.clamp_linear};
auto const &nearest{ctx.sampling.clamp_nearest}; auto const &nearest{ctx.sampling.clamp_nearest};
weather.temporal.dispatch(commands, weather.temporal.dispatch(commands,
@@ -544,14 +610,15 @@ void vulkan14_renderer::render_weather_view(glm::mat4 const &Projection, glm::ma
} }
else else
{ {
ctx.passes.begin_pass("clouds, fog volume");
previousclouds.valid = false; previousclouds.valid = false;
} }
if ((Global.gfx_fog == 1) && weather.fog_integrated && weather.fog_scattering[0] && weather.fog_scattering[1]) if ((Global.gfx_fog == 1) && weather.fog_integrated && weather.fog_scattering[0] && weather.fog_scattering[1])
{ {
ctx.passes.begin_pass("fog volume");
bind_compute_set(weather.inject); bind_compute_set(weather.inject);
auto const temporal{Global.gfx_fog_temporal && gfx::weather_state::continuity(previousfog, Camera)}; auto const fogcontinues{gfx::weather_state::continuity(previousfog, Camera)};
auto const temporal{Global.gfx_fog_temporal && fogcontinues};
auto const jitter{m_weatherstate.fog_jitter(temporal)}; auto const jitter{m_weatherstate.fog_jitter(temporal)};
auto const slicing{gfx::weather_state::fog_slicing(weather.fog_grid.z, jitter)}; auto const slicing{gfx::weather_state::fog_slicing(weather.fog_grid.z, jitter)};
auto const next{1 - weather.fog_current}; auto const next{1 - weather.fog_current};
@@ -571,12 +638,22 @@ void vulkan14_renderer::render_weather_view(glm::mat4 const &Projection, glm::ma
block.cablamps = Cablamps; block.cablamps = Cablamps;
block.previous_slicing = glm::vec2{previousfog.slicing.x, previousfog.slicing.z}; block.previous_slicing = glm::vec2{previousfog.slicing.x, previousfog.slicing.z};
block.jitter = glm::vec2{jitter.x, jitter.y}; block.jitter = glm::vec2{jitter.x, jitter.y};
if (occlusion && fogcontinues)
{
// the texels of the pyramid looked at reach as far as the cells a pixel takes its fog from: three and a half cells, each some
// pixels of the scene wide (fog_inject.comp); a texel of the level n is 2^(n + 1) pixels
auto const reach{3.5f * static_cast<float>(Size.x) / static_cast<float>(std::max(weather.fog_grid.x, 1))};
auto const level{std::clamp(static_cast<int>(std::ceil(std::log2(std::max(reach, 2.f)))) - 1, 0, hidden.levels - 1)};
block.occlusion_projection_view = pyramid.projection_view();
block.occlusion = glm::vec4{static_cast<float>(level + 1), Projection[3][2], 0.f, 0.f};
}
auto const slice{ctx.ring.allocate(sizeof(block), ctx.device.caps().limits.minUniformBufferOffsetAlignment)}; auto const slice{ctx.ring.allocate(sizeof(block), ctx.device.caps().limits.minUniformBufferOffsetAlignment)};
if (slice) if (slice)
{ {
std::memcpy(slice.data, &block, sizeof(block)); std::memcpy(slice.data, &block, sizeof(block));
weather.inject.run(commands, weather.inject.run(commands,
{vk14::storage_image(result.view()), vk14::image_descriptor(ctx.sampling.clamp_linear, history.view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL), vk14::buffer_descriptor(slice.buffer, slice.offset, sizeof(block))}, {vk14::storage_image(result.view()), vk14::image_descriptor(ctx.sampling.clamp_linear, history.view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL), vk14::buffer_descriptor(slice.buffer, slice.offset, sizeof(block)),
pyramiddescriptor},
groups(weather.fog_grid.x), groups(weather.fog_grid.y), static_cast<std::uint32_t>(weather.fog_grid.z)); groups(weather.fog_grid.x), groups(weather.fog_grid.y), static_cast<std::uint32_t>(weather.fog_grid.z));
} }
vk14::barriers(commands, {vk14::use_of(result, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, sampled_stage, sampled_access), vk14::use_of(*weather.fog_integrated, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write)}); vk14::barriers(commands, {vk14::use_of(result, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, sampled_stage, sampled_access), vk14::use_of(*weather.fog_integrated, VK_IMAGE_LAYOUT_GENERAL, compute_stage, storage_write)});

View File

@@ -55,6 +55,9 @@ const int CLOUD_SCATTER_OCTAVES = 6;
// light falls off fastest (about a 30th of the reach at 4 steps) and the steps get close to even when there are many of them // light falls off fastest (about a 30th of the reach at 4 steps) and the steps get close to even when there are many of them
const float CLOUD_LIGHT_GROWTH = 24.0; const float CLOUD_LIGHT_GROWTH = 24.0;
// the phase of the view for each order of the scattering (cloud_sunlight): the same for every sample of a ray, worked out once
float cloud_scatter_phase[CLOUD_SCATTER_OCTAVES];
float henyey_greenstein(float cosangle, float g) float henyey_greenstein(float cosangle, float g)
{ {
// times 4 pi: 1 for an even scattering // times 4 pi: 1 for an even scattering
@@ -77,8 +80,12 @@ vec3 cloud_sky_light(float up)
} }
// light of the sun reaching a point inside a layer through the cloud towards it, with the light scattered many times // light of the sun reaching a point inside a layer through the cloud towards it, with the light scattered many times
// (octaves after Wrenninge), and the phase of the view // (octaves after Wrenninge), and the phase of the view (cloud_scatter_phase). Offset: where in its step each sample is taken
float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float powder, bool detail) // (0-1), another place for every sample of the view and every frame. taken always in the middle, a far sample (the last step is
// some 40% of the reach) came into the cloud at the same place for all the pixels around, and the shaded sides of the clouds
// were cut into bands of grey along it; moved about, the bands turn into a noise the samples of the ray and the blending over
// frames (clouds_temporal.comp) smooth out
float cloud_sunlight(int index, vec3 position, int steps, float powder, bool detail, float offset)
{ {
vec3 sun = weather_sun_direction.xyz; vec3 sun = weather_sun_direction.xyz;
cloud_layer_s layer = cloud_layers[index]; cloud_layer_s layer = cloud_layers[index];
@@ -92,7 +99,7 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
float step = reach * (growth - 1.0) / (pow(growth, float(steps)) - 1.0); float step = reach * (growth - 1.0) / (pow(growth, float(steps)) - 1.0);
for (int s = 0; s < steps; s++) for (int s = 0; s < steps; s++)
{ {
vec3 point = position + sun * (travelled + 0.5 * step); vec3 point = position + sun * (travelled + offset * step);
travelled += step; travelled += step;
// the nearest steps with the detail noise: the small billows shade one another, lit on top and grey beneath // the nearest steps with the detail noise: the small billows shade one another, lit on top and grey beneath
depth += cloud_layer_density(index, point, cloud_height(point), detail && (s < CLOUD_LIGHT_DETAIL_STEPS)) * step; depth += cloud_layer_density(index, point, cloud_height(point), detail && (s < CLOUD_LIGHT_DETAIL_STEPS)) * step;
@@ -101,26 +108,25 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
float light = 0.0; float light = 0.0;
float amount = 1.0; float amount = 1.0;
float fade = 1.0; float fade = 1.0;
float spread = 1.0;
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++) for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
{ {
light += amount * exp(-depth * fade) * cloud_phase(cosangle, spread) * (octave == 0 ? powder : 1.0); light += amount * exp(-depth * fade) * cloud_scatter_phase[octave] * (octave == 0 ? powder : 1.0);
amount *= 0.5; amount *= 0.5;
fade *= 0.5; fade *= 0.5;
spread *= 0.5;
} }
return light; return light;
} }
// optical depth of the cloud over a point up to Reach above it, from Samples of the coarse density: under a lump and deep in a // optical depth of the cloud over a point up to Reach above it, from Samples of the coarse density: under a lump and deep in a
// thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud // thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud.
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples) // Offset: where in its part of the reach each sample is taken (0-1), as in cloud_sunlight()
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples, float offset)
{ {
float depth = 0.0; float depth = 0.0;
for (int s = 0; s < samples; s++) for (int s = 0; s < samples; s++)
{ {
// up along the vertical: the height grows by the same distance (cloud_height bends only with the horizontal distance) // up along the vertical: the height grows by the same distance (cloud_height bends only with the horizontal distance)
float up = reach * (float(s) + 0.5) / float(samples); float up = reach * (float(s) + offset) / float(samples);
depth += cloud_layer_density(index, position + vec3(0.0, up, 0.0), height + up, false); depth += cloud_layer_density(index, position + vec3(0.0, up, 0.0), height + up, false);
} }
return depth * reach / float(samples); return depth * reach / float(samples);
@@ -164,6 +170,16 @@ void main()
int skysteps = (lightsteps >= 4) ? 2 : 1; int skysteps = (lightsteps >= 4) ? 2 : 1;
bool detail = quality.w > 0.5; bool detail = quality.w > 0.5;
float jitter = fract(interleaved_noise(vec2(texel)) + float(frame) * 0.618034); float jitter = fract(interleaved_noise(vec2(texel)) + float(frame) * 0.618034);
// where the samples of the light are taken in their steps: another sequence than the one of the samples of the view
float lightjitter = fract(interleaved_noise(vec2(texel) + vec2(37.0, 17.0)) + float(frame) * 0.754878);
{
float spread = 1.0;
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
{
cloud_scatter_phase[octave] = cloud_phase(cosangle, spread);
spread *= 0.5;
}
}
vec3 scattered = vec3(0.0); vec3 scattered = vec3(0.0);
float transmittance = 1.0; float transmittance = 1.0;
@@ -222,13 +238,15 @@ void main()
if (density <= 0.0) if (density <= 0.0)
continue; continue;
float powder = mix(1.0, 1.0 - exp(-density / max(layer.shape.y, 1e-6) * CLOUD_POWDER), powderview); float powder = mix(1.0, 1.0 - exp(-density / max(layer.shape.y, 1e-6) * CLOUD_POWDER), powderview);
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, cosangle, lightsteps, powder, detail) : 0.0; // every sample of the view takes the samples of its light at another place of their steps
float lightoffset = fract(lightjitter + float(s) * 0.754878);
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, lightsteps, powder, detail, lightoffset) : 0.0;
// the light of the sky reaches inside through the cloud above, that of the ground through the cloud below: the base of a // the light of the sky reaches inside through the cloud above, that of the ground through the cloud below: the base of a
// thick cloud is darker than its top, and greyer, lit only by the ground and the sky at its sides // thick cloud is darker than its top, and greyer, lit only by the ground and the sky at its sides
float column = layer.shape.y * layer.shape.x; float column = layer.shape.y * layer.shape.x;
float top = cloud_layer_bounds(layer).y; float top = cloud_layer_bounds(layer).y;
float reach = clamp(top - height, 0.0, CLOUD_SKY_REACH); float reach = clamp(top - height, 0.0, CLOUD_SKY_REACH);
float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0)); float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps, fract(lightoffset + 0.569840)) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0));
float below = column * max(height - layer.heights.x, 0.0); float below = column * max(height - layer.heights.x, 0.0);
vec3 light = sunlight * sunshare + (ambienttop * mix(CLOUD_SKY_FLOOR, 1.0, sky) + ambientbottom * exp(-CLOUD_AMBIENT_FADE * below)); vec3 light = sunlight * sunshare + (ambienttop * mix(CLOUD_SKY_FLOOR, 1.0, sky) + ambientbottom * exp(-CLOUD_AMBIENT_FADE * below));
float through = exp(-density * step); float through = exp(-density * step);

View File

@@ -4,7 +4,12 @@
// (sky_weather.frag). mode 0: the view of the main camera at a part of the resolution, smoothed over frames by // (sky_weather.frag). mode 0: the view of the main camera at a part of the resolution, smoothed over frames by
// clouds_temporal.comp; mode 1: a panorama around the camera (azimuth across, elevation up), a band of rows per frame, for the // clouds_temporal.comp; mode 1: a panorama around the camera (azimuth across, elevation up), a band of rows per frame, for the
// environment map and the other views. rgb: the light of the clouds with the fog in front of them, a: what they let through // environment map and the other views. rgb: the light of the clouds with the fog in front of them, a: what they let through
// of the sky behind; the depth image gets the distance of the clouds, for the reprojection // of the sky behind; the depth image gets the distance of the clouds, for the reprojection.
// mode 0, gfx.weather.occlusion: the pixels the scenery covered in the last frame, with everything around them, in front of all the
// clouds of their ray aren't marched (the depth pyramid of the culling of that frame says so); their alpha is -1, which tells
// clouds_temporal.comp to keep what the history holds for them.
// mode 0, gfx.clouds.interleave: one pixel of every block of stride x stride is marched a frame, the one at phase, each in its
// turn; the others keep what they were given in their own turn, and clouds_temporal.comp makes the image of the frame of them
#define LAYOUT_COMPUTE 1 #define LAYOUT_COMPUTE 1
#include "common.glsl" #include "common.glsl"
@@ -14,20 +19,30 @@ layout(local_size_x = WEATHER_GROUP_SIZE, local_size_y = WEATHER_GROUP_SIZE, loc
layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image2D clouds_color; layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image2D clouds_color;
layout(r32f, set = COMPUTE_SET, binding = 1) uniform writeonly image2D clouds_depth; layout(r32f, set = COMPUTE_SET, binding = 1) uniform writeonly image2D clouds_depth;
// the depth pyramid of the main view of the last frame (hiz_build.comp: the farthest depth of what a texel covers, reversed, 0 the
// sky); read only with occlusion set
layout(set = COMPUTE_SET, binding = 2) uniform sampler2D occlusion_depth;
// the parameters of the kernel (push constants) // the parameters of the kernel (push constants)
layout(push_constant) uniform kernel_constants layout(push_constant) uniform kernel_constants
{ {
int mode; int mode;
// mode 0: the level of the depth pyramid looked at for what the scenery hides, plus 1; 0: nothing is taken for hidden
int occlusion;
ivec2 size; ivec2 size;
// mode 0: clip space to view space of the camera, and view space to world axes // mode 0: clip space to view space of the camera, and view space to world axes
mat4 inverse_projection; mat4 inverse_projection;
mat4 view_to_world; mat4 view_to_world;
int frame; int frame;
// mode 0: the pixels marched in this frame are the ones at phase in the blocks of stride x stride pixels (1: all of them)
int stride;
ivec2 phase;
// x: samples through a layer at most, y: samples towards the sun, z: farthest distance (m), w: 1 with the detail noise // x: samples through a layer at most, y: samples towards the sun, z: farthest distance (m), w: 1 with the detail noise
vec4 quality; vec4 quality;
// mode 1: first row of the band and the rows in it // mode 1: first row of the band and the rows in it
ivec2 rows; ivec2 rows;
// world axes to the clip space of the frame the depth pyramid was made in
mat4 occlusion_projection_view;
}; };
// lowest elevation of the panorama (radians); its rows go with the square of the height, denser at the horizon // lowest elevation of the panorama (radians); its rows go with the square of the height, denser at the horizon
@@ -60,6 +75,9 @@ const int CLOUD_SCATTER_OCTAVES = 6;
// light falls off fastest (about a 30th of the reach at 4 steps) and the steps get close to even when there are many of them // light falls off fastest (about a 30th of the reach at 4 steps) and the steps get close to even when there are many of them
const float CLOUD_LIGHT_GROWTH = 24.0; const float CLOUD_LIGHT_GROWTH = 24.0;
// the phase of the view for each order of the scattering (cloud_sunlight): the same for every sample of a ray, worked out once
float cloud_scatter_phase[CLOUD_SCATTER_OCTAVES];
float henyey_greenstein(float cosangle, float g) float henyey_greenstein(float cosangle, float g)
{ {
// times 4 pi: 1 for an even scattering // times 4 pi: 1 for an even scattering
@@ -82,8 +100,12 @@ vec3 cloud_sky_light(float up)
} }
// light of the sun reaching a point inside a layer through the cloud towards it, with the light scattered many times // light of the sun reaching a point inside a layer through the cloud towards it, with the light scattered many times
// (octaves after Wrenninge), and the phase of the view // (octaves after Wrenninge), and the phase of the view (cloud_scatter_phase). Offset: where in its step each sample is taken
float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float powder, bool detail) // (0-1), another place for every sample of the view and every frame. taken always in the middle, a far sample (the last step is
// some 40% of the reach) came into the cloud at the same place for all the pixels around, and the shaded sides of the clouds
// were cut into bands of grey along it; moved about, the bands turn into a noise the samples of the ray and the blending over
// frames (clouds_temporal.comp) smooth out
float cloud_sunlight(int index, vec3 position, int steps, float powder, bool detail, float offset)
{ {
vec3 sun = weather_sun_direction.xyz; vec3 sun = weather_sun_direction.xyz;
cloud_layer_s layer = cloud_layers[index]; cloud_layer_s layer = cloud_layers[index];
@@ -97,7 +119,7 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
float step = reach * (growth - 1.0) / (pow(growth, float(steps)) - 1.0); float step = reach * (growth - 1.0) / (pow(growth, float(steps)) - 1.0);
for (int s = 0; s < steps; s++) for (int s = 0; s < steps; s++)
{ {
vec3 point = position + sun * (travelled + 0.5 * step); vec3 point = position + sun * (travelled + offset * step);
travelled += step; travelled += step;
// the nearest steps with the detail noise: the small billows shade one another, lit on top and grey beneath // the nearest steps with the detail noise: the small billows shade one another, lit on top and grey beneath
depth += cloud_layer_density(index, point, cloud_height(point), detail && (s < CLOUD_LIGHT_DETAIL_STEPS)) * step; depth += cloud_layer_density(index, point, cloud_height(point), detail && (s < CLOUD_LIGHT_DETAIL_STEPS)) * step;
@@ -106,31 +128,57 @@ float cloud_sunlight(int index, vec3 position, float cosangle, int steps, float
float light = 0.0; float light = 0.0;
float amount = 1.0; float amount = 1.0;
float fade = 1.0; float fade = 1.0;
float spread = 1.0;
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++) for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
{ {
light += amount * exp(-depth * fade) * cloud_phase(cosangle, spread) * (octave == 0 ? powder : 1.0); light += amount * exp(-depth * fade) * cloud_scatter_phase[octave] * (octave == 0 ? powder : 1.0);
amount *= 0.5; amount *= 0.5;
fade *= 0.5; fade *= 0.5;
spread *= 0.5;
} }
return light; return light;
} }
// optical depth of the cloud over a point up to Reach above it, from Samples of the coarse density: under a lump and deep in a // optical depth of the cloud over a point up to Reach above it, from Samples of the coarse density: under a lump and deep in a
// thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud // thick cloud the sky light is held back, at the tops and the thin edges it is not, so the shaded sides take the form of the cloud.
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples) // Offset: where in its part of the reach each sample is taken (0-1), as in cloud_sunlight()
float cloud_sky_depth(int index, vec3 position, float height, float reach, int samples, float offset)
{ {
float depth = 0.0; float depth = 0.0;
for (int s = 0; s < samples; s++) for (int s = 0; s < samples; s++)
{ {
// up along the vertical: the height grows by the same distance (cloud_height bends only with the horizontal distance) // up along the vertical: the height grows by the same distance (cloud_height bends only with the horizontal distance)
float up = reach * (float(s) + 0.5) / float(samples); float up = reach * (float(s) + offset) / float(samples);
depth += cloud_layer_density(index, position + vec3(0.0, up, 0.0), height + up, false); depth += cloud_layer_density(index, position + vec3(0.0, up, 0.0), height + up, false);
} }
return depth * reach / float(samples); return depth * reach / float(samples);
} }
// how far along a ray of the view (world axes) the scenery reached at the farthest in the last frame, in the four texels of the
// depth pyramid around the ray: some 16 to 48 pixels each way at the level of 32 pixels, so what an edge of the scenery uncovers
// as the camera moves is marched before it shows. 0: the sky showed there, the ray wasn't in that view, or there's no pyramid.
// the depth is that of a projection with the far plane at infinity (near / w); longer by a margin for the camera moved since and
// the angle of the ray to the axis of the view changing over the texels
float scenery_reach(vec3 direction)
{
if (occlusion <= 0)
return 0.0;
vec4 clip = occlusion_projection_view * vec4(direction, 0.0);
if (clip.w <= 1e-3)
return 0.0;
vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
if (any(lessThan(coords, vec2(0.0))) || any(greaterThan(coords, vec2(1.0))))
return 0.0;
int level = occlusion - 1;
ivec2 levelsize = textureSize(occlusion_depth, level);
ivec2 base = ivec2(floor(coords * vec2(levelsize) - 0.5));
float farthest = 1.0;
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
farthest = min(farthest, texelFetch(occlusion_depth, clamp(base + ivec2(x, y), ivec2(0), levelsize - 1), level).r);
if (farthest <= 0.0)
return 0.0;
return projection[3][2] / farthest / clip.w * 1.1 + 2.0;
}
float interleaved_noise(vec2 pixel) float interleaved_noise(vec2 pixel)
{ {
return fract(52.9829189 * fract(dot(pixel, vec2(0.06711056, 0.00583715)))); return fract(52.9829189 * fract(dot(pixel, vec2(0.06711056, 0.00583715))));
@@ -142,6 +190,7 @@ void main()
vec3 direction; vec3 direction;
if (mode == 0) if (mode == 0)
{ {
texel = texel * max(stride, 1) + phase;
if (any(greaterThanEqual(texel, size))) if (any(greaterThanEqual(texel, size)))
return; return;
vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0; vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0;
@@ -169,12 +218,25 @@ void main()
int skysteps = (lightsteps >= 4) ? 2 : 1; int skysteps = (lightsteps >= 4) ? 2 : 1;
bool detail = quality.w > 0.5; bool detail = quality.w > 0.5;
float jitter = fract(interleaved_noise(vec2(texel)) + float(frame) * 0.618034); float jitter = fract(interleaved_noise(vec2(texel)) + float(frame) * 0.618034);
// where the samples of the light are taken in their steps: another sequence than the one of the samples of the view
float lightjitter = fract(interleaved_noise(vec2(texel) + vec2(37.0, 17.0)) + float(frame) * 0.754878);
{
float spread = 1.0;
for (int octave = 0; octave < CLOUD_SCATTER_OCTAVES; octave++)
{
cloud_scatter_phase[octave] = cloud_phase(cosangle, spread);
spread *= 0.5;
}
}
vec3 scattered = vec3(0.0); vec3 scattered = vec3(0.0);
float transmittance = 1.0; float transmittance = 1.0;
float depthsum = 0.0; float depthsum = 0.0;
float weightsum = 0.0; float weightsum = 0.0;
int layers = int(weather_camera.w); int layers = int(weather_camera.w);
// what the scenery hid of the ray in the last frame: the clouds past it aren't seen
float scenery = (mode == 0) ? scenery_reach(direction) : 0.0;
bool hidden = scenery > 0.0;
// the stretches of the volumetric layers along the ray, nearest first // the stretches of the volumetric layers along the ray, nearest first
vec2 segments[MAX_CLOUD_LAYERS]; vec2 segments[MAX_CLOUD_LAYERS];
@@ -214,6 +276,10 @@ void main()
int index = order[k]; int index = order[k];
cloud_layer_s layer = cloud_layers[index]; cloud_layer_s layer = cloud_layers[index];
vec2 segment = segments[k]; vec2 segment = segments[k];
// the scenery in front of the layer, and of the ones past it
if (scenery > 0.0 && scenery < segment.x)
break;
hidden = false;
float span = segment.y - segment.x; float span = segment.y - segment.x;
float thickness = layer.heights.y - layer.heights.x; float thickness = layer.heights.y - layer.heights.x;
int steps = clamp(int(float(maxsteps) * span / (thickness * 4.0)), 8, maxsteps); int steps = clamp(int(float(maxsteps) * span / (thickness * 4.0)), 8, maxsteps);
@@ -227,13 +293,15 @@ void main()
if (density <= 0.0) if (density <= 0.0)
continue; continue;
float powder = mix(1.0, 1.0 - exp(-density / max(layer.shape.y, 1e-6) * CLOUD_POWDER), powderview); float powder = mix(1.0, 1.0 - exp(-density / max(layer.shape.y, 1e-6) * CLOUD_POWDER), powderview);
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, cosangle, lightsteps, powder, detail) : 0.0; // every sample of the view takes the samples of its light at another place of their steps
float lightoffset = fract(lightjitter + float(s) * 0.754878);
float sunshare = (sunup > 0.0) ? cloud_sunlight(index, position, lightsteps, powder, detail, lightoffset) : 0.0;
// the light of the sky reaches inside through the cloud above, that of the ground through the cloud below: the base of a // the light of the sky reaches inside through the cloud above, that of the ground through the cloud below: the base of a
// thick cloud is darker than its top, and greyer, lit only by the ground and the sky at its sides // thick cloud is darker than its top, and greyer, lit only by the ground and the sky at its sides
float column = layer.shape.y * layer.shape.x; float column = layer.shape.y * layer.shape.x;
float top = cloud_layer_bounds(layer).y; float top = cloud_layer_bounds(layer).y;
float reach = clamp(top - height, 0.0, CLOUD_SKY_REACH); float reach = clamp(top - height, 0.0, CLOUD_SKY_REACH);
float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0)); float sky = exp(-CLOUD_SKY_FADE * cloud_sky_depth(index, position, height, reach, skysteps, fract(lightoffset + 0.569840)) - CLOUD_AMBIENT_FADE * column * max(top - height - reach, 0.0));
float below = column * max(height - layer.heights.x, 0.0); float below = column * max(height - layer.heights.x, 0.0);
vec3 light = sunlight * sunshare + (ambienttop * mix(CLOUD_SKY_FLOOR, 1.0, sky) + ambientbottom * exp(-CLOUD_AMBIENT_FADE * below)); vec3 light = sunlight * sunshare + (ambienttop * mix(CLOUD_SKY_FLOOR, 1.0, sky) + ambientbottom * exp(-CLOUD_AMBIENT_FADE * below));
float through = exp(-density * step); float through = exp(-density * step);
@@ -260,6 +328,9 @@ void main()
float t = crossing.x; float t = crossing.x;
if (t > maxdistance * 2.0) if (t > maxdistance * 2.0)
continue; continue;
if (scenery > 0.0 && scenery < t)
continue;
hidden = false;
vec3 position = direction * t; vec3 position = direction * t;
// the slant of the ray through the sheet where it crosses it, the earth curving away // the slant of the ray through the sheet where it crosses it, the earth curving away
float slope = max(abs(direction.y - (1.0 - direction.y * direction.y) * t / CLOUD_EARTH_RADIUS), 0.03); float slope = max(abs(direction.y - (1.0 - direction.y * direction.y) * t / CLOUD_EARTH_RADIUS), 0.03);
@@ -289,6 +360,11 @@ void main()
scattered = vec3(0.0); scattered = vec3(0.0);
transmittance = 1.0; transmittance = 1.0;
} }
if (hidden)
{
scattered = vec3(0.0);
transmittance = -1.0;
}
imageStore(clouds_color, texel, vec4(scattered, transmittance)); imageStore(clouds_color, texel, vec4(scattered, transmittance));
if (mode == 0) if (mode == 0)
imageStore(clouds_depth, texel, vec4(span_distance)); imageStore(clouds_depth, texel, vec4(span_distance));

View File

@@ -3,7 +3,14 @@
// the clouds of the main view smoothed over frames: the image of this frame (clouds_march.comp, ray started at another // the clouds of the main view smoothed over frames: the image of this frame (clouds_march.comp, ray started at another
// distance every frame) blended into the one of the previous frames, found where the clouds were by the distance of the // distance every frame) blended into the one of the previous frames, found where the clouds were by the distance of the
// clouds, the rotation and the move of the camera. the history is held to the range of the neighbourhood of this frame, // clouds, the rotation and the move of the camera. the history is held to the range of the neighbourhood of this frame,
// so moving clouds and a turning camera don't leave trails // so moving clouds and a turning camera don't leave trails. the pixels the scenery hid (alpha -1 from clouds_march.comp, which
// didn't march them) keep what the history holds where they were, as it is: when the scenery uncovers them there's something to
// blend into.
// with a part of the pixels marched a frame (stride over 1: the ones at phase in the blocks of stride x stride pixels, the others
// hold what they were given in their turn, some frames ago and for the view of that frame) the pixels of this frame are blended
// in as they are; the others take the pixels of this frame next to them, evened out, with a smaller share, so every pixel gets
// something of every frame and the turns of the pixels don't show as a pattern. the history is held to the range of the pixels
// of this frame around
#define LAYOUT_COMPUTE 1 #define LAYOUT_COMPUTE 1
#include "common.glsl" #include "common.glsl"
@@ -27,6 +34,9 @@ layout(push_constant) uniform kernel_constants
vec3 previous_camera; vec3 previous_camera;
// share of this frame in the result; 1 when there is no history // share of this frame in the result; 1 when there is no history
float blend; float blend;
// the pixels marched in this frame: the ones at phase in the blocks of stride x stride pixels (1: all of them)
int stride;
ivec2 phase;
}; };
void main() void main()
@@ -35,34 +45,80 @@ void main()
if (any(greaterThanEqual(texel, size))) if (any(greaterThanEqual(texel, size)))
return; return;
vec4 current = texelFetch(current_color, texel, 0); vec4 current = texelFetch(current_color, texel, 0);
bool hidden = current.a < 0.0;
// no clouds, where there's nothing to keep
if (hidden)
current = vec4(0.0, 0.0, 0.0, 1.0);
if (blend >= 1.0) if (blend >= 1.0)
{ {
imageStore(result, texel, current); imageStore(result, texel, current);
return; return;
} }
// the range of the neighbourhood // the pixels of this frame around: the one the pixel is, or the one before it, and those a stride away on both sides (a
vec4 low = current; // pixel between two of them reaches as far both ways). their range holds the history, and the nearest stands for a pixel of
vec4 high = current; // another frame which has no history
for (int y = -1; y <= 1; y++) // the share of the pixels of this frame next to a pixel of another frame, of the share of a pixel of this frame
for (int x = -1; x <= 1; x++) const float BESIDE_SHARE = 0.35;
{ int spacing = max(stride, 1);
vec4 neighbour = texelFetch(current_color, clamp(texel + ivec2(x, y), ivec2(0), size - 1), 0); ivec2 past = (texel + spacing - phase) % spacing;
low = min(low, neighbour); bool fresh = all(equal(past, ivec2(0)));
high = max(high, neighbour); ivec2 base = texel - past;
} vec4 low = vec4(1e30);
vec4 high = vec4(-1e30);
vec4 nearest = vec4(0.0, 0.0, 0.0, 1.0);
float nearestdistance = 1e30;
bool ranged = false;
// the pixels of this frame on both sides of a pixel of another frame, which is half way between them
vec4 beside = vec4(0.0);
float besidecount = 0.0;
if (!hidden)
{
for (int y = -1; y <= (past.y == 0 ? 1 : 2); y++)
for (int x = -1; x <= (past.x == 0 ? 1 : 2); x++)
{
ivec2 place = base + ivec2(x, y) * spacing;
if (any(lessThan(place, ivec2(0))) || any(greaterThanEqual(place, size)))
continue;
vec4 neighbour = texelFetch(current_color, place, 0);
if (neighbour.a < 0.0)
continue;
low = min(low, neighbour);
high = max(high, neighbour);
ranged = true;
vec2 away = vec2(place - texel);
float distance2 = dot(away, away);
if (distance2 < nearestdistance)
{
nearestdistance = distance2;
nearest = neighbour;
}
if (x >= 0 && y >= 0 && x <= min(past.x, 1) && y <= min(past.y, 1))
{
beside += neighbour;
besidecount += 1.0;
}
}
// what a pixel of another frame holds is of the view of that frame
if (!fresh)
current = (besidecount > 0.0) ? beside / besidecount : nearest;
}
vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0; vec2 ndc = (vec2(texel) + 0.5) / vec2(size) * 2.0 - 1.0;
vec4 near = inverse_projection * vec4(ndc, 1.0, 1.0); vec4 near = inverse_projection * vec4(ndc, 1.0, 1.0);
vec3 direction = normalize(mat3(view_to_world) * normalize(near.xyz / near.w)); vec3 direction = normalize(mat3(view_to_world) * normalize(near.xyz / near.w));
float span_distance = texelFetch(current_depth, texel, 0).r; float span_distance = texelFetch(current_depth, texel, 0).r;
vec3 position = direction * span_distance - previous_camera; vec3 position = direction * span_distance - previous_camera;
vec4 clip = previous_projection_view * vec4(position, 1.0); vec4 clip = previous_projection_view * vec4(position, 1.0);
float share = blend; float share = hidden ? 0.0 : blend * (fresh ? 1.0 : BESIDE_SHARE);
vec4 history = current; vec4 history = current;
if (clip.w > 0.0) if (clip.w > 0.0)
{ {
vec2 coords = clip.xy / clip.w * 0.5 + 0.5; vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
if (all(greaterThanEqual(coords, vec2(0.0))) && all(lessThanEqual(coords, vec2(1.0)))) if (all(greaterThanEqual(coords, vec2(0.0))) && all(lessThanEqual(coords, vec2(1.0))))
history = clamp(textureLod(history_color, coords, 0.0), low, high); {
history = textureLod(history_color, coords, 0.0);
if (ranged)
history = clamp(history, low, high);
}
else else
share = 1.0; share = 1.0;
} }

View File

@@ -8,7 +8,10 @@
// the points from a place of its own, so the volume doesn't flicker as a whole, and the blending smooths what a cell holds over // the points from a place of its own, so the volume doesn't flicker as a whole, and the blending smooths what a cell holds over
// its whole size: a beam doesn't swim over the grid of the cells as the camera moves and turns. what the cell takes the lights // its whole size: a beam doesn't swim over the grid of the cells as the camera moves and turns. what the cell takes the lights
// from doesn't depend on the point (the clusters of the middle of the cell) // from doesn't depend on the point (the clusters of the middle of the cell)
// rgb: light scattered towards the camera per metre, a: extinction per metre // rgb: light scattered towards the camera per metre, a: extinction per metre.
// gfx.weather.occlusion: the cells behind everything the scenery held around them in the last frame (the depth pyramid of the
// culling of that frame) aren't seen through, nothing reads them: they take what the history holds where they were as it is,
// without working out the fog anew, so there's something to blend into when the scenery uncovers them
#define LAYOUT_COMPUTE 1 #define LAYOUT_COMPUTE 1
#include "common.glsl" #include "common.glsl"
@@ -19,6 +22,9 @@ layout(local_size_x = WEATHER_GROUP_SIZE, local_size_y = WEATHER_GROUP_SIZE, loc
layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image3D fog_scattering; layout(rgba16f, set = COMPUTE_SET, binding = 0) uniform writeonly image3D fog_scattering;
layout(set = COMPUTE_SET, binding = 1) uniform sampler3D fog_history; layout(set = COMPUTE_SET, binding = 1) uniform sampler3D fog_history;
// the depth pyramid of the main view of the last frame (hiz_build.comp: the farthest depth of what a texel covers, reversed, 0 the
// sky); read only with occlusion.x set
layout(set = COMPUTE_SET, binding = 3) uniform sampler2D occlusion_depth;
layout(set = PASS_SET, binding = SHADOW_TEX) uniform sampler2DArrayShadow shadowmap; layout(set = PASS_SET, binding = SHADOW_TEX) uniform sampler2DArrayShadow shadowmap;
layout(set = PASS_SET, binding = HEADLIGHT_TEX) uniform sampler2D headlightmap; layout(set = PASS_SET, binding = HEADLIGHT_TEX) uniform sampler2D headlightmap;
@@ -45,6 +51,11 @@ layout(std140, set = COMPUTE_SET, binding = 2) uniform kernel_block
vec2 previous_slicing; vec2 previous_slicing;
// where in its cell the cell is sampled this frame across the view (0-1), as slicing.w is along the depth // where in its cell the cell is sampled this frame across the view (0-1), as slicing.w is along the depth
vec2 jitter; vec2 jitter;
// world axes to the clip space of the frame the depth pyramid was made in
mat4 occlusion_projection_view;
// x: the level of the depth pyramid looked at for what the scenery hides, plus 1 (0: nothing is taken for hidden), y: the near
// plane of the projection the depth is of (a depth d is a surface y / d away along the axis of the view)
vec4 occlusion;
}; };
// ray of the view space through a point of the view (0-1), one metre of the depth long // ray of the view space through a point of the view (0-1), one metre of the depth long
@@ -225,11 +236,67 @@ vec3 lamp_segment(light_s lamp, light_s cone, vec3 start, vec3 direction, float
return sum * (0.5 * (last - first) / (reach * max(span, 1e-4))); return sum * (0.5 * (last - first) / (reach * max(span, 1e-4)));
} }
// slices past the surfaces of the scenery which are still worked out: the fog of a surface is read between two slices, each
// holding the cells up to it (fog_integrate.comp, fog_volume.glsl), with one to spare
const float FOG_HIDDEN_SLICES = 2.0;
// whether all the scenery around the column of a cell was nearer than the cell in the last frame, by the nine texels of the depth
// pyramid around the column. their level is chosen for the size of the cells (vulkan14_renderer::render_weather_view()): the fog
// of a pixel is read from the columns two cells around it, each smoothed with the ones next to it, so a column counts for the
// pixels three and a half cells away, and the texels reach at least that far. the surfaces are taken for farther by a margin, for
// the camera moved and turned since
bool cell_hidden(ivec3 cell)
{
if (occlusion.x < 0.5)
return false;
vec3 direction = mat3(view_to_world) * view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy));
vec4 clip = occlusion_projection_view * vec4(direction, 0.0);
if (clip.w <= 1e-3)
return false;
vec2 coords = clip.xy / clip.w * 0.5 + 0.5;
if (any(lessThan(coords, vec2(0.0))) || any(greaterThan(coords, vec2(1.0))))
return false;
int level = int(occlusion.x) - 1;
ivec2 levelsize = textureSize(occlusion_depth, level);
ivec2 middle = ivec2(coords * vec2(levelsize));
float farthest = 1.0;
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++)
farthest = min(farthest, texelFetch(occlusion_depth, clamp(middle + ivec2(x, y), ivec2(0), levelsize - 1), level).r);
if (farthest <= 0.0)
return false;
return slice_depth(float(cell.z) - FOG_HIDDEN_SLICES) > occlusion.y / farthest * 1.1 + 1.0;
}
// the cell in the fog of the previous frame, where its middle was then (0-1 on each axis); false: it wasn't in that view
bool cell_history(ivec3 cell, out vec3 previous)
{
vec3 centre = mat3(view_to_world) * (view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy)) * slice_depth(float(cell.z) + 0.5));
vec4 clip = previous_projection_view * vec4(centre - previous_camera, 1.0);
previous = vec3(0.0);
if (clip.w <= 0.0)
return false;
float previousslice = log(max((clip.w + FOG_VOLUME_BIAS) / (previous_slicing.x + FOG_VOLUME_BIAS), 1e-6)) * previous_slicing.y;
previous = vec3(clip.xy / clip.w * 0.5 + 0.5, previousslice / float(grid.z));
return all(greaterThanEqual(previous, vec3(0.0))) && all(lessThanEqual(previous, vec3(1.0)));
}
void main() void main()
{ {
ivec3 cell = ivec3(gl_GlobalInvocationID); ivec3 cell = ivec3(gl_GlobalInvocationID);
if (any(greaterThanEqual(cell, grid))) if (any(greaterThanEqual(cell, grid)))
return; return;
// the previous frame, where the middle of the cell was then: the cells of the history hold what the cells were on the whole,
// not at the point they were sampled at
float share = options.x;
bool hidden = cell_hidden(cell);
vec3 previous = vec3(0.0);
bool found = ((share < 1.0) || hidden) && cell_history(cell, previous);
if (hidden && found)
{
imageStore(fog_scattering, cell, textureLod(fog_history, previous, 0.0));
return;
}
vec3 offset = cell_offset(cell); vec3 offset = cell_offset(cell);
vec2 coords = (vec2(cell.xy) + offset.xy) / vec2(grid.xy); vec2 coords = (vec2(cell.xy) + offset.xy) / vec2(grid.xy);
float depth = slice_depth(float(cell.z) + offset.z); float depth = slice_depth(float(cell.z) + offset.z);
@@ -306,21 +373,8 @@ void main()
result = vec4(light * fog_haze.w * extinction, extinction); result = vec4(light * fog_haze.w * extinction, extinction);
} }
// the previous frame, where the middle of the cell was then: the cells of the history hold what the cells were on the whole, if ((share < 1.0) && found)
// not at the point they were sampled at result = mix(textureLod(fog_history, previous, 0.0), result, share);
float share = options.x;
if (share < 1.0)
{
vec3 centre = mat3(view_to_world) * (view_ray((vec2(cell.xy) + 0.5) / vec2(grid.xy)) * slice_depth(float(cell.z) + 0.5));
vec4 clip = previous_projection_view * vec4(centre - previous_camera, 1.0);
if (clip.w > 0.0)
{
float previousslice = log(max((clip.w + FOG_VOLUME_BIAS) / (previous_slicing.x + FOG_VOLUME_BIAS), 1e-6)) * previous_slicing.y;
vec3 previous = vec3(clip.xy / clip.w * 0.5 + 0.5, previousslice / float(grid.z));
if (all(greaterThanEqual(previous, vec3(0.0))) && all(lessThanEqual(previous, vec3(1.0))))
result = mix(textureLod(fog_history, previous, 0.0), result, share);
}
}
if (any(isnan(result))) if (any(isnan(result)))
result = vec4(0.0); result = vec4(0.0);
imageStore(fog_scattering, cell, result); imageStore(fog_scattering, cell, result);

View File

@@ -2246,6 +2246,8 @@ global_settings::ConfigParse_gfx_sky( cParser &Parser, std::string_view const To
else if( Token == "gfx.fog.shadows" ) { readswitch( gfx_fog_shadows ); } else if( Token == "gfx.fog.shadows" ) { readswitch( gfx_fog_shadows ); }
else if( Token == "gfx.fog.temporal" ) { readswitch( gfx_fog_temporal ); } else if( Token == "gfx.fog.temporal" ) { readswitch( gfx_fog_temporal ); }
else if( Token == "gfx.fog.temporal.share" ) { readvalue( gfx_fog_temporal_share, 0.02f, 1.f ); } else if( Token == "gfx.fog.temporal.share" ) { readvalue( gfx_fog_temporal_share, 0.02f, 1.f ); }
else if( Token == "gfx.weather.occlusion" ) { readswitch( gfx_weather_occlusion ); }
else if( Token == "gfx.clouds.interleave" ) { readswitch( gfx_clouds_interleave ); }
else if( Token == "gfx.clouds" ) { else if( Token == "gfx.clouds" ) {
// no, or the quality: low, medium (yes), high, ultra // no, or the quality: low, medium (yes), high, ultra
Parser.getTokens( 1 ); Parser.getTokens( 1 );
@@ -2667,6 +2669,8 @@ global_settings::export_as_text( std::ostream &Output ) const {
export_as_text( Output, "gfx.fog.shadows", gfx_fog_shadows ); export_as_text( Output, "gfx.fog.shadows", gfx_fog_shadows );
export_as_text( Output, "gfx.fog.temporal", gfx_fog_temporal ); export_as_text( Output, "gfx.fog.temporal", gfx_fog_temporal );
export_as_text( Output, "gfx.fog.temporal.share", gfx_fog_temporal_share ); export_as_text( Output, "gfx.fog.temporal.share", gfx_fog_temporal_share );
export_as_text( Output, "gfx.weather.occlusion", gfx_weather_occlusion );
export_as_text( Output, "gfx.clouds.interleave", gfx_clouds_interleave );
export_as_text( Output, "gfx.clouds", std::string{ gfx_clouds == 1 ? "low" : gfx_clouds == 2 ? "medium" : gfx_clouds == 3 ? "high" : gfx_clouds == 4 ? "ultra" : "no" } ); export_as_text( Output, "gfx.clouds", std::string{ gfx_clouds == 1 ? "low" : gfx_clouds == 2 ? "medium" : gfx_clouds == 3 ? "high" : gfx_clouds == 4 ? "ultra" : "no" } );
export_as_text( Output, "gfx.clouds.distance", gfx_clouds_distance ); export_as_text( Output, "gfx.clouds.distance", gfx_clouds_distance );
export_as_text( Output, "gfx.clouds.shadows", gfx_clouds_shadows ); export_as_text( Output, "gfx.clouds.shadows", gfx_clouds_shadows );

View File

@@ -628,6 +628,10 @@ struct global_settings {
bool gfx_clouds_shadows { true }; bool gfx_clouds_shadows { true };
int gfx_clouds_shadows_size { 512 }; int gfx_clouds_shadows_size { 512 };
float gfx_clouds_shadows_range { 16000.f }; float gfx_clouds_shadows_range { 16000.f };
// the clouds and the cells of the fog volume the scenery hid in the last frame aren't worked out anew (the vulkan renderer)
bool gfx_weather_occlusion { true };
// a quarter of the pixels of the clouds of the main view is marched a frame, the image made of the last ones (the vulkan renderer)
bool gfx_clouds_interleave { true };
// stars from the catalogue, and their brightness // stars from the catalogue, and their brightness
bool gfx_stars { true }; bool gfx_stars { true };
float gfx_stars_brightness { 1.f }; float gfx_stars_brightness { 1.f };