public void DrawPerfEvents(int lastFrameIndex)
        {
            // Next valid index
            int windowEnd = (lastFrameIndex + 1 + MyProfiler.UPDATE_WINDOW) % MyProfiler.MAX_FRAMES;

            // Draw graphs for selected events
            var children = m_selectedProfiler.SelectedRootChildren;

            for (int i = 0; i < children.Count; i++)
            {
                var block = children[i];
                DrawBlockLineSeparated(block.Miliseconds, lastFrameIndex, windowEnd, m_milisecondsGraphScale, IndexToColor(i));

                // Broken/Unused
                //DrawBlockLineSeparated(MemoryProfiling ? block.ProcessMemory : block.ManagedMemory, lastFrameIndex, windowEnd, m_memoryGraphScale, IndexToColor(i));
            }

            // Draw legend
            float x_legend_start   = m_milisecondsGraphScale.x_start - 0.02f;
            float x_legend_ms_size = 0.01f;

            m_lineBatch.DrawOnScreenLine(new Vector3(-1.0f + x_legend_start, 0, 0), new Vector3(-1.0f + x_legend_start, m_milisecondsGraphScale.y_scale, 0), Color.Silver);

            m_text.Clear();

            float viewportWidth  = MyRender.GraphicsDevice.Viewport.Width;
            float viewportHeight = MyRender.GraphicsDevice.Viewport.Height;

            // Miliseconds legend
            for (int i = 0; i <= m_milisecondsGraphScale.y_legend_ms_count; i++)
            {
                m_lineBatch.DrawOnScreenLine(new Vector3(-1.0f + x_legend_start, m_milisecondsGraphScale.y_legend_increment * i, 0),
                                             new Vector3(-1.0f + x_legend_start + x_legend_ms_size, m_milisecondsGraphScale.y_legend_increment * i, 0), Color.Silver);

                m_text.Clear();
                m_text.Append((i * m_milisecondsGraphScale.y_legend_ms_increment).ToString());
                MyRender.DrawText(new Vector2(0.5f * viewportWidth * x_legend_start - 25f + 3 * x_legend_ms_size, -10 + 0.5f * viewportHeight - m_milisecondsGraphScale.y_legend_increment * i * 0.5f * viewportHeight), m_text, Color.Silver, 0.7f);
            }

            m_text.Clear();
            m_text.Append("[ms]");
            MyRender.DrawText(new Vector2(0.5f * viewportWidth * x_legend_start - 25f + 3 * x_legend_ms_size, -10 + 0.5f * viewportHeight - m_milisecondsGraphScale.y_scale * 0.5f * viewportHeight * 1.05f), m_text, Color.Silver, 0.7f);

            // Broken/Unused
            // Memory legend
            //x_legend_start = m_milisecondsGraphScale.x_start + 0.48f;
            //x_legend_ms_size = -0.01f;

            //for (int i = 0; i <= legendMsCount; i++)
            //{
            //    m_text.Clear();
            //    m_text.AppendDecimal((i * m_memoryGraphScale.y_range / legendMsCount), 4);
            //    MyRender.DrawText(new Vector2(0.5f * viewportWidth * x_legend_start - 25f + 3 * x_legend_ms_size,
            //        -10 + 0.85f * viewportHeight - m_memoryGraphScale.y_scale * 0.5f * viewportHeight * ((float)i / legendMsCount)),
            //        m_text, Color.Yellow, 0.7f);

            //    if (i == 0)
            //    {
            //        m_text.Clear();
            //        m_text.Append("[MB]");
            //        MyRender.DrawText(new Vector2(0.5f * viewportWidth * x_legend_start - 25f + 3 * x_legend_ms_size,
            //            -30 + 0.85f * viewportHeight - m_memoryGraphScale.y_scale * 0.5f * viewportHeight * ((float)i / legendMsCount)),
            //            m_text, Color.Yellow, 0.7f);
            //    }
            //}

            // Draw selected frame
            if (m_useCustomFrame)
            {
                Vector3 v0, v1;
                if (m_selectedFrame >= 0 && m_selectedFrame < MyProfiler.MAX_FRAMES)
                {
                    v0.X = -1.0f + m_memoryGraphScale.x_start + m_memoryGraphScale.x_scale * (m_selectedFrame) / 512.0f;
                    v0.Y = m_memoryGraphScale.y_start;
                    v0.Z = 0;

                    v1.X = v0.X;
                    v1.Y = 0.9f;
                    v1.Z = 0;

                    m_lineBatch.DrawOnScreenLine(v0, v1, Color.Yellow);
                }
            }
        }
 public override float DrawText(Vector2 screenCoord, StringBuilder text, Color color, float scale)
 {
     return(MyRender.DrawText(screenCoord, text, color, scale));
 }
        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)
            {
                SharpDX.Direct3D9.Device device = MyRender.GraphicsDevice;
                m_lineBatch = new MyLineBatch(Matrix.Identity, Matrix.CreateOrthographicOffCenter(0, device.Viewport.Width, device.Viewport.Height, 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       = MyRender.GraphicsDevice.Viewport.Height / 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.OwnerThread.Name).AppendLine();
                m_text.Append("Level limit: ").AppendInt32(m_levelLimit).AppendLine();
                MyRender.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);
                MyRender.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);
                MyRender.DrawText(new Vector2(20, textOffsetY), m_text, Color.Red, 1);
                textOffsetY += largeTextLineSize;

                textOffsetY = MyRender.GraphicsDevice.Viewport.Height / 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();
            }

            // Update horizontal offset
            if (!Paused && !m_useCustomFrame)
            {
                m_selectedFrame = lastFrameIndex;
            }
        }