Sign in to follow this  
Grumple

GLSL 330 sampler2D array access via vertex attribute?

Recommended Posts

Grumple    177

Hello,

 

After a lot of programming and debugging I feel like a dumb ass.   I set up an entire billboard shader system around instancing, and as part of that design I was passing a vertex attribute representing the texture unit to sample from for a given billboard instance.   

 

After setting all of this up I was getting strange artifacts, and found that GLSL 330 only officially supports constant (compile-time) index into a uniform sampler2D array?

 

Is there any nice way around this limitation short of compiling against a newer version of GLSL?  Is there at least a way to check if the local driver supports the sampler index as vertex attribute through an extension or something?  I tested my implementation on an NVidia card and it worked despite the spec, but ATI (as usual) seems more strict.

 

For now I have patched the problem by manually branching *shudder* based on the index and accessing the equivalent sampler via a constant in my fragment shader.  

 

For example:

        if(passModelTexIndex == 0) 
        { 
            fragColor = texture2D(texturesArray_Uni[0], passTexCoords); 
        } 
        else if(passModelTexIndex == 1) 
        { 
            fragColor = texture2D(texturesArray_Uni[1], passTexCoords); 
        } 
        else 
        { 
            fragColor = vec4(0.0, 0.0, 0.0, 1.0); 
        } 

Share this post


Link to post
Share on other sites
TheChubu    9446

Hmm, I'm not sure, I never tried it but I don't see why you wouldn't be able to select the texture with an uniform as index since that was the whole idea for texture arrays (ie, upload everything then select texture on the fly).

 

Have a look at this http://www.opengl.org/wiki/Array_Texture in "Access in Shaders" section. Also here http://www.opengl.org/wiki/Sampler_(GLSL) in "Basic Texture Access"

Edited by TheChubu

Share this post


Link to post
Share on other sites
Grumple    177

Your link might be a workable option, but having read a bit more I think I might be confusing people with my description.

 

I think the real compatibility issue is more generalized to accessing a uniform array from within the fragment shader using a non-constant index.   The index variable I am using is originally received in the vertex shader as an attribute (in), and passed to the fragment shader (out),  The fragment shader then  uses that to index into the uniform array of texture samplers.   

 

What I've found in hindsight is that glsl 330 doesn't like using any form of variable as an index into said uniform array, even though NVidia seems to allow it.  =/

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