Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glFinish() vs glFenceSync() + glClientWaitSync()

Tags:

c++

opengl

Is there a difference between running

glFinish()

and running

glFenceSync(...)
glClientWaitSync(...)

with a big timeout?

What I'm trying to do: I run a pipeline of OpenGL commands, and I want to time how long each command takes. Without any of the above commands, everything will be pipelined / buffered, and it looks as thought the last command takes all processing time.

timer start

Run Opengl part 1

sync / glFinish
timer measure

Run Opengl part 2

sync / glFinish
timer measure

...

So I'm trying to figure out how to best measure the "speed" of the individual parts without influencing too much the overall runtime.

like image 728
Jan Rüegg Avatar asked Mar 13 '23 21:03

Jan Rüegg


1 Answers

All of the options you mentioned will influence the performance of the application, since they will stall the pipeline. The modern way of measuring timing in OpenGL is to use Timer Queries: You tell OpenGL that it should save a timestamp when the query is executed on the GPU, thus no synchronization between GPU and CPU is required. The code could look, for example, like this:

GLuint64 startTime, stopTime;
unsigned int queryID[2];

// generate two queries
glGenQueries(2, queryID);
...
// issue the first query
// Records the time only after all previous 
// commands have been completed
glQueryCounter(queryID[0], GL_TIMESTAMP);

// call a sequence of OpenGL commands
...
// issue the second query
// records the time when the sequence of OpenGL 
// commands has been fully executed
glQueryCounter(queryID[1], GL_TIMESTAMP);
...

// get query results
// (automatically blocks until results are available)
glGetQueryObjectui64v(queryID[0], GL_QUERY_RESULT, &startTime);
glGetQueryObjectui64v(queryID[1], GL_QUERY_RESULT, &stopTime);

printf("Time spent on the GPU: %f ms\n", (stopTime - startTime) / 1000000.0);

(Code taken from Lighthouse3d.com).

Another option is to use glBeginQuery with the GL_TIME_ELAPSED parameter, which is also described in the linked article.

like image 110
BDL Avatar answered Mar 27 '23 10:03

BDL