Ejemplo n.º 1
0
        public static MyProfiler.MyProfilerBlock FindBlockByMax(int frameIndex, int lastValidFrame)
        {
            if (!IsValidIndex(frameIndex, lastValidFrame))
            {
                return(null);
            }

            float max = float.MinValue;

            MyProfiler.MyProfilerBlock block = null;

            var children = m_selectedProfiler.SelectedRootChildren;

            for (int i = 0; i < children.Count; i++)
            {
                var   profilerBlock = children[i];
                float val           = profilerBlock.Miliseconds[frameIndex];
                if (val > max)
                {
                    max   = val;
                    block = profilerBlock;
                }
            }
            return(block);
        }
Ejemplo n.º 2
0
        public static void HandleInput(RenderProfilerCommand command, int index)
        {
            switch (command)
            {
            case RenderProfilerCommand.Enable:
            {
                if (!m_enabled)
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;         // Enable when disabled and keep enabled
                    SetLevel();
                }
                break;
            }

            case RenderProfilerCommand.ToggleEnabled:
            {
                // Enable or Disable profiler drawing
                if (m_enabled)
                {
                    m_enabled        = false;
                    m_useCustomFrame = false;
                }
                else
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;         // Enable when disabled and keep enabled
                }
                break;
            }

            case RenderProfilerCommand.JumpToRoot:
                m_selectedProfiler.SelectedRoot = null;
                break;

            case RenderProfilerCommand.JumpToLevel:
            {
                // Enable when disabled, added this for programmers who are too used to using the numpad 0 to open the profiler.
                if (index == 0 && !m_enabled)
                {
                    m_enabled = true;
                    m_profilerProcessingEnabled = true;
                }
                else
                {
                    MyProfiler.MyProfilerBlock nextBlock = FindBlockByIndex(index - 1);         // On screen it's indexed from 1 (zero is level up)
                    if (nextBlock != null || (m_selectedProfiler.SelectedRoot != null && m_selectedProfiler.SelectedRoot.Parent == null))
                    {
                        m_selectedProfiler.SelectedRoot = nextBlock;
                    }
                }
                break;
            }

            case RenderProfilerCommand.Pause:
            {
                Paused = !Paused;
                break;
            }

            case RenderProfilerCommand.NextThread:
            {
                lock (m_threadProfilers)
                {
                    int profilerIndex = (m_threadProfilers.IndexOf(m_selectedProfiler) + 1) % m_threadProfilers.Count;
                    m_selectedProfiler = m_threadProfilers[profilerIndex];
                }
                break;
            }

            case RenderProfilerCommand.PreviousThread:
            {
                lock (m_threadProfilers)
                {
                    int profilerIndex = (m_threadProfilers.IndexOf(m_selectedProfiler) - 1 + m_threadProfilers.Count) % m_threadProfilers.Count;
                    m_selectedProfiler = m_threadProfilers[profilerIndex];
                }
                break;
            }

            case RenderProfilerCommand.Reset:
            {
                lock (m_threadProfilers)
                {
                    foreach (var profiler in m_threadProfilers)
                    {
                        profiler.Reset();
                    }
                    m_selectedFrame = 0;
                }
                break;
            }

            case RenderProfilerCommand.NextFrame:
            {
                MyRenderProfiler.NextFrame(index);
                break;
            }

            case RenderProfilerCommand.PreviousFrame:
            {
                MyRenderProfiler.PreviousFrame(index);
                break;
            }

            case RenderProfilerCommand.DisableFrameSelection:
            {
                m_useCustomFrame = false;
                break;
            }

            case RenderProfilerCommand.IncreaseLevel:
            {
                m_levelLimit++;
                SetLevel();
                break;
            }

            case RenderProfilerCommand.DecreaseLevel:
            {
                m_levelLimit--;
                if (m_levelLimit < -1)
                {
                    m_levelLimit = -1;
                }
                SetLevel();
                break;
            }

            case RenderProfilerCommand.CopyPathToClipboard:
            {
                StringBuilder pathBuilder = new StringBuilder(200);
                MyProfiler.MyProfilerBlock currentBlock = m_selectedProfiler.SelectedRoot;

                while (currentBlock != null)
                {
                    if (pathBuilder.Length > 0)
                    {
                        pathBuilder.Insert(0, " > ");
                    }
                    pathBuilder.Insert(0, currentBlock.Name);
                    currentBlock = currentBlock.Parent;
                }

                if (pathBuilder.Length > 0)
                {
                    // Clipboard can only be accessed from a thread on the STA apartment
                    System.Threading.Thread thread = new System.Threading.Thread(() => System.Windows.Forms.Clipboard.SetText(pathBuilder.ToString()));
                    thread.SetApartmentState(System.Threading.ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                }
                break;
            }

            case RenderProfilerCommand.TryGoToPathInClipboard:
            {
                string fullPath = string.Empty;

                Exception threadEx = null;
                System.Threading.Thread staThread = new System.Threading.Thread(
                    delegate()
                    {
                        try
                        {
                            fullPath = System.Windows.Forms.Clipboard.GetText();
                        }

                        catch (Exception ex)
                        {
                            threadEx = ex;
                        }
                    });
                staThread.SetApartmentState(System.Threading.ApartmentState.STA);
                staThread.Start();
                staThread.Join();

                if (!string.IsNullOrEmpty(fullPath))
                {
                    string[] split = fullPath.Split(new string[] { " > " }, StringSplitOptions.None);

                    MyProfiler.MyProfilerBlock        pathBlock = null;
                    List <MyProfiler.MyProfilerBlock> blockSet  = m_selectedProfiler.RootBlocks;
                    for (int i = 0; i < split.Length; i++)
                    {
                        string blockName = split[i];
                        MyProfiler.MyProfilerBlock oldPath = pathBlock;

                        for (int j = 0; j < blockSet.Count; j++)
                        {
                            MyProfiler.MyProfilerBlock block = blockSet[j];
                            if (block.Name == blockName)
                            {
                                pathBlock = block;
                                blockSet  = pathBlock.Children;
                                break;
                            }
                        }

                        // If the path did not change, we cannot go any deeper, break out of this loop
                        if (oldPath == pathBlock)
                        {
                            break;
                        }
                    }

                    if (pathBlock != null)
                    {
                        m_selectedProfiler.SelectedRoot = pathBlock;
                    }
                }
                break;
            }

            case RenderProfilerCommand.SetLevel:
            {
                m_levelLimit = index;
                if (m_levelLimit < -1)
                {
                    m_levelLimit = -1;
                }
                SetLevel();
                break;
            }

            case RenderProfilerCommand.DecreaseLocalArea:
                m_frameLocalArea = Math.Max(2, m_frameLocalArea / 2);
                break;

            case RenderProfilerCommand.IncreaseLocalArea:
                m_frameLocalArea = Math.Min(MyProfiler.MAX_FRAMES, m_frameLocalArea * 2);
                break;

            case RenderProfilerCommand.IncreaseRange:
                m_milisecondsGraphScale.IncreaseYRange();
                break;

            case RenderProfilerCommand.DecreaseRange:
                m_milisecondsGraphScale.DecreaseYRange();
                break;

            case RenderProfilerCommand.ChangeSortingOrder:
                m_sortingOrder += 1;
                if (m_sortingOrder >= RenderProfilerSortingOrder.NumSortingTypes)
                {
                    m_sortingOrder = RenderProfilerSortingOrder.Id;
                }
                break;

            default:
                System.Diagnostics.Debug.Assert(false, "Unknown command");
                break;
            }
        }
