18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 10:37:07 +01:00
Files
maszyna/rendering/vulkan14/vulkan14water.cpp
T
lcddisplay 3de73b0e85 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD
2026-11-29 11:39:13 +01:00

351 lines
12 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "rendering/vulkan14/vulkan14water.h"
#include "rendering/vulkan14/vulkan14commands.h"
#include "utilities/Logs.h"
#include <algorithm>
#include <cstring>
namespace vk14
{
namespace
{
std::uint32_t groups(std::size_t const Count)
{
return static_cast<std::uint32_t>((Count + gl::WATER_GROUP_SIZE - 1) / gl::WATER_GROUP_SIZE);
}
VkBufferUsageFlags constexpr storage_usage{VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT};
} // namespace
windscreen_water::~windscreen_water()
{
// the materials may be gone already
set_host({});
destroy();
}
bool windscreen_water::init(VkDevice const Device, pipeline_library &Pipelines, shader_library &Shaders, VkDescriptorSetLayout const Passlayout, allocator &Memory, uploader &Uploads,
deletion_queue &Deletions, frame_ring &Ring, VkDeviceSize const Alignment, host Host)
{
m_device = Device;
m_pipelines = &Pipelines;
m_shaders = &Shaders;
m_passlayout = Passlayout;
m_memory = &Memory;
m_uploads = &Uploads;
m_deletions = &Deletions;
m_ring = &Ring;
m_alignment = std::max<VkDeviceSize>(Alignment, 16);
m_host = std::move(Host);
m_raincells = std::make_shared<buffer>();
if (false == m_raincells->create(Memory, static_cast<VkDeviceSize>(gl::RAIN_CELLS_COUNT * 16), storage_usage, memory_kind::device, "rain cells"))
{
m_raincells.reset();
return false;
}
m_storage = std::make_shared<buffer>();
m_full = m_storage->create(Memory, static_cast<VkDeviceSize>(gl::WATER_STORAGE_SIZE), storage_usage, memory_kind::device, "windscreen water");
if (false == m_full)
{
// the pass sets need a buffer to hold: a small one, which no glass reads (none gets a slot)
ErrorLog("windscreen water: no storage of " + std::to_string(gl::WATER_STORAGE_SIZE / (1024 * 1024)) + " MiB, the windscreens make their water up");
m_storage = std::make_shared<buffer>();
if (false == m_storage->create(Memory, 65536, storage_usage, memory_kind::device, "windscreen water"))
{
m_storage.reset();
return false;
}
}
return true;
}
void windscreen_water::destroy()
{
if (m_device == VK_NULL_HANDLE)
{
return;
}
for (auto &kernel : m_kernels)
{
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;
reset();
}
bool windscreen_water::gpu_init()
{
if ((m_device == VK_NULL_HANDLE) || (false == m_full) || (false == bool(m_host.commands)) || (false == bool(m_host.texture)))
{
return false;
}
try
{
std::array<char const *, pass::count> const names{"water_trail.comp", "water_spawn.comp", "water_move.comp", "water_clear.comp", "water_grid.comp",
"water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp", "water_snow.comp",
"water_snowlayer.comp"};
for (std::size_t i = 0; i < names.size(); ++i)
{
if (false == m_kernels[i].create(m_device, *m_pipelines, m_shaders->get(names[i]), m_passlayout, {}, names[i]))
{
throw gfx::shader_exception(std::string("no compute pipeline of ") + names[i]);
}
}
}
catch (gfx::shader_exception const &e)
{
ErrorLog("windscreen water: can't use the programs of the simulation: " + std::string(e.what()));
return false;
}
return true;
}
void windscreen_water::begin(command_stream *const Commands)
{
// the glasses of the frames before read the storage, the fills and the copies write it
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_ACCESS_2_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT);
if (false == m_cleared)
{
// nothing in the storage yet
m_cleared = true;
Commands->fill_buffer(m_storage->handle(), 0, VK_WHOLE_SIZE, 0);
compute_barrier(Commands);
}
}
void windscreen_water::gpu_setup(std::size_t const Slot, std::vector<glm::vec4> const &Axes)
{
auto const commands{m_host.commands()};
if ((commands == nullptr) || (false == m_full))
{
return;
}
begin(commands);
// the drops, the grid, the paths, the header and the counts of the slot cleared with the commands of the frame
auto const parts{gl::water_slot_parts(Slot)};
for (std::size_t part = 0; part < parts.size(); ++part)
{
if (part != 1)
{
commands->fill_buffer(m_storage->handle(), static_cast<VkDeviceSize>(parts[part].first), static_cast<VkDeviceSize>(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<VkDeviceSize>(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<VkDeviceSize>(static_cast<VkDeviceSize>(parts[1].second), static_cast<VkDeviceSize>(Axes.size() * sizeof(glm::vec4)))};
auto staging{std::make_shared<buffer>()};
if ((false == staging->create(*m_memory, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, memory_kind::upload, "windscreen water map")) || (staging->mapped() == nullptr))
{
ErrorLog("windscreen water: no staging buffer for the map of a glass");
compute_barrier(commands);
return;
}
std::memcpy(staging->mapped(), Axes.data(), static_cast<std::size_t>(size));
staging->flush(0, size);
VkBufferCopy2 region{};
region.sType = VK_STRUCTURE_TYPE_BUFFER_COPY_2;
region.dstOffset = static_cast<VkDeviceSize>(parts[1].first);
region.size = size;
VkCopyBufferInfo2 copy{};
copy.sType = VK_STRUCTURE_TYPE_COPY_BUFFER_INFO_2;
copy.srcBuffer = staging->handle();
copy.dstBuffer = m_storage->handle();
copy.regionCount = 1;
copy.pRegions = &region;
commands->copy_buffer2(&copy);
compute_barrier(commands);
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 rain;
std::int32_t pass;
float pad;
};
auto const groups = [](std::size_t const Count) { return static_cast<std::uint32_t>((Count + gl::RAIN_CELLS_GROUP_SIZE - 1) / gl::RAIN_CELLS_GROUP_SIZE); };
std::vector<descriptor> const descriptors{buffer_descriptor(m_raincells->handle(), 0, VK_WHOLE_SIZE)};
// the cells, then the reach of the drops into the quarters of each drop cell, which reads them
m_raincellkernel.dispatch(commands, descriptors, constants{Time, Rain, 0, 0.f}, groups(gl::RAIN_MASK_BASE));
compute_barrier(commands);
m_raincellkernel.dispatch(commands, descriptors, constants{Time, Rain, 1, 0.f}, groups(gl::RAIN_CELLS_COUNT - gl::RAIN_MASK_BASE));
compute_barrier(commands);
}
ring_slice windscreen_water::block(gl::water_ubs const &Block)
{
auto const slice{m_ring->allocate(sizeof(gl::water_ubs), m_alignment)};
if (slice)
{
std::memcpy(slice.data, &Block, sizeof(gl::water_ubs));
}
return slice;
}
bool windscreen_water::gpu_bake(std::size_t const Slot, texture_handle const Mask, gl::water_ubs const &Block)
{
auto const commands{m_host.commands()};
if ((commands == nullptr) || (false == m_full) || (Mask == null_handle))
{
return false;
}
VkImageView view{VK_NULL_HANDLE};
VkSampler sampler{VK_NULL_HANDLE};
if ((false == m_host.texture(Mask, view, sampler)) || (view == VK_NULL_HANDLE))
{
return false;
}
auto const slice{block(Block)};
if (false == bool(slice))
{
return false;
}
begin(commands);
m_kernels[pass::bake].run(commands,
{buffer_descriptor(m_storage->handle(), 0, VK_WHOLE_SIZE), buffer_descriptor(slice.buffer, slice.offset, sizeof(gl::water_ubs)),
image_descriptor(sampler, view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)},
groups(static_cast<std::size_t>(gl::WATER_MAP) * gl::WATER_MAP));
compute_barrier(commands);
return true;
}
bool windscreen_water::gpu_snow_layer(std::size_t const Slot, texture_handle const Atlas, texture_handle const Normal, gl::water_ubs const &Block)
{
auto const commands{m_host.commands()};
if ((commands == nullptr) || (false == m_full) || (Slot >= gl::WATER_SLOTS))
{
return false;
}
VkImageView atlasview{VK_NULL_HANDLE};
VkSampler atlassampler{VK_NULL_HANDLE};
VkImageView normalview{VK_NULL_HANDLE};
VkSampler normalsampler{VK_NULL_HANDLE};
if ((false == m_host.texture(Atlas, atlasview, atlassampler)) || (atlasview == VK_NULL_HANDLE) || (false == m_host.texture(Normal, normalview, normalsampler)) ||
(normalview == VK_NULL_HANDLE))
{
return false;
}
auto const slice{block(Block)};
if (false == bool(slice))
{
return false;
}
begin(commands);
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(gl::WATER_LAYER_TEXELS));
compute_barrier(commands);
return true;
}
void windscreen_water::gpu_step(gl::water_ubs const &Block, std::uint32_t const Tries)
{
auto const commands{m_host.commands()};
if ((commands == nullptr) || (false == m_full))
{
return;
}
auto const slice{block(Block)};
if (false == bool(slice))
{
return;
}
begin(commands);
std::vector<descriptor> const descriptors{buffer_descriptor(m_storage->handle(), 0, VK_WHOLE_SIZE), buffer_descriptor(slice.buffer, slice.offset, sizeof(gl::water_ubs))};
auto const run = [&](pass const Pass, std::uint32_t const Groups) {
if (Groups == 0)
{
return;
}
m_kernels[Pass].run(commands, descriptors, Groups, gl::WATER_SLOTS);
compute_barrier(commands);
};
auto const drops{groups(gl::WATER_DROPS)};
auto const cells{groups(static_cast<std::size_t>(gl::WATER_GRID) * gl::WATER_GRID)};
// the drops only where a glass of the water is (the glasses of the snow have passes of their own)
auto const water{std::ranges::any_of(Block.slots, [](glm::vec4 const &Slot) { return Slot.z > 0.5f; })};
auto const drop_groups{water ? drops : 0u};
auto const cell_groups{water ? cells : 0u};
// the wetted paths the blades went over in the step wiped
if (water && (Block.blades != Block.blades_before))
{
run(pass::trail, groups(static_cast<std::size_t>(gl::WATER_TRAIL) * gl::WATER_TRAIL));
}
run(pass::spawn, water ? groups(Tries) : 0u);
run(pass::move, drop_groups);
run(pass::clear, cell_groups);
run(pass::grid, drop_groups);
run(pass::merge, drop_groups);
run(pass::gather, drop_groups);
run(pass::settle, drop_groups);
run(pass::clear, cell_groups);
run(pass::grid, drop_groups);
// the snow of the glasses of the snow, a texel of their maps a thread
if (Block.snow_slots != glm::vec4{0.f})
{
run(pass::snow, groups(static_cast<std::size_t>(gl::WATER_MAP) * gl::WATER_MAP));
}
}
} // namespace vk14