14 The DirectX Container (DXContainer) file format is the binary file format for
15 compiled shaders targeting the DirectX runtime. The file format is also called
16 the DXIL Container or DXBC file format. Because the file format can be used to
17 include either DXIL or DXBC compiled shaders, the nomenclature in LLVM is simply
20 DirectX Container files are read by the compiler and associated tools as well as
21 the DirectX runtime, profiling tools and other users. This document serves as a
22 companion to the implementation in LLVM to more completely document the file
23 format for its many users.
28 A DXContainer file begins with a header, and is then followed by a sequence of
29 "parts", which are analogous to object file sections. Each part contains a part
30 header, and some number of bytes of data after the header in a defined format.
32 DX Container data structures are encoded little-endian in the binary file.
34 The LLVM versions of all data structures described and/or referenced in this
36 `llvm/include/llvm/BinaryFormat/DXContainer.h
37 <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainer.h>`_.
38 Some pseudo code is provided in blocks below to ease understanding of this
39 document, but reading it with the header available will provide the most
50 uint16_t MajorVersion;
51 uint16_t MinorVersion;
56 The DXContainer header matches the pseudo-definition above. It begins with a
57 four character code (magic number) with the value ``DXBC`` to denote the file
60 The ``Digest`` is a 128bit hash digest computed with a proprietary algorithm and
61 encoded in the binary by the bytecode validator.
63 The ``MajorVersion`` and ``MinorVersion`` encode the file format version
66 The remaining fields encode 32-bit unsigned integers for the file size and
69 Following the part header is an array of ``PartCount`` 32-bit unsigned integers
70 specifying the offsets of each part header.
82 Each part begins with a part header. A part header includes the 4-character part
83 name, and a 32-bit unsigned integer specifying the size of the part data. The
84 part header is followed by ``Size`` bytes of data comprising the part. The
85 format does not explicitly require 32-bit alignment of parts, although LLVM does
86 implement this restriction in the writer code (because it's a good idea). The
87 LLVM object reader code does not assume inputs are correctly aligned to avoid
88 undefined behavior caused by misaligned inputs generated by other compilers.
93 The part name indicates the format of the part data. There are 24 part headers
94 used by DXC and FXC. Not all compiled shaders contain all parts. In the list
95 below parts generated only by DXC are marked with †, and parts generated only by
96 FXC are marked with \*.
98 #. `DXIL`_† - Stores the DXIL bytecode.
99 #. `HASH`_† - Stores the shader MD5 hash.
100 #. ILDB† - Stores the DXIL bytecode with LLVM Debug Information embedded in the module.
101 #. ILDN† - Stores shader debug name for external debug information.
102 #. `ISG1`_ - Stores the input signature for Shader Model 5.1+.
103 #. ISGN\* - Stores the input signature for Shader Model 4 and earlier.
104 #. `OSG1`_ - Stores the output signature for Shader Model 5.1+.
105 #. OSG5\* - Stores the output signature for Shader Model 5.
106 #. OSGN\* - Stores the output signature for Shader Model 4 and earlier.
107 #. PCSG\* - Stores the patch constant signature for Shader Model 5.1 and earlier.
108 #. PDBI† - Stores PDB information.
109 #. PRIV - Stores arbitrary private data (Not encoded by either FXC or DXC).
110 #. `PSG1`_ - Stores the patch constant signature for Shader Model 6+.
111 #. `PSV0`_ - Stores Pipeline State Validation data.
112 #. RDAT† - Stores Runtime Data.
113 #. RDEF\* - Stores resource definitions.
114 #. RTS0 - Stores compiled root signature.
115 #. `SFI0`_ - Stores shader feature flags.
116 #. SHDR\* - Stores compiled DXBC bytecode.
117 #. SHEX\* - Stores compiled DXBC bytecode.
118 #. DXBC\* - Stores compiled DXBC bytecode.
119 #. SRCI† - Stores shader source information.
120 #. STAT† - Stores shader statistics.
121 #. VERS† - Stores shader compiler version information.
127 The DXIL part is comprised of three data structures: the ``ProgramHeader``, the
128 ``BitcodeHeader`` and the bitcode serialized LLVM 3.7 IR Module.
130 The ``ProgramHeader`` contains the shader model version and pipeline stage
131 enumeration value. This identifies the target profile of the contained shader
134 The ``BitcodeHeader`` contains the DXIL version information and refers to the
135 start of the bitcode data.
141 The HASH part contains a 32-bit unsigned integer with the shader hash flags, and
142 a 128-bit MD5 hash digest. The flags field can either have the value ``0`` to
143 indicate no flags, or ``1`` to indicate that the file hash was computed
144 including the source code that produced the binary.
146 Program Signature (SG1) Parts
147 -----------------------------
154 struct ProgramSignatureHeader {
156 uint32_t FirstParamOffset;
159 The program signature parts (ISG1, OSG1, & PSG1) all use the same data
160 structures to encode inputs, outputs and patch information. The
161 ``ProgramSignatureHeader`` includes two 32-bit unsigned integers to specify the
162 number of signature parameters and the offset of the first parameter.
164 Beginning at ``FirstParamOffset`` bytes from the start of the
165 ``ProgramSignatureHeader``, ``ParamCount`` ``ProgramSignatureElement``
166 structures are written. Following the ``ProgramSignatureElements`` is a string
167 table of null terminated strings padded to 32-byte alignment. This string table
168 matches the DWARF string table format as implemented by LLVM.
170 Each ``ProgramSignatureElement`` encodes a ``NameOffset`` value which specifies
171 the offset into the string table. A value of ``0`` denotes no name. The offsets
172 encoded here are from the beginning of the ``ProgramSignatureHeader`` not the
173 beginning of the string table.
175 The ``ProgramSignatureElement`` contains several enumeration fields which are
176 defined in `llvm/include/llvm/BinaryFormat/DXContainerConstants.def <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainerConstants.def>`_.
177 These fields encode the D3D system value, the type of data and its precision
184 The Pipeline State Validation data encodes versioned runtime information
185 structures. These structures use a scheme where in lieu of encoding a version
186 number, they encode the size of the structure and each new version of the
187 structure is additive. This allows readers to infer the version of the structure
188 by comparing the encoded size with the size of known structures. If the encoded
189 size is larger than any known structure, the largest known structure can validly
190 parse the data represented in the known structure.
192 In LLVM we represent the versions of the associated data structures with
193 versioned namespaces under the ``llvm::dxbc::PSV`` namespace (e.g. ``v0``,
194 ``v1``). Each structure in the ``v0`` namespace is the base version, the
195 structures in the ``v1`` namespace inherit from the ``v0`` namespace, and the
196 ``v2`` structures inherit from the ``v1`` structures, and so on.
198 The high-level structure of the PSV data is:
200 #. ``RuntimeInfo`` structure
202 #. Signature elements
203 #. Mask Vectors (Output, Input, InputPatch, PatchOutput)
205 Immediately following the part header for the PSV0 part is a 32-bit unsigned
206 integer specifying the size of the ``RuntimeInfo`` structure that follows.
208 Immediately following the ``RuntimeInfo`` structure is a 32-bit unsigned integer
209 specifying the number of resource bindings. If the number of resources is
210 greater than zero, another unsigned 32-bit integer follows to specify the size
211 of the ``ResourceBindInfo`` structure. This is followed by the specified number
212 of structures of the specified size (which infers the version of the structure).
214 For version 0 of the data this ends the part data.
216 PSV0 Signature Elements
217 ~~~~~~~~~~~~~~~~~~~~~~~
219 The signature elements are conceptually a single concept but the data is encoded
220 in three different blocks. The first block is a string table, the second block
221 is an index table, and the third block is the elements themselves, which in turn
222 are separeated by input, output and patch constant or primitive elements.
224 Signature elements capture much of the same data captured in the :ref:`SG1
225 <ISG1>` parts. The use of an index table allows de-duplciation of data for a more
226 compact final representation.
228 The string table begins with a 32-bit unsigned integer specifying the table
229 size. This string table uses the DXContainer format as implemented in LLVM. This
230 format prefixes the string table with a null byte so that offset ``0`` is a null
231 string, and pads to 32-byte alignment.
233 The index table begins with a 32-bit unsigned integer specifying the size of the
234 table, and is followed by that many 32-bit unsigned integers representing the
235 table. The index table may or may not deduplicate repeated sequences (both DXC
236 and Clang do). The indices signify the indices in the flattened aggregate
237 representation which the signature element describes. A single semantic may have
238 more than one entry in this table to denote the different attributes of its
241 For example given the following code:
255 float2 f2[4] : VOUT1;
260 void main(out VSOut o1 : A) {
263 The semantic ``A`` gets expanded into 5 output signature elements. Those
268 In the example below, it is a coincidence that the rows match the indices, in
269 more complicated examples with multiple semantics this is not the case.
271 #. Index 0 starts at row 0, contains 4 columns, and is float32. This represents
272 ``f1`` in the source.
273 #. Index 1, 2, 3, and 4 starts at row 1, contains two columns and is float32.
274 This represents ``f2`` in the source, and it spreads across rows 1 - 4.
275 #. Index 5 starts at row 5, contains 4 columns, and is float32. This represents
276 ``f3`` in the source.
277 #. Index 6 starts at row 6, contains 3 columns, and is float32. This represents
279 #. Index 7 starts at row 7, contains 4 columns, and is signed 32-bit integer.
280 This represents ``f5`` in the source.
282 The LLVM ``obj2yaml`` tool can parse this data out of the PSV and present it in
283 human readable YAML. For the example above it produces the output:
295 ComponentType: Float32
296 Interpolation: Linear
300 Indices: [ 1, 2, 3, 4 ]
306 ComponentType: Float32
307 Interpolation: Linear
317 ComponentType: Float32
318 Interpolation: Linear
328 ComponentType: Float32
329 Interpolation: Linear
339 ComponentType: SInt32
340 Interpolation: Constant
344 The number of signature elements of each type is encoded in the
345 ``llvm::dxbc::PSV::v1::RuntimeInfo`` structure. If any of the element count
346 values are non-zero, the size of the ``ProgramSignatureElement`` structure is
347 encoded next to allow versioning of that structure. Today there is only one
348 version. Following the size field is the specified number of signature elements
349 in the order input, output, then patch constant or primitive.
351 Following the signature elements is a sequence of mask vectors encoded as a
352 series of 32-bit integers. Each 32-bit integer in the mask encodes values for 8
353 input/output/patch or primitive elements. The mask vector is filled from least
354 significant bit to most significant bit with each added element shifting the
355 previous elements left. A reader needs to consult the total number of vectors
356 encoded in the ``RuntimeInfo`` structure to know how to read the mask vector.
358 If the shader has ``UsesViewID`` enabled in the ``RuntimeInfo`` an output mask
359 vector will be included. The output mask vector is four arrays of 32-bit
360 unsigned integers. Each of the four arrays corresponds to an output stream.
361 Geometry shaders have a maximum of four output streams, all other shader stages
362 only support one output stream. Each bit in the mask vector identifies one
363 column of an output from the output signature depends on the ViewID.
365 If the shader has ``UsesViewID`` enabled, it is a hull shader, and it has patch
366 constant or primitive vector elements, a patch constant or primitive vector mask
367 will be included. It is identical in structure to the output mask vector. Each
368 bit in the mask vector identifies one column of a patch constant output which
369 depends on the ViewID.
371 The next series of mask vectors are similar in structure to the output mask
372 vector, but they contain an extra dimension.
374 The output/input map is encoded next if the shader has inputs and outputs. The
375 output/input mask encodes which outputs are impacted by each column of each
376 input. The size for each mask vector is the size of the output max vector * the
377 number of inputs * 4 (for each component). Each bit in the mask vector
378 identifies one column of an output and a column of an input. A value of 1 means
379 the output is impacted by the input.
381 If the shader is a hull shader, and it has inputs and patch outputs, an input to
382 patch map will be included next. This is identical in structure to the
383 output/input map. The dimensions are defined by the size of the patch constant
384 or primitive vector mask * the number of inputs * 4 (for each component). Each
385 bit in the mask vector identifies one column of a patch constant output and a
386 column of an input. A value of 1 means the output is impacted by the input.
388 If the shader is a domain shader, and it has outputs and patch outputs, an
389 output patch map will be included next. This is identical in structure to the
390 output/input map. The dimensions are defined by the size of the patch constant
391 or primitive vector mask * the number of outputs * 4 (for each component). Each
392 bit in the mask vector identifies one column of a patch constant input and a
393 column of an output. A value of 1 means the output is impacted by the primitive
400 The SFI0 part encodes a 64-bit unsigned integer bitmask of the feature flags.
401 This denotes which optional features the shader requires. The flag values are
402 defined in `llvm/include/llvm/BinaryFormat/DXContainerConstants.def <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainerConstants.def>`_.