mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 10:37:07 +01:00
5d6f443450
- the cells of the rain: the opengl renderer makes their storage (cleared) with the first bind and keeps it bound, the glasses read the cells only when they were worked out in the frame (scene_ubs.rain_state.w, both renderers): never the storage before the first rain, nor the stand-in of the vulkan renderer - the pass of the motion blur and (opengl) the velocities drawn only in the frames which need them: the motion blur with something moving, the taa, or a glass of the rain, the insects or the snow in the view (windscreen_water::wants_glass_requests(): the cab seen from inside has one, or the view is not from the cab). the glasses keep their drops and haze with the motion blur off, the motion is blurred by 0 then Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD
379 lines
14 KiB
C++
379 lines
14 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);
|
|
// the cells of the rain: the pass sets hold their buffer from the start (made later, the sets of the passes and of the cullings
|
|
// would have to be written anew in the middle of a frame). without it a small stand-in, which no glass reads (the glasses with
|
|
// rain stay dry, the rest of the simulation works on)
|
|
m_raincells = std::make_shared<buffer>();
|
|
m_raincellsfull = m_raincells->create(Memory, static_cast<VkDeviceSize>(gl::RAIN_CELLS_COUNT * 16), storage_usage, memory_kind::device, "rain cells");
|
|
if (false == m_raincellsfull)
|
|
{
|
|
ErrorLog("rain cells: no storage of " + std::to_string(gl::RAIN_CELLS_COUNT * 16 / (1024 * 1024)) + " MiB, the glasses with rain stay dry");
|
|
m_raincells = std::make_shared<buffer>();
|
|
if (false == m_raincells->create(Memory, 65536, 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_raincellsfull = false;
|
|
m_raincellsvalid = false;
|
|
m_raincells.reset();
|
|
m_storage.reset();
|
|
m_device = VK_NULL_HANDLE;
|
|
m_cleared = false;
|
|
m_snowkernels = true;
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
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)
|
|
{
|
|
if (i < pass::snow)
|
|
{
|
|
throw;
|
|
}
|
|
// the passes of the snow are optional: without them the glasses of the snow stay clear, the rain works on
|
|
ErrorLog("windscreen water: can't use " + std::string(names[i]) + ", the glasses of the snow stay clear: " + std::string(e.what()));
|
|
m_snowkernels = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
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 = ®ion;
|
|
commands->copy_buffer2(©);
|
|
compute_barrier(commands);
|
|
m_deletions->push(m_uploads->current_value(), [staging]() {});
|
|
}
|
|
|
|
void windscreen_water::update_rain_cells(float const Time, float const Rain)
|
|
{
|
|
m_raincellsvalid = false;
|
|
if ((m_device == VK_NULL_HANDLE) || (false == bool(m_raincells)) || (false == m_raincellsfull) || (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);
|
|
m_raincellsvalid = true;
|
|
}
|
|
|
|
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) || (false == m_snowkernels) || (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}) && m_snowkernels)
|
|
{
|
|
run(pass::snow, groups(static_cast<std::size_t>(gl::WATER_MAP) * gl::WATER_MAP));
|
|
}
|
|
}
|
|
|
|
} // namespace vk14
|