using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Shaders.Mappings;
using ComputeSharp.Shaders.Renderer.Models.Fields;
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
using ComputeSharp.Shaders.Translation.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Vortice.DirectX.Direct3D12;
#pragma warning disable CS8618 // Non-nullable field is uninitialized
namespace ComputeSharp.Shaders.Translation
{
///
/// A responsible for loading and processing instances
///
internal sealed class ShaderLoader
{
///
/// The that represents the shader to load
///
private readonly Action Action;
///
/// Creates a new with the specified parameters
///
/// The to use to build the shader
private ShaderLoader(Action action)
{
Action = action;
ShaderType = action.Method.DeclaringType;
}
///
/// The number of constant buffers to define in the shader
///
private int _ConstantBuffersCount;
///
/// The number of readonly buffers to define in the shader
///
private int _ReadOnlyBuffersCount;
///
/// The number of read write buffers to define in the shader
///
private int _ReadWriteBuffersCount;
///
/// Gets the closure for the field
///
public Type ShaderType { get; }
///
/// The of items that are required to load the captured values
///
private readonly List DescriptorRanges = new List();
private RootParameter1[] _RootParameters;
///
/// Gets the array for the current shader
///
public RootParameter1[] RootParameters => _RootParameters ??= DescriptorRanges.Select(range => new RootParameter1(new RootDescriptorTable1(range), ShaderVisibility.All)).ToArray();
///
/// The of instances mapping the captured buffers in the current shader
///
private readonly List<(ReadableMember Member, IEnumerable? Parents)> _BufferMembers = new List<(ReadableMember, IEnumerable?)>();
///
/// Gets the ordered collection of buffers used as fields in the current shader
///
/// The to use to build the shader
public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action action)
{
foreach (var (item, i) in _BufferMembers.Select((item, i) => (item, i)))
{
if (item.Parents == null) yield return (i + 1, (GraphicsResource)item.Member.GetValue(action.Target));
else
{
object target = item.Parents.Aggregate(action.Target, (obj, member) => member.GetValue(obj));
yield return (i + 1, (GraphicsResource)item.Member.GetValue(target));
}
}
}
///
/// The of instances mapping the captured scalar/vector variables in the current shader
///
private readonly List<(ReadableMember Member, IEnumerable? Parents)> _VariableMembers = new List<(ReadableMember, IEnumerable?)>();
///
/// Gets the collection of values of the captured fields for the current shader
///
/// The to use to build the shader
public IEnumerable