Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sr2echa/TF2-Source-Code/llms.txt

Use this file to discover all available pages before exploring further.

The material system (materialsystem/) is the abstraction layer between game rendering code and the GPU. All surfaces in TF2 — world faces, models, particles, HUD elements — reference a .vmt (Valve Material Type) file that defines which shader to use and what parameters to pass. The IMaterialSystem interface (public/materialsystem/imaterialsystem.h) is the primary API used by the engine and game code.

IMaterialSystem interface

IMaterialSystem is exposed as a factory interface with the name MATERIAL_SYSTEM_INTERFACE_VERSION. It is the entry point for creating materials, render contexts, and textures.
// public/materialsystem/imaterialsystem.h
abstract_class IMaterialSystem : public IAppSystem
{
public:
    // Material access
    virtual IMaterial *FindMaterial( char const *pMaterialName,
        const char *pTextureGroupName,
        bool complain = true,
        const char *pComplainPrefix = NULL ) = 0;

    // Render context
    virtual IMatRenderContext *GetRenderContext() = 0;
    virtual IMatRenderContext *CreateRenderContext( MaterialContextType_t type ) = 0;

    // Texture creation
    virtual ITexture *CreateNamedRenderTargetTextureEx( const char *pRTName,
        int w, int h, RenderTargetSizeMode_t sizeMode,
        ImageFormat format, MaterialRenderTargetDepth_t depth = MATERIAL_RT_DEPTH_SHARED,
        unsigned int textureFlags = TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT,
        unsigned int renderTargetFlags = 0 ) = 0;
};

VMT material files

VMT files are KeyValues text files that specify the shader and its parameters. TF2 ships with hundreds of shaders in materialsystem/stdshaders/.
VertexLitGeneric example (tf_red_base.vmt)
"VertexLitGeneric"
{
    "$basetexture"          "models/player/scout/scout"
    "$bumpmap"              "models/player/scout/scout_normal"
    "$envmap"               "env_cubemap"
    "$envmaptint"           "[0.3 0.3 0.3]"
    "$rimlight"             "1"
    "$rimlightexponent"     "4"
    "$rimlightboost"        "1"
    "$phong"                "1"
    "$phongexponent"        "50"
}
Common TF2 shaders:
ShaderUsage
VertexLitGenericWorld props, player models, weapons
LightmappedGenericBSP world surfaces with lightmaps
UnlitGenericHUD elements, skybox faces
WorldVertexTransitionBlended ground materials
SpriteParticle sprites, lens flares
CableRope and wire geometry

VTF texture format

Valve Texture Format (.vtf) files are the native texture format. The IVTFTexture interface and VTF file format are defined in public/vtf/vtf.h. Key flags:
// public/vtf/vtf.h (selected flags)
enum CompiledVtfFlags
{
    TEXTUREFLAGS_POINTSAMPLE        = 0x00000001,
    TEXTUREFLAGS_TRILINEAR          = 0x00000002,
    TEXTUREFLAGS_CLAMPS             = 0x00000004,
    TEXTUREFLAGS_CLAMPT             = 0x00000008,
    TEXTUREFLAGS_NOMIP              = 0x00000100,
    TEXTUREFLAGS_NOLOD              = 0x00000200,
    TEXTUREFLAGS_MINMIP             = 0x00000400,
    TEXTUREFLAGS_PROCEDURAL         = 0x00000800,
    TEXTUREFLAGS_SRGB               = 0x00040000,
    TEXTUREFLAGS_NODEBUGOVERRIDE    = 0x00080000,
};

Render context and draw calls

The IMatRenderContext interface manages GPU state for a sequence of draw calls. Game code obtains a render context, sets material state, and issues mesh draws:
// Typical render context usage pattern
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->Bind( pMaterial );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
CMeshBuilder meshBuilder;
meshBuilder.Begin( pMesh, MATERIAL_TRIANGLES, nVerts, nIndices );
// ... fill vertices ...
meshBuilder.End();
pMesh->Draw();

Texture compositor

CTextureCompositor (materialsystem/ctexturecompositor.cpp) assembles composite textures at runtime — used for team-colored cosmetics and painted item wear overlays in TF2.
The queued render context (CMatQueuedRenderContext) allows render commands to be recorded on a worker thread and replayed on the render thread, enabling multi-threaded rendering for better CPU utilization.

Build docs developers (and LLMs) love