Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a sampler is null in glsl?

Tags:

opengl

glsl

I have a shader with a _color uniform and a sampler. Now I want to draw with _color ONLY if the sampler was not set. Is there any way to figure that our within the shader? (Unfortunately the sampler returns 1,1,1,1 when not assigned, which makes mixing it via alpha impossible)

like image 906
pixartist Avatar asked Jul 19 '14 14:07

pixartist


People also ask

What is a sampler GLSL?

A sampler is a set of GLSL variable types. Variables of one of the sampler types must be uniforms or as function parameters. Each sampler in a program represents a single texture of a particular texture type. The type of the sampler corresponds to the type of the texture that can be used by that sampler.

What is vec3 in GLSL?

vec3 is a floating point vector with three components. It can be initialized by: Providing a scalar value for each component. Providing one scalar value. This value is used for all components.

What is texture2D GLSL?

The texture2D function returns a texel, i.e. the (color) value of the texture for the given coordinates. The function has one input parameter of the type sampler2D and one input parameter of the type vec2 : sampler, the uniform the texture is bound to, and coord, the 2-dimensional coordinates of the texel to look up.

What is gl_Position in GLSL?

gl_Position is a special variable that holds the position of the vertex in clip space. Since a vertex shader's main output is the position in clip space, it must always set gl_Position. This vertex shader just transforms each vertex position (by the VP matrix).


2 Answers

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.

like image 134
derhass Avatar answered Oct 10 '22 07:10

derhass


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)

like image 25
22-42-7 Avatar answered Oct 10 '22 06:10

22-42-7