Ejemplo n.º 3
0
 static MyRenderProfiler()
 {
     // Create block, some unique id
     m_fpsBlock = MyProfiler.CreateExternalBlock("FPS", -2);
 }
        void DrawEvent(float textPosY, MyProfiler.MyProfilerBlock profilerBlock, int blockIndex, int frameIndex, int lastValidFrame)
        {
            Color color         = IndexToColor(blockIndex);
            float miliseconds   = 0;
            float managedMemory = 0;
            float processMemory = 0;
            int   numCalls      = -1; // To show update window in profiler
            float customValue   = 0;

            if (IsValidIndex(frameIndex, lastValidFrame))
            {
                miliseconds   = profilerBlock.Miliseconds[frameIndex];
                managedMemory = profilerBlock.ManagedMemory[frameIndex];
                processMemory = profilerBlock.ProcessMemory[frameIndex];
                numCalls      = profilerBlock.NumCallsArray[frameIndex];
                customValue   = profilerBlock.CustomValues[frameIndex];
            }

            float Y_TEXT_POSITION = ViewportSize.Y / 2;

            float textScale = 0.7f;

            m_text.Clear().Append(blockIndex + 1).Append(" ").Append(profilerBlock.Name);
            DrawTextShadow(new Vector2(20, textPosY), m_text, color, textScale);

            float length = 500;

            m_text.Clear();
            m_text.Append("(").Append(profilerBlock.Children.Count).Append(") ");
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 50 * textScale;

            m_text.Clear();
            //text.Append(((index != -1 ? profilerBlock.TimePercentage[index] : profilerBlock.averagePctg)).ToString("#,#0.0%"));
            //MyDebugDraw.DrawTextShadow(new Vector2(20 + length, textPosY), text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", miliseconds);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 155 * textScale;

            m_text.Clear();
            m_text.Concat(managedMemory, 3).Append(" GC");
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
            length += 40 + 158 * textScale;

            m_text.Clear();
            if (MemoryProfiling)
            {
                m_text.Concat(processMemory, 3).Append(" MB");
                DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
                length += 158 * textScale;

                m_text.Clear();
            }

            length += 40 + 40 * textScale;
            m_text.ConcatFormat(profilerBlock.CallFormat ?? "{0} calls", numCalls);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += 150 * textScale;
            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.ValueFormat ?? "Custom: {0:.00}", customValue);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            int maxIndex;

            length += 250 * textScale;
            float max = FindMaxWrap(profilerBlock.Miliseconds, frameIndex - m_frameLocalArea / 2, frameIndex + m_frameLocalArea / 2, lastValidFrame, out maxIndex);

            m_text.Clear();
            m_text.ConcatFormat(profilerBlock.TimeFormat ?? "{0:.00}ms", max);
            DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);

            length += DrawTextShadow(new Vector2(20 + length, textPosY), m_text, color, textScale);
        }
