|
| 1 | +#include "clang/Sema/ParseHLSLRootSignature.h" |
| 2 | + |
| 3 | +namespace llvm { |
| 4 | +namespace hlsl { |
| 5 | +namespace root_signature { |
| 6 | + |
| 7 | +// TODO: Hook up with Sema to properly report semantic/validation errors |
| 8 | +bool Parser::ReportError() { return true; } |
| 9 | + |
| 10 | +bool Parser::ParseRootFlags() { |
| 11 | + // Set to RootFlags::None and skip whitespace to catch when we have RootFlags( |
| 12 | + // ) |
| 13 | + RootFlags Flags = RootFlags::None; |
| 14 | + Buffer = Buffer.drop_while(isspace); |
| 15 | + StringLiteral Prefix = ""; |
| 16 | + |
| 17 | + // Loop until we reach the end of the rootflags |
| 18 | + while (!Buffer.starts_with(")")) { |
| 19 | + // Trim expected | when more than 1 flag |
| 20 | + if (!Buffer.consume_front(Prefix)) |
| 21 | + return ReportError(); |
| 22 | + Prefix = "|"; |
| 23 | + |
| 24 | + // Remove any whitespace |
| 25 | + Buffer = Buffer.drop_while(isspace); |
| 26 | + |
| 27 | + RootFlags CurFlag; |
| 28 | + if (ParseRootFlag(CurFlag)) |
| 29 | + return ReportError(); |
| 30 | + Flags |= CurFlag; |
| 31 | + |
| 32 | + // Remove any whitespace |
| 33 | + Buffer = Buffer.drop_while(isspace); |
| 34 | + } |
| 35 | + |
| 36 | + // Create and push the root element on the parsed elements |
| 37 | + Elements->push_back(RootElement(Flags)); |
| 38 | + return false; |
| 39 | +} |
| 40 | + |
| 41 | +template <typename EnumType> |
| 42 | +bool Parser::ParseEnum(SmallVector<std::pair<StringLiteral, EnumType>> Mapping, |
| 43 | + EnumType &Enum) { |
| 44 | + // Retrieve enum |
| 45 | + Token = Buffer.take_while([](char C) { return isalnum(C) || C == '_'; }); |
| 46 | + Buffer = Buffer.drop_front(Token.size()); |
| 47 | + |
| 48 | + // Try to get the case-insensitive enum |
| 49 | + auto Switch = llvm::StringSwitch<std::optional<EnumType>>(Token); |
| 50 | + for (auto Pair : Mapping) |
| 51 | + Switch.CaseLower(Pair.first, Pair.second); |
| 52 | + auto MaybeEnum = Switch.Default(std::nullopt); |
| 53 | + if (!MaybeEnum) |
| 54 | + return true; |
| 55 | + Enum = *MaybeEnum; |
| 56 | + |
| 57 | + return false; |
| 58 | +} |
| 59 | + |
| 60 | +bool Parser::ParseRootFlag(RootFlags &Flag) { |
| 61 | + SmallVector<std::pair<StringLiteral, RootFlags>> Mapping = { |
| 62 | + {"0", RootFlags::None}, |
| 63 | + {"ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT", |
| 64 | + RootFlags::AllowInputAssemblerInputLayout}, |
| 65 | + {"DENY_VERTEX_SHADER_ROOT_ACCESS", RootFlags::DenyVertexShaderRootAccess}, |
| 66 | + {"DENY_HULL_SHADER_ROOT_ACCESS", RootFlags::DenyHullShaderRootAccess}, |
| 67 | + {"DENY_DOMAIN_SHADER_ROOT_ACCESS", RootFlags::DenyDomainShaderRootAccess}, |
| 68 | + {"DENY_GEOMETRY_SHADER_ROOT_ACCESS", |
| 69 | + RootFlags::DenyGeometryShaderRootAccess}, |
| 70 | + {"DENY_PIXEL_SHADER_ROOT_ACCESS", RootFlags::DenyPixelShaderRootAccess}, |
| 71 | + {"ALLOW_STREAM_OUTPUT", RootFlags::AllowStreamOutput}, |
| 72 | + {"LOCAL_ROOT_SIGNATURE", RootFlags::LocalRootSignature}, |
| 73 | + {"DENY_AMPLIFICATION_SHADER_ROOT_ACCESS", |
| 74 | + RootFlags::DenyAmplificationShaderRootAccess}, |
| 75 | + {"DENY_MESH_SHADER_ROOT_ACCESS", RootFlags::DenyMeshShaderRootAccess}, |
| 76 | + {"CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED", |
| 77 | + RootFlags::CBVSRVUAVHeapDirectlyIndexed}, |
| 78 | + {"SAMPLER_HEAP_DIRECTLY_INDEXED", RootFlags::SamplerHeapDirectlyIndexed}, |
| 79 | + {"AllowLowTierReservedHwCbLimit", |
| 80 | + RootFlags::AllowLowTierReservedHwCbLimit}, |
| 81 | + }; |
| 82 | + |
| 83 | + return ParseEnum<RootFlags>(Mapping, Flag); |
| 84 | +} |
| 85 | + |
| 86 | +bool Parser::ParseRootElement() { |
| 87 | + // Define different ParserMethods to use StringSwitch for dispatch |
| 88 | + enum class ParserMethod { |
| 89 | + ReportError, |
| 90 | + ParseRootFlags, |
| 91 | + }; |
| 92 | + |
| 93 | + // Retreive which method should be used |
| 94 | + auto Method = llvm::StringSwitch<ParserMethod>(Token) |
| 95 | + .Case("RootFlags", ParserMethod::ParseRootFlags) |
| 96 | + .Default(ParserMethod::ReportError); |
| 97 | + |
| 98 | + // Dispatch on the correct method |
| 99 | + switch (Method) { |
| 100 | + case ParserMethod::ReportError: |
| 101 | + return ReportError(); |
| 102 | + case ParserMethod::ParseRootFlags: |
| 103 | + return ParseRootFlags(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Parser entry point function |
| 108 | +bool Parser::Parse() { |
| 109 | + StringLiteral Prefix = ""; |
| 110 | + while (!Buffer.empty()) { |
| 111 | + // Trim expected comma when more than 1 root element |
| 112 | + if (!Buffer.consume_front(Prefix)) |
| 113 | + return ReportError(); |
| 114 | + Prefix = ","; |
| 115 | + |
| 116 | + // Remove any whitespace |
| 117 | + Buffer = Buffer.drop_while(isspace); |
| 118 | + |
| 119 | + // Retrieve the root element identifier |
| 120 | + auto Split = Buffer.split('('); |
| 121 | + Token = Split.first; |
| 122 | + Buffer = Split.second; |
| 123 | + |
| 124 | + // Dispatch to the applicable root element parser |
| 125 | + if (ParseRootElement()) |
| 126 | + return true; |
| 127 | + |
| 128 | + // Then we can clean up the remaining ")" |
| 129 | + if (!Buffer.consume_front(")")) |
| 130 | + return ReportError(); |
| 131 | + } |
| 132 | + |
| 133 | + // All input has been correctly parsed |
| 134 | + return false; |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace root_signature |
| 138 | +} // namespace hlsl |
| 139 | +} // namespace llvm |
0 commit comments