Simplified shader resources loading

このコミットが含まれているのは:
Sergio Pedri committed 2020-04-12 17:27:25 +02:00
1 親 997ed35c90
コミット 21e5628af8
2ファイル変更
+10 -82

検索条件に一致するファイルはありません

+10 -69
ファイルの表示
@@ -2,7 +2,6 @@
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -34,22 +33,22 @@ namespace ComputeSharp.Shaders.Translation
private readonly List<ReadableMember> _CapturedMembers = new List<ReadableMember>();
/// <summary>
/// A <see langword="delegate"/> that loads all the captured members from a shader closure
/// A <see langword="delegate"/> that loads all the captured members from a shader instance
/// </summary>
/// <param name="obj">The shader closure instance to load the data from</param>
/// <param name="shader">The shader instance to load the data from</param>
/// <param name="r0">A reference to the buffer with all the captured <see cref="GraphicsResource"/> instances</param>
/// <param name="r1">A reference to the buffer to use to store all the captured value types</param>
private delegate void DispatchDataLoader(object obj, ref GraphicsResource r0, ref byte r1);
private delegate void DispatchDataLoader(in T shader, ref GraphicsResource r0, ref byte r1);
/// <summary>
/// Gets a new <see cref="DispatchData"/> instance with all the captured data for the current shader instance
/// </summary>
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run</param>
/// <param name="x">The number of iterations to run on the X axis</param>
/// <param name="y">The number of iterations to run on the Y axis</param>
/// <param name="z">The number of iterations to run on the Z axis</param>
[Pure]
public DispatchData GetDispatchData(Action<ThreadIds> action, int x, int y, int z)
public DispatchData GetDispatchData(in T shader, int x, int y, int z)
{
// Resources and variables buffers
GraphicsResource[] resources = ArrayPool<GraphicsResource>.Shared.Rent(_ResourcesCount);
@@ -63,7 +62,7 @@ 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!(action.Target, ref r0, ref r1);
_DispatchDataLoader!(shader, ref r0, ref r1);
return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize);
}
@@ -73,66 +72,8 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
private void BuildDispatchDataLoader()
{
// Mapping of parent members to index of the relative local variable
var map =
_CapturedMembers
.Where(m => !m.IsStatic && m.Parents != null)
.SelectMany(m => m.Parents)
.Distinct()
.Select((m, i) => (Member: m, Index: i))
.ToDictionary(p => (object)p.Member, p => p.Index + 1);
object root = new object(); // Placeholder
map.Add(root, 0);
// Set of indices of the loaded parent members
HashSet<int> loaded = new HashSet<int>(new[] { 0 });
// Build the dynamic IL method
_DispatchDataLoader = DynamicMethod<DispatchDataLoader>.New(il =>
{
// Loads a given member on the top of the execution stack
void LoadMember(ReadableMember member)
{
if (!member.IsStatic)
{
// Load the parent instance on the execution stack if the member isn't static
int index = map[member.Parents?.Last() ?? root];
if (loaded.Contains(index)) il.EmitLoadLocal(index);
else
{
// Seek upwards to find the most in depth loaded parent
int i = member.Parents!.Count - 1;
while (i > 0 && !loaded.Contains(map[member.Parents[i]])) i--;
// Load the local variables for all the parents of the current member
il.EmitLoadLocal(i);
for (; i < member.Parents.Count; i++)
{
index = map[member.Parents[i]];
il.Emit(OpCodes.Ldfld, member.Parents[i]);
il.EmitStoreLocal(index);
il.EmitLoadLocal(index);
loaded.Add(index);
}
}
}
il.EmitReadMember(member);
}
// Declare the local variables
il.DeclareLocal(typeof(T));
foreach (ReadableMember member in map.OrderBy(p => p.Value).Skip(1).Select(p => p.Key))
{
il.DeclareLocal(member.MemberType);
}
// Cast the closure instance and assign it to the local variable
il.Emit(OpCodes.Ldarg_0);
il.EmitCastOrUnbox(typeof(T));
il.Emit(OpCodes.Stloc_0);
// Handle all the captured members, both resources and variables
foreach (ReadableMember member in _CapturedMembers)
{
if (HlslKnownTypes.IsKnownBufferType(member.MemberType))
@@ -155,11 +96,11 @@ namespace ComputeSharp.Shaders.Translation
}
else throw new InvalidOperationException($"Invalid captured member of type {member.MemberType}");
/* Load the current member accordingly.
* When this method returns, the value of the current
* member will be at the top of the execution stack */
LoadMember(member);
// Load the reference to the input delegate if the member is not static
if (!member.IsStatic) il.Emit(OpCodes.Ldarg_0);
// Read the current member and store it to the target address
il.EmitReadMember(member);
il.EmitStoreToAddress(member.MemberType);
}
-13
ファイルの表示
@@ -202,19 +202,6 @@ namespace ComputeSharp.Shaders.Translation
string typeName = HlslKnownTypes.GetMappedName(fieldType);
_FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName));
}
else if (fieldType.IsClass && fieldName.StartsWith("CS$<>"))
{
// Captured scope, update the parents list
List<ReadableMember> updatedParents = parents?.ToList() ?? new List<ReadableMember>();
updatedParents.Add(memberInfo);
// Recurse on the new compiler generated class
IReadOnlyList<FieldInfo> fields = fieldType.GetFields().ToArray();
foreach (FieldInfo fieldInfo in fields)
{
LoadFieldInfo(fieldInfo, null, updatedParents);
}
}
else if (fieldType.IsDelegate() &&
memberInfo.GetValue(Shader) is Delegate func &&
(func.Method.IsStatic || func.Method.DeclaringType.IsStatelessDelegateContainer()) &&