Removed GraphicsResource class, code refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-20 01:05:11 +01:00
1 親 e34d050ab1
コミット 2ce7cbeb97
5ファイル変更
+30 -98

検索条件に一致するファイルはありません

-43
ファイルの表示
@@ -1,43 +0,0 @@
using ComputeSharp.Core.Interop;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Buffers.Abstract
{
/// <summary>
/// An abstract <see langword="class"/> that represents a graphics resource object.
/// </summary>
public abstract unsafe class GraphicsResource : NativeObject
{
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// Creates a new <see cref="GraphicsResource"/> instance with the specified parameters.
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
protected GraphicsResource(GraphicsDevice device, ComPtr<ID3D12Resource> d3d12resource)
{
this.d3D12Resource = d3d12resource;
GraphicsDevice = device;
}
/// <summary>
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <inheritdoc/>
protected override void OnDispose()
{
d3D12Resource.Dispose();
}
}
}
+27 -3
ファイルの表示
@@ -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 <see langword="class"/> representing a typed buffer stored on GPU memory.
/// </summary>
/// <typeparam name="T">The type of items stored on the buffer.</typeparam>
public unsafe class Buffer<T> : GraphicsResource
public unsafe class Buffer<T> : NativeObject
where T : unmanaged
{
/// <summary>
/// The <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
private ComPtr<ID3D12Resource> d3D12Resource;
/// <summary>
/// The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.
/// </summary>
@@ -57,14 +62,17 @@ namespace ComputeSharp.Graphics.Buffers
/// <param name="sizeInBytes">The size in bytes for the current buffer</param>
/// <param name="bufferType">The buffer type for the current buffer</param>
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<T>();
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
}
}
/// <summary>
/// Gets the <see cref="GraphicsDevice"/> associated with the current instance.
/// </summary>
public GraphicsDevice GraphicsDevice { get; }
/// <summary>
/// Gets the size of the current buffer, as in the number of <typeparamref name="T"/> values it contains
/// </summary>
@@ -88,6 +101,11 @@ namespace ComputeSharp.Graphics.Buffers
/// </summary>
internal bool IsPaddingPresent => PaddedElementSizeInBytes > ElementSizeInBytes;
/// <summary>
/// Gets the <see cref="ID3D12Resource"/> instance currently mapped.
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Creates a committed resource for a given buffer type.
/// </summary>
@@ -152,5 +170,11 @@ namespace ComputeSharp.Graphics.Buffers
GraphicsDevice.D3D12Device->CreateUnorderedAccessView(D3D12Resource, null, &d3D12UnorderedAccessViewDescription, D3D12CpuDescriptorHandle);
}
/// <inheritdoc/>
protected override void OnDispose()
{
this.d3D12Resource.Dispose();
}
}
}
+2 -2
ファイルの表示
@@ -92,10 +92,10 @@ namespace ComputeSharp.Graphics.Commands
}
/// <summary>
/// Binds an input <see cref="GraphicsResource"/> object to a specified root parameter.
/// Binds an input <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to a specified root parameter.
/// </summary>
/// <param name="rootParameterIndex">The root parameter index to bind to the input resource.</param>
/// <param name="resource">The input <see cref="GraphicsResource"/> instance to bind.</param>
/// <param name="d3D12GpuDescriptorHandle">The input <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to bind.</param>
public readonly void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
{
this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle);
-48
ファイルの表示
@@ -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
{
/// <summary>
/// A custom <see cref="GraphicsDeviceMismatchException"/> that indicates when mismatched devices are being used
/// </summary>
public sealed class GraphicsDeviceMismatchException : InvalidOperationException
{
/// <summary>
/// Creates a new <see cref="GraphicsDeviceMismatchException"/> instance
/// </summary>
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();
}
}
}
+1 -2
ファイルの表示
@@ -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;
/// <summary>
/// The number of <see cref="GraphicsResource"/> instances in <see cref="resourcesArray"/>.
/// The number of <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> values in <see cref="resourcesArray"/>.
/// </summary>
private readonly int resourcesCount;