using System; using System.Diagnostics.Contracts; using System.Text; using ComputeSharp.Core.Interop; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; namespace ComputeSharp.Exceptions { /// /// A custom that indicates when mismatched devices are being used. /// public sealed class GraphicsDeviceMismatchException : InvalidOperationException { /// /// Creates a new instance. /// /// The message that describes the error. private GraphicsDeviceMismatchException(string message) : base(message) { } /// /// Creates a new instance from the specified parameters. /// /// The input that was used. /// The source instance tied to . /// The target instance that was used. /// A new instance with a formatted error message. /// /// This method only takes a instance and the associated instance as /// will still be available, but without the unnecessary generic type specializations for the method. /// [Pure] private static GraphicsDeviceMismatchException Create(NativeObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice) { StringBuilder builder = new(512); builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers."); builder.AppendLine($"The target device to run the compute shader is \"{destinationDevice}\"."); builder.AppendLine($"The buffer of type {resource.GetType()} was allocated on device \"{sourceDevice}\"."); builder.Append("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); builder.ToString(); return new(builder.ToString()); } /// /// Throws a new instance from the specified parameters. /// /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. [Pure] internal static void Throw(Buffer buffer, GraphicsDevice device) where T : unmanaged { throw Create(buffer, buffer.GraphicsDevice, device); } /// /// Throws a new instance from the specified parameters. /// /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. [Pure] internal static void Throw(Texture2D texture, GraphicsDevice device) where T : unmanaged { throw Create(texture, texture.GraphicsDevice, device); } } }