From 2fe11d39a1f3989bb3142794a5f8308aedfc7863 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 13:39:59 +0100 Subject: [PATCH] Rain windscreen: the rain fallen since a blade went over each place of its sweep The glass took the rain fallen since one of the two moments the blades were at the ends of their sweep, picked by the way the blade was going. After a pass that gave a place the blade had just wiped the rain of the sweep before, until the blade reached the other end: the haze and the drops there jumped back and forth with every sweep (a jump in brightness under the wiper mask, best seen in a light rain). Now the renderers keep, for 64 places along the sweep of each blade (the value of the wiper mask), the rain fallen when the blade last went over them (gl::scene_ubs::wiper_rain_pass, marked for all the places a blade went over since the last frame), and the glass reads the one of its pixel. wiper_rain_out / wiper_rain_return are gone; the timers and the insects keep the ends of the sweep. No assets change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- gl/glsl_common.cpp | 1 + gl/ubo.h | 29 ++++++++++++++++++++----- rendering/opengl46/opengl46renderer.cpp | 15 ++++++++----- rendering/opengl46/opengl46renderer.h | 3 +++ rendering/vulkan14/vulkan14frame.cpp | 15 ++++++++----- rendering/vulkan14/vulkan14renderer.h | 3 +++ shaders/ogl46/common.glsl | 6 ++--- shaders/ogl46/mat_rain_windscreen.frag | 21 ++++++++++-------- shaders/vk14/common.glsl | 6 ++--- shaders/vk14/mat_rain_windscreen.frag | 21 ++++++++++-------- 10 files changed, 79 insertions(+), 41 deletions(-) diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index 981d7b05e..799698e3b 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -95,6 +95,7 @@ void gl::glsl_common_setup() "#define HEADLIGHT_SHADOW_TEX " + std::to_string(HEADLIGHT_SHADOW_TEX) + "\n" + "#define MAX_HEADLIGHT_SHADOWS " + std::to_string(MAX_HEADLIGHT_SHADOWS) + "U\n" + "#define MAX_CAB_PROBE_LAMPS " + std::to_string(MAX_CAB_PROBE_LAMPS) + "U\n" + + "#define WIPER_PASS_BUCKETS " + std::to_string(WIPER_PASS_BUCKETS) + "\n" + "#define FOG_VOLUME_TEX " + std::to_string(FOG_VOLUME_TEX) + "\n" + "#define CLOUD_SHADOW_TEX " + std::to_string(CLOUD_SHADOW_TEX) + "\n" + "#define CLOUD_TEX " + std::to_string(CLOUD_TEX) + "\n" + diff --git a/gl/ubo.h b/gl/ubo.h index d2556a4ee..a9a77d32f 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -4,6 +4,7 @@ #include "bindable.h" #include "buffer.h" #include +#include #include #include #include @@ -72,6 +73,9 @@ namespace gl const size_t MAX_HEADLIGHT_SHADOWS = 4; // lights of the occupied cab the probe of the cab is drawn with (gfx::cab_probe, gl::scene_ubs::cab_probe_lamp), at most const size_t MAX_CAB_PROBE_LAMPS = 16; + // the places along the sweep of a blade (dWiperPos 0 .. 1) the rain fallen when it last went over them is kept for + // (gl::scene_ubs::wiper_rain_pass) + const size_t WIPER_PASS_BUCKETS = 64; // weather (opengl46_weather): the fog of the main view (3d, apply_fog.glsl) and the shadows the clouds cast (2d), read by the // shading. bound directly, like the ones above const size_t FOG_VOLUME_TEX = MAX_TEXTURES + HELPER_TEXTURES + 4; @@ -210,13 +214,28 @@ namespace gl // at speed: the glass runs into it); y how wet the glass is, 0 dry .. 1 as full of drops as it gets (it fills with the rain, // dries when it stops); z how fast the glass fills: the share left to fill goes down as exp(-z x the rain fallen); w unused glm::vec4 rain_state; - // the rain fallen (rain_state.x) when the blades were at the ends of their sweep, as wiper_timer_out and wiper_timer_return - // (the drops come back with the rain fallen since); -1 not known - glm::vec4 wiper_rain_out; - glm::vec4 wiper_rain_return; + // the rain fallen (rain_state.x) when a blade last went over a place of its sweep: element b for the places b / 64 .. (b + 1) + // / 64 of dWiperPos (the value of the wiper mask of a pixel), x .. w the blades (as wiper_pos); -1 not known. the drops and the + // haze come back with the rain fallen since. (the moments the blades were at the ends of their sweep, which the pixel took + // the one of by the way the blade went, gave the rain of a whole sweep back to the place a blade had just wiped) + glm::vec4 wiper_rain_pass[WIPER_PASS_BUCKETS]; }; - static_assert(sizeof(scene_ubs) == 1040 + 80 * MAX_HEADLIGHT_SHADOWS + 96 * MAX_CAB_PROBE_LAMPS, "bad size of ubs"); + static_assert(sizeof(scene_ubs) == 1008 + 16 * WIPER_PASS_BUCKETS + 80 * MAX_HEADLIGHT_SHADOWS + 96 * MAX_CAB_PROBE_LAMPS, "bad size of ubs"); + + // the bucket of scene_ubs::wiper_rain_pass a place of a blade (dWiperPos, 0 .. 1) falls in + inline std::size_t wiper_pass_bucket(double const Position) + { + return std::min(static_cast(std::max(Position, 0.0) * static_cast(WIPER_PASS_BUCKETS)), WIPER_PASS_BUCKETS - 1); + } + + // the places blade Wiper went over from From to To (dWiperPos) get the rain fallen Value (-1: not known) + inline void wiper_pass_mark(scene_ubs &Scene, glm::length_t const Wiper, double const From, double const To, float const Value) + { + auto const last{wiper_pass_bucket(std::max(From, To))}; + for (auto bucket{wiper_pass_bucket(std::min(From, To))}; bucket <= last; ++bucket) + Scene.wiper_rain_pass[bucket][Wiper] = Value; + } // the camera of a pass in the world for the scene block (scene_ubs::camera_hi, camera_lo) inline void set_camera_place(scene_ubs &Ubs, glm::dvec3 const &Place) diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index c4e11b31d..ba5b5ec46 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -1706,8 +1706,9 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) scene_ubs.wiper_timer_return = glm::vec4{-1000.}; scene_ubs.wiper_bugs_out = glm::vec4{-1.}; scene_ubs.wiper_bugs_return = glm::vec4{-1.}; - scene_ubs.wiper_rain_out = glm::vec4{-1.}; - scene_ubs.wiper_rain_return = glm::vec4{-1.}; + for (auto &pass : scene_ubs.wiper_rain_pass) + pass = glm::vec4{-1.f}; + m_wiperlastpos.fill(-1.0); m_wiperbugs = 0.0; } // the insects the vehicle meets (mat_bugs_windscreen): none below 20 km/h, above it the more the faster, counted in seconds of @@ -1740,6 +1741,10 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) if (i < owner->dWiperPos.size()) { int index = cabactive > 0 ? i : static_cast(owner->dWiperPos.size() - 1) - i; + // the places of its sweep the blade went over since the last frame get the rain fallen now + auto const position{owner->dWiperPos[index]}; + gl::wiper_pass_mark(scene_ubs, i, (m_wiperlastpos[i] < 0.0) ? position : m_wiperlastpos[i], position, static_cast(m_wiperrain)); + m_wiperlastpos[i] = position; scene_ubs.wiper_pos[i] = owner->dWiperPos[index]; if (owner->dWiperPos[index] > 0. && owner->wiperDirection[index]) { @@ -1749,13 +1754,11 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) { scene_ubs.wiper_timer_out[i] = scene_ubs.time; scene_ubs.wiper_bugs_out[i] = static_cast(m_wiperbugs); - scene_ubs.wiper_rain_out[i] = static_cast(m_wiperrain); } if (owner->dWiperPos[index] > .975) { scene_ubs.wiper_timer_return[i] = scene_ubs.time; scene_ubs.wiper_bugs_return[i] = static_cast(m_wiperbugs); - scene_ubs.wiper_rain_return[i] = static_cast(m_wiperrain); } } else @@ -1765,8 +1768,8 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) scene_ubs.wiper_timer_return[i] = -1000.; scene_ubs.wiper_bugs_out[i] = -1.; scene_ubs.wiper_bugs_return[i] = -1.; - scene_ubs.wiper_rain_out[i] = -1.; - scene_ubs.wiper_rain_return[i] = -1.; + gl::wiper_pass_mark(scene_ubs, i, 0.0, 1.0, -1.f); + m_wiperlastpos[i] = -1.0; } } } diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index 5528974b6..445b936c1 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -856,6 +856,9 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { TDynamicObject const *m_wipervehicle{nullptr}; // the insects that vehicle met so far: seconds of driving at 100 km/h (scene_ubs.rain_wind.w) double m_wiperbugs{0.0}; + // the place of each blade (dWiperPos) the last frame, -1 not known: the places it went over since get the rain fallen + // (gl::wiper_pass_mark) + std::array m_wiperlastpos{-1.0, -1.0, -1.0, -1.0}; // the rain the glass met so far and how wet it is (scene_ubs::rain_state) double m_wiperrain{0.0}; double m_glasswet{0.0}; diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index f89beaa62..a4f31b27e 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1583,8 +1583,9 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) scene_ubs.wiper_timer_return = glm::vec4{-1000.}; scene_ubs.wiper_bugs_out = glm::vec4{-1.}; scene_ubs.wiper_bugs_return = glm::vec4{-1.}; - scene_ubs.wiper_rain_out = glm::vec4{-1.}; - scene_ubs.wiper_rain_return = glm::vec4{-1.}; + for (auto &pass : scene_ubs.wiper_rain_pass) + pass = glm::vec4{-1.f}; + m_wiperlastpos.fill(-1.0); m_wiperbugs = 0.0; } // the insects the vehicle meets (mat_bugs_windscreen): none below 20 km/h, above it the more the faster, counted in seconds of @@ -1617,6 +1618,10 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) if (i < owner->dWiperPos.size()) { int index = cabactive > 0 ? i : static_cast(owner->dWiperPos.size() - 1) - i; + // the places of its sweep the blade went over since the last frame get the rain fallen now + auto const position{owner->dWiperPos[index]}; + gl::wiper_pass_mark(scene_ubs, i, (m_wiperlastpos[i] < 0.0) ? position : m_wiperlastpos[i], position, static_cast(m_wiperrain)); + m_wiperlastpos[i] = position; scene_ubs.wiper_pos[i] = owner->dWiperPos[index]; if (owner->dWiperPos[index] > 0. && owner->wiperDirection[index]) { @@ -1626,13 +1631,11 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) { scene_ubs.wiper_timer_out[i] = scene_ubs.time; scene_ubs.wiper_bugs_out[i] = static_cast(m_wiperbugs); - scene_ubs.wiper_rain_out[i] = static_cast(m_wiperrain); } if (owner->dWiperPos[index] > .975) { scene_ubs.wiper_timer_return[i] = scene_ubs.time; scene_ubs.wiper_bugs_return[i] = static_cast(m_wiperbugs); - scene_ubs.wiper_rain_return[i] = static_cast(m_wiperrain); } } else @@ -1642,8 +1645,8 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) scene_ubs.wiper_timer_return[i] = -1000.; scene_ubs.wiper_bugs_out[i] = -1.; scene_ubs.wiper_bugs_return[i] = -1.; - scene_ubs.wiper_rain_out[i] = -1.; - scene_ubs.wiper_rain_return[i] = -1.; + gl::wiper_pass_mark(scene_ubs, i, 0.0, 1.0, -1.f); + m_wiperlastpos[i] = -1.0; } } } diff --git a/rendering/vulkan14/vulkan14renderer.h b/rendering/vulkan14/vulkan14renderer.h index 6539eed87..477274efc 100644 --- a/rendering/vulkan14/vulkan14renderer.h +++ b/rendering/vulkan14/vulkan14renderer.h @@ -565,6 +565,9 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk TDynamicObject const *m_wipervehicle{nullptr}; // the insects that vehicle met so far: seconds of driving at 100 km/h (scene_ubs.rain_wind.w) double m_wiperbugs{0.0}; + // the place of each blade (dWiperPos) the last frame, -1 not known: the places it went over since get the rain fallen + // (gl::wiper_pass_mark) + std::array m_wiperlastpos{-1.0, -1.0, -1.0, -1.0}; // the rain the glass met so far and how wet it is (scene_ubs::rain_state) double m_wiperrain{0.0}; double m_glasswet{0.0}; diff --git a/shaders/ogl46/common.glsl b/shaders/ogl46/common.glsl index f8cb04883..7c1947a9e 100644 --- a/shaders/ogl46/common.glsl +++ b/shaders/ogl46/common.glsl @@ -104,6 +104,7 @@ #define HEADLIGHT_SHADOW_TEX 15 #define MAX_HEADLIGHT_SHADOWS 4U #define MAX_CAB_PROBE_LAMPS 16U +#define WIPER_PASS_BUCKETS 64 #define FOG_VOLUME_TEX 16 #define CLOUD_SHADOW_TEX 17 #define CLOUD_TEX 18 @@ -377,9 +378,8 @@ layout(std140, binding = SCENE_UBO_BINDING) uniform scene_ubo vec4 camera_lo; // the rain the glass meets: x fallen so far, y how wet the glass is, z how fast it fills (mat_rain_windscreen), see gl::scene_ubs vec4 rain_state; - // the rain fallen when the blades were at the ends of their sweep, see gl::scene_ubs - vec4 wiper_rain_out; - vec4 wiper_rain_return; + // the rain fallen when a blade last went over a place of its sweep (the wiper mask / 64), see gl::scene_ubs + vec4 wiper_rain_pass[WIPER_PASS_BUCKETS]; }; #ifdef STAGE_VERTEX diff --git a/shaders/ogl46/mat_rain_windscreen.frag b/shaders/ogl46/mat_rain_windscreen.frag index 9f7012cf4..ca68ca98e 100644 --- a/shaders/ogl46/mat_rain_windscreen.frag +++ b/shaders/ogl46/mat_rain_windscreen.frag @@ -269,6 +269,14 @@ vec4 wsOver(vec4 over, vec4 under) // =========================================================================== float GetMixFactor(in vec2 co, out float side); +// the rain fallen when each blade last went over the place of its sweep given by the wiper mask (-1: not known), see +// gl::scene_ubs::wiper_rain_pass +vec4 wsWipePass(vec4 mask) +{ + ivec4 b = min(ivec4(max(mask, vec4(0.0)) * float(WIPER_PASS_BUCKETS)), ivec4(WIPER_PASS_BUCKETS - 1)); + return vec4(wiper_rain_pass[b.x].x, wiper_rain_pass[b.y].y, wiper_rain_pass[b.z].z, wiper_rain_pass[b.w].w); +} + uvec2 cellKey(vec2 c); vec2 tileOffset(float choice) @@ -563,14 +571,8 @@ float wsKept(vec2 cg, float rad, float sCW, float sCH, float gridSize) // the rain fallen (rain_state.x) since a blade last went over this place (texture UV); -1 where no blade goes or not known float wsWipeRain(vec2 co) { - vec4 movePhase = wiper_pos; - bvec4 is_out = lessThanEqual(movePhase, vec4(1.)); - movePhase = mix(vec4(2.) - movePhase, movePhase, is_out); vec4 mask = textureLod(wipermask, co, 0.0); - vec4 maskVal = mix(1. - mask, mask, is_out); - vec4 passed = step(maskVal, movePhase); - vec4 sideV = mix(passed, vec4(1.) - passed, is_out); - vec4 at = mix(wiper_rain_out, wiper_rain_return, sideV); + vec4 at = wsWipePass(mask); float fallen = -1.0; for (int i = 0; i < 4; ++i) if ((mask[i] > 0.001) && (at[i] >= 0.0)) @@ -1105,8 +1107,9 @@ float GetMixFactor(in vec2 co, out float side) { // "regeneration", raindrops gradually returning after wiper pass: // the share of the drops back: the glass fills with the rain fallen since the blade (rain_state), up to as wet as the glass is // where no blade goes (the drops of a cell are there only up to that share, so the share of those back is the fill over it) - vec4 fallen = vec4(rain_state.x) - mix(wiper_rain_out, wiper_rain_return, side_v); - vec4 known = step(vec4(0.), mix(wiper_rain_out, wiper_rain_return, side_v)); + vec4 at = wsWipePass(mask); + vec4 fallen = vec4(rain_state.x) - at; + vec4 known = step(vec4(0.), at); vec4 filled = mix(vec4(1.), vec4(1.) - exp(-max(fallen, vec4(0.)) * rain_state.z), known); vec4 regenPhase = clamp(filled / max(rain_state.y, 1e-4), vec4(0.), vec4(1.)); diff --git a/shaders/vk14/common.glsl b/shaders/vk14/common.glsl index 515b32d50..180ceb472 100644 --- a/shaders/vk14/common.glsl +++ b/shaders/vk14/common.glsl @@ -100,6 +100,7 @@ #define MAX_PARAMS 3U #define MAX_HEADLIGHT_SHADOWS 4U #define MAX_CAB_PROBE_LAMPS 16U +#define WIPER_PASS_BUCKETS 64 #define CLUSTER_GRID_X 16 #define CLUSTER_GRID_Y 9 #define CLUSTER_GRID_Z 24 @@ -339,9 +340,8 @@ layout(std140, set = PASS_SET, binding = SCENE_UBO) uniform scene_ubo vec4 camera_lo; // the rain the glass meets: x fallen so far, y how wet the glass is, z how fast it fills (mat_rain_windscreen), see gl::scene_ubs vec4 rain_state; - // the rain fallen when the blades were at the ends of their sweep, see gl::scene_ubs - vec4 wiper_rain_out; - vec4 wiper_rain_return; + // the rain fallen when a blade last went over a place of its sweep (the wiper mask / 64), see gl::scene_ubs + vec4 wiper_rain_pass[WIPER_PASS_BUCKETS]; }; #ifndef LAYOUT_COMPUTE diff --git a/shaders/vk14/mat_rain_windscreen.frag b/shaders/vk14/mat_rain_windscreen.frag index cada855a8..89f1c263f 100644 --- a/shaders/vk14/mat_rain_windscreen.frag +++ b/shaders/vk14/mat_rain_windscreen.frag @@ -274,6 +274,14 @@ vec4 wsOver(vec4 over, vec4 under) // =========================================================================== float GetMixFactor(in vec2 co, out float side); +// the rain fallen when each blade last went over the place of its sweep given by the wiper mask (-1: not known), see +// gl::scene_ubs::wiper_rain_pass +vec4 wsWipePass(vec4 mask) +{ + ivec4 b = min(ivec4(max(mask, vec4(0.0)) * float(WIPER_PASS_BUCKETS)), ivec4(WIPER_PASS_BUCKETS - 1)); + return vec4(wiper_rain_pass[b.x].x, wiper_rain_pass[b.y].y, wiper_rain_pass[b.z].z, wiper_rain_pass[b.w].w); +} + uvec2 cellKey(vec2 c); vec2 tileOffset(float choice) @@ -568,14 +576,8 @@ float wsKept(vec2 cg, float rad, float sCW, float sCH, float gridSize) // the rain fallen (rain_state.x) since a blade last went over this place (texture UV); -1 where no blade goes or not known float wsWipeRain(vec2 co) { - vec4 movePhase = wiper_pos; - bvec4 is_out = lessThanEqual(movePhase, vec4(1.)); - movePhase = mix(vec4(2.) - movePhase, movePhase, is_out); vec4 mask = textureLod(wipermask, co, 0.0); - vec4 maskVal = mix(1. - mask, mask, is_out); - vec4 passed = step(maskVal, movePhase); - vec4 sideV = mix(passed, vec4(1.) - passed, is_out); - vec4 at = mix(wiper_rain_out, wiper_rain_return, sideV); + vec4 at = wsWipePass(mask); float fallen = -1.0; for (int i = 0; i < 4; ++i) if ((mask[i] > 0.001) && (at[i] >= 0.0)) @@ -1110,8 +1112,9 @@ float GetMixFactor(in vec2 co, out float side) { // "regeneration", raindrops gradually returning after wiper pass: // the share of the drops back: the glass fills with the rain fallen since the blade (rain_state), up to as wet as the glass is // where no blade goes (the drops of a cell are there only up to that share, so the share of those back is the fill over it) - vec4 fallen = vec4(rain_state.x) - mix(wiper_rain_out, wiper_rain_return, side_v); - vec4 known = step(vec4(0.), mix(wiper_rain_out, wiper_rain_return, side_v)); + vec4 at = wsWipePass(mask); + vec4 fallen = vec4(rain_state.x) - at; + vec4 known = step(vec4(0.), at); vec4 filled = mix(vec4(1.), vec4(1.) - exp(-max(fallen, vec4(0.)) * rain_state.z), known); vec4 regenPhase = clamp(filled / max(rain_state.y, 1e-4), vec4(0.), vec4(1.));