From 00bce20a34daba5ee080699be5fe87c3bcbd7fe5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 18 Nov 2020 02:47:13 +0100 Subject: [PATCH] Added Luid type and property --- .../GraphicsDeviceMismatchException.cs | 6 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 6 ++ src/ComputeSharp.Graphics/Luid.cs | 71 +++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Luid.cs diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 20ab7cbb..0ed136f0 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -28,7 +28,7 @@ namespace ComputeSharp.Exceptions 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.D3D12Device->GetAdapterLuid().Equals(resource.GraphicsDevice.D3D12Device->GetAdapterLuid())) + 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."); @@ -36,8 +36,8 @@ namespace ComputeSharp.Exceptions else { // Actually different devices - builder.AppendLine($"The target device to run the compute shader is \"{device.Name}\" (id: {device.D3D12Device->GetAdapterLuid()})."); - builder.AppendLine($"The buffer of type {resource.GetType().ToFriendlyString()} was allocated on device \"{resource.GraphicsDevice.Name}\" (id: {resource.GraphicsDevice.D3D12Device->GetAdapterLuid()})."); + 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."); } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index b0a1352b..58d5fb97 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -81,6 +81,7 @@ namespace ComputeSharp.Graphics this.copyCommandAllocatorPool = new ID3D12CommandAllocatorPool(D3D12_COMMAND_LIST_TYPE_COPY); this.shaderResourceViewDescriptorAllocator = new DescriptorAllocator(d3d12device); + Luid = *(Luid*)&dxgiDescription1->AdapterLuid; Name = new string((char*)dxgiDescription1->Description); MemorySize = dxgiDescription1->DedicatedVideoMemory; @@ -90,6 +91,11 @@ namespace ComputeSharp.Graphics WavefrontSize = d3D12Options1Data.WaveLaneCountMin; } + /// + /// Gets the locally unique identifier for the current device. + /// + public Luid Luid { get; } + /// /// Gets the name of the current instance. /// diff --git a/src/ComputeSharp.Graphics/Luid.cs b/src/ComputeSharp.Graphics/Luid.cs new file mode 100644 index 00000000..7123b1b3 --- /dev/null +++ b/src/ComputeSharp.Graphics/Luid.cs @@ -0,0 +1,71 @@ +using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace ComputeSharp +{ + /// + /// A locally unique identifier for a graphics device. + /// + public readonly struct Luid : IEquatable + { + private readonly uint lowPart; + private readonly int highPart; + + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Luid other) + { + return + this.lowPart == other.lowPart && + this.highPart == other.highPart; + } + + /// + [Pure] + public override bool Equals(object? other) + { + return other is Luid luid && Equals(luid); + } + + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override int GetHashCode() + { + return HashCode.Combine(this.lowPart, this.highPart); + } + + /// + [Pure] + public override string ToString() + { + return (((long)this.highPart) << 32 | this.lowPart).ToString(); + } + + /// + /// Check whether two values are equal. + /// + /// The first value to compare. + /// The second value to compare. + /// Whether and are the same. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Luid a, Luid b) + { + return a.Equals(b); + } + + /// + /// Check whether two values are different. + /// + /// The first value to compare. + /// The second value to compare. + /// Whether and are different. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Luid a, Luid b) + { + return !a.Equals(b); + } + } +}