From ea1c2dd37b1aa930be9ffe7789939717307edcc0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 30 Nov 2020 00:12:07 +0100 Subject: [PATCH] More code refactoring --- .../Renderer/HlslShaderRenderer.cs | 20 ++++++---- src/ComputeSharp.Shaders/ShaderRunner{T}.cs | 14 +------ .../Translation/IShaderLoader.cs | 36 ++++++++++++++++++ .../Translation/ShaderLoader{T}.cs | 38 +++---------------- 4 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Translation/IShaderLoader.cs diff --git a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs index 3f95cc37..d411ba63 100644 --- a/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs +++ b/src/ComputeSharp.Shaders/Renderer/HlslShaderRenderer.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Shaders.Renderer.Models; +using ComputeSharp.Shaders.Translation; namespace ComputeSharp.Shaders.Renderer { @@ -15,10 +16,13 @@ namespace ComputeSharp.Shaders.Renderer /// /// Renders a new HLSL shader source with the given parameters /// - /// The input instance with the shader information + /// The number of threads in each thread group for the X axis. + /// The number of threads in each thread group for the Y axis. + /// The number of threads in each thread group for the Z axis. + /// The input instance with the shader information /// The source code for the new HLSL shader [Pure] - public static ArrayPoolStringBuilder Render(ShaderInfo info) + public static ArrayPoolStringBuilder Render(int threadsX, int threadsY, int threadsZ, IShaderLoader info) { var builder = ArrayPoolStringBuilder.Create(); @@ -44,7 +48,7 @@ namespace ComputeSharp.Shaders.Renderer builder.AppendLine(" uint __y;"); builder.AppendLine(" uint __z;"); - foreach (var local in info.FieldsList) + foreach (var local in info.FieldsInfo) { builder.Append(" "); builder.Append(local.FieldType); @@ -56,7 +60,7 @@ namespace ComputeSharp.Shaders.Renderer builder.AppendLine('}'); // Buffers - foreach (var buffer in info.BuffersList) + foreach (var buffer in info.HslsBuffersInfo) { builder.AppendLine(); @@ -97,7 +101,7 @@ namespace ComputeSharp.Shaders.Renderer } // Local functions - foreach (var function in info.LocalFunctionsList) + foreach (var function in info.LocalFunctionsInfo) { builder.AppendLine(); builder.AppendLine(function); @@ -106,11 +110,11 @@ namespace ComputeSharp.Shaders.Renderer // Entry point builder.AppendLine(); builder.Append("[NumThreads("); - builder.Append(info.NumThreadsX.ToString()); + builder.Append(threadsX.ToString()); builder.Append(", "); - builder.Append(info.NumThreadsY.ToString()); + builder.Append(threadsY.ToString()); builder.Append(", "); - builder.Append(info.NumThreadsZ.ToString()); + builder.Append(threadsZ.ToString()); builder.AppendLine(")]"); builder.AppendLine(info.EntryPoint); diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 81acf440..f2980817 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -168,19 +168,7 @@ namespace ComputeSharp.Shaders ShaderLoader shaderLoader = ShaderLoader.Load(in shader); // Render the loaded shader - using var shaderSource = HlslShaderRenderer.Render(new ShaderInfo - { - BuffersList = shaderLoader.HslsBuffersInfo, - FieldsList = shaderLoader.FieldsInfo, - NumThreadsX = threadsX, - NumThreadsY = threadsY, - NumThreadsZ = threadsZ, - ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName, - EntryPoint = shaderLoader.EntryPoint, - FunctionsList = shaderLoader.FunctionsInfo, - LocalFunctionsList = shaderLoader.LocalFunctionsInfo, - DeclaredTypes = shaderLoader.DeclaredTypes - }); + using var shaderSource = HlslShaderRenderer.Render(threadsX, threadsY, threadsZ, shaderLoader); // Compile the loaded shader to HLSL bytecode IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource.WrittenSpan)); diff --git a/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs new file mode 100644 index 00000000..08f75f95 --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/IShaderLoader.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using ComputeSharp.Shaders.Renderer.Models; + +namespace ComputeSharp.Shaders.Translation +{ + /// + /// A responsible for loading and processing shaders of any type. + /// + internal interface IShaderLoader + { + /// + /// Gets the generated source code for the method in the current shader. + /// + public string EntryPoint { get; } + + /// + /// Gets the collection of items for the shader fields. + /// + public IReadOnlyList HslsBuffersInfo { get; } + + /// + /// Gets the collection of items for the shader fields. + /// + public IReadOnlyList FieldsInfo { get; } + + /// + /// Gets the collection of items for the shader. + /// + public IReadOnlyCollection LocalFunctionsInfo { get; } + + /// + /// Gets the collection of declared types for the shader. + /// + public IReadOnlyCollection DeclaredTypes { get; } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index cadbf629..08ea6d57 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Runtime.InteropServices; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models; -using ComputeSharp.Shaders.Translation.Models; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE; @@ -19,7 +18,7 @@ namespace ComputeSharp.Shaders.Translation /// A responsible for loading and processing shaders of a given type. /// /// The type of compute shader currently in use. - internal sealed partial class ShaderLoader + internal sealed partial class ShaderLoader : IShaderLoader where T : struct, IComputeShader { /// @@ -57,11 +56,6 @@ namespace ComputeSharp.Shaders.Translation /// private readonly List fieldsInfo = new(); - /// - /// The with the items for the shader functions. - /// - private readonly List functionsInfo = new(); - /// /// The with the items for the shader local functions. /// @@ -82,39 +76,19 @@ namespace ComputeSharp.Shaders.Translation get => CollectionsMarshal.AsSpan(this.d3D12DescriptorRanges1); } - /// - /// Gets the name of the variable used as input for the shader method. - /// - public string ThreadsIdsVariableName { get; private set; } - - /// - /// Gets the generated source code for the method in the current shader. - /// + /// public string EntryPoint { get; private set; } - /// - /// Gets the collection of items for the shader fields. - /// + /// public IReadOnlyList HslsBuffersInfo => this.hlslBuffersInfo; - /// - /// Gets the collection of items for the shader fields. - /// + /// public IReadOnlyList FieldsInfo => this.fieldsInfo; - /// - /// Gets the collection of items for the shader. - /// - public IReadOnlyList FunctionsInfo => this.functionsInfo; - - /// - /// Gets the collection of items for the shader. - /// + /// public IReadOnlyCollection LocalFunctionsInfo => this.localFunctionsInfo; - /// - /// Gets the collection of declared types for the shader. - /// + /// public IReadOnlyCollection DeclaredTypes { get; private set; } ///