diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs deleted file mode 100644 index 0b5a258a..00000000 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/GraphicsResource.cs +++ /dev/null @@ -1,43 +0,0 @@ -using ComputeSharp.Core.Interop; -using TerraFX.Interop; - -namespace ComputeSharp.Graphics.Buffers.Abstract -{ - /// - /// An abstract that represents a graphics resource object. - /// - public abstract unsafe class GraphicsResource : NativeObject - { - /// - /// The instance currently mapped. - /// - private ComPtr d3D12Resource; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The associated with the current instance. - protected GraphicsResource(GraphicsDevice device, ComPtr d3d12resource) - { - this.d3D12Resource = d3d12resource; - - GraphicsDevice = device; - } - - /// - /// Gets the associated with the current instance. - /// - public GraphicsDevice GraphicsDevice { get; } - - /// - /// Gets the instance currently mapped. - /// - internal ID3D12Resource* D3D12Resource => this.d3D12Resource; - - /// - protected override void OnDispose() - { - d3D12Resource.Dispose(); - } - } -} diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs index 263a9c8b..872c4d58 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs @@ -1,5 +1,5 @@ using System.Runtime.CompilerServices; -using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop; @@ -16,9 +16,14 @@ namespace ComputeSharp.Graphics.Buffers /// A representing a typed buffer stored on GPU memory. /// /// The type of items stored on the buffer. - public unsafe class Buffer : GraphicsResource + public unsafe class Buffer : NativeObject where T : unmanaged { + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + /// /// The instance for the current resource. /// @@ -57,14 +62,17 @@ namespace ComputeSharp.Graphics.Buffers /// The size in bytes for the current buffer /// The buffer type for the current buffer internal Buffer(GraphicsDevice device, int size, int sizeInBytes, BufferType bufferType) - : base(device, CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes)) { + this.d3D12Resource = CreateCommittedResource(device.D3D12Device, bufferType, sizeInBytes); + Size = size; SizeInBytes = sizeInBytes; ElementSizeInBytes = Unsafe.SizeOf(); BufferType = bufferType; PaddedElementSizeInBytes = sizeInBytes / size; + GraphicsDevice = device; + if (bufferType is BufferType.Constant or BufferType.ReadOnly or BufferType.ReadWrite) { GraphicsDevice.AllocateShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -78,6 +86,11 @@ namespace ComputeSharp.Graphics.Buffers } } + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + /// /// Gets the size of the current buffer, as in the number of values it contains /// @@ -88,6 +101,11 @@ namespace ComputeSharp.Graphics.Buffers /// internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes; + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// /// Creates a committed resource for a given buffer type. /// @@ -152,5 +170,11 @@ namespace ComputeSharp.Graphics.Buffers GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle); } + + /// + protected override void OnDispose() + { + this.d3D12Resource.Dispose(); + } } } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index beda0a6c..73b5b8c4 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -92,10 +92,10 @@ namespace ComputeSharp.Graphics.Commands } /// - /// Binds an input object to a specified root parameter. + /// Binds an input value to a specified root parameter. /// /// The root parameter index to bind to the input resource. - /// The input instance to bind. + /// The input value to bind. public readonly void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle); diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs deleted file mode 100644 index 0ed136f0..00000000 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Diagnostics.Contracts; -using System.Text; -using ComputeSharp.Graphics; -using ComputeSharp.Graphics.Buffers.Abstract; -using ComputeSharp.Graphics.Extensions; - -namespace ComputeSharp.Exceptions -{ - /// - /// A custom that indicates when mismatched devices are being used - /// - public sealed class GraphicsDeviceMismatchException : InvalidOperationException - { - /// - /// Creates a new instance - /// - internal GraphicsDeviceMismatchException(GraphicsDevice device, GraphicsResource resource) - : base(GetExceptionMessage(device, resource)) - { - } - - // Gets a proper exception message for a pair of device and resource - [Pure] - private static unsafe string GetExceptionMessage(GraphicsDevice device, GraphicsResource resource) - { - StringBuilder builder = new StringBuilder(); - builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers."); - - // Check if the device is effectively the same but with two instances pointing to the same adapter - if (device.Luid == resource.GraphicsDevice.Luid) - { - builder.AppendLine("The graphics devices used to allocate buffers and run the shader must be the same instance, not just be using the same physical device."); - builder.AppendLine("You can still run different shaders on different devices, but a shader run on one device must only use buffers allocated by the same device instance."); - } - else - { - // Actually different devices - builder.AppendLine($"The target device to run the compute shader is \"{device.Name}\" (id: {device.Luid})."); - builder.AppendLine($"The buffer of type {resource.GetType().ToFriendlyString()} was allocated on device \"{resource.GraphicsDevice.Name}\" (id: {resource.GraphicsDevice.Luid})."); - builder.AppendLine("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); - } - - - return builder.ToString(); - } - } -} diff --git a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs index 2f40566e..7b1d16c5 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs @@ -2,7 +2,6 @@ using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.Graphics.Buffers.Abstract; using TerraFX.Interop; namespace ComputeSharp.Shaders.Translation.Models @@ -18,7 +17,7 @@ namespace ComputeSharp.Shaders.Translation.Models private readonly D3D12_GPU_DESCRIPTOR_HANDLE[] resourcesArray; /// - /// The number of instances in . + /// The number of values in . /// private readonly int resourcesCount;