using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.__Internals;
using ComputeSharp.Shaders.Translation.Models;
using TerraFX.Interop;
#pragma warning disable CS0618
namespace ComputeSharp.Shaders.Translation
{
///
internal sealed partial class ShaderLoader
{
///
/// A that loads all the captured members from a shader instance.
///
/// The to use to run the shader.
/// The shader instance to load the data from.
///
/// A reference to the buffer with all the captured instances.
/// This is just represented as a reference, to avoid exposing the type publicly.
///
/// A reference to the buffer to use to store all the captured value types.
/// The returned value indicates the total number of written bytes into .
private delegate int DispatchDataLoader(GraphicsDevice device, in T shader, ref ulong r0, ref byte r1);
///
/// The number of captured graphics resources (buffers).
///
private int totalResourceCount;
///
/// The cached instance to load dispatch data before execution.
///
private DispatchDataLoader? dispatchDataLoader;
///
/// The of instances mapping the captured variables in the current shader.
///
private readonly List capturedFields = new();
///
/// Gets a new instance with all the captured data for the current shader instance.
///
/// The to use to run the shader.
/// The input instance representing the compute shader to run.
/// The number of iterations to run on the X axis.
/// The number of iterations to run on the Y axis.
/// The number of iterations to run on the Z axis.
[Pure]
public DispatchData GetDispatchData(GraphicsDevice device, in T shader, int x, int y, int z)
{
// Resources and variables buffers
ulong[] resources = ArrayPool.Shared.Rent(this.totalResourceCount);
byte[] variables = ArrayPool.Shared.Rent(256);
ref ulong r0 = ref MemoryMarshal.GetArrayDataReference(resources);
ref byte r1 = ref MemoryMarshal.GetArrayDataReference(variables);
// Set the x, y and z counters
Unsafe.As(ref r1) = x;
Unsafe.Add(ref Unsafe.As(ref r1), 1) = y;
Unsafe.Add(ref Unsafe.As(ref r1), 2) = z;
// Invoke the dynamic method to extract the captured data
int totalVariablesByteSize = this.dispatchDataLoader!(device, in shader, ref r0, ref r1);
return new(resources, this.totalResourceCount, variables, totalVariablesByteSize);
}
///
/// Initializes the instance that loads the dispatch data for the current shader type.
///
private void InitializeDispatchDataLoader()
{
// Get the generated data loader
Type[] argumentTypes = new[] { typeof(GraphicsDevice), typeof(T).MakeByRefType(), typeof(ulong).MakeByRefType(), typeof(byte).MakeByRefType() };
Type type = typeof(T).Assembly.GetType("ComputeSharp.__Internals.DispatchDataLoader")!;
MethodInfo method = type.GetMethod("LoadDispatchData", argumentTypes)!;
// Extract the computed count of 32 bit root constants to load
D3D12Root32BitConstantsCount = ((ComputeRoot32BitConstantsAttribute)method.ReturnTypeCustomAttributes.GetCustomAttributes(false)[0]).Count;
// Create a delegate from the generated shader data loader
this.dispatchDataLoader = method.CreateDelegate();
}
}
}