internal static void DisposeSwapChain(ref SwapChainComponent com)
 {
     com.backbuffer.Dispose();
     com.depthbuffer.Dispose();
     com.TargetView.Dispose();
     com.StencilView.Dispose();
     com.Bitmap.Dispose();
     com.swapChain.Dispose();
 }
예제 #2
0
        public SwapChain(double width, double height)
        {
            Width     = width;
            Height    = height;
            com       = new SwapChainComponent();
            com.panel = this;
            com.dpi   = DisplayInformation.GetForCurrentView().LogicalDpi;
            DX_Core.CreateD3DInstance(ref com);
            DX_Child                  = new List <UIElement>();
            this.PointerPressed      += PointerOperation;
            this.PointerMoved        += PointerOperation;
            this.PointerReleased     += PointerOperation;
            this.PointerEntered      += PointerOperation;
            this.PointerExited       += PointerOperation;
            this.PointerWheelChanged += PointerOperation;
            var bp = new D2D1.BitmapProperties1(
                new D2D1.PixelFormat(Dxgi.Format.B8G8R8A8_UNorm, D2D1.AlphaMode.Premultiplied),
                com.dpi, com.dpi, D2D1.BitmapOptions.Target);

            lock (DX_Core.D2D)
                BitmapBuffer = new D2D1.Bitmap1(DX_Core.D2D.d2dContext, new Size2((int)width, (int)height), bp);
            ThreadManage.StartLoop(this);
        }
        /// <summary>
        /// main thread
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="dpi"></param>
        internal static void CreateD3DInstance(ref SwapChainComponent com)
        {
            int width  = (int)com.panel.Width;
            int height = (int)com.panel.Height;

            dpi      = (int)com.dpi;
            com.port = new Viewport(0, 0, width, height, 0, 1);
            D2D.d3dContext.Rasterizer.SetViewport(com.port);

            var multisampleDesc = new Dxgi.SampleDescription(1, 0);
            var desc            = new Dxgi.SwapChainDescription1()
            {
                // Automatic sizing
                AlphaMode         = Dxgi.AlphaMode.Premultiplied,
                Width             = width,
                Height            = height,
                Format            = Dxgi.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = multisampleDesc,
                Usage             = Dxgi.Usage.RenderTargetOutput,
                BufferCount       = 2,
                SwapEffect        = Dxgi.SwapEffect.FlipSequential,
                Scaling           = Dxgi.Scaling.Stretch
            };

            using (var dxgiDevice2 = D2D.d3dDevice.QueryInterface <Dxgi.Device2>())
                using (var dxgiAdapter = dxgiDevice2.Adapter)
                    using (var dxgiFactory2 = dxgiAdapter.GetParent <Dxgi.Factory2>())
                        using (com.NativePanel = ComObject.As <Dxgi.ISwapChainPanelNative>(com.panel))
                        {
                            com.swapChain                   = new Dxgi.SwapChain1(dxgiFactory2, dxgiDevice2, ref desc, null);
                            com.NativePanel.SwapChain       = com.swapChain;
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            using (com.backbuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(com.swapChain, 0))
                com.TargetView = new D3D11.RenderTargetView(D2D.d3dDevice, com.backbuffer);

            using (com.depthbuffer = new D3D11.Texture2D(D2D.d3dDevice, new D3D11.Texture2DDescription()
            {
                Format = Dxgi.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = width,
                Height = height,
                SampleDescription = multisampleDesc,
                Usage = D3D11.ResourceUsage.Default,
                BindFlags = D3D11.BindFlags.DepthStencil,
            }))
                com.StencilView = new D3D11.DepthStencilView(D2D.d3dDevice, com.depthbuffer);
            D2D.d3dContext.OutputMerger.SetTargets(com.StencilView, com.TargetView);
            var bitmapProperties = new D2D1.BitmapProperties1(
                new D2D1.PixelFormat(Dxgi.Format.B8G8R8A8_UNorm, D2D1.AlphaMode.Premultiplied),
                com.dpi, com.dpi,
                D2D1.BitmapOptions.Target | D2D1.BitmapOptions.CannotDraw);

            using (var dxgiBackBuffer = com.swapChain.GetBackBuffer <Dxgi.Surface>(0))
            {
                com.Bitmap = new D2D1.Bitmap1(D2D.d2dContext, dxgiBackBuffer, bitmapProperties);
            }
            D2D.d2dContext.Target            = com.Bitmap;
            D2D.d2dContext.TextAntialiasMode = D2D1.TextAntialiasMode.Cleartype;
        }