Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Nathan Reed Dec 11 '11 at 0:19
    
@NathanReed I have tried to clarify what I see as the *why*s –  Will Dec 11 '11 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;
}
share|improve this answer
    
Is it really so cheap to swap program a few hundred times per frame? –  Will Dec 11 '11 at 11:49
1  
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. –  Sam Hocevar Dec 11 '11 at 12:12
    
Actually, you'll most likely get BOTH useless texture lookup AND a useless test, the way shaders work =) –  Jari Komppa Dec 12 '11 at 17:31
    
@JariKomppa such a cryptic comment needs an explanation! Please elaborate –  Will Dec 14 '11 at 7:22
    
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 –  Jari Komppa Dec 14 '11 at 21:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.