MGFX compiled effects
An effect (.fx) in a MonoGame game is compiled by the content pipeline into an MGFX blob and stored as an
XNB whose reader is Microsoft.Xna.Framework.Content.EffectReader; the object is
an i32 byte count and the MGFX bytes. Proven · STUNTBOOST STUNTBOOST’s 18 effects are version 12 (BytingGames’ fork of
MonoGame 3.8.1; upstream releases write 10 and 11) for the OpenGL profile: the shaders are GLSL text that MojoShader wrote from
the effect’s Shader Model 3 programs. Proven · STUNTBOOST
Every effect parses to its last byte, including the trailing MGFX signature; every shader’s GLSL declares the samplers, the
vertex attributes and the uniform array that its tables list; every technique’s passes name shaders of the right stage; every
sampler names a parameter and every constant-buffer entry names one. Proven · STUNTBOOST
Layout
Section titled “Layout”All numbers are little-endian; strings are .NET’s (a 7-bit encoded length, then UTF-8).
| Offset | Size | Type | Field | Confidence | Notes |
|---|---|---|---|---|---|
| 0x0 | 4 | char[4] = MGFX | signature | Proven | |
| 0x4 | 1 | u8 = 12 | version | Proven | |
| 0x5 | 1 | u8 | profile | Proven | 0 OpenGL (all 18 files), 1 DirectX 11 |
| 0x6 | 4 | i32 | effect key | Proven | a different value in each of the 18 files |
| 0xA | 1 | u8 = 1 | MojoShader flag | Observed | 1 in every file; the game's reader treats the shaders as MojoShader output |
After the header, in this order: Proven · STUNTBOOST
| Part | Content | Confidence |
|---|---|---|
| constant buffers | i32 count; per buffer its name, a byte 0, its size in bytes (i16), a byte 0, an i32 parameter count and per parameter its index (i32) and its byte offset in the buffer (u16) |
Proven · STUNTBOOST |
| shaders | i32 count; per shader the stage (a byte: 0 vertex, 1 pixel), source file name and entry point (strings, empty in every file), a byte 0, the code (i32 length and the GLSL bytes), its samplers, the constant buffers it reads and its vertex attributes |
Proven · STUNTBOOST |
| parameters | i32 count; per parameter its class and type, name and semantic, an annotation count (0), rows and columns, its elements and structure members (parameters again), and for a plain bool, int or float its rows × columns default values |
Proven · STUNTBOOST |
| techniques | i32 count; per technique its name, an annotation count (0) and its passes |
Proven · STUNTBOOST |
| tail | MGFX again, at the end of the effect’s bytes |
Proven · STUNTBOOST |
Samplers of a shader: a byte count, then per sampler its kind (2D, cube, volume, 1D), texture slot, sampler slot, whether it
has a state, that state’s 20 bytes (address modes U, V, W as bytes with 0 wrap, 1 clamp, 2 mirror, 3 border; a border colour;
the filter with 0 linear, 1 point, 2 anisotropic; the maximum anisotropy; the largest mip level; the mip bias), then its name
(ps_s0) and the index of the parameter that holds its texture. Proven · STUNTBOOST Constant buffers a shader
reads: a byte count and a byte index each. Vertex attributes: a byte count and per attribute its name, usage
(VertexElementUsage: 0 position, 1 colour, 2 texture coordinate, 3 normal, 4 binormal, 5 tangent, 6 blend indices, 7 blend
weight, …), usage index, location (i16) and a byte 1. Proven · STUNTBOOST
Parameters carry MonoGame’s EffectParameterClass (0 scalar, 1 vector, 2 matrix, 3 object, 4 struct) and
EffectParameterType (1 bool, 2 int, 3 float, 7 texture 2D, 9 texture cube, …). Passes: a name, an annotation count (0),
six shader indices (vertex, pixel, then four more, −1 when the pass has none) and three bytes saying whether the pass has a
blend, a depth-stencil and a rasterizer state. Proven · STUNTBOOST No effect has an annotation, a string parameter or
a pass with a state, so those layouts are not described. Proven · STUNTBOOST
The GLSL
Section titled “The GLSL”The shaders are GLSL with the register names of the Direct3D 9 program they came from: ps_r0 registers, ps_c56 constants,
ps_uniforms_vec4[N] as the one uniform array of vec4s, and a GL_OES_standard_derivatives extension line for derivatives.
Float constants that the program defines are const vec4 ps_c<N>; the effect’s parameters live in the uniform array at
byte offset / 16. Observed · STUNTBOOST A vertex shader’s position output is corrected by a posFixup uniform for
OpenGL’s clip space. Observed · STUNTBOOST
STUNTBOOST’s effects
Section titled “STUNTBOOST’s effects”Content/Effects/D3 holds 10 effects (3D rendering and its post passes), Content/Effects/D2 8 (2D, UI and screen effects).
Proven · STUNTBOOST
| Effect | Shaders | Techniques | Size (bytes) |
|---|---|---|---|
D3/Render |
10 | Render, RenderWithDepthOutput, RenderInstanced, RenderInstancedWithDepthOutput, RenderBeam, RenderBoosterBeam, RenderVolumetricLight | 79,223 |
D3/Depth |
3 | Render, RenderInstanced | 15,698 |
D3/DropShadow |
3 | Render, RenderInstanced | 18,275 |
D3/DisplayPosFromDepth |
3 | RenderWorld, RenderView | 4,216 |
D3/Outlines |
2 | Render | 6,212 |
D3/SSAO |
2 | Render | 4,058 |
D3/Post |
2 | Technique | 4,012 |
D3/DisplayDepth, DisplayShadowMap, Skybox |
2 each | Render | 2,034, 1,885, 1,866 |
D2/Post2D |
2 | Technique | 18,310 |
D2/SpeedSmear |
2 | Radial | 9,535 |
D2/SpeedMeter |
2 | Technique | 3,431 |
D2/GammaPost, Grayscale, D2Template, DropShadowBugDetect, TextColorBlend |
2 each | Technique or Render | 1,934, 1,688, 1,565, 1,486, 1,952 |
All Proven · STUNTBOOST. The Render effect’s pixel constant buffer is a ps_uniforms_vec4 array of 56 vec4 (896 bytes)
with parameters such as ViewProjectionLight[4], ShadowDepthBiases[4], GammaInverse, FogStart, FogLength, FogColor,
ShadowMapSplitCount, Color, LightDir, CamPos, AmbientColor, DiffuseColor, SpecularColor, ShadowColor,
AmbientLightInShadow, MetallicFactor, RoughnessFactor, EmissiveFactor, DynamicObject, InstancedEnabled,
VertexColorEnabled, AlbedoMapEnabled, NormalMapEnabled, OrmMapEnabled, EmissiveMapEnabled, ShadowsEnabled,
DiffuseLightingEnabled and InverseHullOutlines; its vertex buffer (7,184 bytes) has 106 joint matrices and World,
ViewProjection, OutlineThicknessOn0Meters, OutlineThicknessOn3Meters, SkinnedEnabled and VertexShiftEnabled. Its
samplers are AlbedoTex, NormalTex, ORMTex, EmissiveTex, LightDepthTex, DiffuseTex and, for the volumetric technique,
BlueNoiseTex. Proven · STUNTBOOST What these compute is on level rendering.