Refactored extensions to return ComPtr<T>

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-16 19:41:22 +01:00
1 親 bb962fee80
コミット 3818fef1d0
3ファイル変更
+49 -43

検索条件に一致するファイルはありません

+30 -24
ファイルの表示
@@ -24,24 +24,25 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="type">The type of command queue to create.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12CommandQueue"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12CommandQueue* CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type)
public static ComPtr<ID3D12CommandQueue> CreateCommandQueue(this ref ID3D12Device d3d12device, D3D12_COMMAND_LIST_TYPE type)
{
using ComPtr<ID3D12CommandQueue> 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();
}
/// <summary>
@@ -50,20 +51,21 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="d3d12device">The target <see cref="ID3D12Device"/> to use to create the fence.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12Fence"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12Fence* CreateFence(this ref ID3D12Device d3d12device)
public static ComPtr<ID3D12Fence> CreateFence(this ref ID3D12Device d3d12device)
{
using ComPtr<ID3D12Fence> 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();
}
/// <summary>
@@ -73,24 +75,25 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="descriptorsCount">The number of descriptors to allocate.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12DescriptorHeap"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12DescriptorHeap* CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount)
public static ComPtr<ID3D12DescriptorHeap> CreateDescriptorHeap(this ref ID3D12Device d3d12device, uint descriptorsCount)
{
using ComPtr<ID3D12DescriptorHeap> 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();
}
/// <summary>
@@ -100,13 +103,15 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="descriptorsCount">The number of descriptors to allocate.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12DescriptorHeap"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command queue fails.</exception>
public static ID3D12Resource* CreateCommittedResource(
public static ComPtr<ID3D12Resource> CreateCommittedResource(
this ref ID3D12Device d3d12device,
D3D12_HEAP_TYPE d3D12HeapType,
ulong width,
D3D12_RESOURCE_FLAGS d3D12ResourceFlags,
D3D12_RESOURCE_STATES d3D12ResourceStates)
{
using ComPtr<ID3D12Resource> 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();
}
/// <summary>
@@ -138,21 +142,22 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="d3d12CommandListType">The type of command list allocator to create.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12CommandAllocator"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command allocator fails.</exception>
public static ID3D12CommandAllocator* CreateCommandAllocator(
public static ComPtr<ID3D12CommandAllocator> CreateCommandAllocator(
this ref ID3D12Device d3d12device,
D3D12_COMMAND_LIST_TYPE d3d12CommandListType)
{
ID3D12CommandAllocator* d3D12CommandAllocator;
using ComPtr<ID3D12CommandAllocator> 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();
}
/// <summary>
@@ -162,12 +167,13 @@ namespace ComputeSharp.Graphics.Extensions
/// <param name="d3d12CommandListType">The type of command list to create.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12GraphicsCommandList"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the command list fails.</exception>
public static ID3D12GraphicsCommandList* CreateCommandList(
public static ComPtr<ID3D12GraphicsCommandList> CreateCommandList(
this ref ID3D12Device d3d12device,
D3D12_COMMAND_LIST_TYPE d3d12CommandListType,
ID3D12CommandAllocator* d3D12CommandAllocator)
{
ID3D12GraphicsCommandList* d3d12GraphicsCommandList;
using ComPtr<ID3D12GraphicsCommandList> 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();
}
/// <summary>
+16 -15
ファイルの表示
@@ -20,11 +20,14 @@ namespace ComputeSharp.Shaders.Extensions
/// <param name="d3D12DescriptorRanges1">The input descriptor ranges for the signature to create.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12RootSignature"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the root signature fails.</exception>
public static ID3D12RootSignature* CreateRootSignature(
public static ComPtr<ID3D12RootSignature> CreateRootSignature(
this ref ID3D12Device d3d12device,
ReadOnlySpan<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1)
{
ID3DBlob* d3D3Blob, d3D3BlobError;
using ComPtr<ID3DBlob> d3D3Blob = default;
using ComPtr<ID3DBlob> d3D3BlobError = default;
using ComPtr<ID3D12RootSignature> 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();
}
/// <summary>
@@ -82,11 +82,13 @@ namespace ComputeSharp.Shaders.Extensions
/// <param name="d3d12ShaderBytecode">The shader bytecode to use for the pipeline state.</param>
/// <returns>A pointer to the newly allocated <see cref="ID3D12PipelineState"/> instance.</returns>
/// <exception cref="Exception">Thrown when the creation of the pipeline state fails.</exception>
public static ID3D12PipelineState* CreateComputePipelineState(
public static ComPtr<ID3D12PipelineState> CreateComputePipelineState(
this ref ID3D12Device d3d12device,
ID3D12RootSignature* d3D12RootSignature,
D3D12_SHADER_BYTECODE d3d12ShaderBytecode)
{
using ComPtr<ID3D12PipelineState> 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();
}
}
}
+3 -4
ファイルの表示
@@ -70,6 +70,7 @@ namespace ComputeSharp.Shaders.Translation
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncoding = default;
using ComPtr<IDxcOperationResult> dxcOperationResult = default;
using ComPtr<IDxcBlob> 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<IDxcBlob> 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);