How to check if a sampler is null in glsl?

I have a shader with a uniform shape and a sampler. Now I want to paint with _color ONLY if the sampler has not been installed. Is there a way to understand that we are inside a shader? (Unfortunately, the sampler returns 1,1,1,1, if not assigned, which makes it impossible to mix it through alpha)

+6
source share
2 answers

You cannot do this. A sampler is an opaque handle that simply refers to a texture block. I am not sure if the specification guarantees that (1,1,1,1) when fetching from a unit where there is no texture, or if this behavior is undefined.

What you can do is simply use a different shape to switch between a sampler or a uniform color, or just use different shaders and switch between them. Subroutine features are also here, but I don't know if that would be right for such a simple problem.

+7
source

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)

+1
source

Source: https://habr.com/ru/post/972502/


All Articles