|
| 1 | +#include "slang-ir-entry-point-decorations.h" |
| 2 | + |
| 3 | +#include "compiler-core/slang-diagnostic-sink.h" |
| 4 | +#include "core/slang-signal.h" |
| 5 | +#include "core/slang-string.h" |
| 6 | +#include "core/slang-type-text-util.h" |
| 7 | +#include "slang-compiler.h" |
| 8 | +#include "slang-ir-insts.h" |
| 9 | +#include "slang-ir.h" |
| 10 | +#include "slang-options.h" |
| 11 | + |
| 12 | +namespace Slang |
| 13 | +{ |
| 14 | + |
| 15 | +class CheckEntryPointDecorationsContext |
| 16 | +{ |
| 17 | +public: |
| 18 | + CheckEntryPointDecorationsContext(IRModule* module, CodeGenTarget target, DiagnosticSink* sink) |
| 19 | + : m_module(module), m_target(target), m_sink(sink) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + void check() |
| 24 | + { |
| 25 | + for (auto inst : m_module->getGlobalInsts()) |
| 26 | + { |
| 27 | + const auto func = as<IRFunc>(inst); |
| 28 | + if (!func) |
| 29 | + continue; |
| 30 | + const auto entryPointDecoration = func->findDecoration<IREntryPointDecoration>(); |
| 31 | + if (!entryPointDecoration) |
| 32 | + continue; |
| 33 | + |
| 34 | + checkEntryPoint(func, entryPointDecoration->getProfile().getStage()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | +private: |
| 39 | + void checkEntryPoint(IRFunc* entryPoint, Stage stage) |
| 40 | + { |
| 41 | + for (auto decoration : entryPoint->getDecorations()) |
| 42 | + { |
| 43 | + if (auto outputTopologyDecoration = as<IROutputTopologyDecoration>(decoration)) |
| 44 | + { |
| 45 | + checkOutputTopologyDecoration(outputTopologyDecoration, stage); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void checkOutputTopologyDecoration(IROutputTopologyDecoration* decoration, Stage stage) |
| 51 | + { |
| 52 | + if (stage == Stage::Mesh) |
| 53 | + { |
| 54 | + const auto outputTopologyType = OutputTopologyType(decoration->getTopologyType()); |
| 55 | + if (isTargetGLSL() || isTargetSPIRV() || isTargetMetal()) |
| 56 | + { |
| 57 | + if (outputTopologyType != OutputTopologyType::Point && |
| 58 | + outputTopologyType != OutputTopologyType::Line && |
| 59 | + outputTopologyType != OutputTopologyType::Triangle) |
| 60 | + { |
| 61 | + diagnoseInvalidMeshStageOutputTopology( |
| 62 | + decoration, |
| 63 | + "'point', 'line', 'triangle'"); |
| 64 | + } |
| 65 | + } |
| 66 | + else if (isTargetHLSL()) |
| 67 | + { |
| 68 | + if (outputTopologyType != OutputTopologyType::Line && |
| 69 | + outputTopologyType != OutputTopologyType::Triangle) |
| 70 | + { |
| 71 | + diagnoseInvalidMeshStageOutputTopology(decoration, "'line', 'triangle'"); |
| 72 | + } |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + SLANG_UNEXPECTED("Invalid compilation target for mesh stage"); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void diagnoseInvalidMeshStageOutputTopology( |
| 82 | + IROutputTopologyDecoration* decoration, |
| 83 | + String validTopologies) |
| 84 | + { |
| 85 | + m_sink->diagnose( |
| 86 | + decoration, |
| 87 | + Diagnostics::invalidMeshStageOutputTopology, |
| 88 | + decoration->getTopology()->getStringSlice(), |
| 89 | + TypeTextUtil::getCompileTargetName(SlangCompileTarget(m_target)), |
| 90 | + validTopologies); |
| 91 | + } |
| 92 | + |
| 93 | + bool isTargetHLSL() const { return m_target == CodeGenTarget::HLSL; } |
| 94 | + |
| 95 | + bool isTargetGLSL() const { return m_target == CodeGenTarget::GLSL; } |
| 96 | + |
| 97 | + bool isTargetSPIRV() const |
| 98 | + { |
| 99 | + return m_target == CodeGenTarget::SPIRV || m_target == CodeGenTarget::SPIRVAssembly; |
| 100 | + } |
| 101 | + |
| 102 | + bool isTargetMetal() const |
| 103 | + { |
| 104 | + return m_target == CodeGenTarget::Metal || m_target == CodeGenTarget::MetalLib || |
| 105 | + m_target == CodeGenTarget::MetalLibAssembly; |
| 106 | + } |
| 107 | + |
| 108 | + IRModule* m_module; |
| 109 | + const CodeGenTarget m_target; |
| 110 | + DiagnosticSink* m_sink; |
| 111 | +}; |
| 112 | + |
| 113 | +void checkEntryPointDecorations(IRModule* module, CodeGenTarget target, DiagnosticSink* sink) |
| 114 | +{ |
| 115 | + CheckEntryPointDecorationsContext(module, target, sink).check(); |
| 116 | +} |
| 117 | + |
| 118 | +OutputTopologyType convertOutputTopologyStringToEnum(String rawOutputTopology) |
| 119 | +{ |
| 120 | + auto name = rawOutputTopology.toLower(); |
| 121 | + |
| 122 | + OutputTopologyType outputTopologyType = OutputTopologyType::Unknown; |
| 123 | + |
| 124 | +#define CASE(ID, NAME) \ |
| 125 | + if (name == String(#NAME).toLower()) \ |
| 126 | + { \ |
| 127 | + outputTopologyType = OutputTopologyType::ID; \ |
| 128 | + } \ |
| 129 | + else |
| 130 | + |
| 131 | + OUTPUT_TOPOLOGY_TYPES(CASE) |
| 132 | +#undef CASE |
| 133 | + { |
| 134 | + outputTopologyType = OutputTopologyType::Unknown; |
| 135 | + // no match |
| 136 | + } |
| 137 | + return outputTopologyType; |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace Slang |
0 commit comments