I came across this question, trying to solve a similar problem.
Since GLSL 4.30
int textureQueryLevels( gsamplerX sampler);
It is a built-in function. In the GLSL specification. n. 151 he says
A value of zero will be returned if the texture or incomplete texture is not associated with the sampler.
In OpenGL-Forms, I found a post on this question suggesting using
ivecY textureSize(gsamplerX sampler,int lod);
and testing if the texture size is greater than zero. But this, in my opinion, is not covered by the standard. Section 11.1.3.4 of the OpenGL specification says that
If the level of the calculated texture 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 problems with this function, which leads to a non-zero value when no texture is connected. (See nvidia bug report from 2015)
source share