Shaders
Purpose
Section titled “Purpose”Inventory of every shader in MainframeEngine/Assets/Shaders,
which C# class loads it, its interface (inputs, sets, bindings, push constants), and how shaders are
built, shared and kept in sync with C#. Shaders are written in Slang (ADR 0144);
the rules for writing them are under Writing shaders.
Workflow
Section titled “Workflow”Shaders compile as part of dotnet build (ADR 0007).
flowchart LR
J["limits.json"] -->|GenerateShaderLimits| L["include/limits.slang<br/>Src/Rendering/Generated/ShaderLimits.g.cs"]
A["*.vk.vert.slang / *.vk.frag.slang<br/>+ include/*.slang"] -->|"CompileShaders (incremental)<br/>slangc -target spirv -profile spirv_1_3<br/>-matrix-layout-row-major -I include"| S["obj/<config>/Shaders/**.spv"]
L --> S
S -->|copied| O["bin/…/Assets/Shaders/**.spv"]
C["committed *.spv<br/>(just shaders)"] -.->|"no slangc: warning MFSHADER001"| O
O --> R["ShaderModuleCache.Get("Shaders/…spv")<br/>(ContentPaths)"]
- One stage per file:
X.vk.vert.slang/X.vk.frag.slang/X.vk.comp.slang, entry pointmain([shader("vertex")]etc.), compiled toX.vk.vert.spvand so on — the names C# loads. The stage comes from the file name. The targets live inbuild/Shaders.targets, imported byMainframeEngine.csproj. - The SPIR-V is 1.3 (Vulkan 1.1) on every platform, the Android baseline (ADR 0285):
-profile spirv_1_3, andbuild/shaders.shvalidates withspirv-val --target-env vulkan1.1. It is-profile, not-capability: a capability only adds to slangc’s default, which emits SPIR-V 1.5. A shader feature that needs a newer SPIR-V needs a capability check and a fallback. slangcis$(Slangc), else$(VULKAN_SDK)/bin/slangc(the LunarG Vulkan SDK ships it next to the validation layers), elseslangconPATH. Without it the build warns and copies the committed.spvfiles;-p:CompileShaders=falseforces that (CI does: its build runners have no slangc and build with-warnaserror). CI’s shaders job installs Slang v2026.1 from its GitHub release (pinned by SHA-256). An olderslangc(belowSlangMinimumVersion, 2026.1) is treated like a missing one: warningMFSHADER001and the committed.spv.shaders.lockrecords theslangcthat built the committed.spv(# slangc …);just shaderswith a different one says so, since the bytes may change (CI checks the lock against the sources, not the bytes). Slang is a build tool only: nothing of it ships.- Shader sources, includes and
shaders.lockare not copied to the output; only.spvfiles are. - After editing a shader run
just shaders(recompiles the committed.spvwith the same flags and rewritesshaders.lock) and commit the.spvfiles with the lock.just shaders-check(and CI) fail when a source, an include or a.spvno longer matches the lock. - C# loads modules through
IVulkanContext.Shaders(ShaderModuleCache): one module per file, shared by every pipeline. Pipelines go throughIVulkanContext.Pipelines(the persisted pipeline cache).
Shared includes
Section titled “Shared includes”#include "x.slang" (resolved through -I include; every include has #pragma once). The includes are textual
headers, not Slang modules, because they are configured by macros defined before including them (MATERIAL_SET,
LIGHTS_SET, SHADOW_SET, CANVAS_LIGHT_SET).
| Include | Contents | Override macros |
|---|---|---|
limits.slang | Generated from limits.json — MAX_DIR_LIGHTS, MAX_POINT_LIGHTS, MAX_SPOT_LIGHTS, MAX_SHADOW_DIR/SPOT/POINT, MAX_SHADOW_CASCADES, MAX_SHADOW_ATLAS_MAPS | — |
common.slang | PI, srgbToLinear, linearToSrgb, glsl_mod, textureSize2D; includes limits.slang | — |
frame.slang | Set 0 binding 0 FrameData (camera, viewport, near/far, time, exposure), linearizeDepth, depthTowardsCamera, afterPostLineDepth | — |
depth.slang | The reversed depth convention (ADR 0288): kDepthNear 1, kDepthFar 0, isSky, nearerDepth, isNearer, linearDepthFrom, depthAtLinear, clipInsideNear/Far, orthoDepthTowardsCamera. Included by common.slang; no other shader compares a sampled depth with a literal | — |
shadows.slang | Shadow set (ShadowUBO, cascade array, atlas, point cubes; all comparison samplers), receiver bias, PCF kernels, dirShadow/spotShadow/pointShadow, shadowCascadeTint (includes frame.slang) | SHADOW_SET (default 1) |
lights.slang | Lights UBO, Blinn-Phong per light, shadeLightsBlinnPhong(base, N, Ngeo, worldPos, specular, shininess) (Ngeo: geometric normal for the shadow normal offset) and shadeLights (0.3, 32); counts.w = 1 skips shadow maps (offscreen views) (needs shadows.slang first) | LIGHTS_SET/LIGHTS_BINDING (default 0/1) |
material.slang | StandardMaterial3D set 2 (MATERIAL_SET; the cutout shadow casters use 1) (parameters UBO, one sampler, albedo/normal/emission images), materialUv/Albedo/Emission, materialNormal (derivative tangent frame) | — |
sky.slang | Sky push constants (SkyParams), skyRay(ndc) | — |
spatial.slang | The helpers of the entries generated around spatial surface shaders (ADR 0205): spatial_normal_matrix, spatial_inv_view, spatial_normal_map (no descriptors, so the casters include it too) | — |
The surface-shader modules (ADR 0237) are real Slang modules under include/mainframe/, imported with import, never
included:
| Module | Contents |
|---|---|
mainframe/spatial.slang, mainframe/canvas.slang | What a game shader imports (import mainframe.spatial; / import mainframe.canvas;): the render-mode attributes and their enums, and it re-exports the three below |
mainframe/material.slang | The parameter attributes ([color], [range], [filter], [repeat], [defaultTexture], [group]) and their enums |
mainframe/math.slang | PI, TAU, E and GLSL’s mod |
mainframe/surface/spatial.slang, mainframe/surface/canvas.slang | The built-in structs (SurfaceVertex, SurfaceFragment, CanvasVertex, CanvasFragment). The generated entries import only these, since they include common.slang’s own PI |
Descriptor frequency model
Section titled “Descriptor frequency model”| Set | Frequency | Contents | Owner |
|---|---|---|---|
| 0 | per frame | b0 FrameData (368 B), b1 lights UBO (1200 B) | FrameContext (IVulkanContext.Frame) |
| 1 | per frame | shadows (shadows.slang) | ShadowSystem or the renderer’s fallback |
| 2 | per material | StandardMaterial3D (material.slang); other drawers’ textures (sky, Spine) | MaterialGpu, the drawer |
| binding 1 (vertex input) | per instance | mesh instances: model matrix + object id (MeshInstanceData) | MeshRenderer instance buffer |
| push | per draw | 128-byte vertex+fragment range: model matrix (+ extras) for non-batched drawers | the drawer |
FrameContext.Begin(camera, lights) writes set 0 once per frame; renderer-owned drawers (sky, grid,
Spine) call EnsureCamera/EnsureLights with the camera they were given, which write only if nothing
has this frame. Pipeline layouts made with FrameContext.CreatePipelineLayout share set 0 (and set 1
when they take shadows) and the push range, so they are compatible. Every frame view (main view, offscreen
SubViewports) has its own set 0 copy (FrameContext.SetView). Meshes follow the model through the
mesh renderer.
In use
Section titled “In use”Each row’s source is the name plus .slang (Mesh/Mesh.vk.vert.slang → Mesh/Mesh.vk.vert.spv).
| Shader | Loaded by | Inputs | Sets / bindings | Push constants |
|---|---|---|---|---|
Mesh/Mesh.vk.vert | MeshRenderer (lit pipelines) | 0 vec3 pos, 1 vec3 normal, 2 vec2 uv; instance 3–6 model rows | s0 frame | — |
Mesh/MeshId.vk.vert | MeshRenderer (MeshObjectId) | 0 pos, 2 uv; instance 3–6 model rows, 7 uint objectId (outputs uv + id only) | s0 frame | — |
Mesh/Mesh.vk.frag | MeshRenderer (MeshLit) | world pos, normal, uv | s0 frame + lights · s1 shadows · s2 material | — ; specialization 0 kAlphaMode |
Mesh/MeshId.vk.frag | MeshRenderer (MeshObjectId) | uv, id | s2 material (cutout) | — ; specialization 0 kAlphaMode; writes uint |
Mesh/MeshOutline.vk.vert | MeshRenderer (MeshOutline) | same as Mesh.vk.vert | s0 frame · s2 b0 material (flags.w = outline width bits; the binding is visible to the vertex stage) | — |
Shadows/Shadow2DInstanced.vk.vert, ShadowPointInstanced.vk.vert | ShadowSystem (instanced casters) | 0 vec3; instance 1–4 model rows | s0 b0 LightVP (dynamic offset) | point: lightPosRange at offset 64 |
Spine/SpineLit.vk.vert | SpineRenderer | 0 vec3, 1 vec2, 2 vec4 | s0 frame | mat4 model; vec4 worldNormal |
Spine/SpineLit.vk.frag | same | uv, tint, world pos, normal | s0 frame + lights · s1 shadows · s2 b0 Sampler2D | — ; specialization 0 kPremultipliedTexture |
Shadows/Shadow2D.vk.vert/.frag | ShadowSystem | 0 vec3 | s0 b0 LightVP{mat4} (dynamic offset) | mat4 model (64 B). The frag shader is empty (depth only). |
Shadows/ShadowPoint.vk.vert/.frag | ShadowSystem | 0 vec3 | s0 b0 LightVP{mat4} (dynamic offset) | mat4 model; vec4 lightPosRange (80 B). The frag shader writes linear depth (SV_Depth). |
Sky/Sky.vk.vert | SkyEnvironment | SV_VulkanVertexID | — | — |
Sky/Sky.Procedural.vk.frag | same | NDC xy | s0 frame | SkyParams (96 B) |
Sky/Sky.Panoramic.vk.frag | same | NDC xy | s0 frame, s1 b0 Sampler2D (sRGB) | SkyParams |
Sky/Sky.Cubemap.vk.frag | same | NDC xy | s0 frame, s1 b0 SamplerCube (sRGB) | SkyParams |
SceneGrid/SceneGrid.vk.vert/.frag | SceneGrid | 0 vec3, 1 vec4, 2 vec3 (other end of the line; the vertex shader clips the line) | s0 frame | — |
SceneGrid/GridAnalytic.vk.vert/.frag | SceneGridAnalytic (Scene grid) | none (6 vertices from SV_VulkanVertexID) | s0 frame | vec4 params (pixel scale, plane height, cell); specialization 0 kDisplayOutput |
Gizmos/Gizmo.vk.vert/.frag | GizmoRenderer (Gizmos) | 0 vec3, 1 vec3 (other end), 2 vec4 (sRGB), 3 vec2 UV, 4 vec2 (side, width px) | s0 frame, s1 atlas | specialization 0 kDisplayOutput |
Post/Fullscreen.vk.vert | tonemap pass | SV_VulkanVertexID | — | — |
Post/Tonemap.vk.frag | VulkanRenderer | SV_Position | s0 b0 Sampler2D HDR scene | float exposure; uint encodeSrgb (8 B) |
Post/TonemapPost.vk.frag | VulkanRenderer (non-default PostProcessSettings, ADR 0124) | SV_Position | s0 b0 Sampler2D HDR scene, b1 sampler2D[7] glow levels | exposure, encodeSrgb, tonemapper, glow mode/enabled/intensity, white, whiteTonemapped, 7 weights (60 B) |
Post/GlowBlur.vk.frag | GlowEffect (ADR 0124) | SV_Position | s0 b0 Sampler2D source (scene, temp or previous level) | dstSize, flags (horizontal, first), strength, exposure, threshold, scale, bloom, luminanceCap (36 B) |
Gizmos/ScreenGizmo.vk.vert/.frag | ScreenGizmosRenderer (Developer overlay) | 0 vec2 (pixels), 1 vec4 (sRGB, straight alpha) | — | vec2 scale; vec2 translate; specialization 0 kLinearizeColors |
The legacy OpenGL shaders and the unused Quad.vk.*/Spine.vk.* were deleted in M3, and Shapes/Shapes.vk.*
with ShapeBase (replaced by the mesh shaders).
Writing shaders
Section titled “Writing shaders”Slang is HLSL-like. These rules keep the shaders matching the C# side (they were checked against the former GLSL shaders’ SPIR-V when the engine moved to Slang):
- Matrices are the C# values. Every shader compiles with
-matrix-layout-row-major, so afloat4x4in a buffer or push constant holds exactly the bytes of the uploadedSystem.Numerics.Matrix4x4, andmul(v, M)transforms likeVector4.Transform(v, M). Chain transforms left to right: world → clip ismul(mul(float4(p, 1.0), model), frame.viewProjection). A matrix built in a shader lists the four C# rows:float4x4(inModel0, inModel1, inModel2, inModel3);m[i]is rowi;(float3x3)mis the upper 3×3. - Built-ins:
SV_VulkanVertexID/SV_VulkanInstanceID(notSV_VertexID/SV_InstanceID, which subtract the base vertex/instance),SV_Position(also the fragment coordinate),SV_Depth,SV_IsFrontFace,SV_Target0. - Interfaces: stage inputs and outputs carry
[[vk::location(n)]]; integer varyings arenointerpolation. Resources carry[[vk::binding(binding, set)]]; push constants are a[[vk::push_constant]] ConstantBuffer<T>([[vk::offset(n)]]on a member reproduces a range that starts past 0); specialization constants are[vk::constant_id(n)] const T k = default;. Uniform buffers are std140 and push constants std430 by default. - Textures: combined
Sampler2D/SamplerCube/Sampler2DShadow/Sampler2DArrayShadow/SamplerCubeShadowwithSample,SampleLevel,SampleGrad,SampleCmp(implicit LOD) andLoad; separateTexture2D+SamplerStatefor the material set. - Maths:
lerp,frac,atan2(y, x),rsqrt,ddx/ddy;glsl_mod(fromcommon.slang) for GLSL’smod— neverfmod, which rounds towards zero. - A stage writes only what the next one reads. Slang drops every unread stage input from the SPIR-V (no option
keeps them), and the validation layer warns about a vertex output no fragment input reads, and about a vertex
attribute the vertex shader does not read. So a vertex shader shared by several fragment shaders outputs only what
they all read (
Post/Fullscreen.vk.vertoutputs nothing but the position), the object-ID pass has its ownMesh/MeshId.vk.vert, and each mesh pipeline passes only the attributes its vertex shader reads (VertexLayouts.MeshAttributes/MeshIdAttributes). Unused resources are dropped too; that needs nothing (the pipeline layouts come from C#). - No Metal keywords as names (
vertex,fragment,kernel,constant,device, …): Slang keeps local names in the SPIR-V, and MoltenVK’s SPIRV-Cross passes them into the Metal source, which then fails to compile. - Game shaders are surface shaders in plain Slang (Surface shaders below): the engine generates their passes, so the rules above are its job, not the game’s.
Surface shaders
Section titled “Surface shaders”Game shaders are Slang surface shaders (ADR 0237): .slang files
under a project’s Assets/ whose first statement is import mainframe.spatial; (3D meshes, as a ShaderMaterial: on a
MeshInstance3D or MultiMeshInstance3D, as a surface material, MaterialOverride, MaterialOverlay or NextPass; ADR
0205) or import mainframe.canvas; (canvas items, Canvas shaders; ADR 0113). The game writes
a material, the values its stages share and up to two functions against built-in structs; the engine generates every pass
around them and owns their descriptor sets, vertex inputs and stage interfaces. Any other .slang file under Assets/ is
a helper module shaders can import (start it with module name;), never a Shader resource. Before ADR 0237 game
shaders were Godot’s shading language (.mfshader, .gdshader); such files now fail to load with an error that points at
Porting from Godot.
import mainframe.spatial;
[cull(Cull.Disabled), specular(Specular.BlinnPhong)]struct Material{ [color] float3 stalk_color = float3(0.85, 0.80, 0.70); float sway = 0.45; float speed = 1.3;};
struct Varyings{ float3 tint;};
void vertex(inout SurfaceVertex v, Material m, out Varyings o){ float3 world = mul(float4(v.vertex, 1.0), v.modelMatrix).xyz; // the C# matrices: mul(v, M) float bend = (1.0 - v.uv.y) * (1.0 - v.uv.y); v.vertex.x += sin(v.time * m.speed + v.instanceCustom.x * TAU + world.x * 0.4) * m.sway * bend; o.tint = v.instanceCustom.yzw;}
void fragment(inout SurfaceFragment s, Material m, Varyings i){ s.albedo = lerp(m.stalk_color, i.tint, smoothstep(0.25, 0.15, s.uv.y)); s.roughness = 0.6;}The contract
Section titled “The contract”| Piece | Rule |
|---|---|
import mainframe.spatial; / import mainframe.canvas; | The first statement (after comments): the file is a shader, and of that kind |
struct Material | Optional. Its fields are the material parameters, its attributes the render modes (below) |
struct Varyings | Optional. What vertex passes to fragment; integer fields are flat, others take nointerpolation to be |
void vertex(inout SurfaceVertex v[, Material m][, out Varyings o]) | Optional; the material and varyings in either order. Without it the mesh’s own vertices draw |
void fragment(inout SurfaceFragment s[, Material m][, Varyings i]) | Optional. Without it the surface is white (canvas: the vertex colour × the texture) |
| Canvas | CanvasVertex / CanvasFragment, the same optional parameters |
| Anything else | Helper functions, static consts, structs, imports of the project’s modules (next to the shader or below it). A global shader parameter (uniform float x;, a global Sampler2D) is a build error: parameters go in Material |
mf-shaders reads only the first statement, which optional parameters each function declares, the literal defaults and
whether the code names normalMap, lightVertex, alpha or alphaScissorThreshold; slangc checks everything else.
Names starting with mf_, main and the engine includes’ globals (frame, lights, shadeLightsPbr, …) are reserved:
the generated entry imports the shader as a module beside them.
Built-ins are camelCase struct fields with the meanings and spaces Godot’s built-ins have. SurfaceVertex (model space;
world space with [worldVertexCoords]): vertex and normal (write them to displace, in every pass), uv, uv2 (= uv:
meshes have one UV set), color (white without vertex colours), custom0 (custom1–3 are zero), instanceId and
instanceCustom (a MultiMesh’s index and custom data, zero otherwise), vertexId, modelMatrix, modelNormalMatrix
(normals: mul(n, modelNormalMatrix)), modelViewMatrix, modelViewNormalMatrix, viewMatrix, invViewMatrix,
mainCamInvViewMatrix, projectionMatrix, invProjectionMatrix, nodePositionWorld, nodePositionView,
cameraPositionWorld, cameraDirectionWorld, viewportSize, time. SurfaceFragment (view space): vertex, normal
(written back, it shades), view, uv, uv2, color, fragCoord, frontFacing, screenUv, the same matrices and
camera values, time, and the outputs albedo (1), alpha (1), alphaScissorThreshold (0), metallic (0), roughness
(1), specular (0.5, Blinn-Phong only), ao (1), emission (0), normalMap + normalMapDepth (a tangent frame from
screen derivatives, as material.slang; the colour pass applies them only when the code names normalMap) and
lightVertex (the position lights are computed at; used only when the code names it). Matrices are the C# values:
mul(v, M), m[3].xyz the translation. In the shadow passes “view space” is world space (viewMatrix = identity), so
mul(float4(s.vertex, 1.0), s.invViewMatrix) is still the world position. Godot built-ins the engine ignored (rim,
clearcoat, anisotropy, SSS, backlight, fog, radiance, irradiance, tangents, point size) are not declared, so using one is a
compile error. mainframe.math adds PI, TAU, E and GLSL’s mod (Slang’s fmod rounds towards zero).
Material parameters
Section titled “Material parameters”struct Material’s fields are the parameters, read from slangc’s -reflection-json: one std140 block at binding 0 of the
material set (set 2; set 1 in the shadow casters; canvas: set 1) in declaration order, then the textures at bindings 1….
A material of textures only is declared at binding 1, so its textures still start there (slangc drops the empty block).
Types: bool, int, uint, float, floatN, intN/uintN, float2x2/3x3/4x4, 1-D arrays of those, and
Sampler2D (up to 4 in a spatial shader: the mesh fragment stage already binds 12 of MoltenVK’s 16 samplers). Anything
else is a build error naming Material.<field>. Values come from ShaderMaterial.SetShaderParameter or a .mfres
(ShaderParameters); the mesh side is in Materials & meshes.
| Attribute | Meaning |
|---|---|
[color] | A vector authored in sRGB: converted to linear for spatial shaders, kept as written for canvas ones (Godot’s non-HDR canvas); on a texture, sampled as sRGB |
[range(a, b)] | The range an editor offers (stored nowhere yet) |
[filter(Filter.Nearest)], Filter.Linear | A texture’s filter over the texture’s own sampling |
[repeat(Repeat.Enabled)], Repeat.Disabled | A texture’s repeat |
[defaultTexture(DefaultTexture.Black)], .Transparent, .Normal | What an unset texture samples (white without it; Normal is linear data) |
[group("Wind")] | An inspector group (stored nowhere yet) |
Defaults are literal initializers: float sway = 0.45;, float4 tint = float4(0.4, 0.8, 0.3, 1.0); (one value
broadcasts), bool on = true;. mf-shaders reads them from the text and checks their count against the reflected type;
anything computed (0.5 * 2.0, PI) is a build error, as are array defaults for now. Slang’s reflection reports no
initializers (its C API, scalars only), so this is the one place mf-shaders reads values from the source.
Render modes
Section titled “Render modes”Attributes on struct Material (an empty struct Material {} carries them for a shader without parameters), typed by the
module’s enums, so slangc rejects a typo at its line ('Disabld' is not a member of 'typeof(Cull)'):
| Attribute | Spatial |
|---|---|
[blend(Blend.Mix)], Blend.Add, Blend.PremulAlpha | Any but Mix blends the surface |
[alpha(Alpha.Opaque)], Alpha.Cutout, Alpha.Blend | Opaque without it. Cutout discards below alphaScissorThreshold and casts cut shadows; Blend blends back to front, without shadows. A non-Mix blend implies Blend. mf-shaders warns when the code writes alpha on an opaque material, or names alphaScissorThreshold without Cutout |
[cull(Cull.Back)], Cull.Front, Cull.Disabled | Disabled lights back faces with the flipped normal |
[depthDraw(DepthDraw.Opaque)], .Always, .Never | Which surfaces write depth |
[noDepthTest], [unshaded], [worldVertexCoords], [noFog] | No depth test; albedo + emission, no lights; vertex in world space; no fog |
[specular(Specular.Pbr)], Specular.BlinnPhong | The engine’s PBR (the default), or its Blinn-Phong (shininess from roughness, strength from specular) |
Canvas shaders take [blend(Blend.Mix|Add|Sub|Mul|PremulAlpha|Disabled)], [unshaded] and [lightOnly].
Passes
Section titled “Passes”Each spatial shader is compiled for five passes, a vertex and a fragment stage each, and every pass runs the shader’s
vertex and fragment, so displacement, discard and the alpha cut agree everywhere:
| Pass | SPIR-V (x.slang + …) | Set 0 / the material set | What it writes |
|---|---|---|---|
| Colour | .vert.spv, .frag.spv | the frame, lights, sky lighting, SSAO / set 2 | the surface lit by the engine (or [unshaded]) + emission, fogged |
| Depth prepass (ADR 0163) | .depth.vert.spv, .depth.frag.spv | the frame / set 2 | depth and the velocity: vertex runs for this frame and for the last (last frame’s model matrix and time) |
| Directional/spot shadows | .shadow.* | the light matrix / set 1 | depth (time and the camera from the casters’ push block; in fragment through a flat varying) |
| Point shadows | .shadow-point.* | the light matrix / set 1 | the linear distance to the light (SV_Depth) |
| Object IDs | .id.* | the frame / set 2 | the instance’s object id (cut out like the colour pass) |
Constant 0 of the colour, depth and ID fragment stages is the alpha mode (a prepassed cutout runs as opaque: its EQUAL
depth test replaces the discard); the casters are built for the shader’s own mode. The colour and prepass vertex stages
get an invariant position (SpirvInvariance). Next passes and overlays draw with a small depth bias towards the camera
(another vertex module reaches the surface by other instructions, a rounding step apart). Varyings sit from location 12
(canvas: 2) up to 31.
Not yet: light(), instance and global parameters, POSITION/DEPTH writes, tangents. Use SampleLevel in vertex.
Building
Section titled “Building”The SPIR-V is built ahead of time (no runtime compiler) and committed next to the source: just canvas-shaders [folders]
(mf-shaders build) and just shaders-check (mf-shaders check over surface_shader_folders). For each shader
SurfaceShaderBuild:
- compiles a probe entry (the material at its binding, a fragment input of the varyings, nothing else) for its
-reflection-json, whichSlangReflectionturns into the layout: the parameters with slangc’s offsets, the render modes, the varyings’ locations. Every other reflected parameter is the shader’s own global, which is an error; - generates every pass’s entry (
SurfaceShaderCompiler;mf-shaders translate x.slang [pass]prints one), whichimports the shader as a module, fills the built-in struct, calls its functions with the parameters they declare and finishes the pass. Each fragment stage compiles first; its vertex stage then writes only the inputs that stage kept (SpirvInputs; Slang drops unread fragment inputs). Ten slangc runs in parallel for a spatial shader; - writes
x.slang.mflayout.json(the layout in the engine’s own format, so nothing reads Slang’s JSON at run time) andx.slang.mfspvlock: its first line hashes the shader, its layout and its generated entries (whatShader.Loadchecks, warning when stale); each further line hashes a file slangc’s-depfilelisted (include:frame.slang,include:mainframe/spatial.slang, an importednoise.slang), whichcheckverifies too. The depfile names files by their real path (slangc resolves symbolic links: macOS’s temp folder/var/…is/private/var/…), so the build compares and relativises them against real paths too (SurfaceShaderBuild.RealPath); its own temp entry files never reach the lock.
The flags are the engine’s (-matrix-layout-row-major, -profile spirv_1_3) plus -obfuscate, which drops every name from the
SPIR-V (MoltenVK’s SPIRV-Cross would carry a name like the Metal keyword vertex into the Metal source); the probe is not
obfuscated. Errors name the shader’s file and line, since slangc compiles it as a module: ground.slang(11): error 30015: undefined identifier 'x'. No #line directives are generated, so slangc 2026.1’s Linux crash under -obfuscate, which
needed them, cannot happen. A shader without its layout file loads with no parameters and draws with the default
material (a warning).
Editor support
Section titled “Editor support”.slang is the shader-slang VS Code extension’s own language (slangd underneath): diagnostics, completion on s. and
v., hover with the modules’ /// docs, go-to-definition. It needs the engine’s include folder on its search path. Every
build of a game’s executable copies that folder to .mainframe/shaders/include/ next to project.mfproj (from the
package’s mainframe/shaders/include/, or a checkout’s), and the mfgame template’s .vscode/settings.json points there:
"slang.additionalSearchPaths": ["${workspaceFolder}/.mainframe/shaders/include"]In this repository, point it at ${workspaceFolder}/MainframeEngine/Assets/Shaders/include.
Building from a game
Section titled “Building from a game”Games run mf-shaders through the MainframeShaders target (MainframeGame.props, the package’s or a checkout’s):
dotnet msbuild MyGame.Desktop -t:MainframeShaders builds the stale shaders under Assets/ (needs slangc), and
-p:MainframeShadersCommand=check only checks them. mf-shaders takes --include <dir>; without it, the package’s copy
finds mainframe/shaders/include/ next to itself and a checkout’s finds MainframeEngine/Assets/Shaders/include/
(Distribution via NuGet → Surface shaders).
Porting from Godot
Section titled “Porting from Godot”A Godot .mfshader (or .gdshader) becomes x.slang in place: git mv it and its .mfmeta (the UID stays) and delete
its old .spv and .mfspvlock, then:
shader_type spatial;→import mainframe.spatial;(canvas_item→mainframe.canvas);uniform T name : hints = value;→ aMaterialfield with the attribute table’s attributes and a literal initializer (source_color→[color],hint_range→[range],filter_*→[filter(…)],repeat_*→[repeat(…)],hint_default_*/hint_normal→[defaultTexture(…)]);render_mode→Materialattributes;varying→ aVaryingsfield (flat→nointerpolation);- give
vertex()/fragment()their parameters, and prefix built-ins (VERTEX→v.vertex,ALBEDO→s.albedo,INSTANCE_CUSTOM→v.instanceCustom), parameters (m.name) and varyings (o.nameinvertex,i.nameinfragment). Helpers that sampled a uniform take the texture (or theMaterial) as a parameter; - the language:
vecN/ivecN/matN→floatN/intN/floatNxN,mix→lerp,fract→frac,dFdx/dFdy→ddx/ddy,atan(y, x)→atan2(y, x),texture(t, uv)→t.Sample(uv),textureLod→SampleLevel,const→static const; rename locals that are HLSL keywords (line,point,patch,sample); - matrix products swap:
M * v→mul(v, M)(MODEL_MATRIX[3]staysmodelMatrix[3]); - Godot’s alpha rule is gone: a shader that wrote
ALPHA_SCISSOR_THRESHOLDtakes[alpha(Alpha.Cutout)], one that wroteALPHA[alpha(Alpha.Blend)].
The 19 shaders of the render-test host, the Demo and the Neon were ported this way with every render golden unchanged.
Changes from the proposal
Section titled “Changes from the proposal”The G18 proposal (folded into this section when it shipped) left five questions to the owner, which ADR 0237 records; those answers and what building it found:
.slang, not.mfshader(owner): the files are Slang’s, so editors know them unconfigured. Detection is the first statement, so helper modules share the extension.- camelCase built-ins (owner), not Godot’s upper case.
- Literal initializers (owner) instead of
[Default("…")]. Slang’s C API reports scalar defaults only,slangievaluated a vector initializer wrongly, and an extra SPIR-V compile can’t default-construct aMaterialholding aSampler2D, so mf-shaders reads the literals itself. - No deprecation period (owner): the Godot front end,
-allow-glslandglsl_compat.slangare gone, and.mfshader/.gdshaderfiles fail to load with the porting error. The proposal’s Godot/Slang golden-parity theory went with it; the unchanged goldens prove the port instead. import, not#include, of the user file: with a.slangextension the shader is a module, with its own namespace, and errors still name it.- Lowercase attributes (
[color],[cull(…)]), as Slang’s own ([shader],[numthreads]). - The per-pass code stays C# templates (
SurfaceShaderCompiler), ported from the Godot ones, rather than moving into static Slang modules: the SPIR-V keeps the same maths. - A textures-only material moves to binding 1, and the engine’s constants live apart (
mainframe.math;mainframe.surface.*hold the structs the entries import), because slangc drops an empty block andcommon.slangdeclares its own PI. Neither was in the proposal. - The include path for editors, the proposal’s
.mainframe/shaders/copy, came in a follow-up: the package ships the modules andmf-shaders, and the template’s.vscode/settings.jsonpoints there.
Constants that must match C#
Section titled “Constants that must match C#”Light and shadow limits come from limits.json
(edit only there): LightEnvironment.MaxDirectional/MaxPoint/MaxSpot and
ShadowSystem.MaxShadowDir/MaxShadowSpot/MaxShadowPoint/MaxCascades are defined from the generated ShaderLimits;
ShaderLimitsTests fails if the generated files drift or a shader re-#defines a limit (limits.json names the shader
macro shader).
The UBO and push layouts (LightsUBO 1200 B, FrameData 368 B, SkyParams 96 B, ShadowUBO
1680 B) must match the C# writers byte for byte. See Lighting,
Sky and Shadow system.
Known issues
Section titled “Known issues”.spvfiles are committed as the no-SDK fallback, so a shader change touches both the source and the binary (enforced byshaders.lock).
Related docs
Section titled “Related docs”Build & platforms · GPU resources · Color pipeline · Vulkan renderer