Пример #1
0
        /// <summary>
        /// Present the results to the swap chain.
        /// </summary>
        public virtual void Present()
        {
            // The application may optionally specify "dirty" or "scroll" rects to improve efficiency
            // in certain scenarios.  In this sample, however, we do not utilize those features.
            var parameters = new SharpDX.DXGI.PresentParameters();

            try
            {
                // The first argument instructs DXGI to block until VSync, putting the application
                // to sleep until the next VSync. This ensures we don't waste any cycles rendering
                // frames that will never be displayed to the screen.
                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None, parameters);
            }
            catch (SharpDX.SharpDXException ex)
            {
                // TODO PLUG CODE HERE TO REINITIALIZE

                // If the device was removed either by a disconnect or a driver upgrade, we
                // must completely reinitialize the renderer.
                if (ex.ResultCode == SharpDX.DXGI.ResultCode.DeviceRemoved ||
                    ex.ResultCode == SharpDX.DXGI.ResultCode.DeviceReset)
                {
                    DeviceManager.Initialize(DeviceManager.Dpi);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #2
0
            public void Run()
            {
                Debug.WriteLine("Run");

                // First, create the Direct3D device.

                // This flag is required in order to enable compatibility with Direct2D.
                var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

#if DEBUG
                // If the project is in a debug build, enable debugging via SDK Layers with this flag.
                creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
#endif

                // This array defines the ordering of feature levels that D3D should attempt to create.
                var featureLevels = new SharpDX.Direct3D.FeatureLevel[]
                {
                    SharpDX.Direct3D.FeatureLevel.Level_11_1,
                    SharpDX.Direct3D.FeatureLevel.Level_11_0,
                };

                using (var d3dDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware
                                                                     , creationFlags, featureLevels))
                {
                    m_d3dDevice = d3dDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
                }
                m_d3dDeviceContext = m_d3dDevice.ImmediateContext1;

                // After the D3D device is created, create additional application resources.
                CreateWindowSizeDependentResources();

                // Enter the render loop.  Note that Windows Store apps should never exit.
                while (true)
                {
                    // Process events incoming to the window.
                    m_window.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessAllIfPresent);

                    // Specify the render target we created as the output target.
                    m_d3dDeviceContext.OutputMerger.SetRenderTargets(null,
                                                                     m_renderTargetView
                                                                     );

                    // Clear the render target to a solid color.
                    m_d3dDeviceContext.ClearRenderTargetView(
                        m_renderTargetView,
                        new SharpDX.Color4(0.071f, 0.04f, 0.561f, 1.0f)
                        );

                    // Present the rendered image to the window.  Because the maximum frame latency is set to 1,
                    // the render loop will generally be throttled to the screen refresh rate, typically around
                    // 60Hz, by sleeping the application on Present until the screen is refreshed.
                    m_swapChain.Present(1, 0);
                }
            }
Пример #3
0
        /// <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);
            }
        }
Пример #4
0
        /// <summary>
        /// Ends a draw operation.
        /// </summary>
        public void Dispose()
        {
            foreach (var layer in _layerPool)
            {
                layer.Dispose();
            }

            try
            {
                _deviceContext.EndDraw();

                _swapChain?.Present(1, SharpDX.DXGI.PresentFlags.None);
                _finishedCallback?.Invoke();
            }
            catch (SharpDXException ex) when((uint)ex.HResult == 0x8899000C)  // D2DERR_RECREATE_TARGET
            {
                throw new RenderTargetCorruptedException(ex);
            }
        }
Пример #5
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);
            }
        }
Пример #6
0
        double Draw()
        {
            var del = Content;

            if (del == null)
            {
                return(0);
            }

            var startT = DateTime.Now;

            g.BeginDrawing();
            g.BeginEntity(del);

            //
            // Draw
            //
            var fr = new System.Drawing.RectangleF(0, 0, (float)ActualWidth, (float)ActualHeight);

            //if (_ppi != NativePointsPerInch) {
            //	fr.Width /= _ppi / (float)NativePointsPerInch;
            //	fr.Height /= _ppi / (float)NativePointsPerInch;
            //}
            del.Frame = fr;
            try {
                Console.WriteLine(g.Transform);
                //g.Transform = transform;
                g.Clear(Colors.Red);
                del.Draw(g);
            }
            catch (Exception) {
            }

            g.EndDrawing();

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

            var endT = DateTime.Now;

            return((endT - startT).TotalSeconds);
        }
Пример #7
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)
                {
                    // 源已改变,故需要变换抓取大小,首先改变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);
            }
        }
Пример #8
0
        private void OnRender()
        {
            lock (_renderContextLock)
            {
                if (_renderTarget?.IsDisposed ?? true)
                {
                    return;
                }

                _renderTarget.BeginDraw();
                _renderTarget.Clear(_backgroundColor);

                if (_experimentStarted) // Draw blocks
                {
                    var secsPassed = (CurrentTime - _stageUpdatedAt) / 1000.0;
                    switch (_paradigm)
                    {
                    case SsvepExperiment.Configuration.TestConfig.StimulationParadigm.Flicker:
                        foreach (var block in _blocks)
                        {
                            if (block.BorderWidth > 0)
                            {
                                _solidColorBrush.Color = _blockBorderColor;
                                _renderTarget.FillRectangle(block.BorderRect, _solidColorBrush);
                            }
                            if (!_trialStarted || block.Patterns == null)
                            {
                                _solidColorBrush.Color = _blockNormalColor;
                            }
                            else
                            {
                                _solidColorBrush.Color = Color.SmoothStep(_blockNormalColor, _blockFlashingColor,
                                                                          (float)ConvertCosineValueToGrayScale(block.Patterns[0].Sample(secsPassed)));
                            }
                            _renderTarget.FillRectangle(block.ContentRect, _solidColorBrush);

                            if (block.FixationPointSize > 0)
                            {
                                _solidColorBrush.Color = _blockFixationPointColor;
                                _renderTarget.FillEllipse(block.CenterPointEllipse, _solidColorBrush);
                            }
                        }
                        break;

                    case SsvepExperiment.Configuration.TestConfig.StimulationParadigm.DualFlickers:
                        foreach (var block in _blocks)
                        {
                            if (block.BorderWidth > 0)
                            {
                                _solidColorBrush.Color = _blockBorderColor;
                                _renderTarget.FillRectangle(block.BorderRect, _solidColorBrush);
                            }

                            for (var i = 0; i < block.DualFlickerRects.Length; i++)
                            {
                                if (!_trialStarted || block.Patterns == null)
                                {
                                    _solidColorBrush.Color = _blockNormalColor;
                                }
                                else
                                {
                                    _solidColorBrush.Color = Color.SmoothStep(_blockNormalColor, _blockFlashingColor,
                                                                              (float)ConvertCosineValueToGrayScale(block.Patterns[i].Sample(secsPassed)));
                                }
                                _renderTarget.FillRectangle(block.DualFlickerRects[i], _solidColorBrush);
                            }

                            if (block.FixationPointSize > 0)
                            {
                                _solidColorBrush.Color = _blockFixationPointColor;
                                _renderTarget.FillEllipse(block.CenterPointEllipse, _solidColorBrush);
                            }
                        }
                        break;
                    }
                }
                else if (!(_displayText?.IsBlank() ?? true)) // Draw text
                {
                    _solidColorBrush.Color = _fontColor;
                    _renderTarget.DrawText(_displayText, _textFormat, new RawRectangleF(0, 0, Width, Height),
                                           _solidColorBrush, SharpDX.Direct2D1.DrawTextOptions.None);
                }

                _renderTarget.EndDraw();

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