Move dynamic shader types to ComputeSharp.Dynamic
このコミットが含まれているのは:
1 親
5f8196716b
コミット
3bbf7754ba
13ファイル変更
+7
-1
検索条件に一致するファイルはありません
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute that indicates that a method will be indirectly used in a compute shader.
|
||||
/// This method is necessary for methods that are captured and used within a given shader
|
||||
/// through a <see cref="Delegate"/> field (of some custom type). In order to be processed
|
||||
/// correctly, wrapped methods need to have this attribute manually applied to them.
|
||||
/// Methods also need to be static, though this can only be tested at runtime.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||
public sealed class ShaderMethodAttribute : Attribute
|
||||
{
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.__Internals;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public sealed partial class ShaderMethodSourceAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Appends the mapped source code for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="mapping">The mapping of already discovered constant names.</param>
|
||||
public void AppendConstants(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> constant in this.constants)
|
||||
{
|
||||
if (mapping.Add(constant.Key))
|
||||
{
|
||||
builder.Append("#define ");
|
||||
builder.Append(constant.Key);
|
||||
builder.Append(' ');
|
||||
builder.Append(constant.Value);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the discovered types for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="mapping">The mapping of already discovered type names.</param>
|
||||
public void AppendTypes(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> type in this.types)
|
||||
{
|
||||
if (mapping.Add(type.Key))
|
||||
{
|
||||
builder.Append('\n');
|
||||
builder.Append(type.Value);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the discovered forward declarations for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="mapping">The mapping of already discovered type names.</param>
|
||||
public void AppendForwardDeclarations(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> method in this.methods)
|
||||
{
|
||||
if (mapping.Add(method.Key))
|
||||
{
|
||||
builder.Append('\n');
|
||||
builder.Append(method.Key);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the discovered methods for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="mapping">The mapping of already discovered type names.</param>
|
||||
public void AppendMethods(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> method in this.methods)
|
||||
{
|
||||
if (mapping.Remove(method.Key))
|
||||
{
|
||||
builder.Append('\n');
|
||||
builder.Append(method.Value);
|
||||
builder.Append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the mapped source code for the current method.
|
||||
/// </summary>
|
||||
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
|
||||
/// <param name="name">The name to bind the method to.</param>
|
||||
public void AppendMappedInvokeMethod(ref ArrayPoolStringBuilder builder, string name)
|
||||
{
|
||||
builder.Append(this.invokeMethod.Replace(InvokeMethodIdentifier, name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="ShaderMethodAttribute"/>.
|
||||
/// </summary>
|
||||
/// <remarks>This attribute is not meant to be directly used by applications using ComputeSharp.</remarks>
|
||||
[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
|
||||
{
|
||||
/// <summary>
|
||||
/// The identifier for the invoke method, for late binding.
|
||||
/// </summary>
|
||||
internal const string InvokeMethodIdentifier = "__<NAME>__";
|
||||
|
||||
/// <summary>
|
||||
/// The fully qualified name of the current method.
|
||||
/// </summary>
|
||||
private readonly string methodName;
|
||||
|
||||
/// <summary>
|
||||
/// The source code for the target entry point method.
|
||||
/// </summary>
|
||||
private readonly string invokeMethod;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of custom types.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> types;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of processed methods.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> methods;
|
||||
|
||||
/// <summary>
|
||||
/// The collection of discovered constants.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> constants;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderMethodSourceAttribute"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="methodName">The fully qualified name of the current method.</param>
|
||||
/// <param name="invokeMethod">The source code for the target entry point method.</param>
|
||||
/// <param name="types">The collection of custom types.</param>
|
||||
/// <param name="methods">The collection of processed methods.</param>
|
||||
/// <param name="constants">The collection of discovered constants.</param>
|
||||
public ShaderMethodSourceAttribute(string methodName, string invokeMethod, object[] types, object[] methods, object[] constants)
|
||||
{
|
||||
this.methodName = methodName;
|
||||
this.invokeMethod = invokeMethod;
|
||||
this.types = types.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
this.methods = methods.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
this.constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the associated <see cref="ShaderMethodSourceAttribute"/> instance for a specified delegate.
|
||||
/// </summary>
|
||||
/// <param name="function">The input <see cref="Delegate"/> instance to get info for.</param>
|
||||
/// <param name="name">The name of the shader field containing <paramref name="function"/>.</param>
|
||||
/// <returns>The associated <see cref="ShaderMethodSourceAttribute"/> instance for the given delegate.</returns>
|
||||
[Pure]
|
||||
public static ShaderMethodSourceAttribute GetForDelegate(Delegate function, string name)
|
||||
{
|
||||
if (!function.Method.IsStatic)
|
||||
{
|
||||
return ThrowArgumentExceptionForNonStaticMethod(name);
|
||||
}
|
||||
|
||||
var attributes = function.Method.DeclaringType!.Assembly.GetCustomAttributes<ShaderMethodSourceAttribute>();
|
||||
string methodName = function.Method.GetFullName();
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (attribute.methodName.Equals(methodName))
|
||||
{
|
||||
return attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return ThrowArgumentExceptionForMissingShaderMethodAttribute(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when a given <see cref="Delegate"/> is not static.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the shader field containing the invalid method.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when a given <see cref="Delegate"/> doesn't have <see cref="ShaderMethodAttribute"/> applied to it.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the shader field containing the invalid method.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする