Skip to content

Instantly share code, notes, and snippets.

@Groovounet
Created November 25, 2013 10:57
Show Gist options
  • Save Groovounet/7639664 to your computer and use it in GitHub Desktop.
Save Groovounet/7639664 to your computer and use it in GitHub Desktop.
Ah twitter!
OpenGL is thread-safe which is actually pretty annoying and expective.
OpenGL traditionally uses the "Bind to Edit" model. An alternative is called "Direct State Access" (DSA).
Bind to Edit:
GLuint Texture(0);
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Direct State Access:
GLuint Texture(0);
glGenTextures(1, &Texture);
glTextureParameteriEXT(Texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteriEXT(Texture, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
DSA looks nicer but it's slower because OpenGL is thread safe. Each time an OpenGL texture object is accessed an OpenGL implementation need one mutex to access the table that map OpenGL names to OpenGL objects and one mutex to access the object state modified by the API call.
This is because: any thread can create or delete an OpenGL object and any thread can modify an OpenGL object.
But DSA remains a better design because there are three approaches to manage OpenGL states:
1/ States overwrite (We just set all the states necessarilly)
2/ States tracking (We keep a copy of every states)
3/ Assumption (We know how the state should at a logical point of the rendering execution.)
An OpenGL renderer uses a bit of all that but because every OpenGL call is quite expensive we prefer to avoid 1.
DSA is a better approach even if it's more expensive because it's design to provide some garantees to the OpenGL states so that we can use assumption a lot more. We affect the rendering states only when rendering, not when creating objects or do memory transfer. With bind to edit, we need to invalidate some states hence relying on 2 or 1 but in practice more 1 than 2 hence traver the code to reset the OpenGL state because we don't know anymore where we are.
So in my opinion glTextureParameteriEXT(Texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); or glTextureWrapS(Texture, GL_CLAMP_TO_EDGE), this is proportionally to the problem, quite decorative.
If I had to got to decorativeness, I'll rather have real enums starting by 0 for GL_TEXTURE_WRAP_S so that behind a simple table access could allow to access the object state and that would give the same performance that glTextureWrapS without an API number explosion.
@sherief
Copy link

sherief commented Nov 25, 2013

Using opaque pointers as names should have happened a long time ago. RIP Longs Peak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment