Uniform Buffer Object Sharing

I’d like to share a UBO between multiple programs. But, what happens if two programs use the same block name but the contents of the uniform block are slightly different? Is there a way to detect that uniform block are truly the same across multiple programs?


Sharing contexts only works in the same process.

See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd374390(v=vs.85).aspx
Quote: “You can only share display lists with rendering contexts within the same process.”

If you use shared contexts the object IDs will be unique. For example two threads each with his own GL context (but shared)

void myThread1WithContext1()
glGenTextures(1, myTextureId1)

void myThread2WithContext2()
glGenTextures(1, myTextureId2)

Will take care for you that myTextureId1 and myTextureId2 will each have a unique ID over the shared contexts.
But you must use fances or glFinish after one thread worked with an GL Object before signaling the other thread that he can touch that GL Object!

The “shared” and “std140” layout qualifiers should defeat any compiler optimizations that would eliminate unused block members. But you do need to declare the blocks identically in all the programs you intend to share blocks. (You can put the block declaration in its own string, remember that program compilation accepts multiple strings.)

After linking, you should introspect the block layouts to convince yourself; see glGetActiveUniformsiv, with UNIFORM_OFFSET, UNIFORM_ARRAY_STRIDE, etc.