Skip to main content
added 2 characters in body
Source Link
Aulaulz
  • 249
  • 3
  • 8

I know that this could be fixed by just keeping the array in the cpu and send the material as uniform. But why dodoes this not lead to framerate drop and the other do?

I know that this could be fixed by just keeping the array in the cpu and send the material as uniform. But why do this not lead to framerate drop and the other do?

I know that this could be fixed by just keeping the array in the cpu and send the material as uniform. But why does this not lead to framerate drop and the other do?

Source Link
Aulaulz
  • 249
  • 3
  • 8

glsl uniform int as index of const array

I have a const array that define a few materials for directionnal light. I'd like to change the material using an uniform int as index of that array. Doing that leads to severe framerate drop.

I know that this could be fixed by just keeping the array in the cpu and send the material as uniform. But why do this not lead to framerate drop and the other do?

It seems that even with 2 elements in my array I have framerate drops. But accessing the array with a constant doesn't make me lag ( I presume glsl drivers optimize this by replacing the line with the correct element in the array ).

This is my shader :

struct Material
{
    vec3 ambiant, diffuse, specular;
    float shininess;
};

const Material materials[] = Material[]
(
    Material( vec3( 0.05375, 0.05, 0.06625 ), vec3( 0.18275, 0.17, 0.22525 ), vec3(  0.332741, 0.328634, 0.346435 ), 0.3 * 128 ),
    Material( vec3( 0.1, 0.18725, 0.1745 ), vec3( 0.396, 0.74151, 0.69102 ), vec3( 0.297254, 0.30829, 0.306678 ), 0.1 * 128 ),
    Material( vec3( 0.25, 0.25, 0.25 ), vec3( 0.4, 0.4, 0.4 ), vec3( 0.774597, 0.774597, 0.774597 ), 0.6 ),
    Material( vec3( 0.19125, 0.0735, 0.0225 ), vec3( 0.7038, 0.27048, 0.0828 ), vec3( 0.256777, 0.137622, 0.086014 ), 0.1 * 128 ),
    Material( vec3( 0.24725, 0.1995, 0.0745 ), vec3( 0.75164, 0.60648, 0.22648 ), vec3( 0.628281, 0.555802, 0.366065 ), 0.4 * 128 ),
    Material( vec3( 0.0, 0.0, 0.0 ), vec3( 0.5, 0.5, 0.0 ), vec3( 0.60, 0.60, 0.50 ), 0.25 * 128 ),
    Material( vec3( 0.05, 0.0, 0.0 ), vec3( 0.5, 0.4, 0.4 ), vec3( 0.7, 0.04, 0.04 ), 0.078125 * 128 )
);

void main()
{
    Material mat = materials[ index ];

    vec3 lightColor = vec3( 1.0, 1.0, 1.0 );

    float diffuseFactor = max( dot( normalize( Normal ), lightPos ), 0.0 );

    vec3 reflectedRay = reflect( -lightPos, normalize( Normal ) );
    float specularFactor = pow( max( dot( reflectedRay, normalize( cameraPos - Position ) ), 0.0 ), mat.shininess );

    vec3 ambiantColor = lightColor * mat.ambiant;
    vec3 diffuseColor = diffuseFactor * lightColor * mat.diffuse;
    vec3 specularColor = specularFactor * lightColor * mat.specular;

    color = vec4( ambiantColor + diffuseColor + specularColor, 1.0f );
}