[Mesa-dev] glsl2: optimizing unused struct assignments

Aras Pranckevicius aras at unity3d.com
Thu Aug 5 04:33:50 PDT 2010


> I believe the plan is to eventually break structures and arrays that are
> not variably indexed into individual variables.  Your structure above
> would be broken into s_used and s_unused.  The existing dead code paths
> would take care of s_unused.  We'll need to do this break-up anyway to
> do proper register assignment.

Ok, nice. If someone else is going to do that I don't have to worry
about it. Any idea on when that work is planned to be done?

For the GLSL optimizer I'm working on this currently seems to be the
largest possible optimization left. On a certain mobile platform that
starts with 'i' a fragment shader like this:

struct v2f { vec4 pos; vec2 uv0; vec2 uv1; };
uniform vec4 _Color;
uniform sampler2D _Detail;
uniform sampler2D _MainTex;
varying vec4 xlv_TEXCOORD0;
varying vec4 xlv_TEXCOORD1;
void main ()
{
  v2f xlt_i;
  xlt_i.uv0 = xlv_TEXCOORD0.xy;
  xlt_i.uv1 = xlv_TEXCOORD1.xy;
  v2f i;
  i = xlt_i;
  vec4 c;
  vec4 tmpvar_10;
  tmpvar_10 = texture2D (_MainTex, i.uv0);
  c = (_Color * tmpvar_10);
  vec4 tmpvar_12;
  tmpvar_12 = texture2D (_Detail, i.uv1);
  vec4 tmpvar_13;
  tmpvar_13 = ((c * tmpvar_12) * 2.000000);
  c = tmpvar_13;
  gl_FragData[0] = tmpvar_13.xyzw;
}

Is running about 2.5X slower than the one I hand-optimize by removing
structure member moves:

// ...
void main ()
{
  vec4 c;
  vec4 tmpvar_10;
  tmpvar_10 = texture2D (_MainTex, xlv_TEXCOORD0.xy);
  c = (_Color * tmpvar_10);
  vec4 tmpvar_12;
  tmpvar_12 = texture2D (_Detail, xlv_TEXCOORD1.xy);
  vec4 tmpvar_13;
  tmpvar_13 = ((c * tmpvar_12) * 2.000000);
  c = tmpvar_13;
  gl_FragData[0] = tmpvar_13.xyzw;
}



> We'll have similar issue with code like:
> uniform vec4 angles;
> void main() {
>   vec4 v;
>   v.x = sin(angles.x);
>   v.y = cos(angles.y);
>   v.z = tan(angles.z);
>   v.w = 1/tan(angles.w);
>   gl_Position = v.xyxy;
> }
>
> In this case v.zw is never used, and the (expensive) assignments should
> be killed.

What's the plan for attacking this? Splitting up structures into
individual variables won't help with this case.


-- 
Aras Pranckevičius
work: http://unity3d.com
home: http://aras-p.info


More information about the mesa-dev mailing list