Uniforms on a shader program used in multiple threads

This isn’t really a question but rather something we’ve recently discovered about uniforms while making Qt’s graphics sub-system threadable.

In Qt we use lots of shader programs and try to share them between contexts. So we have a library of compiled shaders which may be used by different but sharing contexts. E.g. Multiple top-level windows each have a separate context, but share a common pool of shader programs. This has never been a problem before because although an application can have multiple top-level windows, they are only rendered into one-at-a-time from a single thread. However, we have recently been trying to make Qt more threadable to enable E.g. splitting separate top-levels into their own thread.

Several features of OpenGL combine to give behavior which, while logical as described in the spec, may not be exactly what one would want in this admittedly quite unusual use case:

  • Two OpenGL contexts can be current at the same time in two separate threads
  • Two OpenGL contexts can be sharing some resources
  • Shader programs are a resource which can be shared across contexts
  • A shader program can therefore be use()'d in two contexts simultaneously
  • Uniforms are part of a shader program’s state (not the context’s state)
  • When one thread sets a uniform, it sets it for all threads

It’s this last point which created an “huh?” moment in our office.

The issue is fairly easy to work around. I only post as I suspect it might be a use case which others might find interesting.

Cheers,

Tom

You should be rendering everything in one OpenGL context.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.