diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
index ac0cd0d9..fcb643e4 100644
--- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
+++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs
@@ -3,6 +3,7 @@ using System.Buffers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Interop;
+using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Buffers.Enums;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop;
@@ -234,5 +235,19 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
{
this.d3D12Resource.Dispose();
}
+
+ ///
+ /// Throws a if the target device doesn't match the current one.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal void ThrowIfDeviceMismatch(GraphicsDevice device)
+ {
+ if (GraphicsDevice != device)
+ {
+ void Throw() => throw GraphicsDeviceMismatchException.Create(this, device);
+
+ Throw();
+ }
+ }
}
}
diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs
new file mode 100644
index 00000000..9fb31b09
--- /dev/null
+++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Diagnostics.Contracts;
+using System.Text;
+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 type of values in the input buffer.
+ /// The input that was used.
+ /// The target instance that was used.
+ /// A new instance with a formatted error message.
+ [Pure]
+ public static GraphicsDeviceMismatchException Create(Buffer buffer, GraphicsDevice device)
+ where T : unmanaged
+ {
+ 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 \"{device}\".");
+ builder.AppendLine($"The buffer of type {buffer.GetType()} was allocated on device \"{buffer.GraphicsDevice}\".");
+ builder.AppendLine("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());
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs
index dc779697..753b42b8 100644
--- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs
+++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs
@@ -123,7 +123,7 @@ namespace ComputeSharp.Shaders
commandList.SetPipelineData(pipelineData);
// Extract the dispatch data for the shader invocation
- using DispatchData dispatchData = shaderData.Loader.GetDispatchData(in shader, x, y, z);
+ using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, z);
// Load the captured buffers
ReadOnlySpan resources = dispatchData.Resources;
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs
index 4f0a181c..cc2b52fe 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs
@@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Core.Helpers;
using ComputeSharp.Core.Interop;
+using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Shaders.Mappings;
using ComputeSharp.Shaders.Translation.Models;
@@ -20,10 +21,11 @@ namespace ComputeSharp.Shaders.Translation
///
/// A that loads all the captured members from a shader instance.
///
+ /// The to use to run the shader.
/// The shader instance to load the data from.
/// A reference to the buffer with all the captured instances.
/// A reference to the buffer to use to store all the captured value types.
- private delegate void DispatchDataLoader(in T shader, ref D3D12_GPU_DESCRIPTOR_HANDLE r0, ref byte r1);
+ private delegate void DispatchDataLoader(GraphicsDevice device, in T shader, ref D3D12_GPU_DESCRIPTOR_HANDLE r0, ref byte r1);
///
/// The number of captured graphics resources (buffers).
@@ -49,12 +51,13 @@ namespace ComputeSharp.Shaders.Translation
///
/// Gets a new instance with all the captured data for the current shader instance.
///
+ /// The to use to run the shader.
/// The input instance representing the compute shader to run.
/// The number of iterations to run on the X axis.
/// The number of iterations to run on the Y axis.
/// The number of iterations to run on the Z axis.
[Pure]
- public DispatchData GetDispatchData(in T shader, int x, int y, int z)
+ public DispatchData GetDispatchData(GraphicsDevice device, in T shader, int x, int y, int z)
{
// Resources and variables buffers
D3D12_GPU_DESCRIPTOR_HANDLE[] resources = ArrayPool.Shared.Rent(this.totalResourceCount);
@@ -69,7 +72,7 @@ namespace ComputeSharp.Shaders.Translation
Unsafe.Add(ref Unsafe.As(ref r1), 2) = z;
// Invoke the dynamic method to extract the captured data
- this.dispatchDataLoader!(in shader, ref r0, ref r1);
+ this.dispatchDataLoader!(device, in shader, ref r0, ref r1);
return new DispatchData(resources, this.totalResourceCount, variables, this.totalVariablesByteSize);
}
@@ -79,11 +82,12 @@ namespace ComputeSharp.Shaders.Translation
///
private void BuildDispatchDataLoader()
{
- // The delegate signature is .
+ // The delegate signature is .
//
- // ldarg.0 = in T shader
- // ldarg.1 = ref D3D12_GPU_DESCRIPTOR_HANDLE r0
- // ldarg.1 = ref byte r1
+ // ldarg.0 = GraphicsDevice
+ // ldarg.1 = in T shader
+ // ldarg.2 = ref D3D12_GPU_DESCRIPTOR_HANDLE r0
+ // ldarg.3 = ref byte r1
this.dispatchDataLoader = DynamicMethod.New(il =>
{
foreach (ReadableMember member in this.capturedMembers)
@@ -91,14 +95,14 @@ namespace ComputeSharp.Shaders.Translation
if (HlslKnownTypes.IsKnownBufferType(member.MemberType))
{
// Load the offset address into the resource buffers
- il.Emit(OpCodes.Ldarg_1);
+ il.Emit(OpCodes.Ldarg_2);
if (totalResourceCount > 0)
{
il.EmitAddOffset(this.totalResourceCount);
}
- if (!member.IsStatic) il.Emit(OpCodes.Ldarg_0);
+ if (!member.IsStatic) il.Emit(OpCodes.Ldarg_1);
il.EmitReadMember(member);
// Call NativeObject.ThrowIfDisposed()
@@ -107,10 +111,17 @@ namespace ComputeSharp.Shaders.Translation
nameof(NativeObject.ThrowIfDisposed),
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
+ // Call Buffer.ThrowIfDeviceMismatch(GraphicsDevice)
+ il.Emit(OpCodes.Dup);
+ il.Emit(OpCodes.Ldarg_0);
+ il.EmitCall(OpCodes.Callvirt, member.MemberType.GetMethod(
+ nameof(Buffer.ThrowIfDeviceMismatch),
+ BindingFlags.Instance | BindingFlags.NonPublic)!, null);
+
// Access Buffer.D3D12GpuDescriptorHandle
il.EmitReadMember(member.MemberType.GetField(
nameof(Buffer.D3D12GpuDescriptorHandle),
- BindingFlags.NonPublic | BindingFlags.Instance)!);
+ BindingFlags.Instance | BindingFlags.NonPublic)!);
il.EmitStoreToAddress(typeof(D3D12_GPU_DESCRIPTOR_HANDLE));
this.totalResourceCount++;
@@ -129,10 +140,10 @@ namespace ComputeSharp.Shaders.Translation
}
// Load the target address into the variables buffer
- il.Emit(OpCodes.Ldarg_2);
+ il.Emit(OpCodes.Ldarg_3);
il.EmitAddOffset(totalVariablesByteSize);
- if (!member.IsStatic) il.Emit(OpCodes.Ldarg_0);
+ if (!member.IsStatic) il.Emit(OpCodes.Ldarg_1);
il.EmitReadMember(member);
il.EmitStoreToAddress(member.MemberType);