PDA

View Full Version : Writing Unit Tests using OpenGL



KTurkowski
05-04-2011, 06:18 PM
I am writing some code that is built on top of OpenGL to do rendering. I would like to write some unit tests (using google test preferably) to exercise and verify that code. As such, it does not require a UI. It will merely render some images, read them back, and check them for correctness, e.g. by comparing with golden images. Perhaps the PBuffers or Frame Buffer Objects are better than opening a window, but it doesn't really matter to me as long as I can determine when rendering is done and get the pixels. I also want to do timing measurements.

Can someone provide me with some code that can initialize GL for rendering offscreen? Am I correct to assume that rendering to an offscreen buffer takes the same amount of time as rendering to a window?

TestState *st =InitGLForMe(width, height, pixelFormat, pixelType);
StartTimer(st);
...
glDrawArrays(...);
...
StopTimerAfterDrawingIsComplete(st);
GetResultantImage(st, pixels);
ShutDownGL(st);

Initially, I am developing this for the Macintosh with OpenGL 1 or 2, but would also like to run on other platforms, such as Android with OpenGL ES.

Can you provide me with these functions (in green above)? Or possible refer me?
- InitGLForMe
- StopTimerAfterDrawingIsComplete
- GetResultantImage
- ShutDownGL

Thanks,
Ken

Kelvin
05-04-2011, 11:22 PM
You can call glFinish() to force rendering completion. All previously issued rendering commands are guaranteed to be complete (pixels committed to the framebuffer) when glFinish() returns.

Then you can stop your timer.

Then use glReadPixels() to get the image.

This may not be the most accurate timing method -- you may be able to take advantage of vendor-specific performance extensions to measure actual GPU execution time.

I wouldn't know where to start with setting up or tearing down GL on the Mac though.