DXContainer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/BinaryFormat/DXContainer.h - The DXBC file format --*- C++/-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines manifest constants for the DXContainer object file format.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_BINARYFORMAT_DXCONTAINER_H
  18. #define LLVM_BINARYFORMAT_DXCONTAINER_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/SwapByteOrder.h"
  21. #include <stdint.h>
  22. namespace llvm {
  23. // The DXContainer file format is arranged as a header and "parts". Semantically
  24. // parts are similar to sections in other object file formats. The File format
  25. // structure is roughly:
  26. // ┌────────────────────────────────┐
  27. // │ Header │
  28. // ├────────────────────────────────┤
  29. // │ Part │
  30. // ├────────────────────────────────┤
  31. // │ Part │
  32. // ├────────────────────────────────┤
  33. // │ ... │
  34. // └────────────────────────────────┘
  35. namespace dxbc {
  36. struct Hash {
  37. uint8_t Digest[16];
  38. };
  39. enum class HashFlags : uint32_t {
  40. None = 0, // No flags defined.
  41. IncludesSource = 1, // This flag indicates that the shader hash was computed
  42. // taking into account source information (-Zss)
  43. };
  44. struct ShaderHash {
  45. uint32_t Flags; // dxbc::HashFlags
  46. uint8_t Digest[16];
  47. bool isPopulated();
  48. void swapBytes() { sys::swapByteOrder(Flags); }
  49. };
  50. struct ContainerVersion {
  51. uint16_t Major;
  52. uint16_t Minor;
  53. void swapBytes() {
  54. sys::swapByteOrder(Major);
  55. sys::swapByteOrder(Minor);
  56. }
  57. };
  58. struct Header {
  59. uint8_t Magic[4]; // "DXBC"
  60. Hash FileHash;
  61. ContainerVersion Version;
  62. uint32_t FileSize;
  63. uint32_t PartCount;
  64. void swapBytes() {
  65. Version.swapBytes();
  66. sys::swapByteOrder(FileSize);
  67. sys::swapByteOrder(PartCount);
  68. }
  69. // Structure is followed by part offsets: uint32_t PartOffset[PartCount];
  70. // The offset is to a PartHeader, which is followed by the Part Data.
  71. };
  72. /// Use this type to describe the size and type of a DXIL container part.
  73. struct PartHeader {
  74. uint8_t Name[4];
  75. uint32_t Size;
  76. void swapBytes() { sys::swapByteOrder(Size); }
  77. StringRef getName() const {
  78. return StringRef(reinterpret_cast<const char *>(&Name[0]), 4);
  79. }
  80. // Structure is followed directly by part data: uint8_t PartData[PartSize].
  81. };
  82. struct BitcodeHeader {
  83. uint8_t Magic[4]; // ACSII "DXIL".
  84. uint8_t MajorVersion; // DXIL version.
  85. uint8_t MinorVersion; // DXIL version.
  86. uint16_t Unused;
  87. uint32_t Offset; // Offset to LLVM bitcode (from start of header).
  88. uint32_t Size; // Size of LLVM bitcode (in bytes).
  89. // Followed by uint8_t[BitcodeHeader.Size] at &BitcodeHeader + Header.Offset
  90. void swapBytes() {
  91. sys::swapByteOrder(MinorVersion);
  92. sys::swapByteOrder(MajorVersion);
  93. sys::swapByteOrder(Offset);
  94. sys::swapByteOrder(Size);
  95. }
  96. };
  97. struct ProgramHeader {
  98. uint8_t MinorVersion : 4;
  99. uint8_t MajorVersion : 4;
  100. uint8_t Unused;
  101. uint16_t ShaderKind;
  102. uint32_t Size; // Size in uint32_t words including this header.
  103. BitcodeHeader Bitcode;
  104. void swapBytes() {
  105. sys::swapByteOrder(ShaderKind);
  106. sys::swapByteOrder(Size);
  107. Bitcode.swapBytes();
  108. }
  109. };
  110. static_assert(sizeof(ProgramHeader) == 24, "ProgramHeader Size incorrect!");
  111. #define CONTAINER_PART(Part) Part,
  112. enum class PartType {
  113. Unknown = 0,
  114. #include "DXContainerConstants.def"
  115. };
  116. #define SHADER_FLAG(Num, Val, Str) Val = 1ull << Num,
  117. enum class FeatureFlags : uint64_t {
  118. #include "DXContainerConstants.def"
  119. };
  120. static_assert((uint64_t)FeatureFlags::NextUnusedBit <= 1ull << 63,
  121. "Shader flag bits exceed enum size.");
  122. PartType parsePartType(StringRef S);
  123. } // namespace dxbc
  124. } // namespace llvm
  125. #endif // LLVM_BINARYFORMAT_DXCONTAINER_H
  126. #ifdef __GNUC__
  127. #pragma GCC diagnostic pop
  128. #endif