From f759e8b495dec3e3d94894aba6139e3f4d19d4a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Aug 2020 14:01:56 +0200 Subject: [PATCH] Added validation for some DirectX calls --- src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs | 11 ++++++++++- .../Commands/CommandAllocatorPool.cs | 13 ++++++++++++- src/ComputeSharp.Graphics/Commands/CommandList.cs | 12 +++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs index aefda97e..1d8f881c 100644 --- a/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Buffer{T}.cs @@ -1,8 +1,10 @@ using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Enums; +using SharpGen.Runtime; using Vortice.Direct3D12; namespace ComputeSharp.Graphics.Buffers @@ -41,7 +43,14 @@ namespace ComputeSharp.Graphics.Buffers // Create the native resource ResourceDescription description = ResourceDescription.Buffer(sizeInBytes, flags); - NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states); + Result result = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(heapType), HeapFlags.None, description, states, out ID3D12Resource? resource); + + if (result.Failure) + { + throw new COMException("Failed to create the committed resource", result.Code); + } + + NativeResource = resource!; // Create the resource handles, if needed (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = bufferType switch diff --git a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs index 0081dbce..700f632c 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandAllocatorPool.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; using ComputeSharp.Graphics.Commands.Abstract; +using SharpGen.Runtime; using Vortice.Direct3D12; namespace ComputeSharp.Graphics.Commands @@ -40,6 +42,7 @@ namespace ComputeSharp.Graphics.Commands { lock (CommandAllocatorQueue) { + // Reuse an existing command allocator, if one is present in the queue if (CommandAllocatorQueue.Count > 0) { (ID3D12CommandAllocator commandAllocator, long fenceValue) = CommandAllocatorQueue.Peek(); @@ -61,7 +64,15 @@ namespace ComputeSharp.Graphics.Commands } } - return GraphicsDevice.NativeDevice.CreateCommandAllocator(CommandListType); + // Create a new command allocator + Result result = GraphicsDevice.NativeDevice.CreateCommandAllocator(CommandListType, out ID3D12CommandAllocator? nativeCommandAllocator); + + if (result.Failure) + { + throw new COMException("Failed to create the command allocator", result.Code); + } + + return nativeCommandAllocator!; } } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 5de7d30f..8ae8c766 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -1,7 +1,9 @@ using System; +using System.Runtime.InteropServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Commands.Abstract; +using SharpGen.Runtime; using Vortice.Direct3D12; namespace ComputeSharp.Graphics.Commands @@ -21,7 +23,15 @@ namespace ComputeSharp.Graphics.Commands CommandListType.Direct => GraphicsDevice.DirectAllocatorPool.GetCommandAllocator(), _ => throw new NotSupportedException($"Unsupported command list type with value {CommandListType}") }; - NativeCommandList = GraphicsDevice.NativeDevice.CreateCommandList(0, CommandListType, CommandAllocator, null); + + Result result = GraphicsDevice.NativeDevice.CreateCommandList(0, commandListType, CommandAllocator, null, out ID3D12GraphicsCommandList? nativeCommandList); + + if (result.Failure) + { + throw new COMException("Failed to create the commands list", result.Code); + } + + NativeCommandList = nativeCommandList!; // Set the heap descriptor if the command list is not for copy operations if (CommandListType != CommandListType.Copy)