Hi all,
I’ve got a problem with the performance of my shaderprogram. All it does is texturemapping for 4 texture units, but it runs very slow (~15 FPS with an Object with about 6000 Triangles consisting out of 20 submeshes). I think the fragmentshader is the problem, because when I’m very far away from the object, so that it’s only some pixels large, the object renders at 60 fps. My device is a SE Satio with SGX530.
VertexShader:
uniform highp mat4 u_Modelmat;
uniform highp mat4 u_Viewmat;
uniform highp mat4 u_Projectionmat;
attribute highp vec4 a_position;
attribute lowp vec3 a_normal;
uniform mediump vec4 u_posbiasscale;
attribute lowp vec4 a_color;
uniform mediump vec4 u_texbiasscale_0;
uniform mediump vec4 u_texbiasscale_1;
uniform mediump vec4 u_texbiasscale_2;
uniform mediump vec4 u_texbiasscale_3;
uniform int u_texmode_0;
uniform int u_texmode_1;
uniform int u_texmode_2;
uniform int u_texmode_3;
attribute mediump vec3 a_texcoord_0;
attribute mediump vec3 a_texcoord_1;
attribute mediump vec3 a_texcoord_2;
attribute mediump vec3 a_texcoord_3;
varying lowp vec4 v_color;
varying mediump vec2 v_texcoord_0;
varying mediump vec2 v_texcoord_1;
varying mediump vec2 v_texcoord_2;
varying mediump vec2 v_texcoord_3;
void main(void){
mat4 mvmat=u_Viewmatu_Modelmat;
mat4 mvpmat=u_Projectionmatmvmat;
gl_Position=mvpmata_position;
//convert normal to eye space
lowp vec3 eyenormal = normalize((mvmatvec4(a_normal,0)).xyz);
//calculate spheremap texture coordinates
lowp vec3 refvec = normalize(reflect(gl_Position.xyz,eyenormal));
//P=sqrt(R.x²+R.y²+(R.z+1)²)
mediump float spheremapP=sqrt(refvec.xrefvec.x+refvec.yrefvec.y+(refvec.z+1.0)(refvec.z+1.0));
mediump vec2 spheremaptexcoord = vec2(refvec.x/spheremapP0.5+0.5,refvec.y/spheremapP0.5+0.5);
if(u_texmode_0==1)
v_texcoord_0=(a_texcoord_0u_texbiasscale_0.w+u_texbiasscale_0.xyz ).xy;
else if(u_texmode_0==2)
v_texcoord_0=spheremaptexcoord;
if(u_texmode_1==1)
v_texcoord_1=(a_texcoord_1u_texbiasscale_1.w+u_texbiasscale_1.xyz ).xy;
else if(u_texmode_1==2)
v_texcoord_1=spheremaptexcoord;
if(u_texmode_2==1)
v_texcoord_2=(a_texcoord_2u_texbiasscale_2.w+u_texbiasscale_2.xyz ).xy;
else if(u_texmode_2==2)
v_texcoord_2=spheremaptexcoord;
if(u_texmode_3==1)
v_texcoord_3=(a_texcoord_3*u_texbiasscale_3.w+u_texbiasscale_3.xyz ).xy;
else if(u_texmode_3==2)
v_texcoord_3=spheremaptexcoord;
v_color=a_color;
}
[/CODE]
Fragment Shader:<br>const int TOP_REPLACE = 0; <br>const int TOP_MODULATE = 1;<br>const int TOP_DECAL = 2;<br>const int TOP_BLEND = 3;<br>const int TOP_ADD = 4;<br>const int TOP_SUBTRACT = 5;<br>const int TOP_ADDSIGNED_ALPHA_MODULATE= 6;<br>const int TOP_LERP = 7;<br><br><br>varying lowp vec4 v_color;<br>varying mediump vec2 v_texcoord_0;<br>varying mediump vec2 v_texcoord_1;<br>varying mediump vec2 v_texcoord_2;<br>varying mediump vec2 v_texcoord_3;<br><br>uniform lowp sampler2D s_tex2D_0;<br>uniform lowp sampler2D s_tex2D_1;<br>uniform lowp sampler2D s_tex2D_2;<br>uniform lowp sampler2D s_tex2D_3;<br><br>uniform int u_texmode_0;<br>uniform int u_texmode_1;<br>uniform int u_texmode_2;<br>uniform int u_texmode_3;<br><br>uniform int u_texBlending_0;<br>uniform int u_texBlending_1;<br>uniform int u_texBlending_2;<br>uniform int u_texBlending_3;<br><br>uniform lowp vec4 u_texConstColor_0;<br>uniform lowp vec4 u_texConstColor_1;<br>uniform lowp vec4 u_texConstColor_2;<br>uniform lowp vec4 u_texConstColor_3;<br><br>void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor);<br><br>void main(void){<br> //gl_FragColor=v_color;<br> //for testing purposes:<br> gl_FragColor=vec4(0.8,0.8,0.8,1);<br> if(u_texmode_0!=0){<br> blendColorToDest(u_texBlending_0,texture2D(s_tex2D_0,v_texcoord_0),u_texConstColor_0);<br> }<br> if(u_texmode_1!=0){<br> blendColorToDest(u_texBlending_1,texture2D(s_tex2D_1,v_texcoord_1),u_texConstColor_1);<br> }<br> if(u_texmode_2!=0){<br> blendColorToDest(u_texBlending_2,texture2D(s_tex2D_2,v_texcoord_2),u_texConstColor_2);<br> }<br> if(u_texmode_3!=0){<br> blendColorToDest(u_texBlending_3,texture2D(s_tex2D_3,v_texcoord_3),u_texConstColor_3);<br> }<br>}<br><br>//might the many branches be the problem?<br>void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor){<br> if(blendMode==TOP_MODULATE){<br> gl_FragColor*=color;<br> }else if(blendMode==TOP_REPLACE){<br> gl_FragColor=color; <br> }else if(blendMode==TOP_ADD){<br> gl_FragColor+=color; <br> }else if(blendMode==TOP_SUBTRACT){<br> gl_FragColor-=color; <br> }else if(blendMode==TOP_DECAL){<br> gl_FragColor=vec4(gl_FragColor.rgb*(1.0-color.a)+color.rgb*color.a,gl_FragColor.a); <br> }else if(blendMode==TOP_BLEND){<br> gl_FragColor=vec4(gl_FragColor.rgb*(1.0-color.rgb)+color.rgb*constColor.rgb,gl_FragColor.a*color.a); <br> }else if(blendMode==TOP_ADDSIGNED_ALPHA_MODULATE){<br> gl_FragColor=vec4(gl_FragColor.rgb+(color.rgb-0.5),gl_FragColor.a*color.a); <br> }else if(blendMode==TOP_LERP){<br> gl_FragColor=color*constColor+gl_FragColor*(1.0-constColor); <br> }<br>}<br>
Do you have any optimization tips for me or any idea what causes the problem?
BTW: The object is rendered with lightning and texturing enabled via the fixed-function pileline at 80 fps.
Thanks in advance
<br>uniform highp mat4 u_Modelmat;<br>uniform highp mat4 u_Viewmat;<br>uniform highp mat4 u_Projectionmat;<br><br>attribute highp vec4 a_position;<br>attribute lowp vec3 a_normal;<br><br>uniform mediump vec4 u_posbiasscale;<br><br>attribute lowp vec4 a_color;<br><br><br>uniform mediump vec4 u_texbiasscale_0;<br>uniform mediump vec4 u_texbiasscale_1;<br>uniform mediump vec4 u_texbiasscale_2;<br>uniform mediump vec4 u_texbiasscale_3;<br><br>uniform int u_texmode_0;<br>uniform int u_texmode_1;<br>uniform int u_texmode_2;<br>uniform int u_texmode_3;<br><br>attribute mediump vec3 a_texcoord_0;<br>attribute mediump vec3 a_texcoord_1;<br>attribute mediump vec3 a_texcoord_2;<br>attribute mediump vec3 a_texcoord_3;<br><br>varying lowp vec4 v_color;<br>varying mediump vec2 v_texcoord_0;<br>varying mediump vec2 v_texcoord_1;<br>varying mediump vec2 v_texcoord_2;<br>varying mediump vec2 v_texcoord_3;<br><br><br>void main(void){<br><br> <br> mat4 mvmat=u_Viewmat*u_Modelmat;<br> mat4 mvpmat=u_Projectionmat*mvmat;<br> <br> gl_Position=mvpmat*a_position;<br> <br> //convert normal to eye space<br> lowp vec3 eyenormal = normalize((mvmat*vec4(a_normal,0)).xyz);<br> <br> //calculate spheremap texture coordinates<br> lowp vec3 refvec = normalize(reflect(gl_Position.xyz,eyenormal));<br> //P=sqrt(R.x²+R.y²+(R.z+1)²)<br> mediump float spheremapP=sqrt(refvec.x*refvec.x+refvec.y*refvec.y+(refvec.z+1.0)*(refvec.z+1.0));<br> mediump vec2 spheremaptexcoord = vec2(refvec.x/spheremapP*0.5+0.5,refvec.y/spheremapP*0.5+0.5);<br> <br> if(u_texmode_0==1)<br> v_texcoord_0=(a_texcoord_0*u_texbiasscale_0.w+u_texbiasscale_0.xyz ).xy;<br> else if(u_texmode_0==2)<br> v_texcoord_0=spheremaptexcoord;<br> <br> if(u_texmode_1==1) <br> v_texcoord_1=(a_texcoord_1*u_texbiasscale_1.w+u_texbiasscale_1.xyz ).xy;<br> else if(u_texmode_1==2) <br> v_texcoord_1=spheremaptexcoord;<br> <br> if(u_texmode_2==1) <br> v_texcoord_2=(a_texcoord_2*u_texbiasscale_2.w+u_texbiasscale_2.xyz ).xy;<br> else if(u_texmode_2==2) <br> v_texcoord_2=spheremaptexcoord;<br> <br> if(u_texmode_3==1) <br> v_texcoord_3=(a_texcoord_3*u_texbiasscale_3.w+u_texbiasscale_3.xyz ).xy;<br> else if(u_texmode_3==2)<br> v_texcoord_3=spheremaptexcoord;<br> <br> v_color=a_color;<br> <br>}<br>
Fragment Shader:
const int TOP_REPLACE = 0;
const int TOP_MODULATE = 1;
const int TOP_DECAL = 2;
const int TOP_BLEND = 3;
const int TOP_ADD = 4;
const int TOP_SUBTRACT = 5;
const int TOP_ADDSIGNED_ALPHA_MODULATE= 6;
const int TOP_LERP = 7;
varying lowp vec4 v_color;
varying mediump vec2 v_texcoord_0;
varying mediump vec2 v_texcoord_1;
varying mediump vec2 v_texcoord_2;
varying mediump vec2 v_texcoord_3;
uniform lowp sampler2D s_tex2D_0;
uniform lowp sampler2D s_tex2D_1;
uniform lowp sampler2D s_tex2D_2;
uniform lowp sampler2D s_tex2D_3;
uniform int u_texmode_0;
uniform int u_texmode_1;
uniform int u_texmode_2;
uniform int u_texmode_3;
uniform int u_texBlending_0;
uniform int u_texBlending_1;
uniform int u_texBlending_2;
uniform int u_texBlending_3;
uniform lowp vec4 u_texConstColor_0;
uniform lowp vec4 u_texConstColor_1;
uniform lowp vec4 u_texConstColor_2;
uniform lowp vec4 u_texConstColor_3;
void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor);
void main(void){
//gl_FragColor=v_color;
//for testing purposes:
gl_FragColor=vec4(0.8,0.8,0.8,1);
if(u_texmode_0!=0){
blendColorToDest(u_texBlending_0,texture2D(s_tex2D_0,v_texcoord_0),u_texConstColor_0);
}
if(u_texmode_1!=0){
blendColorToDest(u_texBlending_1,texture2D(s_tex2D_1,v_texcoord_1),u_texConstColor_1);
}
if(u_texmode_2!=0){
blendColorToDest(u_texBlending_2,texture2D(s_tex2D_2,v_texcoord_2),u_texConstColor_2);
}
if(u_texmode_3!=0){
blendColorToDest(u_texBlending_3,texture2D(s_tex2D_3,v_texcoord_3),u_texConstColor_3);
}
}
//might the many branches be the problem?
void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor){
if(blendMode==TOP_MODULATE){
gl_FragColor*=color;
}else if(blendMode==TOP_REPLACE){
gl_FragColor=color;
}else if(blendMode==TOP_ADD){
gl_FragColor+=color;
}else if(blendMode==TOP_SUBTRACT){
gl_FragColor-=color;
}else if(blendMode==TOP_DECAL){
gl_FragColor=vec4(gl_FragColor.rgb*(1.0-color.a)+color.rgbcolor.a,gl_FragColor.a);
}else if(blendMode==TOP_BLEND){
gl_FragColor=vec4(gl_FragColor.rgb(1.0-color.rgb)+color.rgbconstColor.rgb,gl_FragColor.acolor.a);
}else if(blendMode==TOP_ADDSIGNED_ALPHA_MODULATE){
gl_FragColor=vec4(gl_FragColor.rgb+(color.rgb-0.5),gl_FragColor.acolor.a);
}else if(blendMode==TOP_LERP){
gl_FragColor=colorconstColor+gl_FragColor*(1.0-constColor);
}
}
[/CODE]
Do you have any optimization tips for me or any idea what causes the problem?
BTW: The object is rendered with lightning and texturing enabled via the fixed-function pileline at 80 fps.
Thanks in advance
<br>const int TOP_REPLACE = 0; <br>const int TOP_MODULATE = 1;<br>const int TOP_DECAL = 2;<br>const int TOP_BLEND = 3;<br>const int TOP_ADD = 4;<br>const int TOP_SUBTRACT = 5;<br>const int TOP_ADDSIGNED_ALPHA_MODULATE= 6;<br>const int TOP_LERP = 7;<br><br><br>varying lowp vec4 v_color;<br>varying mediump vec2 v_texcoord_0;<br>varying mediump vec2 v_texcoord_1;<br>varying mediump vec2 v_texcoord_2;<br>varying mediump vec2 v_texcoord_3;<br><br>uniform lowp sampler2D s_tex2D_0;<br>uniform lowp sampler2D s_tex2D_1;<br>uniform lowp sampler2D s_tex2D_2;<br>uniform lowp sampler2D s_tex2D_3;<br><br>uniform int u_texmode_0;<br>uniform int u_texmode_1;<br>uniform int u_texmode_2;<br>uniform int u_texmode_3;<br><br>uniform int u_texBlending_0;<br>uniform int u_texBlending_1;<br>uniform int u_texBlending_2;<br>uniform int u_texBlending_3;<br><br>uniform lowp vec4 u_texConstColor_0;<br>uniform lowp vec4 u_texConstColor_1;<br>uniform lowp vec4 u_texConstColor_2;<br>uniform lowp vec4 u_texConstColor_3;<br><br>void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor);<br><br>void main(void){<br> //gl_FragColor=v_color;<br> //for testing purposes:<br> gl_FragColor=vec4(0.8,0.8,0.8,1);<br> if(u_texmode_0!=0){<br> blendColorToDest(u_texBlending_0,texture2D(s_tex2D_0,v_texcoord_0),u_texConstColor_0);<br> }<br> if(u_texmode_1!=0){<br> blendColorToDest(u_texBlending_1,texture2D(s_tex2D_1,v_texcoord_1),u_texConstColor_1);<br> }<br> if(u_texmode_2!=0){<br> blendColorToDest(u_texBlending_2,texture2D(s_tex2D_2,v_texcoord_2),u_texConstColor_2);<br> }<br> if(u_texmode_3!=0){<br> blendColorToDest(u_texBlending_3,texture2D(s_tex2D_3,v_texcoord_3),u_texConstColor_3);<br> }<br>}<br><br>//might the many branches be the problem?<br>void blendColorToDest(in int blendMode, in lowp vec4 color, in lowp vec4 constColor){<br> if(blendMode==TOP_MODULATE){<br> gl_FragColor*=color;<br> }else if(blendMode==TOP_REPLACE){<br> gl_FragColor=color; <br> }else if(blendMode==TOP_ADD){<br> gl_FragColor+=color; <br> }else if(blendMode==TOP_SUBTRACT){<br> gl_FragColor-=color; <br> }else if(blendMode==TOP_DECAL){<br> gl_FragColor=vec4(gl_FragColor.rgb*(1.0-color.a)+color.rgb*color.a,gl_FragColor.a); <br> }else if(blendMode==TOP_BLEND){<br> gl_FragColor=vec4(gl_FragColor.rgb*(1.0-color.rgb)+color.rgb*constColor.rgb,gl_FragColor.a*color.a); <br> }else if(blendMode==TOP_ADDSIGNED_ALPHA_MODULATE){<br> gl_FragColor=vec4(gl_FragColor.rgb+(color.rgb-0.5),gl_FragColor.a*color.a); <br> }else if(blendMode==TOP_LERP){<br> gl_FragColor=color*constColor+gl_FragColor*(1.0-constColor); <br> }<br>}<br>
Do you have any optimization tips for me or any idea what causes the problem?
BTW: The object is rendered with lightning and texturing enabled via the fixed-function pileline at 80 fps.
Thanks in advance