Ejemplo n.º 5
0
 static MyRenderProfiler()
 {
     m_levelLimit = VRage.MyCompilationSymbols.ProfileFromStart ? -1 : 0;
     // Create block, some unique id
     m_fpsBlock = MyProfiler.CreateExternalBlock("FPS", -2);
 }
        protected sealed override void Draw(MyProfiler drawProfiler, int lastFrameIndex, int frameToDraw)
        {
            Debug.Assert(frameToDraw >= 0 && frameToDraw < MyProfiler.MAX_FRAMES, "Invalid selected frame");

            if (!m_initialized)
            {
                // Init linebatch
                Init();
                m_initialized = true;
                m_fpsBlock.Start(false);
            }

            // Handle FPS timer
            m_fpsBlock.End(false);

            float elapsedTime    = (float)m_fpsBlock.Elapsed.Seconds;
            float invElapsedTime = elapsedTime > 0 ? 1 / elapsedTime : 0;

            m_fpsPctg = 0.9f * m_fpsPctg + 0.1f * invElapsedTime;

            if (MemoryProfiling)
            {
                // Handle memory usage for frame
                float processDeltaMB = m_fpsBlock.ProcessDeltaMB;
                m_fpsBlock.ProcessMemory[lastFrameIndex] = processDeltaMB;
            }

            float managedDeltaMB = m_fpsBlock.ManagedDeltaMB;

            m_fpsBlock.ManagedMemory[lastFrameIndex] = managedDeltaMB;
            m_fpsBlock.CustomValues[lastFrameIndex]  = m_fpsBlock.CustomValue;

            m_fpsBlock.Reset();

            m_fpsBlock.Start(false);

            if (m_enabled)
            {
                // Draw events as text
                float eventLineSize     = 20;
                float largeTextLineSize = 28;
                float textOffsetY       = ViewportSize.Y / 2 - 8 * largeTextLineSize;

                // Draw thread name and level limit
                m_text.Clear();
                m_text.ConcatFormat("\"{2}\" ({0}/{1})", m_selectedProfiler.GlobalProfilerIndex + 1, m_threadProfilers.Count, m_selectedProfiler.DisplayedName).AppendLine();
                m_text.Append("Level limit: ").AppendInt32(m_selectedProfiler.LevelLimit).AppendLine();
                DrawText(new Vector2(20, textOffsetY), m_text, Color.LightGray, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw frame number and local area
                m_text.Clear();
                m_text.Append("Frame: ").AppendInt32(frameToDraw).AppendLine();
                m_text.Append("Local area: ").AppendInt32(m_frameLocalArea);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Yellow, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw fps and total calls
                m_text.Clear();
                m_text.Append(m_fpsBlock.Name).Append(" ");
                if (!m_useCustomFrame) // Show FPS only when not using custom frame
                {
                    m_text.AppendDecimal(m_fpsPctg, 3);
                }
                m_text.AppendLine();
                m_text.Append("Total calls: ").AppendInt32(IsValidIndex(frameToDraw, lastFrameIndex) ? m_selectedProfiler.TotalCalls[frameToDraw] : -1);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Red, 1);
                textOffsetY += largeTextLineSize;

                m_text.Clear();
                if (!VRage.MyCompilationSymbols.PerformanceProfiling)
                {
                    m_text.Append("MyCompilationSymbols.PerformanceProfiling NOT ENABLED!").AppendLine();
                }
                if (!ProfilerProcessingEnabled)
                {
                    m_text.Append("Profiler processing disabled, F12 -> Profiler").AppendLine();
                }
                DrawText(new Vector2(0, 0), m_text, Color.Yellow, 0.6f);

                textOffsetY = ViewportSize.Y / 2;
                var children = m_selectedProfiler.SelectedRootChildren;
                for (int i = 0; i < children.Count; i++)
                {
                    MyProfiler.MyProfilerBlock profilerBlock = children[i];

                    DrawEvent(textOffsetY, profilerBlock, i, frameToDraw, lastFrameIndex);
                    textOffsetY += eventLineSize;
                }

                // Draw graphs
                BeginLineBatch();
                DrawPerfEvents(lastFrameIndex);
                EndLineBatch();
            }

            // Update horizontal offset
            if (!Paused && !m_useCustomFrame)
            {
                m_selectedFrame = lastFrameIndex;
            }
        }
