diff --git a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs index 152c3681..f6b26f28 100644 --- a/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ConstantBuffer{T}.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Exceptions; using ComputeSharp.Graphics.Helpers; using SharpDX.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; @@ -38,7 +39,7 @@ namespace ComputeSharp.Graphics.Buffers /// /// 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[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + public T this[uint i] => throw new InvalidExecutionContextException($"{nameof(ConstantBuffer)}[uint i]"); /// public override void GetData(Span span, int offset, int count) diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs index f6aee2ec..1b4e1073 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyBuffer{T}.cs @@ -1,6 +1,6 @@ -using System; -using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Exceptions; namespace ComputeSharp.Graphics.Buffers { @@ -22,6 +22,6 @@ namespace ComputeSharp.Graphics.Buffers /// /// 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 T this[uint i] => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + public T this[uint i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer)}[uint i]"); } } diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs index b6f7cf2d..def4e799 100644 --- a/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteBuffer{T}.cs @@ -1,6 +1,6 @@ -using System; -using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Exceptions; namespace ComputeSharp.Graphics.Buffers { @@ -24,8 +24,8 @@ namespace ComputeSharp.Graphics.Buffers /// This API can only be used from a compute shader, and will always throw if used anywhere else public T this[uint i] { - get => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); - set => throw new InvalidOperationException("The indexer APIs can only be used from a compute shader"); + get => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer)}[uint i]"); + set => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer)}[uint i]"); } } } diff --git a/src/ComputeSharp.Graphics/Exceptions/InvalidExecutionContextException.cs b/src/ComputeSharp.Graphics/Exceptions/InvalidExecutionContextException.cs new file mode 100644 index 00000000..8e1a8628 --- /dev/null +++ b/src/ComputeSharp.Graphics/Exceptions/InvalidExecutionContextException.cs @@ -0,0 +1,19 @@ +using System; +using System.Runtime.CompilerServices; + +namespace ComputeSharp.Graphics.Exceptions +{ + /// + /// A custom that indicates when an HLSL-only API is called from C# + /// + public sealed class InvalidExecutionContextException : InvalidOperationException + { + /// + /// Creates a new instance + /// + /// The name of the caller API + internal InvalidExecutionContextException([CallerMemberName] string? name = null) + : base($"The API {name} can only be used from within an HLSL shader") + { } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index 466e4a24..f91bf5e4 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Reflection; using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis;