How to check if a sampler is null in glsl?

You cannot do that. The sampler is an opaque handle which just references a texture unit. I'm not sure if the spec guarantees that (1,1,1,1) when sampling from a unit where no texture is bound, or if that is undefined behavior.

What you can do is just use another uniform to switch betwenn using the sampler or the uniform color, or just use different shaders and switch between those. There are also the possibilities of subprograms here, but I don't know if that would be the right appraoch for such a simple problem.


I stumbled over this question trying to solve a similar problem.

Since GLSL 4.30

int textureQueryLevels( gsamplerX sampler);

Is a build-in function. In the GLSL spec. p. 151 it says

The value zero will be returned if no texture or an incomplete texture is associated with sampler.

In the OpenGL-Forms I found an entry to this question suggesting to use

ivecY textureSize(gsamplerX sampler,int lod);

and testing if the texture size is greater than zero. But this is, to my understanding, not covered by the standard. In the section 11.1.3.4 of the OpenGL specification it is said that

If the computed texture image level is outside the range [levelbase,q], the results are undefined ...

Edit: I just tried this method on my problem and as it turns out nvidia has some issues with this function, resulting in a non zero value when no texture is bound. (See nvidia bug report from 2015)

Tags:

Opengl

Glsl