示例#1
0
            public void PresentSurface(IDirect3DSurface surface)
            {
                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(surface))
                {
                    if (!_isSwapChainSized)
                    {
                        var description = sourceTexture.Description;

                        _swapChain.ResizeBuffers(
                            2,
                            description.Width,
                            description.Height,
                            SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                            SharpDX.DXGI.SwapChainFlags.None);

                        _isSwapChainSized = true;
                    }

                    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);
            }
        public SurfaceWithInfo WaitForNewFrame()
        {
            // Let's get a fresh one.
            _currentFrame?.Dispose();
            _frameEvent.Reset();

            var signaledEvent = _events[WaitHandle.WaitAny(_events)];

            if (signaledEvent == _closedEvent)
            {
                Cleanup();
                return(null);
            }

            var result = new SurfaceWithInfo();

            //System.Diagnostics.Debug.WriteLine("Using _currentFrame");
            result.SystemRelativeTime = _currentFrame.SystemRelativeTime;
            //System.Diagnostics.Debug.WriteLine("Done using _currentFrame");
            //result.Surface = _currentFrame.Surface; return result;
            using (var multithreadLock = new MultithreadLock(_multithread))
                using (var sourceTexture = Direct3D11Helpers.CreateSharpDXTexture2D(_currentFrame.Surface))
                {
                    var description = sourceTexture.Description;
                    description.Usage          = SharpDX.Direct3D11.ResourceUsage.Default;
                    description.BindFlags      = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget;
                    description.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None;
                    description.OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None;

                    using (var copyTexture = new SharpDX.Direct3D11.Texture2D(_d3dDevice, description))
                    {
                        var width  = Math.Clamp(_currentFrame.ContentSize.Width, 0, _currentFrame.Surface.Description.Width);
                        var height = Math.Clamp(_currentFrame.ContentSize.Height, 0, _currentFrame.Surface.Description.Height);

                        var region = new SharpDX.Direct3D11.ResourceRegion(0, 0, 0, width, height, 1);

                        _d3dDevice.ImmediateContext.CopyResource(_blankTexture, copyTexture);
                        _d3dDevice.ImmediateContext.CopySubresourceRegion(sourceTexture, 0, region, copyTexture, 0);
                        result.Surface = Direct3D11Helpers.CreateDirect3DSurfaceFromSharpDXTexture(copyTexture);
                    }
                }

            return(result);
        }
示例#3
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 our swap chain first, then blit the pixels.
                    // After we do that, retire the frame and then recreate our 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 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);
                        }
            } // retire the frame

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

            if (newSize)
            {
                _framePool.Recreate(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    _lastSize);
            }
        }