public static ID2D1Factory CreateFactory()
    {
        D2D1_FACTORY_OPTIONS opts = new D2D1_FACTORY_OPTIONS();

        object factory = D2D1CreateFactory(
            D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED,
            new Guid(IID_ID2D1Factory), opts);

        return((ID2D1Factory)factory);
    }
示例#2
0
    /// <summary>
    /// Creates an <see cref="ID2D1Factory2"/> instance.
    /// </summary>
    /// <returns>A new <see cref="ID2D1Factory2"/> instance.</returns>
    public static unsafe ComPtr <ID2D1Factory2> CreateD2D1Factory2()
    {
        using ComPtr <ID2D1Factory2> d2D1Factory2 = default;

        D2D1_FACTORY_OPTIONS d2D1FactoryOptions = default;

        // Create a Direct2D factory
        DirectX.D2D1CreateFactory(
            factoryType: D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED,
            riid: Windows.__uuidof <ID2D1Factory2>(),
            pFactoryOptions: &d2D1FactoryOptions,
            ppIFactory: (void **)d2D1Factory2.GetAddressOf()).Assert();

        return(d2D1Factory2.Move());
    }
 private static extern object D2D1CreateFactory(D2D1_FACTORY_TYPE factoryType,
                                                [MarshalAs(UnmanagedType.LPStruct)] Guid riid,
                                                D2D1_FACTORY_OPTIONS pFactoryOptions);
示例#4
0
        void EnsureDevice(IntPtr hWnd)
        {
            if (m_device)
            {
                return;
            }

            // D3D
            Span <D3D_FEATURE_LEVEL> levels = stackalloc D3D_FEATURE_LEVEL[]
            {
                D3D_FEATURE_LEVEL._11_1,
                D3D_FEATURE_LEVEL._11_0,
                D3D_FEATURE_LEVEL._10_1,
                D3D_FEATURE_LEVEL._10_0,
                D3D_FEATURE_LEVEL._9_3,
                D3D_FEATURE_LEVEL._9_2,
                D3D_FEATURE_LEVEL._9_1
            };
            var flags =
                D3D11_CREATE_DEVICE_FLAG._DEBUG |
                D3D11_CREATE_DEVICE_FLAG._BGRA_SUPPORT;
            var level = default(D3D_FEATURE_LEVEL);

            d3d11.D3D11CreateDevice(
                null,
                D3D_DRIVER_TYPE._HARDWARE,
                IntPtr.Zero,
                (uint)flags,
                ref MemoryMarshal.GetReference(levels),
                (uint)levels.Length,
                Constants.D3D11_SDK_VERSION,
                out m_device,
                out level,
                out m_context).ThrowIfFailed();

            // D2D
            using (var dxgiDevice = new IDXGIDevice())
            {
                m_device.QueryInterface(ref IDXGIDevice.IID, out dxgiDevice.PtrForNew).ThrowIfFailed();

                using (var d2dFactory = new ID2D1Factory1())
                {
                    var factory_opt = new D2D1_FACTORY_OPTIONS
                    {
                    };
                    d2d1.D2D1CreateFactory(D2D1_FACTORY_TYPE._SINGLE_THREADED,
                                           ref ID2D1Factory1.IID, ref factory_opt, out d2dFactory.PtrForNew).ThrowIfFailed();

                    d2dFactory.GetDesktopDpi(out float x, out float y);

                    // using (var d2dDevice = new ())
                    {
                        var prop = new D2D1_CREATION_PROPERTIES
                        {
                        };
                        d2dFactory.CreateDevice(dxgiDevice, out ID2D1Device d2dDevice).ThrowIfFailed();
                        using (d2dDevice)
                            d2dDevice.CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS._NONE, out m_d2dContext).ThrowIfFailed();
                    }
                }

                // SWAPChain
                // using (var adapter = new ())
                {
                    dxgiDevice.GetAdapter(out IDXGIAdapter adapter).ThrowIfFailed();
                    using (adapter)
                        using (var dxgiFactory = new IDXGIFactory2())
                        {
                            adapter.GetParent(ref IDXGIFactory2.IID, out dxgiFactory.PtrForNew).ThrowIfFailed();

                            var swapChainDesc = new DXGI_SWAP_CHAIN_DESC1
                            {
                                Width  = 0,
                                Height = 0,
                                Format = DXGI_FORMAT._B8G8R8A8_UNORM,
                                Stereo = 0
                            };
                            swapChainDesc.SampleDesc.Count   = 1;
                            swapChainDesc.SampleDesc.Quality = 0;
                            swapChainDesc.BufferUsage        = DXGI_USAGE._RENDER_TARGET_OUTPUT;
                            swapChainDesc.BufferCount        = 2;
                            //swapChainDesc.Scaling = DXGI_SCALING_NONE;
                            swapChainDesc.Scaling = DXGI_SCALING._STRETCH;
                            //swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
                            swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT._DISCARD;
                            swapChainDesc.AlphaMode  = DXGI_ALPHA_MODE._UNSPECIFIED;

                            var fs = new DXGI_SWAP_CHAIN_FULLSCREEN_DESC
                            {
                                Windowed = 1,
                            };
                            dxgiFactory.CreateSwapChainForHwnd(
                                dxgiDevice,
                                hWnd,
                                ref swapChainDesc,
                                ref fs,
                                null,
                                out m_swapchain).ThrowIfFailed();

                            Console.Write("CreateSwapchain");
                        }
                }
            }

            // Dwrite
            dwrite.DWriteCreateFactory(DWRITE_FACTORY_TYPE._SHARED, ref IDWriteFactory.IID, out m_dwriteFactory.PtrForNew).ThrowIfFailed();
        }