Lazy evaluation for the captured variables

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-17 12:56:58 +02:00
1 親 153e050262
コミット e26b74d276
1ファイル変更
+23 -18
+23 -18
ファイルの表示
@@ -26,11 +26,6 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private readonly Action<ThreadIds> Action;
/// <summary>
/// The closure <see cref="Type"/> for the <see cref="Action"/> field
/// </summary>
private readonly Type ShaderType;
/// <summary>
/// The closure instance for the <see cref="Action"/> field
/// </summary>
@@ -39,7 +34,7 @@ namespace ComputeSharp.Shaders.Translation
/// <summary>
/// The sequence of fields in the targeted closure
/// </summary>
private readonly IReadOnlyList<System.Reflection.FieldInfo> ShaderFields;
private readonly IReadOnlyList<FieldInfo> ShaderFields;
/// <summary>
/// Creates a new <see cref="ShaderLoader"/> with the specified parameters
@@ -53,17 +48,25 @@ namespace ComputeSharp.Shaders.Translation
ShaderFields = ShaderType.GetFields().ToArray();
}
/// <summary>
/// Gets the closure <see cref="Type"/> for the <see cref="Action"/> field
/// </summary>
public Type ShaderType { get; }
/// <summary>
/// Gets the <see cref="RootParameter"/> array for the current shader
/// </summary>
public RootParameter[] RootParameters { get; private set; }
private readonly List<(int, GraphicsResource)> _Buffers = new List<(int, GraphicsResource)>();
/// <summary>
/// The <see cref="List{T}"/> of <see cref="FieldInfo"/> instances mapping the captured buffers in the current shader
/// </summary>
private readonly List<FieldInfo> _BufferFields = new List<FieldInfo>();
/// <summary>
/// Gets the ordered collection of buffers used as fields in the current shader
/// </summary>
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<HlslBufferInfo> _BuffersList = new List<HlslBufferInfo>();
@@ -79,12 +82,15 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
public IReadOnlyList<CapturedFieldInfo> FieldsList => _FieldsList;
private readonly List<object> _FieldValuesList = new List<object>();
/// <summary>
/// The <see cref="List{T}"/> of <see cref="FieldInfo"/> instances mapping the captured scalar/vector variables in the current shader
/// </summary>
private readonly List<FieldInfo> _VariableFields = new List<FieldInfo>();
/// <summary>
/// Gets the collection of values of the captured fields for the current shader
/// </summary>
public IReadOnlyList<object> FieldValuesList => _FieldValuesList;
public IEnumerable<object> FieldValuesList => _VariableFields.Select(field => field.GetValue(ShaderInstance));
/// <summary>
/// Gets the name of the <see cref="ThreadIds"/> 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));
}