From b7de92c1c0788a798ed10fd29c7b7c31c4d74d75 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 23:36:12 +0200 Subject: [PATCH 1/4] Added field type to template models --- .../Models/Fields/Abstract/HlslBufferInfo.cs | 9 ++++--- .../Models/Fields/CapturedFieldInfo.cs | 27 ++++++++++++------- .../Models/Fields/ConstantBufferFieldInfo.cs | 9 ++++--- .../Models/Fields/ReadOnlyBufferFieldInfo.cs | 9 ++++--- .../Models/Fields/ReadWriteBufferFieldInfo.cs | 9 ++++--- .../Translation/ShaderLoader.cs | 8 +++--- 6 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs index cc41114a..85eaee9c 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract +using System; + +namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract { /// /// A that contains info on a shader buffer field @@ -13,10 +15,11 @@ /// /// Creates a new instance with the specified parameters /// - /// The type of the current field + /// The type of the current field in the C# source + /// The type of the current field in the HLSL shader /// The name of the current field /// The index of the current buffer field - protected HlslBufferInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName) + protected HlslBufferInfo(Type fieldCsharpType, string fieldHlslType, string fieldName, int bufferIndex) : base(fieldCsharpType, fieldHlslType, fieldName) { BufferIndex = bufferIndex; } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs index a037c8b4..ba1f4092 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs @@ -1,4 +1,6 @@ -namespace ComputeSharp.Shaders.Renderer.Models.Fields +using System; + +namespace ComputeSharp.Shaders.Renderer.Models.Fields { /// /// A that contains info on a shader field @@ -6,23 +8,30 @@ internal class CapturedFieldInfo { /// - /// Gets or sets the type of the current field + /// Gets the type of the current field in the C# source /// - public string FieldType { get; set; } + public Type FieldCsharpType { get; } /// - /// Gets or sets the name to use for the current field + /// Gets the type of the current field in the HLSL shader /// - public string FieldName { get; set; } + public string FieldHlslType { get; } + + /// + /// Gets the name to use for the current field + /// + public string FieldName { get; } /// /// Creates a new instance with the specified parameters /// - /// The type of the current field - /// The name of the current field - public CapturedFieldInfo(string fieldType, string fieldName) + /// The type of the current field in the C# source + /// The type of the current field in the HLSL shader + /// The name to use for the current field + public CapturedFieldInfo(Type fieldCsharpType, string fieldHlslType, string fieldName) { - FieldType = fieldType; + FieldCsharpType = fieldCsharpType; + FieldHlslType = fieldHlslType; FieldName = fieldName; } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index 5dff2be0..a2554735 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; +using System; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; namespace ComputeSharp.Shaders.Renderer.Models.Fields { @@ -15,9 +16,11 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// Creates a new instance with the specified parameters /// - /// The type of the current field + /// The type of the current field in the C# source + /// The type of the current field in the HLSL shader /// The name of the current field /// The index of the current constant buffer field - public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } + public ConstantBufferFieldInfo(Type fieldCsharpType, string fieldHlslType, string fieldName, int bufferIndex) + : base(fieldCsharpType, fieldHlslType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs index 6bba7e0a..b5ecb59c 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; +using System; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; namespace ComputeSharp.Shaders.Renderer.Models.Fields { @@ -15,9 +16,11 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// Creates a new instance with the specified parameters /// - /// The type of the current field + /// The type of the current field in the C# source + /// The type of the current field in the HLSL shader /// The name of the current field /// The index of the current readonly buffer field - public ReadOnlyBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } + public ReadOnlyBufferFieldInfo(Type fieldCsharpType, string fieldHlslType, string fieldName, int bufferIndex) + : base(fieldCsharpType, fieldHlslType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs index 5dbf8d02..86c04a47 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; +using System; +using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; namespace ComputeSharp.Shaders.Renderer.Models.Fields { @@ -15,9 +16,11 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// Creates a new instance with the specified parameters /// - /// The type of the current field + /// The type of the current field in the C# source + /// The type of the current field in the HLSL shader /// The name of the current field /// The index of the current read write buffer field - public ReadWriteBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } + public ReadWriteBufferFieldInfo(Type fieldCsharpType, string fieldHlslType, string fieldName, int bufferIndex) + : base(fieldCsharpType, fieldHlslType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 14c8395a..0d456606 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -139,7 +139,7 @@ namespace ComputeSharp.Shaders.Translation _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); - _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++)); + _BuffersList.Add(new ConstantBufferFieldInfo(fieldType, typeName, fieldName, constantBuffersCount++)); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) { @@ -150,7 +150,7 @@ namespace ComputeSharp.Shaders.Translation _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); - _BuffersList.Add(new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++)); + _BuffersList.Add(new ReadOnlyBufferFieldInfo(fieldType, typeName, fieldName, readOnlyBuffersCount++)); } else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) { @@ -161,14 +161,14 @@ namespace ComputeSharp.Shaders.Translation _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); - _BuffersList.Add(new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++)); + _BuffersList.Add(new ReadWriteBufferFieldInfo(fieldType, typeName, fieldName, readWriteBuffersCount++)); } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { // Register the captured field _VariableFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); - _FieldsList.Add(new CapturedFieldInfo(typeName, fieldName)); + _FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName)); } else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); } From b6eca71e2dc1e1f1d5f4920cbafe216c65102c5d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 23:36:23 +0200 Subject: [PATCH 2/4] Updated mustache template to show original C# types --- .../Renderer/Models/ShaderInfo.cs | 5 ----- .../Renderer/Templates/ShaderTemplate.mustache | 14 +++++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index 9b3c202f..8dc33e8b 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -11,11 +11,6 @@ namespace ComputeSharp.Shaders.Renderer.Models /// internal sealed class ShaderInfo { - /// - /// Gets the text indicating a generic type, which can't be escaped in mustache - /// - public string T { get; } = ""; - /// /// Gets the list of captured buffers being present in the current shader /// diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index d6b563b8..037aa4da 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -13,30 +13,30 @@ cbuffer __Variables__Buffer : register(b0) uint __y__; // Target Y iterations uint __z__; // Target Z iterations {{#FieldsList}} - {{&FieldType}} {{FieldName}}; + {{&FieldHlslType}} {{FieldName}}; {{/FieldsList}} } {{#BuffersList}} {{#IsConstantBuffer}} {{!Constant buffer}} -// Captured ConstantBuffer{{&T}} buffer "{{FieldName}}" +// Captured {{&FieldCsharpType}} buffer "{{FieldName}}" cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) { - {{&FieldType}} {{FieldName}}[2]; + {{&FieldHlslType}} {{FieldName}}[2]; } {{/IsConstantBuffer}} {{#IsReadOnlyBuffer}} {{!Readonly buffer}} -// Captured ReadOnlyBuffer{{&T}} buffer "{{FieldName}}" -{{&FieldType}} {{FieldName}} : register(t{{BufferIndex}}); +// Captured ReadOnlyBuffer{{&FieldCsharpType}} buffer "{{FieldName}}" +{{&FieldHlslType}} {{FieldName}} : register(t{{BufferIndex}}); {{/IsReadOnlyBuffer}} {{#IsReadWriteBuffer}} {{!Read write buffer}} -// Captured ReadWriteBuffer{{&T}} buffer "{{FieldName}}" -{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); +// Captured ReadWriteBuffer{{&FieldCsharpType}} buffer "{{FieldName}}" +{{&FieldHlslType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); {{/IsReadWriteBuffer}} {{/BuffersList}} From 035ce5f62792374e985b7103f00b3f52daa366e2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 23:53:32 +0200 Subject: [PATCH 3/4] Improved type display in comments in the HLSL shader --- .../Extensions/TypeExtensions.cs | 50 +++++++++++++++++++ .../Models/Fields/CapturedFieldInfo.cs | 5 +- 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs diff --git a/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs new file mode 100644 index 00000000..852dd8b1 --- /dev/null +++ b/src/ComputeSharp.Shaders/Extensions/TypeExtensions.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Linq; + +namespace ComputeSharp.Shaders.Extensions +{ + /// + /// A that provides extension methods for the type + /// + internal static class TypeExtensions + { + /// + /// The mapping of supported known types to display string + /// + private static readonly IReadOnlyDictionary KnownTypes = new Dictionary + { + [typeof(bool)] = "bool", + [typeof(int)] = "int", + [typeof(uint)] = "uint", + [typeof(float)] = "float", + [typeof(double)] = "double" + }; + + /// + /// Gets a friendly display for the input type + /// + /// The input type to analyze + [Pure] + public static string ToFriendlyString(this Type type) + { + // Array types of arbitrary rank + if (type.IsArray) + { + int rank = type.GetArrayRank(); + return $"{type.GetElementType().ToFriendlyString()}[{new string(Enumerable.Repeat(',', rank - 1).ToArray())}]"; + } + + // Non generic types + string name; + if (type.GetGenericArguments().Length == 0) return KnownTypes.TryGetValue(type, out name) ? name : type.Name; + + // Generic types + Type[] genericArguments = type.GetGenericArguments(); + string typeDefeninition = KnownTypes.TryGetValue(type, out name) ? name : type.Name; + string polishedName = typeDefeninition.Substring(0, typeDefeninition.IndexOf("`")); + return $"{polishedName}<{string.Join(",", genericArguments.Select(ToFriendlyString))}>"; + } + } +} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs index ba1f4092..b0226ac3 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs @@ -1,4 +1,5 @@ using System; +using ComputeSharp.Shaders.Extensions; namespace ComputeSharp.Shaders.Renderer.Models.Fields { @@ -10,7 +11,7 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// Gets the type of the current field in the C# source /// - public Type FieldCsharpType { get; } + public string FieldCsharpType { get; } /// /// Gets the type of the current field in the HLSL shader @@ -30,7 +31,7 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// The name to use for the current field public CapturedFieldInfo(Type fieldCsharpType, string fieldHlslType, string fieldName) { - FieldCsharpType = fieldCsharpType; + FieldCsharpType = fieldCsharpType.ToFriendlyString(); FieldHlslType = fieldHlslType; FieldName = fieldName; } From 82d8ab910e37e15dc97312fe398e3a0badc04876 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 23:57:42 +0200 Subject: [PATCH 4/4] Mustache template tweaked --- .../Renderer/Templates/ShaderTemplate.mustache | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index 037aa4da..ee2a34a3 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -6,8 +6,8 @@ * For info or issues: https://github.com/Sergio0694/ComputeSharp */ {{!Captured variables buffer}} -// Captured scalar/vector variables -cbuffer __Variables__Buffer : register(b0) +// Scalar/vector variables +cbuffer _ : register(b0) { uint __x__; // Target X iterations uint __y__; // Target Y iterations @@ -20,8 +20,8 @@ cbuffer __Variables__Buffer : register(b0) {{#IsConstantBuffer}} {{!Constant buffer}} -// Captured {{&FieldCsharpType}} buffer "{{FieldName}}" -cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) +// {{&FieldCsharpType}} buffer "{{FieldName}}" +cbuffer _{{FieldName}} : register(b{{BufferIndex}}) { {{&FieldHlslType}} {{FieldName}}[2]; } @@ -29,13 +29,13 @@ cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) {{#IsReadOnlyBuffer}} {{!Readonly buffer}} -// Captured ReadOnlyBuffer{{&FieldCsharpType}} buffer "{{FieldName}}" +// {{&FieldCsharpType}} buffer "{{FieldName}}" {{&FieldHlslType}} {{FieldName}} : register(t{{BufferIndex}}); {{/IsReadOnlyBuffer}} {{#IsReadWriteBuffer}} {{!Read write buffer}} -// Captured ReadWriteBuffer{{&FieldCsharpType}} buffer "{{FieldName}}" +// {{&FieldCsharpType}} buffer "{{FieldName}}" {{&FieldHlslType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); {{/IsReadWriteBuffer}} {{/BuffersList}}