Esempio n. 1
0
        internal static void Present()
        {
            if (m_swapchain != null)
            {
                GetRenderProfiler().StartProfilingBlock("Screenshot");
                if (m_screenshot.HasValue)
                {
                    if (m_screenshot.Value.SizeMult == VRageMath.Vector2.One)
                    {
                        MyCopyToRT.ClearAlpha(Backbuffer);
                        SaveScreenshotFromResource(Backbuffer);
                    }
                    else
                    {
                        TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult);
                    }
                }
                GetRenderProfiler().EndProfilingBlock();

                try
                {
                    MyManagers.OnFrameEnd();
                    MyGpuProfiler.IC_BeginBlock("Waiting for present");
                    GetRenderProfiler().StartProfilingBlock("Waiting for present");
                    m_swapchain.Present(m_settings.VSync ? 1 : 0, 0);
                    GetRenderProfiler().EndProfilingBlock();
                    MyGpuProfiler.IC_EndBlock();

                    MyStatsUpdater.Timestamps.Update(ref MyStatsUpdater.Timestamps.Present, ref MyStatsUpdater.Timestamps.PreviousPresent);

                    MyManagers.OnUpdate();

                    if (VRage.OpenVRWrapper.MyOpenVR.Static != null)
                    {
                        MyGpuProfiler.IC_BeginBlock("OpenVR.FrameDone");
                        GetRenderProfiler().StartProfilingBlock("OpenVR.FrameDone");

                        /*var handle=MyOpenVR.GetOverlay("menu");
                         * MyOpenVR.SetOverlayTexture(handle, MyRender11.Backbuffer.m_resource.NativePointer);
                         */
                        VRage.OpenVRWrapper.MyOpenVR.FrameDone();
                        GetRenderProfiler().EndProfilingBlock();
                        MyGpuProfiler.IC_EndBlock();
                    }
                }
                catch (SharpDXException e)
                {
                    Log.WriteLine("Graphics device error! Reason:\n" + e.Message);

                    if (e.Descriptor == SharpDX.DXGI.ResultCode.DeviceRemoved)
                    {
                        Log.WriteLine("Device removed reason:\n" + Device.DeviceRemovedReason);
                    }

                    Log.WriteLine("Exception stack trace:\n" + e.StackTrace);

                    StringBuilder sb = new StringBuilder();

                    foreach (var column in MyRenderStats.m_stats.Values)
                    {
                        foreach (var stats in column)
                        {
                            sb.Clear();
                            stats.WriteTo(sb);
                            Log.WriteLine(sb.ToString());
                        }
                    }

                    MyStatsUpdater.UpdateStats();
                    MyStatsDisplay.WriteTo(sb);
                    Log.WriteLine(sb.ToString());

                    throw new MyDeviceErrorException("The graphics device encountered a problem.\nSee the log for more details.");
                }

                GetRenderProfiler().StartProfilingBlock("GPU profiler");
                MyGpuProfiler.EndFrame();
                MyGpuProfiler.StartFrame();
                GetRenderProfiler().EndProfilingBlock();

                // waiting for change to fullscreen - window migh overlap or some other dxgi excuse to fail :(
                GetRenderProfiler().StartProfilingBlock("TryChangeToFullscreen");
                TryChangeToFullscreen();
                GetRenderProfiler().EndProfilingBlock();
            }
        }
        internal static void Present()
        {
            if (m_swapchain != null)
            {
                if (m_screenshot.HasValue)
                {
                    if (m_screenshot.Value.SizeMult == VRageMath.Vector2.One)
                    {
                        MyCopyToRT.ClearAlpha(Backbuffer);
                        SaveScreenshotFromResource(Backbuffer.m_resource);
                    }
                    else
                    {
                        TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult);
                    }
                }

                GetRenderProfiler().StartProfilingBlock("Waiting for present");

                try
                {
                    m_swapchain.Present(m_settings.VSync ? 1 : 0, 0);
                    m_consecutivePresentFails = 0;

                    if (m_presentTimer == null)
                    {
                        m_presentTimer = new Stopwatch();
                    }
                    else
                    {
                        m_presentTimer.Stop();

                        if (m_presentTimes.Count >= PresentTimesStored)
                        {
                            m_presentTimes.Dequeue();
                        }
                        m_presentTimes.Enqueue(m_presentTimer.ElapsedMilliseconds);
                    }

                    m_presentTimer.Restart();
                }
                catch (SharpDXException e)
                {
                    Log.WriteLine("Device removed - resetting device; reason: " + e.ToString());
                    HandleDeviceReset();
                    Log.WriteLine("Device removed - resetting completed");

                    m_consecutivePresentFails++;

                    if (m_consecutivePresentFails == 5)
                    {
                        Log.WriteLine("Present failed");
                        Log.IncreaseIndent();

                        if (e.Descriptor == SharpDX.DXGI.ResultCode.DeviceRemoved)
                        {
                            Log.WriteLine("Device removed: " + Device.DeviceRemovedReason);
                        }

                        var timings = "";
                        while (m_presentTimes.Count > 0)
                        {
                            timings += m_presentTimes.Dequeue();
                            if (m_presentTimes.Count > 0)
                            {
                                timings += ", ";
                            }
                        }

                        Log.WriteLine("Last present timings = [ " + timings + " ]");
                        Log.DecreaseIndent();
                    }
                }

                GetRenderProfiler().EndProfilingBlock();

                if (m_profilingStarted)
                {
                    MyGpuProfiler.IC_EndBlock();
                }

                MyGpuProfiler.EndFrame();
                MyGpuProfiler.StartFrame();
                m_profilingStarted = true;

                // waiting for change to fullscreen - window migh overlap or some other dxgi excuse to fail :(
                TryChangeToFullscreen();
            }
        }
        internal static void Present()
        {
            if (m_swapchain != null)
            {
                GetRenderProfiler().StartProfilingBlock("Screenshot");
                if (m_screenshot.HasValue)
                {
                    if (m_screenshot.Value.SizeMult == VRageMath.Vector2.One)
                    {
                        MyCopyToRT.ClearAlpha(Backbuffer);
                        SaveScreenshotFromResource(Backbuffer.Resource);
                    }
                    else
                    {
                        TakeCustomSizedScreenshot(m_screenshot.Value.SizeMult);
                    }
                }
                GetRenderProfiler().EndProfilingBlock();

                try
                {
                    MyManagers.OnFrameEnd();
                    MyGpuProfiler.IC_BeginBlock("Waiting for present");
                    GetRenderProfiler().StartProfilingBlock("Waiting for present");
                    m_swapchain.Present(m_settings.VSync ? 1 : 0, 0);
                    GetRenderProfiler().EndProfilingBlock();
                    MyGpuProfiler.IC_EndBlock();

                    MyStatsUpdater.Timestamps.Update(ref MyStatsUpdater.Timestamps.Present, ref MyStatsUpdater.Timestamps.PreviousPresent);

                    if (VRage.OpenVRWrapper.MyOpenVR.Static != null)
                    {
                        MyGpuProfiler.IC_BeginBlock("OpenVR.FrameDone");
                        GetRenderProfiler().StartProfilingBlock("OpenVR.FrameDone");

                        /*var handle=MyOpenVR.GetOverlay("menu");
                         * MyOpenVR.SetOverlayTexture(handle, MyRender11.Backbuffer.m_resource.NativePointer);
                         */
                        VRage.OpenVRWrapper.MyOpenVR.FrameDone();
                        GetRenderProfiler().EndProfilingBlock();
                        MyGpuProfiler.IC_EndBlock();
                    }

                    m_consecutivePresentFails = 0;

                    GetRenderProfiler().StartProfilingBlock("Stopwatch");
                    if (m_presentTimer == null)
                    {
                        m_presentTimer = new Stopwatch();
                    }
                    else
                    {
                        m_presentTimer.Stop();

                        if (m_presentTimes.Count >= PresentTimesStored)
                        {
                            m_presentTimes.Dequeue();
                        }
                        m_presentTimes.Enqueue(m_presentTimer.ElapsedMilliseconds);
                    }

                    m_presentTimer.Restart();
                    GetRenderProfiler().EndProfilingBlock();
                }
                catch (SharpDXException e)
                {
                    Log.WriteLine("Device removed - resetting device; reason: " + e.Message);
                    HandleDeviceReset();
                    Log.WriteLine("Device removed - resetting completed");

                    m_consecutivePresentFails++;

                    if (m_consecutivePresentFails == 5)
                    {
                        Log.WriteLine("Present failed");
                        Log.IncreaseIndent();

                        if (e.Descriptor == SharpDX.DXGI.ResultCode.DeviceRemoved)
                        {
                            Log.WriteLine("Device removed: " + Device.DeviceRemovedReason);
                        }

                        var timings = "";
                        while (m_presentTimes.Count > 0)
                        {
                            timings += m_presentTimes.Dequeue();
                            if (m_presentTimes.Count > 0)
                            {
                                timings += ", ";
                            }
                        }

                        Log.WriteLine("Last present timings = [ " + timings + " ]");
                        Log.DecreaseIndent();
                    }
                }

                GetRenderProfiler().StartProfilingBlock("GPU profiler");
                MyGpuProfiler.EndFrame();
                MyGpuProfiler.StartFrame();
                m_profilingStarted = true;
                GetRenderProfiler().EndProfilingBlock();

                // waiting for change to fullscreen - window migh overlap or some other dxgi excuse to fail :(
                GetRenderProfiler().StartProfilingBlock("TryChangeToFullscreen");
                TryChangeToFullscreen();
                GetRenderProfiler().EndProfilingBlock();
            }
        }