From 28716ae1149148c295313ec85a3600c1fe4893ec Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 15:54:28 +0100 Subject: [PATCH 1/4] Rain windscreen: the cells of the drops and rivulets worked out once a frame The glass with rain worked out, in every pixel, the hashes and the life cycles of the 9 drop cells around it, of the rivulet cells of its 5 layers and of those the drops are swept by. A compute pass (rain_cells.comp) now does it once a frame for every cell into a buffer (rain_cells.glsl: RAIN_CELLS_SSBO in the pass set of Vulkan 1.4, binding 18 in OpenGL 4.6); the glass only reads it, and asks the wipers only for the drops the pixel lies on. The hashes are the same, so the drops and the rivulets are where they were. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- gl/glsl_common.cpp | 1 + gl/ubo.h | 8 ++ rendering/opengl46/opengl46renderer.cpp | 4 + rendering/opengl46/opengl46water.cpp | 32 +++++ rendering/opengl46/opengl46water.h | 6 + rendering/vulkan14/vulkan14bindings.h | 5 +- rendering/vulkan14/vulkan14frame.cpp | 2 + rendering/vulkan14/vulkan14scene.cpp | 3 + rendering/vulkan14/vulkan14shaders.cpp | 1 + rendering/vulkan14/vulkan14water.cpp | 52 ++++++++ rendering/vulkan14/vulkan14water.h | 11 ++ shaders/ogl46/common.glsl | 1 + shaders/ogl46/mat_rain_windscreen.frag | 168 ++++++++++-------------- shaders/ogl46/rain_cells.comp | 131 ++++++++++++++++++ shaders/ogl46/rain_cells.glsl | 67 ++++++++++ shaders/vk14/common.glsl | 3 +- shaders/vk14/mat_rain_windscreen.frag | 168 ++++++++++-------------- shaders/vk14/rain_cells.comp | 136 +++++++++++++++++++ shaders/vk14/rain_cells.glsl | 67 ++++++++++ 19 files changed, 674 insertions(+), 192 deletions(-) create mode 100644 shaders/ogl46/rain_cells.comp create mode 100644 shaders/ogl46/rain_cells.glsl create mode 100644 shaders/vk14/rain_cells.comp create mode 100644 shaders/vk14/rain_cells.glsl diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index f6f3df06d..981d7b05e 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -53,6 +53,7 @@ void gl::glsl_common_setup() "#define WATER_MAP " + std::to_string(WATER_MAP) + "\n" + "#define WATER_GROUP_SIZE " + std::to_string(WATER_GROUP_SIZE) + "\n" + "#define WATER_SSBO_BINDING " + std::to_string(WATER_SSBO_BINDING) + "\n" + + "#define RAIN_CELLS_SSBO_BINDING " + std::to_string(RAIN_CELLS_SSBO_BINDING) + "\n" + "#define WATER_UBO_BINDING " + std::to_string(WATER_UBO_BINDING) + "\n" + "#define WATER_MASK_TEX " + std::to_string(WATER_MASK_TEX) + "\n" + "#define INSTANCE_COUNTER_HEADER " + std::to_string(INSTANCE_COUNTER_HEADER) + "\n" + diff --git a/gl/ubo.h b/gl/ubo.h index 3f1efff28..564127d43 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -706,6 +706,14 @@ namespace gl // of the step of the simulation (water_ubs) and the texture unit of the mask of the wipers the map is baked from. the storage // point is used by nothing else, bound with the colour passes const GLuint WATER_SSBO_BINDING = 16; + // the drops and the rivulets of the cells of the glasses with rain (mat_rain_windscreen, rain_cells.comp, rain_cells.glsl): worked + // out once a frame for every cell, so a glass only reads them. RAIN_CELLS_COUNT uvec4, as rain_cells.glsl lays them out + const GLuint RAIN_CELLS_SSBO_BINDING = 18; + const size_t RAIN_DROP_CELLS = 512; + const size_t RAIN_RIV_CELLS = 128; + const size_t RAIN_RIV_LAYERS = 5; + const size_t RAIN_CELLS_COUNT = RAIN_DROP_CELLS * RAIN_DROP_CELLS + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS; + const size_t RAIN_CELLS_GROUP_SIZE = 64; const GLuint WATER_UBO_BINDING = 6; const size_t WATER_MASK_TEX = MAX_TEXTURES + HELPER_TEXTURES + 13; // the parts of the storage of the water (std430, GLSL water_ssbo), offsets in bytes: the header of every glass (vec4), the diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 942a3a4ab..6584dde68 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -1710,7 +1710,11 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) scene_ubo->bind_uniform(); // the water on the windscreens of the cab: a step of its simulation once a frame, its storage for the glasses of every view if (vp.main) + { m_windscreenwater.update(Timer::GetDeltaTime()); + // the drops and the rivulets of the glasses with rain, for the time of the block of the scene + m_windscreenwater.update_rain_cells(scene_ubs.time, scene_ubs.rain_params.x); + } m_windscreenwater.bind(); if (Global.bWireFrame) diff --git a/rendering/opengl46/opengl46water.cpp b/rendering/opengl46/opengl46water.cpp index 805ae1bd8..e9052c53b 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -50,6 +50,38 @@ void opengl46_windscreen_water::bind() const { if (m_storage) m_storage->bind_base(); + if (m_raincells) + m_raincells->bind_base(); +} + +void opengl46_windscreen_water::update_rain_cells(float const Time, float const Rain) +{ + if (Rain <= 0.f) + return; + if (false == m_raincelltried) + { + m_raincelltried = true; + try + { + gl::shader shader("rain_cells.comp"); + m_raincellprogram = std::make_unique(std::vector>{shader}); + m_raincells = std::make_unique(static_cast(gl::RAIN_CELLS_COUNT * 16), gl::RAIN_CELLS_SSBO_BINDING); + } + catch (gl::shader_exception const &e) + { + ErrorLog("rain cells: can't use rain_cells.comp, the glasses with rain stay dry: " + std::string(e.what())); + m_raincellprogram.reset(); + } + } + if ((!m_raincellprogram) || (!m_raincells)) + return; + // the glasses of the frame before read the cells, this one writes them + ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + m_raincells->bind_base(); + ::glProgramUniform1f(*m_raincellprogram, 0, Time); + m_raincellprogram->dispatch(static_cast((gl::RAIN_CELLS_COUNT + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE)); + ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + gl::program::unbind(); } void opengl46_windscreen_water::gpu_setup(std::size_t const Slot, std::vector const &Axes) diff --git a/rendering/opengl46/opengl46water.h b/rendering/opengl46/opengl46water.h index 851464e80..7e5a20d29 100644 --- a/rendering/opengl46/opengl46water.h +++ b/rendering/opengl46/opengl46water.h @@ -27,6 +27,9 @@ class opengl46_windscreen_water : public gfx::windscreen_water // the storage bound to its point for the glasses drawn next (the bindings are kept by every context of its own) void bind() const; + // the drops and the rivulets of the cells of the glasses with rain (mat_rain_windscreen, rain_cells.comp) worked out for the + // time of the frame (scene_ubs.time), once a frame before the passes; nothing while it doesn't rain + void update_rain_cells(float Time, float Rain); protected: bool gpu_init() override; @@ -52,4 +55,7 @@ class opengl46_windscreen_water : public gfx::windscreen_water std::array, pass::count> m_programs; std::unique_ptr m_storage; std::unique_ptr m_block; + std::unique_ptr m_raincellprogram; + std::unique_ptr m_raincells; + bool m_raincelltried{false}; }; diff --git a/rendering/vulkan14/vulkan14bindings.h b/rendering/vulkan14/vulkan14bindings.h index 0e98ccc88..c671e372f 100644 --- a/rendering/vulkan14/vulkan14bindings.h +++ b/rendering/vulkan14/vulkan14bindings.h @@ -86,7 +86,10 @@ constexpr std::uint32_t SKY_OCCLUSION_HEIGHT_TEX = 31; constexpr std::uint32_t SKY_OCCLUSION_TEX = 32; // the probe of the cab drawn last, a small level of its albedo, which the drawing of the probe reads to tell where the windows are constexpr std::uint32_t CAB_PROBE_MASK_TEX = 33; -constexpr std::uint32_t PASS_BINDING_COUNT = 34; +// the drops and the rivulets of the cells of the glasses with rain (mat_rain_windscreen, rain_cells.comp): worked out once a frame +// for every cell, so a glass only reads them +constexpr std::uint32_t RAIN_CELLS_SSBO = 34; +constexpr std::uint32_t PASS_BINDING_COUNT = 35; // the offsets of the bindings above with dynamic offsets, in the order of the bindings constexpr std::uint32_t PASS_DYNAMIC_OFFSETS = 6; // the textures of a material take the bindings of their units (#pragma texture(name, unit, ...)), 0 .. MAX_TEXTURES - 1 diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index e7f1ffb84..b619e5690 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1046,6 +1046,8 @@ void vulkan14_renderer::render_scene() auto &scene{ctx.scene}; // the water on the windscreens of the cab: a step of its simulation once a frame, before the passes which draw the glasses scene.water.update(Timer::GetDeltaTime()); + // the drops and the rivulets of the glasses with rain, for the time the block of the scene gets (scene_ubs.time) + scene.water.update_rain_cells(static_cast(Timer::GetTime()), glm::saturate(Global.Overcast - 1.f)); if (scene.targets.size() < m_viewports.size()) { scene.targets.resize(m_viewports.size()); diff --git a/rendering/vulkan14/vulkan14scene.cpp b/rendering/vulkan14/vulkan14scene.cpp index df80d6df2..77319a6ca 100644 --- a/rendering/vulkan14/vulkan14scene.cpp +++ b/rendering/vulkan14/vulkan14scene.cpp @@ -155,6 +155,7 @@ VkDescriptorType pass_binding_type(std::uint32_t const Binding) case CLUSTER_INDEX_SSBO: case SKY_SSBO: case WATER_SSBO: + case RAIN_CELLS_SSBO: case CAB_PROBE_SSBO: return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; case ATMOSPHERE_UBO: @@ -1065,6 +1066,7 @@ void vulkan14_renderer::write_pass_sets(vk14::instance_cull *Cull) VkDescriptorBufferInfo const weatherblock{ctx.ring.handle(), 0, offsetof(gl::weather_ubs, fog_horizon)}; VkDescriptorBufferInfo const horizonblock{scene.weather_horizon.handle(), 0, sizeof(glm::vec4) * gl::FOG_HORIZON_SAMPLES}; VkDescriptorBufferInfo const water{scene.water.storage(), 0, VK_WHOLE_SIZE}; + VkDescriptorBufferInfo const raincells{scene.water.rain_cells(), 0, VK_WHOLE_SIZE}; VkDescriptorBufferInfo const cabprobe{scene.cabprobe_bounce.handle(), 0, sizeof(gl::cab_probe_data)}; VkDescriptorImageInfo const headlightsinfo{scene.headlights_sampler, scene.headlights_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL}; std::vector writes; @@ -1100,6 +1102,7 @@ void vulkan14_renderer::write_pass_sets(vk14::instance_cull *Cull) write(handle, WEATHER_UBO, &weatherblock, nullptr); write(handle, WEATHER_HORIZON_UBO, &horizonblock, nullptr); write(handle, WATER_SSBO, &water, nullptr); + write(handle, RAIN_CELLS_SSBO, &raincells, nullptr); write(handle, HEADLIGHT_TEX, nullptr, &headlightsinfo); write(handle, SHADOW_TEX, nullptr, image(ctx.sampling.shadow, views[SHADOW_TEX])); write(handle, ENV_TEX, nullptr, image(ctx.sampling.clamp_linear, views[ENV_TEX])); diff --git a/rendering/vulkan14/vulkan14shaders.cpp b/rendering/vulkan14/vulkan14shaders.cpp index 08d9e7797..a910e2ab9 100644 --- a/rendering/vulkan14/vulkan14shaders.cpp +++ b/rendering/vulkan14/vulkan14shaders.cpp @@ -858,6 +858,7 @@ std::string binding_preamble(bool const Raytracing) define("SKY_OCCLUSION_HEIGHT_TEX", SKY_OCCLUSION_HEIGHT_TEX); define("SKY_OCCLUSION_TEX", SKY_OCCLUSION_TEX); define("CAB_PROBE_MASK_TEX", CAB_PROBE_MASK_TEX); + define("RAIN_CELLS_SSBO", RAIN_CELLS_SSBO); define("PASS_BINDING_COUNT", PASS_BINDING_COUNT); define("MATERIAL_TEXTURE_FIRST", MATERIAL_TEXTURE_FIRST); define("CL_CLUSTER_UBO", CL_CLUSTER_UBO); diff --git a/rendering/vulkan14/vulkan14water.cpp b/rendering/vulkan14/vulkan14water.cpp index 4d323b333..5c32c2974 100644 --- a/rendering/vulkan14/vulkan14water.cpp +++ b/rendering/vulkan14/vulkan14water.cpp @@ -49,6 +49,12 @@ bool windscreen_water::init(VkDevice const Device, pipeline_library &Pipelines, m_ring = &Ring; m_alignment = std::max(Alignment, 16); m_host = std::move(Host); + m_raincells = std::make_shared(); + if (false == m_raincells->create(Memory, static_cast(gl::RAIN_CELLS_COUNT * 16), storage_usage, memory_kind::device, "rain cells")) + { + m_raincells.reset(); + return false; + } m_storage = std::make_shared(); m_full = m_storage->create(Memory, static_cast(gl::WATER_STORAGE_SIZE), storage_usage, memory_kind::device, "windscreen water"); if (false == m_full) @@ -75,6 +81,9 @@ void windscreen_water::destroy() { kernel.destroy(m_device); } + m_raincellkernel.destroy(m_device); + m_raincelltried = false; + m_raincells.reset(); m_storage.reset(); m_device = VK_NULL_HANDLE; m_cleared = false; @@ -164,6 +173,49 @@ void windscreen_water::gpu_setup(std::size_t const Slot, std::vector m_deletions->push(m_uploads->current_value(), [staging]() {}); } +void windscreen_water::update_rain_cells(float const Time, float const Rain) +{ + if ((m_device == VK_NULL_HANDLE) || (false == bool(m_raincells)) || (Rain <= 0.f) || (false == bool(m_host.commands))) + { + return; + } + if (false == m_raincelltried) + { + m_raincelltried = true; + try + { + if (false == m_raincellkernel.create(m_device, *m_pipelines, m_shaders->get("rain_cells.comp"), m_passlayout, {}, "rain_cells.comp")) + { + ErrorLog("rain cells: no compute pipeline of rain_cells.comp, the glasses with rain stay dry"); + } + } + catch (gfx::shader_exception const &e) + { + ErrorLog("rain cells: can't use rain_cells.comp: " + std::string(e.what())); + } + } + if (false == bool(m_raincellkernel)) + { + return; + } + auto const commands{m_host.commands()}; + if (commands == nullptr) + { + return; + } + // the glasses of the frames before read the cells, this one writes them + memory_barrier(commands, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, + VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT); + struct constants + { + float time; + float pad[3]; + } const data{Time, {0.f, 0.f, 0.f}}; + m_raincellkernel.dispatch(commands, {buffer_descriptor(m_raincells->handle(), 0, VK_WHOLE_SIZE)}, data, + static_cast((gl::RAIN_CELLS_COUNT + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE)); + compute_barrier(commands); +} + ring_slice windscreen_water::block(gl::water_ubs const &Block) { auto const slice{m_ring->allocate(sizeof(gl::water_ubs), m_alignment)}; diff --git a/rendering/vulkan14/vulkan14water.h b/rendering/vulkan14/vulkan14water.h index 2b24bbf9d..901e241bd 100644 --- a/rendering/vulkan14/vulkan14water.h +++ b/rendering/vulkan14/vulkan14water.h @@ -50,6 +50,14 @@ class windscreen_water : public gfx::windscreen_water { return m_storage ? m_storage->handle() : VK_NULL_HANDLE; } + // the drops and the rivulets of the cells of the glasses with rain (RAIN_CELLS_SSBO, rain_cells.comp), the pass sets hold it + VkBuffer rain_cells() const + { + return m_raincells ? m_raincells->handle() : VK_NULL_HANDLE; + } + // the cells worked out for the time of the frame (the time of the block of the scene), with the commands of the frame before + // its passes; nothing while it doesn't rain + void update_rain_cells(float Time, float Rain); protected: bool gpu_init() override; @@ -89,6 +97,9 @@ class windscreen_water : public gfx::windscreen_water host m_host; std::array m_kernels; std::shared_ptr m_storage; + std::shared_ptr m_raincells; + compute_kernel m_raincellkernel; + bool m_raincelltried{false}; bool m_full{false}; // the storage is as large as the simulation needs (else a stand-in, the glasses make their water up) bool m_cleared{false}; }; diff --git a/shaders/ogl46/common.glsl b/shaders/ogl46/common.glsl index 4ba306c3f..01bdfab85 100644 --- a/shaders/ogl46/common.glsl +++ b/shaders/ogl46/common.glsl @@ -62,6 +62,7 @@ #define WATER_MAP 256 #define WATER_GROUP_SIZE 64 #define WATER_SSBO_BINDING 16 +#define RAIN_CELLS_SSBO_BINDING 18 #define WATER_UBO_BINDING 6 #define WATER_MASK_TEX 25 #define INSTANCE_COUNTER_HEADER 8 diff --git a/shaders/ogl46/mat_rain_windscreen.frag b/shaders/ogl46/mat_rain_windscreen.frag index ec7fdc6a3..e4e2de711 100644 --- a/shaders/ogl46/mat_rain_windscreen.frag +++ b/shaders/ogl46/mat_rain_windscreen.frag @@ -18,6 +18,10 @@ // The trail right above the head is taken from the sprite part that touches the head, then it blends // into the trail pattern that is fixed to the glass (beads stay where they were left). // +// Speed: the hashes and the life cycles of the cells (which drop a cell holds now, where, how big; where the head of a rivulet is) +// are worked out once a frame for every cell by rain_cells.comp (rain_cells.glsl), not here for the 9 cells around every pixel; +// the wipers are asked only for the drops the pixel lies on. the look is the same. +// // Shading: the grey atlas (bright rim, dark middle) is read once per pixel, only for the drop that wins the // pixel (it used to be read for all 9 neighbours). Micro droplets are shaded analytically. // @@ -64,6 +68,7 @@ layout(location = 1) out vec4 out_motion; #include "apply_fog.glsl" #include "tonemapping.glsl" #include "random.glsl" +#include "rain_cells.glsl" layout(binding = 0) uniform sampler2D diffuse; layout(binding = 1) uniform sampler2D raindropsatlas; @@ -83,14 +88,11 @@ const float WS_DROP_BIAS = 192.0; const float WS_ENC = 64.0; // the haze request in the velocity target; must match rain_windscreen_haze.glsl // --- Drops (atlas tiles) ------------------------------------------------------ +// (how long they live: rain_cells.glsl; the cells of the drops and of the rivulets are worked out once a frame by rain_cells.comp) const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) const float drop_size_min = 0.45; // drop size in grid cells const float drop_size_max = 1.15; const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones -const float drop_life_min = 14.0; // seconds a drop sits on the glass -const float drop_life_max = 45.0; -const float drop_appear = 0.18; // seconds of the landing "pop" -const float drop_fade = 1.5; // seconds a drop takes to vanish at the end of its life const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) // --- Micro droplets ------------------------------------------------------------- @@ -130,7 +132,6 @@ const float streak_density = 1.00; // share of cells carrying a rivulet (scal const float streak_cell = 6.0; // width of a rivulet cell in drop cells const float streak_run = 20.0; // height of a rivulet cell in drop cells = the longest run of one rivulet const float streak_head = 1.7; // mean width of the drop head, in drop cells (randomised 0.75x .. 1.25x) -const float streak_period = 40.0; // seconds a rivulet lives (it runs for ~90% of that and then fades out), average const float streak_wiggle = 0.35; // how far the path swings sideways, in head widths (0 = straight) const float streak_wiggle_speed = 0.5; // speed of the little waves that flow down the trail const float streak_flow = 0.15; // ripples flowing down inside the trail (shows in refraction and glints), 0 = off @@ -152,7 +153,6 @@ const float wind_run_scale = 1.2; // ... and their runs this long against const float wind_head_scale = 0.4; // their heads this much smaller (a small bead at the front of a long trail) const int wind_layers = 4; // layers of them, each with drops of its own (1 .. 4) const float wind_long_run = 3.0; // the last layer: its trails this much longer than those of gravity (the long ones, a part of all) -const float wind_streak_period = 18.0; // seconds a rivulet driven by the air lives (they run faster than the ones of gravity) const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float WS_DRAG_SPEED = 24.0; // speed of the air along the glass at which its drag equals the weight of a drop, m/s const float WS_HEAD_ON = 0.35; // share of the air meeting the glass head on which turns up along it @@ -164,13 +164,7 @@ const float WS_WIND_STEP = 0.1309; // the direction of the rivulets of the const float streak_thin = 0.5; // the thinnest const float streak_thick = 2.0; // the thickest const float streak_thick_bias = 2.0; // > 1: more thin ones -// --- What the blades leave: they don't take every drop (a few stay where they were), and the water they push lies along the edge -// of their sweep, just outside it, as a row of beads (where the blades went lately) ------------------------------------------------ -const float wiper_miss = 0.05; // share of the drops a blade goes over without taking them -const float wiper_edge_boost = 0.0; // along the edge of the sweep this many times more drops (and droplets) (0 = off) ... -const float wiper_edge_size = 0.65; // ... this much smaller: a row of beads -const float wiper_edge_reach = 4.0; // how wide the row is: the mip level of the mask read (4: ~16 texels of the mask out) -const float wiper_edge_keep = 180.0; // seconds after the last stroke the water at the edge keeps (then it's like the rest of the glass) +// --- What the blades leave: they don't take every drop (a few stay where they were): wiper_miss of rain_cells.glsl ----------------- #ifndef STREAK_FLIP_Y #define STREAK_FLIP_Y 0 // set to 1 if the rivulets run UP the glass (head on top) @@ -309,8 +303,7 @@ struct Riv // the layer of rivulets being worked on: the one of gravity (the rivulets as they always were, coordinates = glass UV with v // up) or the one of the air (its own cells and seeds, turned so that its rivulets run along the push of the air) float wsLayerDensity = 1.0; // x rain x streak_density: share of the cells holding a rivulet -float wsLayerPeriod = streak_period; -uint wsLayerSalt = 91u; +int wsLayerIndex = 0; // its cells in rain_cells (0 gravity, 1 .. 4 the air) float wsLayerHead = 1.0; // x streak_head float wsLayerCos = 1.0; // the layer is turned by this angle (its "down" is the way its water runs on the glass) float wsLayerSin = 0.0; @@ -344,8 +337,7 @@ void wsSetLayer(int layer) if (layer == 0) { wsLayerDensity = 1.0 - wsWindShare; - wsLayerPeriod = streak_period; - wsLayerSalt = 91u; + wsLayerIndex = 0; wsLayerHead = 1.0; wsLayerCos = 1.0; wsLayerSin = 0.0; @@ -354,8 +346,7 @@ void wsSetLayer(int layer) else { wsLayerDensity = wsWindShare * wind_streaks; - wsLayerPeriod = wind_streak_period; - wsLayerSalt = 91u + 2u * uint(layer); + wsLayerIndex = layer; wsLayerHead = wind_head_scale; wsLayerCos = wsWindCos; wsLayerSin = wsWindSin; @@ -363,35 +354,33 @@ void wsSetLayer(int layer) } } +// the rivulet of cell SCell of the layer set (wsSetLayer): the randoms and the life of the cell from rain_cells (rain_cells.comp +// worked them out as this did once from the hashes of the cell), the sizes and the places from them Riv getRiv(vec2 sCell, float sCW, float sCH, float gridSize) { Riv r; - uint sSeed = Hash(uvec3(cellKey(sCell), wsLayerSalt)); - r.on = (RandF(sSeed) < rain_params.x * streak_density * wsLayerDensity); - float sPeriod = wsLayerPeriod * mix(0.6, 1.6, RandF(sSeed)); - float sPhase = fract(time / sPeriod + RandF(sSeed)); - r.life = smoothstep(0.0, 0.05, sPhase) * (1.0 - smoothstep(0.86, 1.0, sPhase)); - float wr = RandF(sSeed); + uvec4 d = rain_cells[rain_riv_index(wsLayerIndex, ivec2(sCell))]; + r.on = ((float(d.x & 65535u) + 0.5) * (1.0 / 65536.0) < rain_params.x * streak_density * wsLayerDensity); + r.life = float((d.x >> 16) & 255u) * (1.0 / 255.0); + float wr = rain_byte(d.y, 16u); r.width = streak_head * wsLayerHead / gridSize * (wsLayerVaried ? mix(streak_thin, streak_thick, pow(wr, streak_thick_bias)) : mix(0.75, 1.25, wr)); if (wsLayerVaried) r.width = min(r.width, sCW / 2.6); // the trail with its swing (sMargin) stays in its cell r.headH = r.width * 0.88 * streak_uv_aspect; - float sSpan = max(sCH * mix(0.35, 0.70, RandF(sSeed)), r.headH * 3.0); - // stick-slip: the drop waits, then lurches forward; N waits on the way down - float sSteps = floor(mix(3.0, 8.0, RandF(sSeed))); - float sXs = clamp(sPhase / 0.88, 0.0, 1.0) * sSteps; - float sRun = clamp((floor(sXs) + smoothstep(0.55, 1.0, fract(sXs))) / sSteps, 0.0, 1.0); - r.topY = sCell.y * sCH + sSpan + RandF(sSeed) * max(sCH - sSpan, 0.0); + float sSpan = max(sCH * mix(0.35, 0.70, rain_byte(d.y, 24u)), r.headH * 3.0); + // stick-slip: how far down its run the head is now (rain_cells.comp) + float sRun = float(d.y & 65535u) * (1.0 / 65535.0); + r.topY = sCell.y * sCH + sSpan + rain_byte(d.z, 0u) * max(sCH - sSpan, 0.0); r.bottom = r.topY - r.headH * 1.2 - sRun * (sSpan - r.headH * 1.2); const float sMargin = 0.8; - r.left = sCell.x * sCW + sMargin * r.width + RandF(sSeed) * max(sCW - r.width * (1.0 + 2.0 * sMargin), 0.0); - r.flip = (RandF(sSeed) < 0.5) ? 1.0 : 0.0; - r.col = floor(RandF(sSeed) * 4.0); - r.wipe = RandF(sSeed); - r.wavK = 6.2831853 / mix(2.5, 5.0, RandF(sSeed)); - r.wavPh = RandF(sSeed) * 6.2831853; - r.wavSp = streak_wiggle_speed * mix(0.6, 1.4, RandF(sSeed)); + r.left = sCell.x * sCW + sMargin * r.width + rain_byte(d.z, 8u) * max(sCW - r.width * (1.0 + 2.0 * sMargin), 0.0); + r.flip = (((d.w >> 8) & 1u) != 0u) ? 1.0 : 0.0; + r.col = float((d.w >> 9) & 3u); + r.wipe = rain_byte(d.x, 24u); + r.wavK = 6.2831853 / mix(2.5, 5.0, rain_byte(d.z, 16u)); + r.wavPh = rain_byte(d.z, 24u) * 6.2831853; + r.wavSp = streak_wiggle_speed * mix(0.6, 1.4, rain_byte(d.w, 0u)); return r; } @@ -585,18 +574,20 @@ float wsWipeAge(vec2 co) return age; } -// how much the place Co (texture UV) lies along the edge of the sweep of the blades, just outside it: 0 .. 1. the mask is 0 there and -// a coarse level of it isn't (the sweep is near). times wsWiperRecent: only where the blades went lately -float wsWiperRecent = 0.0; -float wsWiperEdge(vec2 co) +// the drop of a cell for one side of the blades (A, B: its half of the cell in rain_cells): where it lies, how big it is and its +// shape (the sag, the stretch by the air along PushT). true when P lies on it +bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float speed, vec2 pushT, vec2 p, + out vec2 center, out float sq, out float sag, out mat2 shapeM, out vec2 dl) { - if ((wiper_edge_boost <= 0.0) || (wsWiperRecent <= 0.0)) - return 0.0; - vec4 m = textureLod(wipermask, co, 0.0); - if (max(max(m.r, m.g), max(m.b, m.a)) > 0.001) - return 0.0; - vec4 c = textureLod(wipermask, co, wiper_edge_reach); - return smoothstep(0.01, 0.12, max(max(c.r, c.g), max(c.b, c.a))) * wsWiperRecent; + center = (nc + vec2(rain_byte(a, 8u), rain_byte(a, 16u))) / gridSize; + float size = mix(drop_size_min, drop_size_max, pow(rain_byte(a, 24u), drop_size_bias)) / gridSize; + sag = 1.0 + drop_sag * rain_byte(b, 0u); + // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) + float stretch = 1.0 + drop_wind_stretch * speed * rain_byte(b, 8u); + shapeM = mat2(1.0, 0.0, 0.0, 1.0 / sag) * (mat2(1.0) + (1.0 / stretch - 1.0) * outerProduct(pushT, pushT)); + sq = size * spreadF; // spreads out as it lands + dl = shapeM * (p - center); + return max(abs(dl.x), abs(dl.y)) < sq * 0.5; } void main() @@ -643,13 +634,6 @@ void main() // wiper state of this pixel (blur, micro droplets) float hazeSide; float wipeVis = GetMixFactor(f_coord, hazeSide); - // the blades went over the glass lately (the water they pushed still lies along the edge of the sweep) - for (int i = 0; i < 4; ++i) - { - float t = max(wiper_timer_out[i], wiper_timer_return[i]); - if ((t > -500.0) && (t <= time)) - wsWiperRecent = max(wsWiperRecent, 1.0 - smoothstep(0.5 * wiper_edge_keep, wiper_edge_keep, time - t)); - } Hit hit; hit.score = 0.0; hit.cov = 0.0; hit.colA = 0.0; hit.n = vec3(0.0, 0.0, 1.0); @@ -709,52 +693,46 @@ void main() // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== - vec2 cell = floor(rainCoord * gridSize); + vec2 cell = floor(rainCoord * gridSize); + float actMax = min(rain_params.x * drop_density, 0.95); for (int oy = -1; oy <= 1; oy++) for (int ox = -1; ox <= 1; ox++) { - vec2 nc = cell + vec2(ox, oy); - uvec2 key = cellKey(nc); - // life cycle of this cell: a new drop every lifeLen seconds - uint s0 = Hash(uvec3(key, 23u)); - float lifeLen = mix(drop_life_min, drop_life_max, RandF(s0)); - float tt = time / lifeLen + RandF(s0); - float epoch = floor(tt); - float ph = tt - epoch; - uint ep = uint(mod(epoch, 65521.0)); - // activation first (cheap), the wiper texture only for live drops - uint s1 = Hash(uvec3(key, ep * 4u + 1u)); - float edge = wsWiperEdge((nc + 0.5) / gridSize); - if (RandF(s1) > min(rain_params.x * drop_density * (1.0 + wiper_edge_boost * edge), 0.95)) + vec2 nc = cell + vec2(ox, oy); + // the drop of the cell as it is now (rain_cells.comp): none, or the one of either side of the blades + uvec4 d = rain_cells[rain_drop_index(ivec2(nc))]; + if (rain_byte(d.y, 24u) > actMax) continue; + float vis = float(d.w >> 24) * (1.0 / 255.0); + if (vis <= 0.001) + continue; + float spreadF = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); // a drop the blades miss: the same drop before and after the stroke, left where it is - bool missed = RandF(s1) < wiper_miss; - float side; + bool missed = ((d.w >> 19) & 1u) != 0u; + vec2 center0, center1, dl0, dl1; + float sq0, sq1, sag0, sag1; + mat2 shape0, shape1; + bool in0 = wsDropShape(d.x, d.y, nc, gridSize, spreadF, wsSpeed, wsPushT, rainCoord, center0, sq0, sag0, shape0, dl0); + bool in1 = (!missed) && wsDropShape(d.z, d.w, nc, gridSize, spreadF, wsSpeed, wsPushT, rainCoord, center1, sq1, sag1, shape1, dl1); + // the wipers asked only for the drops the pixel lies on + if (!(in0 || in1)) + continue; + float side = 0.0; float mixF = missed ? 1.0 : GetMixFactor((nc + 0.5) / gridSize, side); - if (missed) side = 0.0; - uint s2 = Hash(uvec3(key, ep * 4u + 2u + uint(side) * 2u)); - if (mixF < RandF(s2)) + bool back = (side > 0.5); + if (!(back ? in1 : in0)) continue; - - vec2 center = (nc + vec2(RandF(s2), RandF(s2))) / gridSize; - float size = mix(drop_size_min, drop_size_max, pow(RandF(s2), drop_size_bias)) / gridSize * mix(1.0, wiper_edge_size, edge); - float sag = 1.0 + drop_sag * RandF(s2); - float flipX = (RandF(s2) < 0.5) ? 1.0 : 0.0; - float choice = RandF(s2); - // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) - float stretch = 1.0 + drop_wind_stretch * wsSpeed * RandF(s2); - mat2 shapeM = mat2(1.0, 0.0, 0.0, 1.0 / sag) - * (mat2(1.0) + (1.0 / stretch - 1.0) * outerProduct(wsPushT, wsPushT)); - - float appearF = drop_appear / lifeLen; - float fadeF = drop_fade / lifeLen; - float vis = smoothstep(0.0, appearF, ph) * (1.0 - smoothstep(1.0 - fadeF, 1.0, ph)); - float sq = size * mix(0.8, 1.0, smoothstep(0.0, 2.0 * appearF, ph)); // spreads out as it lands - - vec2 diff = rainCoord - center; - vec2 dl = shapeM * diff; - if (max(abs(dl.x), abs(dl.y)) >= sq * 0.5) + if (mixF < rain_byte(back ? d.z : d.x, 0u)) continue; + uint db = back ? d.w : d.y; + vec2 center = back ? center1 : center0; + float sq = back ? sq1 : sq0; + float sag = back ? sag1 : sag0; + mat2 shapeM = back ? shape1 : shape0; + vec2 dl = back ? dl1 : dl0; + float flipX = (((db >> 16) & 1u) != 0u) ? 1.0 : 0.0; + float choice = (float((db >> 17) & 3u) + 0.5) * 0.25; + vec2 diff = rainCoord - center; if (sweepOn) { @@ -799,7 +777,7 @@ void main() float mph = fract(mtt); uint mep = uint(mod(floor(mtt), 65521.0)); uint m1 = Hash(uvec3(mk, mep * 4u + 1u)); - if (RandF(m1) < min(rain_params.x * micro_density * (1.0 + wiper_edge_boost * wsWiperEdge((mc + 0.5) / mg)), 0.95)) + if (RandF(m1) < min(rain_params.x * micro_density, 0.95)) { uint m2 = Hash(uvec3(mk, mep * 4u + 2u + uint(hazeSide) * 2u)); if (wipeVis >= RandF(m2)) diff --git a/shaders/ogl46/rain_cells.comp b/shaders/ogl46/rain_cells.comp new file mode 100644 index 000000000..7cece6a0b --- /dev/null +++ b/shaders/ogl46/rain_cells.comp @@ -0,0 +1,131 @@ +#version 460 core +#define STAGE_COMPUTE 1 +// the cells of the glasses with rain (rain_cells.glsl): the drops and the rivulets of every cell worked out for the time of the +// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them) + +#include "common.glsl" + +layout(location = 0) uniform float rain_time; // the time of the block of the scene (scene_ubs.time) + +#include "random.glsl" +#include "rain_cells.glsl" + +layout(local_size_x = RAIN_CELLS_GROUP_SIZE) in; + +uvec2 rain_key(ivec2 Cell) +{ + return uvec2(Cell + ivec2(65536)); +} + +// a random kept to 8 bits (rain_byte() takes it back) +uint q8(float Value) +{ + return min(uint(Value * 256.0), 255u); +} + +// a share 0 .. 1 kept to Steps + 1 levels, 0 and 1 exact +uint qn(float Value, float Steps) +{ + return uint(round(clamp(Value, 0.0, 1.0) * Steps)); +} + +// a drop cell: its life now, the activation, and the drop of either side of the blades (the sequence of the randoms is the +// one mat_rain_windscreen.frag had) +uvec4 drop_cell(ivec2 Cell) +{ + uvec2 key = rain_key(Cell); + uint s0 = Hash(uvec3(key, 23u)); + float lifeLen = mix(drop_life_min, drop_life_max, RandF(s0)); + float tt = rain_time / lifeLen + RandF(s0); + float epoch = floor(tt); + float ph = tt - epoch; + uint ep = uint(mod(epoch, 65521.0)); + uint s1 = Hash(uvec3(key, ep * 4u + 1u)); + float landing = RandF(s1); + bool missed = RandF(s1) < wiper_miss; + float appearF = drop_appear / lifeLen; + float fadeF = drop_fade / lifeLen; + float vis = smoothstep(0.0, appearF, ph) * (1.0 - smoothstep(1.0 - fadeF, 1.0, ph)); + float spread = smoothstep(0.0, 2.0 * appearF, ph); + uvec4 result; + for (uint side = 0u; side < 2u; ++side) + { + uint s2 = Hash(uvec3(key, ep * 4u + 2u + side * 2u)); + float thr = RandF(s2); + float cx = RandF(s2); + float cy = RandF(s2); + float size = RandF(s2); + float sag = RandF(s2); + float flip = RandF(s2); + float choice = RandF(s2); + float stretch = RandF(s2); + uint tile = min(uint(choice * 4.0), 3u); + uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(size) << 24); + uint b = q8(sag) | (q8(stretch) << 8) | ((flip < 0.5) ? (1u << 16) : 0u) | (tile << 17); + if (side == 0u) + { + b |= (qn(spread, 31.0) << 19) | (q8(landing) << 24); + result.x = a; + result.y = b; + } + else + { + b |= (missed ? (1u << 19) : 0u) | (qn(vis, 255.0) << 24); + result.z = a; + result.w = b; + } + } + return result; +} + +// a rivulet cell of layer Layer (0 gravity, 1 .. 4 the air), as getRiv of mat_rain_windscreen.frag drew it +uvec4 rivulet_cell(int Layer, ivec2 Cell) +{ + uint salt = 91u + 2u * uint(Layer); + uint sSeed = Hash(uvec3(rain_key(Cell), salt)); + float on = RandF(sSeed); + float sPeriod = ((Layer == 0) ? streak_period : wind_streak_period) * mix(0.6, 1.6, RandF(sSeed)); + float sPhase = fract(rain_time / sPeriod + RandF(sSeed)); + float life = smoothstep(0.0, 0.05, sPhase) * (1.0 - smoothstep(0.86, 1.0, sPhase)); + float wr = RandF(sSeed); + float span = RandF(sSeed); + float sSteps = floor(mix(3.0, 8.0, RandF(sSeed))); + float sXs = clamp(sPhase / 0.88, 0.0, 1.0) * sSteps; + float sRun = clamp((floor(sXs) + smoothstep(0.55, 1.0, fract(sXs))) / sSteps, 0.0, 1.0); + float top = RandF(sSeed); + float left = RandF(sSeed); + float flip = RandF(sSeed); + float col = RandF(sSeed); + float wipe = RandF(sSeed); + float wavK = RandF(sSeed); + float wavPh = RandF(sSeed); + float wavSp = RandF(sSeed); + uvec4 result; + result.x = min(uint(on * 65536.0), 65535u) | (qn(life, 255.0) << 16) | (q8(wipe) << 24); + result.y = qn(sRun, 65535.0) | (q8(wr) << 16) | (q8(span) << 24); + result.z = q8(top) | (q8(left) << 8) | (q8(wavK) << 16) | (q8(wavPh) << 24); + result.w = q8(wavSp) | ((flip < 0.5) ? (1u << 8) : 0u) | (min(uint(col * 4.0), 3u) << 9); + return result; +} + +void main() +{ + uint index = gl_GlobalInvocationID.x; + if (index >= uint(RAIN_CELLS_COUNT)) + return; + if (index < uint(RAIN_RIV_BASE)) + { + ivec2 texel = ivec2(int(index % uint(RAIN_DROP_CELLS)), int(index / uint(RAIN_DROP_CELLS))); + // the last column and row stand for the cells -1 (the neighbours of the first ones) + ivec2 cell = ivec2(texel.x == RAIN_DROP_CELLS - 1 ? -1 : texel.x, texel.y == RAIN_DROP_CELLS - 1 ? -1 : texel.y); + rain_cells[index] = drop_cell(cell); + return; + } + uint rest = index - uint(RAIN_RIV_BASE); + int layer = int(rest / uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS)); + uint place = rest % uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS); + ivec2 texel = ivec2(int(place % uint(RAIN_RIV_CELLS)), int(place / uint(RAIN_RIV_CELLS))); + // the cells -32 .. 95: the upper quarter stands for the cells below 0 + ivec2 cell = ivec2(texel.x >= 96 ? texel.x - RAIN_RIV_CELLS : texel.x, texel.y >= 96 ? texel.y - RAIN_RIV_CELLS : texel.y); + rain_cells[index] = rivulet_cell(layer, cell); +} diff --git a/shaders/ogl46/rain_cells.glsl b/shaders/ogl46/rain_cells.glsl new file mode 100644 index 000000000..97819d724 --- /dev/null +++ b/shaders/ogl46/rain_cells.glsl @@ -0,0 +1,67 @@ +// the cells of the glasses with rain (mat_rain_windscreen.frag): the drops of the grid of drops and the rivulets of the layers of +// rivulets, worked out once a frame for every cell by rain_cells.comp (a thread a cell) instead of in every pixel of a glass for +// its 9 neighbouring cells. a cell holds the random values of its drop or rivulet as they are now (the hashes of the cell and of +// its epoch, the life cycle at the time of the frame); the glass takes the sizes and the places from them with its own settings. +// so the glass looks the same as when it worked them out itself (the hashes are the same; the values are kept to 8 or 16 bits) +// +// a cell is one uvec4. the drops: RAIN_DROP_CELLS x RAIN_DROP_CELLS cells, cell c of the grid in element (c.y & 511) * 512 + +// (c.x & 511), whole for the cells -1 .. 510 (grids up to 510 cells; finer ones repeat their drops every 512 cells): +// x side 0 (the drop of a cell the blades went over on their way out): bits 0-7 the threshold the wipers have to be past for +// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 the random of its size +// y side 0: bits 0-7 the random of its sag, 8-15 of its stretch by the air, 16 mirrored, 17-18 the tile of the atlas, +// 19-23 how far it spread out landing (0 .. 31), 24-31 the random a cell holds a drop by (a drop when below the share) +// z side 1 (the blades on their way back): as x +// w side 1: as y, bit 19 set when the blades miss the drop, bits 24-31 its visibility in its life (0 .. 255) +// the rivulets: RAIN_RIV_LAYERS layers (0 gravity, 1 .. 4 the air) of RAIN_RIV_CELLS x RAIN_RIV_CELLS cells, cell c in element +// RAIN_RIV_BASE + layer * 128 * 128 + (c.y & 127) * 128 + (c.x & 127), whole for the cells -32 .. 95: +// x bits 0-15 the random the cell holds a rivulet by, 16-23 its life (0 .. 255), 24-31 the random of the wipers it stays under +// y bits 0-15 how far down its run the head is (0 .. 65535), 16-23 the random of its width, 24-31 of its span +// z the randoms of its top, its left edge, the length and the phase of its waves (a byte each) +// w bits 0-7 the random of the speed of its waves, 8 mirrored, 9-10 the sprite of the atlas + +#ifndef RAIN_CELLS_GLSL +#define RAIN_CELLS_GLSL + +#define RAIN_DROP_CELLS 512 +#define RAIN_RIV_CELLS 128 +#define RAIN_RIV_LAYERS 5 +#define RAIN_RIV_BASE (RAIN_DROP_CELLS * RAIN_DROP_CELLS) +#define RAIN_CELLS_COUNT (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_CELLS_GROUP_SIZE 64 + +#if defined(STAGE_COMPUTE) +layout(std430, binding = RAIN_CELLS_SSBO_BINDING) writeonly buffer rain_cells_ssbo +#else +layout(std430, binding = RAIN_CELLS_SSBO_BINDING) readonly buffer rain_cells_ssbo +#endif +{ + uvec4 rain_cells[]; +}; + +// the life of the drops and of the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +const float drop_life_min = 14.0; // seconds a drop sits on the glass +const float drop_life_max = 45.0; +const float drop_appear = 0.18; // seconds of the landing "pop" +const float drop_fade = 1.5; // seconds a drop takes to vanish at the end of its life +const float wiper_miss = 0.05; // share of the drops a blade goes over without taking them +const float streak_period = 40.0; // seconds a rivulet lives (it runs for ~90% of that and then fades out), average +const float wind_streak_period = 18.0; // seconds a rivulet driven by the air lives (they run faster than the ones of gravity) + +// a random kept to 8 bits, back to 0 .. 1 (the middle of its step) +float rain_byte(uint Value, uint Shift) +{ + return (float((Value >> Shift) & 255u) + 0.5) * (1.0 / 256.0); +} + +uint rain_drop_index(ivec2 Cell) +{ + return uint(Cell.y & (RAIN_DROP_CELLS - 1)) * uint(RAIN_DROP_CELLS) + uint(Cell.x & (RAIN_DROP_CELLS - 1)); +} + +uint rain_riv_index(int Layer, ivec2 Cell) +{ + return uint(RAIN_RIV_BASE) + uint(Layer) * uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS) + uint(Cell.y & (RAIN_RIV_CELLS - 1)) * uint(RAIN_RIV_CELLS) + + uint(Cell.x & (RAIN_RIV_CELLS - 1)); +} + +#endif diff --git a/shaders/vk14/common.glsl b/shaders/vk14/common.glsl index d29dc677b..b2aa5218b 100644 --- a/shaders/vk14/common.glsl +++ b/shaders/vk14/common.glsl @@ -68,7 +68,8 @@ #define SKY_OCCLUSION_HEIGHT_TEX 31 #define SKY_OCCLUSION_TEX 32 #define CAB_PROBE_MASK_TEX 33 -#define PASS_BINDING_COUNT 34 +#define RAIN_CELLS_SSBO 34 +#define PASS_BINDING_COUNT 35 #define MATERIAL_TEXTURE_FIRST 0 #define CL_CLUSTER_UBO 0 #define CL_AABB_SSBO 1 diff --git a/shaders/vk14/mat_rain_windscreen.frag b/shaders/vk14/mat_rain_windscreen.frag index 975fd3eb8..1c167c9e4 100644 --- a/shaders/vk14/mat_rain_windscreen.frag +++ b/shaders/vk14/mat_rain_windscreen.frag @@ -19,6 +19,10 @@ // The trail right above the head is taken from the sprite part that touches the head, then it blends // into the trail pattern that is fixed to the glass (beads stay where they were left). // +// Speed: the hashes and the life cycles of the cells (which drop a cell holds now, where, how big; where the head of a rivulet is) +// are worked out once a frame for every cell by rain_cells.comp (rain_cells.glsl), not here for the 9 cells around every pixel; +// the wipers are asked only for the drops the pixel lies on. the look is the same. +// // Shading: the grey atlas (bright rim, dark middle) is read once per pixel, only for the drop that wins the // pixel (it used to be read for all 9 neighbours). Micro droplets are shaded analytically. // @@ -69,6 +73,7 @@ layout(location = 1) out vec4 out_motion; #include "apply_fog.glsl" #include "tonemapping.glsl" #include "random.glsl" +#include "rain_cells.glsl" layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 0) uniform sampler2D diffuse; layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 1) uniform sampler2D raindropsatlas; @@ -88,14 +93,11 @@ const float WS_DROP_BIAS = 192.0; const float WS_ENC = 64.0; // the haze request in the velocity target; must match rain_windscreen_haze.glsl // --- Drops (atlas tiles) ------------------------------------------------------ +// (how long they live: rain_cells.glsl; the cells of the drops and of the rivulets are worked out once a frame by rain_cells.comp) const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) const float drop_size_min = 0.45; // drop size in grid cells const float drop_size_max = 1.15; const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones -const float drop_life_min = 14.0; // seconds a drop sits on the glass -const float drop_life_max = 45.0; -const float drop_appear = 0.18; // seconds of the landing "pop" -const float drop_fade = 1.5; // seconds a drop takes to vanish at the end of its life const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) // --- Micro droplets ------------------------------------------------------------- @@ -135,7 +137,6 @@ const float streak_density = 1.00; // share of cells carrying a rivulet (scal const float streak_cell = 6.0; // width of a rivulet cell in drop cells const float streak_run = 20.0; // height of a rivulet cell in drop cells = the longest run of one rivulet const float streak_head = 1.7; // mean width of the drop head, in drop cells (randomised 0.75x .. 1.25x) -const float streak_period = 40.0; // seconds a rivulet lives (it runs for ~90% of that and then fades out), average const float streak_wiggle = 0.35; // how far the path swings sideways, in head widths (0 = straight) const float streak_wiggle_speed = 0.5; // speed of the little waves that flow down the trail const float streak_flow = 0.15; // ripples flowing down inside the trail (shows in refraction and glints), 0 = off @@ -157,7 +158,6 @@ const float wind_run_scale = 1.2; // ... and their runs this long against const float wind_head_scale = 0.4; // their heads this much smaller (a small bead at the front of a long trail) const int wind_layers = 4; // layers of them, each with drops of its own (1 .. 4) const float wind_long_run = 3.0; // the last layer: its trails this much longer than those of gravity (the long ones, a part of all) -const float wind_streak_period = 18.0; // seconds a rivulet driven by the air lives (they run faster than the ones of gravity) const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float WS_DRAG_SPEED = 24.0; // speed of the air along the glass at which its drag equals the weight of a drop, m/s const float WS_HEAD_ON = 0.35; // share of the air meeting the glass head on which turns up along it @@ -169,13 +169,7 @@ const float WS_WIND_STEP = 0.1309; // the direction of the rivulets of the const float streak_thin = 0.5; // the thinnest const float streak_thick = 2.0; // the thickest const float streak_thick_bias = 2.0; // > 1: more thin ones -// --- What the blades leave: they don't take every drop (a few stay where they were), and the water they push lies along the edge -// of their sweep, just outside it, as a row of beads (where the blades went lately) ------------------------------------------------ -const float wiper_miss = 0.05; // share of the drops a blade goes over without taking them -const float wiper_edge_boost = 0.0; // along the edge of the sweep this many times more drops (and droplets) (0 = off) ... -const float wiper_edge_size = 0.65; // ... this much smaller: a row of beads -const float wiper_edge_reach = 4.0; // how wide the row is: the mip level of the mask read (4: ~16 texels of the mask out) -const float wiper_edge_keep = 180.0; // seconds after the last stroke the water at the edge keeps (then it's like the rest of the glass) +// --- What the blades leave: they don't take every drop (a few stay where they were): wiper_miss of rain_cells.glsl ----------------- #ifndef STREAK_FLIP_Y #define STREAK_FLIP_Y 0 // set to 1 if the rivulets run UP the glass (head on top) @@ -314,8 +308,7 @@ struct Riv // the layer of rivulets being worked on: the one of gravity (the rivulets as they always were, coordinates = glass UV with v // up) or the one of the air (its own cells and seeds, turned so that its rivulets run along the push of the air) float wsLayerDensity = 1.0; // x rain x streak_density: share of the cells holding a rivulet -float wsLayerPeriod = streak_period; -uint wsLayerSalt = 91u; +int wsLayerIndex = 0; // its cells in rain_cells (0 gravity, 1 .. 4 the air) float wsLayerHead = 1.0; // x streak_head float wsLayerCos = 1.0; // the layer is turned by this angle (its "down" is the way its water runs on the glass) float wsLayerSin = 0.0; @@ -349,8 +342,7 @@ void wsSetLayer(int layer) if (layer == 0) { wsLayerDensity = 1.0 - wsWindShare; - wsLayerPeriod = streak_period; - wsLayerSalt = 91u; + wsLayerIndex = 0; wsLayerHead = 1.0; wsLayerCos = 1.0; wsLayerSin = 0.0; @@ -359,8 +351,7 @@ void wsSetLayer(int layer) else { wsLayerDensity = wsWindShare * wind_streaks; - wsLayerPeriod = wind_streak_period; - wsLayerSalt = 91u + 2u * uint(layer); + wsLayerIndex = layer; wsLayerHead = wind_head_scale; wsLayerCos = wsWindCos; wsLayerSin = wsWindSin; @@ -368,35 +359,33 @@ void wsSetLayer(int layer) } } +// the rivulet of cell SCell of the layer set (wsSetLayer): the randoms and the life of the cell from rain_cells (rain_cells.comp +// worked them out as this did once from the hashes of the cell), the sizes and the places from them Riv getRiv(vec2 sCell, float sCW, float sCH, float gridSize) { Riv r; - uint sSeed = Hash(uvec3(cellKey(sCell), wsLayerSalt)); - r.on = (RandF(sSeed) < rain_params.x * streak_density * wsLayerDensity); - float sPeriod = wsLayerPeriod * mix(0.6, 1.6, RandF(sSeed)); - float sPhase = fract(time / sPeriod + RandF(sSeed)); - r.life = smoothstep(0.0, 0.05, sPhase) * (1.0 - smoothstep(0.86, 1.0, sPhase)); - float wr = RandF(sSeed); + uvec4 d = rain_cells[rain_riv_index(wsLayerIndex, ivec2(sCell))]; + r.on = ((float(d.x & 65535u) + 0.5) * (1.0 / 65536.0) < rain_params.x * streak_density * wsLayerDensity); + r.life = float((d.x >> 16) & 255u) * (1.0 / 255.0); + float wr = rain_byte(d.y, 16u); r.width = streak_head * wsLayerHead / gridSize * (wsLayerVaried ? mix(streak_thin, streak_thick, pow(wr, streak_thick_bias)) : mix(0.75, 1.25, wr)); if (wsLayerVaried) r.width = min(r.width, sCW / 2.6); // the trail with its swing (sMargin) stays in its cell r.headH = r.width * 0.88 * streak_uv_aspect; - float sSpan = max(sCH * mix(0.35, 0.70, RandF(sSeed)), r.headH * 3.0); - // stick-slip: the drop waits, then lurches forward; N waits on the way down - float sSteps = floor(mix(3.0, 8.0, RandF(sSeed))); - float sXs = clamp(sPhase / 0.88, 0.0, 1.0) * sSteps; - float sRun = clamp((floor(sXs) + smoothstep(0.55, 1.0, fract(sXs))) / sSteps, 0.0, 1.0); - r.topY = sCell.y * sCH + sSpan + RandF(sSeed) * max(sCH - sSpan, 0.0); + float sSpan = max(sCH * mix(0.35, 0.70, rain_byte(d.y, 24u)), r.headH * 3.0); + // stick-slip: how far down its run the head is now (rain_cells.comp) + float sRun = float(d.y & 65535u) * (1.0 / 65535.0); + r.topY = sCell.y * sCH + sSpan + rain_byte(d.z, 0u) * max(sCH - sSpan, 0.0); r.bottom = r.topY - r.headH * 1.2 - sRun * (sSpan - r.headH * 1.2); const float sMargin = 0.8; - r.left = sCell.x * sCW + sMargin * r.width + RandF(sSeed) * max(sCW - r.width * (1.0 + 2.0 * sMargin), 0.0); - r.flip = (RandF(sSeed) < 0.5) ? 1.0 : 0.0; - r.col = floor(RandF(sSeed) * 4.0); - r.wipe = RandF(sSeed); - r.wavK = 6.2831853 / mix(2.5, 5.0, RandF(sSeed)); - r.wavPh = RandF(sSeed) * 6.2831853; - r.wavSp = streak_wiggle_speed * mix(0.6, 1.4, RandF(sSeed)); + r.left = sCell.x * sCW + sMargin * r.width + rain_byte(d.z, 8u) * max(sCW - r.width * (1.0 + 2.0 * sMargin), 0.0); + r.flip = (((d.w >> 8) & 1u) != 0u) ? 1.0 : 0.0; + r.col = float((d.w >> 9) & 3u); + r.wipe = rain_byte(d.x, 24u); + r.wavK = 6.2831853 / mix(2.5, 5.0, rain_byte(d.z, 16u)); + r.wavPh = rain_byte(d.z, 24u) * 6.2831853; + r.wavSp = streak_wiggle_speed * mix(0.6, 1.4, rain_byte(d.w, 0u)); return r; } @@ -590,18 +579,20 @@ float wsWipeAge(vec2 co) return age; } -// how much the place Co (texture UV) lies along the edge of the sweep of the blades, just outside it: 0 .. 1. the mask is 0 there and -// a coarse level of it isn't (the sweep is near). times wsWiperRecent: only where the blades went lately -float wsWiperRecent = 0.0; -float wsWiperEdge(vec2 co) +// the drop of a cell for one side of the blades (A, B: its half of the cell in rain_cells): where it lies, how big it is and its +// shape (the sag, the stretch by the air along PushT). true when P lies on it +bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float speed, vec2 pushT, vec2 p, + out vec2 center, out float sq, out float sag, out mat2 shapeM, out vec2 dl) { - if ((wiper_edge_boost <= 0.0) || (wsWiperRecent <= 0.0)) - return 0.0; - vec4 m = textureLod(wipermask, co, 0.0); - if (max(max(m.r, m.g), max(m.b, m.a)) > 0.001) - return 0.0; - vec4 c = textureLod(wipermask, co, wiper_edge_reach); - return smoothstep(0.01, 0.12, max(max(c.r, c.g), max(c.b, c.a))) * wsWiperRecent; + center = (nc + vec2(rain_byte(a, 8u), rain_byte(a, 16u))) / gridSize; + float size = mix(drop_size_min, drop_size_max, pow(rain_byte(a, 24u), drop_size_bias)) / gridSize; + sag = 1.0 + drop_sag * rain_byte(b, 0u); + // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) + float stretch = 1.0 + drop_wind_stretch * speed * rain_byte(b, 8u); + shapeM = mat2(1.0, 0.0, 0.0, 1.0 / sag) * (mat2(1.0) + (1.0 / stretch - 1.0) * outerProduct(pushT, pushT)); + sq = size * spreadF; // spreads out as it lands + dl = shapeM * (p - center); + return max(abs(dl.x), abs(dl.y)) < sq * 0.5; } void main() @@ -648,13 +639,6 @@ void main() // wiper state of this pixel (blur, micro droplets) float hazeSide; float wipeVis = GetMixFactor(f_coord, hazeSide); - // the blades went over the glass lately (the water they pushed still lies along the edge of the sweep) - for (int i = 0; i < 4; ++i) - { - float t = max(wiper_timer_out[i], wiper_timer_return[i]); - if ((t > -500.0) && (t <= time)) - wsWiperRecent = max(wsWiperRecent, 1.0 - smoothstep(0.5 * wiper_edge_keep, wiper_edge_keep, time - t)); - } Hit hit; hit.score = 0.0; hit.cov = 0.0; hit.colA = 0.0; hit.n = vec3(0.0, 0.0, 1.0); @@ -714,52 +698,46 @@ void main() // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== - vec2 cell = floor(rainCoord * gridSize); + vec2 cell = floor(rainCoord * gridSize); + float actMax = min(rain_params.x * drop_density, 0.95); for (int oy = -1; oy <= 1; oy++) for (int ox = -1; ox <= 1; ox++) { - vec2 nc = cell + vec2(ox, oy); - uvec2 key = cellKey(nc); - // life cycle of this cell: a new drop every lifeLen seconds - uint s0 = Hash(uvec3(key, 23u)); - float lifeLen = mix(drop_life_min, drop_life_max, RandF(s0)); - float tt = time / lifeLen + RandF(s0); - float epoch = floor(tt); - float ph = tt - epoch; - uint ep = uint(mod(epoch, 65521.0)); - // activation first (cheap), the wiper texture only for live drops - uint s1 = Hash(uvec3(key, ep * 4u + 1u)); - float edge = wsWiperEdge((nc + 0.5) / gridSize); - if (RandF(s1) > min(rain_params.x * drop_density * (1.0 + wiper_edge_boost * edge), 0.95)) + vec2 nc = cell + vec2(ox, oy); + // the drop of the cell as it is now (rain_cells.comp): none, or the one of either side of the blades + uvec4 d = rain_cells[rain_drop_index(ivec2(nc))]; + if (rain_byte(d.y, 24u) > actMax) continue; + float vis = float(d.w >> 24) * (1.0 / 255.0); + if (vis <= 0.001) + continue; + float spreadF = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); // a drop the blades miss: the same drop before and after the stroke, left where it is - bool missed = RandF(s1) < wiper_miss; - float side; + bool missed = ((d.w >> 19) & 1u) != 0u; + vec2 center0, center1, dl0, dl1; + float sq0, sq1, sag0, sag1; + mat2 shape0, shape1; + bool in0 = wsDropShape(d.x, d.y, nc, gridSize, spreadF, wsSpeed, wsPushT, rainCoord, center0, sq0, sag0, shape0, dl0); + bool in1 = (!missed) && wsDropShape(d.z, d.w, nc, gridSize, spreadF, wsSpeed, wsPushT, rainCoord, center1, sq1, sag1, shape1, dl1); + // the wipers asked only for the drops the pixel lies on + if (!(in0 || in1)) + continue; + float side = 0.0; float mixF = missed ? 1.0 : GetMixFactor((nc + 0.5) / gridSize, side); - if (missed) side = 0.0; - uint s2 = Hash(uvec3(key, ep * 4u + 2u + uint(side) * 2u)); - if (mixF < RandF(s2)) + bool back = (side > 0.5); + if (!(back ? in1 : in0)) continue; - - vec2 center = (nc + vec2(RandF(s2), RandF(s2))) / gridSize; - float size = mix(drop_size_min, drop_size_max, pow(RandF(s2), drop_size_bias)) / gridSize * mix(1.0, wiper_edge_size, edge); - float sag = 1.0 + drop_sag * RandF(s2); - float flipX = (RandF(s2) < 0.5) ? 1.0 : 0.0; - float choice = RandF(s2); - // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) - float stretch = 1.0 + drop_wind_stretch * wsSpeed * RandF(s2); - mat2 shapeM = mat2(1.0, 0.0, 0.0, 1.0 / sag) - * (mat2(1.0) + (1.0 / stretch - 1.0) * outerProduct(wsPushT, wsPushT)); - - float appearF = drop_appear / lifeLen; - float fadeF = drop_fade / lifeLen; - float vis = smoothstep(0.0, appearF, ph) * (1.0 - smoothstep(1.0 - fadeF, 1.0, ph)); - float sq = size * mix(0.8, 1.0, smoothstep(0.0, 2.0 * appearF, ph)); // spreads out as it lands - - vec2 diff = rainCoord - center; - vec2 dl = shapeM * diff; - if (max(abs(dl.x), abs(dl.y)) >= sq * 0.5) + if (mixF < rain_byte(back ? d.z : d.x, 0u)) continue; + uint db = back ? d.w : d.y; + vec2 center = back ? center1 : center0; + float sq = back ? sq1 : sq0; + float sag = back ? sag1 : sag0; + mat2 shapeM = back ? shape1 : shape0; + vec2 dl = back ? dl1 : dl0; + float flipX = (((db >> 16) & 1u) != 0u) ? 1.0 : 0.0; + float choice = (float((db >> 17) & 3u) + 0.5) * 0.25; + vec2 diff = rainCoord - center; if (sweepOn) { @@ -804,7 +782,7 @@ void main() float mph = fract(mtt); uint mep = uint(mod(floor(mtt), 65521.0)); uint m1 = Hash(uvec3(mk, mep * 4u + 1u)); - if (RandF(m1) < min(rain_params.x * micro_density * (1.0 + wiper_edge_boost * wsWiperEdge((mc + 0.5) / mg)), 0.95)) + if (RandF(m1) < min(rain_params.x * micro_density, 0.95)) { uint m2 = Hash(uvec3(mk, mep * 4u + 2u + uint(hazeSide) * 2u)); if (wipeVis >= RandF(m2)) diff --git a/shaders/vk14/rain_cells.comp b/shaders/vk14/rain_cells.comp new file mode 100644 index 000000000..c8ca2af4b --- /dev/null +++ b/shaders/vk14/rain_cells.comp @@ -0,0 +1,136 @@ +#version 460 core +#define STAGE_COMPUTE 1 +// the cells of the glasses with rain (rain_cells.glsl): the drops and the rivulets of every cell worked out for the time of the +// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them) + +// none of the pass set: the cells pushed with the dispatch (set COMPUTE_SET), the time in the push constants +#define LAYOUT_UI 1 +#include "common.glsl" + +layout(push_constant) uniform kernel_constants +{ + float rain_time; // the time of the block of the scene (scene_ubs.time) +}; + +#include "random.glsl" +#include "rain_cells.glsl" + +layout(local_size_x = RAIN_CELLS_GROUP_SIZE) in; + +uvec2 rain_key(ivec2 Cell) +{ + return uvec2(Cell + ivec2(65536)); +} + +// a random kept to 8 bits (rain_byte() takes it back) +uint q8(float Value) +{ + return min(uint(Value * 256.0), 255u); +} + +// a share 0 .. 1 kept to Steps + 1 levels, 0 and 1 exact +uint qn(float Value, float Steps) +{ + return uint(round(clamp(Value, 0.0, 1.0) * Steps)); +} + +// a drop cell: its life now, the activation, and the drop of either side of the blades (the sequence of the randoms is the +// one mat_rain_windscreen.frag had) +uvec4 drop_cell(ivec2 Cell) +{ + uvec2 key = rain_key(Cell); + uint s0 = Hash(uvec3(key, 23u)); + float lifeLen = mix(drop_life_min, drop_life_max, RandF(s0)); + float tt = rain_time / lifeLen + RandF(s0); + float epoch = floor(tt); + float ph = tt - epoch; + uint ep = uint(mod(epoch, 65521.0)); + uint s1 = Hash(uvec3(key, ep * 4u + 1u)); + float landing = RandF(s1); + bool missed = RandF(s1) < wiper_miss; + float appearF = drop_appear / lifeLen; + float fadeF = drop_fade / lifeLen; + float vis = smoothstep(0.0, appearF, ph) * (1.0 - smoothstep(1.0 - fadeF, 1.0, ph)); + float spread = smoothstep(0.0, 2.0 * appearF, ph); + uvec4 result; + for (uint side = 0u; side < 2u; ++side) + { + uint s2 = Hash(uvec3(key, ep * 4u + 2u + side * 2u)); + float thr = RandF(s2); + float cx = RandF(s2); + float cy = RandF(s2); + float size = RandF(s2); + float sag = RandF(s2); + float flip = RandF(s2); + float choice = RandF(s2); + float stretch = RandF(s2); + uint tile = min(uint(choice * 4.0), 3u); + uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(size) << 24); + uint b = q8(sag) | (q8(stretch) << 8) | ((flip < 0.5) ? (1u << 16) : 0u) | (tile << 17); + if (side == 0u) + { + b |= (qn(spread, 31.0) << 19) | (q8(landing) << 24); + result.x = a; + result.y = b; + } + else + { + b |= (missed ? (1u << 19) : 0u) | (qn(vis, 255.0) << 24); + result.z = a; + result.w = b; + } + } + return result; +} + +// a rivulet cell of layer Layer (0 gravity, 1 .. 4 the air), as getRiv of mat_rain_windscreen.frag drew it +uvec4 rivulet_cell(int Layer, ivec2 Cell) +{ + uint salt = 91u + 2u * uint(Layer); + uint sSeed = Hash(uvec3(rain_key(Cell), salt)); + float on = RandF(sSeed); + float sPeriod = ((Layer == 0) ? streak_period : wind_streak_period) * mix(0.6, 1.6, RandF(sSeed)); + float sPhase = fract(rain_time / sPeriod + RandF(sSeed)); + float life = smoothstep(0.0, 0.05, sPhase) * (1.0 - smoothstep(0.86, 1.0, sPhase)); + float wr = RandF(sSeed); + float span = RandF(sSeed); + float sSteps = floor(mix(3.0, 8.0, RandF(sSeed))); + float sXs = clamp(sPhase / 0.88, 0.0, 1.0) * sSteps; + float sRun = clamp((floor(sXs) + smoothstep(0.55, 1.0, fract(sXs))) / sSteps, 0.0, 1.0); + float top = RandF(sSeed); + float left = RandF(sSeed); + float flip = RandF(sSeed); + float col = RandF(sSeed); + float wipe = RandF(sSeed); + float wavK = RandF(sSeed); + float wavPh = RandF(sSeed); + float wavSp = RandF(sSeed); + uvec4 result; + result.x = min(uint(on * 65536.0), 65535u) | (qn(life, 255.0) << 16) | (q8(wipe) << 24); + result.y = qn(sRun, 65535.0) | (q8(wr) << 16) | (q8(span) << 24); + result.z = q8(top) | (q8(left) << 8) | (q8(wavK) << 16) | (q8(wavPh) << 24); + result.w = q8(wavSp) | ((flip < 0.5) ? (1u << 8) : 0u) | (min(uint(col * 4.0), 3u) << 9); + return result; +} + +void main() +{ + uint index = gl_GlobalInvocationID.x; + if (index >= uint(RAIN_CELLS_COUNT)) + return; + if (index < uint(RAIN_RIV_BASE)) + { + ivec2 texel = ivec2(int(index % uint(RAIN_DROP_CELLS)), int(index / uint(RAIN_DROP_CELLS))); + // the last column and row stand for the cells -1 (the neighbours of the first ones) + ivec2 cell = ivec2(texel.x == RAIN_DROP_CELLS - 1 ? -1 : texel.x, texel.y == RAIN_DROP_CELLS - 1 ? -1 : texel.y); + rain_cells[index] = drop_cell(cell); + return; + } + uint rest = index - uint(RAIN_RIV_BASE); + int layer = int(rest / uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS)); + uint place = rest % uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS); + ivec2 texel = ivec2(int(place % uint(RAIN_RIV_CELLS)), int(place / uint(RAIN_RIV_CELLS))); + // the cells -32 .. 95: the upper quarter stands for the cells below 0 + ivec2 cell = ivec2(texel.x >= 96 ? texel.x - RAIN_RIV_CELLS : texel.x, texel.y >= 96 ? texel.y - RAIN_RIV_CELLS : texel.y); + rain_cells[index] = rivulet_cell(layer, cell); +} diff --git a/shaders/vk14/rain_cells.glsl b/shaders/vk14/rain_cells.glsl new file mode 100644 index 000000000..e61cfe4f6 --- /dev/null +++ b/shaders/vk14/rain_cells.glsl @@ -0,0 +1,67 @@ +// the cells of the glasses with rain (mat_rain_windscreen.frag): the drops of the grid of drops and the rivulets of the layers of +// rivulets, worked out once a frame for every cell by rain_cells.comp (a thread a cell) instead of in every pixel of a glass for +// its 9 neighbouring cells. a cell holds the random values of its drop or rivulet as they are now (the hashes of the cell and of +// its epoch, the life cycle at the time of the frame); the glass takes the sizes and the places from them with its own settings. +// so the glass looks the same as when it worked them out itself (the hashes are the same; the values are kept to 8 or 16 bits) +// +// a cell is one uvec4. the drops: RAIN_DROP_CELLS x RAIN_DROP_CELLS cells, cell c of the grid in element (c.y & 511) * 512 + +// (c.x & 511), whole for the cells -1 .. 510 (grids up to 510 cells; finer ones repeat their drops every 512 cells): +// x side 0 (the drop of a cell the blades went over on their way out): bits 0-7 the threshold the wipers have to be past for +// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 the random of its size +// y side 0: bits 0-7 the random of its sag, 8-15 of its stretch by the air, 16 mirrored, 17-18 the tile of the atlas, +// 19-23 how far it spread out landing (0 .. 31), 24-31 the random a cell holds a drop by (a drop when below the share) +// z side 1 (the blades on their way back): as x +// w side 1: as y, bit 19 set when the blades miss the drop, bits 24-31 its visibility in its life (0 .. 255) +// the rivulets: RAIN_RIV_LAYERS layers (0 gravity, 1 .. 4 the air) of RAIN_RIV_CELLS x RAIN_RIV_CELLS cells, cell c in element +// RAIN_RIV_BASE + layer * 128 * 128 + (c.y & 127) * 128 + (c.x & 127), whole for the cells -32 .. 95: +// x bits 0-15 the random the cell holds a rivulet by, 16-23 its life (0 .. 255), 24-31 the random of the wipers it stays under +// y bits 0-15 how far down its run the head is (0 .. 65535), 16-23 the random of its width, 24-31 of its span +// z the randoms of its top, its left edge, the length and the phase of its waves (a byte each) +// w bits 0-7 the random of the speed of its waves, 8 mirrored, 9-10 the sprite of the atlas + +#ifndef RAIN_CELLS_GLSL +#define RAIN_CELLS_GLSL + +#define RAIN_DROP_CELLS 512 +#define RAIN_RIV_CELLS 128 +#define RAIN_RIV_LAYERS 5 +#define RAIN_RIV_BASE (RAIN_DROP_CELLS * RAIN_DROP_CELLS) +#define RAIN_CELLS_COUNT (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_CELLS_GROUP_SIZE 64 + +#if defined(STAGE_COMPUTE) +layout(std430, set = COMPUTE_SET, binding = 0) writeonly buffer rain_cells_ssbo +#else +layout(std430, set = PASS_SET, binding = RAIN_CELLS_SSBO) readonly buffer rain_cells_ssbo +#endif +{ + uvec4 rain_cells[]; +}; + +// the life of the drops and of the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +const float drop_life_min = 14.0; // seconds a drop sits on the glass +const float drop_life_max = 45.0; +const float drop_appear = 0.18; // seconds of the landing "pop" +const float drop_fade = 1.5; // seconds a drop takes to vanish at the end of its life +const float wiper_miss = 0.05; // share of the drops a blade goes over without taking them +const float streak_period = 40.0; // seconds a rivulet lives (it runs for ~90% of that and then fades out), average +const float wind_streak_period = 18.0; // seconds a rivulet driven by the air lives (they run faster than the ones of gravity) + +// a random kept to 8 bits, back to 0 .. 1 (the middle of its step) +float rain_byte(uint Value, uint Shift) +{ + return (float((Value >> Shift) & 255u) + 0.5) * (1.0 / 256.0); +} + +uint rain_drop_index(ivec2 Cell) +{ + return uint(Cell.y & (RAIN_DROP_CELLS - 1)) * uint(RAIN_DROP_CELLS) + uint(Cell.x & (RAIN_DROP_CELLS - 1)); +} + +uint rain_riv_index(int Layer, ivec2 Cell) +{ + return uint(RAIN_RIV_BASE) + uint(Layer) * uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS) + uint(Cell.y & (RAIN_RIV_CELLS - 1)) * uint(RAIN_RIV_CELLS) + + uint(Cell.x & (RAIN_RIV_CELLS - 1)); +} + +#endif From dba4e532445c32e44de76d0539ba8b8641bca8ff Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 16:58:47 +0100 Subject: [PATCH 2/4] Rain windscreen: a switch to measure what the glass costs (WS_PROFILE, 0 as in the game) [skip ci] Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- shaders/ogl46/mat_rain_windscreen.frag | 20 +++++++++++++++++--- shaders/vk14/mat_rain_windscreen.frag | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/shaders/ogl46/mat_rain_windscreen.frag b/shaders/ogl46/mat_rain_windscreen.frag index e4e2de711..668f21e8a 100644 --- a/shaders/ogl46/mat_rain_windscreen.frag +++ b/shaders/ogl46/mat_rain_windscreen.frag @@ -179,6 +179,13 @@ const float WS_REFR_MIX = 0.85; // 0 = no refraction, 1 = only the refrac const float WS_LIGHT_GAIN = 1.0; // brightness of the lights near a drop shown in it (0 = off) const float WS_LIGHT_CONTRAST = 3.0; // a light: this many times brighter than the place around it +// measuring what costs what (the time of "colour, translucent and cab" with each): 0 everything (as in the game), 1 the drops not lit +// (no sun glint, lamps, reflection), 2 no rivulets (and nothing swept by them), 3 no micro droplets, 4 no drops of the atlas, +// 5 none of the rain (only the glass and the haze) +#ifndef WS_PROFILE +#define WS_PROFILE 0 +#endif + const vec3 DROP_LIGHT = vec3(-0.5527, 0.6532, 0.5226); // same light as the atlas generator (upper left) // the surroundings in a view space direction where the image doesn't hold them: the environment map above the horizon, @@ -686,14 +693,15 @@ void main() vec2 sUVg = vec2(f_coord.x, (STREAK_FLIP_Y != 0) ? 1.0 - f_coord.y : f_coord.y); // glass UV, v up vec2 sCell = floor(sUVg / vec2(sCW, sCH)); Riv rivP = getRiv(sCell, sCW, sCH, gridSize); - bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0); + bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0) && (WS_PROFILE != 2); - if (rain_params.x > 0.001) + if ((rain_params.x > 0.001) && (WS_PROFILE != 5)) { // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== vec2 cell = floor(rainCoord * gridSize); +#if WS_PROFILE != 4 float actMax = min(rain_params.x * drop_density, 0.95); for (int oy = -1; oy <= 1; oy++) for (int ox = -1; ox <= 1; ox++) @@ -762,11 +770,12 @@ void main() consider(hit, cov * (nm.b * sq * sag + 1e-5), cov, 0.0, n, diff, 0.5 * sq, 1.0, vis, dropAtlasUV(choice, luv), gx, gy, 1.0, 0.0); } +#endif // =================================================================== // 2. micro droplets: procedural, one per micro cell, fully inside it // =================================================================== - if (micro_density > 0.0) + if ((micro_density > 0.0) && (WS_PROFILE != 3)) { float mg = gridSize * micro_scale; vec2 mc = floor(rainCoord * mg); @@ -819,6 +828,7 @@ void main() // =================================================================== // 3. rivulets of this pixel's cells: the layer of gravity and, at speed, the one of the air // =================================================================== +#if WS_PROFILE != 2 wsSetLayer(0); drawRivulet(hit, rivP, sUVg, wsRd1, wsRd2); if (wsWindShare > 0.001) @@ -834,6 +844,7 @@ void main() } wsSetLayer(0); } +#endif } // ----------------------------------------------------------------------- @@ -958,6 +969,9 @@ void main() float NoV = clamp(dot(Nv, -I), 0.0, 1.0); glint += envmap_color_lod(Nv, 0.8) * EnvBRDFApprox(vec3(0.04), 0.1, NoV) * drop_env * wsNight; } +#if WS_PROFILE == 1 + glint = vec3(0.0); +#endif dropGlint = glint; if (!POSTFX_ENABLED) dropGlint = min(dropGlint, vec3(1.0)); // this path is not tonemapped diff --git a/shaders/vk14/mat_rain_windscreen.frag b/shaders/vk14/mat_rain_windscreen.frag index 1c167c9e4..3ac5ca31a 100644 --- a/shaders/vk14/mat_rain_windscreen.frag +++ b/shaders/vk14/mat_rain_windscreen.frag @@ -184,6 +184,13 @@ const float WS_REFR_MIX = 0.85; // 0 = no refraction, 1 = only the refrac const float WS_LIGHT_GAIN = 1.0; // brightness of the lights near a drop shown in it (0 = off) const float WS_LIGHT_CONTRAST = 3.0; // a light: this many times brighter than the place around it +// measuring what costs what (the time of "colour, translucent and cab" with each): 0 everything (as in the game), 1 the drops not lit +// (no sun glint, lamps, reflection), 2 no rivulets (and nothing swept by them), 3 no micro droplets, 4 no drops of the atlas, +// 5 none of the rain (only the glass and the haze) +#ifndef WS_PROFILE +#define WS_PROFILE 0 +#endif + const vec3 DROP_LIGHT = vec3(-0.5527, 0.6532, 0.5226); // same light as the atlas generator (upper left) // the surroundings in a view space direction where the image doesn't hold them: the environment map above the horizon, @@ -691,14 +698,15 @@ void main() vec2 sUVg = vec2(f_coord.x, (STREAK_FLIP_Y != 0) ? 1.0 - f_coord.y : f_coord.y); // glass UV, v up vec2 sCell = floor(sUVg / vec2(sCW, sCH)); Riv rivP = getRiv(sCell, sCW, sCH, gridSize); - bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0); + bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0) && (WS_PROFILE != 2); - if (rain_params.x > 0.001) + if ((rain_params.x > 0.001) && (WS_PROFILE != 5)) { // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== vec2 cell = floor(rainCoord * gridSize); +#if WS_PROFILE != 4 float actMax = min(rain_params.x * drop_density, 0.95); for (int oy = -1; oy <= 1; oy++) for (int ox = -1; ox <= 1; ox++) @@ -767,11 +775,12 @@ void main() consider(hit, cov * (nm.b * sq * sag + 1e-5), cov, 0.0, n, diff, 0.5 * sq, 1.0, vis, dropAtlasUV(choice, luv), gx, gy, 1.0, 0.0); } +#endif // =================================================================== // 2. micro droplets: procedural, one per micro cell, fully inside it // =================================================================== - if (micro_density > 0.0) + if ((micro_density > 0.0) && (WS_PROFILE != 3)) { float mg = gridSize * micro_scale; vec2 mc = floor(rainCoord * mg); @@ -824,6 +833,7 @@ void main() // =================================================================== // 3. rivulets of this pixel's cells: the layer of gravity and, at speed, the one of the air // =================================================================== +#if WS_PROFILE != 2 wsSetLayer(0); drawRivulet(hit, rivP, sUVg, wsRd1, wsRd2); if (wsWindShare > 0.001) @@ -839,6 +849,7 @@ void main() } wsSetLayer(0); } +#endif } // ----------------------------------------------------------------------- @@ -963,6 +974,9 @@ void main() float NoV = clamp(dot(Nv, -I), 0.0, 1.0); glint += envmap_color_lod(Nv, 0.8) * EnvBRDFApprox(vec3(0.04), 0.1, NoV) * drop_env * wsNight; } +#if WS_PROFILE == 1 + glint = vec3(0.0); +#endif dropGlint = glint; if (!POSTFX_ENABLED) dropGlint = min(dropGlint, vec3(1.0)); // this path is not tonemapped From a7fc8541c823d6c6f710fcbf9ad49b866d75ce1b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 17:20:36 +0100 Subject: [PATCH 3/4] Rain windscreen: a glass looks only at the drops which reach the quarter of its cell A second pass of rain_cells.comp writes, for every drop cell, which of the drops of the 9 cells around reach each quarter of it, so a pixel looks at those (usually 1 to 3) instead of all 9. The bias of the drop sizes is applied there too, and the glass keeps fewer values through its loops (the frame of the glass and the lens are worked out after them, the rivulet of the pixel is read again where it is needed), so more pixels run at once. The look is the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- gl/ubo.h | 4 +- rendering/opengl46/opengl46water.cpp | 9 ++- rendering/vulkan14/vulkan14water.cpp | 14 +++- shaders/ogl46/mat_rain_windscreen.frag | 108 ++++++++++++++----------- shaders/ogl46/rain_cells.comp | 65 ++++++++++++++- shaders/ogl46/rain_cells.glsl | 23 +++++- shaders/vk14/mat_rain_windscreen.frag | 108 ++++++++++++++----------- shaders/vk14/rain_cells.comp | 67 +++++++++++++-- shaders/vk14/rain_cells.glsl | 23 +++++- 9 files changed, 300 insertions(+), 121 deletions(-) diff --git a/gl/ubo.h b/gl/ubo.h index 564127d43..8f2619f01 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -712,7 +712,9 @@ namespace gl const size_t RAIN_DROP_CELLS = 512; const size_t RAIN_RIV_CELLS = 128; const size_t RAIN_RIV_LAYERS = 5; - const size_t RAIN_CELLS_COUNT = RAIN_DROP_CELLS * RAIN_DROP_CELLS + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS; + // the cells (the first pass of rain_cells.comp), then the reach of the drops into the quarters of each drop cell (the second) + const size_t RAIN_MASK_BASE = RAIN_DROP_CELLS * RAIN_DROP_CELLS + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS; + const size_t RAIN_CELLS_COUNT = RAIN_MASK_BASE + RAIN_DROP_CELLS * RAIN_DROP_CELLS; const size_t RAIN_CELLS_GROUP_SIZE = 64; const GLuint WATER_UBO_BINDING = 6; const size_t WATER_MASK_TEX = MAX_TEXTURES + HELPER_TEXTURES + 13; diff --git a/rendering/opengl46/opengl46water.cpp b/rendering/opengl46/opengl46water.cpp index e9052c53b..741536b99 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -78,8 +78,15 @@ void opengl46_windscreen_water::update_rain_cells(float const Time, float const // the glasses of the frame before read the cells, this one writes them ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); m_raincells->bind_base(); + auto const groups = [](std::size_t const Count) { return static_cast((Count + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE); }; ::glProgramUniform1f(*m_raincellprogram, 0, Time); - m_raincellprogram->dispatch(static_cast((gl::RAIN_CELLS_COUNT + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE)); + ::glProgramUniform1f(*m_raincellprogram, 1, Rain); + // the cells, then the reach of the drops into the quarters of each drop cell, which reads them + ::glProgramUniform1i(*m_raincellprogram, 2, 0); + m_raincellprogram->dispatch(groups(gl::RAIN_MASK_BASE)); + ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + ::glProgramUniform1i(*m_raincellprogram, 2, 1); + m_raincellprogram->dispatch(groups(gl::RAIN_CELLS_COUNT - gl::RAIN_MASK_BASE)); ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); gl::program::unbind(); } diff --git a/rendering/vulkan14/vulkan14water.cpp b/rendering/vulkan14/vulkan14water.cpp index 5c32c2974..c3aefc748 100644 --- a/rendering/vulkan14/vulkan14water.cpp +++ b/rendering/vulkan14/vulkan14water.cpp @@ -209,10 +209,16 @@ void windscreen_water::update_rain_cells(float const Time, float const Rain) struct constants { float time; - float pad[3]; - } const data{Time, {0.f, 0.f, 0.f}}; - m_raincellkernel.dispatch(commands, {buffer_descriptor(m_raincells->handle(), 0, VK_WHOLE_SIZE)}, data, - static_cast((gl::RAIN_CELLS_COUNT + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE)); + float rain; + std::int32_t pass; + float pad; + }; + auto const groups = [](std::size_t const Count) { return static_cast((Count + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE); }; + std::vector const descriptors{buffer_descriptor(m_raincells->handle(), 0, VK_WHOLE_SIZE)}; + // the cells, then the reach of the drops into the quarters of each drop cell, which reads them + m_raincellkernel.dispatch(commands, descriptors, constants{Time, Rain, 0, 0.f}, groups(gl::RAIN_MASK_BASE)); + compute_barrier(commands); + m_raincellkernel.dispatch(commands, descriptors, constants{Time, Rain, 1, 0.f}, groups(gl::RAIN_CELLS_COUNT - gl::RAIN_MASK_BASE)); compute_barrier(commands); } diff --git a/shaders/ogl46/mat_rain_windscreen.frag b/shaders/ogl46/mat_rain_windscreen.frag index 668f21e8a..a7bceccc9 100644 --- a/shaders/ogl46/mat_rain_windscreen.frag +++ b/shaders/ogl46/mat_rain_windscreen.frag @@ -89,11 +89,6 @@ const float WS_ENC = 64.0; // the haze request in the velocity // --- Drops (atlas tiles) ------------------------------------------------------ // (how long they live: rain_cells.glsl; the cells of the drops and of the rivulets are worked out once a frame by rain_cells.comp) -const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) -const float drop_size_min = 0.45; // drop size in grid cells -const float drop_size_max = 1.15; -const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones -const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) // --- Micro droplets ------------------------------------------------------------- const float micro_density = 0.45; // share of micro cells holding a droplet at full rain, 0 = off @@ -153,7 +148,6 @@ const float wind_run_scale = 1.2; // ... and their runs this long against const float wind_head_scale = 0.4; // their heads this much smaller (a small bead at the front of a long trail) const int wind_layers = 4; // layers of them, each with drops of its own (1 .. 4) const float wind_long_run = 3.0; // the last layer: its trails this much longer than those of gravity (the long ones, a part of all) -const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float WS_DRAG_SPEED = 24.0; // speed of the air along the glass at which its drag equals the weight of a drop, m/s const float WS_HEAD_ON = 0.35; // share of the air meeting the glass head on which turns up along it const float WS_WIND_STEP = 0.1309; // the direction of the rivulets of the air in steps of 7.5 degrees (no seams on a @@ -543,12 +537,10 @@ void drawRivulet(inout Hit hit, Riv r, vec2 sUVr, vec2 rd1, vec2 rd2) } // how much of a drop centred at Cg (glass UV, v up) with radius Rad the rivulets of both layers have swept up (0..1 kept) -float wsKept(vec2 cg, float rad, Riv rivP, vec2 sCell, float sCW, float sCH, float gridSize) +float wsKept(vec2 cg, float rad, float sCW, float sCH, float gridSize) { wsSetLayer(0); - vec2 rc = floor(cg / vec2(sCW, sCH)); - Riv rv = rivP; - if (rc != sCell) rv = getRiv(rc, sCW, sCH, gridSize); + Riv rv = getRiv(floor(cg / vec2(sCW, sCH)), sCW, sCH, gridSize); float kept = 1.0 - streak_sweep * rivSwept(rv, cg, rad); if (wsWindShare > 0.001) { @@ -581,13 +573,27 @@ float wsWipeAge(vec2 co) return age; } +// the tangent frame of the glass (the directions of u and v on it, view space) from the screen-space change of the place and the uv +void wsGlassFrame(vec3 Ngl, vec2 duv1, vec2 duv2, out vec3 Tgl, out vec3 Bgl) +{ + vec3 dp1 = dFdx(f_pos.xyz); + vec3 dp2 = dFdy(f_pos.xyz); + vec3 dp2perp = cross(dp2, Ngl); + vec3 dp1perp = cross(Ngl, dp1); + Tgl = dp2perp * duv1.x + dp1perp * duv2.x; + Bgl = dp2perp * duv1.y + dp1perp * duv2.y; + float invmax = inversesqrt(max(max(dot(Tgl, Tgl), dot(Bgl, Bgl)), 1e-20)); + Tgl *= invmax; + Bgl *= invmax; +} + // the drop of a cell for one side of the blades (A, B: its half of the cell in rain_cells): where it lies, how big it is and its // shape (the sag, the stretch by the air along PushT). true when P lies on it bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float speed, vec2 pushT, vec2 p, out vec2 center, out float sq, out float sag, out mat2 shapeM, out vec2 dl) { center = (nc + vec2(rain_byte(a, 8u), rain_byte(a, 16u))) / gridSize; - float size = mix(drop_size_min, drop_size_max, pow(rain_byte(a, 24u), drop_size_bias)) / gridSize; + float size = mix(drop_size_min, drop_size_max, rain_byte(a, 24u)) / gridSize; // the bias of the sizes: rain_cells.comp sag = 1.0 + drop_sag * rain_byte(b, 0u); // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) float stretch = 1.0 + drop_wind_stretch * speed * rain_byte(b, 8u); @@ -600,28 +606,12 @@ bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float s void main() { // ---- screen-space derivatives (before any non-uniform flow) ---- - float uvPerPx = max(length(dFdx(f_coord)), length(dFdy(f_coord))); - vec2 sxy = f_pos.xy / max(-f_pos.z, 1e-3); - float pxPerSlope = 1.0 / max(max(length(dFdx(sxy)), length(dFdy(sxy))), 1e-6); - vec3 Ngl = normalize(f_normal); - vec3 wsView = normalize(f_pos.xyz); // the view through the glass and its change per pixel (for the bent view) - vec3 wsDvx = dFdx(wsView); - vec3 wsDvy = dFdy(wsView); - vec3 dp1 = dFdx(f_pos.xyz); - vec3 dp2 = dFdy(f_pos.xyz); + // only what the drops, the droplets and the rivulets read is kept through their loops; the frame of the glass, the change of the + // view and the lens are worked out again for the shading after them (fewer registers in the loops: more pixels at once) vec2 duv1 = dFdx(f_coord); vec2 duv2 = dFdy(f_coord); - vec3 dp2perp = cross(dp2, Ngl); - vec3 dp1perp = cross(Ngl, dp1); - vec3 Tgl = dp2perp * duv1.x + dp1perp * duv2.x; - vec3 Bgl = dp2perp * duv1.y + dp1perp * duv2.y; - float invmax = inversesqrt(max(max(dot(Tgl, Tgl), dot(Bgl, Bgl)), 1e-20)); - Tgl *= invmax; - Bgl *= invmax; - // glass UV offset -> screen pixels (inverse of the UV Jacobian), for the drop lens - mat2 Juv = mat2(duv1, duv2); - float detJ = determinant(Juv); - mat2 Jinv = (abs(detJ) > 1e-20) ? inverse(Juv) : mat2(0.0); + float uvPerPx = max(length(duv1), length(duv2)); + vec3 Ngl = normalize(f_normal); vec4 tex_color = texture(diffuse, f_coord); float glassA = tex_color.a * alpha_mult; @@ -647,11 +637,16 @@ void main() hit.offUV = vec2(0.0); hit.radUV = 1.0; hit.isDrop = 1.0; hit.vis = 0.0; hit.auv = vec2(0.0); hit.agx = vec2(0.0); hit.agy = vec2(0.0); hit.atlas = 0.0; hit.shade = 0.0; - // ---- the air against the vehicle on the glass (uniform flow: the derivatives of the glass above) ---- + // ---- the air against the vehicle on the glass (uniform flow: the derivatives of the glass) ---- + vec2 wsPushT; + float wsSpeed; float flipS = (STREAK_FLIP_Y != 0) ? -1.0 : 1.0; // texture v -> glass v (up) + vec3 wsN = (dot(Ngl, f_pos.xyz) < 0.0) ? -Ngl : Ngl; // the outside of the glass + { + vec3 Tgl, Bgl; + wsGlassFrame(Ngl, duv1, duv2, Tgl, Bgl); vec3 wsUp = vec3(0.0, 1.0, 0.0) * mat3(inv_view); // world up, view space vec3 wsAir = rain_wind.xyz * mat3(inv_view); // the air against the vehicle, view space, m/s - vec3 wsN = (dot(Ngl, f_pos.xyz) < 0.0) ? -Ngl : Ngl; // the outside of the glass vec3 wsGrav = -wsUp - wsN * dot(-wsUp, wsN); // the weight along the glass vec3 wsRise = wsUp - wsN * dot(wsUp, wsN); wsRise = (length(wsRise) > 0.1) ? normalize(wsRise) : vec3(0.0); @@ -662,7 +657,7 @@ void main() // to the glass UV (v up) through the tangent frame of the texture vec2 gravG = vec2(dot(wsGrav, Tgl), dot(wsGrav, Bgl) * flipS); vec2 flowG = vec2(dot(wsFlowDir, Tgl), dot(wsFlowDir, Bgl) * flipS); - float wsSpeed = smoothstep(0.3, 1.2, wsDrag); // 0 standing still .. 1 at speed (haze, drops) + wsSpeed = smoothstep(0.3, 1.2, wsDrag); // 0 standing still .. 1 at speed (haze, drops) wsWindShare = smoothstep(0.5, 1.3, wsDrag); // how much the air drives the rivulets if (wsWindShare > 0.0) { @@ -680,8 +675,9 @@ void main() wsWindShare = 0.0; } // the push of the air on the drops, texture UV (for stretching them) - vec2 wsPushT = vec2(dot(wsPush, Tgl), dot(wsPush, Bgl)); + wsPushT = vec2(dot(wsPush, Tgl), dot(wsPush, Bgl)); wsPushT = (dot(wsPushT, wsPushT) > 1e-10) ? normalize(wsPushT) : vec2(0.0, -1.0); + } // the change of the glass UV (v up) per screen pixel, for the rivulets vec2 wsRd1 = vec2(duv1.x, flipS * duv1.y); vec2 wsRd2 = vec2(duv2.x, flipS * duv2.y); @@ -692,7 +688,6 @@ void main() float sCH = streak_run / gridSize; vec2 sUVg = vec2(f_coord.x, (STREAK_FLIP_Y != 0) ? 1.0 - f_coord.y : f_coord.y); // glass UV, v up vec2 sCell = floor(sUVg / vec2(sCW, sCH)); - Riv rivP = getRiv(sCell, sCW, sCH, gridSize); bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0) && (WS_PROFILE != 2); if ((rain_params.x > 0.001) && (WS_PROFILE != 5)) @@ -700,20 +695,20 @@ void main() // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== - vec2 cell = floor(rainCoord * gridSize); + vec2 cellF = rainCoord * gridSize; + vec2 cell = floor(cellF); #if WS_PROFILE != 4 - float actMax = min(rain_params.x * drop_density, 0.95); - for (int oy = -1; oy <= 1; oy++) - for (int ox = -1; ox <= 1; ox++) + // the drops of the 9 cells around which reach the quarter of the cell the pixel lies in (worked out by rain_cells.comp, + // with the drops there are now): usually 1 .. 3 of them + uvec2 quarter = uvec2(greaterThanEqual(cellF - cell, vec2(0.5))); + uint reach = rain_cells[rain_mask_index(ivec2(cell))][quarter.y * 2u + quarter.x]; + while (reach != 0u) { - vec2 nc = cell + vec2(ox, oy); - // the drop of the cell as it is now (rain_cells.comp): none, or the one of either side of the blades + int bit = findLSB(reach); + reach &= reach - 1u; + vec2 nc = cell + vec2(float(bit % 3 - 1), float(bit / 3 - 1)); uvec4 d = rain_cells[rain_drop_index(ivec2(nc))]; - if (rain_byte(d.y, 24u) > actMax) - continue; float vis = float(d.w >> 24) * (1.0 / 255.0); - if (vis <= 0.001) - continue; float spreadF = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); // a drop the blades miss: the same drop before and after the stroke, left where it is bool missed = ((d.w >> 19) & 1u) != 0u; @@ -745,7 +740,7 @@ void main() if (sweepOn) { vec2 cg = vec2(center.x, (STREAK_FLIP_Y != 0) ? 1.0 - center.y : center.y); - vis *= wsKept(cg, 0.35 * sq, rivP, sCell, sCW, sCH, gridSize); + vis *= wsKept(cg, 0.35 * sq, sCW, sCH, gridSize); } if (vis <= 0.001) continue; @@ -804,7 +799,7 @@ void main() { vec2 cu = c / mg; vec2 cg = vec2(cu.x, (STREAK_FLIP_Y != 0) ? 1.0 - cu.y : cu.y); - vis *= wsKept(cg, r / mg, rivP, sCell, sCW, sCH, gridSize); + vis *= wsKept(cg, r / mg, sCW, sCH, gridSize); } float d = sqrt(d2); float cov = smoothstep(1.0, 0.8, d) * vis; @@ -830,7 +825,7 @@ void main() // =================================================================== #if WS_PROFILE != 2 wsSetLayer(0); - drawRivulet(hit, rivP, sUVg, wsRd1, wsRd2); + drawRivulet(hit, getRiv(sCell, sCW, sCH, gridSize), sUVg, wsRd1, wsRd2); if (wsWindShare > 0.001) { for (int wl = 1; wl <= wind_layers; ++wl) @@ -847,6 +842,21 @@ void main() #endif } + // ---- the frame of the glass, the change of the view and the lens, for the shading (uniform flow again) ---- + vec2 sxy = f_pos.xy / max(-f_pos.z, 1e-3); + float pxPerSlope = 1.0 / max(max(length(dFdx(sxy)), length(dFdy(sxy))), 1e-6); + vec3 wsView = normalize(f_pos.xyz); // the view through the glass and its change per pixel (for the bent view) + vec3 wsDvx = dFdx(wsView); + vec3 wsDvy = dFdy(wsView); + vec3 dp1 = dFdx(f_pos.xyz); + vec3 dp2 = dFdy(f_pos.xyz); + vec3 Tgl, Bgl; + wsGlassFrame(Ngl, duv1, duv2, Tgl, Bgl); + // glass UV offset -> screen pixels (inverse of the UV Jacobian), for the drop lens + mat2 Juv = mat2(duv1, duv2); + float detJ = determinant(Juv); + mat2 Jinv = (abs(detJ) > 1e-20) ? inverse(Juv) : mat2(0.0); + // ----------------------------------------------------------------------- // Shading of the winning dome // ----------------------------------------------------------------------- diff --git a/shaders/ogl46/rain_cells.comp b/shaders/ogl46/rain_cells.comp index 7cece6a0b..6916c3eab 100644 --- a/shaders/ogl46/rain_cells.comp +++ b/shaders/ogl46/rain_cells.comp @@ -1,11 +1,14 @@ #version 460 core #define STAGE_COMPUTE 1 // the cells of the glasses with rain (rain_cells.glsl): the drops and the rivulets of every cell worked out for the time of the -// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them) +// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them). two passes: the +// cells (rain_pass 0), then which drops reach the quarters of each drop cell (rain_pass 1, it reads the first) #include "common.glsl" -layout(location = 0) uniform float rain_time; // the time of the block of the scene (scene_ubs.time) +layout(location = 0) uniform float rain_time; // the time of the block of the scene (scene_ubs.time) +layout(location = 1) uniform float rain_amount; // how hard it rains (scene_ubs.rain_params.x) +layout(location = 2) uniform int rain_pass; // 0 the cells, 1 the reach of the drops #include "random.glsl" #include "rain_cells.glsl" @@ -60,7 +63,7 @@ uvec4 drop_cell(ivec2 Cell) float choice = RandF(s2); float stretch = RandF(s2); uint tile = min(uint(choice * 4.0), 3u); - uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(size) << 24); + uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(pow(size, drop_size_bias)) << 24); uint b = q8(sag) | (q8(stretch) << 8) | ((flip < 0.5) ? (1u << 16) : 0u) | (tile << 17); if (side == 0u) { @@ -108,10 +111,64 @@ uvec4 rivulet_cell(int Layer, ivec2 Cell) return result; } +// how far from the centre (in cells) the drop of side half A, B of a cell can reach at most: half its size, the sag, the stretch of the +// air at its strongest, any way it is turned +float drop_reach(uint A, uint B, float Spread) +{ + float half_size = 0.5 * mix(drop_size_min, drop_size_max, rain_byte(A, 24u)) * Spread; + float sag = 1.0 + drop_sag * rain_byte(B, 0u); + float stretch = 1.0 + drop_wind_stretch * rain_byte(B, 8u); + return half_size * sqrt(1.0 + sag * sag) * stretch * 1.01; +} + +// the reach of the drops around a drop cell into its quarters (rain_cells.glsl), from the cells worked out by the first pass +uvec4 drop_masks(ivec2 Cell) +{ + uvec4 result = uvec4(0u); + float most = min(rain_amount * drop_density, 0.95); + for (int dy = -1; dy <= 1; ++dy) + for (int dx = -1; dx <= 1; ++dx) + { + uvec4 d = rain_cells[rain_drop_index(Cell + ivec2(dx, dy))]; + if ((rain_byte(d.y, 24u) > most) || ((d.w >> 24) == 0u)) + continue; + float spread = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); + bool missed = ((d.w >> 19) & 1u) != 0u; + uint bit = 1u << uint((dy + 1) * 3 + dx + 1); + for (uint side = 0u; side < 2u; ++side) + { + if ((side == 1u) && missed) + break; + uint a = (side == 0u) ? d.x : d.z; + uint b = (side == 0u) ? d.y : d.w; + vec2 centre = vec2(dx, dy) + vec2(rain_byte(a, 8u), rain_byte(a, 16u)); + float reach = drop_reach(a, b, spread); + for (uint q = 0u; q < 4u; ++q) + { + vec2 low = vec2(float(q & 1u), float(q >> 1)) * 0.5; + vec2 away = max(max(low - centre, centre - (low + 0.5)), vec2(0.0)); + if (dot(away, away) < reach * reach) + result[q] |= bit; + } + } + } + return result; +} + void main() { uint index = gl_GlobalInvocationID.x; - if (index >= uint(RAIN_CELLS_COUNT)) + if (rain_pass == 1) + { + // the second pass: the reach of the drops, a thread a drop cell + if (index >= uint(RAIN_DROP_CELLS * RAIN_DROP_CELLS)) + return; + ivec2 texel = ivec2(int(index % uint(RAIN_DROP_CELLS)), int(index / uint(RAIN_DROP_CELLS))); + ivec2 cell = ivec2(texel.x == RAIN_DROP_CELLS - 1 ? -1 : texel.x, texel.y == RAIN_DROP_CELLS - 1 ? -1 : texel.y); + rain_cells[uint(RAIN_MASK_BASE) + index] = drop_masks(cell); + return; + } + if (index >= uint(RAIN_MASK_BASE)) return; if (index < uint(RAIN_RIV_BASE)) { diff --git a/shaders/ogl46/rain_cells.glsl b/shaders/ogl46/rain_cells.glsl index 97819d724..52e248032 100644 --- a/shaders/ogl46/rain_cells.glsl +++ b/shaders/ogl46/rain_cells.glsl @@ -7,11 +7,14 @@ // a cell is one uvec4. the drops: RAIN_DROP_CELLS x RAIN_DROP_CELLS cells, cell c of the grid in element (c.y & 511) * 512 + // (c.x & 511), whole for the cells -1 .. 510 (grids up to 510 cells; finer ones repeat their drops every 512 cells): // x side 0 (the drop of a cell the blades went over on their way out): bits 0-7 the threshold the wipers have to be past for -// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 the random of its size +// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 its size (0 .. 1 from drop_size_min to drop_size_max) // y side 0: bits 0-7 the random of its sag, 8-15 of its stretch by the air, 16 mirrored, 17-18 the tile of the atlas, // 19-23 how far it spread out landing (0 .. 31), 24-31 the random a cell holds a drop by (a drop when below the share) // z side 1 (the blades on their way back): as x // w side 1: as y, bit 19 set when the blades miss the drop, bits 24-31 its visibility in its life (0 .. 255) +// the reach of the drops: for every drop cell (element RAIN_MASK_BASE + the index of the drop cell) the drops of the 9 cells around +// it (bit (dy + 1) * 3 + dx + 1) which reach into each quarter of it (x the lower left, y the lower right, z the upper left, w the +// upper right), with the drops the cells hold now in the rain there is now (a glass only looks at those) // the rivulets: RAIN_RIV_LAYERS layers (0 gravity, 1 .. 4 the air) of RAIN_RIV_CELLS x RAIN_RIV_CELLS cells, cell c in element // RAIN_RIV_BASE + layer * 128 * 128 + (c.y & 127) * 128 + (c.x & 127), whole for the cells -32 .. 95: // x bits 0-15 the random the cell holds a rivulet by, 16-23 its life (0 .. 255), 24-31 the random of the wipers it stays under @@ -26,11 +29,12 @@ #define RAIN_RIV_CELLS 128 #define RAIN_RIV_LAYERS 5 #define RAIN_RIV_BASE (RAIN_DROP_CELLS * RAIN_DROP_CELLS) -#define RAIN_CELLS_COUNT (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_MASK_BASE (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_CELLS_COUNT (RAIN_MASK_BASE + RAIN_DROP_CELLS * RAIN_DROP_CELLS) #define RAIN_CELLS_GROUP_SIZE 64 #if defined(STAGE_COMPUTE) -layout(std430, binding = RAIN_CELLS_SSBO_BINDING) writeonly buffer rain_cells_ssbo +layout(std430, binding = RAIN_CELLS_SSBO_BINDING) buffer rain_cells_ssbo #else layout(std430, binding = RAIN_CELLS_SSBO_BINDING) readonly buffer rain_cells_ssbo #endif @@ -38,7 +42,13 @@ layout(std430, binding = RAIN_CELLS_SSBO_BINDING) readonly buffer rain_cells_ssb uvec4 rain_cells[]; }; -// the life of the drops and of the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +// the drops and the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) +const float drop_size_min = 0.45; // drop size in grid cells +const float drop_size_max = 1.15; +const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones +const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) +const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float drop_life_min = 14.0; // seconds a drop sits on the glass const float drop_life_max = 45.0; const float drop_appear = 0.18; // seconds of the landing "pop" @@ -58,6 +68,11 @@ uint rain_drop_index(ivec2 Cell) return uint(Cell.y & (RAIN_DROP_CELLS - 1)) * uint(RAIN_DROP_CELLS) + uint(Cell.x & (RAIN_DROP_CELLS - 1)); } +uint rain_mask_index(ivec2 Cell) +{ + return uint(RAIN_MASK_BASE) + rain_drop_index(Cell); +} + uint rain_riv_index(int Layer, ivec2 Cell) { return uint(RAIN_RIV_BASE) + uint(Layer) * uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS) + uint(Cell.y & (RAIN_RIV_CELLS - 1)) * uint(RAIN_RIV_CELLS) diff --git a/shaders/vk14/mat_rain_windscreen.frag b/shaders/vk14/mat_rain_windscreen.frag index 3ac5ca31a..9b8bfb2a8 100644 --- a/shaders/vk14/mat_rain_windscreen.frag +++ b/shaders/vk14/mat_rain_windscreen.frag @@ -94,11 +94,6 @@ const float WS_ENC = 64.0; // the haze request in the velocity // --- Drops (atlas tiles) ------------------------------------------------------ // (how long they live: rain_cells.glsl; the cells of the drops and of the rivulets are worked out once a frame by rain_cells.comp) -const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) -const float drop_size_min = 0.45; // drop size in grid cells -const float drop_size_max = 1.15; -const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones -const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) // --- Micro droplets ------------------------------------------------------------- const float micro_density = 0.45; // share of micro cells holding a droplet at full rain, 0 = off @@ -158,7 +153,6 @@ const float wind_run_scale = 1.2; // ... and their runs this long against const float wind_head_scale = 0.4; // their heads this much smaller (a small bead at the front of a long trail) const int wind_layers = 4; // layers of them, each with drops of its own (1 .. 4) const float wind_long_run = 3.0; // the last layer: its trails this much longer than those of gravity (the long ones, a part of all) -const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float WS_DRAG_SPEED = 24.0; // speed of the air along the glass at which its drag equals the weight of a drop, m/s const float WS_HEAD_ON = 0.35; // share of the air meeting the glass head on which turns up along it const float WS_WIND_STEP = 0.1309; // the direction of the rivulets of the air in steps of 7.5 degrees (no seams on a @@ -548,12 +542,10 @@ void drawRivulet(inout Hit hit, Riv r, vec2 sUVr, vec2 rd1, vec2 rd2) } // how much of a drop centred at Cg (glass UV, v up) with radius Rad the rivulets of both layers have swept up (0..1 kept) -float wsKept(vec2 cg, float rad, Riv rivP, vec2 sCell, float sCW, float sCH, float gridSize) +float wsKept(vec2 cg, float rad, float sCW, float sCH, float gridSize) { wsSetLayer(0); - vec2 rc = floor(cg / vec2(sCW, sCH)); - Riv rv = rivP; - if (rc != sCell) rv = getRiv(rc, sCW, sCH, gridSize); + Riv rv = getRiv(floor(cg / vec2(sCW, sCH)), sCW, sCH, gridSize); float kept = 1.0 - streak_sweep * rivSwept(rv, cg, rad); if (wsWindShare > 0.001) { @@ -586,13 +578,27 @@ float wsWipeAge(vec2 co) return age; } +// the tangent frame of the glass (the directions of u and v on it, view space) from the screen-space change of the place and the uv +void wsGlassFrame(vec3 Ngl, vec2 duv1, vec2 duv2, out vec3 Tgl, out vec3 Bgl) +{ + vec3 dp1 = dFdx(f_pos.xyz); + vec3 dp2 = dFdy(f_pos.xyz); + vec3 dp2perp = cross(dp2, Ngl); + vec3 dp1perp = cross(Ngl, dp1); + Tgl = dp2perp * duv1.x + dp1perp * duv2.x; + Bgl = dp2perp * duv1.y + dp1perp * duv2.y; + float invmax = inversesqrt(max(max(dot(Tgl, Tgl), dot(Bgl, Bgl)), 1e-20)); + Tgl *= invmax; + Bgl *= invmax; +} + // the drop of a cell for one side of the blades (A, B: its half of the cell in rain_cells): where it lies, how big it is and its // shape (the sag, the stretch by the air along PushT). true when P lies on it bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float speed, vec2 pushT, vec2 p, out vec2 center, out float sq, out float sag, out mat2 shapeM, out vec2 dl) { center = (nc + vec2(rain_byte(a, 8u), rain_byte(a, 16u))) / gridSize; - float size = mix(drop_size_min, drop_size_max, pow(rain_byte(a, 24u), drop_size_bias)) / gridSize; + float size = mix(drop_size_min, drop_size_max, rain_byte(a, 24u)) / gridSize; // the bias of the sizes: rain_cells.comp sag = 1.0 + drop_sag * rain_byte(b, 0u); // at speed the air stretches the drop along its push (1 standing still: the drop as it always was) float stretch = 1.0 + drop_wind_stretch * speed * rain_byte(b, 8u); @@ -605,28 +611,12 @@ bool wsDropShape(uint a, uint b, vec2 nc, float gridSize, float spreadF, float s void main() { // ---- screen-space derivatives (before any non-uniform flow) ---- - float uvPerPx = max(length(dFdx(f_coord)), length(dFdy(f_coord))); - vec2 sxy = f_pos.xy / max(-f_pos.z, 1e-3); - float pxPerSlope = 1.0 / max(max(length(dFdx(sxy)), length(dFdy(sxy))), 1e-6); - vec3 Ngl = normalize(f_normal); - vec3 wsView = normalize(f_pos.xyz); // the view through the glass and its change per pixel (for the bent view) - vec3 wsDvx = dFdx(wsView); - vec3 wsDvy = dFdy(wsView); - vec3 dp1 = dFdx(f_pos.xyz); - vec3 dp2 = dFdy(f_pos.xyz); + // only what the drops, the droplets and the rivulets read is kept through their loops; the frame of the glass, the change of the + // view and the lens are worked out again for the shading after them (fewer registers in the loops: more pixels at once) vec2 duv1 = dFdx(f_coord); vec2 duv2 = dFdy(f_coord); - vec3 dp2perp = cross(dp2, Ngl); - vec3 dp1perp = cross(Ngl, dp1); - vec3 Tgl = dp2perp * duv1.x + dp1perp * duv2.x; - vec3 Bgl = dp2perp * duv1.y + dp1perp * duv2.y; - float invmax = inversesqrt(max(max(dot(Tgl, Tgl), dot(Bgl, Bgl)), 1e-20)); - Tgl *= invmax; - Bgl *= invmax; - // glass UV offset -> screen pixels (inverse of the UV Jacobian), for the drop lens - mat2 Juv = mat2(duv1, duv2); - float detJ = determinant(Juv); - mat2 Jinv = (abs(detJ) > 1e-20) ? inverse(Juv) : mat2(0.0); + float uvPerPx = max(length(duv1), length(duv2)); + vec3 Ngl = normalize(f_normal); vec4 tex_color = texture(diffuse, f_coord); float glassA = tex_color.a * alpha_mult; @@ -652,11 +642,16 @@ void main() hit.offUV = vec2(0.0); hit.radUV = 1.0; hit.isDrop = 1.0; hit.vis = 0.0; hit.auv = vec2(0.0); hit.agx = vec2(0.0); hit.agy = vec2(0.0); hit.atlas = 0.0; hit.shade = 0.0; - // ---- the air against the vehicle on the glass (uniform flow: the derivatives of the glass above) ---- + // ---- the air against the vehicle on the glass (uniform flow: the derivatives of the glass) ---- + vec2 wsPushT; + float wsSpeed; float flipS = (STREAK_FLIP_Y != 0) ? -1.0 : 1.0; // texture v -> glass v (up) + vec3 wsN = (dot(Ngl, f_pos.xyz) < 0.0) ? -Ngl : Ngl; // the outside of the glass + { + vec3 Tgl, Bgl; + wsGlassFrame(Ngl, duv1, duv2, Tgl, Bgl); vec3 wsUp = vec3(0.0, 1.0, 0.0) * mat3(inv_view); // world up, view space vec3 wsAir = rain_wind.xyz * mat3(inv_view); // the air against the vehicle, view space, m/s - vec3 wsN = (dot(Ngl, f_pos.xyz) < 0.0) ? -Ngl : Ngl; // the outside of the glass vec3 wsGrav = -wsUp - wsN * dot(-wsUp, wsN); // the weight along the glass vec3 wsRise = wsUp - wsN * dot(wsUp, wsN); wsRise = (length(wsRise) > 0.1) ? normalize(wsRise) : vec3(0.0); @@ -667,7 +662,7 @@ void main() // to the glass UV (v up) through the tangent frame of the texture vec2 gravG = vec2(dot(wsGrav, Tgl), dot(wsGrav, Bgl) * flipS); vec2 flowG = vec2(dot(wsFlowDir, Tgl), dot(wsFlowDir, Bgl) * flipS); - float wsSpeed = smoothstep(0.3, 1.2, wsDrag); // 0 standing still .. 1 at speed (haze, drops) + wsSpeed = smoothstep(0.3, 1.2, wsDrag); // 0 standing still .. 1 at speed (haze, drops) wsWindShare = smoothstep(0.5, 1.3, wsDrag); // how much the air drives the rivulets if (wsWindShare > 0.0) { @@ -685,8 +680,9 @@ void main() wsWindShare = 0.0; } // the push of the air on the drops, texture UV (for stretching them) - vec2 wsPushT = vec2(dot(wsPush, Tgl), dot(wsPush, Bgl)); + wsPushT = vec2(dot(wsPush, Tgl), dot(wsPush, Bgl)); wsPushT = (dot(wsPushT, wsPushT) > 1e-10) ? normalize(wsPushT) : vec2(0.0, -1.0); + } // the change of the glass UV (v up) per screen pixel, for the rivulets vec2 wsRd1 = vec2(duv1.x, flipS * duv1.y); vec2 wsRd2 = vec2(duv2.x, flipS * duv2.y); @@ -697,7 +693,6 @@ void main() float sCH = streak_run / gridSize; vec2 sUVg = vec2(f_coord.x, (STREAK_FLIP_Y != 0) ? 1.0 - f_coord.y : f_coord.y); // glass UV, v up vec2 sCell = floor(sUVg / vec2(sCW, sCH)); - Riv rivP = getRiv(sCell, sCW, sCH, gridSize); bool sweepOn = streak_sweep > 0.0 && streak_density > 0.0 && (wind_streaks > 0.0 || wsWindShare < 1.0) && (WS_PROFILE != 2); if ((rain_params.x > 0.001) && (WS_PROFILE != 5)) @@ -705,20 +700,20 @@ void main() // =================================================================== // 1. drops from the atlas: this cell and its 8 neighbours // =================================================================== - vec2 cell = floor(rainCoord * gridSize); + vec2 cellF = rainCoord * gridSize; + vec2 cell = floor(cellF); #if WS_PROFILE != 4 - float actMax = min(rain_params.x * drop_density, 0.95); - for (int oy = -1; oy <= 1; oy++) - for (int ox = -1; ox <= 1; ox++) + // the drops of the 9 cells around which reach the quarter of the cell the pixel lies in (worked out by rain_cells.comp, + // with the drops there are now): usually 1 .. 3 of them + uvec2 quarter = uvec2(greaterThanEqual(cellF - cell, vec2(0.5))); + uint reach = rain_cells[rain_mask_index(ivec2(cell))][quarter.y * 2u + quarter.x]; + while (reach != 0u) { - vec2 nc = cell + vec2(ox, oy); - // the drop of the cell as it is now (rain_cells.comp): none, or the one of either side of the blades + int bit = findLSB(reach); + reach &= reach - 1u; + vec2 nc = cell + vec2(float(bit % 3 - 1), float(bit / 3 - 1)); uvec4 d = rain_cells[rain_drop_index(ivec2(nc))]; - if (rain_byte(d.y, 24u) > actMax) - continue; float vis = float(d.w >> 24) * (1.0 / 255.0); - if (vis <= 0.001) - continue; float spreadF = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); // a drop the blades miss: the same drop before and after the stroke, left where it is bool missed = ((d.w >> 19) & 1u) != 0u; @@ -750,7 +745,7 @@ void main() if (sweepOn) { vec2 cg = vec2(center.x, (STREAK_FLIP_Y != 0) ? 1.0 - center.y : center.y); - vis *= wsKept(cg, 0.35 * sq, rivP, sCell, sCW, sCH, gridSize); + vis *= wsKept(cg, 0.35 * sq, sCW, sCH, gridSize); } if (vis <= 0.001) continue; @@ -809,7 +804,7 @@ void main() { vec2 cu = c / mg; vec2 cg = vec2(cu.x, (STREAK_FLIP_Y != 0) ? 1.0 - cu.y : cu.y); - vis *= wsKept(cg, r / mg, rivP, sCell, sCW, sCH, gridSize); + vis *= wsKept(cg, r / mg, sCW, sCH, gridSize); } float d = sqrt(d2); float cov = smoothstep(1.0, 0.8, d) * vis; @@ -835,7 +830,7 @@ void main() // =================================================================== #if WS_PROFILE != 2 wsSetLayer(0); - drawRivulet(hit, rivP, sUVg, wsRd1, wsRd2); + drawRivulet(hit, getRiv(sCell, sCW, sCH, gridSize), sUVg, wsRd1, wsRd2); if (wsWindShare > 0.001) { for (int wl = 1; wl <= wind_layers; ++wl) @@ -852,6 +847,21 @@ void main() #endif } + // ---- the frame of the glass, the change of the view and the lens, for the shading (uniform flow again) ---- + vec2 sxy = f_pos.xy / max(-f_pos.z, 1e-3); + float pxPerSlope = 1.0 / max(max(length(dFdx(sxy)), length(dFdy(sxy))), 1e-6); + vec3 wsView = normalize(f_pos.xyz); // the view through the glass and its change per pixel (for the bent view) + vec3 wsDvx = dFdx(wsView); + vec3 wsDvy = dFdy(wsView); + vec3 dp1 = dFdx(f_pos.xyz); + vec3 dp2 = dFdy(f_pos.xyz); + vec3 Tgl, Bgl; + wsGlassFrame(Ngl, duv1, duv2, Tgl, Bgl); + // glass UV offset -> screen pixels (inverse of the UV Jacobian), for the drop lens + mat2 Juv = mat2(duv1, duv2); + float detJ = determinant(Juv); + mat2 Jinv = (abs(detJ) > 1e-20) ? inverse(Juv) : mat2(0.0); + // ----------------------------------------------------------------------- // Shading of the winning dome // ----------------------------------------------------------------------- diff --git a/shaders/vk14/rain_cells.comp b/shaders/vk14/rain_cells.comp index c8ca2af4b..780addf0e 100644 --- a/shaders/vk14/rain_cells.comp +++ b/shaders/vk14/rain_cells.comp @@ -1,15 +1,18 @@ #version 460 core #define STAGE_COMPUTE 1 // the cells of the glasses with rain (rain_cells.glsl): the drops and the rivulets of every cell worked out for the time of the -// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them) +// frame, a thread a cell, before the passes which draw the glasses (mat_rain_windscreen.frag only reads them). two passes: the +// cells (rain_pass 0), then which drops reach the quarters of each drop cell (rain_pass 1, it reads the first) -// none of the pass set: the cells pushed with the dispatch (set COMPUTE_SET), the time in the push constants +// none of the pass set: the cells pushed with the dispatch (set COMPUTE_SET), the time and the pass in the push constants #define LAYOUT_UI 1 #include "common.glsl" layout(push_constant) uniform kernel_constants { - float rain_time; // the time of the block of the scene (scene_ubs.time) + float rain_time; // the time of the block of the scene (scene_ubs.time) + float rain_amount; // how hard it rains (scene_ubs.rain_params.x) + int rain_pass; // 0 the cells, 1 the reach of the drops }; #include "random.glsl" @@ -65,7 +68,7 @@ uvec4 drop_cell(ivec2 Cell) float choice = RandF(s2); float stretch = RandF(s2); uint tile = min(uint(choice * 4.0), 3u); - uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(size) << 24); + uint a = q8(thr) | (q8(cx) << 8) | (q8(cy) << 16) | (q8(pow(size, drop_size_bias)) << 24); uint b = q8(sag) | (q8(stretch) << 8) | ((flip < 0.5) ? (1u << 16) : 0u) | (tile << 17); if (side == 0u) { @@ -113,10 +116,64 @@ uvec4 rivulet_cell(int Layer, ivec2 Cell) return result; } +// how far from the centre (in cells) the drop of side half A, B of a cell can reach at most: half its size, the sag, the stretch of the +// air at its strongest, any way it is turned +float drop_reach(uint A, uint B, float Spread) +{ + float half_size = 0.5 * mix(drop_size_min, drop_size_max, rain_byte(A, 24u)) * Spread; + float sag = 1.0 + drop_sag * rain_byte(B, 0u); + float stretch = 1.0 + drop_wind_stretch * rain_byte(B, 8u); + return half_size * sqrt(1.0 + sag * sag) * stretch * 1.01; +} + +// the reach of the drops around a drop cell into its quarters (rain_cells.glsl), from the cells worked out by the first pass +uvec4 drop_masks(ivec2 Cell) +{ + uvec4 result = uvec4(0u); + float most = min(rain_amount * drop_density, 0.95); + for (int dy = -1; dy <= 1; ++dy) + for (int dx = -1; dx <= 1; ++dx) + { + uvec4 d = rain_cells[rain_drop_index(Cell + ivec2(dx, dy))]; + if ((rain_byte(d.y, 24u) > most) || ((d.w >> 24) == 0u)) + continue; + float spread = mix(0.8, 1.0, float((d.y >> 19) & 31u) * (1.0 / 31.0)); + bool missed = ((d.w >> 19) & 1u) != 0u; + uint bit = 1u << uint((dy + 1) * 3 + dx + 1); + for (uint side = 0u; side < 2u; ++side) + { + if ((side == 1u) && missed) + break; + uint a = (side == 0u) ? d.x : d.z; + uint b = (side == 0u) ? d.y : d.w; + vec2 centre = vec2(dx, dy) + vec2(rain_byte(a, 8u), rain_byte(a, 16u)); + float reach = drop_reach(a, b, spread); + for (uint q = 0u; q < 4u; ++q) + { + vec2 low = vec2(float(q & 1u), float(q >> 1)) * 0.5; + vec2 away = max(max(low - centre, centre - (low + 0.5)), vec2(0.0)); + if (dot(away, away) < reach * reach) + result[q] |= bit; + } + } + } + return result; +} + void main() { uint index = gl_GlobalInvocationID.x; - if (index >= uint(RAIN_CELLS_COUNT)) + if (rain_pass == 1) + { + // the second pass: the reach of the drops, a thread a drop cell + if (index >= uint(RAIN_DROP_CELLS * RAIN_DROP_CELLS)) + return; + ivec2 texel = ivec2(int(index % uint(RAIN_DROP_CELLS)), int(index / uint(RAIN_DROP_CELLS))); + ivec2 cell = ivec2(texel.x == RAIN_DROP_CELLS - 1 ? -1 : texel.x, texel.y == RAIN_DROP_CELLS - 1 ? -1 : texel.y); + rain_cells[uint(RAIN_MASK_BASE) + index] = drop_masks(cell); + return; + } + if (index >= uint(RAIN_MASK_BASE)) return; if (index < uint(RAIN_RIV_BASE)) { diff --git a/shaders/vk14/rain_cells.glsl b/shaders/vk14/rain_cells.glsl index e61cfe4f6..0629492e2 100644 --- a/shaders/vk14/rain_cells.glsl +++ b/shaders/vk14/rain_cells.glsl @@ -7,11 +7,14 @@ // a cell is one uvec4. the drops: RAIN_DROP_CELLS x RAIN_DROP_CELLS cells, cell c of the grid in element (c.y & 511) * 512 + // (c.x & 511), whole for the cells -1 .. 510 (grids up to 510 cells; finer ones repeat their drops every 512 cells): // x side 0 (the drop of a cell the blades went over on their way out): bits 0-7 the threshold the wipers have to be past for -// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 the random of its size +// it, 8-15 / 16-23 the place in the cell (x, y), 24-31 its size (0 .. 1 from drop_size_min to drop_size_max) // y side 0: bits 0-7 the random of its sag, 8-15 of its stretch by the air, 16 mirrored, 17-18 the tile of the atlas, // 19-23 how far it spread out landing (0 .. 31), 24-31 the random a cell holds a drop by (a drop when below the share) // z side 1 (the blades on their way back): as x // w side 1: as y, bit 19 set when the blades miss the drop, bits 24-31 its visibility in its life (0 .. 255) +// the reach of the drops: for every drop cell (element RAIN_MASK_BASE + the index of the drop cell) the drops of the 9 cells around +// it (bit (dy + 1) * 3 + dx + 1) which reach into each quarter of it (x the lower left, y the lower right, z the upper left, w the +// upper right), with the drops the cells hold now in the rain there is now (a glass only looks at those) // the rivulets: RAIN_RIV_LAYERS layers (0 gravity, 1 .. 4 the air) of RAIN_RIV_CELLS x RAIN_RIV_CELLS cells, cell c in element // RAIN_RIV_BASE + layer * 128 * 128 + (c.y & 127) * 128 + (c.x & 127), whole for the cells -32 .. 95: // x bits 0-15 the random the cell holds a rivulet by, 16-23 its life (0 .. 255), 24-31 the random of the wipers it stays under @@ -26,11 +29,12 @@ #define RAIN_RIV_CELLS 128 #define RAIN_RIV_LAYERS 5 #define RAIN_RIV_BASE (RAIN_DROP_CELLS * RAIN_DROP_CELLS) -#define RAIN_CELLS_COUNT (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_MASK_BASE (RAIN_RIV_BASE + RAIN_RIV_LAYERS * RAIN_RIV_CELLS * RAIN_RIV_CELLS) +#define RAIN_CELLS_COUNT (RAIN_MASK_BASE + RAIN_DROP_CELLS * RAIN_DROP_CELLS) #define RAIN_CELLS_GROUP_SIZE 64 #if defined(STAGE_COMPUTE) -layout(std430, set = COMPUTE_SET, binding = 0) writeonly buffer rain_cells_ssbo +layout(std430, set = COMPUTE_SET, binding = 0) buffer rain_cells_ssbo #else layout(std430, set = PASS_SET, binding = RAIN_CELLS_SSBO) readonly buffer rain_cells_ssbo #endif @@ -38,7 +42,13 @@ layout(std430, set = PASS_SET, binding = RAIN_CELLS_SSBO) readonly buffer rain_c uvec4 rain_cells[]; }; -// the life of the drops and of the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +// the drops and the rivulets (the settings of mat_rain_windscreen.frag which the cells are worked out with) +const float drop_density = 0.6; // share of grid cells holding a drop at full rain (scaled by rain intensity) +const float drop_size_min = 0.45; // drop size in grid cells +const float drop_size_max = 1.15; +const float drop_size_bias = 1.8; // >1 = more small/medium drops, fewer big ones +const float drop_sag = 0.12; // max vertical stretch of a resting drop (gravity) +const float drop_wind_stretch = 0.4; // drops are stretched up to this much along the push of the air (0 = off) const float drop_life_min = 14.0; // seconds a drop sits on the glass const float drop_life_max = 45.0; const float drop_appear = 0.18; // seconds of the landing "pop" @@ -58,6 +68,11 @@ uint rain_drop_index(ivec2 Cell) return uint(Cell.y & (RAIN_DROP_CELLS - 1)) * uint(RAIN_DROP_CELLS) + uint(Cell.x & (RAIN_DROP_CELLS - 1)); } +uint rain_mask_index(ivec2 Cell) +{ + return uint(RAIN_MASK_BASE) + rain_drop_index(Cell); +} + uint rain_riv_index(int Layer, ivec2 Cell) { return uint(RAIN_RIV_BASE) + uint(Layer) * uint(RAIN_RIV_CELLS * RAIN_RIV_CELLS) + uint(Cell.y & (RAIN_RIV_CELLS - 1)) * uint(RAIN_RIV_CELLS) From 422ccd16cd5bb33b1761c24bb8285b718e73e422 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 18:42:07 +0100 Subject: [PATCH 4/4] Rain: the cab in the depth prepass also in the rain In the rain the cab is drawn after the precipitation (so the rain inside it is hidden), and it was left out of the depth prepass too, so the opaque pass shaded everything behind it. Its depth now goes into the prepass in any weather (Vulkan 1.4, and OpenGL 4.6 without gfx.depthprepass.replay); the colour of the cab is still drawn after the rain, so nothing changes in the picture. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- rendering/opengl46/opengl46renderer.cpp | 5 +++-- rendering/vulkan14/vulkan14frame.cpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 6584dde68..e74510f71 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -4294,8 +4294,9 @@ void opengl46_renderer::Render_Scene_Depth( glm::mat4 const &Future, bool const m_scenerecordedparts = static_cast( m_scenechunks.size() ); } else { - // the conditions of the cab drawn early by the colour pass - if( ( false == FreeFlyModeFlag ) && ( Global.Overcast <= 1.0f ) && ( Global.render_cab ) ) { + // the cab first, also in the rain: the colour pass draws it after the rain then (the rain inside it hidden), but with its depth + // here nothing the cab hides is shaded by the opaque pass + if( ( false == FreeFlyModeFlag ) && ( Global.render_cab ) ) { model_ubs.future = m_cabfuture; auto const *vehicle { simulation::Train->Dynamic() }; Render_cab( vehicle, vehicle->InteriorLightLevel, false ); diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index b619e5690..eb7a4567d 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1893,8 +1893,9 @@ void vulkan14_renderer::render_scene_depth(std::size_t const Index, glm::mat4 co auto const colourstats{m_renderpass.draw_stats}; m_renderpass.draw_stats = {}; walk_blending(false); - // the conditions of the cab drawn early by the colour pass - if ((false == FreeFlyModeFlag) && (Global.Overcast <= 1.0f) && Global.render_cab && simulation::Train) + // the cab first, also in the rain: the colour pass draws it after the rain then (the rain inside it hidden), but with its depth here + // nothing the cab hides is shaded by the opaque pass (in a station seen from the cab: about a third of the time of the opaque pass) + if ((false == FreeFlyModeFlag) && Global.render_cab && simulation::Train) { model_ubs.future = m_cabfuture; auto const *vehicle{simulation::Train->Dynamic()};