1 //===- DXContainerYAML.h - DXContainer YAMLIO implementation ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file declares classes for handling the YAML representation 11 /// of DXContainer. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_OBJECTYAML_DXCONTAINERYAML_H 16 #define LLVM_OBJECTYAML_DXCONTAINERYAML_H 17 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/BinaryFormat/DXContainer.h" 20 #include "llvm/ObjectYAML/YAML.h" 21 #include "llvm/Support/YAMLTraits.h" 22 #include <array> 23 #include <cstdint> 24 #include <optional> 25 #include <string> 26 #include <vector> 27 28 namespace llvm { 29 namespace DXContainerYAML { 30 31 struct VersionTuple { 32 uint16_t Major; 33 uint16_t Minor; 34 }; 35 36 // The optional header fields are required in the binary and will be populated 37 // when reading from binary, but can be omitted in the YAML text because the 38 // emitter can calculate them. 39 struct FileHeader { 40 std::vector<llvm::yaml::Hex8> Hash; 41 VersionTuple Version; 42 std::optional<uint32_t> FileSize; 43 uint32_t PartCount; 44 std::optional<std::vector<uint32_t>> PartOffsets; 45 }; 46 47 struct DXILProgram { 48 uint8_t MajorVersion; 49 uint8_t MinorVersion; 50 uint16_t ShaderKind; 51 std::optional<uint32_t> Size; 52 uint16_t DXILMajorVersion; 53 uint16_t DXILMinorVersion; 54 std::optional<uint32_t> DXILOffset; 55 std::optional<uint32_t> DXILSize; 56 std::optional<std::vector<llvm::yaml::Hex8>> DXIL; 57 }; 58 59 #define SHADER_FEATURE_FLAG(Num, DxilModuleNum, Val, Str) bool Val = false; 60 struct ShaderFeatureFlags { 61 ShaderFeatureFlags() = default; 62 ShaderFeatureFlags(uint64_t FlagData); 63 uint64_t getEncodedFlags(); 64 #include "llvm/BinaryFormat/DXContainerConstants.def" 65 }; 66 67 struct ShaderHash { 68 ShaderHash() = default; 69 ShaderHash(const dxbc::ShaderHash &Data); 70 71 bool IncludesSource; 72 std::vector<llvm::yaml::Hex8> Digest; 73 }; 74 75 using ResourceFlags = dxbc::PSV::ResourceFlags; 76 using ResourceBindInfo = dxbc::PSV::v2::ResourceBindInfo; 77 78 struct SignatureElement { 79 SignatureElement() = default; 80 81 SignatureElement(dxbc::PSV::v0::SignatureElement El, StringRef StringTable, 82 ArrayRef<uint32_t> IdxTable) 83 : Name(StringTable.substr(El.NameOffset, 84 StringTable.find('\0', El.NameOffset) - 85 El.NameOffset)), 86 Indices(IdxTable.slice(El.IndicesOffset, El.Rows)), 87 StartRow(El.StartRow), Cols(El.Cols), StartCol(El.StartCol), 88 Allocated(El.Allocated != 0), Kind(El.Kind), Type(El.Type), 89 Mode(El.Mode), DynamicMask(El.DynamicMask), Stream(El.Stream) {} 90 StringRef Name; 91 SmallVector<uint32_t> Indices; 92 93 uint8_t StartRow; 94 uint8_t Cols; 95 uint8_t StartCol; 96 bool Allocated; 97 dxbc::PSV::SemanticKind Kind; 98 99 dxbc::PSV::ComponentType Type; 100 dxbc::PSV::InterpolationMode Mode; 101 llvm::yaml::Hex8 DynamicMask; 102 uint8_t Stream; 103 }; 104 105 struct PSVInfo { 106 // The version field isn't actually encoded in the file, but it is inferred by 107 // the size of data regions. We include it in the yaml because it simplifies 108 // the format. 109 uint32_t Version; 110 111 dxbc::PSV::v3::RuntimeInfo Info; 112 uint32_t ResourceStride; 113 SmallVector<ResourceBindInfo> Resources; 114 SmallVector<SignatureElement> SigInputElements; 115 SmallVector<SignatureElement> SigOutputElements; 116 SmallVector<SignatureElement> SigPatchOrPrimElements; 117 118 using MaskVector = SmallVector<llvm::yaml::Hex32>; 119 std::array<MaskVector, 4> OutputVectorMasks; 120 MaskVector PatchOrPrimMasks; 121 std::array<MaskVector, 4> InputOutputMap; 122 MaskVector InputPatchMap; 123 MaskVector PatchOutputMap; 124 125 StringRef EntryName; 126 127 void mapInfoForVersion(yaml::IO &IO); 128 129 PSVInfo(); 130 PSVInfo(const dxbc::PSV::v0::RuntimeInfo *P, uint16_t Stage); 131 PSVInfo(const dxbc::PSV::v1::RuntimeInfo *P); 132 PSVInfo(const dxbc::PSV::v2::RuntimeInfo *P); 133 PSVInfo(const dxbc::PSV::v3::RuntimeInfo *P, StringRef StringTable); 134 }; 135 136 struct SignatureParameter { 137 uint32_t Stream; 138 std::string Name; 139 uint32_t Index; 140 dxbc::D3DSystemValue SystemValue; 141 dxbc::SigComponentType CompType; 142 uint32_t Register; 143 uint8_t Mask; 144 uint8_t ExclusiveMask; 145 dxbc::SigMinPrecision MinPrecision; 146 }; 147 148 struct Signature { 149 llvm::SmallVector<SignatureParameter> Parameters; 150 }; 151 152 struct Part { 153 Part() = default; 154 Part(std::string N, uint32_t S) : Name(N), Size(S) {} 155 std::string Name; 156 uint32_t Size; 157 std::optional<DXILProgram> Program; 158 std::optional<ShaderFeatureFlags> Flags; 159 std::optional<ShaderHash> Hash; 160 std::optional<PSVInfo> Info; 161 std::optional<DXContainerYAML::Signature> Signature; 162 }; 163 164 struct Object { 165 FileHeader Header; 166 std::vector<Part> Parts; 167 }; 168 169 } // namespace DXContainerYAML 170 } // namespace llvm 171 172 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::Part) 173 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::ResourceBindInfo) 174 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureElement) 175 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector) 176 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter) 177 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind) 178 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType) 179 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode) 180 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ResourceType) 181 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ResourceKind) 182 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::D3DSystemValue) 183 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigComponentType) 184 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::SigMinPrecision) 185 186 namespace llvm { 187 188 class raw_ostream; 189 190 namespace yaml { 191 192 template <> struct MappingTraits<DXContainerYAML::VersionTuple> { 193 static void mapping(IO &IO, DXContainerYAML::VersionTuple &Version); 194 }; 195 196 template <> struct MappingTraits<DXContainerYAML::FileHeader> { 197 static void mapping(IO &IO, DXContainerYAML::FileHeader &Header); 198 }; 199 200 template <> struct MappingTraits<DXContainerYAML::DXILProgram> { 201 static void mapping(IO &IO, DXContainerYAML::DXILProgram &Program); 202 }; 203 204 template <> struct MappingTraits<DXContainerYAML::ShaderFeatureFlags> { 205 static void mapping(IO &IO, DXContainerYAML::ShaderFeatureFlags &Flags); 206 }; 207 208 template <> struct MappingTraits<DXContainerYAML::ShaderHash> { 209 static void mapping(IO &IO, DXContainerYAML::ShaderHash &Hash); 210 }; 211 212 template <> struct MappingTraits<DXContainerYAML::PSVInfo> { 213 static void mapping(IO &IO, DXContainerYAML::PSVInfo &PSV); 214 }; 215 216 template <> struct MappingTraits<DXContainerYAML::Part> { 217 static void mapping(IO &IO, DXContainerYAML::Part &Version); 218 }; 219 220 template <> struct MappingTraits<DXContainerYAML::Object> { 221 static void mapping(IO &IO, DXContainerYAML::Object &Obj); 222 }; 223 224 template <> struct MappingTraits<DXContainerYAML::ResourceFlags> { 225 static void mapping(IO &IO, DXContainerYAML::ResourceFlags &Flags); 226 }; 227 228 template <> struct MappingTraits<DXContainerYAML::ResourceBindInfo> { 229 static void mapping(IO &IO, DXContainerYAML::ResourceBindInfo &Res); 230 }; 231 232 template <> struct MappingTraits<DXContainerYAML::SignatureElement> { 233 static void mapping(IO &IO, llvm::DXContainerYAML::SignatureElement &El); 234 }; 235 236 template <> struct MappingTraits<DXContainerYAML::SignatureParameter> { 237 static void mapping(IO &IO, llvm::DXContainerYAML::SignatureParameter &El); 238 }; 239 240 template <> struct MappingTraits<DXContainerYAML::Signature> { 241 static void mapping(IO &IO, llvm::DXContainerYAML::Signature &El); 242 }; 243 244 } // namespace yaml 245 246 } // namespace llvm 247 248 #endif // LLVM_OBJECTYAML_DXCONTAINERYAML_H 249