Beispiel #1
0
        public async Task <SoftwareBitmap> GetCaptureAsync()
        {
            var d2d   = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0);
            var frame = Direct3D11Helper.CreateDirect3DSurfaceFromSharpDXTexture(d2d);

            return(await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame));
        }
Beispiel #2
0
            public EncoderPreview(SharpDX.Direct3D11.Device device)
            {
                _d3dDevice = device;

                var dxgiDevice = _d3dDevice.QueryInterface <SharpDX.DXGI.Device>();
                var adapter    = dxgiDevice.GetParent <SharpDX.DXGI.Adapter>();
                var factory    = adapter.GetParent <SharpDX.DXGI.Factory2>();

                var description = new SharpDX.DXGI.SwapChainDescription1
                {
                    Width             = 1,
                    Height            = 1,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                    SampleDescription = new SharpDX.DXGI.SampleDescription()
                    {
                        Count   = 1,
                        Quality = 0
                    },
                    BufferCount = 2,
                    Scaling     = SharpDX.DXGI.Scaling.Stretch,
                    SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                    AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied
                };
                var swapChain = new SharpDX.DXGI.SwapChain1(factory, dxgiDevice, ref description);

                using (var backBuffer = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                    using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
                    {
                        _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0));
                    }

                _swapChain = swapChain;
            }
        private void InitializeDirectXResources()
        {
            var clientSize     = ClientSize;
            var backBufferDesc = new SharpDX.DXGI.ModeDescription(clientSize.Width, clientSize.Height,
                                                                  new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm);

            var swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
            {
                ModeDescription   = backBufferDesc,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = Handle,
                SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                IsWindowed        = false
            };

            SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport,
                                                          new[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, swapChainDesc,
                                                          out _d3DDevice, out var swapChain);
            _d3DDeviceContext = _d3DDevice.ImmediateContext;

            _swapChain = new SharpDX.DXGI.SwapChain1(swapChain.NativePointer);

            _d2DFactory = new SharpDX.Direct2D1.Factory();

            using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
            {
                _renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3DDevice, backBuffer);
                _renderTarget     = new SharpDX.Direct2D1.RenderTarget(_d2DFactory, backBuffer.QueryInterface <SharpDX.DXGI.Surface>(),
                                                                       new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)))
                {
                    TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype
                };
            }

            _solidColorBrush = new SharpDX.Direct2D1.SolidColorBrush(_renderTarget, Color.White);

            _dwFactory  = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
            _textFormat = new SharpDX.DirectWrite.TextFormat(_dwFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold,
                                                             SharpDX.DirectWrite.FontStyle.Normal, SharpDX.DirectWrite.FontStretch.Normal, 84 * (float)GraphicsUtils.Scale)
            {
                TextAlignment      = SharpDX.DirectWrite.TextAlignment.Center,
                ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center
            };
            //                var rectangleGeometry = new D2D1.RoundedRectangleGeometry(_d2DFactory,
            //                    new D2D1.RoundedRectangle() { RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, Width - 128 * 2, Height - 128 * 2) });
        }
