public static void InitAsUnorderedAccessView([NativeTypeName("D3D12_ROOT_PARAMETER &")] out D3D12_ROOT_PARAMETER rootParam, uint shaderRegister, uint registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam = default; rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; rootParam.ShaderVisibility = visibility; D3D12_ROOT_DESCRIPTOR.Init(out rootParam.Anonymous.Descriptor, shaderRegister, registerSpace); }
public static void InitAsConstants([NativeTypeName("D3D12_ROOT_PARAMETER &")] out D3D12_ROOT_PARAMETER rootParam, uint num32BitValues, uint shaderRegister, uint registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam = default; rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; rootParam.ShaderVisibility = visibility; D3D12_ROOT_CONSTANTS.Init(out rootParam.Anonymous.Constants, num32BitValues, shaderRegister, registerSpace); }
public static void InitAsDescriptorTable([NativeTypeName("D3D12_ROOT_PARAMETER &")] out D3D12_ROOT_PARAMETER rootParam, uint numDescriptorRanges, [NativeTypeName("const D3D12_DESCRIPTOR_RANGE *")] D3D12_DESCRIPTOR_RANGE *pDescriptorRanges, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam = default; rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; rootParam.ShaderVisibility = visibility; D3D12_ROOT_DESCRIPTOR_TABLE.Init(out rootParam.Anonymous.DescriptorTable, numDescriptorRanges, pDescriptorRanges); }
private static void TranslateRootParameters(ReadOnlyMemory <RootParameter> rootParameters, D3D12_ROOT_PARAMETER[] outRootParams) { var span = rootParameters.Span; for (var i = 0; i < span.Length; i++) { var inRootParam = span[i]; D3D12_ROOT_PARAMETER outRootParam = new D3D12_ROOT_PARAMETER { ParameterType = (D3D12_ROOT_PARAMETER_TYPE)inRootParam.Type, ShaderVisibility = (D3D12_SHADER_VISIBILITY)inRootParam.Visibility }; switch (inRootParam.Type) { case RootParameterType.DescriptorTable: outRootParam.Anonymous.DescriptorTable = new D3D12_ROOT_DESCRIPTOR_TABLE { NumDescriptorRanges = (uint)inRootParam.DescriptorTable !.Length, // IMPORTANT: we *know* this is pinned, because it can only come from RootParameter.CreateDescriptorTable, which strictly makes sure it is pinned pDescriptorRanges = (D3D12_DESCRIPTOR_RANGE *)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(inRootParam.DescriptorTable)) }; break; case RootParameterType.DwordConstants: outRootParam.Anonymous.Constants = inRootParam.Constants; break; case RootParameterType.ConstantBufferView: case RootParameterType.ShaderResourceView: case RootParameterType.UnorderedAccessView: outRootParam.Anonymous.Descriptor = inRootParam.Descriptor; break; } outRootParams[i] = outRootParam; } }
private Pointer <ID3D12RootSignature> CreateD3D12RootSignature() { _state.ThrowIfDisposedOrDisposing(); ID3DBlob *rootSignatureBlob = null; ID3DBlob *rootSignatureErrorBlob = null; try { ID3D12RootSignature *d3d12RootSignature; var rootParameters = Array.Empty <D3D12_ROOT_PARAMETER>(); var staticSamplers = Array.Empty <D3D12_STATIC_SAMPLER_DESC>(); var rootSignatureDesc = new D3D12_ROOT_SIGNATURE_DESC { Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT, }; var resources = Resources; var resourcesLength = resources.Length; var rootParametersLength = 0; var staticSamplersLength = 0; var rootParametersIndex = 0; var constantShaderRegister = 0; var textureShaderRegister = 0; var staticSamplersIndex = 0; if (resourcesLength != 0) { for (var inputIndex = 0; inputIndex < resourcesLength; inputIndex++) { rootParametersLength++; if (resources[inputIndex].Kind == GraphicsPipelineResourceKind.Texture) { staticSamplersLength++; } } rootParameters = new D3D12_ROOT_PARAMETER[rootParametersLength]; staticSamplers = new D3D12_STATIC_SAMPLER_DESC[staticSamplersLength]; for (var inputIndex = 0; inputIndex < resourcesLength; inputIndex++) { var input = resources[inputIndex]; switch (input.Kind) { case GraphicsPipelineResourceKind.ConstantBuffer: { var shaderVisibility = GetD3D12ShaderVisiblity(input.ShaderVisibility); rootParameters[rootParametersIndex].InitAsConstantBufferView(unchecked ((uint)constantShaderRegister), registerSpace: 0, shaderVisibility); constantShaderRegister++; rootParametersIndex++; break; } case GraphicsPipelineResourceKind.Texture: { var descriptorRange = new D3D12_DESCRIPTOR_RANGE(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, numDescriptors: 1, baseShaderRegister: unchecked ((uint)textureShaderRegister)); var shaderVisibility = GetD3D12ShaderVisiblity(input.ShaderVisibility); rootParameters[rootParametersIndex].InitAsDescriptorTable(1, &descriptorRange, shaderVisibility); staticSamplers[staticSamplersIndex] = new D3D12_STATIC_SAMPLER_DESC( shaderRegister: unchecked ((uint)staticSamplersIndex), shaderVisibility: shaderVisibility ); textureShaderRegister++; rootParametersIndex++; staticSamplersIndex++; break; } default: { break; } } } } fixed(D3D12_ROOT_PARAMETER *pRootParameters = rootParameters) fixed(D3D12_STATIC_SAMPLER_DESC * pStaticSamplers = staticSamplers) { rootSignatureDesc.NumParameters = unchecked ((uint)rootParametersLength); rootSignatureDesc.pParameters = pRootParameters; rootSignatureDesc.NumStaticSamplers = unchecked ((uint)staticSamplersLength); rootSignatureDesc.pStaticSamplers = pStaticSamplers; ThrowExternalExceptionIfFailed(nameof(D3D12SerializeRootSignature), D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &rootSignatureBlob, &rootSignatureErrorBlob)); } var iid = IID_ID3D12RootSignature; ThrowExternalExceptionIfFailed(nameof(ID3D12Device.CreateRootSignature), Device.D3D12Device->CreateRootSignature(0, rootSignatureBlob->GetBufferPointer(), rootSignatureBlob->GetBufferSize(), &iid, (void **)&d3d12RootSignature)); return(d3d12RootSignature); } finally { ReleaseIfNotNull(rootSignatureErrorBlob); ReleaseIfNotNull(rootSignatureBlob); }