Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if GL_TEXTURE_2D is active in shader

Tags:

opengl

glsl

I would like to know if GL_TEXTURE_2D is active in the shader.

I am binding a color to the shader as well as the active texture (if GL_TEXTURE_2D is set) and need to combine these two.

So if texture is bound, mix the color and the texture (sampler2D * color) and if no texture is bound, use color.

Or should I go another way about this?

like image 552
RobotRock Avatar asked Feb 20 '13 11:02

RobotRock


People also ask

How many times does fragment shader run?

Unless you have early depth testing enabled, a fragment is only discarded after you run the fragment shader. In this case, the frag shader would run once for every fragment in both quads, so 20000 times.

How does a shader work OpenGL?

A Shader is a user-defined program designed to run on some stage of a graphics processor. Shaders provide the code for certain programmable stages of the rendering pipeline. They can also be used in a slightly more limited form for general, on-GPU computation.

Is Glsl a shader?

Shaders use GLSL (OpenGL Shading Language), a special OpenGL Shading Language with syntax similar to C. GLSL is executed directly by the graphics pipeline. There are several kinds of shaders, but two are commonly used to create graphics on the web: Vertex Shaders and Fragment (Pixel) Shaders.


1 Answers

It is not quite clear what you mean by 'GL_TEXTURE_2D is active' or 'GL_TEXTURE_2D is set'.

Please note the following:

  • glEnable(GL_TEXTURE_2D) has no effect on your (fragment) shader. It parametrizes the fixed function part of your pipeline that you just replaced by using a fragment shader.
  • There is no 'direct'/'clean' way of telling from inside the GLSL shader whether there is a valid texture bound to the texture unit associated with your texture sampler (to my knowledge).
  • Starting with GLSL 1.3 you might have luck using textureSize(sampler, 0).x > 0 to detect the presence of a valid texture associated with sampler, but that might result in undefined behavior.
  • The ARB_texture_query_levels extension does indeed explicitly state that textureQueryLevels(gsampler2D sampler) returns 0 if there is no texture associated with sampler.

Should you go another way about this? I think so: Instead of making a decision inside the shader, simply bind a 1x1 pixel texture of 'white' and unconditionally sample that texture and multiply the result with color, which will obviously return 1.0 * color. That is going to be more portable and faster, too.

like image 121
axxel Avatar answered Sep 21 '22 18:09

axxel