/// <summary> /// 帧到达事件 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnFrameArrived(Direct3D11CaptureFramePool sender, object args) { var newSize = false; using Direct3D11CaptureFrame frame = sender.TryGetNextFrame(); if (frame == null) { return; } if (frame.ContentSize.Width != lastSize.Width || frame.ContentSize.Height != lastSize.Height) { // 我们捕捉到的东西变大了。 // 我们需要先调整交换链的大小,然后blit像素。 // 完成此操作后,请注销帧,然后重新创建帧池。 newSize = true; lastSize = frame.ContentSize; swapChain.ResizeBuffers( 2, lastSize.Width, lastSize.Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None); } using Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0); using Texture2D tex = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface); d3dDevice.ImmediateContext.CopyResource(tex, backBuffer); // 保存当前帧到位图 if (GetOneFrameFromBitmapEvent != null) { TryGetOneFrameToBitmap(tex); } // GetOneFrameToBitmap(tex); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); if (newSize) { framePool.Recreate(device, Windows.Graphics.DirectX.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) { // 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); } }
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; }
// 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); } }