18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/rendering/vulkan14/vulkan14water.cpp
T
Hirek 48b7c679a0 Windscreen snow: no crash of the simulator when the driver's compiler faults
A player's simulator (Vulkan, GeForce RTX 4060, NVIDIA 610.88, through Steam) crashed at every start
as the cab loaded: an access violation (read of address 8) inside nvgpucomp64.dll while
vkCreateComputePipelines compiled water_snowlayer.comp. The crash came on $baltyk_skm1.scn and $td.scn
alike, at the same address in the driver.

The snow shaders (shaders/vk14 and shaders/ogl46, the same in both) no longer use what the
compute shader alone had: a vector component written at an index known only while running through an
inout parameter (snRef), constant arrays of structs and of ints indexed at run time (sn_grids,
sn_lump_tiles, the neighbours of snSpan) and a local vec4 indexed by the blade. They are worked out by
small functions and explicit components instead (snRefPut, snRefWord, snGrid, snLumpTile, snNear),
and the blade is read from the storage as water_snow.comp, which the same driver compiled, does.
The results are the same. glslc compiles both variants and spirv-val passes; whether 610.88 compiles
them now can't be checked without that driver.

So that a driver crashing on a program the renderer can go without doesn't take the simulator down,
the programs of the windscreen water are made under gfx::DriverCrashGuard
(rendering/common/drivercrashguard.*):
- with msvc the fault of the driver is caught (SEH) and the session goes on without the program: the
  glasses without the snow, or without the simulation of the water;
- the program is written down in the cache of the renderer (driver_crash_attempt.txt) before the driver
  gets it and crossed out after. a program a session ended on is suspect (driver_crash_suspects.txt);
  when a second session ends on it, or when a fault was caught, it is recorded (driver_crashes.txt) and
  not made again with that driver. a new driver or a changed program is tried again;
- Vulkan makes these pipelines without the VkPipelineCache, whose lock a faulting driver might leave
  taken (the drivers keep their own cache on the disk).

Tested: unit tests (1211/1211, with a caught access violation), Vulkan and OpenGL 4.6 in the cab of
303E on $td.scn (RTX 4070, 621.14), and on Vulkan with a real access violation injected into the
pipeline of water_snowlayer.comp: the session went on without the snow, the next one skipped it.

No change of the game data.
2026-11-30 19:39:29 +01:00

381 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
{
// under the guard against the driver crashing on a kernel (the compiler of the NVIDIA driver 610.88 did on
// water_snowlayer.comp): the glasses then go without the snow, or without the simulation
if (false == m_kernels[i].create(m_device, *m_pipelines, m_shaders->get(names[i]), m_passlayout, {}, names[i], PipelineGuard::driverCrash))
{
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 = &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)
{
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