Sign in to follow this  
kuroioranda

[GLSL] Detecting if a sampler is set or not

Recommended Posts

Is there a way to tell from within the shader if a given sampler is set or not? Alternatively, can I specify the color value that is returned when a sampler is not bound in a shader? I'd rather not have to either write two shaders or bind a small single-color texture to do this.

Share this post


Link to post
Share on other sites
I never encounter any option to check if a sampler is bound or not. But why don't you use an uniform to control your shader code instead of checking if a texture is bound ?

Share this post


Link to post
Share on other sites
textureSize() ?

In any case, it's sub-optimal stuff to do the decision-making in the shader. And besides, what would you do in the case when there are other, valid textures bound to the sampler-units (0..31) ? If your cpu code doesn't change them, there will potentially be garbage: your C++ code won't be changing gl_texture1, but the shader will be seeing the previous drawcall's value for the sampler.
So, wise-up the cpu code.

Share this post


Link to post
Share on other sites
The other day I was wondering this. For a solution I used a 1x1 pixel white texture when I didn't have any other texture I wanted to send to the sampler.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ashaman73
I never encounter any option to check if a sampler is bound or not. But why don't you use an uniform to control your shader code instead of checking if a texture is bound ?


I actually hadn't thought of this. It's still not quite what I want, because it still requires data redundancy (both the uniform value and the texture index bound to the texture unit need to be kept in sync), but it's a possibility, thanks!

Quote:
In any case, it's sub-optimal stuff to do the decision-making in the shader. And besides, what would you do in the case when there are other, valid textures bound to the sampler-units (0..31) ? If your cpu code doesn't change them, there will potentially be garbage: your C++ code won't be changing gl_texture1, but the shader will be seeing the previous drawcall's value for the sampler.
So, wise-up the cpu code.


Yeah, I'd rather not do it in the shader. I think you misunderstand my intent, though. I'm aware that there could be garbage in the sampler units, what I want is to be able to *clear* a texture unit (glBindTexture(GL_TEXTURE_2D, 0)) and have the sampler return something like solid white so that a blank texture stage is effectively a no-op, similar to what you get with the fixed functionality pipe. Now I can do things like say "this model doesn't have a diffuse map" and still have the shader work. Otherwise I need to duplicate state (which stages are bound to textures) with a uniform, or duplicate the shader without that sampler and make a shader state change.


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