2013-09-27 14:54:01 +00:00
|
|
|
#include "perftimer.h"
|
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_timer.h>
|
|
|
|
#include <glew.h>
|
2013-09-27 14:54:01 +00:00
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "debugwriter.h"
|
2013-09-27 14:54:01 +00:00
|
|
|
|
|
|
|
struct TimerQuery
|
|
|
|
{
|
|
|
|
GLuint query;
|
|
|
|
static bool queryActive;
|
|
|
|
bool thisQueryActive;
|
|
|
|
|
|
|
|
TimerQuery()
|
|
|
|
: thisQueryActive(false)
|
|
|
|
{
|
|
|
|
glGenQueries(1, &query);
|
|
|
|
}
|
|
|
|
|
|
|
|
void begin()
|
|
|
|
{
|
|
|
|
if (queryActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (thisQueryActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glBeginQuery(GL_TIME_ELAPSED, query);
|
|
|
|
queryActive = true;
|
|
|
|
thisQueryActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void end()
|
|
|
|
{
|
|
|
|
if (!thisQueryActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glEndQuery(GL_TIME_ELAPSED);
|
|
|
|
queryActive = false;
|
|
|
|
thisQueryActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getResult(GLuint64 *result)
|
|
|
|
{
|
|
|
|
if (thisQueryActive)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GLint isReady = GL_FALSE;
|
|
|
|
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady);
|
|
|
|
|
|
|
|
if (isReady != GL_TRUE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetQueryObjectui64v(query, GL_QUERY_RESULT, result);
|
|
|
|
|
|
|
|
if (glGetError() == GL_INVALID_OPERATION)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint64 getResultSync()
|
|
|
|
{
|
|
|
|
if (thisQueryActive)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
GLuint64 result;
|
|
|
|
GLint isReady = GL_FALSE;
|
|
|
|
|
|
|
|
while (isReady == GL_FALSE)
|
|
|
|
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady);
|
|
|
|
|
|
|
|
glGetQueryObjectui64v(query, GL_QUERY_RESULT, &result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TimerQuery()
|
|
|
|
{
|
|
|
|
if (thisQueryActive)
|
|
|
|
end();
|
|
|
|
|
|
|
|
glDeleteQueries(1, &query);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool TimerQuery::queryActive = false;
|
|
|
|
|
2013-11-30 10:47:03 +00:00
|
|
|
#define GPU_QUERIES 2
|
|
|
|
|
2013-09-27 14:54:01 +00:00
|
|
|
struct GPUTimerGLQuery : public PerfTimer
|
|
|
|
{
|
2013-11-30 10:47:03 +00:00
|
|
|
TimerQuery queries[GPU_QUERIES];
|
2013-09-27 14:54:01 +00:00
|
|
|
const int iter;
|
|
|
|
|
|
|
|
uint8_t ind;
|
|
|
|
uint64_t acc;
|
|
|
|
int32_t counter;
|
|
|
|
bool first;
|
|
|
|
|
|
|
|
GPUTimerGLQuery(int iter)
|
|
|
|
: iter(iter),
|
|
|
|
ind(0),
|
|
|
|
acc(0),
|
|
|
|
counter(0),
|
|
|
|
first(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void startTiming()
|
|
|
|
{
|
|
|
|
queries[ind].begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void endTiming()
|
|
|
|
{
|
|
|
|
queries[ind].end();
|
|
|
|
|
2013-10-01 10:05:58 +00:00
|
|
|
swapInd();
|
|
|
|
|
2013-09-27 14:54:01 +00:00
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint64 result;
|
|
|
|
if (!queries[ind].getResult(&result))
|
|
|
|
return;
|
|
|
|
|
|
|
|
acc += result;
|
|
|
|
|
|
|
|
if (++counter < iter)
|
|
|
|
return;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << " "
|
2013-09-27 14:54:01 +00:00
|
|
|
"Avg. GPU time:" << ((double) acc / (iter * 1000 * 1000)) << "ms";
|
|
|
|
|
|
|
|
acc = counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swapInd()
|
|
|
|
{
|
2013-11-30 10:47:03 +00:00
|
|
|
if (++ind > GPU_QUERIES)
|
|
|
|
ind = 0;
|
2013-09-27 14:54:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GPUTimerDummy : public PerfTimer
|
|
|
|
{
|
|
|
|
void startTiming() {}
|
|
|
|
void endTiming() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CPUTimer : public PerfTimer
|
|
|
|
{
|
|
|
|
const int iter;
|
|
|
|
|
|
|
|
uint64_t acc;
|
|
|
|
int32_t counter;
|
|
|
|
Uint64 ticks;
|
|
|
|
Uint64 perfFreq;
|
|
|
|
|
|
|
|
CPUTimer(int iter)
|
|
|
|
: iter(iter),
|
|
|
|
acc(0),
|
|
|
|
counter(0),
|
|
|
|
ticks(0)
|
|
|
|
{
|
|
|
|
perfFreq = SDL_GetPerformanceFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void startTiming()
|
|
|
|
{
|
|
|
|
ticks = SDL_GetPerformanceCounter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void endTiming()
|
|
|
|
{
|
|
|
|
acc += SDL_GetPerformanceCounter() - ticks;
|
|
|
|
|
|
|
|
if (++counter < iter)
|
|
|
|
return;
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << "Avg. CPU time:" << ((double) acc / (iter * (perfFreq / 1000))) << "ms";
|
2013-09-27 14:54:01 +00:00
|
|
|
acc = counter = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PerfTimer *createCPUTimer(int iter)
|
|
|
|
{
|
|
|
|
return new CPUTimer(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
PerfTimer *createGPUTimer(int iter)
|
|
|
|
{
|
2013-12-31 21:31:03 +00:00
|
|
|
if (GLEW_ARB_timer_query || GLEW_EXT_timer_query)
|
2013-09-27 14:54:01 +00:00
|
|
|
{
|
|
|
|
return new GPUTimerGLQuery(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << "GL_EXT_timer_query not present: cannot measure GPU performance";
|
2013-09-27 14:54:01 +00:00
|
|
|
return new GPUTimerDummy();
|
|
|
|
}
|
|
|
|
}
|