I have a scene. Sometimes I draw untextured, coloured, semi-transparent triangles, sometimes I texture them, and I draw a lot of both in a fairly interleaved manner.

In a fixed function pipeline the difference would be if I had set the GL_TEXTURE_2D to be 0 or a texture handle. My codebase is being migrated from FFP to OpenGLES 2, so these bindings to 0 already exist everywhere they are needed. My ambition is for my generic OpenGL code to run on both OpenGL 1.x and OpenGLES 2. So I'm looking for shaders that are a cut-down FFP imitation.

How can an OpenGLES 2 shader know if the Sampler2D is set to 0 or has a valid texture?

My reasoning is that an if-statement that all fragments take the same branch is much cheaper than switching program pretty much every other statement as I alternate between textured ops and fixed-colour ops.

If I add a uniform then I have to go visit my code and each time I bind a texture I'll have to add a call to set the uniform flag. I have a large codebase that I have to ensure I am strictly setting it appropriately.

If I introduce a opaque white 1x1 texture this is like the uniform - I have to go everywhere in the code base ensuring I am binding it instead of binding to 0 to get colour-only primatives.

So I'd much prefer an approach where the sampler is interrogated in a shader. Is this possible?

  • \$\begingroup\$ Why would it be such a bad thing to set a uniform flag from the main application? The approach you're proposing (if it's possible at all) would add a couple extra instructions to every execution of the shader, possibly impacting performance, so I'd think setting the uniform would be the preferable approach. \$\endgroup\$ Commented Dec 11, 2011 at 0:19
  • \$\begingroup\$ @NathanReed I have tried to clarify what I see as the whys \$\endgroup\$
    – Will
    Commented Dec 11, 2011 at 11:48

1 Answer 1


Testing for the sampler in the fragment shader is going to make either a useless test or a useless texture lookup for each fragment being rendered. There is really no reason to do that.

A really nicer method is to bind a program using a specific fragment shader when you want to render flat triangles:

precision highp float;
uniform vec4 u_FixedColor;
void main()
    gl_FragColor = u_FixedColor;
  • \$\begingroup\$ Is it really so cheap to swap program a few hundred times per frame? \$\endgroup\$
    – Will
    Commented Dec 11, 2011 at 11:49
  • 2
    \$\begingroup\$ I believe it is about as cheap as changing a texture sampler a few hundred times per frame. Which means that if it starts becoming a problem, you should group your primitives so that they can be sent to the GPU in as few parameter changes as possible. \$\endgroup\$ Commented Dec 11, 2011 at 12:12
  • \$\begingroup\$ Actually, you'll most likely get BOTH useless texture lookup AND a useless test, the way shaders work =) \$\endgroup\$ Commented Dec 12, 2011 at 17:31
  • \$\begingroup\$ @JariKomppa such a cryptic comment needs an explanation! Please elaborate \$\endgroup\$
    – Will
    Commented Dec 14, 2011 at 7:22
  • \$\begingroup\$ Shaders may be executed by tons of cores in lock-step, so that all the cores run all the instructions, and simply ignore the results from any "wrong" branches. AFAIR wavefronts in cuda are related to this, but I'm writing this on iPhone so I can't give you any references. @Will \$\endgroup\$ Commented Dec 14, 2011 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .