alca259/monogame-skills

monogame-effects

MonoGame custom effects guide covering MGFX runtime, stock effects (BasicEffect, SpriteEffect, etc.), cross-platform shader compilation (HLSL vs GLSL), shader model targets, preprocessor symbols, and portability pitfalls.

Voir la source
Document Skill original

Rendu depuis le dépôt source en conservant titres, exemples, code, tableaux, liens et images.

MonoGame Custom Effects Implementation Guide

This skill covers the MGFX effect system, stock effects, and cross-platform shader compilation. For HLSL syntax, the Material pattern, and SpriteBatch integration, see the monogame-shaders skill. For compiled API signatures, see references/effects.md.

MGFX — MonoGame's Effect System

MonoGame uses its own FX runtime called MGFX, designed to support multiple shader backends from a single .fx source file. Core capabilities:

  • Same technique/pass/shader structure as Microsoft FX files.
  • Textual .fx source compiled to optimized .mgfxo binary at build time.
  • Cross-platform: targets DirectX HLSL and OpenGL GLSL from one source via MojoShader.

Pipeline:

MyEffect.fx  →  MGFXC (or MGCB)  →  MyEffect.mgfxo  →  Content.Load<Effect>()

Stock Effects

MonoGame ships six built-in effects, all fully supported on current platforms:

EffectPurpose
SpriteEffectDefault 2D sprite rendering — used internally by SpriteBatch
BasicEffectSimple 3D with lighting, fog, and vertex colors
AlphaTestEffectAlpha-tested cutout rendering (no blending)
DualTextureEffectTwo-texture blending (detail maps, lightmaps)
EnvironmentMapEffectCubemap reflections for 3D surfaces
SkinnedEffectGPU skinning for animated models (up to 72 bones)

These are built with the same MGFX pipeline. To optimize a stock effect for your target platform, copy it from MonoGame.Framework/Platform/Graphics/Effect/Resources, edit out unused features, and recompile with MGFXC.

Shader Model Targets

DirectX targets

Shader modelMinimum GraphicsProfile
vs_4_0_level_9_1 / ps_4_0_level_9_1Reach
vs_4_0_level_9_3 / ps_4_0_level_9_3Reach
vs_4_0 / ps_4_0HiDef
vs_4_1 / ps_4_1HiDef
vs_5_0 / ps_5_0HiDef

OpenGL/GLSL targets (via MojoShader)

| Shader model | |---| | vs_2_0 / ps_2_0 | | vs_3_0 / ps_3_0 |

OpenGL targets are limited to SM 3.0 — do not use SM 4+ features in code paths that must run on GL platforms.

Cross-Platform Preprocessor Symbols

MonoGame defines these symbols when compiling .fx files:

SymbolWhen defined
2MGFXAlways (marks that MGFX is the compiler, not FXC)
HLSLDirectX builds
SM4DirectX builds
OpenGLOpenGL builds
GLSLOpenGL builds

Canonical cross-platform shader model selection

Always use #if OPENGL guards to pick the right shader model — never hardcode vs_4_0 in portable shaders:

hlsl
#if OPENGL
    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
#else
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1
#endif

technique MyTechnique
{
    pass Pass1
    {
        VertexShader = compile VS_SHADERMODEL MainVS();
        PixelShader  = compile PS_SHADERMODEL MainPS();
    }
}

Custom symbols can be defined via the MGCB Editor (per-asset Defines field) or the MGFXC command line (--defines).

Effect Writing Rules

These prevent silent bugs and cross-platform breakage:

  1. Vertex shader outputs must exactly match pixel shader inputs — same types, same semantic order. Omitting a parameter may compile but will pass garbage in the wrong register on some platforms.
  1. No default values on GL platforms — float Intensity = 0.5; in the effect file works on DX but is silently ignored on OpenGL. Either set the value from C# after loading, or use #define:
hlsl
   #define INTENSITY 0.5
  1. Unused parameters are aggressively stripped — if you declare a parameter but never read it in a shader, the compiler removes it. Parameters["MyParam"].SetValue(...) will then silently fail (the parameter won't exist). Ensure every declared parameter is actually read in the shader body.
  1. Preshaders are not supported — MonoGame ignores preshader blocks.
  1. Test on both DX and GL — a shader that compiles on DX may fail on GL due to SM level differences or MojoShader translation limits.

Two Ways to Use Custom Effects

MethodWhen to use
MGCB (Effect - MonoGame processor)Standard workflow — assets managed with Content Pipeline, loaded via ContentManager
MGFXC standalone toolRuntime-loaded effects, build scripts, CI pipelines without MGCB

Prefer MGCB for all game assets. Use MGFXC directly only for tools or editor workflows that bypass the Content Pipeline.

Reference

For HLSL code templates, parameter setting patterns, and multi-pass effect examples, see references/effects.md. For SpriteBatch integration, post-processing pipelines, and the Material class pattern, see the monogame-shaders skill.

du même dépôt

Autres Skills

Tous les Skills
alca259
Communauté

monogame-audio

MonoGame audio implementation guide covering SoundEffect vs Song, SoundEffectInstance pooling, volume/pitch control, 3D audio, DynamicSoundEffectInstance, WAV streaming via TitleContainer, microphone recording, and memory management rules. Use this skill whenever the user asks about playing sounds, background music, audio effects, looping audio, 3D positional sound, sound pools, microphone input, recording audio, raw WAV loading, InstancePlayLimitException, or any audio-related implementation in

installations
2
GitHub Stars
2
Mis à jour
22 sept.
alca259
Communauté

monogame-camera-modes

MonoGame 3D camera mode patterns — Fixed, Fixed Tracking, First Person, Third Person (with spring), Top-Down Fixed, and Top-Down Tracked. Use this skill whenever the user asks about camera modes, first person camera, third person camera, top-down camera, tracking camera, camera spring, follow camera, or how to implement a specific camera behaviour in a 3D game — even if they just say "I want the camera to follow the player" or "how do I make a first person / third person / top-down view".

installations
2
GitHub Stars
2
Mis à jour
22 sept.
alca259
Communauté

monogame-ecs

MonoGame component system (ECS) guide using GameBehaviour / GameEntity / GameWorld. Use this skill whenever the user mentions entities, components, behaviours, game objects, spawning/destroying actors, querying entities by type or interface, or asks how to structure game logic in MonoGame without deep class inheritance.

installations
2
GitHub Stars
2
Mis à jour
22 sept.
alca259
Communauté

monogame-math

MonoGame math implementation guide covering Vector2/3/4, Matrix, Rectangle, collision detection, camera transforms, and MathHelper utilities. Use this skill whenever the user asks about vectors, positions, distances, rotations, scaling, collision detection, AABB, bounding volumes, 2D camera math, coordinate transforms, or any math-related implementation in MonoGame.

installations
2
GitHub Stars
2
Mis à jour
22 sept.