Beispiel #1
0
        private void DrawFrame()
        {
            try
            {
                if (_deviceLost)
                {
                    _context.InvokeReset();
                    _deviceLost = false;
#if DEBUG
                    _context.PerformanceMonitor.IncreaseLifetimeCounter(LifetimeCounters.DeviceResets);
#endif
                    return;
                }

                if (_drawState == null)
                {
                    _drawState = new DrawState(_container);
                }

                _drawState.IncrementFrameIndex();
                _drawState.FrameTime     = _gameTime.FrameTime;
                _drawState.FrameTimeMs   = _gameTime.FrameTimeMs;
                _drawState.TotalGameTime = _gameTime.TotalGameTime;
                _drawState.Paused        = _gameTime.Paused;
#if DEBUG
                _context.PerformanceMonitor.StartTimer(DeviceTimers.BeforeDraw);
#endif
                OnBeforeDraw();
#if DEBUG
                _context.PerformanceMonitor.StopTimer(DeviceTimers.BeforeDraw);
                _context.PerformanceMonitor.StartTimer(DeviceTimers.RenderTimer);
#endif
                ushort stackHeight, stateHeight, cameraHeight, preCull, postCull;

                _drawState.GetStackHeight(out stackHeight, out stateHeight, out cameraHeight, out preCull, out postCull);
                _drawState.PrepareForNewFrame();

                if (_drawState.BeginScene())
                {
                    _screenTarget.BeginWorld(_drawState);
                    _worldRenderChain.Execute(_drawState);
                    _drawState.UnbindShader();
                    _screenTarget.EndWorld(_drawState);

                    _screenTarget.BeginUI(_drawState);
                    _uiRenderChain.Execute(_drawState);
                    _drawState.UnbindShader();
                    _screenTarget.EndUI(_drawState);

                    _screenTarget.Combine(_drawState);

                    _drawState.ValidateStackHeight(stackHeight, stateHeight, cameraHeight, preCull, postCull);
                    _drawState.EndScene();
                }
#if DEBUG
                _context.PerformanceMonitor.StopTimer(DeviceTimers.RenderTimer);
                _context.PerformanceMonitor.StartTimer(DeviceTimers.AfterDraw);
#endif
                OnAfterDraw();
#if DEBUG
                _context.PerformanceMonitor.StopTimer(DeviceTimers.AfterDraw);
#endif
                _context.Present();
            }
            catch (SharpDXException e)
            {
                switch (_context.CheckDeviceState())
                {
                case DeviceState.DeviceHung:
                    throw new Exception("Device hung like a horse.", e);

                case DeviceState.DeviceLost:
                    _deviceLost = true;
                    break;

                case DeviceState.DeviceRemoved:
                    throw new Exception("Device was removed", e);

                case DeviceState.OutOfVideoMemory:
                    throw new Exception("Out of video memory", e);

                default: throw;
                }
            }
        }