Browse Source

Added GPU timer and fixed up formatting.

shared-context
Doug Binks 11 years ago
parent
commit
bb6b19c410
2 changed files with 58 additions and 25 deletions
  1. +20
    -22
      example/demo.c
  2. +38
    -3
      example/example_gl3.c

+ 20
- 22
example/demo.c View File

@@ -911,28 +911,26 @@ void renderFPS(struct NVGcontext* vg, float x, float y, struct FPScounter* fps,
nvgFill(vg); nvgFill(vg);


nvgFontFace(vg, "sans"); nvgFontFace(vg, "sans");
if( RENDER_FPS == style )
{
nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f FPS", 1.0f / avg);
nvgText(vg, x+w-5,y+h/2, str, NULL);

nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,160));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+5,y+h/2, str, NULL);
}
else
{
nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+w-5,y+h/2, str, NULL);
}
if( RENDER_FPS == style ) {
nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f FPS", 1.0f / avg);
nvgText(vg, x+w-5,y+h/2, str, NULL);
nvgFontSize(vg, 15.0f);
nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,160));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+5,y+h/2, str, NULL);
}
else {
nvgFontSize(vg, 18.0f);
nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
nvgFillColor(vg, nvgRGBA(240,240,240,255));
sprintf(str, "%.2f ms", avg * 1000.0f);
nvgText(vg, x+w-5,y+h/2, str, NULL);
}




} }


+ 38
- 3
example/example_gl3.c View File

@@ -31,7 +31,6 @@
#include "nanovg_gl3buf.h" #include "nanovg_gl3buf.h"
#include "demo.h" #include "demo.h"



void errorcb(int error, const char* desc) void errorcb(int error, const char* desc)
{ {
printf("GLFW error %d: %s\n", error, desc); printf("GLFW error %d: %s\n", error, desc);
@@ -49,14 +48,19 @@ static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
blowup = !blowup; blowup = !blowup;
} }


enum numqueries {
NUM_QUERIES = 5
};

int main() int main()
{ {
GLFWwindow* window; GLFWwindow* window;
struct DemoData data; struct DemoData data;
struct NVGcontext* vg = NULL; struct NVGcontext* vg = NULL;
struct FPScounter fps;
struct FPScounter cpuTimes;
struct FPScounter fps, cpuTimes, gpuTimes;
double prevt = 0, cpuTime = 0; double prevt = 0, cpuTime = 0;
int timerquery = GL_FALSE, currquery = 0, retquery = 0;
GLuint timerqueryid[NUM_QUERIES];


if (!glfwInit()) { if (!glfwInit()) {
printf("Failed to init GLFW."); printf("Failed to init GLFW.");
@@ -65,6 +69,7 @@ int main()


initFPS(&fps); initFPS(&fps);
initFPS(&cpuTimes); initFPS(&cpuTimes);
initFPS(&gpuTimes);


glfwSetErrorCallback(errorcb); glfwSetErrorCallback(errorcb);
#ifndef _WIN32 // don't require this on win32, and works with more cards #ifndef _WIN32 // don't require this on win32, and works with more cards
@@ -110,6 +115,11 @@ int main()


glfwSwapInterval(0); glfwSwapInterval(0);


timerquery = glfwExtensionSupported("GL_ARB_timer_query");
if( timerquery ) {
glGenQueries(NUM_QUERIES, timerqueryid);
}

glfwSetTime(0); glfwSetTime(0);
prevt = glfwGetTime(); prevt = glfwGetTime();


@@ -125,6 +135,10 @@ int main()
prevt = t; prevt = t;
updateFPS(&fps, dt); updateFPS(&fps, dt);
updateFPS(&cpuTimes, cpuTime); updateFPS(&cpuTimes, cpuTime);
if( timerquery ) {
glBeginQuery(GL_TIME_ELAPSED, timerqueryid[currquery % NUM_QUERIES] );
currquery = ++currquery;
}


glfwGetCursorPos(window, &mx, &my); glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetWindowSize(window, &winWidth, &winHeight);
@@ -147,11 +161,32 @@ int main()
renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
renderFPS(vg, 5,5, &fps, RENDER_FPS); renderFPS(vg, 5,5, &fps, RENDER_FPS);
renderFPS(vg, 310,5, &cpuTimes, RENDER_MS); renderFPS(vg, 310,5, &cpuTimes, RENDER_MS);
renderFPS(vg, 615,5, &gpuTimes, RENDER_MS);


nvgEndFrame(vg); nvgEndFrame(vg);


glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);

// Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
cpuTime = glfwGetTime() - t; cpuTime = glfwGetTime() - t;

if( timerquery ) {
GLint available = 1;
glEndQuery(GL_TIME_ELAPSED);
while( available && retquery <= currquery ) {
// check for results if there are any
glGetQueryObjectiv(timerqueryid[retquery], GL_QUERY_RESULT_AVAILABLE, &available);
if( available ) {
GLuint64 timeElapsed = 0;
double gpuTime;
glGetQueryObjectui64v(timerqueryid[retquery % NUM_QUERIES], GL_QUERY_RESULT, &timeElapsed);
retquery = ++retquery ;
gpuTime = (double)timeElapsed * 1e-9;
updateFPS(&gpuTimes, (float)gpuTime);
}
}

}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }


Loading…
Cancel
Save