From d31de66753c3c3b8dbe1395ee05ec0dec9ab038c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Aug 2019 11:24:08 +0200 Subject: [PATCH] Added HlslKnownKeywords class --- .../Mappings/HlslKnownKeywords.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs new file mode 100644 index 00000000..2301017a --- /dev/null +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Text.RegularExpressions; + +namespace ComputeSharp.Shaders.Mappings +{ + /// + /// A that contains and maps known HLSL method names to common .NET method + /// + internal static class HlslKnownKeywords + { + /// + /// The mapping of supported known methods to HLSL methods + /// + private static readonly IReadOnlyCollection KnownKeywords = new[] + { + "asm", "asm_fragment", "cbuffer", "centroid", "column_major", + "compile", "discard", "dword", "export", "fxgroup", "groupshared", + "half", "inline", "inout", "line", "lineadj", "linear", "matrix", + "nointerpolation", "noperspective", "NULL", "packoffset", "pass", + "pixelfragment", "point", "precise", "register", "row_major", "sample", + "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer", + "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm", + "unsigned", "vector", "vertexfragment" + }; + + private static Regex _KeywordsRegex; + + /// + /// The to use to match variable names + /// + private static Regex KeywordsRegex + { + get + { + if (_KeywordsRegex == null) + { + string + keywords = string.Join("|", KnownKeywords), + pattern = $@"(? + /// Gets the mapped HLSL-compatible variable name for the input identifier name + /// + /// The input variable name to map + /// The HLSL-compatible variable name name that can be used in an HLSL shader + [Pure] + public static string GetMappedName(string name) => KeywordsRegex.IsMatch(name) ? $"_{name}" : name; + } +}