Introduction
VTF, the Valve Texture Format, is the texture format of Valve’s Source engine games.
A texture can carry mipmaps, animation frames, the faces of a cube map, the slices of a
volume and a small low-resolution image. A model or a surface does not name the texture
itself: it names a material, a text file with the extension .vmt, which
names the textures and says how they are drawn. This page describes both for the PC
files, versions 7.0 to 7.4 of Valve’s header, and the header of the Xbox 360 files, from
the Source SDK 2013.
Drop a .vtf file on the page to see its header, resources and images.
Header
All numbers are little-endian. The offsets are from the start of the file.
| Offset | Type | Field |
|---|---|---|
| 0 | 4 bytes | signature VTF and a zero byte |
| 4 | 2 × int32 | version: major (7) and minor |
| 12 | int32 | header size; in versions before 7.3 the images follow it |
| 16 | 2 × uint16 | width and height of the largest mipmap |
| 20 | uint32 | flags |
| 24 | 2 × uint16 | number of frames and first frame |
| 32 | 3 × float | reflectivity, after 4 bytes of padding |
| 48 | float | bump map scale, after 4 bytes of padding |
| 52 | int32 | image format |
| 56 | uint8 | number of mipmaps |
| 57 | int32 | format of the low-resolution image, −1 for none |
| 61 | 2 × uint8 | width and height of the low-resolution image |
| 63 | uint16 | from 7.2: depth, the number of slices of a volume |
| 68 | uint32 | from 7.3: number of resources, at most 32, after 3 bytes of padding |
| 80 | 8 bytes each | from 7.3: the resource entries, after 8 bytes of padding |
Flags
| Bit | Meaning |
|---|---|
| 0x1, 0x2, 0x10 | point sampling, trilinear, anisotropic |
| 0x4, 0x8, 0x2000000 | clamp S, clamp T, clamp U |
| 0x20, 0x40, 0x80 | DXT5 hint, sRGB, normal map |
| 0x100, 0x200, 0x400 | no mipmaps, no level of detail, all mipmaps |
| 0x800 | procedural |
| 0x1000, 0x2000 | one-bit alpha, eight-bit alpha |
| 0x4000 | environment (cube) map |
| 0x8000, 0x10000, 0x800000 | render target, depth render target, no depth buffer |
| 0x20000, 0x40000, 0x80000, 0x100000, 0x200000 | no debug override, single copy, staging memory, immediate cleanup, ignore picmip |
| 0x4000000, 0x8000000 | vertex texture, self-shadowing bump map |
| 0x20000000, 0x40000000, 0x80000000 | border, streamable (coarse), streamable (fine) |
Resources
From version 7.3 each entry is three identifier bytes, a byte of resource flags and a 32-bit value. The value is the offset of the resource’s data, or, when the flag 0x02 is set, the datum itself.
| Identifier | Resource |
|---|---|
| 01 00 00 | low-resolution image |
| 30 00 00 | image: all mipmaps, frames, faces and slices |
| 10 00 00 | particle sheet |
LOD | level-of-detail clamp |
TS0 | extended flags |
CRC | checksum |
STR | streaming settings |
The two image resources are the bare images. Every other resource that has data starts with a 32-bit length.
Four resources have a value of four bytes, held in the entry when the flag 0x02 is set, or after the length word:
| Resource | Bytes |
|---|---|
LOD | width clamp, height clamp, width clamp on Xbox 360, height clamp on Xbox 360. A clamp n keeps the side at 2n pixels or less at the highest detail setting (picmip 0), and half that for each setting below |
TS0 | four flag bytes; the SDK names only 0x01 of the first, as unused, and sets the other three to zero |
CRC | one 32-bit checksum |
STR | first mipmap available, last mipmap available, two reserved bytes |
Order of the images
Before 7.3 the low-resolution image follows the header and the images follow it. The mipmaps are stored smallest first. Inside a mipmap come the frames, inside a frame the faces, and inside a face the slices. Each side of mipmap n is the full side halved n times, never less than 1.
A cube map (flag 0x4000) has the faces right (rt), left
(lf), back or +y (bk), front or −y (ft), up and
down (dn), and may have a seventh, the sphere map (sph).
Xbox 360 files
The Xbox 360 header has the version 0x360.8 and is big-endian. Its structures are packed without padding.
| Offset | Type | Field |
|---|---|---|
| 0 | 4 bytes | type string. The SDK’s comment gives VTF and a zero byte; the independent library SourcePP names these files VTFX |
| 4 | 2 × int32 | version: 0x360 and 8 |
| 12 | int32 | header size |
| 16 | uint32 | flags |
| 20 | 3 × uint16 | width, height and depth of the data in the file |
| 26 | uint16 | number of frames |
| 28 | uint16 | size of the preload data, the part loaded first, which may reach into the images |
| 30 | uint8 | mipmaps skipped; the full size is each side doubled that many times |
| 31 | uint8 | number of resources |
| 32 | 3 × float | reflectivity |
| 44 | float | bump map scale |
| 48 | int32 | image format, in the Xbox 360 numbering |
| 52 | 4 bytes | a sample that takes the place of the low-resolution image |
| 56 | uint32 | compressed size |
| 60 | 8 bytes each | the resource entries |
In the Xbox 360 numbering the formats up to ATI1N (38) are those below. After them come X360_DST16 (39), X360_DST24, X360_DST24F, LINEAR_BGRX8888 (42), LINEAR_RGBA8888, LINEAR_ABGR8888, LINEAR_ARGB8888, LINEAR_BGRA8888, LINEAR_RGB888, LINEAR_BGR888, LINEAR_BGRX5551, LINEAR_I8, LINEAR_RGBA16161616, LE_BGRX8888, LE_BGRA8888, DXT1_RUNTIME (54) and DXT5_RUNTIME (55).
Compressed data starts with the engine’s LZMA header, 17 bytes: the characters
LZMA, the unpacked size and the packed size (both 32-bit and always
little-endian), and five bytes of LZMA properties. The SDK publishes neither the layout
of the Xbox 360 image data nor that of the data once unpacked.
Image formats
The names are those of Valve’s imageformat.h. The letters of a byte format
give the order of the bytes. The 16-bit formats with separate blue, green and red fields
are described there from the lowest bit: BGR565 is blue 5, green 6, red 5; BGRA5551 is
blue 5, green 5, red 5, alpha 1; BGRA4444 four bits each in the order blue, green, red,
alpha. DXT formats are blocks of 4 by 4 pixels (BC1 to BC3). A P8 pixel is a number
into a palette; the header has no palette and the SDK defines none in the file. The SDK
gives no byte size for the depth and special formats 30 to 36, and calls NV_NULL a dummy
format that takes no video memory.
| Number | Format | Size |
|---|---|---|
| 0, 1 | RGBA8888, ABGR8888 | 4 bytes a pixel |
| 2, 3 | RGB888, BGR888 | 3 bytes |
| 4 | RGB565 | 2 bytes |
| 5, 6, 7, 8 | I8, IA88, P8 (palette numbers), A8 | 1, 2, 1, 1 bytes |
| 9, 10 | RGB888 and BGR888 with pure blue meaning transparent | 3 bytes |
| 11, 12, 16 | ARGB8888, BGRA8888, BGRX8888 | 4 bytes |
| 13, 14, 15, 20 | DXT1, DXT3, DXT5, DXT1 with one-bit alpha | 8, 16, 16, 8 bytes a block |
| 17, 18, 19, 21 | BGR565, BGRX5551, BGRA4444, BGRA5551 | 2 bytes |
| 22, 23, 26 | UV88, UVWQ8888, UVLX8888 | 2, 4, 4 bytes |
| 24, 25 | RGBA16161616F, RGBA16161616 | 8 bytes |
| 27, 28, 29 | R32F, RGB323232F, RGBA32323232F | 4, 12, 16 bytes |
| 30 to 36 | NV_DST16, NV_DST24, NV_INTZ, NV_RAWZ, ATI_DST16, ATI_DST24, NV_NULL | depth and special formats |
| 37, 38 | ATI2N, ATI1N | 16, 8 bytes a block |
| 39, 40 | DXT1_RUNTIME, DXT5_RUNTIME | 8, 16 bytes a block |
Materials (.vmt)
A material is KeyValues text: the name of a shader followed by a block in braces of
pairs of a key and a value, either of which may be quoted, and of nested blocks.
// starts a comment. A pair may end with a condition in square brackets,
such as [$X360] or [!$OSX]; on a Windows PC
$WIN32 and $WINDOWS are true. Keys do not depend on case.
$basetexture names the main texture, as a path under
materials/ without the extension .vtf.
Patch materials
A material of the shader patch has an include key naming
another material and a block insert, whose pairs are set whether or not
the included material has them, or replace, whose pairs change only keys it
already has. The included material may be a patch in turn; Valve’s compiler follows up
to 10. When a patch has both blocks, that compiler loses the replace
block.
Search by a model
A Source model holds names of materials and a
list of folders. For each name the folders are tried in order, and the first file
materials/ + folder + name + .vmt that loads is the material.
Transparency
In the SDK’s standard shaders $translucent, $vertexalpha or an
$alpha below 1 blend the surface; $alphatest instead cuts
away what lies below $alphatestreference, which the unlit shader declares
as 0.7. $additive adds the colour to what lies behind.
$selfillum and $basealphaenvmapmask give the alpha channel of
the base texture another use, and switch the alpha test off.
$blendtintbybasealpha does the same for blending.
$nocull draws both sides.
#include and #base
Both lines name another KeyValues file, taken from the folder of the file that holds the
line. The blocks of an #include file are added beside the file’s own first
block, so a material, which is the first block, does not change. A #base
file is merged into the first block: the file’s own pairs stand, pairs it lacks are
added, and blocks of the same name are merged in the same way. In this merge names are
compared with case. A named file that is not found is passed over.
Values
A value is a number, a vector in square brackets, such as [1 0.5 0], a
texture transform written center .5 .5 scale 1 1 rotate 0 translate 0 0,
or text. A texture transform is applied as: scale about the centre, rotate about it (in
degrees), then translate.
Colour, transform and frame
These apply to the standard shaders VertexLitGeneric and
UnlitGeneric.
| Key | Default | Meaning |
|---|---|---|
$color, $color2 | [1 1 1] | multiplied together to tint the surface; values up to 1 are turned into linear light, larger ones used as they are |
$alpha | 1 | opacity |
$basetexturetransform | none | texture transform of the base texture |
$frame | 0 | animation frame of the base texture |
$blendtintbybasealpha | 0 | the tint acts only as far as the base alpha says; $tintreplacesbasecolor moves from tinting towards replacing the colour |
Detail texture
| Key | Default | Meaning |
|---|---|---|
$detail | the texture | |
$detailscale | 4 | one number or two; multiplies the coordinates |
$detailtexturetransform | that of the base texture | transform used before the scale |
$detailframe | 0 | its animation frame |
$detailtint | [1 1 1] | multiplies its colour |
$detailblendfactor | 1 | strength, f below |
$detailblendmode | 0 | how it is combined |
With base b and detail d in linear light (in mode 0 the detail value is read as stored) and mix(x, y, t) = x + (y − x)t:
| Mode | Result |
|---|---|
| 0 | colour × mix(1, 2d, f) |
| 1 | colour + f·d |
| 2 | colour mixed with d by f × detail alpha |
| 3 | colour and alpha mixed with d by f |
| 4 | colour mixed with d by f × (1 − base alpha); alpha from the detail |
| 5 | after lighting, f·d is added to the lit colour |
| 6 | after lighting, m·d + a held to 0 to 1 is added; m = 1/f and a = 1 − m when f is at least 0.5, else m = 4f and a = −m/2 |
| 7 | c = mix(detail red, detail alpha, base alpha); colour × mix(1, 2c, f) |
| 8 | colour and alpha mixed with their product with d by f |
| 9 | alpha mixed with alpha × detail alpha by f |
| 10 | modifies the lighting of a bump map |
| 11 | colour × 2 (red + green + blue of the detail) / 3 |
Self-illumination
With $selfillum a VertexLitGeneric surface is mixed from its lit
colour towards the base colour times the tint $selfillumtint, which is unlit,
by the base alpha, or by $selfillummask when one is named.
UnlitGeneric ignores it.
Proxies
A block Proxies holds one block for each proxy, run in the order written
to change variables as time passes. A number input may be written as a number, a variable
or one component of one ($v[1]).
| Proxy | Keys | Effect |
|---|---|---|
AnimatedTexture | animatedTextureVar, animatedTextureFrameNumVar, animatedTextureFrameRate (15), animationNoWrap | frame = whole part of rate × time, wrapping at the number of frames unless told not to |
AnimatedOffsetTexture | as above | the same, counted from a start time the entity gives |
TextureScroll | textureScrollVar, textureScrollRate (1), textureScrollAngle (0), textureScale (1) | offset = time × rate along the angle, kept from 0 to 1 |
TextureTransform | centerVar, scaleVar, rotateVar, translateVar, resultVar | builds a texture transform |
MatrixRotate | angle, axisVar (0 0 1), resultVar | builds a rotation by the angle in degrees about the axis, brought to length 1; an axis shorter than 0.001 stands for 0 0 1. A texture coordinate takes the first two rows, applied to (u, v, 0, 1) |
ToggleTexture | toggleTextureVar, toggleTextureFrameNumVar, toggleShouldWrap (1) | frame = the entity’s texture frame index, wrapped at the number of frames; without wrapping, an index past that number gives the number itself. Without an entity nothing changes |
CurrentTime | resultVar | the time in seconds |
Sine | sinePeriod (1), sineMin (0), sineMax (1), timeOffset (0) | a sine wave between the two values |
LinearRamp | rate (1), initialValue (0) | rate × time + initial value |
Add, Subtract, Multiply, Divide | srcVar1, srcVar2, resultVar | the sum and so on, on numbers or whole vectors |
Equals, Abs, Frac, Int | srcVar1, resultVar | copy, magnitude, fraction, whole part |
Clamp, WrapMinMax | min, max and minVal, maxVal | held within, or wrapped within, two limits |
Exponential | scale, offset, minVal, maxVal | scale × evalue + offset, held within the limits |
LessOrEqual | srcVar1, srcVar2, lessEqualVar, greaterVar | chooses one of the two last by comparing the two first |
SelectFirstIfNonZero | srcVar1, srcVar2 | the first unless it is zero |
Empty | nothing |
Other proxies read the state of the game or its entities, or draw random numbers.
The viewer
- It reads the header, the resource entries and where every surface lies, and shows the low-resolution image and any surface: mipmap, frame, face and slice. The first 16 full-size surfaces are on the Overview. 65,536 surfaces are listed at once; for a file with more, an option chooses the first one listed, so all can be reached.
- A surface of more than 67,108,864 pixels is not turned into a picture. Floating-point values are cut to the range 0 to 1, and 16-bit channels show their high byte.
- The values of the
LOD,TS0,CRCandSTRresources are read; other resources with data are shown as bytes. A P8 surface is shown as grey levels, one for each palette number, and is not used as a material texture. Of a depth or special format the image bytes are marked in the tree and not drawn. - Of an Xbox 360 file it reads the header, the resource entries and an LZMA header, and marks where each resource’s data starts. Its images are not unpacked or drawn.
- In the 3D view of a Source model, materials found beside the model are drawn with their
$basetexture,$color, texture transform, animation frames, detail texture (modes 0 to 9 and 11), self-illumination (also on additive materials), additive blending and the proxies listed above, which run on the view’s time line (30 seconds long when the model has no animation of its own). The layers are worked out on the texture, over the area of the mesh’s texture coordinates, or over one unit square of them, repeated, when every texture and transform repeats with it (sides then raised to powers of two); a coloured$selfillummaskis drawn channel by channel; the self-lit part, and what detail modes 5 and 6 add, are blended or added on the colours as shown, not in linear light; on an additive material the lit surface is darkened by the mask and the self-lit part added beside it. WithoutanimationNoWrap’s wrapping the view holds the last frame (the SDK’s code writes it only on the update that passes the end). A.vmtor.vtflarger than 256 MB is not read. The first frames of the textures a view loads come to at most 268,435,456 pixels (1 GB). A material’s detail texture and mask are loaded with it, so the material loaded first always has all its textures; later animation frames are decoded as the time line reaches them, and the last 134,217,728 pixels of them (512 MB) are kept. The view works layers out on textures of 134,217,728 pixels together (512 MB), or on one material alone up to 268,435,456 (1 GB); the option “Materials: start at material” chooses which materials come first, so each can be brought in. Where a material’s worked-out texture would be more than 16,384 pixels a side, or its layers more than 1 GB, it is made as a grid of pieces, each of at most 16,384 pixels a side and 512 MB, and the triangles are cut along the grid so that every part has the piece it lies on; nothing is scaled down. Pieces are taken like materials: up to 1 GB of one material’s pieces in a view, 512 MB when other materials are worked out with them. The pieces left out are drawn with the base texture alone, and the note over the view gives the values of “Materials: start at material” and “Materials: start at piece” that bring in the next ones. Where a proxy changes a texture’s scale over time, the pieces are sized for the finest scale found at tenths of a second over the 30 seconds, with a tenth to spare. The cutting is counted as the pieces that each triangle’s texture coordinates reach, summed over the triangles: at most 4,000,000 for a model, taken by the materials in the order of their numbers from the chosen start. A material left without is drawn without its layers, and the view says which number gives it them first. A material that alone reaches more has its layers on as many triangles as fit, from the one chosen with “Materials: start at triangle”, the others with the base texture alone, and the view says which number brings in the next ones. A single triangle that alone reaches more than 4,000,000 pieces has the base texture alone.A view needs graphics hardware that takes textures of the size asked for. In an opened folder the materials of a model’s names are looked for until 4,194,304 paths (names times search folders) have been tried. A file not found under its whole path is looked for by shorter tails of it, so a file of the same name elsewhere can be taken; the table “Material files” names what was used. - Two readings can be chosen at the top of the window when material files are present: how a patch with both blocks is read, and the alpha test reference of a material that gives none (0.7 or 0.5). The option “Entity texture frame” (0) gives the index that
ToggleTexturereads; where the proxy asks for a frame the texture does not have, the first frame is shown.
Some readings are the viewer’s own, where the SDK publishes no code: whether a cube map
has six or seven faces is decided from the size of the image data; RGB565 is taken as
BGR565 with red and blue exchanged; ATI1N and ATI2N are read as BC4 and BC5; P8
numbers are shown as grey; an Xbox 360 file is recognised by the version, with
VTF and a zero byte or VTFX before it, and its resource data
is taken to run to the next resource or the end of the file; and a version 7.5 file, a
number the SDK’s header does not reach, is read like 7.4.
Not described
Known, not yet covered
- Reference: the image data of the Xbox 360 files, and the PlayStation 3 files, both of which the MIT-licensed library SourcePP reads; the keys of the individual shaders beyond those above; the proxies that depend on the game.
- Viewer: the images of Xbox 360 files; PlayStation 3 files; the images of depth and special formats.
- Viewer: of materials, the bump, environment-map and phong layers; detail mode 10; proxies other than those listed; shader fallback blocks and colours written in braces.
Unknown
- Reference: what version 7.5 changes; the contents of the particle sheet resource, whose class the SDK names but does not define; where the palette of a P8 texture comes from; the byte sizes of the depth and special formats.
- Reference: the rules of the unpublished material system: when it counts a texture as translucent, which alpha test reference it uses for a lit material that gives none, how it chooses shader fallback blocks, how it reads colours written in braces and texture-transform text, and whether a variable holds a whole or a floating number.
Sources
Valve’s Source SDK 2013
(read for facts): public/vtf/vtf.h,
public/bitmap/imageformat.h, utils/vtf2tga/vtf2tga.cpp,
public/tier1/lzmaDecoder.h, public/particles/particles.h,
tier1/KeyValues.cpp, utils/vbsp/materialpatch.cpp,
utils/vrad/vradstaticprops.cpp and, in
materialsystem/stdshaders, BaseVSShader.cpp,
vertexlitgeneric_dx9.cpp, vertexlitgeneric_dx9_helper.cpp,
vertexlit_and_unlit_generic_ps2x.fxc, common_ps_fxc.h and
unlitgeneric_dx9.cpp; the proxies in game/client
(functionproxy.cpp, mathproxy.cpp, matrixproxy.cpp,
texturescrollmaterialproxy.cpp, baseanimatedtextureproxy.cpp,
animatedtextureproxy.cpp, animatedoffsettextureproxy.cpp,
timematerialproxy.cpp, toggletextureproxy.cpp) and
mathlib/vmatrix.cpp.
The name VTFX and the existence of the PlayStation 3 files are from the description of SourcePP.
Links
Software
- VTFEdit – graphical viewer and creator of VTF textures built on VTFLib (C++ .NET)
Developers
- VTFLib – LGPL library for reading and writing VTF and VMT files (C and C++)
- galaco/vtf – VTF parser (Go)
- source-vtf – reader for PC VTF 7.0 to 7.5 (Rust)
Reference
- galaco/vtf header types – header fields by version