diff --git a/src/ComputeSharp.Shaders/Translation/Models/DispatchData2.cs b/src/ComputeSharp.Shaders/Translation/Models/DispatchData2.cs index d5c86fc9..0034c4fd 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/DispatchData2.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/DispatchData2.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; +using TerraFX.Interop; namespace ComputeSharp.Shaders.Translation.Models { @@ -12,9 +13,9 @@ namespace ComputeSharp.Shaders.Translation.Models internal readonly ref struct DispatchData2 { /// - /// The array with the captured buffers. + /// The array with the captured buffers. /// - private readonly GraphicsResource2[] resourcesArray; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE[] resourcesArray; /// /// The number of instances in . @@ -34,11 +35,11 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// Creates a new instance with the specified parameters. /// - /// The array with the captured buffers. - /// The number of instances in . + /// 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 DispatchData2(GraphicsResource2[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesByteSize) + public DispatchData2(D3D12_GPU_DESCRIPTOR_HANDLE[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesByteSize) { this.resourcesArray = resourcesArray; this.variablesArray = variablesArray; @@ -47,21 +48,21 @@ namespace ComputeSharp.Shaders.Translation.Models } /// - /// Gets a with all the captured buffers. + /// Gets a with all the captured buffers. /// - public ReadOnlySpan Resources + public ReadOnlySpan Resources { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - ref GraphicsResource2 r0 = ref MemoryMarshal.GetArrayDataReference(this.resourcesArray); + ref D3D12_GPU_DESCRIPTOR_HANDLE r0 = ref MemoryMarshal.GetArrayDataReference(this.resourcesArray); return MemoryMarshal.CreateReadOnlySpan(ref r0, this.resourcesCount); } } /// - /// Gets a with the padded data representing all the captured variables. + /// Gets a with the padded data representing all the captured variables. /// public ReadOnlySpan Variables { @@ -80,7 +81,7 @@ namespace ComputeSharp.Shaders.Translation.Models /// public void Dispose() { - ArrayPool.Shared.Return(this.resourcesArray, true); + ArrayPool.Shared.Return(this.resourcesArray); ArrayPool.Shared.Return(this.variablesArray); } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader2{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader2{T}.IL.cs index e44d03c1..efdb2d9b 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader2{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader2{T}.IL.cs @@ -2,12 +2,14 @@ using System.Buffers; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Translation.Models; +using TerraFX.Interop; namespace ComputeSharp.Shaders.Translation { @@ -18,9 +20,9 @@ namespace ComputeSharp.Shaders.Translation /// 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 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 GraphicsResource2 r0, ref byte r1); + private delegate void DispatchDataLoader(in T shader, ref D3D12_GPU_DESCRIPTOR_HANDLE r0, ref byte r1); /// /// The number of captured graphics resources (buffers). @@ -54,11 +56,11 @@ namespace ComputeSharp.Shaders.Translation public DispatchData2 GetDispatchData(in T shader, int x, int y, int z) { // Resources and variables buffers - GraphicsResource2[] resources = ArrayPool.Shared.Rent(this.totalResourceCount); + D3D12_GPU_DESCRIPTOR_HANDLE[] resources = ArrayPool.Shared.Rent(this.totalResourceCount); byte[] variables = ArrayPool.Shared.Rent(this.totalVariablesByteSize); - ref GraphicsResource2 r0 = ref MemoryMarshal.GetArrayDataReference(resources); - ref byte r1 = ref variables[0]; + ref D3D12_GPU_DESCRIPTOR_HANDLE r0 = ref MemoryMarshal.GetArrayDataReference(resources); + ref byte r1 = ref MemoryMarshal.GetArrayDataReference(variables); // Set the x, y and z counters Unsafe.As(ref r1) = x; @@ -87,9 +89,20 @@ namespace ComputeSharp.Shaders.Translation if (totalResourceCount > 0) { - il.EmitAddOffset(this.totalResourceCount); + il.EmitAddOffset(this.totalResourceCount); } + il.Emit(OpCodes.Ldarg_0); + il.EmitReadMember(member); + + // Access Buffer.D3D12GpuDescriptorHandle + FieldInfo gpuDescriptorInfo = member.MemberType.GetField( + nameof(Buffer2.D3D12GpuDescriptorHandle), + BindingFlags.NonPublic | BindingFlags.Instance)!; + + il.EmitReadMember(gpuDescriptorInfo); + il.EmitStoreToAddress(member.MemberType); + this.totalResourceCount++; } else if (HlslKnownTypes.IsKnownScalarType(member.MemberType) ||