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.
Shaders
| Directory | What is in it |
|---|---|
ogl46/ |
GLSL sources of the OpenGL 4.6 renderer |
ogl46bin/ |
SPIR-V binaries of ogl46/ (optional, made by compile_spirv.py) |
vk14/ |
GLSL sources of the Vulkan 1.4 renderer |
vk14bin/ |
SPIR-V binaries of vk14/ (optional, made by compile_spirv.py) |
The renderers load the binary of a shader first, when there is one and it was compiled from the sources that are there now
(manifest.txt of the directory of the binaries lists the sources of each binary with their hashes). Otherwise the source is
compiled: by the OpenGL driver for ogl46/, by glslang inside the simulator for vk14/ (with a cache of the results in the
directory of the user). gfx.shaders.binaries no in eu07.ini makes them always use the sources. The sources are needed
even with the binaries: the directives of the materials (#pragma texture, #pragma param) are read from them.
Compiling to SPIR-V
python shaders/compile_spirv.py
compiles every shader of ogl46/ (with the variant MODEL_DRAWS of the vertex and fragment shaders) and of vk14/ with
glslc (Vulkan SDK, VULKAN_SDK, or --glslc <path>). A single file compiles the same way by hand:
glslc --target-env=opengl4.5 ogl46/mat_default.frag -o ogl46bin/mat_default.frag.spv
glslc --target-env=opengl4.5 -DMODEL_DRAWS=1 ogl46/vertex.vert -o ogl46bin/vertex.vert.MODEL_DRAWS.spv
glslc --target-env=vulkan1.4 vk14/imgui.frag -o vk14bin/imgui.frag.spv
A binary made by hand isn't in the manifest, so it is used only where its source isn't there (otherwise the source is compiled and the log says so). After a change of a source, compile the binaries again (or delete them): a binary older than its source is not used, and the log says so.
Writing shaders
- Every stage file starts with
#version 460 coreand#define STAGE_VERTEX 1(STAGE_FRAGMENT,STAGE_GEOMETRY,STAGE_COMPUTE); files included by others (.glsl) don't. - Includes:
#include "file.glsl", relative to the directory of the file. - Every input and output between the stages has an explicit
layout(location = N), every sampler, image and block an explicit binding, every loose uniform an explicit location: SPIR-V has no names to link by. The locations of the inputs and outputs ofogl46/are one table for all the shaders (a name has the same location everywhere), seeogl46/common.glsl. - The settings of the game (
SHADOWMAP_ENABLED,ENVMAP_ENABLED,MOTIONBLUR_ENABLED,POSTFX_ENABLED,EXTRAEFFECTS_ENABLED,OUTPUT_GAMMA) areconst bool(specialization constants in the binaries,ogl46/settings.glsl): use them inif, not in#if. - A material shader names its textures and parameters with
#pragma texture(name, unit, components)and#pragma param(name, location, offset, size, default); a.matfile sets them astexture_<name>:andparam_<name>:. Every directive of a file and of its includes is read, whatever#ifdefit is in. A fourth argument of#pragma texture(black,white,red,yellow) makes the texture one the material may leave out: the unit then shows a single texel of that colour. - The materials of the metallic and roughness workflow are
mat_pbr.fragand its variants (mat_pbr_detail.frag,mat_pbr_parallax.frag,mat_pbr_coat.frag):mat_pbr.glsldeclares what they share and says what each texture and parameter is,mat_pbr_surface.glslis their fragment stage, the same file inogl46/andvk14/. They definePBR_SURFACE(andPBR_COAT), whichlight_common.glsllights as the texturing tools show the surface. - The constants of the game (bindings, sizes) are written in
ogl46/common.glslandvk14/common.glslfor the binaries; the game puts its own values before the sources and checks at the start that the files have the same ones (the shader validator inci_shadervalidatorchecks them too).
vk14
The shaders of vk14/ are those of ogl46/ made for the descriptor sets of the Vulkan renderer
(rendering/vulkan14/vulkan14bindings.h), drawn with the same matrices and the same conventions as the OpenGL renderer
(nothing is turned upside down but the picture shown in the window, present.frag).
- A stage file includes
common.glslright after its stage define. The pipeline layout it is drawn with is picked by a define before that include: none for the passes of the scene,LAYOUT_POSTfor the post-processing (withquad.vert),LAYOUT_CLUSTERfor the compute passes of the light clusters,LAYOUT_UIfor the user interface. - The scene: set
PASS_SETholds the blocks of the scene, of the draws (model_draws[], the one of a draw picked by the push constantpc.draw), of the instances and of the lights and clusters, and the shadow, environment and headlight maps; setMATERIAL_SETthe textures of the material atMATERIAL_TEXTURE_FIRST + unitof their#pragma texture. The names of the model block (modelview,param,opacity...) stand for the fields of the draw, as in theMODEL_DRAWSvariant ofogl46/. - The push constants (
pc,PUSH_CONSTANTS_SIZEbytes) carry the draw and the parameters of the special shaders inpc.data[]; each of those says at the top what it takes from them. In the post-processingparamstands forpc.data. - The settings are specialization constants with the ids
SPEC_*(vk14/settings.glsl). - The features of the later stages of the renderer (the shadows of the lamps and the headlights, the contact shadows, the
weather and its fog, the sky ambient light, the prefiltered environment map, the automatic exposure, soft and gpu
particles) aren't in
vk14/yet; the code paths are the onesogl46/takes with them off, the fog is an analytic one (vk14/apply_fog.glsl).