More code refactoring, removed all legacy code
このコミットが含まれているのは:
1 親
47ca3adf0c
コミット
7c3c642fad
6ファイル変更
+30
-239
検索条件に一致するファイルはありません
@@ -1,108 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
internal sealed partial class ReadableMember
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of members to getter delegates
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, Getter> GettersMapping = new Dictionary<string, Getter>();
|
||||
|
||||
/// <summary>
|
||||
/// The local <see cref="Getter"/> instance, if already loaded
|
||||
/// </summary>
|
||||
private Getter? _Getter;
|
||||
|
||||
/// <summary>
|
||||
/// Ensures the dynamic accessor for the current instance is loaded and ready to use
|
||||
/// </summary>
|
||||
public void PreloadAccessor()
|
||||
{
|
||||
if (_Getter == null)
|
||||
{
|
||||
/* If the local delegate is available, use it and save the dictionary
|
||||
* access entirely. If it's not, try to get the delegate from the dictionary
|
||||
* first. This can save unnecessary overhead if a delegate for the same member has
|
||||
* already been built, eg. if it belongs to a captured field that is used by two different
|
||||
* shaders in the same closure. Once the getter is built, cache it and invoke it */
|
||||
if (GettersMapping.TryGetValue(Id, out Getter getter)) _Getter = getter;
|
||||
else
|
||||
{
|
||||
_Getter = getter = BuildDynamicGetter();
|
||||
GettersMapping.Add(Id, getter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
/// <remarks>This method doesn't ensure that the accessor is available</remarks>
|
||||
/// <exception cref="NullReferenceException">The dynamic accessor is not available</exception>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public object DangerousGetValue(object? instance) => _Getter!(instance);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
[Pure]
|
||||
public object GetValue(object? instance)
|
||||
{
|
||||
PreloadAccessor();
|
||||
return _Getter!(instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="delegate"/> that represents a getter for a specific member
|
||||
/// </summary>
|
||||
/// <param name="obj">The source object to get the member from</param>
|
||||
/// <returns>The value of the member, upcast to <see cref="object"/></returns>
|
||||
private delegate object Getter(object? obj);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of parent members to access the current one from the root instance
|
||||
/// </summary>
|
||||
public IReadOnlyList<ReadableMember>? Parents { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Builds a dynamic IL method to retrieve the value of the current member
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private Getter BuildDynamicGetter()
|
||||
{
|
||||
return DynamicMethod<Getter>.New(il =>
|
||||
{
|
||||
// Info for hierarchical access
|
||||
ReadableMember[] hierarchy = (Parents ?? Array.Empty<ReadableMember>()).Concat(new[] { this }).ToArray();
|
||||
|
||||
// Load the argument (the object instance) and cast it to the right type, if needed
|
||||
if (!IsStatic)
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.EmitCastOrUnbox(hierarchy[0].DeclaringType);
|
||||
}
|
||||
|
||||
// Unroll the member access
|
||||
foreach (ReadableMember member in hierarchy)
|
||||
{
|
||||
il.EmitReadMember(member);
|
||||
}
|
||||
|
||||
// Box the value, if needed
|
||||
if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType);
|
||||
|
||||
// Return the member value from the top of the evaluation stack
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
@@ -6,13 +8,8 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that wraps a readable member of another <see langword="class"/> and provides access to it
|
||||
/// </summary>
|
||||
internal sealed partial class ReadableMember
|
||||
internal sealed class ReadableMember
|
||||
{
|
||||
/// <summary>
|
||||
/// The id of the current member
|
||||
/// </summary>
|
||||
private readonly string Id;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="MemberInfo"/> object wrapped by the current instance
|
||||
/// </summary>
|
||||
@@ -25,7 +22,6 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
private ReadableMember(MemberInfo memberInfo)
|
||||
{
|
||||
// General properties
|
||||
Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{memberInfo.Name}";
|
||||
Member = memberInfo;
|
||||
Name = memberInfo.Name;
|
||||
DeclaringType = memberInfo.DeclaringType;
|
||||
@@ -66,6 +62,26 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
/// </summary>
|
||||
public Type MemberType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of parent members to access the current one from the root instance
|
||||
/// </summary>
|
||||
public IReadOnlyList<ReadableMember>? Parents { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the value of the wrapped member for the current instance
|
||||
/// </summary>
|
||||
/// <param name="instance">The target instance to use to read the value from</param>
|
||||
[Pure]
|
||||
public object GetValue(object? instance)
|
||||
{
|
||||
return Member switch
|
||||
{
|
||||
FieldInfo field => field.GetValue(instance),
|
||||
PropertyInfo property => property.GetValue(instance),
|
||||
_ => throw new InvalidOperationException("Field and property can't both be null at the same time")
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="FieldInfo"/> object into a <see cref="ReadableMember"/> instance
|
||||
/// </summary>
|
||||
|
||||
@@ -51,6 +51,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
[Pure]
|
||||
public DispatchData GetDispatchData(Action<ThreadIds> action, int x, int y, int z)
|
||||
{
|
||||
/* Create the data loader instance, if not available already.
|
||||
* This will also initialize the other local fields in the shader
|
||||
* loader class, which are used to rent the buffers used to store
|
||||
* the captured resources and variables. This is why this call
|
||||
* has to be the first one in this method, it must NOT be moved */
|
||||
DispatchDataLoader loader = _DispatchDataLoader ??= BuildDispatchDataLoader();
|
||||
|
||||
// Resources and variables buffers
|
||||
GraphicsResource[] resources = ArrayPool<GraphicsResource>.Shared.Rent(_ResourcesCount);
|
||||
byte[] variables = ArrayPool<byte>.Shared.Rent(_VariablesByteSize);
|
||||
@@ -63,7 +70,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
Unsafe.Add(ref Unsafe.As<byte, int>(ref r1), 2) = z;
|
||||
|
||||
// Invoke the dynamic method to extract the captured data
|
||||
DispatchDataLoader loader = _DispatchDataLoader ??= BuildDispatchDataLoader();
|
||||
loader(action.Target, ref r0, ref r1);
|
||||
|
||||
return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize);
|
||||
|
||||
@@ -223,14 +223,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
// Captured static delegates with a return type
|
||||
LoadStaticMethodSource(fieldName, func.Method);
|
||||
}
|
||||
|
||||
/* Build the dynamic IL accessor for the current member, if one is
|
||||
* not available already. This is done during while the shader is
|
||||
* being loaded and not just when needed because the internal cache
|
||||
* system for the accessors is not thread safe. Preloading the accessor here
|
||||
* leverages the external lock around the shader loading, ensuring
|
||||
* that no race conditions occur when accessing the cached mapping */
|
||||
memberInfo.PreloadAccessor();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする