private void CreateCommandListAndAllocator(ID3D12Device *d3D12Device, ID3D12PipelineState *d3D12PipelineState, out ID3D12GraphicsCommandList *d3D12CommandList, out ID3D12CommandAllocator *d3D12CommandAllocator)
    {
        using ComPtr <ID3D12CommandAllocator> d3D12CommandAllocatorComPtr = d3D12Device->CreateCommandAllocator(this.d3D12CommandListType);
        using ComPtr <ID3D12GraphicsCommandList> d3D12CommandListComPtr   = d3D12Device->CreateCommandList(this.d3D12CommandListType, d3D12CommandAllocatorComPtr.Get(), d3D12PipelineState);

        fixed(ID3D12GraphicsCommandList **d3D12CommandListPtr = &d3D12CommandList)
        fixed(ID3D12CommandAllocator **d3D12CommandAllocatorPtr = &d3D12CommandAllocator)
        {
            d3D12CommandListComPtr.CopyTo(d3D12CommandListPtr);
            d3D12CommandAllocatorComPtr.CopyTo(d3D12CommandAllocatorPtr);
        }
    }
Beispiel #2
0
    /// <summary>
    /// Creates an <see cref="ID2D1Device"/> instance.
    /// </summary>
    /// <param name="d2D1Factory2">The input <see cref="ID2D1Factory2"/> instance to use to create the device.</param>
    /// <returns>A new <see cref="ID2D1Device"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Device> CreateD2D1Device(ID2D1Factory2 *d2D1Factory2)
    {
        using ComPtr <ID3D11Device> d3D11Device = default;

        uint creationFlags = (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT;
        D3D_FEATURE_LEVEL *featureLevels = stackalloc[]
        {
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_1,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_1,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_0,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_3,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_2,
            D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_1
        };
        D3D_FEATURE_LEVEL d3DFeatureLevel;

        // Create the Direct3D 11 API device and context
        DirectX.D3D11CreateDevice(
            pAdapter: null,
            DriverType: D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
            Software: HMODULE.NULL,
            Flags: creationFlags,
            pFeatureLevels: featureLevels,
            FeatureLevels: 7,
            SDKVersion: D3D11.D3D11_SDK_VERSION,
            ppDevice: d3D11Device.GetAddressOf(),
            pFeatureLevel: &d3DFeatureLevel,
            ppImmediateContext: null).Assert();

        using ComPtr <IDXGIDevice3> dxgiDevice3 = default;

        // Get a DXGI device from the D3D11 device
        d3D11Device.CopyTo(dxgiDevice3.GetAddressOf()).Assert();

        using ComPtr <ID2D1Device> d2D1Device = default;

        // Create a D2D1 device
        d2D1Factory2->CreateDevice(
            dxgiDevice: (IDXGIDevice *)dxgiDevice3.Get(),
            d2dDevice: d2D1Device.GetAddressOf());

        return(d2D1Device.Move());
    }
Beispiel #3
0
        /// <summary>
        /// Tries to check or create a warp <see cref="ID3D12Device"/> object.
        /// </summary>
        /// <param name="d3D12Device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
        /// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
        /// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
        /// <returns>Whether a warp device was created successfully.</returns>
        private static unsafe bool TryGetWarpDevice(ID3D12Device **d3D12Device, IDXGIAdapter **dxgiAdapter, DXGI_ADAPTER_DESC1 *dxgiDescription1)
        {
            using ComPtr <IDXGIFactory4> dxgiFactory4 = default;

            EnableDebugMode();

            FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof <IDXGIFactory4>(), dxgiFactory4.GetVoidAddressOf()).Assert();

            using ComPtr <IDXGIAdapter1> dxgiAdapter1 = default;

            dxgiFactory4.Get()->EnumWarpAdapter(FX.__uuidof <IDXGIAdapter1>(), dxgiAdapter1.GetVoidAddressOf()).Assert();

            dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert();

            HRESULT createDeviceResult = FX.D3D12CreateDevice(
                dxgiAdapter1.AsIUnknown().Get(),
                D3D_FEATURE_LEVEL_11_0,
                FX.__uuidof <ID3D12Device>(),
                (void **)d3D12Device);

            dxgiAdapter1.CopyTo(dxgiAdapter);

            return(FX.SUCCEEDED(createDeviceResult));
        }
Beispiel #4
0
        /// <summary>
        /// Tries to check or create a default <see cref="ID3D12Device"/> object.
        /// </summary>
        /// <param name="d3D12Device">A pointer to the <see cref="ID3D12Device"/> object to create, or <see langword="null"/>.</param>
        /// <param name="dxgiAdapter">A pointer to the <see cref="IDXGIAdapter"/> object used to create <paramref name="d3D12Device"/>, or <see langword="null"/>.</param>
        /// <param name="dxgiDescription1">A pointer to the <see cref="DXGI_ADAPTER_DESC1"/> value for the device found.</param>
        /// <returns>Whether a default device was found with the requested feature level.</returns>
        private static unsafe bool TryGetDefaultDevice(ID3D12Device **d3D12Device, IDXGIAdapter **dxgiAdapter, DXGI_ADAPTER_DESC1 *dxgiDescription1)
        {
            using ComPtr <IDXGIFactory4> dxgiFactory4 = default;

            EnableDebugMode();

            FX.CreateDXGIFactory2(IDXGIFactoryCreationFlags, FX.__uuidof <IDXGIFactory4>(), dxgiFactory4.GetVoidAddressOf()).Assert();

            uint i = 0;

            while (true)
            {
                using ComPtr <IDXGIAdapter1> dxgiAdapter1 = default;

                HRESULT enumAdapters1Result = dxgiFactory4.Get()->EnumAdapters1(i++, dxgiAdapter1.GetAddressOf());

                if (enumAdapters1Result == FX.DXGI_ERROR_NOT_FOUND)
                {
                    return(false);
                }

                enumAdapters1Result.Assert();

                dxgiAdapter1.Get()->GetDesc1(dxgiDescription1).Assert();

                if (dxgiDescription1->VendorId == MicrosoftVendorId &&
                    dxgiDescription1->DeviceId == WarpDeviceId)
                {
                    continue;
                }

                // Explicit paths for when a device is being retrieved or not, with special handling
                // for the additional check that is required for the SM6 level. This can't be checked
                // without creating a device first, so the path for when the target device pointer is
                // null is useful to do an initial filtering using D3D12CreateDevice to avoid creating
                // a device for adapters that would've failed at the FL11 check already.
                if (d3D12Device == null)
                {
                    HRESULT createDeviceResult = FX.D3D12CreateDevice(
                        dxgiAdapter1.AsIUnknown().Get(),
                        D3D_FEATURE_LEVEL_11_0,
                        FX.__uuidof <ID3D12Device>(),
                        null);

                    if (FX.SUCCEEDED(createDeviceResult))
                    {
                        using ComPtr <ID3D12Device> d3D12DeviceCandidate = default;

                        createDeviceResult = FX.D3D12CreateDevice(
                            dxgiAdapter1.AsIUnknown().Get(),
                            D3D_FEATURE_LEVEL_11_0,
                            FX.__uuidof <ID3D12Device>(),
                            d3D12DeviceCandidate.GetVoidAddressOf());

                        if (FX.SUCCEEDED(createDeviceResult) &&
                            d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    using ComPtr <ID3D12Device> d3D12DeviceCandidate = default;

                    HRESULT createDeviceResult = FX.D3D12CreateDevice(
                        dxgiAdapter1.AsIUnknown().Get(),
                        D3D_FEATURE_LEVEL_11_0,
                        FX.__uuidof <ID3D12Device>(),
                        d3D12DeviceCandidate.GetVoidAddressOf());

                    if (FX.SUCCEEDED(createDeviceResult) &&
                        d3D12DeviceCandidate.Get()->IsShaderModelSupported(D3D_SHADER_MODEL_6_0))
                    {
                        d3D12DeviceCandidate.CopyTo(d3D12Device);
                        dxgiAdapter1.CopyTo(dxgiAdapter);

                        return(true);
                    }
                }
            }
        }