3
votes

I want to render a scene to texture, and share the texture sampler in several programs. Similar to share the project view matrix in multiple programs. Unlike project view matrix, which can be put into uniform blocks, "Samplers cannot be part of uniform blocks". https://www.opengl.org/wiki/Uniform_(GLSL)

Some discussion here describes why:

multiple issues:
GLSL samplers embody two things: source of texture data and how to filter from it (nearest, linear, mipmapping, anisotropic, etc)
Samples are opaque things, and to place them into a uniform buffer object requires they have a well defined (and cross-platform) size.
Those issues together make it dicey.

I want to see some explanation of why this cannot be handled and any other ways to achieve sharing texture samplers.

1
You can always use bindless textures to do what you seem to want, if your implementation supports them.Andon M. Coleman
Thanks @AndonM.Coleman. What I want to do is to create a uniform in another program by passing this texture ID. Since the texture is under "this scope", I don't know if it will work or if this consider as something similar to "call by reference".draupnie
Yeah, you have to understand that samplers are not texture IDs. They are opaque handles (which is a fancy way of saying that they refer to a Texture Image Unit). When you set a sampler uniform's value, what that does is tell GL to use the texture that is currently bound to the sampler associated with Texture Image Unit X. The thing is, as an opaque type, they don't have any actual traditional storage so they cannot be put into things like structures or uniform/shader storage buffer objects.Andon M. Coleman

1 Answers

4
votes

The explanation is right here:

Samples are opaque things, and to place them into a uniform buffer object requires they have a well defined (and cross-platform) size.

The purpose of uniform blocks is, that you can set then with a single OpenGL call from a single structure in your (client) program. But for this to work the memory layout of this structure must be known, so that the memory layout produced by the compiler matches that of the shader uniform block.

But since the memory layout of samplers is not defined a memory layout can not be determined. Without a definitive memory layout no structure, without a structure no block.