| 1 | |
| 2 | //===-- llvm/BinaryFormat/DXContainer.cpp - DXContainer Utils ----*- C++-*-===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains utility functions for working with DXContainers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/BinaryFormat/DXContainer.h" |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/Support/ScopedPrinter.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::dxbc; |
| 20 | |
| 21 | #define ROOT_PARAMETER(Val, Enum) \ |
| 22 | case Val: \ |
| 23 | return true; |
| 24 | bool llvm::dxbc::isValidParameterType(uint32_t V) { |
| 25 | switch (V) { |
| 26 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 27 | } |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | bool llvm::dxbc::isValidRangeType(uint32_t V) { |
| 32 | return V <= llvm::to_underlying(E: dxil::ResourceClass::LastEntry); |
| 33 | } |
| 34 | |
| 35 | #define SHADER_VISIBILITY(Val, Enum) \ |
| 36 | case Val: \ |
| 37 | return true; |
| 38 | bool llvm::dxbc::isValidShaderVisibility(uint32_t V) { |
| 39 | switch (V) { |
| 40 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | #define FILTER(Val, Enum) \ |
| 46 | case Val: \ |
| 47 | return true; |
| 48 | bool llvm::dxbc::isValidSamplerFilter(uint32_t V) { |
| 49 | switch (V) { |
| 50 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | #define TEXTURE_ADDRESS_MODE(Val, Enum) \ |
| 56 | case Val: \ |
| 57 | return true; |
| 58 | bool llvm::dxbc::isValidAddress(uint32_t V) { |
| 59 | switch (V) { |
| 60 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | #define COMPARISON_FUNC(Val, Enum) \ |
| 66 | case Val: \ |
| 67 | return true; |
| 68 | bool llvm::dxbc::isValidComparisonFunc(uint32_t V) { |
| 69 | switch (V) { |
| 70 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | #define STATIC_BORDER_COLOR(Val, Enum) \ |
| 76 | case Val: \ |
| 77 | return true; |
| 78 | bool llvm::dxbc::isValidBorderColor(uint32_t V) { |
| 79 | switch (V) { |
| 80 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | template <typename FlagT> |
| 86 | static bool isValidFlags(std::underlying_type_t<FlagT> V) { |
| 87 | decltype(V) LargestValue = |
| 88 | llvm::to_underlying(FlagT::LLVM_BITMASK_LARGEST_ENUMERATOR); |
| 89 | return V < NextPowerOf2(LargestValue); |
| 90 | } |
| 91 | |
| 92 | bool llvm::dxbc::isValidRootDesciptorFlags(uint32_t V) { |
| 93 | return isValidFlags<dxbc::RootDescriptorFlags>(V); |
| 94 | } |
| 95 | |
| 96 | bool llvm::dxbc::isValidDescriptorRangeFlags(uint32_t V) { |
| 97 | return isValidFlags<dxbc::DescriptorRangeFlags>(V); |
| 98 | } |
| 99 | |
| 100 | bool llvm::dxbc::isValidStaticSamplerFlags(uint32_t V) { |
| 101 | return isValidFlags<dxbc::StaticSamplerFlags>(V); |
| 102 | } |
| 103 | |
| 104 | bool llvm::dxbc::isValidCompilerVersionFlags(uint32_t V) { |
| 105 | return isValidFlags<dxbc::CompilerVersionFlags>(V); |
| 106 | } |
| 107 | |
| 108 | template <typename EnumT> |
| 109 | static bool isValidEnumValue(std::underlying_type_t<EnumT> V) { |
| 110 | decltype(V) LargestValue = |
| 111 | llvm::to_underlying(EnumT::LLVM_BITMASK_LARGEST_ENUMERATOR); |
| 112 | return V <= LargestValue; |
| 113 | } |
| 114 | |
| 115 | bool llvm::dxbc::SourceInfo::Contents::isValidCompressionType(uint16_t V) { |
| 116 | return isValidEnumValue<CompressionType>(V); |
| 117 | } |
| 118 | |
| 119 | bool SourceInfo::isValidSectionType(uint16_t V) { |
| 120 | return isValidEnumValue<SourceInfo::SectionType>(V); |
| 121 | } |
| 122 | |
| 123 | dxbc::PartType dxbc::parsePartType(StringRef S) { |
| 124 | #define CONTAINER_PART(PartName) .Case(#PartName, PartType::PartName) |
| 125 | return StringSwitch<dxbc::PartType>(S) |
| 126 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 127 | .Default(Value: dxbc::PartType::Unknown); |
| 128 | } |
| 129 | |
| 130 | bool dxbc::isDebugProgramPart(PartType PT) { return PT == PartType::ILDB; } |
| 131 | |
| 132 | const char *dxbc::getProgramPartName(bool IsDebug) { |
| 133 | return IsDebug ? "ILDB" : "DXIL" ; |
| 134 | } |
| 135 | |
| 136 | bool dxbc::isProgramPart(StringRef PartName) { |
| 137 | return PartName == "DXIL" || PartName == "ILDB" ; |
| 138 | } |
| 139 | |
| 140 | bool ShaderHash::isPopulated() { |
| 141 | static uint8_t Zeros[16] = {0}; |
| 142 | return Flags > 0 || 0 != memcmp(s1: &Digest, s2: &Zeros, n: 16); |
| 143 | } |
| 144 | |
| 145 | EnumStrings<SigMinPrecision> dxbc::getSigMinPrecisions() { |
| 146 | constexpr EnumStringDef<SigMinPrecision> SigMinPrecisionNameDefs[] = { |
| 147 | #define COMPONENT_PRECISION(Val, Enum) {{#Enum}, SigMinPrecision::Enum}, |
| 148 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 149 | }; |
| 150 | static constexpr auto SigMinPrecisionNames = |
| 151 | BUILD_ENUM_STRINGS(SigMinPrecisionNameDefs); |
| 152 | return EnumStrings(SigMinPrecisionNames); |
| 153 | } |
| 154 | |
| 155 | EnumStrings<D3DSystemValue> dxbc::getD3DSystemValues() { |
| 156 | constexpr EnumStringDef<D3DSystemValue> D3DSystemValueNameDefs[] = { |
| 157 | #define D3D_SYSTEM_VALUE(Val, Enum) {{#Enum}, D3DSystemValue::Enum}, |
| 158 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 159 | }; |
| 160 | static constexpr auto D3DSystemValueNames = |
| 161 | BUILD_ENUM_STRINGS(D3DSystemValueNameDefs); |
| 162 | return EnumStrings(D3DSystemValueNames); |
| 163 | } |
| 164 | |
| 165 | EnumStrings<SigComponentType> dxbc::getSigComponentTypes() { |
| 166 | constexpr EnumStringDef<SigComponentType> SigComponentTypeDefs[] = { |
| 167 | #define COMPONENT_TYPE(Val, Enum) {{#Enum}, SigComponentType::Enum}, |
| 168 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 169 | }; |
| 170 | static constexpr auto SigComponentTypes = |
| 171 | BUILD_ENUM_STRINGS(SigComponentTypeDefs); |
| 172 | return EnumStrings(SigComponentTypes); |
| 173 | } |
| 174 | |
| 175 | EnumStrings<RootFlags> dxbc::getRootFlags() { |
| 176 | constexpr EnumStringDef<RootFlags> RootFlagNameDefs[] = { |
| 177 | #define ROOT_SIGNATURE_FLAG(Val, Enum) {{#Enum}, RootFlags::Enum}, |
| 178 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 179 | }; |
| 180 | static constexpr auto RootFlagNames = BUILD_ENUM_STRINGS(RootFlagNameDefs); |
| 181 | return EnumStrings(RootFlagNames); |
| 182 | } |
| 183 | |
| 184 | EnumStrings<RootDescriptorFlags> dxbc::getRootDescriptorFlags() { |
| 185 | constexpr EnumStringDef<RootDescriptorFlags> RootDescriptorFlagNameDefs[] = { |
| 186 | #define ROOT_DESCRIPTOR_FLAG(Val, Enum, Flag) \ |
| 187 | {{#Enum}, RootDescriptorFlags::Enum}, |
| 188 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 189 | }; |
| 190 | static constexpr auto RootDescriptorFlagNames = |
| 191 | BUILD_ENUM_STRINGS(RootDescriptorFlagNameDefs); |
| 192 | return EnumStrings(RootDescriptorFlagNames); |
| 193 | } |
| 194 | |
| 195 | EnumStrings<DescriptorRangeFlags> dxbc::getDescriptorRangeFlags() { |
| 196 | constexpr EnumStringDef<DescriptorRangeFlags> DescriptorRangeFlagNameDefs[] = |
| 197 | { |
| 198 | #define DESCRIPTOR_RANGE_FLAG(Val, Enum, Flag) \ |
| 199 | {{#Enum}, DescriptorRangeFlags::Enum}, |
| 200 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 201 | }; |
| 202 | static constexpr auto DescriptorRangeFlagNames = |
| 203 | BUILD_ENUM_STRINGS(DescriptorRangeFlagNameDefs); |
| 204 | return EnumStrings(DescriptorRangeFlagNames); |
| 205 | } |
| 206 | |
| 207 | EnumStrings<StaticSamplerFlags> dxbc::getStaticSamplerFlags() { |
| 208 | constexpr EnumStringDef<StaticSamplerFlags> StaticSamplerFlagNameDefs[] = { |
| 209 | #define STATIC_SAMPLER_FLAG(Val, Enum, Flag) \ |
| 210 | {{#Enum}, StaticSamplerFlags::Enum}, |
| 211 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 212 | }; |
| 213 | static constexpr auto StaticSamplerFlagNames = |
| 214 | BUILD_ENUM_STRINGS(StaticSamplerFlagNameDefs); |
| 215 | return EnumStrings(StaticSamplerFlagNames); |
| 216 | } |
| 217 | |
| 218 | EnumStrings<ShaderVisibility> dxbc::getShaderVisibility() { |
| 219 | constexpr EnumStringDef<ShaderVisibility> ShaderVisibilityValueDefs[] = { |
| 220 | #define SHADER_VISIBILITY(Val, Enum) {{#Enum}, ShaderVisibility::Enum}, |
| 221 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 222 | }; |
| 223 | static constexpr auto ShaderVisibilityValues = |
| 224 | BUILD_ENUM_STRINGS(ShaderVisibilityValueDefs); |
| 225 | return EnumStrings(ShaderVisibilityValues); |
| 226 | } |
| 227 | |
| 228 | EnumStrings<SamplerFilter> dxbc::getSamplerFilters() { |
| 229 | constexpr EnumStringDef<SamplerFilter> SamplerFilterNameDefs[] = { |
| 230 | #define FILTER(Val, Enum) {{#Enum}, SamplerFilter::Enum}, |
| 231 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 232 | }; |
| 233 | static constexpr auto SamplerFilterNames = |
| 234 | BUILD_ENUM_STRINGS(SamplerFilterNameDefs); |
| 235 | return EnumStrings(SamplerFilterNames); |
| 236 | } |
| 237 | |
| 238 | EnumStrings<TextureAddressMode> dxbc::getTextureAddressModes() { |
| 239 | constexpr EnumStringDef<TextureAddressMode> TextureAddressModeNameDefs[] = { |
| 240 | #define TEXTURE_ADDRESS_MODE(Val, Enum) {{#Enum}, TextureAddressMode::Enum}, |
| 241 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 242 | }; |
| 243 | static constexpr auto TextureAddressModeNames = |
| 244 | BUILD_ENUM_STRINGS(TextureAddressModeNameDefs); |
| 245 | return EnumStrings(TextureAddressModeNames); |
| 246 | } |
| 247 | |
| 248 | EnumStrings<ComparisonFunc> dxbc::getComparisonFuncs() { |
| 249 | constexpr EnumStringDef<ComparisonFunc> ComparisonFuncNameDefs[] = { |
| 250 | #define COMPARISON_FUNC(Val, Enum) {{#Enum}, ComparisonFunc::Enum}, |
| 251 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 252 | }; |
| 253 | static constexpr auto ComparisonFuncNames = |
| 254 | BUILD_ENUM_STRINGS(ComparisonFuncNameDefs); |
| 255 | return EnumStrings(ComparisonFuncNames); |
| 256 | } |
| 257 | |
| 258 | EnumStrings<StaticBorderColor> dxbc::getStaticBorderColors() { |
| 259 | constexpr EnumStringDef<StaticBorderColor> StaticBorderColorValueDefs[] = { |
| 260 | #define STATIC_BORDER_COLOR(Val, Enum) {{#Enum}, StaticBorderColor::Enum}, |
| 261 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 262 | }; |
| 263 | static constexpr auto StaticBorderColorValues = |
| 264 | BUILD_ENUM_STRINGS(StaticBorderColorValueDefs); |
| 265 | return EnumStrings(StaticBorderColorValues); |
| 266 | } |
| 267 | |
| 268 | EnumStrings<RootParameterType> dxbc::getRootParameterTypes() { |
| 269 | constexpr EnumStringDef<RootParameterType> RootParameterTypeDefs[] = { |
| 270 | #define ROOT_PARAMETER(Val, Enum) {{#Enum}, RootParameterType::Enum}, |
| 271 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 272 | }; |
| 273 | static constexpr auto RootParameterTypes = |
| 274 | BUILD_ENUM_STRINGS(RootParameterTypeDefs); |
| 275 | return EnumStrings(RootParameterTypes); |
| 276 | } |
| 277 | |
| 278 | EnumStrings<PSV::SemanticKind> PSV::getSemanticKinds() { |
| 279 | constexpr EnumStringDef<PSV::SemanticKind> SemanticKindNameDefs[] = { |
| 280 | #define SEMANTIC_KIND(Val, Enum) {{#Enum}, PSV::SemanticKind::Enum}, |
| 281 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 282 | }; |
| 283 | static constexpr auto SemanticKindNames = |
| 284 | BUILD_ENUM_STRINGS(SemanticKindNameDefs); |
| 285 | return EnumStrings(SemanticKindNames); |
| 286 | } |
| 287 | |
| 288 | EnumStrings<PSV::ComponentType> PSV::getComponentTypes() { |
| 289 | constexpr EnumStringDef<PSV::ComponentType> ComponentTypeNameDefs[] = { |
| 290 | #define COMPONENT_TYPE(Val, Enum) {{#Enum}, PSV::ComponentType::Enum}, |
| 291 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 292 | }; |
| 293 | static constexpr auto ComponentTypeNames = |
| 294 | BUILD_ENUM_STRINGS(ComponentTypeNameDefs); |
| 295 | return EnumStrings(ComponentTypeNames); |
| 296 | } |
| 297 | |
| 298 | EnumStrings<PSV::InterpolationMode> PSV::getInterpolationModes() { |
| 299 | constexpr EnumStringDef<PSV::InterpolationMode> InterpolationModeNameDefs[] = |
| 300 | { |
| 301 | #define INTERPOLATION_MODE(Val, Enum) {{#Enum}, PSV::InterpolationMode::Enum}, |
| 302 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 303 | }; |
| 304 | static constexpr auto InterpolationModeNames = |
| 305 | BUILD_ENUM_STRINGS(InterpolationModeNameDefs); |
| 306 | return EnumStrings(InterpolationModeNames); |
| 307 | } |
| 308 | |
| 309 | EnumStrings<PSV::ResourceType> PSV::getResourceTypes() { |
| 310 | constexpr EnumStringDef<PSV::ResourceType> ResourceTypeNameDefs[] = { |
| 311 | #define RESOURCE_TYPE(Val, Enum) {{#Enum}, PSV::ResourceType::Enum}, |
| 312 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 313 | }; |
| 314 | static constexpr auto ResourceTypeNames = |
| 315 | BUILD_ENUM_STRINGS(ResourceTypeNameDefs); |
| 316 | return EnumStrings(ResourceTypeNames); |
| 317 | } |
| 318 | |
| 319 | EnumStrings<PSV::ResourceKind> PSV::getResourceKinds() { |
| 320 | constexpr EnumStringDef<PSV::ResourceKind> ResourceKindNameDefs[] = { |
| 321 | #define RESOURCE_KIND(Val, Enum) {{#Enum}, PSV::ResourceKind::Enum}, |
| 322 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 323 | }; |
| 324 | static constexpr auto ResourceKindNames = |
| 325 | BUILD_ENUM_STRINGS(ResourceKindNameDefs); |
| 326 | return EnumStrings(ResourceKindNames); |
| 327 | } |
| 328 | |
| 329 | EnumStrings<SourceInfo::SectionType> SourceInfo::getSectionTypes() { |
| 330 | constexpr EnumStringDef<SectionType> SectionNameDefs[] = { |
| 331 | #define SOURCE_INFO_TYPE(Num, Val) {{#Val}, SourceInfo::SectionType::Val}, |
| 332 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 333 | }; |
| 334 | static constexpr auto SectionNames = BUILD_ENUM_STRINGS(SectionNameDefs); |
| 335 | return EnumStrings(SectionNames); |
| 336 | } |
| 337 | |
| 338 | StringRef SourceInfo::getSectionName(SourceInfo::SectionType Type) { |
| 339 | auto V = to_underlying(E: Type); |
| 340 | if (!isValidSectionType(V)) |
| 341 | return StringRef(); |
| 342 | return getSectionTypes()[V].name(); |
| 343 | } |
| 344 | |
| 345 | EnumStrings<SourceInfo::Contents::CompressionType> |
| 346 | SourceInfo::Contents::getCompressionTypes() { |
| 347 | constexpr EnumStringDef<CompressionType> CompressionTypeDefs[] = { |
| 348 | #define COMPRESSION_TYPE(Num, Val) \ |
| 349 | {{#Val}, SourceInfo::Contents::CompressionType::Val}, |
| 350 | #include "llvm/BinaryFormat/DXContainerConstants.def" |
| 351 | }; |
| 352 | static constexpr auto CompressionTypes = |
| 353 | BUILD_ENUM_STRINGS(CompressionTypeDefs); |
| 354 | return EnumStrings(CompressionTypes); |
| 355 | } |
| 356 | |