Beispiel #4
0
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != lastSize.Width ||
                    frame.ContentSize.Height != lastSize.Height)
                {
                    // The thing we have been capturing has changed size.
                    // We need to resize the swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate the frame pool.
                    newSize  = true;
                    lastSize = frame.ContentSize;
                    swapChain.ResizeBuffers(
                        2,
                        lastSize.Width,
                        lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }


                using (var backBuffer = swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                    using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                    {
                        d3dDevice.ImmediateContext.CopyResource(bitmap, backBuffer);
                    }
            } // Retire the frame.

            swapChain.Present(0, SharpDX.DXGI.PresentFlags.None);

            if (newSize)
            {
                framePool.Recreate(
                    device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    lastSize);
            }
        }
        private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args)
        {
            var newSize = false;

            using (var frame = sender.TryGetNextFrame())
            {
                if (frame.ContentSize.Width != _lastSize.Width ||
                    frame.ContentSize.Height != _lastSize.Height)
                {
                    // 源已改变,故需要变换抓取大小,首先改变swap chain,之后是Texture
                    newSize   = true;
                    _lastSize = frame.ContentSize;
                    _swapChain.ResizeBuffers(
                        2,
                        _lastSize.Width,
                        _lastSize.Height,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        SharpDX.DXGI.SwapChainFlags.None);
                }

                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(frame.Surface))
                    using (var backBuffer = _swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                        using (var renderTargetView = new SharpDX.Direct3D11.RenderTargetView(_d3dDevice, backBuffer))
                        {
                            _d3dDevice.ImmediateContext.ClearRenderTargetView(renderTargetView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 1));
                            _d3dDevice.ImmediateContext.CopyResource(sourceTexture, backBuffer);
                        }
            }

            _swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);

            if (newSize)//帧池重构
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }
Beispiel #6
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice  = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            SafeDispose(ref renderTargetView);
            SafeDispose(ref depthStencilView);
            SafeDispose(ref bitmapTarget);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain
                using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    using (var dxgiAdapter = dxgiDevice2.Adapter)
                        using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                        {
                            swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc));

                            // Ensure that DXGI does not queue more than one frame at a time. This both reduces
                            // latency and ensures that the application will only render after each VSync, minimizing
                            // power consumption.
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            using (var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0))
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, backBuffer));

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = backBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription()
                {
                    Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D
                }));

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.Direct3D11.Viewport((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewports(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain.
            // Whenever we render to this bitmap, it will be directly rendered to the
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))
                bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
        }
Beispiel #7
0
            // This method creates all application resources that depend on
            // the application window size.  It is called at app initialization,
            // and whenever the application window size changes.
            void CreateWindowSizeDependentResources()
            {
                if (m_swapChain != null)
                {
                    // If the swap chain already exists, resize it.
                    m_swapChain.ResizeBuffers(
                        2,
                        0,
                        0,
                        SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                        0
                        );
                }
                else
                {
                    // If the swap chain does not exist, create it.
                    var swapChainDesc = new SharpDX.DXGI.SwapChainDescription1
                    {
                        Stereo  = false,
                        Usage   = SharpDX.DXGI.Usage.RenderTargetOutput,
                        Scaling = SharpDX.DXGI.Scaling.None,
                        Flags   = 0,
                    };

                    // Use automatic sizing.
                    swapChainDesc.Width  = 0;
                    swapChainDesc.Height = 0;

                    // This is the most common swap chain format.
                    swapChainDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm;

                    // Don't use multi-sampling.
                    swapChainDesc.SampleDescription.Count   = 1;
                    swapChainDesc.SampleDescription.Quality = 0;

                    // Use two buffers to enable flip effect.
                    swapChainDesc.BufferCount = 2;

                    // We recommend using this swap effect for all applications.
                    swapChainDesc.SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential;

                    // Once the swap chain description is configured, it must be
                    // created on the same adapter as the existing D3D Device.

                    // First, retrieve the underlying DXGI Device from the D3D Device.
                    using (var dxgiDevice = m_d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    {
                        // Ensure that DXGI does not queue more than one frame at a time. This both reduces
                        // latency and ensures that the application will only render after each VSync, minimizing
                        // power consumption.
                        dxgiDevice.MaximumFrameLatency = 1;

                        // Next, get the parent factory from the DXGI Device.
                        using (var dxgiAdapter = dxgiDevice.Adapter)
                            using (var dxgiFactory = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                                // Finally, create the swap chain.
                                using (var coreWindow = new SharpDX.ComObject(m_window))
                                {
                                    m_swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory
                                                                              , m_d3dDevice, coreWindow, ref swapChainDesc);
                                }
                    }
                }

                // Once the swap chain is created, create a render target view.  This will
                // allow Direct3D to render graphics to the window.
                using (var backBuffer = m_swapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))
                {
                    m_renderTargetView = new SharpDX.Direct3D11.RenderTargetView(m_d3dDevice, backBuffer);

                    // After the render target view is created, specify that the viewport,
                    // which describes what portion of the window to draw to, should cover
                    // the entire window.

                    var backBufferDesc = backBuffer.Description;

                    var viewport = new SharpDX.ViewportF
                    {
                        X        = 0.0f,
                        Y        = 0.0f,
                        Width    = backBufferDesc.Width,
                        Height   = backBufferDesc.Height,
                        MinDepth = 0,
                        MaxDepth = 1,
                    };

                    m_d3dDeviceContext.Rasterizer.SetViewport(viewport);
                }
            }