From ce8a2e49af692d3114f61a08b4cd11f7cea573a3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Aug 2019 11:31:11 +0200 Subject: [PATCH] Implemented name replacement for reserved keywords --- src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs | 10 +++++++++- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs index 2301017a..32f85dad 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownKeywords.cs @@ -21,7 +21,7 @@ namespace ComputeSharp.Shaders.Mappings "pixelfragment", "point", "precise", "register", "row_major", "sample", "sampler", "shared", "snorm", "stateblock", "stateblock_state", "tbuffer", "technique", "typedef", "triangle", "triangleadj", "uniform", "unorm", - "unsigned", "vector", "vertexfragment" + "unsigned", "vector", "vertexfragment", "zero" }; private static Regex _KeywordsRegex; @@ -52,5 +52,13 @@ namespace ComputeSharp.Shaders.Mappings /// 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; + + /// + /// Processes an input text and replaces all the occurrences of reserved keywords + /// + /// The input text to process + /// A valid shader body source code that can be used in HLSL + [Pure] + public static string GetMappedText(string text) => KeywordsRegex.Replace(text, m => $"_{m.Value}"); } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index c64254e7..669f05c2 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -157,7 +157,7 @@ namespace ComputeSharp.Shaders.Translation private void LoadFieldInfo(ReadableMember memberInfo, string? name = null) { Type fieldType = memberInfo.MemberType; - string fieldName = name ?? memberInfo.Name; + string fieldName = HlslKnownKeywords.GetMappedName(name ?? memberInfo.Name); // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) @@ -226,7 +226,7 @@ namespace ComputeSharp.Shaders.Translation // Additional preprocessing MethodBody = Regex.Replace(MethodBody, @"(?<=\W)(\d+)[fFdD]", m => m.Groups[1].Value); MethodBody = MethodBody.TrimEnd('\n', '\r', ' '); - MethodBody = Regex.Replace(MethodBody, @"(?