1================= 2DirectX Container 3================= 4 5.. contents:: 6 :local: 7 8.. toctree:: 9 :hidden: 10 11Overview 12======== 13 14The DirectX Container (DXContainer) file format is the binary file format for 15compiled shaders targeting the DirectX runtime. The file format is also called 16the DXIL Container or DXBC file format. Because the file format can be used to 17include either DXIL or DXBC compiled shaders, the nomenclature in LLVM is simply 18DirectX Container. 19 20DirectX Container files are read by the compiler and associated tools as well as 21the DirectX runtime, profiling tools and other users. This document serves as a 22companion to the implementation in LLVM to more completely document the file 23format for its many users. 24 25Basic Structure 26=============== 27 28A 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 30header, and some number of bytes of data after the header in a defined format. 31 32DX Container data structures are encoded little-endian in the binary file. 33 34The LLVM versions of all data structures described and/or referenced in this 35file are defined in 36`llvm/include/llvm/BinaryFormat/DXContainer.h 37<https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainer.h>`_. 38Some pseudo code is provided in blocks below to ease understanding of this 39document, but reading it with the header available will provide the most 40clarity. 41 42File Header 43----------- 44 45.. code-block:: c 46 47 struct Header { 48 uint8_t Magic[4]; 49 uint8_t Digest[16]; 50 uint16_t MajorVersion; 51 uint16_t MinorVersion; 52 uint32_t FileSize; 53 uint32_t PartCount; 54 }; 55 56The DXContainer header matches the pseudo-definition above. It begins with a 57four character code (magic number) with the value ``DXBC`` to denote the file 58format. 59 60The ``Digest`` is a 128bit hash digest computed with a proprietary algorithm and 61encoded in the binary by the bytecode validator. 62 63The ``MajorVersion`` and ``MinorVersion`` encode the file format version 64``1.0``. 65 66The remaining fields encode 32-bit unsigned integers for the file size and 67number of parts. 68 69Following the part header is an array of ``PartCount`` 32-bit unsigned integers 70specifying the offsets of each part header. 71 72Part Data 73--------- 74 75.. code-block:: c 76 77 struct PartHeader { 78 uint8_t Name[4]; 79 uint32_t Size; 80 } 81 82Each part begins with a part header. A part header includes the 4-character part 83name, and a 32-bit unsigned integer specifying the size of the part data. The 84part header is followed by ``Size`` bytes of data comprising the part. The 85format does not explicitly require 32-bit alignment of parts, although LLVM does 86implement this restriction in the writer code (because it's a good idea). The 87LLVM object reader code does not assume inputs are correctly aligned to avoid 88undefined behavior caused by misaligned inputs generated by other compilers. 89 90Part Formats 91============ 92 93The part name indicates the format of the part data. There are 24 part headers 94used by DXC and FXC. Not all compiled shaders contain all parts. In the list 95below parts generated only by DXC are marked with †, and parts generated only by 96FXC are marked with \*. 97 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. 122 123DXIL Part 124--------- 125.. _DXIL: 126 127The DXIL part is comprised of three data structures: the ``ProgramHeader``, the 128``BitcodeHeader`` and the bitcode serialized LLVM 3.7 IR Module. 129 130The ``ProgramHeader`` contains the shader model version and pipeline stage 131enumeration value. This identifies the target profile of the contained shader 132bitcode. 133 134The ``BitcodeHeader`` contains the DXIL version information and refers to the 135start of the bitcode data. 136 137HASH Part 138--------- 139.. _HASH: 140 141The HASH part contains a 32-bit unsigned integer with the shader hash flags, and 142a 128-bit MD5 hash digest. The flags field can either have the value ``0`` to 143indicate no flags, or ``1`` to indicate that the file hash was computed 144including the source code that produced the binary. 145 146Program Signature (SG1) Parts 147----------------------------- 148.. _ISG1: 149.. _OSG1: 150.. _PSG1: 151 152.. code-block:: c 153 154 struct ProgramSignatureHeader { 155 uint32_t ParamCount; 156 uint32_t FirstParamOffset; 157 } 158 159The program signature parts (ISG1, OSG1, & PSG1) all use the same data 160structures to encode inputs, outputs and patch information. The 161``ProgramSignatureHeader`` includes two 32-bit unsigned integers to specify the 162number of signature parameters and the offset of the first parameter. 163 164Beginning at ``FirstParamOffset`` bytes from the start of the 165``ProgramSignatureHeader``, ``ParamCount`` ``ProgramSignatureElement`` 166structures are written. Following the ``ProgramSignatureElements`` is a string 167table of null terminated strings padded to 32-byte alignment. This string table 168matches the DWARF string table format as implemented by LLVM. 169 170Each ``ProgramSignatureElement`` encodes a ``NameOffset`` value which specifies 171the offset into the string table. A value of ``0`` denotes no name. The offsets 172encoded here are from the beginning of the ``ProgramSignatureHeader`` not the 173beginning of the string table. 174 175The ``ProgramSignatureElement`` contains several enumeration fields which are 176defined in `llvm/include/llvm/BinaryFormat/DXContainerConstants.def <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainerConstants.def>`_. 177These fields encode the D3D system value, the type of data and its precision 178requirements. 179 180PSV0 Part 181--------- 182.. _PSV0: 183 184The Pipeline State Validation data encodes versioned runtime information 185structures. These structures use a scheme where in lieu of encoding a version 186number, they encode the size of the structure and each new version of the 187structure is additive. This allows readers to infer the version of the structure 188by comparing the encoded size with the size of known structures. If the encoded 189size is larger than any known structure, the largest known structure can validly 190parse the data represented in the known structure. 191 192In LLVM we represent the versions of the associated data structures with 193versioned namespaces under the ``llvm::dxbc::PSV`` namespace (e.g. ``v0``, 194``v1``). Each structure in the ``v0`` namespace is the base version, the 195structures in the ``v1`` namespace inherit from the ``v0`` namespace, and the 196``v2`` structures inherit from the ``v1`` structures, and so on. 197 198The high-level structure of the PSV data is: 199 200#. ``RuntimeInfo`` structure 201#. Resource bindings 202#. Signature elements 203#. Mask Vectors (Output, Input, InputPatch, PatchOutput) 204 205Immediately following the part header for the PSV0 part is a 32-bit unsigned 206integer specifying the size of the ``RuntimeInfo`` structure that follows. 207 208Immediately following the ``RuntimeInfo`` structure is a 32-bit unsigned integer 209specifying the number of resource bindings. If the number of resources is 210greater than zero, another unsigned 32-bit integer follows to specify the size 211of the ``ResourceBindInfo`` structure. This is followed by the specified number 212of structures of the specified size (which infers the version of the structure). 213 214For version 0 of the data this ends the part data. 215 216PSV0 Signature Elements 217~~~~~~~~~~~~~~~~~~~~~~~ 218 219The signature elements are conceptually a single concept but the data is encoded 220in three different blocks. The first block is a string table, the second block 221is an index table, and the third block is the elements themselves, which in turn 222are separeated by input, output and patch constant or primitive elements. 223 224Signature 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 226compact final representation. 227 228The string table begins with a 32-bit unsigned integer specifying the table 229size. This string table uses the DXContainer format as implemented in LLVM. This 230format prefixes the string table with a null byte so that offset ``0`` is a null 231string, and pads to 32-byte alignment. 232 233The index table begins with a 32-bit unsigned integer specifying the size of the 234table, and is followed by that many 32-bit unsigned integers representing the 235table. The index table may or may not deduplicate repeated sequences (both DXC 236and Clang do). The indices signify the indices in the flattened aggregate 237representation which the signature element describes. A single semantic may have 238more than one entry in this table to denote the different attributes of its 239members. 240 241For example given the following code: 242 243.. code-block:: c 244 245 struct VSOut_1 246 { 247 float4 f3 : VOUT2; 248 float3 f4 : VOUT3; 249 }; 250 251 252 struct VSOut 253 { 254 float4 f1 : VOUT0; 255 float2 f2[4] : VOUT1; 256 VSOut_1 s; 257 int4 f5 : VOUT4; 258 }; 259 260 void main(out VSOut o1 : A) { 261 } 262 263The semantic ``A`` gets expanded into 5 output signature elements. Those 264elements are: 265 266.. note:: 267 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. 270 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 278 ``f4``. 279#. Index 7 starts at row 7, contains 4 columns, and is signed 32-bit integer. 280 This represents ``f5`` in the source. 281 282The LLVM ``obj2yaml`` tool can parse this data out of the PSV and present it in 283human readable YAML. For the example above it produces the output: 284 285.. code-block:: YAML 286 287 SigOutputElements: 288 - Name: A 289 Indices: [ 0 ] 290 StartRow: 0 291 Cols: 4 292 StartCol: 0 293 Allocated: true 294 Kind: Arbitrary 295 ComponentType: Float32 296 Interpolation: Linear 297 DynamicMask: 0x0 298 Stream: 0 299 - Name: A 300 Indices: [ 1, 2, 3, 4 ] 301 StartRow: 1 302 Cols: 2 303 StartCol: 0 304 Allocated: true 305 Kind: Arbitrary 306 ComponentType: Float32 307 Interpolation: Linear 308 DynamicMask: 0x0 309 Stream: 0 310 - Name: A 311 Indices: [ 5 ] 312 StartRow: 5 313 Cols: 4 314 StartCol: 0 315 Allocated: true 316 Kind: Arbitrary 317 ComponentType: Float32 318 Interpolation: Linear 319 DynamicMask: 0x0 320 Stream: 0 321 - Name: A 322 Indices: [ 6 ] 323 StartRow: 6 324 Cols: 3 325 StartCol: 0 326 Allocated: true 327 Kind: Arbitrary 328 ComponentType: Float32 329 Interpolation: Linear 330 DynamicMask: 0x0 331 Stream: 0 332 - Name: A 333 Indices: [ 7 ] 334 StartRow: 7 335 Cols: 4 336 StartCol: 0 337 Allocated: true 338 Kind: Arbitrary 339 ComponentType: SInt32 340 Interpolation: Constant 341 DynamicMask: 0x0 342 Stream: 0 343 344The number of signature elements of each type is encoded in the 345``llvm::dxbc::PSV::v1::RuntimeInfo`` structure. If any of the element count 346values are non-zero, the size of the ``ProgramSignatureElement`` structure is 347encoded next to allow versioning of that structure. Today there is only one 348version. Following the size field is the specified number of signature elements 349in the order input, output, then patch constant or primitive. 350 351Following the signature elements is a sequence of mask vectors encoded as a 352series of 32-bit integers. Each 32-bit integer in the mask encodes values for 8 353input/output/patch or primitive elements. The mask vector is filled from least 354significant bit to most significant bit with each added element shifting the 355previous elements left. A reader needs to consult the total number of vectors 356encoded in the ``RuntimeInfo`` structure to know how to read the mask vector. 357 358If the shader has ``UsesViewID`` enabled in the ``RuntimeInfo`` an output mask 359vector will be included. The output mask vector is four arrays of 32-bit 360unsigned integers. Each of the four arrays corresponds to an output stream. 361Geometry shaders have a maximum of four output streams, all other shader stages 362only support one output stream. Each bit in the mask vector identifies one 363column of an output from the output signature depends on the ViewID. 364 365If the shader has ``UsesViewID`` enabled, it is a hull shader, and it has patch 366constant or primitive vector elements, a patch constant or primitive vector mask 367will be included. It is identical in structure to the output mask vector. Each 368bit in the mask vector identifies one column of a patch constant output which 369depends on the ViewID. 370 371The next series of mask vectors are similar in structure to the output mask 372vector, but they contain an extra dimension. 373 374The output/input map is encoded next if the shader has inputs and outputs. The 375output/input mask encodes which outputs are impacted by each column of each 376input. The size for each mask vector is the size of the output max vector * the 377number of inputs * 4 (for each component). Each bit in the mask vector 378identifies one column of an output and a column of an input. A value of 1 means 379the output is impacted by the input. 380 381If the shader is a hull shader, and it has inputs and patch outputs, an input to 382patch map will be included next. This is identical in structure to the 383output/input map. The dimensions are defined by the size of the patch constant 384or primitive vector mask * the number of inputs * 4 (for each component). Each 385bit in the mask vector identifies one column of a patch constant output and a 386column of an input. A value of 1 means the output is impacted by the input. 387 388If the shader is a domain shader, and it has outputs and patch outputs, an 389output patch map will be included next. This is identical in structure to the 390output/input map. The dimensions are defined by the size of the patch constant 391or primitive vector mask * the number of outputs * 4 (for each component). Each 392bit in the mask vector identifies one column of a patch constant input and a 393column of an output. A value of 1 means the output is impacted by the primitive 394input. 395 396SFI0 Part 397--------- 398.. _SFI0: 399 400The SFI0 part encodes a 64-bit unsigned integer bitmask of the feature flags. 401This denotes which optional features the shader requires. The flag values are 402defined in `llvm/include/llvm/BinaryFormat/DXContainerConstants.def <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/DXContainerConstants.def>`_. 403