From 3818fef1d0e4dc984125f2804c45e454186c8c98 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Nov 2020 19:41:22 +0100 Subject: [PATCH] Refactored extensions to return ComPtr --- .../Extensions/ID3D12DeviceExtensions.cs | 54 ++++++++++--------- .../Extensions/ID3D12DeviceExtensions.cs | 31 +++++------ .../Translation/ShaderCompiler2.cs | 7 ++- 3 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index d76624a7..b126b97f 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -24,24 +24,25 @@ namespace ComputeSharp.Graphics.Extensions /// The type of command queue to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12CommandQueue* CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type) + public static ComPtr CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type) { + using ComPtr d3D12CommandQueue = default; + D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc; d3d12CommandQueueDesc.Type = type; d3d12CommandQueueDesc.Priority = 0; d3d12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; d3d12CommandQueueDesc.NodeMask = 0; Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue; - ID3D12CommandQueue* commandQueue; int result = d3d12device.CreateCommandQueue( &d3d12CommandQueueDesc, &d3d12CommandQueueDescGuid, - (void**)&commandQueue); + d3D12CommandQueue.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return commandQueue; + return d3D12CommandQueue.Move(); } /// @@ -50,20 +51,21 @@ namespace ComputeSharp.Graphics.Extensions /// The target to use to create the fence. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12Fence* CreateFence(this ref ID3D12Device d3d12device) + public static ComPtr CreateFence(this ref ID3D12Device d3d12device) { + using ComPtr d3d12Fence = default; + Guid d3D12FenceGuid = FX.IID_ID3D12Fence; - ID3D12Fence* d3d12Fence; int result = d3d12device.CreateFence( 0, D3D12_FENCE_FLAG_NONE, &d3D12FenceGuid, - (void**)&d3d12Fence); + d3d12Fence.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3d12Fence; + return d3d12Fence.Move(); } /// @@ -73,24 +75,25 @@ namespace ComputeSharp.Graphics.Extensions /// The number of descriptors to allocate. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12DescriptorHeap* CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount) + public static ComPtr CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount) { + using ComPtr d3d12DescriptorHeap = default; + D3D12_DESCRIPTOR_HEAP_DESC d3d12DescriptorHeapDesc; d3d12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; d3d12DescriptorHeapDesc.NumDescriptors = descriptorsCount; d3d12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; d3d12DescriptorHeapDesc.NodeMask = 0; Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap; - ID3D12DescriptorHeap* d3d12DescriptorHeap; int result = d3d12device.CreateDescriptorHeap( &d3d12DescriptorHeapDesc, &d3d12DescriptorHeapDescGuid, - (void**)&d3d12DescriptorHeap); + d3d12DescriptorHeap.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3d12DescriptorHeap; + return d3d12DescriptorHeap.Move(); } /// @@ -100,13 +103,15 @@ namespace ComputeSharp.Graphics.Extensions /// The number of descriptors to allocate. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ID3D12Resource* CreateCommittedResource( + public static ComPtr CreateCommittedResource( this ref ID3D12Device d3d12device, D3D12_HEAP_TYPE d3D12HeapType, ulong width, D3D12_RESOURCE_FLAGS d3D12ResourceFlags, D3D12_RESOURCE_STATES d3D12ResourceStates) { + using ComPtr d3D12Resource = default; + D3D12_HEAP_PROPERTIES d3D12HeapProperties; d3D12HeapProperties.Type = d3D12HeapType; d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; @@ -115,7 +120,6 @@ namespace ComputeSharp.Graphics.Extensions d3D12HeapProperties.VisibleNodeMask = 1; D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(width, d3D12ResourceFlags); Guid d3D12ResourceGuid = FX.IID_ID3D12Resource; - ID3D12Resource* d3D12Resource; int result = d3d12device.CreateCommittedResource( &d3D12HeapProperties, @@ -124,11 +128,11 @@ namespace ComputeSharp.Graphics.Extensions d3D12ResourceStates, null, &d3D12ResourceGuid, - (void**)&d3D12Resource); + d3D12Resource.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3D12Resource; + return d3D12Resource.Move(); } /// @@ -138,21 +142,22 @@ namespace ComputeSharp.Graphics.Extensions /// The type of command list allocator to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command allocator fails. - public static ID3D12CommandAllocator* CreateCommandAllocator( + public static ComPtr CreateCommandAllocator( this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType) { - ID3D12CommandAllocator* d3D12CommandAllocator; + using ComPtr d3D12CommandAllocator = default; + Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator; int result = d3d12device.CreateCommandAllocator( d3d12CommandListType, &d3D12CommandAllocatorGuid, - (void**)&d3D12CommandAllocator); + d3D12CommandAllocator.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3D12CommandAllocator; + return d3D12CommandAllocator.Move(); } /// @@ -162,12 +167,13 @@ namespace ComputeSharp.Graphics.Extensions /// The type of command list to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command list fails. - public static ID3D12GraphicsCommandList* CreateCommandList( + public static ComPtr CreateCommandList( this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE d3d12CommandListType, ID3D12CommandAllocator* d3D12CommandAllocator) { - ID3D12GraphicsCommandList* d3d12GraphicsCommandList; + using ComPtr d3d12GraphicsCommandList = default; + Guid d3D12GraphicsCommandListGuid = FX.IID_ID3D12GraphicsCommandList; int result = d3d12device.CreateCommandList( @@ -176,11 +182,11 @@ namespace ComputeSharp.Graphics.Extensions d3D12CommandAllocator, null, &d3D12GraphicsCommandListGuid, - (void**)&d3d12GraphicsCommandList); + d3d12GraphicsCommandList.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3d12GraphicsCommandList; + return d3d12GraphicsCommandList.Move(); } /// diff --git a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs index 382593a5..6bb97ae7 100644 --- a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs @@ -20,11 +20,14 @@ namespace ComputeSharp.Shaders.Extensions /// The input descriptor ranges for the signature to create. /// A pointer to the newly allocated instance. /// Thrown when the creation of the root signature fails. - public static ID3D12RootSignature* CreateRootSignature( + public static ComPtr CreateRootSignature( this ref ID3D12Device d3d12device, ReadOnlySpan d3D12DescriptorRanges1) { - ID3DBlob* d3D3Blob, d3D3BlobError; + using ComPtr d3D3Blob = default; + using ComPtr d3D3BlobError = default; + using ComPtr d3D12RootSignature = default; + int result; fixed (D3D12_DESCRIPTOR_RANGE1* d3D12DescriptorRange1 = d3D12DescriptorRanges1) @@ -51,27 +54,24 @@ namespace ComputeSharp.Shaders.Extensions // we just work with the resulting blobs, so the input data can be unpinned. result = FX.D3D12SerializeVersionedRootSignature( &d3d12VersionedRootSignatureDescription, - &d3D3Blob, - &d3D3BlobError); + d3D3Blob.GetAddressOf(), + d3D3BlobError.GetAddressOf()); ThrowHelper.ThrowIfFailed(result, d3D3BlobError); } Guid d3D12RootSignatureGuid = FX.IID_ID3D12RootSignature; - ID3D12RootSignature* d3D12RootSignature; result = d3d12device.CreateRootSignature( 0, - d3D3Blob->GetBufferPointer(), - d3D3Blob->GetBufferSize(), + d3D3Blob.Get()->GetBufferPointer(), + d3D3Blob.Get()->GetBufferSize(), &d3D12RootSignatureGuid, - (void**)&d3D12RootSignature); - - d3D3Blob->Release(); + d3D12RootSignature.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3D12RootSignature; + return d3D12RootSignature.Move(); } /// @@ -82,11 +82,13 @@ namespace ComputeSharp.Shaders.Extensions /// The shader bytecode to use for the pipeline state. /// A pointer to the newly allocated instance. /// Thrown when the creation of the pipeline state fails. - public static ID3D12PipelineState* CreateComputePipelineState( + public static ComPtr CreateComputePipelineState( this ref ID3D12Device d3d12device, ID3D12RootSignature* d3D12RootSignature, D3D12_SHADER_BYTECODE d3d12ShaderBytecode) { + using ComPtr d3D12PipelineState = default; + D3D12_COMPUTE_PIPELINE_STATE_DESC d3d12ComputePipelineStateDescription; d3d12ComputePipelineStateDescription.pRootSignature = d3D12RootSignature; d3d12ComputePipelineStateDescription.CS = d3d12ShaderBytecode; @@ -94,16 +96,15 @@ namespace ComputeSharp.Shaders.Extensions d3d12ComputePipelineStateDescription.CachedPSO = default; d3d12ComputePipelineStateDescription.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; Guid d3d12ComputePipelineStateGuid = FX.IID_ID3D12PipelineState; - ID3D12PipelineState* d3D12PipelineState; int result = d3d12device.CreateComputePipelineState( &d3d12ComputePipelineStateDescription, &d3d12ComputePipelineStateGuid, - (void**)&d3D12PipelineState); + d3D12PipelineState.GetVoidAddressOf()); ThrowHelper.ThrowIfFailed(result); - return d3D12PipelineState; + return d3D12PipelineState.Move(); } } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs index c2811246..5dab1701 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs @@ -70,6 +70,7 @@ namespace ComputeSharp.Shaders.Translation { using ComPtr dxcBlobEncoding = default; using ComPtr dxcOperationResult = default; + using ComPtr dxcBlobBytecode = default; int result; @@ -112,13 +113,11 @@ namespace ComputeSharp.Shaders.Translation // The compilation was successful, so we can extract the shader bytecode if (status == 0) { - using ComPtr dxcBlob = default; - - result = dxcOperationResult.Get()->GetResult(dxcBlob.GetAddressOf()); + result = dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()); ThrowHelper.ThrowIfFailed(result); - return dxcBlob.Move(); + return dxcBlobBytecode.Move(); } return ThrowHslsCompilationException(dxcOperationResult);