From 72b4a05d270b386a3818eceac27543fea9ed3119 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 13:09:43 +0200 Subject: [PATCH] Implemented external loading for captured values --- src/ComputeSharp.Shaders/ShaderRunner.cs | 4 +-- .../Translation/ShaderLoader.cs | 30 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 09cf2fd2..a424548d 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -89,13 +89,13 @@ namespace ComputeSharp.Shaders commandList.SetPipelineState(pipelineState); // Load the captured buffers - foreach (var buffer in shaderLoader.Buffers) + foreach (var buffer in shaderLoader.GetBuffers(action)) { commandList.SetComputeRootDescriptorTable(buffer.Index, buffer.Resource); } // Initialize the loop targets - IReadOnlyList variables = new object[] { (uint)x, (uint)y, (uint)z }.Concat(shaderLoader.FieldValuesList).ToArray(); + IReadOnlyList variables = new object[] { (uint)x, (uint)y, (uint)z }.Concat(shaderLoader.GetVariables(action)).ToArray(); using GraphicsResource variablesBuffer = device.AllocateConstantBufferFromReflectedValues(variables); commandList.SetComputeRootDescriptorTable(0, variablesBuffer); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 27f03881..4bbb7a80 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -26,11 +26,6 @@ namespace ComputeSharp.Shaders.Translation /// private readonly Action Action; - /// - /// The closure instance for the field - /// - private readonly object ShaderInstance; - /// /// Creates a new with the specified parameters /// @@ -39,7 +34,6 @@ namespace ComputeSharp.Shaders.Translation { Action = action; ShaderType = action.Method.DeclaringType; - ShaderInstance = action.Target; } /// @@ -60,7 +54,19 @@ namespace ComputeSharp.Shaders.Translation /// /// Gets the ordered collection of buffers used as fields in the current shader /// - public IEnumerable<(int Index, GraphicsResource Resource)> Buffers => _BufferFields.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(ShaderInstance))); + /// The to use to build the shader + public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action action) => _BufferFields.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(action.Target))); + + /// + /// The of instances mapping the captured scalar/vector variables in the current shader + /// + private readonly List _VariableFields = new List(); + + /// + /// Gets the collection of values of the captured fields for the current shader + /// + /// The to use to build the shader + public IEnumerable GetVariables(Action action) => _VariableFields.Select(field => field.GetValue(action.Target)); private readonly List _BuffersList = new List(); @@ -76,16 +82,6 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FieldsList => _FieldsList; - /// - /// The of instances mapping the captured scalar/vector variables in the current shader - /// - private readonly List _VariableFields = new List(); - - /// - /// Gets the collection of values of the captured fields for the current shader - /// - public IEnumerable FieldValuesList => _VariableFields.Select(field => field.GetValue(ShaderInstance)); - /// /// Gets the name of the variable used as input for the shader method ///