3 This file is part of gldbg
4 Copyright © 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
8 #include <msp/io/print.h>
9 #include <msp/time/units.h>
10 #include <msp/time/utils.h>
17 Profiler::Profiler(GlDbg &dbg):
18 decoder(gldecoder_new(this, 0)),
21 dbg.get_command_interpreter().register_command("profile", this, &Profiler::cmd_profile)
22 .set_help("Profiles GL usage and performance",
24 " Enables or disables profiling\n");
26 decoder->glDrawArrays = glDrawArrays;
27 decoder->glDrawElements = glDrawElements;
28 decoder->glDrawRangeElements = glDrawRangeElements;
29 decoder->glXSwapBuffers = glXSwapBuffers;
32 void Profiler::decode(const char *data, unsigned len)
35 gldecoder_decode(decoder, data, len);
38 void Profiler::cmd_profile(const string &args)
40 if(args.empty() || args=="on")
47 start = Msp::Time::now();
52 throw InvalidParameterValue("Invalid argument");
55 void Profiler::glDrawArrays(void *user_data, GLenum mode, int, int count)
57 glDrawElements(user_data, mode, count, 0, 0);
60 void Profiler::glDrawElements(void *user_data, GLenum mode, int count, GLenum, const void *)
62 Profiler *self = reinterpret_cast<Profiler *>(user_data);
65 self->vertices += count;
70 self->triangles += count/3;
72 case GL_TRIANGLE_STRIP:
75 self->triangles += count-2;
78 self->triangles += count/4;
82 self->triangles += count/2-1;
87 void Profiler::glDrawRangeElements(void *user_data, GLenum mode, unsigned, unsigned, int count, GLenum type, const void *data)
89 glDrawElements(user_data, mode, count, type, data);
92 void Profiler::glXSwapBuffers(void *user_data, Display *, GLXDrawable)
94 Profiler *self = reinterpret_cast<Profiler *>(user_data);
96 IO::print("%d draw calls, %d vertices, %d triangles\n", self->draw_calls, self->vertices, self->triangles);
102 Msp::Time::TimeStamp t = Msp::Time::now();
103 Msp::Time::TimeDelta dt = t-self->start;
104 if(dt>Msp::Time::sec)
106 Msp::IO::print("%d frames in %s seconds\n", self->frames, dt);