18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 23:17:56 +01:00
Files
maszyna/rendering/common/drivercrashguard.h
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

80 lines
3.8 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/.
*/
// the programs a graphics driver crashed on while it compiled them. a fault inside the driver (an access violation of its
// compiler, not an error it reports) takes the whole simulator down: the NVIDIA driver 610.88 did that on water_snowlayer.comp
// as the cab loaded, at every start. so a program a renderer can go without (the water and the snow on the windscreens) is made
// under this guard:
// - where the fault can be caught (built with msvc), it is, and the session goes on without the program;
// - the program is written down in the cache of the renderer before the driver gets it and crossed out after, so a session the
// driver took down anyway (a fault not caught, on a thread of the driver) leaves it behind. the next session takes it as
// suspect, and as one the driver crashed on when a session ends while it is made a second time: a session ended from outside
// while the driver compiled (some programs take seconds) doesn't leave the program out. made well, it is no longer suspect;
// - a program the driver crashed on isn't made again with that driver. the records name the driver and the program with the hash
// of its code, so a new version of the driver, or a changed program, is tried again.
//
// no graphics api here (the unit tests build it on its own)
#pragma once
#include <functional>
#include <mutex>
#include <set>
#include <string>
#include <string_view>
namespace gfx
{
class DriverCrashGuard
{
public:
enum class Outcome
{
made, // the program was made (whether the driver could make it or not is the caller's to tell)
skipped, // the driver crashed on the program before: nothing was done
crashed // the driver faulted while it made the program, caught
};
// Directory: the cache of the renderer, with a separator at the end (made when it isn't there); Driver: the device and the
// version of its driver. a program the session before left written down is taken as suspect, or as one the driver crashed on
// when it was suspect already
void Init(std::string_view directory, std::string_view driver);
// Make(Context) run for the program Key (its name and the hash of its code) under the guard. a c++ exception of Make goes on
// through, the program crossed out. one program is made under the guard at a time
Outcome Run(std::string_view key, void (*make)(void *), void *context);
// whether the driver crashed on the program Key before
bool Crashed(std::string_view key) const;
// whether a session ended once while the driver made the program Key
bool Suspect(std::string_view key) const;
private:
void writeAttempt(std::string const &key) const;
void removeAttempt() const;
void recordCrash(std::string const &key);
void clearSuspect(std::string const &key);
mutable std::mutex mutex;
// driver_crashes.txt, driver_crash_suspects.txt and driver_crash_attempt.txt of the cache, lines of the driver and the program
// parted by a tab; empty before Init(), when nothing is written down (the programs are still guarded)
std::string recordsPath;
std::string suspectsPath;
std::string attemptPath;
std::string driver;
std::set<std::string, std::less<>> crashedKeys;
std::set<std::string, std::less<>> suspectKeys;
};
// Function(Context); false when it raised a fault (a structured exception of windows other than one of c++, and other than the
// overflow of the stack, which leaves the thread unable to go on) which was caught. a c++ exception goes on through. where faults
// can't be caught (not built with msvc) the call is made as it is and true returned
bool CallCatchingFaults(void (*function)(void *), void *context);
} // namespace gfx