From c8138c465c20a26a12b228e591131a10c8e2ae9f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Nov 2020 17:47:51 +0100 Subject: [PATCH] Added shader D3D12 extensions --- .../Extensions/ID3D12DeviceExtensions.cs | 2 +- .../Extensions/ID3D12DeviceExtensions.cs | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index a16d0a56..7a7549b4 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -16,7 +16,7 @@ namespace ComputeSharp.Graphics.Extensions /// /// A with extensions for the type. /// - internal static unsafe class D3D12Helper + internal static unsafe class ID3D12DeviceExtensions { /// /// Creates a new of the specified type, for a given device. diff --git a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs new file mode 100644 index 00000000..720c82e9 --- /dev/null +++ b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs @@ -0,0 +1,117 @@ +using System; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_PIPELINE_STATE_FLAGS; +using static TerraFX.Interop.D3D12_ROOT_PARAMETER_TYPE; +using static TerraFX.Interop.D3D12_SHADER_VISIBILITY; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Shaders.Extensions +{ + /// + /// A with extensions for the type. + /// + internal static unsafe class ID3D12DeviceExtensions + { + /// + /// Creates a new for a given device. + /// + /// The target to use to create the root signature. + /// 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( + this ref ID3D12Device d3d12device, + ReadOnlySpan d3D12DescriptorRanges1) + { + ID3DBlob* d3D3Blob, d3D3BlobError; + int result; + + fixed (D3D12_DESCRIPTOR_RANGE1* d3D12DescriptorRange1 = d3D12DescriptorRanges1) + { + D3D12_ROOT_PARAMETER1* d3D12RootParameters1 = stackalloc D3D12_ROOT_PARAMETER1[d3D12DescriptorRanges1.Length]; + + // Pack each descriptor range into a root parameter with that single range as content + for (int i = 0; i < d3D12DescriptorRanges1.Length; i++) + { + D3D12_ROOT_PARAMETER1* d3D12RootParameter1 = d3D12RootParameters1 + i; + d3D12RootParameter1->ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; + d3D12RootParameter1->ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; + + D3D12_ROOT_DESCRIPTOR_TABLE1.Init(out d3D12RootParameter1->DescriptorTable, 1, d3D12DescriptorRange1 + i); + } + + // Root signature description wrapping the packed collection of root parameters + D3D12_VERSIONED_ROOT_SIGNATURE_DESC.Init_1_1( + out D3D12_VERSIONED_ROOT_SIGNATURE_DESC d3d12VersionedRootSignatureDescription, + (uint)d3D12DescriptorRanges1.Length, + d3D12RootParameters1); + + // Serialize the root signature from the data just computed. When this is done + // we just work with the resulting blobs, so the input data can be unpinned. + result = FX.D3D12SerializeVersionedRootSignature( + &d3d12VersionedRootSignatureDescription, + &d3D3Blob, + &d3D3BlobError); + + if (FX.FAILED(result)) + { + if (d3D3BlobError != null) d3D3BlobError->Release(); + + Marshal.ThrowExceptionForHR(result); + } + } + + Guid d3D12RootSignatureGuid = FX.IID_ID3D12RootSignature; + ID3D12RootSignature* d3D12RootSignature; + + result = d3d12device.CreateRootSignature( + 0, + d3D3Blob->GetBufferPointer(), + d3D3Blob->GetBufferSize(), + &d3D12RootSignatureGuid, + (void**)&d3D12RootSignature); + + if (FX.FAILED(result)) + { + if (d3D3Blob != null) d3D3Blob->Release(); + + Marshal.ThrowExceptionForHR(result); + } + + return d3D12RootSignature; + } + + /// + /// Creates a new for a given device. + /// + /// The target to use to create the pipeline state. + /// The input root signature to use to create the pipeline state. + /// 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( + this ref ID3D12Device d3d12device, + ID3D12RootSignature* d3D12RootSignature, + D3D12_SHADER_BYTECODE d3d12ShaderBytecode) + { + D3D12_COMPUTE_PIPELINE_STATE_DESC d3d12ComputePipelineStateDescription; + d3d12ComputePipelineStateDescription.pRootSignature = d3D12RootSignature; + d3d12ComputePipelineStateDescription.CS = d3d12ShaderBytecode; + d3d12ComputePipelineStateDescription.NodeMask = 0; + 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); + + if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + + return d3D12PipelineState; + } + } +}