Uniform blocks

(win7 x64)

I’m trying to use uniform blocks to share data between shader programs, but am confused about some aspects of them.

I have 2 shader programs, with for example the vertex shaders as:

Vertex shader for program 1


layout(std140) uniform Block1
{
	vec4 var1;
	vec4 var2;
}

etc.

Vertex shader for program 2


layout(std140) uniform Block1
{
	// different variable types and/or names
	float var1;
	vec4 var3;
}

etc.

I actually expected this to not compile, because the uniform block definitions do not match up.
Why does it compile?
And what’s the best way to distinguish between same-named-but-different uniform blocks?

I actually expected this to not compile, because the uniform block definitions do not match up.
Why does it compile?

Because program 1 doesn’t know anything about program 2. The prohibition against blocks with the same name but different block definitions is within a program, not global. There’s no global registry of blocks or something. Indeed, nothing about program 1 can interfere with the compilation/linking of program 2.

And what’s the best way to distinguish between same-named-but-different uniform blocks?

That’s up to you. OpenGL doesn’t care if blocks in two different programs have anything in common. OpenGL only cares if two blocks in the same program have the same name but different definitions. It’s perfectly valid to have two separate programs use blocks with the same name that are different. What matters is what buffer objects you bind for them and what those buffers contain.

And that’s on you to deal with.