using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
namespace ComputeSharp.__Internals
{
///
/// An attribute that contains info on a processed compute shader that can be executed.
///
/// 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 class IComputeShaderSourceAttribute : Attribute
{
///
/// Creates a new instance with the specified parameters.
///
/// The fully qualified name of the shader type.
/// The collection of custom types.
/// The mapped collection of shader fields.
/// The source code for the method.
/// The collection of processed methods.
/// The collection of discovered constants.
public IComputeShaderSourceAttribute(
string shaderTypeName,
string[] types,
object[] args,
string executeMethod,
string[] methods,
object[] constants)
{
ShaderTypeName = shaderTypeName;
Types = types;
Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
ExecuteMethod = executeMethod;
Methods = methods;
Constants = constants.Cast().ToDictionary(static c => c[0], static c => c[1]);
}
///
/// Gets the fully qualified name of the shader type.
///
internal string ShaderTypeName { get; }
///
/// Gets the collection of processed custom types.
///
internal IReadOnlyCollection Types { get; }
///
/// Gets the mapping of field names.
///
internal IReadOnlyDictionary Fields { get; }
///
/// Gets the source code for the method.
///
internal string ExecuteMethod { get; }
///
/// Gets the collection of processed methods.
///
internal IReadOnlyCollection Methods { get; }
///
/// Gets the collection of discovered constants.
///
internal IReadOnlyDictionary Constants { get; }
///
/// Gets the associated instance for a specified type.
///
/// The shader type to get the attribute for.
/// The associated instance for type .
[Pure]
internal static IComputeShaderSourceAttribute GetForType()
{
// Resolve the fully qualified name of the current type, without generic parameters.
// We can't use Type.FullName, as when T is a closed generic it includes the assembly too.
string fullname = typeof(T) switch
{
{ IsGenericType: true } => typeof(T).GetGenericTypeDefinition().ToString().Split('[')[0],
_ => typeof(T).ToString()
};
return (
from attribute in typeof(T).Assembly.GetCustomAttributes()
where attribute.ShaderTypeName == fullname
select attribute).Single();
}
}
}