From 185d6da12fdbd62fff1aae9c8fb17a7cd62c7703 Mon Sep 17 00:00:00 2001 From: lcddisplay Date: Sat, 14 Nov 2026 22:14:18 +0100 Subject: [PATCH] Windscreen snow: the layer over the rectangle of the glass, one for the cab The layer of 1024 x 1024 texels over the whole uv gave a glass taking a part of its texture too few of them (the EP09 104ec: ~470 across the glass, 1.8 pixels of the screen a texel at 1080p), so the lumps came out soft. Now the layer lies over the rectangle of the uv the glass takes, its 2M texels across and down as the glass is wide and high (~1 mm a texel); the renderer works it out from the map of the glass when it sets the glass up and puts it in the storage (water.glsl, water_layer_rect). One layer, for the first glass of the snow of the cab (a cab has one as a rule); a second one lays its snow at every pixel as before the layer, the same look, slower. The storage of the water: 58 MiB before the layers, 82 MiB now (106 MiB with the four layers of 1024 x 1024). No assets change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD --- gl/glsl_common.cpp | 2 +- gl/ubo.h | 27 +++++--- rendering/common/windscreenwater.cpp | 85 ++++++++++++++++++++------ rendering/common/windscreenwater.h | 11 ++++ rendering/opengl46/opengl46water.cpp | 5 +- rendering/vulkan14/vulkan14shaders.cpp | 2 +- rendering/vulkan14/vulkan14water.cpp | 8 ++- shaders/ogl46/common.glsl | 2 +- shaders/ogl46/mat_snow_windscreen.frag | 18 +++++- shaders/ogl46/water.glsl | 48 +++++++++++---- shaders/ogl46/water_snowlayer.comp | 19 ++++-- shaders/vk14/common.glsl | 2 +- shaders/vk14/mat_snow_windscreen.frag | 18 +++++- shaders/vk14/water.glsl | 48 +++++++++++---- shaders/vk14/water_snowlayer.comp | 19 ++++-- 15 files changed, 243 insertions(+), 71 deletions(-) diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index 7c18fb9b8..bfe1a4716 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -56,7 +56,7 @@ void gl::glsl_common_setup() "#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 WATER_LAYER " + std::to_string(WATER_LAYER) + "\n" + + "#define WATER_LAYER_TEXELS " + std::to_string(WATER_LAYER_TEXELS) + "\n" + "#define WATER_LAYER_BANDS " + std::to_string(WATER_LAYER_BANDS) + "\n" + "#define WATER_SNOW_ATLAS_TEX " + std::to_string(WATER_SNOW_ATLAS_TEX) + "\n" + "#define WATER_SNOW_NORMAL_TEX " + std::to_string(WATER_SNOW_NORMAL_TEX) + "\n" + diff --git a/gl/ubo.h b/gl/ubo.h index 053fd2468..fc33d9018 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -732,9 +732,11 @@ namespace gl const GLuint WATER_CELL_DROPS = 32; const GLuint WATER_TRAIL = 1024; const GLuint WATER_MAP = 256; - // the snow of a glass laid together (water_snowlayer.comp): WATER_LAYER x WATER_LAYER texels over its uv, a band of every - // WATER_LAYER_BANDS-th row laid anew in a step (and wherever a blade goes over it), read by mat_snow_windscreen.frag - const GLuint WATER_LAYER = 1024; + // the snow of a glass laid together (water_snowlayer.comp): WATER_LAYER_TEXELS texels over the rectangle of its uv the glass + // takes (across and down as the glass is wide and high, ~1 mm a texel), a band of every WATER_LAYER_BANDS-th row laid anew in + // a step (and wherever a blade goes over it), read by mat_snow_windscreen.frag. one layer: the first glass of the snow of the + // cab has it (a cab has one glass of the snow as a rule), the others lay their snow at every pixel as they're drawn + const GLuint WATER_LAYER_TEXELS = 2 * 1024 * 1024; const GLuint WATER_LAYER_BANDS = 8; const GLuint WATER_GROUP_SIZE = 64; // the binding points of the opengl renderer: the storage of the water (read by the glasses, written by the passes), the block @@ -779,13 +781,18 @@ namespace gl // flakes), a step reads one and writes the other (the header of the glass says which one is the latest) const size_t WATER_SNOW_OFFSET = WATER_TRAIL_OFFSET + 4 * static_cast(WATER_SLOTS) * WATER_TRAIL * WATER_TRAIL; const size_t WATER_SNOW_SIZE = 8 * static_cast(WATER_MAP) * WATER_MAP; - // the snow of every glass laid together (water_snowlayer.comp): WATER_LAYER x WATER_LAYER texels of three uint each (the colour - // and the cover, the normal and the translucency, the bend and the haze, see water.glsl) - const size_t WATER_LAYER_OFFSET = WATER_SNOW_OFFSET + 2 * WATER_SNOW_SIZE * WATER_SLOTS; - const size_t WATER_LAYER_SIZE = 12 * static_cast(WATER_LAYER) * WATER_LAYER; - const size_t WATER_STORAGE_SIZE = WATER_LAYER_OFFSET + WATER_LAYER_SIZE * WATER_SLOTS; + // the snow of the glass of the snow laid together (water_snowlayer.comp): first eight uint of every glass (the rectangle of its + // uv the layer takes, four floats; the texels of the layer across and down, 0 when the glass has no layer), then the layer, + // WATER_LAYER_TEXELS texels of three uint each (the colour and the cover, the normal and the translucency, the bend and the + // haze, see water.glsl) + const size_t WATER_LAYER_INFO_OFFSET = WATER_SNOW_OFFSET + 2 * WATER_SNOW_SIZE * WATER_SLOTS; + const size_t WATER_LAYER_INFO_SIZE = 32; + const size_t WATER_LAYER_OFFSET = WATER_LAYER_INFO_OFFSET + WATER_LAYER_INFO_SIZE * WATER_SLOTS; + const size_t WATER_LAYER_SIZE = 12 * static_cast(WATER_LAYER_TEXELS); + const size_t WATER_STORAGE_SIZE = WATER_LAYER_OFFSET + WATER_LAYER_SIZE; // the parts of a glass in the storage: its drops and what the passes keep of them, its map (the axes the second part), its - // cells and its paths, its header and its counts, its two maps of the snow, its laid snow, (offset, size) in bytes + // cells and its paths, its header and its counts, its two maps of the snow, the layer of the snow (the one of the cab, laid + // anew whichever glass it goes to), (offset, size) in bytes inline std::array, 14> water_slot_parts(size_t const Slot) { auto const drops{static_cast(WATER_DROPS)}; @@ -805,7 +812,7 @@ namespace gl {WATER_COUNTS_OFFSET + 16 * Slot, 16}, {WATER_SNOW_OFFSET + WATER_SNOW_SIZE * Slot, WATER_SNOW_SIZE}, {WATER_SNOW_OFFSET + WATER_SNOW_SIZE * (WATER_SLOTS + Slot), WATER_SNOW_SIZE}, - {WATER_LAYER_OFFSET + WATER_LAYER_SIZE * Slot, WATER_LAYER_SIZE}}}; + {WATER_LAYER_OFFSET, WATER_LAYER_SIZE}}}; } // layout std140: the step of the simulation of the water on the windscreens (GLSL water_ubo), every vector in the space of the diff --git a/rendering/common/windscreenwater.cpp b/rendering/common/windscreenwater.cpp index eb82e27e2..498b85248 100644 --- a/rendering/common/windscreenwater.cpp +++ b/rendering/common/windscreenwater.cpp @@ -10,6 +10,10 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "rendering/common/windscreenwater.h" +#include +#include +#include + #include "model/Model3d.h" #include "model/material.h" #include "rendering/renderer.h" @@ -121,6 +125,7 @@ void windscreen_water::release() assign(false); m_glasses = {}; m_glasscount = 0; + m_layerslot = -1; m_kinds.clear(); m_vehicle = nullptr; m_cab = nullptr; @@ -500,29 +505,75 @@ texture_handle material_texture(opengl_material const &Material, std::string con } // namespace +std::array windscreen_water::layer_info(std::size_t const Slot, std::vector const &Axes) +{ + std::array info{}; + if ((Slot >= m_glasses.size()) || (false == m_glasses[Slot].snow) || ((m_layerslot >= 0) && (m_layerslot != static_cast(Slot)))) + { + return info; + } + auto const size{static_cast(gl::WATER_MAP)}; + glm::ivec2 least{size}; + glm::ivec2 most{-1}; + glm::dvec2 span{0.0}; + auto on{0}; + for (int y = 0; y < size; ++y) + { + for (int x = 0; x < size; ++x) + { + auto const texel{static_cast(y * size + x)}; + if ((texel * 2 + 1 >= Axes.size()) || (Axes[texel * 2].w <= 0.f)) + { + continue; + } + least = glm::min(least, glm::ivec2{x, y}); + most = glm::max(most, glm::ivec2{x, y}); + if (Axes[texel * 2].w >= 1.f) + { + // metres of the glass in a unit of its uv, across and down + span += glm::dvec2{glm::length(glm::vec3{Axes[texel * 2]}), glm::length(glm::vec3{Axes[texel * 2 + 1]})}; + ++on; + } + } + } + if ((on == 0) || (most.x < least.x) || (most.y < least.y)) + { + return info; + } + m_layerslot = static_cast(Slot); + span /= static_cast(on); + auto const corner{glm::vec2{least} / static_cast(size)}; + auto const extent{glm::vec2{most - least + 1} / static_cast(size)}; + // the texels across and down as the glass is wide and high (the same size in metres both ways) + auto const wide{std::max(static_cast(extent.x) * span.x, 1e-3)}; + auto const high{std::max(static_cast(extent.y) * span.y, 1e-3)}; + auto const texels{static_cast(gl::WATER_LAYER_TEXELS)}; + auto const across{static_cast(std::clamp(std::round(std::sqrt(texels * wide / high)), 16.0, texels / 16.0))}; + auto const down{static_cast(gl::WATER_LAYER_TEXELS / across)}; + info = {std::bit_cast(corner.x), std::bit_cast(corner.y), std::bit_cast(extent.x), std::bit_cast(extent.y), across, down, 0u, 0u}; + return info; +} + void windscreen_water::lay_snow(gl::water_ubs const &Block) { - for (std::size_t slot = 0; slot < m_glasscount; ++slot) + // the one glass with the layer + if ((m_layerslot < 0) || (static_cast(m_layerslot) >= m_glasscount)) + { + return; + } + auto const slot{static_cast(m_layerslot)}; + if (Block.snow_slots[static_cast(slot)] >= 0.5f) { - if (Block.snow_slots[static_cast(slot)] < 0.5f) - { - continue; - } auto const *material{m_host.material(m_glasses[slot].material)}; - if (material == nullptr) + auto const atlas{(material != nullptr) ? material_texture(*material, "raindropsatlas") : null_handle}; + auto const normal{(material != nullptr) ? material_texture(*material, "normalmap") : null_handle}; + if ((atlas != null_handle) && (normal != null_handle)) { - continue; + auto layer{Block}; + layer.counts.z = static_cast(slot); + layer.counts.w = m_step; + gpu_snow_layer(slot, atlas, normal, layer); } - auto const atlas{material_texture(*material, "raindropsatlas")}; - auto const normal{material_texture(*material, "normalmap")}; - if ((atlas == null_handle) || (normal == null_handle)) - { - continue; - } - auto layer{Block}; - layer.counts.z = static_cast(slot); - layer.counts.w = m_step; - gpu_snow_layer(slot, atlas, normal, layer); } } diff --git a/rendering/common/windscreenwater.h b/rendering/common/windscreenwater.h index 53df232c0..4acfe991e 100644 --- a/rendering/common/windscreenwater.h +++ b/rendering/common/windscreenwater.h @@ -106,6 +106,16 @@ class windscreen_water // the snow of the glasses of the snow Block says are simulated laid together, after the step void lay_snow(gl::water_ubs const &Block); + protected: + // where the layer of the snow of a glass lies (water.glsl): the rectangle of the uv its map holds the glass in (the corner and + // the size, the bits of floats) and the texels of the layer across and down, as the glass is wide and high in metres (0 when + // the map holds no glass or the glass has no layer) + // the eight uint the renderer puts at gl::WATER_LAYER_INFO_OFFSET for the glass of Slot (set up with its map Axes): the cab has + // one layer, for its first glass of the snow, the others get none (zeros) + std::array layer_info(std::size_t Slot, std::vector const &Axes); + + private: + host m_host; int m_state{0}; // 0: not set up yet, 1: ready, -1: can't be used TDynamicObject const *m_vehicle{nullptr}; @@ -113,6 +123,7 @@ class windscreen_water std::vector> m_kinds; // the materials of the cab with a mask of the wipers, what kind of glass they were std::array m_glasses{}; std::size_t m_glasscount{0}; + int m_layerslot{-1}; // the glass with the layer of the snow, -1 none glm::vec4 m_bladesbefore{-1.f}; double m_time{0.0}; // seconds the simulation runs std::uint32_t m_step{0}; diff --git a/rendering/opengl46/opengl46water.cpp b/rendering/opengl46/opengl46water.cpp index 1b4b26a71..6bff1a066 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -105,6 +105,9 @@ void opengl46_windscreen_water::gpu_setup(std::size_t const Slot, std::vector(part.first), static_cast(part.second), GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr); auto const axes{gl::water_slot_parts(Slot)[1]}; m_storage->update(Axes.data(), static_cast(axes.first), static_cast(std::min(axes.second, Axes.size() * sizeof(glm::vec4)))); + // where the layer of its snow lies + auto const info{layer_info(Slot, Axes)}; + m_storage->update(info.data(), static_cast(gl::WATER_LAYER_INFO_OFFSET + gl::WATER_LAYER_INFO_SIZE * Slot), static_cast(sizeof(info))); ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT); } @@ -140,7 +143,7 @@ bool opengl46_windscreen_water::gpu_snow_layer(std::size_t const Slot, texture_h m_block->bind_uniform(); ::glBindTextureUnit(static_cast(gl::WATER_SNOW_ATLAS_TEX), static_cast(atlas.get_id())); ::glBindTextureUnit(static_cast(gl::WATER_SNOW_NORMAL_TEX), static_cast(normal.get_id())); - m_programs[pass::snowlayer]->dispatch(groups(static_cast(gl::WATER_LAYER) * gl::WATER_LAYER)); + m_programs[pass::snowlayer]->dispatch(groups(gl::WATER_LAYER_TEXELS)); ::glBindTextureUnit(static_cast(gl::WATER_SNOW_ATLAS_TEX), 0); ::glBindTextureUnit(static_cast(gl::WATER_SNOW_NORMAL_TEX), 0); ::glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); diff --git a/rendering/vulkan14/vulkan14shaders.cpp b/rendering/vulkan14/vulkan14shaders.cpp index b04f690f6..559de5e37 100644 --- a/rendering/vulkan14/vulkan14shaders.cpp +++ b/rendering/vulkan14/vulkan14shaders.cpp @@ -938,7 +938,7 @@ std::string binding_preamble(bool const Raytracing) define("WATER_TRAIL", gl::WATER_TRAIL); define("WATER_MAP", gl::WATER_MAP); define("WATER_GROUP_SIZE", gl::WATER_GROUP_SIZE); - define("WATER_LAYER", gl::WATER_LAYER); + define("WATER_LAYER_TEXELS", gl::WATER_LAYER_TEXELS); define("WATER_LAYER_BANDS", gl::WATER_LAYER_BANDS); define("CAB_PROBE_SH_SIZE", static_cast(gl::CAB_PROBE_SH_SIZE)); define("CAB_PROBE_SH_GROUP_SIZE", static_cast(gl::CAB_PROBE_SH_GROUP_SIZE)); diff --git a/rendering/vulkan14/vulkan14water.cpp b/rendering/vulkan14/vulkan14water.cpp index ef37def6a..44ebc587e 100644 --- a/rendering/vulkan14/vulkan14water.cpp +++ b/rendering/vulkan14/vulkan14water.cpp @@ -150,6 +150,12 @@ void windscreen_water::gpu_setup(std::size_t const Slot, std::vector commands->fill_buffer(m_storage->handle(), static_cast(parts[part].first), static_cast(parts[part].second), 0); } } + // where the layer of its snow lies, a word at a time + auto const info{layer_info(Slot, Axes)}; + for (std::size_t word = 0; word < info.size(); ++word) + { + commands->fill_buffer(m_storage->handle(), static_cast(gl::WATER_LAYER_INFO_OFFSET + gl::WATER_LAYER_INFO_SIZE * Slot + 4 * word), 4, info[word]); + } // the axes of the map copied after that, from a staging buffer kept until the gpu is done with the frame auto const size{std::min(static_cast(parts[1].second), static_cast(Axes.size() * sizeof(glm::vec4)))}; auto staging{std::make_shared()}; @@ -287,7 +293,7 @@ bool windscreen_water::gpu_snow_layer(std::size_t const Slot, texture_handle con m_kernels[pass::snowlayer].run(commands, {buffer_descriptor(m_storage->handle(), 0, VK_WHOLE_SIZE), buffer_descriptor(slice.buffer, slice.offset, sizeof(gl::water_ubs)), image_descriptor(atlassampler, atlasview, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL), image_descriptor(normalsampler, normalview, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)}, - groups(static_cast(gl::WATER_LAYER) * gl::WATER_LAYER)); + groups(gl::WATER_LAYER_TEXELS)); compute_barrier(commands); return true; } diff --git a/shaders/ogl46/common.glsl b/shaders/ogl46/common.glsl index 288c4b279..f2193e498 100644 --- a/shaders/ogl46/common.glsl +++ b/shaders/ogl46/common.glsl @@ -65,7 +65,7 @@ #define RAIN_CELLS_SSBO_BINDING 18 #define WATER_UBO_BINDING 6 #define WATER_MASK_TEX 25 -#define WATER_LAYER 1024 +#define WATER_LAYER_TEXELS 2097152 #define WATER_LAYER_BANDS 8 #define WATER_SNOW_ATLAS_TEX 32 #define WATER_SNOW_NORMAL_TEX 33 diff --git a/shaders/ogl46/mat_snow_windscreen.frag b/shaders/ogl46/mat_snow_windscreen.frag index 9776e309d..58a81a870 100644 --- a/shaders/ogl46/mat_snow_windscreen.frag +++ b/shaders/ogl46/mat_snow_windscreen.frag @@ -97,7 +97,23 @@ void main() vec4 layerColour; vec4 layerNormal; vec4 layerBend; - water_layer_at(uint(snSlot), f_coord, layerColour, layerNormal, layerBend); + if (water_layer_of(uint(snSlot))) + { + water_layer_at(uint(snSlot), f_coord, layerColour, layerNormal, layerBend); + } + else + { + // a second glass of the snow in the cab (the layer goes to the first one): its snow laid at the pixel, as it was + // before the layer, slower but the same + snBig = (textureSize(raindropsatlas, 0).x >= 2048); + ivec2 texel = clamp(ivec2(floor(f_coord * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); + uint axis = (uint(snSlot) * uint(WATER_MAP * WATER_MAP) + uint(texel.y * WATER_MAP + texel.x)) * 2u; + vec2 span = max(vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)), vec2(0.05)); + snow_layer_s laid = snCompose(f_coord, span, max(length(duv1), length(duv2))); + layerColour = vec4(laid.rgb, laid.a); + layerNormal = vec4(laid.n, laid.tr); + layerBend = vec4(laid.bend, laid.bendA, laid.haze); + } snowRGB = layerColour.rgb; snowA = layerColour.a; snowN = layerNormal.xyz; diff --git a/shaders/ogl46/water.glsl b/shaders/ogl46/water.glsl index 7bb7958f9..63dd7aab7 100644 --- a/shaders/ogl46/water.glsl +++ b/shaders/ogl46/water.glsl @@ -71,32 +71,56 @@ vec4 water_snow_texel(uint slot, uint map, ivec2 texel) return vec4(unpackHalf2x16(water_cells[index]), unpackHalf2x16(water_cells[index + 1u])); } -// the snow of a glass laid together (water_snowlayer.comp, read by mat_snow_windscreen.frag): WATER_LAYER x WATER_LAYER texels -// over its uv after the maps of the snow, three uint a texel: the colour (premultiplied, linear) and the cover; the normal of the -// sprites (tangent space, premultiplied) and their translucency; the bend of the picture behind (weighted), its weight, and the -// haze of the thin snow -uint water_layer_base(uint slot) +// the snow of a glass laid together (water_snowlayer.comp, read by mat_snow_windscreen.frag), after the maps of the snow: eight +// uint of every glass (the rectangle of its uv the layer takes: the corner and the size, floats; the texels of the layer across +// and down, 0 when the glass has no layer: the cab has one, for its first glass of the snow), then the layer, WATER_LAYER_TEXELS +// texels of three uint each: the colour +// (premultiplied, linear) and the cover; the normal of the sprites (tangent space, premultiplied) and their translucency; the bend +// of the picture behind (weighted), its weight, and the haze of the thin snow +void water_layer_rect(uint slot, out vec4 rect, out uvec2 size) { - return water_snow_base(0u, 2u) + slot * uint(WATER_LAYER * WATER_LAYER * 3); + uint info = water_snow_base(0u, 2u) + slot * 8u; + rect = uintBitsToFloat(uvec4(water_cells[info], water_cells[info + 1u], water_cells[info + 2u], water_cells[info + 3u])); + size = uvec2(water_cells[info + 4u], water_cells[info + 5u]); +} + +uint water_layer_base() +{ + return water_snow_base(0u, 2u) + uint(WATER_SLOTS) * 8u; +} + +// whether the glass has the layer of the snow +bool water_layer_of(uint slot) +{ + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + return (size.x > 0u) && (size.y > 0u) && all(greaterThan(rect.zw, vec2(0.0))); } // the laid snow of a glass at UV, filtered between the texels: Colour (rgb, cover), Normal (xyz, translucency), Bend (xy, its -// weight, the haze) +// weight, the haze); nothing outside the layer void water_layer_at(uint slot, vec2 uv, out vec4 colour, out vec4 normal, out vec4 bend) { - vec2 p = uv * float(WATER_LAYER) - 0.5; - ivec2 i = ivec2(floor(p)); - vec2 f = p - vec2(i); colour = vec4(0.0); normal = vec4(0.0); bend = vec4(0.0); + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + if ((size.x == 0u) || (size.y == 0u) || any(lessThanEqual(rect.zw, vec2(0.0)))) + return; + vec2 p = (uv - rect.xy) / rect.zw * vec2(size) - 0.5; + ivec2 i = ivec2(floor(p)); + vec2 f = p - vec2(i); + uint base = water_layer_base(); for (int k = 0; k < 4; ++k) { ivec2 t = i + ivec2(k & 1, k >> 1); - if (any(lessThan(t, ivec2(0))) || any(greaterThanEqual(t, ivec2(WATER_LAYER)))) + if (any(lessThan(t, ivec2(0))) || any(greaterThanEqual(t, ivec2(size)))) continue; float w = ((k & 1) == 1 ? f.x : 1.0 - f.x) * ((k >> 1) == 1 ? f.y : 1.0 - f.y); - uint index = water_layer_base(slot) + 3u * uint(t.y * WATER_LAYER + t.x); + uint index = base + 3u * (uint(t.y) * size.x + uint(t.x)); colour += unpackUnorm4x8(water_cells[index]) * w; normal += unpackSnorm4x8(water_cells[index + 1u]) * w; bend += unpackSnorm4x8(water_cells[index + 2u]) * w; diff --git a/shaders/ogl46/water_snowlayer.comp b/shaders/ogl46/water_snowlayer.comp index 6dbdd9dd2..524f83e95 100644 --- a/shaders/ogl46/water_snowlayer.comp +++ b/shaders/ogl46/water_snowlayer.comp @@ -3,7 +3,8 @@ // the snow on the windscreens (water_sim.glsl, water.glsl, snow_sprites.glsl): the snow of a glass of the snow laid together // from the sprites of its atlas into its layer, which mat_snow_windscreen.frag reads (the glass only reads it: laying the nine // grids of the lumps at every pixel of the glass in every frame cost as much as the rest of the frame). a thread per texel of -// the layer, the glass is water_counts.z. the snow under a lump changes slowly, so a band of every WATER_LAYER_BANDS-th row is +// the layer (over the rectangle of the uv the glass takes, ~1 mm a texel), the glass is water_counts.z. the snow under a lump +// changes slowly, so a band of every WATER_LAYER_BANDS-th row is // laid anew in a step (water_counts.w the step), and at once wherever a blade went over the glass in the step #include "common.glsl" @@ -42,13 +43,19 @@ void main() { uint slot = water_counts.z; uint texel = gl_GlobalInvocationID.x; - if ((slot >= uint(WATER_SLOTS)) || (texel >= uint(WATER_LAYER * WATER_LAYER)) || (water_snow_slots[slot] < 0.5)) + if ((slot >= uint(WATER_SLOTS)) || (texel >= uint(WATER_LAYER_TEXELS)) || (water_snow_slots[slot] < 0.5)) return; uint map; if (false == water_snow_map(slot, map)) return; - ivec2 at = ivec2(int(texel % uint(WATER_LAYER)), int(texel / uint(WATER_LAYER))); - vec2 uv = (vec2(at) + 0.5) / float(WATER_LAYER); + // the layer over the rectangle of the uv the glass takes + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + if ((size.x == 0u) || (texel >= size.x * size.y)) + return; + ivec2 at = ivec2(int(texel % size.x), int(texel / size.x)); + vec2 uv = rect.xy + (vec2(at) + 0.5) / vec2(size) * rect.zw; ivec2 mapAt = clamp(ivec2(floor(uv * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); uint mapTexel = slot * uint(WATER_MAP * WATER_MAP) + uint(mapAt.y * WATER_MAP + mapAt.x); uint axis = mapTexel * 2u; @@ -63,8 +70,8 @@ void main() snMap = map; snBig = (textureSize(raindropsatlas, 0).x >= 2048); vec2 span = max(vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)), vec2(0.05)); - snow_layer_s s = snCompose(uv, span, 1.0 / float(WATER_LAYER)); - uint index = water_layer_base(slot) + 3u * texel; + snow_layer_s s = snCompose(uv, span, max(rect.z / float(size.x), rect.w / float(size.y))); + uint index = water_layer_base() + 3u * texel; water_cells[index] = packUnorm4x8(vec4(s.rgb, s.a)); water_cells[index + 1u] = packSnorm4x8(vec4(s.n, s.tr * 2.0 - 1.0)); water_cells[index + 2u] = packSnorm4x8(vec4(s.bend, s.bendA * 2.0 - 1.0, s.haze * 2.0 - 1.0)); diff --git a/shaders/vk14/common.glsl b/shaders/vk14/common.glsl index f19df191f..fd55c939e 100644 --- a/shaders/vk14/common.glsl +++ b/shaders/vk14/common.glsl @@ -138,7 +138,7 @@ #define WATER_TRAIL 1024 #define WATER_MAP 256 #define WATER_GROUP_SIZE 64 -#define WATER_LAYER 1024 +#define WATER_LAYER_TEXELS 2097152 #define WATER_LAYER_BANDS 8 #define CAB_PROBE_SH_SIZE 16 #define CAB_PROBE_SH_GROUP_SIZE 64 diff --git a/shaders/vk14/mat_snow_windscreen.frag b/shaders/vk14/mat_snow_windscreen.frag index c3ae46e72..e5e8d604a 100644 --- a/shaders/vk14/mat_snow_windscreen.frag +++ b/shaders/vk14/mat_snow_windscreen.frag @@ -102,7 +102,23 @@ void main() vec4 layerColour; vec4 layerNormal; vec4 layerBend; - water_layer_at(uint(snSlot), f_coord, layerColour, layerNormal, layerBend); + if (water_layer_of(uint(snSlot))) + { + water_layer_at(uint(snSlot), f_coord, layerColour, layerNormal, layerBend); + } + else + { + // a second glass of the snow in the cab (the layer goes to the first one): its snow laid at the pixel, as it was + // before the layer, slower but the same + snBig = (textureSize(raindropsatlas, 0).x >= 2048); + ivec2 texel = clamp(ivec2(floor(f_coord * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); + uint axis = (uint(snSlot) * uint(WATER_MAP * WATER_MAP) + uint(texel.y * WATER_MAP + texel.x)) * 2u; + vec2 span = max(vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)), vec2(0.05)); + snow_layer_s laid = snCompose(f_coord, span, max(length(duv1), length(duv2))); + layerColour = vec4(laid.rgb, laid.a); + layerNormal = vec4(laid.n, laid.tr); + layerBend = vec4(laid.bend, laid.bendA, laid.haze); + } snowRGB = layerColour.rgb; snowA = layerColour.a; snowN = layerNormal.xyz; diff --git a/shaders/vk14/water.glsl b/shaders/vk14/water.glsl index 8befd9fcc..d2be3c053 100644 --- a/shaders/vk14/water.glsl +++ b/shaders/vk14/water.glsl @@ -72,32 +72,56 @@ vec4 water_snow_texel(uint slot, uint map, ivec2 texel) return vec4(unpackHalf2x16(water_cells[index]), unpackHalf2x16(water_cells[index + 1u])); } -// the snow of a glass laid together (water_snowlayer.comp, read by mat_snow_windscreen.frag): WATER_LAYER x WATER_LAYER texels -// over its uv after the maps of the snow, three uint a texel: the colour (premultiplied, linear) and the cover; the normal of the -// sprites (tangent space, premultiplied) and their translucency; the bend of the picture behind (weighted), its weight, and the -// haze of the thin snow -uint water_layer_base(uint slot) +// the snow of a glass laid together (water_snowlayer.comp, read by mat_snow_windscreen.frag), after the maps of the snow: eight +// uint of every glass (the rectangle of its uv the layer takes: the corner and the size, floats; the texels of the layer across +// and down, 0 when the glass has no layer: the cab has one, for its first glass of the snow), then the layer, WATER_LAYER_TEXELS +// texels of three uint each: the colour +// (premultiplied, linear) and the cover; the normal of the sprites (tangent space, premultiplied) and their translucency; the bend +// of the picture behind (weighted), its weight, and the haze of the thin snow +void water_layer_rect(uint slot, out vec4 rect, out uvec2 size) { - return water_snow_base(0u, 2u) + slot * uint(WATER_LAYER * WATER_LAYER * 3); + uint info = water_snow_base(0u, 2u) + slot * 8u; + rect = uintBitsToFloat(uvec4(water_cells[info], water_cells[info + 1u], water_cells[info + 2u], water_cells[info + 3u])); + size = uvec2(water_cells[info + 4u], water_cells[info + 5u]); +} + +uint water_layer_base() +{ + return water_snow_base(0u, 2u) + uint(WATER_SLOTS) * 8u; +} + +// whether the glass has the layer of the snow +bool water_layer_of(uint slot) +{ + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + return (size.x > 0u) && (size.y > 0u) && all(greaterThan(rect.zw, vec2(0.0))); } // the laid snow of a glass at UV, filtered between the texels: Colour (rgb, cover), Normal (xyz, translucency), Bend (xy, its -// weight, the haze) +// weight, the haze); nothing outside the layer void water_layer_at(uint slot, vec2 uv, out vec4 colour, out vec4 normal, out vec4 bend) { - vec2 p = uv * float(WATER_LAYER) - 0.5; - ivec2 i = ivec2(floor(p)); - vec2 f = p - vec2(i); colour = vec4(0.0); normal = vec4(0.0); bend = vec4(0.0); + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + if ((size.x == 0u) || (size.y == 0u) || any(lessThanEqual(rect.zw, vec2(0.0)))) + return; + vec2 p = (uv - rect.xy) / rect.zw * vec2(size) - 0.5; + ivec2 i = ivec2(floor(p)); + vec2 f = p - vec2(i); + uint base = water_layer_base(); for (int k = 0; k < 4; ++k) { ivec2 t = i + ivec2(k & 1, k >> 1); - if (any(lessThan(t, ivec2(0))) || any(greaterThanEqual(t, ivec2(WATER_LAYER)))) + if (any(lessThan(t, ivec2(0))) || any(greaterThanEqual(t, ivec2(size)))) continue; float w = ((k & 1) == 1 ? f.x : 1.0 - f.x) * ((k >> 1) == 1 ? f.y : 1.0 - f.y); - uint index = water_layer_base(slot) + 3u * uint(t.y * WATER_LAYER + t.x); + uint index = base + 3u * (uint(t.y) * size.x + uint(t.x)); colour += unpackUnorm4x8(water_cells[index]) * w; normal += unpackSnorm4x8(water_cells[index + 1u]) * w; bend += unpackSnorm4x8(water_cells[index + 2u]) * w; diff --git a/shaders/vk14/water_snowlayer.comp b/shaders/vk14/water_snowlayer.comp index 1ec8a882a..a69efdf1f 100644 --- a/shaders/vk14/water_snowlayer.comp +++ b/shaders/vk14/water_snowlayer.comp @@ -3,7 +3,8 @@ // the snow on the windscreens (water_sim.glsl, water.glsl, snow_sprites.glsl): the snow of a glass of the snow laid together // from the sprites of its atlas into its layer, which mat_snow_windscreen.frag reads (the glass only reads it: laying the nine // grids of the lumps at every pixel of the glass in every frame cost as much as the rest of the frame). a thread per texel of -// the layer, the glass is water_counts.z. the snow under a lump changes slowly, so a band of every WATER_LAYER_BANDS-th row is +// the layer (over the rectangle of the uv the glass takes, ~1 mm a texel), the glass is water_counts.z. the snow under a lump +// changes slowly, so a band of every WATER_LAYER_BANDS-th row is // laid anew in a step (water_counts.w the step), and at once wherever a blade went over the glass in the step // none of the pass set: everything the kernel reads is pushed with its dispatch (set COMPUTE_SET, water_sim.glsl) @@ -44,13 +45,19 @@ void main() { uint slot = water_counts.z; uint texel = gl_GlobalInvocationID.x; - if ((slot >= uint(WATER_SLOTS)) || (texel >= uint(WATER_LAYER * WATER_LAYER)) || (water_snow_slots[slot] < 0.5)) + if ((slot >= uint(WATER_SLOTS)) || (texel >= uint(WATER_LAYER_TEXELS)) || (water_snow_slots[slot] < 0.5)) return; uint map; if (false == water_snow_map(slot, map)) return; - ivec2 at = ivec2(int(texel % uint(WATER_LAYER)), int(texel / uint(WATER_LAYER))); - vec2 uv = (vec2(at) + 0.5) / float(WATER_LAYER); + // the layer over the rectangle of the uv the glass takes + vec4 rect; + uvec2 size; + water_layer_rect(slot, rect, size); + if ((size.x == 0u) || (texel >= size.x * size.y)) + return; + ivec2 at = ivec2(int(texel % size.x), int(texel / size.x)); + vec2 uv = rect.xy + (vec2(at) + 0.5) / vec2(size) * rect.zw; ivec2 mapAt = clamp(ivec2(floor(uv * float(WATER_MAP))), ivec2(0), ivec2(WATER_MAP - 1)); uint mapTexel = slot * uint(WATER_MAP * WATER_MAP) + uint(mapAt.y * WATER_MAP + mapAt.x); uint axis = mapTexel * 2u; @@ -65,8 +72,8 @@ void main() snMap = map; snBig = (textureSize(raindropsatlas, 0).x >= 2048); vec2 span = max(vec2(length(water_axes[axis].xyz), length(water_axes[axis + 1u].xyz)), vec2(0.05)); - snow_layer_s s = snCompose(uv, span, 1.0 / float(WATER_LAYER)); - uint index = water_layer_base(slot) + 3u * texel; + snow_layer_s s = snCompose(uv, span, max(rect.z / float(size.x), rect.w / float(size.y))); + uint index = water_layer_base() + 3u * texel; water_cells[index] = packUnorm4x8(vec4(s.rgb, s.a)); water_cells[index + 1u] = packSnorm4x8(vec4(s.n, s.tr * 2.0 - 1.0)); water_cells[index + 2u] = packSnorm4x8(vec4(s.bend, s.bendA * 2.0 - 1.0, s.haze * 2.0 - 1.0));