I am running this fragment shader on every pixel on screen. I am trying to make it as efficient as possible. BTW I am running open gl es 2.0.
The code segment below is only a sample from the code, there are about 56 different calls to Gaussian() spread across different functions. I am wondering wether it would be valuable to replace the calls to Gaussian() with there appropriate resulting float value.
I know a lot of times stuff like this is pre-calculated on compilation of the shader, or calculated only once because the gpu realizes it is the same calculation for all fragments.
So would it be worthwhile for me to manually calculate each of these and replace them with their values?
uniform float scalar;
varying float scalart;
float deviationScale = 1.2;
float finalScalar = 1.1;
float Gaussian(float x, float deviation)
{
return (1.0 / sqrt(2.0 * 3.141592 * deviation)) * exp(-((x * x) / (2.0 * deviation)));
}
vec3 blurS5()
{
vec3 blr = vec3(0.0);
blr += texture2D(s_texture, (v_texcoord + vec2(2.0 * scalart, 0.0))).xyz * finalScalar * Gaussian(2.0, 2.0 * deviationScale) ;
blr += texture2D(s_texture, (v_texcoord + vec2(1.0 * scalart, 0.0))).xyz * finalScalar * Gaussian(1.0, 2.0 * deviationScale) ;
blr += texture2D(s_texture, (v_texcoord + vec2(0.0 * scalart, 0.0))).xyz * finalScalar * Gaussian(0.0, 2.0 * deviationScale) ;
blr += texture2D(s_texture, (v_texcoord + vec2(-1.0 * scalart, 0.0))).xyz * finalScalar * Gaussian(-1.0, 2.0 * deviationScale) ;
blr += texture2D(s_texture, (v_texcoord + vec2(-2.0 * scalart, 0.0))).xyz * finalScalar * Gaussian(-2.0, 2.0 * deviationScale) ;
return blr;
}
void main()
{
if (scalar == 2.)
{
gl_FragColor = vec4(blurS5(), 1.0);
}
else if (scalar == 3.)
{
gl_FragColor = vec4(blurS9(), 1.0);
}
//There are waaayyy more of these
}