fragment shader if statment and loading array of m

2019-09-05 15:27发布

问题:

I have problem with if here, for reasons uknown to me, it dosnt work. When i delete if statment or malualy write shadowMap[0] 1 or 2 it works fine, but with if i just get set of white triangles and squares.

here is part of my frag shader

float shadow(float bias)
{
    float visibility = 0;
    int index=1;    
    if(gl_FragCoord.z<1.0){
        index=0;
    }       
    vec4 shadowCoord=depthPV*vPos;
    if ( texture(shadowMap[index], shadowCoord.xy).z  <  shadowCoord.z+bias){
      visibility = -1;
    }
    return visibility;
}

Other problem i have is with loading array of mat4 into uniform here is code i tried, but it dosnt work, i use lwjgl 3 libery in java

        shadowPVs=GL20.glGetUniformLocation(pId, "shadowPVs");          
        ByteBuffer shadowPVbuff=BufferUtils.createByteBuffer(shadePV.length*16*4);
        for(int i=0;i<shadePV.length;i++){
            for(int v=0;v<16;v++){
                shadowPVbuff.putFloat(shadePV[i].val[v]);
            }
        }
        shadowPVbuff.flip();

        GL20.glUniformMatrix4f(shadowPVs, shadePV.length, false, shadowPVbuff);

and in shader

uniform mat4 shadowPVs[3];

回答1:

What you are trying to do is not possible with current GL.

As @gemse already pointed out in the comments, the relevant part of the GLSL 3.30 spec is:

Samplers aggregated into arrays within a shader (using square brackets [ ]) can only be indexed with integral constant expressions (see section 4.3.3 “Constant Expressions”).

In GL 4, this constraint has been somehwat relaxed, but not far enough. In the most current GLSL 4.50 spec, the statement is:

When aggregated into arrays within a shader, samplers can only be indexed with a dynamically uniform integral expression, otherwise results are undefined.

With dynamically uniform being defined as

A fragment-shader expression is dynamically uniform if all fragments evaluating it get the same resulting value. When loops are involved, this refers to the expression's value for the same loop iteration. When functions are involved, this refers to calls from the same call point. This is similarly defined for other shader stages, based on the per-instance data they process. Note that constant expressions are trivially dynamically uniform. It follows that typical loop counters based on these are also dynamically uniform.

Your index depends on data which can vary per fragment, so you are getting undefined results.