using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; using ComputeSharp.__Internals; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE; #pragma warning disable CS0618, CS8618 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 : IShaderLoader where T : struct, IComputeShader { /// /// The associated instance for the current shader type. /// private static readonly IComputeShaderSourceAttribute Attribute = IComputeShaderSourceAttribute.GetForType(); /// /// The number of constant buffers to define in the shader. /// private uint constantBuffersCount; /// /// The number of readonly buffers to define in the shader. /// private uint readOnlyBuffersCount; /// /// The number of read write buffers to define in the shader. /// private uint readWriteBuffersCount; /// /// The array of items that are required to load the captured values. /// private D3D12_DESCRIPTOR_RANGE1[] d3D12DescriptorRanges1; /// /// The with the items for the shader fields. /// private readonly List hlslResourceInfo = new(); /// /// The with the items for the shader fields. /// private readonly List fieldsInfo = new(); /// /// The with the collected methods for the shader. /// private readonly List methodsInfo = new(); /// /// The with the collected declared types for the shader. /// private readonly List declaredTypes = new(); /// /// Creates a new instance. /// private ShaderLoader() { } /// /// Gets a with the loaded items for the shader. /// public ReadOnlySpan D3D12DescriptorRanges1 { get => this.d3D12DescriptorRanges1; } /// public string EntryPoint { get; private set; } /// public IReadOnlyList HlslResourceInfo => this.hlslResourceInfo; /// public IReadOnlyList FieldsInfo => this.fieldsInfo; /// public IReadOnlyCollection MethodsInfo => this.methodsInfo; /// public IReadOnlyCollection DeclaredTypes => this.declaredTypes; /// /// Loads and processes an input shadeer. /// /// The instance to use to build the shader. /// A new instance representing the input shader. [Pure] public static ShaderLoader Load(in T shader) { ShaderLoader @this = new(); // Reading members through reflection requires an object parameter, so here we're just boxing // the input shader once to avoid allocating it multiple times while processing the shader. @this.d3D12DescriptorRanges1 = @this.LoadFieldsInfo(shader); @this.LoadMethodMetadata(); @this.InitializeDispatchDataLoader(); return @this; } /// /// Loads the fields info for the current shader being loaded. /// /// The boxed instance to use to build the shader. /// The array of items that are required to load the captured values. private D3D12_DESCRIPTOR_RANGE1[] LoadFieldsInfo(object shader) { IReadOnlyList shaderFields = typeof(T).GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToArray(); if (shaderFields.Count == 0) { ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field"); } List d3D12DescriptorRanges1 = new(); // Descriptor for the buffer for captured scalar/vector variables D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount++); d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); // Inspect the captured fields foreach (FieldInfo fieldInfo in shaderFields) { LoadFieldInfo(shader, fieldInfo, d3D12DescriptorRanges1); } return d3D12DescriptorRanges1.ToArray(); } /// /// Loads a specified and adds it to the shader model. /// /// The boxed instance to use to build the shader. /// The target to load. /// The list of discovered values. private void LoadFieldInfo(object shader, FieldInfo fieldInfo, List d3D12DescriptorRanges1) { Type fieldType = fieldInfo.FieldType; (string hlslName, string hlslType) = Attribute.Fields[fieldInfo.Name]; if (HlslKnownTypes.IsConstantBufferType(fieldType)) { d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount)); this.capturedFields.Add(fieldInfo); this.hlslResourceInfo.Add(new HlslResourceInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++)); this.totalResourceCount++; } else if (HlslKnownTypes.IsReadOnlyResourceType(fieldType)) { d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount)); this.capturedFields.Add(fieldInfo); this.hlslResourceInfo.Add(new HlslResourceInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++)); this.totalResourceCount++; } else if (HlslKnownTypes.IsReadWriteResourceType(fieldType)) { d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount)); this.capturedFields.Add(fieldInfo); this.hlslResourceInfo.Add(new HlslResourceInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++)); this.totalResourceCount++; } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { this.capturedFields.Add(fieldInfo); this.fieldsInfo.Add(new CapturedFieldInfo(hlslType, hlslName)); } else if (fieldInfo.GetValue(shader) is Delegate { Method: { IsStatic: true } } func) { // Captured static delegates with a return type var methodSource = ShaderMethodSourceAttribute.GetForDelegate(func); this.declaredTypes.AddRange(methodSource.Types); this.methodsInfo.Add(methodSource.GetMappedInvokeMethod(hlslName)); this.methodsInfo.AddRange(methodSource.Methods); } else ThrowHelper.ThrowArgumentException("Invalid captured variable"); } /// /// Loads the metadata info for the current shader. /// private void LoadMethodMetadata() { EntryPoint = Attribute.ExecuteMethod; this.methodsInfo.AddRange(Attribute.Methods); this.declaredTypes.AddRange(Attribute.Types); } } }