Problems with colors and samples mixing in GLSL shaders

Hello all!

I have a slight issue here. I have a shader in which I pass the color and sample into the fragment. The texture renders fine, but when I want to draw a plain colored cube, it will be tainted black. This is because I am using mix(), mixing the color and texture.

I can fix this by passing in a uniform u_sampled, which will act as a switch between mixing the texture and color, or just the color. This is pretty messy, however… So my question is: how can I figure out if a texture (sample) is bound from the shader?

Thanks,
-wes :slight_smile:

Few articles on internet said that starting GLSL 1.3, but I’ve never used it :


if( textureSize( SamplerDiffuse, 0).x > 0)
   // sample texture
else
   // use color

Say us if that works !

Create a 4x4 texture filled with white pixels and bind that when you want to use “no” texture.

I though of that solution too, but that will not break the mix between the color and the white pixel, no ?

Perhaps you can play and force the mix_factor to 0 or 1…

This is what I’m seeing at the moment:

  • You’re shader expects that you have a texture and mixes between an input color and a texture sample.
  • You currently don’t have a texture bound.
  • You expect your shader to magically adapt and ignore the texture, but instead GLSL does the only logical thing and feeds you black.

Conditional statements in shaders are worse than messy. They are slow. Very slow. Either do as @theagentd says, as @Gef says or just use a different shader. There is no other way I’m afraid.

You can also use the same shader with preprocessor like “#ifdef”, to enable/disable parts in your shader at compilation.
At the end, you have one shader to maintain but two or more different versions declared in your Java code.


#ifdef USE_TEXTURE
    // mix color with texture
#else
    // use color
#endif


This is a common misconception. If-statements aren’t expensive per se, but they have some quirks which can make them useless for performance gains.

First of all, conditional assignments are extremely cheap. Example: [icode]x = myBoolean ? 10 : 5;[/icode]

Secondly, on OpenGL cards with dynamic branching, if-statements can actually increase performance. Many people want to use if-statements to skip work in shaders, realize that they give no performance gain (but also don’t performance cost!) and proclaim that if-statements are slow. GPUs runs shaders in groups, usually of 16x16 pixels, but the processors on your GPU aren’t very flexible. If you have an if-else-statement in your shader and 255 pixels pass the if-statement and just a single pixel does (= enters the else-block), all 256 shader executions will run both the if and else blocks. For example, I had code that skipped work for pixels with depth=1.0 (meaning the sky was visible so I can skip some computations) and it worked very well, since the sky pixels were generally a continuous area on the screen, so a significant number of 16x16 blocks could skip the computation. If you on the other hand were to look at the sky through the branches of a tree, this would break down since most 16x16 blocks would have at least a few pixels that the branches or leaves covered, so the whole block would have to run the computations.