]> git.sesse.net Git - nageru/commitdiff
Move GPUTimers into its own file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 2 Aug 2018 15:14:28 +0000 (17:14 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 2 Aug 2018 15:14:28 +0000 (17:14 +0200)
Makefile
flow.cpp
gpu_timers.cpp [new file with mode: 0644]
gpu_timers.h [new file with mode: 0644]

index 02ba83358eac66296438c0c732cc59bfa108d744..d82637ea95608198760ec785622391c53ca0d6b8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,7 @@ mainwindow.o: ui_mainwindow.h
 
 futatabi: $(OBJS) $(CEF_LIBS)
        $(CXX) -o $@ $^ $(LDFLAGS) $(LDLIBS)
-flow: flow.o
+flow: flow.o gpu_timers.o
        $(CXX) -o $@ $^ -lepoxy -lSDL2 -lSDL2_image
 eval: eval.o util.o
        $(CXX) -o $@ $^
index 740b23672be7c1cee7db1eb3e88f63004a9c836c..bda8eecfc162ff18bd4db86210a79729036204a2 100644 (file)
--- a/flow.cpp
+++ b/flow.cpp
@@ -15,6 +15,7 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include "gpu_timers.h"
 #include "util.h"
 
 #include <algorithm>
@@ -971,90 +972,6 @@ void ResizeFlow::exec(GLuint flow_tex, GLuint out_tex, int input_width, int inpu
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 }
 
-class GPUTimers {
-public:
-       void print();
-       pair<GLuint, GLuint> begin_timer(const string &name, int level);
-
-private:
-       struct Timer {
-               string name;
-               int level;
-               pair<GLuint, GLuint> query;
-       };
-       vector<Timer> timers;
-};
-
-pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
-{
-       if (!enable_timing) {
-               return make_pair(0, 0);
-       }
-
-       GLuint queries[2];
-       glGenQueries(2, queries);
-       glQueryCounter(queries[0], GL_TIMESTAMP);
-
-       Timer timer;
-       timer.name = name;
-       timer.level = level;
-       timer.query.first = queries[0];
-       timer.query.second = queries[1];
-       timers.push_back(timer);
-       return timer.query;
-}
-
-void GPUTimers::print()
-{
-       for (const Timer &timer : timers) {
-               // NOTE: This makes the CPU wait for the GPU.
-               GLuint64 time_start, time_end;
-               glGetQueryObjectui64v(timer.query.first, GL_QUERY_RESULT, &time_start);
-               glGetQueryObjectui64v(timer.query.second, GL_QUERY_RESULT, &time_end);
-               //fprintf(stderr, "GPU time used = %.1f ms\n", time_elapsed / 1e6);
-               for (int i = 0; i < timer.level * 2; ++i) {
-                       fprintf(stderr, " ");
-               }
-               fprintf(stderr, "%-30s %4.1f ms\n", timer.name.c_str(), GLint64(time_end - time_start) / 1e6);
-       }
-}
-
-// A simple RAII class for timing until the end of the scope.
-class ScopedTimer {
-public:
-       ScopedTimer(const string &name, GPUTimers *timers)
-               : timers(timers), level(0)
-       {
-               query = timers->begin_timer(name, level);
-       }
-
-       ScopedTimer(const string &name, ScopedTimer *parent_timer)
-               : timers(parent_timer->timers),
-                 level(parent_timer->level + 1)
-       {
-               query = timers->begin_timer(name, level);
-       }
-
-       ~ScopedTimer()
-       {
-               end();
-       }
-
-       void end()
-       {
-               if (enable_timing && !ended) {
-                       glQueryCounter(query.second, GL_TIMESTAMP);
-                       ended = true;
-               }
-       }
-
-private:
-       GPUTimers *timers;
-       int level;
-       pair<GLuint, GLuint> query;
-       bool ended = false;
-};
-
 class TexturePool {
 public:
        GLuint get_texture(GLenum format, GLuint width, GLuint height);
diff --git a/gpu_timers.cpp b/gpu_timers.cpp
new file mode 100644 (file)
index 0000000..b58f1cd
--- /dev/null
@@ -0,0 +1,39 @@
+#include <epoxy/gl.h>
+
+#include "gpu_timers.h"
+
+using namespace std;
+
+pair<GLuint, GLuint> GPUTimers::begin_timer(const string &name, int level)
+{
+       if (!enable_timing) {
+               return make_pair(0, 0);
+       }
+
+       GLuint queries[2];
+       glGenQueries(2, queries);
+       glQueryCounter(queries[0], GL_TIMESTAMP);
+
+       Timer timer;
+       timer.name = name;
+       timer.level = level;
+       timer.query.first = queries[0];
+       timer.query.second = queries[1];
+       timers.push_back(timer);
+       return timer.query;
+}
+
+void GPUTimers::print()
+{
+       for (const Timer &timer : timers) {
+               // NOTE: This makes the CPU wait for the GPU.
+               GLuint64 time_start, time_end;
+               glGetQueryObjectui64v(timer.query.first, GL_QUERY_RESULT, &time_start);
+               glGetQueryObjectui64v(timer.query.second, GL_QUERY_RESULT, &time_end);
+               //fprintf(stderr, "GPU time used = %.1f ms\n", time_elapsed / 1e6);
+               for (int i = 0; i < timer.level * 2; ++i) {
+                       fprintf(stderr, " ");
+               }
+               fprintf(stderr, "%-30s %4.1f ms\n", timer.name.c_str(), GLint64(time_end - time_start) / 1e6);
+       }
+}
diff --git a/gpu_timers.h b/gpu_timers.h
new file mode 100644 (file)
index 0000000..34cd625
--- /dev/null
@@ -0,0 +1,62 @@
+#ifndef _GPU_TIMERS_H
+#define _GPU_TIMERS_H 1
+
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <epoxy/gl.h>
+
+extern bool enable_timing;
+
+class GPUTimers {
+public:
+       void print();
+       std::pair<GLuint, GLuint> begin_timer(const std::string &name, int level);
+
+private:
+       struct Timer {
+               std::string name;
+               int level;
+               std::pair<GLuint, GLuint> query;
+       };
+       std::vector<Timer> timers;
+};
+
+// A simple RAII class for timing until the end of the scope.
+class ScopedTimer {
+public:
+       ScopedTimer(const std::string &name, GPUTimers *timers)
+               : timers(timers), level(0)
+       {
+               query = timers->begin_timer(name, level);
+       }
+
+       ScopedTimer(const std::string &name, ScopedTimer *parent_timer)
+               : timers(parent_timer->timers),
+                 level(parent_timer->level + 1)
+       {
+               query = timers->begin_timer(name, level);
+       }
+
+       ~ScopedTimer()
+       {
+               end();
+       }
+
+       void end()
+       {
+               if (enable_timing && !ended) {
+                       glQueryCounter(query.second, GL_TIMESTAMP);
+                       ended = true;
+               }
+       }
+
+private:
+       GPUTimers *timers;
+       int level;
+       std::pair<GLuint, GLuint> query;
+       bool ended = false;
+};
+
+#endif  // !defined(_GPU_TIMERS_H)