Wrapping OpenGL Calls

Started by
11 comments, last by RobinsonUK 12 years, 1 month ago
I'm tired of my code being littered with verbose GL calls. I've been wondering if there's any way to wrap things up in small classes without a serious loss of performance and generality. For example, if I want to have a VBO class, every member function that does something with a VBO needs to make sure it is bound first. If I want to render it, I need to make sure all the client states (e.g. glEnableClientState(GL_VERTEX_ARRAY)) and pointers (e.g. glVertexPointer(...)) are set up correctly. The only robust way to do that is to set all relevant global state in every member function call, even when it's unnecessary. Wouldn't all this state-changing cause some serious performance issues?
Advertisement
Yes it's possible, it's called object orientation. You create classes with specific responsibilities. This will add a very tiny mini overhead but it's more than worth it. The trick is to pull your classes to an as high as possible level of abstraction without creating overly complex class responsibilities. For example, who sais you're using VBOs, maybe you've just got some vertex data that needs to be rendered however the implementation sees fit.

To counter the state changes you might end up with a state manager which only changes state if it is required.

Yes it's possible, it's called object orientation. You create classes with specific responsibilities. This will add a very tiny mini overhead but it's more than worth it. The trick is to pull your classes to an as high as possible level of abstraction without creating overly complex class responsibilities. For example, who sais you're using VBOs, maybe you've just got some vertex data that needs to be rendered however the implementation sees fit.

To counter the state changes you might end up with a state manager which only changes state if it is required.


I thought it was clear from my OP that object orientation is what I'm trying to achieve. I'm just worried about the performance penalties from redundant state changes.
I just want a thin and convenient layer on top of plain OpenGL to ease up the task of writing a higher-level graphics engine. I thought about making a state manager, but I'm not sure how to go about it. How would you go about wrapping "void glBindBuffer( GLenum target, GLuint buffer)", for example?
well, while wrapping the whole api is possible, in my opinion its rather pointless. You might want to write a state manager as suggested, but I think that you shouldn't wrap everything, but rather certain functionality (mesh loading, texture loading, shader loading etc.) which enables you to use OGL without making a single error, because the wrapper class takes care of it.

for ex. you could say that the current buffer stored would be:
struct buffer_state
{
GLuint buffer;
GLenum target;
} current_buffer_state;

and you could implement a function that checks if the current buffer bound is the one you pass to it, and in that case it wouldn't call the OGL function:
void my_bind_buffer(GLenum target, GLuint buffer)
{
if(current_buffer_state.buffer != buffer || current_buffer_state.target != target) //check the current state
{
if(glIsBuffer(buffer)) //check if it is really a buffer
{
glBindBuffer(target, buffer);
}
}
}

or something like this...
you might want to check out pixellight for a whole api wrap example.

when your app starts up you could just simply query the current gl state with glGet* to initialize your states

well, while wrapping the whole api is possible, in my opinion its rather pointless. You might want to write a state manager as suggested, but I think that you shouldn't wrap everything, but rather certain functionality (mesh loading, texture loading, shader loading etc.) which enables you to use OGL without making a single error, because the wrapper class takes care of it.

for ex. you could say that the current buffer stored would be:
struct buffer_state
{
GLuint buffer;
GLenum target;
} current_buffer_state;

and you could implement a function that checks if the current buffer bound is the one you pass to it, and in that case it wouldn't call the OGL function:
void my_bind_buffer(GLenum target, GLuint buffer)
{
if(current_buffer_state.buffer != buffer || current_buffer_state.target != target) //check the current state
{
if(glIsBuffer(buffer)) //check if it is really a buffer
{
glBindBuffer(target, buffer);
}
}
}

or something like this...
you might want to check out pixellight for a whole api wrap example.

when your app starts up you could just simply query the current gl state with glGet* to initialize your states


