OpenGL Application Design

Does anyone know of a good resource for designing an application around OpenGL? The Red and Orange books don’t deal with that, really, just with making use of the API’s. But as Frederick Brooks has famously said, “Show me your flow charts and I shall remain mystified, but show me your tables and I will be enlightened”.

For Krita 2.0, we would like to take our use of OpenGL even further. Not just for rendering and zooming the canvas, but we want to make it possible to write filters and brushes in shaders, maybe even layer composition. Cyrille is laying the first stone by making it possible to replace colorspace functions in the Pigment library like convolveColors at runtime with more powerful equivalents, like MMX or AltiVec routines — or OpenGL routines. And applications like Tom van Laerhoven’s Waterverve (unfortunately not downloadable, the link goes to his Phd thesis) work entirely as Cg shaders.

But how do we best design our data backend for optimal OpenGL usage?

Like most applications, Krita uses tiles, that is memory areas of 64 by 64 pixels. I am thinking of something like this:

    • Store as many of the most-recently used tiles as possible as textures in the graphics card, if OpenGL is enabled
  • Store a certain number of the second-most recently used tiles uncompressed in memory.
  • Compress the tiles that haven’t been accessed for a long time (based on Ariya‘s code, we’ve made a start with this step)
  • If memory gets tight, swap out the compressed tiles

Now there’s a problem: because we really, really want to make life easy for plugin developers (and their life is very easy!), we don’t expose the tiles in our API. That means that colorspaces get a pointer to a chunk of chars in memory, that tools, paintops and filters always work with iterators and so on.

But how do our optimized OpenGL shader programs get access to the pixel data? It would be a bit of a waste to copy the relevant data into a texture before letting OpenGL work on it, so the OpenGL routines must access the textures that are already in memory directly. And at this point I’m stumped.

Hence the query: is there a good book about these issues? Or does the communis opinio hold that it is impossible to write a single application that works well without OpenGL but better with?