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/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..b0226ac3 100644
--- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs
+++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/CapturedFieldInfo.cs
@@ -1,4 +1,7 @@
-namespace ComputeSharp.Shaders.Renderer.Models.Fields
+using System;
+using ComputeSharp.Shaders.Extensions;
+
+namespace ComputeSharp.Shaders.Renderer.Models.Fields
{
///
/// A that contains info on a shader field
@@ -6,23 +9,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 string 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.ToFriendlyString();
+ 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/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..ee2a34a3 100644
--- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache
+++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache
@@ -6,37 +6,37 @@
* 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
uint __z__; // Target Z iterations
{{#FieldsList}}
- {{&FieldType}} {{FieldName}};
+ {{&FieldHlslType}} {{FieldName}};
{{/FieldsList}}
}
{{#BuffersList}}
{{#IsConstantBuffer}}
{{!Constant buffer}}
-// Captured ConstantBuffer{{&T}} buffer "{{FieldName}}"
-cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}})
+// {{&FieldCsharpType}} buffer "{{FieldName}}"
+cbuffer _{{FieldName}} : register(b{{BufferIndex}})
{
- {{&FieldType}} {{FieldName}}[2];
+ {{&FieldHlslType}} {{FieldName}}[2];
}
{{/IsConstantBuffer}}
{{#IsReadOnlyBuffer}}
{{!Readonly buffer}}
-// Captured ReadOnlyBuffer{{&T}} buffer "{{FieldName}}"
-{{&FieldType}} {{FieldName}} : register(t{{BufferIndex}});
+// {{&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}});
+// {{&FieldCsharpType}} buffer "{{FieldName}}"
+{{&FieldHlslType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}});
{{/IsReadWriteBuffer}}
{{/BuffersList}}
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}");
}