Add GPU performance queries.
authorDavid Gow <[email protected]>
Sun, 27 Apr 2014 13:59:06 +0000 (21:59 +0800)
committerDavid Gow <[email protected]>
Sun, 27 Apr 2014 13:59:06 +0000 (21:59 +0800)
bin/ipdf
src/main.h
src/screen.cpp
src/screen.h

index 29d876f..a39e28c 100755 (executable)
Binary files a/bin/ipdf and b/bin/ipdf differ
index e6100ed..6821399 100644 (file)
@@ -70,7 +70,8 @@ inline void MainLoop(Document & doc, const Rect & bounds = Rect(0,0,1,1), const
        {
                scr.Clear();
                view.Render(scr.ViewportWidth(), scr.ViewportHeight());
-               scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (SDL_GetPerformanceCounter() - init_time)* 1000.0/SDL_GetPerformanceFrequency(), SDL_GetPerformanceFrequency()/(SDL_GetPerformanceCounter() - init_time));
+               scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeCPU())* 1000.0, 1.0/scr.GetLastFrameTimeCPU());
+               scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeGPU())* 1000.0, 1.0/scr.GetLastFrameTimeGPU());
                scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
                if (view.UsingGPUTransform())
                {
index 40754b3..9687c55 100644 (file)
@@ -94,6 +94,12 @@ Screen::Screen()
                Fatal("Your system does not support the ARB_explicit_attrib_location extension, which is required.");
        }
 
+       m_frame_begin_time = SDL_GetPerformanceCounter();
+       m_last_frame_time = 0;
+       m_last_frame_gpu_timer = 0;
+       glGenQueries(1, &m_frame_gpu_timer);
+       glBeginQuery(GL_TIME_ELAPSED, m_frame_gpu_timer);
+
        glDebugMessageCallback(opengl_debug_callback, 0);
 
        GLuint default_vao;
@@ -230,7 +236,24 @@ void Screen::Present()
 {
        if (m_debug_font_atlas)
                DebugFontFlush();
+       m_last_frame_time = SDL_GetPerformanceCounter() - m_frame_begin_time;
+       glEndQuery(GL_TIME_ELAPSED);
        SDL_GL_SwapWindow(m_window);
+       m_frame_begin_time = SDL_GetPerformanceCounter();
+       if (m_last_frame_gpu_timer)
+               glDeleteQueries(1, &m_last_frame_gpu_timer);
+       m_last_frame_gpu_timer = m_frame_gpu_timer;
+       glGenQueries(1, &m_frame_gpu_timer);
+       glBeginQuery(GL_TIME_ELAPSED, m_frame_gpu_timer);
+}
+
+double Screen::GetLastFrameTimeGPU() const
+{
+       if (!m_last_frame_gpu_timer)
+               return 0;
+       uint64_t frame_time_ns;
+       glGetQueryObjectui64v(m_last_frame_gpu_timer, GL_QUERY_RESULT, &frame_time_ns);
+       return frame_time_ns/1000000000.0;
 }
 
 void Screen::ScreenShot(const char * filename) const
index d918dd1..f66c7aa 100644 (file)
@@ -59,6 +59,11 @@ namespace IPDF
 
                void ScreenShot(const char * filename) const;
                void RenderBMP(const char * filename) const;
+
+               // Returns the CPU time (in seconds) it took to render the last completed frame.
+               double GetLastFrameTimeCPU() const { return m_last_frame_time / SDL_GetPerformanceFrequency(); }
+               // Returns the GPU time (in seconds) it took to render the last completed frame.
+               double GetLastFrameTimeGPU() const;
        private:
                void ResizeViewport(int width, int height);
                void DebugFontFlush();
@@ -67,6 +72,11 @@ namespace IPDF
                int m_last_mouse_x;
                int m_last_mouse_y;
 
+               double m_last_frame_time;
+               double m_frame_begin_time;
+               GLuint m_frame_gpu_timer;
+               GLuint m_last_frame_gpu_timer;
+
                int m_viewport_width;
                int m_viewport_height;
                SDL_Window *m_window;

UCC git Repository :: git.ucc.asn.au