Microsoft XNA Game Studio 4.0
Compiled (XNB) Content Format
Contents
Byte |
Format identifier |
‘X’ |
Byte |
Format identifier |
‘N’ |
Byte |
Format identifier |
‘B’ |
Byte |
Target platform |
‘w’ = Microsoft Windows ‘m’ = Windows Phone 7 ‘x’ = Xbox 360 |
Byte |
XNB format version |
5 = XNA Game Studio 4.0 |
Byte |
Flag bits |
Bit 0x01 = content is for HiDef profile (otherwise Reach) Bit 0x80 = asset data is compressed |
UInt32 |
Compressed file size |
Total size of the (optionally compressed) .xnb file as stored on disk (including this header block) |
UInt32 |
Decompressed data size |
Only included for compressed .xnb files, where it indicates the expanded size of the compressed data which starts immediately after this field (unlike the compressed file size, this does not include the uncompressed portion of the header) |
If the file is compressed (flag bit 0x80 is set), data from this point on is packed using the Xbox XMemCompress API | ||
7BitEncodedInt |
Type reader count |
|
Repeat <type reader count> { | ||
String |
Type reader name |
.NET assembly qualified name of a ContentTypeReader<T> subclass, which can be used to read one of the types contained in this file |
Int32 |
Reader version number |
Type specific version number (typically zero) |
} | ||
7BitEncodedInt |
Shared resource count |
|
Object |
Primary asset data |
The primary object which will be returned when the .xnb file is loaded |
Repeat <shared resource count> | ||
Object |
Shared resource data |
Used to break cyclic data structures (see below) |
} |
Individual objects can be serialized in one of three ways:
Indicated in this documented by a raw type name such as “Byte”, “String”, or “VertexBuffer”.
This is simply a dump of whatever data is expected by the type reader for the specified type.
Because it does not include any type metadata, the type must be statically specified. It is not possible to serialize polymorphic instances this way, and the value cannot be null.
Indicated in this document by the type name “Object”, or constrained to a specific subtype, eg. “Object: String” or “Object: VertexBuffer”.
This format starts with a 7BitEncodedInt typeId, which indicates the type of the object:
If typeId is zero, the object is null.
If greater than zero, (typeId - 1) is an index into the type reader table from the XNB header. Nonzero ids are followed by the object data, in the same format as a raw value for the selected type reader.
The presence of type metadata enables serialization of polymorphic class hierarchies. For instance where the format specifies a field of type “Object: Effect”, any individual .xnb file could choose to substitute an instance of type BasicEffect or SkinnedEffect.
As shorthand, this document uses the notation “Object? T” to refer to fields that will be serialized as raw values if T is a value type, or as polymorphic objects if T is a reference type.
Indicated in this document by a type name such as “Shared Resource: VertexBuffer”.
These values are serialized as a 7BitEncodedInt resourceId:
If resourceId is zero, the object is null.
If resourceId is greater than zero, (resourceId - 1) is an index into the list of shared resource instances, which are serialized at the end of the .xnb file (after the primary asset data).
Because the instance data is referred to by index rather than directly by value, this mechanism allows cyclic data structures and arbitrary graphs to be serialized, breaking the infinite recursion that would otherwise occur. Shared resource instances may be referenced from the primary asset data, and also from the body of any shared resource (including the resource itself). Resolving these references requires a fixup pass at the end of the main load operation.
This format matches the .NET BinaryReader.Read7BitEncodedInt method. It is a variable size encoding of a 32 bit integer value. C implementation:
int Read7BitEncodedInt()
{
int result = 0;
int bitsRead = 0;
int value;
do
{
value = ReadByte();
result |= (value & 0x7f) << bitsRead;
bitsRead += 7;
}
while (value & 0x80);
return result;
}
Target type |
Type reader name |
Format |
System.Byte |
Microsoft.Xna.Framework.Content.ByteReader |
Unsigned byte |
System.SByte |
Microsoft.Xna.Framework.Content.SByteReader |
Signed byte |
System.Int16 |
Microsoft.Xna.Framework.Content.Int16Reader |
Little endian signed 16 bit int |
System.UInt16 |
Microsoft.Xna.Framework.Content.UInt16Reader |
Little endian unsigned 16 bit int |
System.Int32 |
Microsoft.Xna.Framework.Content.Int32Reader |
Little endian signed 32 bit int |
System.UInt32 |
Microsoft.Xna.Framework.Content.UInt32Reader |
Little endian unsigned 32 bit int |
System.Int64 |
Microsoft.Xna.Framework.Content.Int64Reader |
Little endian signed 64 bit int |
System.UInt64 |
Microsoft.Xna.Framework.Content.UInt64Reader |
Little endian unsigned 64 bit int |
System.Single |
Microsoft.Xna.Framework.Content.SingleReader |
Little endian 32 bit IEEE float |
System.Double |
Microsoft.Xna.Framework.Content.DoubleReader |
Little endian 64 bit IEEE double |
System.Boolean |
Microsoft.Xna.Framework.Content.BooleanReader |
One byte, 0 = false, 1 = true |
System.Char |
Microsoft.Xna.Framework.Content.CharReader |
Single UTF8 encoded character |
System.String |
Microsoft.Xna.Framework.Content.StringReader |
7BitEncodedInt byte count, followed by UTF8 encoded string |
System.Object |
Microsoft.Xna.Framework.Content.ObjectReader |
Never directly invoked, but sometimes referenced in .xnb headers as a base reader for polymorphic values |
Target type: T
Type reader name: Microsoft.Xna.Framework.Content.EnumReader`1[[T]]
T is any enum type
T |
Enum value |
Usually 32 bit, but can be other sizes if T is not integer |
Target type: System.Nullable<T>
Type reader name: Microsoft.Xna.Framework.Content.NullableReader`1[[T]]
T is any value type
Boolean |
Has value |
|
T |
Value |
Only included when has value == true |
Target type: T[]
Type reader name: Microsoft.Xna.Framework.Content.ArrayReader`1[[T]]
UInt32 |
Count |
Repeat <count> { | |
Object? T |
Element value |
} |
Target type: System.Collections.Generic.List<T>
Type reader name: Microsoft.Xna.Framework.Content.ListReader`1[[T]]
UInt32 |
Count |
Repeat <count> { | |
Object? T |
Element value |
} |
Target type: System.Collections.Generic.Dictionary<K,V>
Type reader name: Microsoft.Xna.Framework.Content.DictionaryReader`2[[K],[V]]
UInt32 |
Count |
Repeat <count> { | |
Object? K |
Item key |
Object? V |
Item value |
} |
Target type: System.TimeSpan
Type reader name: Microsoft.Xna.Framework.Content.TimeSpanReader
Int64 |
Tick count |
10000000 ticks per second |
Target type: System.DateTime
Type reader name: Microsoft.Xna.Framework.Content.DateTimeReader
UInt64 |
Packed value |
Low 62 bits hold a .NET DateTime tick count High 2 bits hold a .NET DateTimeKind enum value |
Target type: System.Decimal
Type reader name: Microsoft.Xna.Framework.Content.DecimalReader
UInt32 |
Packed value 1 |
.NET System.Decimal bit pattern |
UInt32 |
Packed value 2 |
|
UInt32 |
Packed value 3 |
|
UInt32 |
Packed value 4 |
|
Target type: T
Type reader name: Microsoft.Xna.Framework.Content.ExternalReferenceReader
String |
Asset name |
Filename (relative to the current .xnb file, and not including the .xnb file extension) pointing to a separate .xnb file which contains an object of type T, the contents of which should be inserted at the current location into the file currently being read. If the string is empty, the resulting object is null. |
Target type: Microsoft.Xna.Framework.Vector2
Type reader name: Microsoft.Xna.Framework.Content.Vector2Reader
Single |
X |
Single |
Y |
Target type: Microsoft.Xna.Framework.Vector3
Type reader name: Microsoft.Xna.Framework.Content.Vector3Reader
Single |
X |
Single |
Y |
Single |
Z |
Target type: Microsoft.Xna.Framework.Vector4
Type reader name: Microsoft.Xna.Framework.Content.Vector4Reader
Single |
X |
Single |
Y |
Single |
Z |
Single |
W |
Target type: Microsoft.Xna.Framework.Matrix
Type reader name: Microsoft.Xna.Framework.Content.MatrixReader
Single |
M11 |
16 floats make up a 4x4 row major matrix |
Single |
M12 |
|
Single |
M13 |
|
Single |
M14 |
|
Single |
M21 |
|
Single |
M22 |
|
Single |
M23 |
|
Single |
M24 |
|
Single |
M31 |
|
Single |
M32 |
|
Single |
M33 |
|
Single |
M34 |
|
Single |
M41 |
|
Single |
M42 |
|
Single |
M43 |
|
Single |
M44 |
|
Target type: Microsoft.Xna.Framework.Quaternion
Type reader name: Microsoft.Xna.Framework.Content.QuaternionReader
Single |
X |
Single |
Y |
Single |
Z |
Single |
W |
Target type: Microsoft.Xna.Framework.Color
Type reader name: Microsoft.Xna.Framework.Content.ColorReader
Byte |
Red |
Byte |
Green |
Byte |
Blue |
Byte |
Alpha |
Target type: Microsoft.Xna.Framework.Plane
Type reader name: Microsoft.Xna.Framework.Content.PlaneReader
Vector3 |
Normal |
Single |
D |
Target type: Microsoft.Xna.Framework.Point
Type reader name: Microsoft.Xna.Framework.Content.PointReader
Int32 |
X |
Int32 |
Y |
Target type: Microsoft.Xna.Framework.Rectangle
Type reader name: Microsoft.Xna.Framework.Content.RectangleReader
Int32 |
X |
Int32 |
Y |
Int32 |
Width |
Int32 |
Height |
Target type: Microsoft.Xna.Framework.BoundingBox
Type reader name: Microsoft.Xna.Framework.Content.BoundingBoxReader
Vector3 |
Min |
Vector3 |
Max |
Target type: Microsoft.Xna.Framework.BoundingSphere
Type reader name: Microsoft.Xna.Framework.Content.BoundingSphereReader
Vector3 |
Center |
Single |
Radius |
Target type: Microsoft.Xna.Framework.BoundingFrustum
Type reader name: Microsoft.Xna.Framework.Content.BoundingFrustumReader
Matrix |
Frustum matrix |
Target type: Microsoft.Xna.Framework.Ray
Type reader name: Microsoft.Xna.Framework.Content.RayReader
Vector3 |
Position |
Vector3 |
Direction |
Target type: Microsoft.Xna.Framework.Curve
Type reader name: Microsoft.Xna.Framework.Content.CurveReader
Int32 |
Pre loop |
0 = constant 1 = cycle 2 = cycle offset 3 = oscillate 4 = linear |
Int32 |
Post loop |
Same values as pre loop |
UInt32 |
Key count |
|
Repeat <key count> { | ||
Single |
Position |
|
Single |
Value |
|
Single |
Tangent in |
|
Single |
Tangent out |
|
Int32 |
Continuity |
0 = smooth 1 = step |
} |
Target type: Microsoft.Xna.Framework.Graphics.Texture
Type reader name: Microsoft.Xna.Framework.Content.TextureReader
Never directly invoked (as Texture is an abstract base type), but sometimes referenced in .xnb headers as a base reader for polymorphic values.
Target type: Microsoft.Xna.Framework.Graphics.Texture2D
Type reader name: Microsoft.Xna.Framework.Content.Texture2DReader
Int32 |
Surface format |
0 = Color 1 = Bgr565 2 = Bgra5551 3 = Bgra4444 4 = Dxt1 5 = Dxt3 6 = Dxt5 7 = NormalizedByte2 8 = NormalizedByte4 9 = Rgba1010102 10 = Rg32 11 = Rgba64 12 = Alpha8 13 = Single 14 = Vector2 15 = Vector4 16 = HalfSingle 17 = HalfVector2 18 = HalfVector4 19 = HdrBlendable |
UInt32 |
Width |
|
UInt32 |
Height |
|
UInt32 |
Mip count |
|
Repeat <mip count> { | ||
UInt32 |
Data size |
|
Byte [data size] |
Image data |
|
} |
Target type: Microsoft.Xna.Framework.Graphics.Texture3D
Type reader name: Microsoft.Xna.Framework.Content.Texture3DReader
Int32 |
Surface format |
See Texture2D for list of enum values |
UInt32 |
Width |
|
UInt32 |
Height |
|
UInt32 |
Depth |
|
UInt32 |
Mip count |
|
Repeat <mip count> { | ||
UInt32 |
Data size |
|
Byte [data size] |
Image data |
|
} |
Target type: Microsoft.Xna.Framework.Graphics.TextureCube
Type reader name: Microsoft.Xna.Framework.Content.TextureCubeReader
Int32 |
Surface format |
See Texture2D for list of enum values |
UInt32 |
Size |
|
UInt32 |
Mip count |
|
Repeat <6x, for each cube face: +x, -x, +y, -y, +z, -z> { Repeat <mip count> { | ||
UInt32 |
Data size |
|
Byte [data size] |
Image data |
|
} } |
Target type: Microsoft.Xna.Framework.Graphics.IndexBuffer
Type reader name: Microsoft.Xna.Framework.Content.IndexBufferReader
Boolean |
Is 16 bit |
If false, index values are 32 bits in size |
UInt32 |
Data size |
|
Byte [data size] |
Index data |
|
Target type: Microsoft.Xna.Framework.Graphics.VertexBuffer
Type reader name: Microsoft.Xna.Framework.Content.VertexBufferReader
VertexDeclaration |
Vertex declaration |
UInt32 |
Vertex count |
Byte [vertex count * declaration.VertexStride] |
Vertex data |
Target type: Microsoft.Xna.Framework.Graphics.VertexDeclaration
Type reader name: Microsoft.Xna.Framework.Content.VertexDeclarationReader
UInt32 |
Vertex stride |
|
UInt32 |
Element count |
|
Repeat <element count> { | ||
UInt32 |
Offset |
|
Int32 |
Element format |
0 = Single 1 = Vector2 2 = Vector3 3 = Vector4 4 = Color 5 = Byte4 6 = Short2 7 = Short4 8 = NormalizedShort2 9 = NormalizedShort4 10 = HalfVector2 11 = HalfVector4 |
Int32 |
Element usage |
0 = Position 1 = Color 2 = TextureCoordinate 3 = Normal 4 = Binormal 5 = Tangent 6 = BlendIndices 7 = BlendWeight 8 = Depth 9 = Fog 10 = PointSize 11 = Sample 12 = TessellateFactor |
UInt32 |
Usage index |
|
} |
Target type: Microsoft.Xna.Framework.Graphics.Effect
Type reader name: Microsoft.Xna.Framework.Content.EffectReader
UInt32 |
Size |
|
Byte [size] |
Effect bytecode |
A compiled XNA effect |
Target type: Microsoft.Xna.Framework.Graphics.EffectMaterial
Type reader name: Microsoft.Xna.Framework.Content.EffectMaterialReader
ExternalReference |
Effect |
Pointer to a separate .xnb file which contains an object of type Effect |
Object: Dictionary<String, Object> |
Parameters |
Named parameter values. Supported types:
|
Target type: Microsoft.Xna.Framework.Graphics.BasicEffect
Type reader name: Microsoft.Xna.Framework.Content.BasicEffectReader
ExternalReference |
Texture |
Pointer to a separate .xnb file which contains an object of type Texture2D |
Vector3 |
Diffuse color |
|
Vector3 |
Emissive color |
|
Vector3 |
Specular color |
|
Single |
Specular power |
|
Single |
Alpha |
|
Boolean |
Vertex color enabled |
|
Target type: Microsoft.Xna.Framework.Graphics.AlphaTestEffect
Type reader name: Microsoft.Xna.Framework.Content.AlphaTestEffectReader
ExternalReference |
Texture |
Pointer to a separate .xnb file which contains an object of type Texture2D |
Int32 |
Compare function |
0 = Always 1 = Never 2 = Less 3 = LessEqual 4 = Equal 5 = GreaterEqual 6 = Greater 7 = NotEqual |
UInt32 |
Reference alpha |
|
Vector3 |
Diffuse color |
|
Single |
Alpha |
|
Boolean |
Vertex color enabled |
|
Target type: Microsoft.Xna.Framework.Graphics.DualTextureEffect
Type reader name: Microsoft.Xna.Framework.Content.DualTextureEffectReader
ExternalReference |
Texture 1 |
Pointer to a separate .xnb file which contains an object of type Texture2D |
ExternalReference |
Texture 2 |
Pointer to a separate .xnb file which contains an object of type Texture2D |
Vector3 |
Diffuse color |
|
Single |
Alpha |
|
Boolean |
Vertex color enabled |
|
Target type: Microsoft.Xna.Framework.Graphics.EnvironmentMapEffect
Type reader name: Microsoft.Xna.Framework.Content.EnvironmentMapEffectReader
ExternalReference |
Texture |
Pointer to a separate .xnb file which contains an object of type Texture2D |
ExternalReference |
Environment map |
Pointer to a separate .xnb file which contains an object of type TextureCube |
Single |
Env map amount |
|
Vector3 |
Env map specular |
|
Single |
Fresnel factor |
|
Vector3 |
Diffuse color |
|
Vector3 |
Emissive color |
|
Single |
Alpha |
|
Target type: Microsoft.Xna.Framework.Graphics.SkinnedEffect
Type reader name: Microsoft.Xna.Framework.Content.SkinnedEffectReader
ExternalReference |
Texture |
Pointer to a separate .xnb file which contains an object of type Texture2D |
UInt32 |
Weights per vertex |
1, 2, or 4 |
Vector3 |
Diffuse color |
|
Vector3 |
Emissive color |
|
Vector3 |
Specular color |
|
Single |
Specular power |
|
Single |
Alpha |
|
Target type: Microsoft.Xna.Framework.Graphics.SpriteFont
Type reader name: Microsoft.Xna.Framework.Content.SpriteFontReader
Object: Texture2D |
Texture |
Texture containing multiple packed glyph images |
Object: List<Rectangle> |
Glyphs |
Location of each glyph image within the texture |
Object: List<Rectangle> |
Cropping |
Blank border space that was cropped from glyph images prior to packing (to save space), but which should still be respected when laying out text for rendering |
Object: List<Char> |
Character map |
Unicode character code points corresponding to each rectangle in the glyph and cropping list (guaranteed sorted into ascending order) |
Int32 |
Vertical line spacing |
|
Single |
Horizontal spacing |
|
Object: List<Vector3> |
Kerning |
ABC character layout data for each glyph |
Nullable<Char> |
Default character |
|
Target type: Microsoft.Xna.Framework.Graphics.Model
Type reader name: Microsoft.Xna.Framework.Content.ModelReader
UInt32 |
Bone count |
Repeat <bone count> { | |
Object: String |
Bone name |
Matrix |
Bone transform |
} | |
Repeat <bone count> { | |
BoneReference (see below) |
Parent bone |
UInt32 |
Child bone count |
Repeat <child bone count> { | |
BoneReference |
Child bone |
} | |
} | |
UInt32 |
Mesh count |
Repeat <mesh count> { | |
Object: String |
Mesh name |
BoneReference |
Mesh parent bone |
BoundingSphere |
Mesh bounds |
Object |
Mesh tag |
UInt32 |
Mesh part count |
Repeat <mesh part count> { | |
UInt32 |
Vertex offset |
UInt32 |
Num vertices |
UInt32 |
Start index |
UInt32 |
Primitive count |
Object |
Mesh part tag |
Shared resource: VertexBuffer |
Vertex buffer |
Shared resource: IndexBuffer |
Index buffer |
Shared resource: Effect |
Effect |
} | |
} | |
BoneReference |
Model root bone |
Object |
Model tag |
The BoneReference type varies in size depending on the number of bones in the model. If bone count is less than 255 this value is serialized as a Byte, otherwise it is UInt32. If the reference value is zero the bone is null, otherwise (bone reference - 1) is an index into the model bone list.
Target type: Microsoft.Xna.Framework.Audio.SoundEffect
Type reader name: Microsoft.Xna.Framework.Content.SoundEffectReader
UInt32 |
Format size |
|
Byte [format size] |
Format |
WAVEFORMATEX structure |
UInt32 |
Data size |
|
Byte [data size] |
Data |
Audio waveform data |
Int32 |
Loop start |
In bytes (start must be format block aligned) |
Int32 |
Loop length |
In bytes (length must be format block aligned) |
Int32 |
Duration |
In milliseconds |
Target type: Microsoft.Xna.Framework.Media.Song
Type reader name: Microsoft.Xna.Framework.Content.SongReader
String |
Streaming filename |
Relative path to a .wma file |
Object: Int32 |
Duration |
In milliseconds |
Target type: Microsoft.Xna.Framework.Media.Video
Type reader name: Microsoft.Xna.Framework.Content.VideoReader
Object: String |
Streaming filename |
Relative path to a .wmv file |
Object: Int32 |
Duration |
In milliseconds |
Object: Int32 |
Width |
|
Object: Int32 |
Height |
|
Object: Single |
Frames per second |
|
Object: Int32 |
Soundtrack type |
0 = Music 1 = Dialog 2 = Music and dialog |
Target type: T
Type reader name: Microsoft.Xna.Framework.Content.ReflectiveReader`1[[T]]
This reader is selected as a default fallback for types that do not provide their own custom ContentTypeWriter and ContentTypeReader implementations. It uses .NET reflection to examine the type T, and recursively calls into other type readers to load its fields and properties.
The deserialization process is:
If T has a base class other than System.Object or System.ValueType, first call the type reader for that base class
Load all valid properties
Load all valid fields
Fields and properties are valid for automatic serialization if:
They are declared directly by the type T (not inherited from a base class)
They are instance members (not static)
They are not decorated with ContentSerializerIgnoreAttribute
They are either public, or decorated with ContentSerializerAttribute
They are gettable
They are either settable, or the type reader for the type of that member specifies CanDeserializeIntoExistingObject = true
They are either not marked as shared resources, or the declaring type T is not a value type
They are not indexer properties
To deserialize a field or property:
Load as a shared resource if the member is decorated with ContentSerializerAttribute.SharedResource = true
Otherwise, If the member is a value type, load as a raw value
Otherwise, load as a polymorphic object