using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using TerraFX.Interop; namespace ComputeSharp.Shaders.Translation.Models { /// /// A that contains all the captured data to dispatch a shader. /// internal readonly ref struct DispatchData { /// /// The array (actually values) with the captured buffers. /// private readonly ulong[] resourcesArray; /// /// The number of values in . /// private readonly int resourcesCount; /// /// The array with all the captured variables, with proper padding. /// private readonly byte[] variablesArray; /// /// The actual size in bytes to use from . /// private readonly int variablesByteSize; /// /// Creates a new instance with the specified parameters. /// /// The array with the captured buffers. /// The number of instances in . /// The array with all the captured variables, with proper padding. /// The actual size in bytes to use from . public DispatchData(ulong[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesByteSize) { this.resourcesArray = resourcesArray; this.variablesArray = variablesArray; this.resourcesCount = resourcesCount; this.variablesByteSize = variablesByteSize; } /// /// Gets a with all the captured buffers. /// public ReadOnlySpan Resources { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { #if NET5_0 ref ulong r0 = ref MemoryMarshal.GetArrayDataReference(this.resourcesArray); ref D3D12_GPU_DESCRIPTOR_HANDLE r1 = ref Unsafe.As(ref r0); return MemoryMarshal.CreateReadOnlySpan(ref r1, this.resourcesCount); #else Span span = this.resourcesArray.AsSpan(0, this.resourcesCount); return MemoryMarshal.Cast(span); #endif } } /// /// Gets a with the padded data representing all the captured variables. /// public unsafe ReadOnlySpan Variables { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { #if NET5_0 ref byte r0 = ref MemoryMarshal.GetArrayDataReference(this.variablesArray); ref uint r1 = ref Unsafe.As(ref r0); int length = (int)((uint)this.variablesByteSize / sizeof(uint)); return MemoryMarshal.CreateReadOnlySpan(ref r1, length); #else Span span = this.variablesArray.AsSpan(0, this.variablesByteSize); return MemoryMarshal.Cast(span); #endif } } /// public void Dispose() { ArrayPool.Shared.Return(this.resourcesArray); ArrayPool.Shared.Return(this.variablesArray); } } }