Added buffer/device validation on loading
このコミットが含まれているのは:
1 親
c2a1980596
コミット
2dfd8855f4
4ファイル変更
+84
-13
検索条件に一致するファイルはありません
@@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a <see cref="GraphicsDeviceMismatchException"/> if the target device doesn't match the current one.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void ThrowIfDeviceMismatch(GraphicsDevice device)
|
||||
{
|
||||
if (GraphicsDevice != device)
|
||||
{
|
||||
void Throw() => throw GraphicsDeviceMismatchException.Create(this, device);
|
||||
|
||||
Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Text;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
|
||||
namespace ComputeSharp.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom <see cref="Exception"/> that indicates when mismatched devices are being used.
|
||||
/// </summary>
|
||||
public sealed class GraphicsDeviceMismatchException : InvalidOperationException
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDeviceMismatchException"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that describes the error.</param>
|
||||
private GraphicsDeviceMismatchException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="GraphicsDeviceMismatchException"/> instance from the specified parameters.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of values in the input buffer.</typeparam>
|
||||
/// <param name="buffer">The input <see cref="Buffer{T}"/> that was used.</param>
|
||||
/// <param name="device">The target <see cref="GraphicsDevice"/> instance that was used.</param>
|
||||
/// <returns>A new <see cref="GraphicsDeviceMismatchException"/> instance with a formatted error message.</returns>
|
||||
[Pure]
|
||||
public static GraphicsDeviceMismatchException Create<T>(Buffer<T> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<D3D12_GPU_DESCRIPTOR_HANDLE> resources = dispatchData.Resources;
|
||||
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// A <see langword="delegate"/> that loads all the captured members from a shader instance.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
|
||||
/// <param name="shader">The shader instance to load the data from.</param>
|
||||
/// <param name="r0">A reference to the buffer with all the captured <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> instances.</param>
|
||||
/// <param name="r1">A reference to the buffer to use to store all the captured value types.</param>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// The number of captured graphics resources (buffers).
|
||||
@@ -49,12 +51,13 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// Gets a new <see cref="DispatchData"/> instance with all the captured data for the current shader instance.
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
|
||||
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
|
||||
/// <param name="x">The number of iterations to run on the X axis.</param>
|
||||
/// <param name="y">The number of iterations to run on the Y axis.</param>
|
||||
/// <param name="z">The number of iterations to run on the Z axis.</param>
|
||||
[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<D3D12_GPU_DESCRIPTOR_HANDLE>.Shared.Rent(this.totalResourceCount);
|
||||
@@ -69,7 +72,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
Unsafe.Add(ref Unsafe.As<byte, int>(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
|
||||
/// </summary>
|
||||
private void BuildDispatchDataLoader()
|
||||
{
|
||||
// The delegate signature is <in T, ref D3D12_GPU_DESCRIPTOR_HANDLE, ref byte, void>.
|
||||
// The delegate signature is <GraphicsDevice, in T, ref D3D12_GPU_DESCRIPTOR_HANDLE, ref byte, void>.
|
||||
//
|
||||
// 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<DispatchDataLoader>.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<D3D12_GPU_DESCRIPTOR_HANDLE>(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<T>.ThrowIfDeviceMismatch(GraphicsDevice)
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.EmitCall(OpCodes.Callvirt, member.MemberType.GetMethod(
|
||||
nameof(Buffer<byte>.ThrowIfDeviceMismatch),
|
||||
BindingFlags.Instance | BindingFlags.NonPublic)!, null);
|
||||
|
||||
// Access Buffer<T>.D3D12GpuDescriptorHandle
|
||||
il.EmitReadMember(member.MemberType.GetField(
|
||||
nameof(Buffer<byte>.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);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする