From a2e8eef12655da7351f5fc003339a7a930fb876a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 15 Aug 2019 18:38:26 +0200 Subject: [PATCH] Code refactoring in the field info models --- .../{FieldInfoBase.cs => FieldInfo.cs} | 6 ++--- .../Models/Fields/Abstract/HlslBufferInfo.cs | 24 +++++++++++++++++++ .../Models/Fields/ConstantBufferFieldInfo.cs | 12 +++------- .../Models/Fields/ReadOnlyBufferFieldInfo.cs | 14 +++-------- .../Models/Fields/ReadWriteBufferFieldInfo.cs | 14 +++-------- .../Models/Fields/StaticScalarFieldInfo.cs | 4 ++-- .../Renderer/Models/ShaderInfo.cs | 2 +- .../Templates/ShaderTemplate.mustache | 4 ++-- .../Translation/ShaderLoader.cs | 13 +++++----- 9 files changed, 48 insertions(+), 45 deletions(-) rename src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/{FieldInfoBase.cs => FieldInfo.cs} (79%) create mode 100644 src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs similarity index 79% rename from src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs rename to src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs index 93cff5c0..8c60411e 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfoBase.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/FieldInfo.cs @@ -3,7 +3,7 @@ /// /// A that contains info on a shader field /// - internal abstract class FieldInfoBase + internal abstract class FieldInfo { /// /// Gets or sets the type of the current field @@ -16,11 +16,11 @@ public string FieldName { get; set; } /// - /// Creates a new instance with the specified parameters + /// Creates a new instance with the specified parameters /// /// The type of the current field /// The name of the current field - protected FieldInfoBase(string fieldType, string fieldName) + protected FieldInfo(string fieldType, string fieldName) { FieldType = fieldType; FieldName = fieldName; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs new file mode 100644 index 00000000..e5c0044e --- /dev/null +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/Abstract/HlslBufferInfo.cs @@ -0,0 +1,24 @@ +namespace ComputeSharp.Shaders.Renderer.Models.Fields.Abstract +{ + /// + /// A that contains info on a shader buffer field + /// + internal abstract class HlslBufferInfo : FieldInfo + { + /// + /// Gets or sets the index of the current buffer, relative to its own type + /// + public int BufferIndex { get; set; } + + /// + /// Creates a new instance with the specified parameters + /// + /// The type of the current field + /// The name of the current field + /// The index of the current buffer field + protected HlslBufferInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName) + { + BufferIndex = bufferIndex; + } + } +} diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs index d79f85c4..ca47d290 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ConstantBufferFieldInfo.cs @@ -5,10 +5,10 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// A that contains info on a constant buffer field /// - internal sealed class ConstantBufferFieldInfo : FieldInfoBase + internal sealed class ConstantBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a constant buffer (always ) + /// Gets whether or not the current instance represents a constant buffer (always ) /// public bool IsConstantBuffer { get; } = true; @@ -17,11 +17,6 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// public bool IsSingleValue { get; } - /// - /// Gets the index of the current constant buffer field - /// - public int ConstantBufferIndex { get; } - /// /// Creates a new instance with the specified parameters /// @@ -29,9 +24,8 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// The name of the current field /// The index of the current constant buffer field /// Whether or not this constant buffer actually contains a single, explicit variable - public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex, bool singleValue) : base(fieldType, fieldName) + public ConstantBufferFieldInfo(string fieldType, string fieldName, int bufferIndex, bool singleValue) : base(fieldType, fieldName, bufferIndex) { - ConstantBufferIndex = bufferIndex; IsSingleValue = singleValue; } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs index 87f6f4e1..a8d826ee 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadOnlyBufferFieldInfo.cs @@ -5,27 +5,19 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// A that contains info on a readonly buffer field /// - internal sealed class ReadOnlyBufferFieldInfo : FieldInfoBase + internal sealed class ReadOnlyBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a readonly buffer (always ) + /// Gets whether or not the current instance represents a readonly buffer (always ) /// public bool IsReadOnlyBuffer { get; } = true; - /// - /// Gets the index of the current constant buffer field - /// - public int ReadOnlyBufferIndex { get; } - /// /// Creates a new instance with the specified parameters /// /// The type of the current field /// 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) - { - ReadOnlyBufferIndex = bufferIndex; - } + public ReadOnlyBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs index e7a9193e..170260dc 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/ReadWriteBufferFieldInfo.cs @@ -5,27 +5,19 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// A that contains info on a read write buffer field /// - internal sealed class ReadWriteBufferFieldInfo : FieldInfoBase + internal sealed class ReadWriteBufferFieldInfo : HlslBufferInfo { /// - /// Gets whether or not the current instance represents a read write buffer (always ) + /// Gets whether or not the current instance represents a read write buffer (always ) /// public bool IsReadWriteBuffer { get; } = true; - /// - /// Gets the index of the current read write buffer field - /// - public int ReadWriteBufferIndex { get; set; } - /// /// Creates a new instance with the specified parameters /// /// The type of the current field /// 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) - { - ReadWriteBufferIndex = bufferIndex; - } + public ReadWriteBufferFieldInfo(string fieldType, string fieldName, int bufferIndex) : base(fieldType, fieldName, bufferIndex) { } } } diff --git a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs index e7d079ac..670abc20 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/Fields/StaticScalarFieldInfo.cs @@ -5,10 +5,10 @@ namespace ComputeSharp.Shaders.Renderer.Models.Fields /// /// A that contains info on a static scalar field /// - internal sealed class StaticScalarFieldInfo : FieldInfoBase + internal sealed class StaticScalarFieldInfo : FieldInfo { /// - /// Gets whether or not the current instance represents a static scalar value (always ) + /// Gets whether or not the current instance represents a static scalar value (always ) /// public bool IsStaticScalarField { get; } = true; diff --git a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs index f9961bfe..255c797a 100644 --- a/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs +++ b/src/ComputeSharp.Shaders/Renderer/Models/ShaderInfo.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.Shaders.Renderer.Models /// /// Gets the list of fields being present in the current shader /// - public IReadOnlyList FieldsList { get; set; } + public IReadOnlyList FieldsList { get; set; } /// /// Gets or sets the total number of threads to run on the X axis diff --git a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache index ef061a4b..664f32ce 100644 --- a/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache +++ b/src/ComputeSharp.Shaders/Renderer/Templates/ShaderTemplate.mustache @@ -5,14 +5,14 @@ static {{&FieldType}} {{FieldName}} = {{FieldValue}}; {{/IsStaticScalarField}} {{!Constant buffer}} {{#IsConstantBuffer}} -cbuffer {{FieldName}}Buffer : register(b{{ConstantBufferIndex}}) +cbuffer {{FieldName}}Buffer : register(b{{BufferIndex}}) { {{&FieldType}} {{FieldName}}{{^IsSingleValue}}[2]{{/IsSingleValue}}; } {{/IsConstantBuffer}} {{!Read write buffer}} {{#IsReadWriteBuffer}} -{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{ReadWriteBufferIndex}}); +{{&FieldType}} {{FieldName}} : SV_DispatchThreadId : register(u{{BufferIndex}}); {{/IsReadWriteBuffer}} {{/FieldsList}} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 404dc19d..58a1400c 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -11,6 +11,7 @@ using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using SharpDX.Direct3D12; +using FieldInfo = ComputeSharp.Shaders.Renderer.Models.Fields.Abstract.FieldInfo; #pragma warning disable CS8618 // Non-nullable field is uninitialized @@ -39,7 +40,7 @@ namespace ComputeSharp.Shaders.Translation /// /// The sequence of fields in the targeted closure /// - private readonly IReadOnlyList ShaderFields; + private readonly IReadOnlyList ShaderFields; /// /// Creates a new with the specified parameters @@ -68,12 +69,12 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList<(int Index, object Value)> CapturedConstantBufferValues { get; private set; } - private readonly List _FieldsInfo = new List(); + private readonly List _FieldsInfo = new List(); /// - /// Gets the collection of items for the shader fields + /// Gets the collection of items for the shader fields /// - public IReadOnlyList FieldsInfo => _FieldsInfo; + public IReadOnlyList FieldsInfo => _FieldsInfo; /// /// Gets the name of the variable used as input for the shader method @@ -116,12 +117,12 @@ namespace ComputeSharp.Shaders.Translation int readWriteBuffersCount = 0; int readOnlyBuffersCount = 0; - foreach (FieldInfo fieldInfo in ShaderFields) + foreach (System.Reflection.FieldInfo fieldInfo in ShaderFields) { Type fieldType = fieldInfo.FieldType; string fieldName = fieldInfo.Name; object fieldValue = fieldInfo.GetValue(ShaderInstance); - FieldInfoBase processedFieldInfo; + FieldInfo processedFieldInfo; // Read write buffer if (HlslKnownTypes.IsReadWriteBufferType(fieldType))