From 21e5628af8ad2c75d6abbf1637940d097a4a120c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 12 Apr 2020 17:27:25 +0200 Subject: [PATCH] Simplified shader resources loading --- .../Translation/ShaderLoader{T}.IL.cs | 79 +++---------------- .../Translation/ShaderLoader{T}.cs | 13 --- 2 files changed, 10 insertions(+), 82 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 889f1abe..7a4242fd 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -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 _CapturedMembers = new List(); /// - /// A that loads all the captured members from a shader closure + /// A that loads all the captured members from a shader instance /// - /// The shader closure instance to load the data from + /// The shader instance to load the data from /// A reference to the buffer with all the captured instances /// A reference to the buffer to use to store all the captured value types - 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); /// /// Gets a new instance with all the captured data for the current shader instance /// - /// The input representing the compute shader to run + /// The input instance representing the compute shader to run /// The number of iterations to run on the X axis /// The number of iterations to run on the Y axis /// The number of iterations to run on the Z axis [Pure] - public DispatchData GetDispatchData(Action 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.Shared.Rent(_ResourcesCount); @@ -63,7 +62,7 @@ namespace ComputeSharp.Shaders.Translation Unsafe.Add(ref Unsafe.As(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 /// 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 loaded = new HashSet(new[] { 0 }); - - // Build the dynamic IL method _DispatchDataLoader = DynamicMethod.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); } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index ad0a7141..89ab1f73 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -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 updatedParents = parents?.ToList() ?? new List(); - updatedParents.Add(memberInfo); - - // Recurse on the new compiler generated class - IReadOnlyList 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()) &&