Ejemplo n.º 7
0
        protected sealed override void Draw(MyProfiler drawProfiler, int lastFrameIndex, int frameToDraw)
        {
            Debug.Assert(frameToDraw >= 0 && frameToDraw < MyProfiler.MAX_FRAMES, "Invalid selected frame");

            if (!m_initialized)
            {
                // Init linebatch
                Init();
                m_initialized = true;
                m_fpsBlock.Start(false);
            }

            // Handle FPS timer
            m_fpsBlock.End(false);

            float elapsedTime    = (float)m_fpsBlock.Elapsed.Seconds;
            float invElapsedTime = elapsedTime > 0 ? 1 / elapsedTime : 0;

            m_fpsPctg = 0.9f * m_fpsPctg + 0.1f * invElapsedTime;

            if (MemoryProfiling)
            {
                // Handle memory usage for frame
                float processDeltaMB = m_fpsBlock.ProcessDeltaMB;
                m_fpsBlock.ProcessMemory[lastFrameIndex] = processDeltaMB;
            }

            long managedDeltaMB = m_fpsBlock.ManagedDeltaMB;

            m_fpsBlock.ManagedMemoryBytes[lastFrameIndex] = managedDeltaMB;
            m_fpsBlock.CustomValues[lastFrameIndex]       = m_fpsBlock.CustomValue;

            m_fpsBlock.Reset();

            m_fpsBlock.Start(false);

            if (m_enabled)
            {
                // Draw events as text
                float eventLineSize     = 20;
                float largeTextLineSize = 28;
                float textOffsetY       = ViewportSize.Y / 2 - 8 * largeTextLineSize;

                // Draw thread name and level limit
                m_text.Clear();
                m_text.ConcatFormat("\"{2}\" ({0}/{1})", m_threadProfilers.IndexOf(m_selectedProfiler) + 1, m_threadProfilers.Count, m_selectedProfiler.DisplayedName).AppendLine();
                m_text.Append("Level limit: ").AppendInt32(m_selectedProfiler.LevelLimit).AppendLine();
                DrawText(new Vector2(20, textOffsetY), m_text, Color.LightGray, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw frame number and local area
                m_text.Clear();
                m_text.Append("Frame: ").AppendInt32(frameToDraw).AppendLine();
                m_text.Append("Local area: ").AppendInt32(m_frameLocalArea);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Yellow, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw fps and total calls
                m_text.Clear();
                m_text.Append(m_fpsBlock.Name).Append(" ");
                if (!m_useCustomFrame) // Show FPS only when not using custom frame
                {
                    m_text.AppendDecimal(m_fpsPctg, 3);
                }
                m_text.AppendLine();
                m_text.Append("Total calls: ").AppendInt32(IsValidIndex(frameToDraw, lastFrameIndex) ? m_selectedProfiler.TotalCalls[frameToDraw] : -1);
                DrawText(new Vector2(20, textOffsetY), m_text, Color.Red, 1);
                textOffsetY += largeTextLineSize;

                m_text.Clear();
                if (!VRage.MyCompilationSymbols.PerformanceProfiling)
                {
                    m_text.Append("MyCompilationSymbols.PerformanceProfiling NOT ENABLED!").AppendLine();
                }
                if (!ProfilerProcessingEnabled)
                {
                    m_text.Append("Profiler processing disabled, F12 -> Profiler").AppendLine();
                }
                DrawText(new Vector2(0, 0), m_text, Color.Yellow, 0.6f);

                textOffsetY = ViewportSize.Y / 2;
                List <MyProfiler.MyProfilerBlock> children       = m_selectedProfiler.SelectedRootChildren;
                List <MyProfiler.MyProfilerBlock> sortedChildren = GetSortedChildren(frameToDraw);

                // Draw the 'stack trace'
                m_text.Clear();
                MyProfiler.MyProfilerBlock currentBlock = m_selectedProfiler.SelectedRoot;

                while (currentBlock != null)
                {
                    // Stop inserting new elements if the path becomes too long
                    if (currentBlock.Name.Length + 3 + m_text.Length > 170)
                    {
                        m_text.Insert(0, "... > ");
                        break;
                    }

                    if (m_text.Length > 0)
                    {
                        m_text.Insert(0, " > ");
                    }
                    m_text.Insert(0, currentBlock.Name);
                    currentBlock = currentBlock.Parent;
                }

                DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                textOffsetY += eventLineSize;

                if (sortedChildren.Count > 0)
                {
                    // Draw the sorting order indicator
                    m_text.Clear().Append("\\/");
                    switch (m_sortingOrder)
                    {
                    case RenderProfilerSortingOrder.Id:
                        m_text.Append(" ASC");
                        DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                        break;

                    case RenderProfilerSortingOrder.MillisecondsLastFrame:
                        m_text.Append(" DESC");
                        DrawTextShadow(new Vector2(660, textOffsetY), m_text, Color.White, 0.7f);
                        break;

                    case RenderProfilerSortingOrder.MillisecondsAverage:
                        m_text.Append(" DESC");
                        DrawTextShadow(new Vector2(1270, textOffsetY), m_text, Color.White, 0.7f);
                        break;
                    }
                    textOffsetY += eventLineSize;

                    // Draw the profiler blocks
                    for (int i = 0; i < sortedChildren.Count; i++)
                    {
                        MyProfiler.MyProfilerBlock profilerBlock = sortedChildren[i];

                        Color lineColor = IndexToColor(children.IndexOf(profilerBlock));

                        DrawEvent(textOffsetY, profilerBlock, i, frameToDraw, lastFrameIndex, ref lineColor);
                        textOffsetY += eventLineSize;
                    }
                }
                else
                {
                    m_text.Clear().Append("No more blocks at this point!");
                    textOffsetY += eventLineSize;
                    DrawTextShadow(new Vector2(20, textOffsetY), m_text, Color.White, 0.7f);
                    textOffsetY += eventLineSize;
                }

                // Draw graphs
                BeginLineBatch();
                DrawPerfEvents(lastFrameIndex);
                EndLineBatch();
            }

            // Update horizontal offset
            if (!Paused && !m_useCustomFrame)
            {
                m_selectedFrame = lastFrameIndex;
            }
        }
Ejemplo n.º 8
0
        protected override void Draw(MyProfiler drawProfiler, int lastFrameIndex, int frameToDraw)
        {
            Debug.Assert(frameToDraw >= 0 && frameToDraw < MyProfiler.MAX_FRAMES, "Invalid selected frame");

            // Init linebatch
            if (m_lineBatch == null)
            {
                m_lineBatch = new MyRenderProfilerLineBatch(Matrix.Identity, Matrix.CreateOrthographicOffCenter(0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y, 0, 0, -1), 50000);
                m_fpsBlock.Start(false);
            }

            // Handle FPS timer
            m_fpsBlock.End(false);

            float elapsedTime    = (float)m_fpsBlock.Elapsed.Seconds;
            float invElapsedTime = elapsedTime > 0 ? 1 / elapsedTime : 0;

            m_fpsPctg = 0.9f * m_fpsPctg + 0.1f * invElapsedTime;

            if (MemoryProfiling)
            {
                // Handle memory usage for frame
                float processDeltaMB = m_fpsBlock.ProcessDeltaMB;
                m_fpsBlock.ProcessMemory[lastFrameIndex] = processDeltaMB;
            }

            float managedDeltaMB = m_fpsBlock.ManagedDeltaMB;

            m_fpsBlock.ManagedMemory[lastFrameIndex] = managedDeltaMB;
            m_fpsBlock.CustomValues[lastFrameIndex]  = m_fpsBlock.CustomValue;

            m_fpsBlock.Reset();

            m_fpsBlock.Start(false);

            if (m_enabled)
            {
                // Draw events as text
                float eventLineSize     = 20;
                float largeTextLineSize = 28;
                float textOffsetY       = MyRender11.ViewportResolution.Y / 2 - 8 * largeTextLineSize;

                // Draw thread name and level limit
                m_text.Clear();
                m_text.ConcatFormat("\"{2}\" ({0}/{1})", m_selectedProfiler.GlobalProfilerIndex + 1, m_threadProfilers.Count, m_selectedProfiler.DisplayedName).AppendLine();
                m_text.Append("Level limit: ").AppendInt32(m_levelLimit).AppendLine();
                MySpritesRenderer.DrawText(new Vector2(20, textOffsetY), m_text, Color.LightGray, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw frame number and local area
                m_text.Clear();
                m_text.Append("Frame: ").AppendInt32(frameToDraw).AppendLine();
                m_text.Append("Local area: ").AppendInt32(m_frameLocalArea);
                MySpritesRenderer.DrawText(new Vector2(20, textOffsetY), m_text, Color.Yellow, 1);
                textOffsetY += largeTextLineSize * 2 + 10;

                // Draw fps and total calls
                m_text.Clear();
                m_text.Append(m_fpsBlock.Name).Append(" ");
                if (!m_useCustomFrame) // Show FPS only when not using custom frame
                {
                    m_text.AppendDecimal(m_fpsPctg, 3);
                }
                m_text.AppendLine();
                m_text.Append("Total calls: ").AppendInt32(IsValidIndex(frameToDraw, lastFrameIndex) ? m_selectedProfiler.TotalCalls[frameToDraw] : -1);
                MySpritesRenderer.DrawText(new Vector2(20, textOffsetY), m_text, Color.Red, 1);
                textOffsetY += largeTextLineSize;

                textOffsetY = MyRender11.ViewportResolution.Y / 2;
                var children = m_selectedProfiler.SelectedRootChildren;
                for (int i = 0; i < children.Count; i++)
                {
                    MyProfiler.MyProfilerBlock profilerBlock = children[i];

                    DrawEvent(textOffsetY, profilerBlock, i, frameToDraw, lastFrameIndex);
                    textOffsetY += eventLineSize;
                }

                // Draw graphs
                m_lineBatch.Begin();
                DrawPerfEvents(lastFrameIndex);

                //VRageRender.Graphics.BlendState.Opaque.Apply();
                m_lineBatch.End();

                //GetRenderProfiler().StartProfilingBlock("MySpritesRenderer.Draw");
                MyLinesRenderer.Draw(null);
                MyCommon.UpdateFrameConstants();
                MySpritesRenderer.Draw(MyRender11.Backbuffer.m_RTV, new MyViewport(MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y));
                //GetRenderProfiler().EndProfilingBlock();

                //GetRenderProfiler().StartProfilingBlock("MyLinesRenderer.Draw");

                //GetRenderProfiler().EndProfilingBlock();
            }

            // Update horizontal offset
            if (!Paused && !m_useCustomFrame)
            {
                m_selectedFrame = lastFrameIndex;
            }
        }
Ejemplo n.º 9
0
        public void Draw()
        {
            if (!m_enabled)
            {
                return;
            }

            if (MyProfiler.EnableAsserts)
            {
                MyCommonDebugUtils.AssertDebug(m_selectedProfiler.CurrentProfilingStack.Count == 0, "Stack size must be 0!");
            }

            // Init linebatch
            if (m_lineBatch == null)
            {
                SharpDX.Direct3D9.Device device = MyMinerGame.Static.GraphicsDevice;
                m_lineBatch = new MyLineBatch(
                    Matrix.Identity,
                    Matrix.CreateOrthographicOffCenter(0.0F, device.Viewport.Width, device.Viewport.Height, 0.0F, 0.0F, -1.0F),
                    50000);

                m_fpsBlock.Stopwatch.Start();
            }


            // Handle FPS timer
            m_fpsBlock.End(false);

            float elapsedTime    = (float)m_fpsBlock.Stopwatch.Elapsed.TotalSeconds;
            float invElapsedTime = elapsedTime > 0 ? 1 / elapsedTime : 0;

            m_fpsBlock.averagePctg = 0.9f * m_fpsBlock.averagePctg + 0.1f * invElapsedTime;

#if MEMORY_PROFILING
            // Handle memory usage for frame
            float processDeltaMB = m_fpsBlock.ProcessDeltaMB;
            m_fpsBlock.ProcessMemory[m_index] = processDeltaMB;
#endif

            float managedDeltaMB = m_fpsBlock.ManagedDeltaMB;
            m_fpsBlock.ManagedMemory[m_index] = managedDeltaMB;
            m_fpsBlock.CustomValues[m_index]  = m_fpsBlock.CustomValue;

            m_fpsBlock.Stopwatch.Reset();

            m_fpsBlock.Start(false);

            int numCalls = 0;  // number of calls of StartProfilingBlock

            // Add new measured time to each graph
            if (!Paused)
            {
                foreach (MyProfiler.MyProfilerBlock profilerBlock in m_selectedProfiler.ProfilingBlocks.Values)
                {
                    float dt;

                    profilerBlock.ManagedMemory[m_index] = profilerBlock.ManagedDeltaMB;
#if MEMORY_PROFILING
                    profilerBlock.ProcessMemory[m_index] = profilerBlock.ProcessDeltaMB;
#endif
                    profilerBlock.NumCallsArray[m_index] = profilerBlock.NumCalls;
                    profilerBlock.CustomValues[m_index]  = profilerBlock.CustomValue;

                    dt = (float)profilerBlock.Stopwatch.Elapsed.TotalSeconds * 1000f;

                    profilerBlock.Miliseconds[m_index] = (float)profilerBlock.Stopwatch.Elapsed.TotalMilliseconds;
                    float pctg = (0.001f * dt) * invElapsedTime;
                    profilerBlock.averagePctg        = 0.9f * profilerBlock.averagePctg + (0.1f * pctg);
                    profilerBlock.averagePctg        = System.Math.Min(profilerBlock.averagePctg, 1.0f); // block cannot take more than 100% of elapsed fram time
                    profilerBlock.averageMiliseconds = 0.9f * profilerBlock.averageMiliseconds + 0.1f * (float)profilerBlock.Stopwatch.Elapsed.TotalMilliseconds;
                    profilerBlock.Stopwatch.Reset();

                    if (profilerBlock.ManagedDeltaMB > memoryRange)
                    {
                        memoryRange = 2 * profilerBlock.ManagedDeltaMB;
                    }

#if MEMORY_PROFILING
                    if (profilerBlock.ProcessDeltaMB > memoryRange)
                    {
                        memoryRange = 2 * profilerBlock.ProcessDeltaMB;
                    }
#endif
                    numCalls += profilerBlock.NumCalls;
                    profilerBlock.NumChildCalls = profilerBlock.GetNumChildCalls();
                }
            }

            if (m_enabled)
            {
                // Draw events as text
                float         Y_TEXT_POSITION = MyMinerGame.Static.GraphicsDevice.Viewport.Height / 2;
                StringBuilder text            = new StringBuilder(100);
                int           textOffsetY     = 0;

                if (m_selectedProfiler.SelectedRoot == null)
                {
                    foreach (MyProfiler.MyProfilerBlock profilerBlock in m_selectedProfiler.ProfilingBlocks.Values)
                    {
                        if (profilerBlock.Parent != null)
                        {
                            continue;
                        }

                        profilerBlock.color = textOffsetY < m_colors.Length ? m_colors[textOffsetY].PackedValue : Color.Green.PackedValue;

                        if (profilerBlock.NumCalls == 0)
                        {
                            profilerBlock.Cooldown--;
                        }
                        else
                        {
                            profilerBlock.Cooldown = 1000;
                        }

                        profilerBlock.DrawGraph = true;

                        if (m_useCustomFrame)
                        {
                            DrawEvent(textOffsetY, profilerBlock, m_selectedFrame);
                        }
                        else
                        {
                            DrawEvent(textOffsetY, profilerBlock, Paused ? (m_selectedFrame - 1) % MyProfiler.MAX_FRAMES : m_selectedFrame);
                        }

                        textOffsetY++;
                    }
                }
                else
                {
                    for (int i = 0; i < m_selectedProfiler.SelectedRoot.Children.Count; i++)
                    {
                        MyProfiler.MyProfilerBlock profilerBlock = m_selectedProfiler.SelectedRoot.Children[i];

                        profilerBlock.color = i < m_colors.Length ? m_colors[i].PackedValue : Color.Green.PackedValue;

                        if (profilerBlock.NumCalls == 0)
                        {
                            profilerBlock.Cooldown--;
                        }
                        else
                        {
                            profilerBlock.Cooldown = 1000;
                        }

                        profilerBlock.DrawGraph = true;

                        if (m_useCustomFrame)
                        {
                            DrawEvent(textOffsetY, profilerBlock, m_selectedFrame);
                        }
                        else
                        {
                            DrawEvent(textOffsetY, profilerBlock);
                        }
                        textOffsetY++;
                    }
                }

                // Draw thread name
                text.Clear();
                text.Append("Thread (" + m_selectedProfilerIndex + "/" + m_threadProfilers.Count + "): " + m_selectedThread.Name);
                text.Append(", Level limit: " + m_levelLimit.ToString());
                MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + (-2) * 20), text, Color.Gray, 1);

                // Draw fps
                text.Clear();
                text.Append(m_fpsBlock.Name);
                if (m_useCustomFrame && m_selectedFrame >= 0 && m_selectedFrame < MyProfiler.MAX_FRAMES)
                {
                    //text.Append(m_fpsBlock.TimePercentage[SelectedFrame].ToString(" #,###0.000"));
                    MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + (-1) * 20), text, Color.Red, 1);
                }
                else
                {
                    text.Append(m_fpsBlock.averagePctg.ToString(" #,###0.000"));
                    MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + (-1) * 20), text, Color.Red, 1);
                }


                // Total calls
                text.Clear();
                text.Append(numCalls.ToString("Total calls 0"));
                MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + (textOffsetY) * 20), text, Color.Red, 1);

                if (m_useCustomFrame)
                {
                    text.Clear();
                    text.Append(m_selectedFrame.ToString("Selected frame: 0"));
                    MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + 2 * (textOffsetY) * 20), text, Color.Yellow, 1);
                }

                // Handle stack checking
                if (m_stackCheckingDuration > 0)
                {
                    text.Clear();
                    text.Append("Checking Profiler Stack");
                    MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + (-3) * 20), text, Color.Orange, 1);

                    m_stackCheckingDuration -= elapsedTime;
                    m_stackCheckingDuration  = Math.Max(m_stackCheckingDuration, 0);

                    MyProfiler.StackChecking = true;   // enable checking for next frame
                }
                else
                {
                    MyProfiler.StackChecking = false;
                }


                // Draw graphs
                m_lineBatch.Begin();
                DrawPerfEvents();
                m_lineBatch.End();
            }

            // Update horizontal offset
            if (!Paused)
            {
                m_index = (++m_index) % MyProfiler.MAX_FRAMES;

                if (!m_useCustomFrame)
                {
                    m_selectedFrame = m_index;
                }
            }

            // Reset childs before next run
            foreach (MyProfiler.MyProfilerBlock profilerBlock in m_selectedProfiler.ProfilingBlocks.Values)
            {
                profilerBlock.StackChildren.Clear();
                profilerBlock.StackParent = null;
                profilerBlock.Stopwatch.Reset();
                profilerBlock.DrawGraph     = false;
                profilerBlock.NumCalls      = 0;
                profilerBlock.NumChildCalls = -1; // -1 means it needs to be computed first.

                profilerBlock.StartManagedMB = 0;
                profilerBlock.EndManagedMB   = 0;
                profilerBlock.DeltaManagedB  = 0;

#if MEMORY_PROFILING
                profilerBlock.startProcessMB = 0;
                profilerBlock.endProcessMB   = 0;
                profilerBlock.deltaProcessB  = 0;
#endif

                profilerBlock.CustomValue = 0;
            }
        }
