using System; using System.Buffers; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Translation.Models; namespace ComputeSharp.Shaders.Translation { /// internal sealed partial class ShaderLoader { /// /// The number of captured graphics resources (buffers) /// private int _ResourcesCount; /// /// The size in bytes of the buffer with the captured variables /// private int _VariablesByteSize = 3 * sizeof(int); // X, Y and Z threads count // The cached loader method private DispatchDataLoader? _DispatchDataLoader; /// /// The of instances mapping the captured variables in the current shader /// private readonly List _CapturedMembers = new List(); /// /// A that loads all the captured members from a shader instance /// /// 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(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 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(in T shader, int x, int y, int z) { // Resources and variables buffers GraphicsResource[] resources = ArrayPool.Shared.Rent(_ResourcesCount); byte[] variables = ArrayPool.Shared.Rent(_VariablesByteSize); ref GraphicsResource r0 = ref resources[0]; ref byte r1 = ref variables[0]; // Set the x, y and z counters Unsafe.As(ref r1) = x; Unsafe.Add(ref Unsafe.As(ref r1), 1) = y; Unsafe.Add(ref Unsafe.As(ref r1), 2) = z; // Invoke the dynamic method to extract the captured data _DispatchDataLoader!(shader, ref r0, ref r1); return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize); } /// /// Builds a new instance that loads the dispatch data for the current shader type /// private void BuildDispatchDataLoader() { _DispatchDataLoader = DynamicMethod.New(il => { foreach (ReadableMember member in _CapturedMembers) { if (HlslKnownTypes.IsKnownBufferType(member.MemberType)) { // Load the offset address into the resource buffers il.Emit(OpCodes.Ldarg_1); if (_ResourcesCount > 0) il.EmitAddOffset(_ResourcesCount); _ResourcesCount++; } else if (HlslKnownTypes.IsKnownScalarType(member.MemberType) || HlslKnownTypes.IsKnownVectorType(member.MemberType)) { // Calculate the right offset with 16-bytes padding (HLSL constant buffer) int size = member.MemberType == typeof(bool) ? sizeof(uint) : Marshal.SizeOf(member.MemberType); // bool is 4 bytes in HLSL if (_VariablesByteSize % 16 > 16 - size) _VariablesByteSize += 16 - _VariablesByteSize % 16; // Load the target address into the variables buffer il.Emit(OpCodes.Ldarg_2); il.EmitAddOffset(_VariablesByteSize); _VariablesByteSize += size; } else throw new InvalidOperationException($"Invalid captured member of type {member.MemberType}"); // 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); } il.Emit(OpCodes.Ret); }); } } }