From d60474a5a6da369c37bd1a218e347a66f9995fa1 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 2 Aug 2018 17:14:28 +0200 Subject: [PATCH] Move GPUTimers into its own file. --- Makefile | 2 +- flow.cpp | 85 +------------------------------------------------- gpu_timers.cpp | 39 +++++++++++++++++++++++ gpu_timers.h | 62 ++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 85 deletions(-) create mode 100644 gpu_timers.cpp create mode 100644 gpu_timers.h diff --git a/Makefile b/Makefile index 02ba833..d82637e 100644 --- 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 $@ $^ diff --git a/flow.cpp b/flow.cpp index 740b236..bda8eec 100644 --- a/flow.cpp +++ b/flow.cpp @@ -15,6 +15,7 @@ #include #include +#include "gpu_timers.h" #include "util.h" #include @@ -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 begin_timer(const string &name, int level); - -private: - struct Timer { - string name; - int level; - pair query; - }; - vector timers; -}; - -pair 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 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 index 0000000..b58f1cd --- /dev/null +++ b/gpu_timers.cpp @@ -0,0 +1,39 @@ +#include + +#include "gpu_timers.h" + +using namespace std; + +pair 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 index 0000000..34cd625 --- /dev/null +++ b/gpu_timers.h @@ -0,0 +1,62 @@ +#ifndef _GPU_TIMERS_H +#define _GPU_TIMERS_H 1 + +#include +#include +#include + +#include + +extern bool enable_timing; + +class GPUTimers { +public: + void print(); + std::pair begin_timer(const std::string &name, int level); + +private: + struct Timer { + std::string name; + int level; + std::pair query; + }; + std::vector 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 query; + bool ended = false; +}; + +#endif // !defined(_GPU_TIMERS_H) -- 2.39.2