From 07b3fb14b504ca02c4774118b2b1a3d4334773ea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Nov 2020 12:19:43 +0100 Subject: [PATCH] Improved debug support for Buffer types --- .../Buffers/ConstantBuffer{T}.cs | 10 +++++++ .../Buffers/ReadOnlyBuffer{T}.cs | 12 +++++++- .../Buffers/ReadWriteBuffer{T}.cs | 12 +++++++- .../Buffers/Views/BufferDebugView{T}.cs | 28 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 2264c206..53250263 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -7,6 +8,7 @@ using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; using ComputeSharp.Graphics.Buffers.Interop; +using ComputeSharp.Graphics.Buffers.Views; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; @@ -18,6 +20,8 @@ namespace ComputeSharp /// A representing a typed read write buffer stored on GPU memory /// /// The type of items stored on the buffer + [DebuggerTypeProxy(typeof(BufferDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] public sealed class ConstantBuffer : Buffer where T : unmanaged { @@ -107,5 +111,11 @@ namespace ComputeSharp } else SetDataWithCpuBuffer(buffer); } + + /// + public override string ToString() + { + return $"ComputeSharp.ConstantBuffer<{typeof(T)}>[{Size}]"; + } } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index 67b39335..e52fb41e 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -1,7 +1,9 @@ -using ComputeSharp.Exceptions; +using System.Diagnostics; +using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; namespace ComputeSharp { @@ -9,6 +11,8 @@ namespace ComputeSharp /// A representing a typed readonly buffer stored on GPU memory /// /// The type of items stored on the buffer + [DebuggerTypeProxy(typeof(BufferDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadOnlyBuffer : StructuredBuffer where T : unmanaged { @@ -28,5 +32,11 @@ namespace ComputeSharp /// The index of the value to get /// This API can only be used from a compute shader, and will always throw if used anywhere else public T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer)}[int]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyBuffer<{typeof(T)}>[{Size}]"; + } } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index 4d7494ba..805b0516 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,7 +1,9 @@ -using ComputeSharp.Exceptions; +using System.Diagnostics; +using ComputeSharp.Exceptions; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; namespace ComputeSharp { @@ -9,6 +11,8 @@ namespace ComputeSharp /// A representing a typed read write buffer stored on GPU memory /// /// The type of items stored on the buffer + [DebuggerTypeProxy(typeof(BufferDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] public sealed class ReadWriteBuffer : StructuredBuffer where T : unmanaged { @@ -28,5 +32,11 @@ namespace ComputeSharp /// The index of the value to get or set /// This API can only be used from a compute shader, and will always throw if used anywhere else public ref T this[int i] => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer)}[int]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteBuffer<{typeof(T)}>[{Size}]"; + } } } diff --git a/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs new file mode 100644 index 00000000..61aa2951 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Views/BufferDebugView{T}.cs @@ -0,0 +1,28 @@ +using System.Diagnostics; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp.Graphics.Buffers.Views +{ + /// + /// A debug proxy used to display items in a instance. + /// + /// The type of items to display. + internal sealed class BufferDebugView + where T : unmanaged + { + /// + /// Initializes a new instance of the class with the specified parameters. + /// + /// The input instance with the items to display. + public BufferDebugView(Buffer? buffer) + { + Items = buffer?.GetData(); + } + + /// + /// Gets the items to display for the current instance + /// + [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)] + public T[]? Items { get; } + } +}