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 2f6955107..6be7979ad 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -710,6 +710,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 7953056c1..99f2f45a1 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -1765,7 +1765,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 a48b9edfb..bd7ce27d9 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -83,6 +83,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 f609291c9..7e5a20d29 100644 --- a/rendering/opengl46/opengl46water.h +++ b/rendering/opengl46/opengl46water.h @@ -27,10 +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 next of the programs of the simulation compiled ahead of its first step (a few seconds each where the driver has none - // of them kept, at the first start of a new version), so the loading screen stays while they're made. false once all are - bool prepare_next(); - static constexpr std::size_t program_count() { return pass::count; } + // 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; @@ -56,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 9e3c05ca0..30c56a2a2 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1135,6 +1135,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 d640b5335..0e221446a 100644 --- a/rendering/vulkan14/vulkan14scene.cpp +++ b/rendering/vulkan14/vulkan14scene.cpp @@ -182,6 +182,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: @@ -1093,6 +1094,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; @@ -1128,6 +1130,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 b0101f9eb..59b51b1d1 100644 --- a/rendering/vulkan14/vulkan14shaders.cpp +++ b/rendering/vulkan14/vulkan14shaders.cpp @@ -898,6 +898,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