Shaders
A game's own shaders are compiled to SPIR-V when the game builds, so the Vulkan renderer never
compiles a shader while the game runs. BSGameLib.Game brings the compiler, bsshaderc, and the
build step; nothing else needs installing.
Shaders compiled at build
Write shaders in GLSL the way the engine's own are written, for OpenGL, with .vert and .frag
extensions, and list them in the project file:
<ItemGroup>
<EmbeddedResource Include="Shaders\*" />
<BSGameLibShader Include="Shaders\*.vert;Shaders\*.frag" />
</ItemGroup>
The EmbeddedResource line embeds the GLSL itself, which the OpenGL renderer uses. The
BSGameLibShader line prepares each shader for Vulkan exactly as the engine would at runtime,
compiles it, and embeds the SPIR-V where the Vulkan renderer looks for it. The stage comes from the
extension.
Shaders already written for Vulkan go in BSGameLibRawShader items instead. They are compiled as
they are and embedded as <RootNamespace>.Shaders.<Name><Vert|Frag>.spv, for code that loads
SPIR-V by name.
The compiler runs on the machine doing the build, so building for another platform works.
Shaders the build never saw
A shader that only exists at runtime (read from a file, downloaded, or generated) has no
precompiled SPIR-V. Without a runtime compiler the Vulkan renderer reports it once and draws without
it. To compile such shaders, add BSGameLib.Game.ShaderCompiler:
dotnet add package BSGameLib.Game.ShaderCompiler --prerelease
using BSGameLib.Game.ShaderCompiler;
using Microsoft.Extensions.DependencyInjection;
protected override void Configure(IServiceCollection services) =>
services.AddShaderCompiler();
Compiled shaders are cached on disk, by default in BSGameLib/ShaderCache under the local
application data folder, shared between games; AddShaderCompiler(options => ...) sets
CacheDirectory, or turns the cache off with UseDiskCache. It is a separate package because it
carries shaderc's native library, which a game does not need when every shader is compiled at build.
The materials, lighting and full-screen effects that use these shaders are in Materials, effects and lighting.