|
| 1 | +//===- HLSLRootSignature.cpp - HLSL Root Signature helper objects ----------===// |
| 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 This file contains helpers for working with HLSL Root Signatures. |
| 10 | +/// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/Frontend/HLSL/HLSLRootSignature.h" |
| 14 | +#include "llvm/IR/IRBuilder.h" |
| 15 | +#include "llvm/IR/Metadata.h" |
| 16 | +#include "llvm/IR/Module.h" |
| 17 | + |
| 18 | +namespace llvm { |
| 19 | +namespace hlsl { |
| 20 | +namespace root_signature { |
| 21 | + |
| 22 | +static MDString *ClauseTypeToName(LLVMContext &Ctx, ClauseType Type) { |
| 23 | + StringRef Name; |
| 24 | + switch (Type) { |
| 25 | + case ClauseType::CBuffer: |
| 26 | + Name = "CBV"; |
| 27 | + break; |
| 28 | + case ClauseType::SRV: |
| 29 | + Name = "SRV"; |
| 30 | + break; |
| 31 | + case ClauseType::UAV: |
| 32 | + Name = "UAV"; |
| 33 | + break; |
| 34 | + case ClauseType::Sampler: |
| 35 | + Name = "Sampler"; |
| 36 | + break; |
| 37 | + } |
| 38 | + return MDString::get(Ctx, Name); |
| 39 | +} |
| 40 | + |
| 41 | +// Helper struct so that we can use the overloaded notation of std::visit |
| 42 | +template <class... Ts> struct OverloadBuilds : Ts... { |
| 43 | + using Ts::operator()...; |
| 44 | +}; |
| 45 | +template <class... Ts> OverloadBuilds(Ts...) -> OverloadBuilds<Ts...>; |
| 46 | + |
| 47 | +MDNode *MetadataBuilder::BuildRootSignature() { |
| 48 | + for (const RootElement &Element : Elements) { |
| 49 | + MDNode *ElementMD = |
| 50 | + std::visit( |
| 51 | + OverloadBuilds{ |
| 52 | + [&](DescriptorTable Table) -> MDNode * { |
| 53 | + return BuildDescriptorTable(Table); |
| 54 | + }, |
| 55 | + [&](DescriptorTableClause Clause) -> MDNode * { |
| 56 | + return BuildDescriptorTableClause(Clause); |
| 57 | + }, |
| 58 | + }, |
| 59 | + Element); |
| 60 | + GeneratedMetadata.push_back(ElementMD); |
| 61 | + } |
| 62 | + |
| 63 | + return MDNode::get(Ctx, GeneratedMetadata); |
| 64 | +} |
| 65 | + |
| 66 | +MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) { |
| 67 | + return MDNode::get(Ctx, {MDString::get(Ctx, "DescriptorTable")}); |
| 68 | +} |
| 69 | + |
| 70 | +MDNode *MetadataBuilder::BuildDescriptorTableClause(const DescriptorTableClause &Clause) { |
| 71 | + return MDNode::get(Ctx, {ClauseTypeToName(Ctx, Clause.Type)}); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace root_signature |
| 75 | +} // namespace hlsl |
| 76 | +} // namespace llvm |
| 77 | + |
0 commit comments