From 2c54aae374a72d6b4692b3cd07923be367e49fcc Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 17:20:36 +0100 Subject: [PATCH] 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 6be7979ad..52da98b92 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -716,7 +716,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)