From e26b74d27687b70d7454409b452c7f87a6a06e82 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 17 Aug 2019 12:56:58 +0200 Subject: [PATCH] Lazy evaluation for the captured variables --- .../Translation/ShaderLoader.cs | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 3ac67495..8a16eeba 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 for the field - /// - private readonly Type ShaderType; - /// /// The closure instance for the field /// @@ -39,7 +34,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 @@ -53,17 +48,25 @@ namespace ComputeSharp.Shaders.Translation ShaderFields = ShaderType.GetFields().ToArray(); } + /// + /// Gets the closure for the field + /// + public Type ShaderType { get; } + /// /// Gets the array for the current shader /// public RootParameter[] RootParameters { get; private set; } - private readonly List<(int, GraphicsResource)> _Buffers = new List<(int, GraphicsResource)>(); + /// + /// The of instances mapping the captured buffers in the current shader + /// + private readonly List _BufferFields = new List(); /// /// Gets the ordered collection of buffers used as fields in the current shader /// - public IReadOnlyList<(int Index, GraphicsResource Resource)> Buffers => _Buffers; + public IEnumerable<(int Index, GraphicsResource Resource)> Buffers => _BufferFields.Select((field, i) => (i + 1, (GraphicsResource)field.GetValue(ShaderInstance))); private readonly List _BuffersList = new List(); @@ -79,12 +82,15 @@ namespace ComputeSharp.Shaders.Translation /// public IReadOnlyList FieldsList => _FieldsList; - private readonly List _FieldValuesList = new List(); + /// + /// 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 IReadOnlyList FieldValuesList => _FieldValuesList; + public IEnumerable FieldValuesList => _VariableFields.Select(field => field.GetValue(ShaderInstance)); /// /// Gets the name of the variable used as input for the shader method @@ -132,15 +138,14 @@ namespace ComputeSharp.Shaders.Translation { Type fieldType = fieldInfo.FieldType; string fieldName = fieldInfo.Name; - object fieldValue = fieldInfo.GetValue(ShaderInstance); // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) { descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ConstantBufferView, 1, constantBuffersCount)); - // Reference to the underlying buffer - _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + // Track the buffer field + _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType.GenericTypeArguments[0]); _BuffersList.Add(new ConstantBufferFieldInfo(typeName, fieldName, constantBuffersCount++)); @@ -150,8 +155,8 @@ namespace ComputeSharp.Shaders.Translation // Root parameter for a readonly buffer descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, readOnlyBuffersCount)); - // Reference to the underlying buffer - _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + // Track the buffer field + _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _BuffersList.Add(new ReadOnlyBufferFieldInfo(typeName, fieldName, readOnlyBuffersCount++)); @@ -161,8 +166,8 @@ namespace ComputeSharp.Shaders.Translation // Root parameter for a read write buffer descriptorRanges.Add(new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, readWriteBuffersCount)); - // Reference to the underlying buffer - _Buffers.Add((descriptorRanges.Count - 1, (GraphicsResource)fieldValue)); + // Track the buffer field + _BufferFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _BuffersList.Add(new ReadWriteBufferFieldInfo(typeName, fieldName, readWriteBuffersCount++)); @@ -170,7 +175,7 @@ namespace ComputeSharp.Shaders.Translation else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { // Register the captured field - _FieldValuesList.Add(fieldValue); + _VariableFields.Add(fieldInfo); string typeName = HlslKnownTypes.GetMappedName(fieldType); _FieldsList.Add(new CapturedFieldInfo(typeName, fieldName)); }