18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/shaders
maj00r 1d8fdffac5 feat(render): models with lights drawn instanced, behind gfx.instancedlights in eu07.ini (#538)
* feat(render): models with lights drawn instanced, behind gfx.instancedlights in eu07.ini

Stage 5 of the EU07M model format. A model with lights (lights, Light_On## / Light_Off##), such as a signal head, was
drawn instance by instance, a draw call for each part of each one. With gfx.instancedlights 1 the instances of such a
model go into the instanced batches like the others, in both renderers, with the culling of the GPU and without it. The
default 0 draws them as before. It applies to EU07M and E3D models alike.

- The light state of an instance is a word in the element [0][3] of its instance matrix, which an affine matrix leaves
  unused: bits 0-7 the lit parts shown, bits 8-15 the dark parts, in the mantissa of a float of exponent 128
  (0x40000000), a normal number the GPU carries through its matrix products whole. Without that marker the shader
  draws everything, as before.
- model_ubs.light_part says which part of which slot a draw is (0 none, 1 + slot lit, 9 + slot dark); a part the
  instance doesn't show gets a zero matrix in the vertex shader (instance_hides_part() and effective_modelview() in
  shaders/vk14/common.glsl and shaders/ogl46/common.glsl). The batch instance shows every light part
  (TAnimModel::RaPrepareInstanced()).
- CPU path: each instance drawn is animated (RaAnimate) and its word put in its matrix (Render_Instanced). GPU culling:
  the word is in rotationscale[0].w of the instance record, refreshed once a frame for buckets with lights
  (Refresh_Light_Records), which marks the records to send again when a word changes.
- An instance shows the whole lit part or the whole dark one: blinking and switching (ls_On / ls_Off / ls_Blink) change
  over at half the fade, where the draw instance by instance fades the lit part in over the dark one. Steady states look
  the same as before.
- The freespot lights (TP_FREESPOTLIGHT with flag 0x4000 and no animation) don't keep a model out of the instancing
  when gfx.instancedlights is on. Light parts aren't put in the ray tracing of an instance batch.

Measured on Vulkan, 200 heads s4ipcpb_dd: with gfx.gpucull 0 frame 9.2 ms -> 1.5-2.5 ms and 2400 -> 16 draw calls, with
gfx.gpucull 1 9.1 ms -> 3.0 ms and 2410 -> 26 draw calls. Steady light states pixel for pixel the same as drawn
instance by instance on all three paths in Vulkan, and on GL 4.6 where the editor panels don't cover them; blinking
checked in Vulkan on all three paths (with time let run in the editor for the test only). Not run in the driver mode,
blinking not checked on GL 4.6, not built with GCC here. Unit tests 1189/1189.

* fix(render): EU07M review of #538: an instance with light colours of its own drawn alone, the light records locked only when stale

- An instance with lightcolors of its own isn't instanced: the colours go onto the shared submodels as the instance is
  prepared, which the instances of a bucket don't each get, so they all showed the colours of one of them.
- The walk takes the exclusive lock for the light records of a bucket only when they are stale in this frame
  (Refresh_Light_Records() checks again under it): the later passes of the frame no longer stop the other walkers.
- instance_cull.comp (both renderers) puts the word of the light parts into [0][3] after the product with the view, so a
  view with a translation can't stretch the first column of a lit instance.
- The spec: ls_Dark / ls_Home switch without the fade too, and the instances with light colours of their own.
2026-12-01 18:11:53 +01:00
..

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 core and #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 of ogl46/ are one table for all the shaders (a name has the same location everywhere), see ogl46/common.glsl.
  • The settings of the game (SHADOWMAP_ENABLED, ENVMAP_ENABLED, MOTIONBLUR_ENABLED, POSTFX_ENABLED, EXTRAEFFECTS_ENABLED, OUTPUT_GAMMA) are const bool (specialization constants in the binaries, ogl46/settings.glsl): use them in if, 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 .mat file sets them as texture_<name>: and param_<name>:. Every directive of a file and of its includes is read, whatever #ifdef it 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.frag and its variants (mat_pbr_detail.frag, mat_pbr_parallax.frag, mat_pbr_coat.frag): mat_pbr.glsl declares what they share and says what each texture and parameter is, mat_pbr_surface.glsl is their fragment stage, the same file in ogl46/ and vk14/. They define PBR_SURFACE (and PBR_COAT), which light_common.glsl lights as the texturing tools show the surface.
  • The constants of the game (bindings, sizes) are written in ogl46/common.glsl and vk14/common.glsl for 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 in ci_shadervalidator checks 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.glsl right 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_POST for the post-processing (with quad.vert), LAYOUT_CLUSTER for the compute passes of the light clusters, LAYOUT_UI for the user interface.
  • The scene: set PASS_SET holds the blocks of the scene, of the draws (model_draws[], the one of a draw picked by the push constant pc.draw), of the instances and of the lights and clusters, and the shadow, environment and headlight maps; set MATERIAL_SET the textures of the material at MATERIAL_TEXTURE_FIRST + unit of their #pragma texture. The names of the model block (modelview, param, opacity ...) stand for the fields of the draw, as in the MODEL_DRAWS variant of ogl46/.
  • The push constants (pc, PUSH_CONSTANTS_SIZE bytes) carry the draw and the parameters of the special shaders in pc.data[]; each of those says at the top what it takes from them. In the post-processing param stands for pc.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 ones ogl46/ takes with them off, the fog is an analytic one (vk14/apply_fog.glsl).