Ejemplo n.º 10
0
        public void DrawEvent(int textOffsetY, MyProfiler.MyProfilerBlock profilerBlock, int index = -1)
        {
            float Y_TEXT_POSITION = MyMinerGame.Static.GraphicsDevice.Viewport.Height / 2;

            float textScale = 0.7f;

            StringBuilder text = new StringBuilder(100);

            text.Clear();
            text.Append((textOffsetY + 1).ToString("0 "));
            text.Append(profilerBlock.Name);

            MyDebugDraw.DrawText(new Vector2(20, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);

            float length = 500;

            text.Clear();
            text.Append(profilerBlock.Children.Count.ToString("(0) "));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
            length += 50 * textScale;

            text.Clear();
            //text.Append(((index != -1 ? profilerBlock.TimePercentage[index] : profilerBlock.averagePctg)).ToString("#,#0.0%"));
            //MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
            length += 155 * textScale;

            text.Clear();
            text.Append((index != -1 ? profilerBlock.Miliseconds[index] : profilerBlock.averageMiliseconds).ToString("#,##0.00ms"));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
            length += 155 * textScale;

            text.Clear();
            text.Append((index != -1 ? profilerBlock.ManagedMemory[index] : profilerBlock.TotalManagedMB).ToString("#,###0.000 GC"));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
            length += 40 + 158 * textScale;

            text.Clear();
#if MEMORY_PROFILING
            text.Append((index != -1 ? profilerBlock.ProcessMemory[index] : profilerBlock.totalProcessMB).ToString("#,###0.000 MB"));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
            length += 158 * textScale;

            text.Clear();
#endif

            length += 40 + 40 * textScale;
            text.Append(profilerBlock.NumCallsArray[index != -1 ? index : 0].ToString());
            text.Append(profilerBlock.NumCalls.ToString(" calls"));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);

            length += 150 * textScale;
            text.Clear();
            text.Append("Custom: ");
            text.Append((index != -1 ? profilerBlock.CustomValues[index] : profilerBlock.CustomValue).ToString("#,###0.000"));
            MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);



//             if (profilerBlock.NumCalls > 1)
//                 text.Append("s");

            length += MyDebugDraw.DrawText(new Vector2(20 + length, Y_TEXT_POSITION + textOffsetY * 20), text, UintToColor(profilerBlock.color), textScale);
        }