Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of an OpenGL texture unit?

Obviously the scope of an OpenGL texture unit is no wider than a single process — on the same system, multiple processes can each glActiveTexture​(GL_TEXTURE0); glBindTexture(...);, and things work just fine — textures don't get mixed up between processes.

But, among shared OpenGL contexts within a single process, are texture units independent or shared?

In other words, can I expect this to work?

// Create a base context and bind one texture to GL_TEXTURE0
CGLContextObj baseContext = createGLContext(NULL);
{
    CGLContextObj cgl_ctx = baseContext;
    glActiveTexture​(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 1);
    // ...render continuously...
}

// On a separate thread, create a context shared with baseContext and bind a _different_ texture to GL_TEXTURE0
{
    CGLContextObj cgl_ctx = createGLContext(baseContext);
    glActiveTexture​(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 2);
    // ...render continuously...
}

...or, on the second context, should I be using GL_TEXTURE1?

Is this documented anywhere? (I've scoured the opengl.org man pages and wiki, but didn't find anything explaining this scenario. For example, http://www.opengl.org/wiki/OpenGL_Context says that texture, buffer, and program objects are shared, but says nothing about texture units.)

like image 387
smokris Avatar asked Feb 15 '23 16:02

smokris


1 Answers

Effectively context resource sharing is limited to objects that actually define a data store, and is disabled by default in most window system implementations.

Things that are shared:

  • Vertex Buffer Objects (Buffer Objects in general)
  • Texture objects
  • GLSL Program objects

Things that are not shared include:

  • The general state machine
  • Container objects like Vertex Array Objects and Framebuffer Objects
    • Yes, despite the name, FBOs are not Buffer Objects!

Each context maintains its own state machine, the only things you share in this example are the texture objects themselves. That is to say, you can use the same texture name (GLuint ID) to reference the texture between contexts, but texture unit state is completely unique to each context.

Texture bindings are not shared between contexts.

If you issue a draw command from one context where texture A is bound to unit 0 then the shader will use texture A anytime it samples from texture unit 0. If you issue the same draw command from a context where texture B is bound to unit 0, then the shader will sample texture B.

like image 121
Andon M. Coleman Avatar answered Feb 27 '23 07:02

Andon M. Coleman