Sign in to follow this  
rewolfer

GLSL check for bound texture

Recommended Posts

I was wondering if GLSL can check whether a texture is bound to a sampler2D or not. The shader should use the material's diffuse colour if there is no texture bound, and I'm trying to use the best approach. I could send a uniform specifying whether to use texture/diffuse. Also, what affect do branches have on shader performance? I know in CUDA if there is branching with a half-warp then each branch/path is executed in serial. But in my case all vertices/fragments being rendered together will have the same material/texture properties because i render the meshes per material. What is the usual way people go about this?

Share this post


Link to post
Share on other sites
You can't check, in other words, there isn't a boolean value to tell you if a texture is there.

Branches cause loss of performance, specially in fragment shader. That's the reason why if a branch doesn't contain much code, the GLSL compiler will get rid of the branch and just execute each branch and choose one of the results.

uniform vec4 NoTexture; //You upload this
//If no texture, set it to {1.0, 1.0, 1.0, 1.0}

vec4 texel = texture2D(Texture0, TexCoord);
texel = clamp(texel+NoTexture, 0.0, 1.0);
texel *= Diffuse;
gl_FragColor = texel;

Share this post


Link to post
Share on other sites
thanks for the info
i don't want to be using the Diffuse if there is a texture present though, and your method will use a combination texel*diffuse. I'm going to try using this:

gl_FragColor = NoTexture * diffuse + texel;

assuming texel = (0,0,0,0) if no texture is bound

thanks again
rewolfer

Share this post


Link to post
Share on other sites
I think if there is no texture bound, then {1.0, 1.0, 1.0, 1.0} gets returned.
The solution is simple, bind a black texture.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this