Basically, here's my problem:
I need to write my own versions of the OGL state-setter functions I care for that check for redundant state changes. Some of those functions take a parameter that specifies which state to change, like glBindBuffer. Ideally, if I write a glBindBuffer wrapper, it better act like the original, accept a target parameter and handle it correctly. How do I do that without manually writing special code for every type of target?
If you're going for a higher level graphics engine you should not think about how to wrap OpenGL. Instead you should think about how you'd like to make the calls to render something and then fill that in by using OpenGL. You might need a few iterations of adjusting requirements due to api specific limitations but you'll end up with a system that's easy to use, which is one of the most important things for anything high level.

To answer your question, my glBindBuffer calls are checked in the implementation of the Renderer class just by comparing ints like suggested by the poster above. I'm not doing this in the state manager because i've defined that to be managing render states like cullmode, blend mode, alpha testing etc etc. I gues you could do that in the state manager aswell, it doesnt really matter.

On a side note, maybe managing glBindBuffer calls isn't your biggest problem if you're worried about redundant state changes performance loss. Other state changes like shader and texture binds will probably be a lot more expensive and your buffer count will be very low when you've implemented batching.
oh woot simultaneous post. I dont think you want to go about wrapping single methods. At least take it higher and use a VBO. Then when making a render call to it, check if it's allready bound and rebind if needed.

If you're going for a higher level graphics engine you should not think about how to wrap OpenGL. Instead you should think about how you'd like to make the calls to render something and then fill that in by using OpenGL. You might need a few iterations of adjusting requirements due to api specific limitations but you'll end up with a system that's easy to use, which is one of the most important things for anything high level.

To answer your question, my glBindBuffer calls are checked in the implementation of the Renderer class just by comparing ints like suggested by the poster above. I'm not doing this in the state manager because i've defined that to be managing render states like cullmode, blend mode, alpha testing etc etc. I gues you could do that in the state manager aswell, it doesnt really matter.

On a side note, maybe managing glBindBuffer calls isn't your biggest problem if you're worried about redundant state changes performance loss. Other state changes like shader and texture binds will probably be a lot more expensive and your buffer count will be very low when you've implemented batching.


What's wrong with an intermediate layer to ease up the task of writing a higher-level engine? I have no intentions of writing a big general-purpose graphics engine. If I wanted one, there are many available. I write small apps with widely varying requirements that don't usually fit a standard engine architecture and I find myself writing over and over again heaps of OGL code to handle low-level tasks like setting up and rendering FBOs, VBOs etc.
only you can know how much abstraction you need or want. just make sure there is some. Wrapping glEnable(Gluint) into myEnable(unsigned int) or into myEnableDepthTest() / myDisableDepthTest() doesn't strike me as an effective way to make the program any more manageable.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

The way I did this was to create a set of classes to totally hide the gl calls and provide additional boiler plate functionality. Basically I've got the following classes:


[indent=1]ArrayBufferGL
[indent=1]IndexBufferGL
[indent=1]VertexBufferGL
[indent=1]GeometryShaderGL
[indent=1]VertexShaderGL
[indent=1]PixelShaderGL
[indent=1]ShaderProgramGL
[indent=1]ShaderVariablesGL
[indent=1]StateGL
[indent=1]FrameBufferGL
[indent=1]RenderBufferGL
[indent=1]SamplerGL
[indent=1]TextureMapGL
[indent=1]TextureArrayGL

After writing these classes, I then implemented a factory template to create instances of them:

template< class GraphicsDevice,
class VertexBuffer,
class IndexBuffer,
class ArrayBuffer,
class TextureMap,
class TextureArray,
class FrameBuffer,
class RenderBuffer,
class PixelShader,
class VertexShader,
class GeometryShader,
class ShaderProgram,
class ShaderVariables,
class Sampler,
class GraphicsState>
class GraphicsFacade
{
...
};


The facade just contains a set of Createxxxxx methods that return shared_ptr to one of the items. I can instantiate the template with D3D versions if I want to at compile time. As kunos says, simply wrapping things like glEnable(x) with your own method is a bit pointless. But hiding the gl- nature of the functionality in an OO way can be really useful.

This topic is closed to new replies.

Advertisement