fragment shader if statment and loading array of mat4 into uniform

♀尐吖头ヾ 提交于 2020-01-06 21:12:15

问题


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.



来源:https://stackoverflow.com/questions/30187813/fragment-shader-if-statment-and-loading-array-of-mat4-into-uniform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!