using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using ComputeSharp.Core.Extensions;
namespace ComputeSharp.__Internals;
///
/// An attribute that contains info on a processed shader method that can be executed within a shader.
/// Instances of this attribute are generated from method annotated with .
///
/// This attribute is not meant to be directly used by applications using ComputeSharp.
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This attribute is not intended to be used directly by user code")]
public sealed partial class ShaderMethodSourceAttribute : Attribute
{
///
/// The identifier for the invoke method, for late binding.
///
internal const string InvokeMethodIdentifier = "____";
///
/// The fully qualified name of the current method.
///
private readonly string methodName;
///
/// The source code for the target entry point method.
///
private readonly string invokeMethod;
///
/// The collection of custom types.
///
private readonly Dictionary types;
///
/// The collection of processed methods.
///
private readonly Dictionary methods;
///
/// The collection of discovered constants.
///
private readonly Dictionary constants;
///
/// Creates a new instance with the specified parameters.
///
/// The fully qualified name of the current method.
/// The source code for the target entry point method.
/// The collection of custom types.
/// The collection of processed methods.
/// The collection of discovered constants.
public ShaderMethodSourceAttribute(string methodName, string invokeMethod, object[] types, object[] methods, object[] constants)
{
this.methodName = methodName;
this.invokeMethod = invokeMethod;
this.types = types.Cast().ToDictionary(static c => c[0], static c => c[1]);
this.methods = methods.Cast().ToDictionary(static c => c[0], static c => c[1]);
this.constants = constants.Cast().ToDictionary(static c => c[0], static c => c[1]);
}
///
/// Gets the associated instance for a specified delegate.
///
/// The input instance to get info for.
/// The name of the shader field containing .
/// The associated instance for the given delegate.
[Pure]
public static ShaderMethodSourceAttribute GetForDelegate(Delegate function, string name)
{
if (!function.Method.IsStatic)
{
return ThrowArgumentExceptionForNonStaticMethod(name);
}
var attributes = function.Method.DeclaringType!.Assembly.GetCustomAttributes();
string methodName = function.Method.GetFullName();
foreach (var attribute in attributes)
{
if (attribute.methodName.Equals(methodName))
{
return attribute;
}
}
return ThrowArgumentExceptionForMissingShaderMethodAttribute(name);
}
///
/// Throws an when a given is not static.
///
/// The name of the shader field containing the invalid method.
private static ShaderMethodSourceAttribute ThrowArgumentExceptionForNonStaticMethod(string name)
{
throw new ArgumentException($"The captured delegate \"{name}\" was wrapping a non static method (only static methods are supported for captured delegates).", name);
}
///
/// Throws an when a given doesn't have applied to it.
///
/// The name of the shader field containing the invalid method.
private static ShaderMethodSourceAttribute ThrowArgumentExceptionForMissingShaderMethodAttribute(string name)
{
throw new ArgumentException($"The captured delegate \"{name}\" was wrapping a method without the [ShaderMethod] attribute applied to it.", name);
}
}