Bug 55153 - Mesa could optimize repeated multiplication of a varying by a constant factor in the fragment shader
Summary: Mesa could optimize repeated multiplication of a varying by a constant factor...
Status: NEW
Alias: None
Product: Mesa
Classification: Unclassified
Component: glsl-compiler (show other bugs)
Version: 8.0
Hardware: x86-64 (AMD64) Linux (All)
: medium normal
Assignee: Ian Romanick
QA Contact:
Depends on:
Blocks: i965-perf
  Show dependency treegraph
Reported: 2012-09-20 15:11 UTC by Samuel Rødal
Modified: 2014-04-16 21:51 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Note You need to log in before you can comment on or make changes to this bug.
Description Samuel Rødal 2012-09-20 15:11:37 UTC
When the fragment shader contains code like this:

varying vec4 pos;

void main(void){
 const float factor=0.1;

 vec3 nf1=texture2D( tex2, pos.xy*factor).xyz;
 vec3 nf2=texture2D( tex2, pos.xz*factor).xyz;
 vec3 nf3=texture2D( tex2, pos.zy*factor).xyz;


and this is the only use of pos in the fragment shader, it's more optimal to multiply the pos varying by 0.1 in the vertex shader. This gives a noticeable performance improvement at least on r600.

Is this something that could be automatically done by the GLSL compiler / optimizer?
Comment 1 Samuel Rødal 2012-09-20 15:23:49 UTC
Example change from real-world code implementing this change (although doing the multiplication with a temporary variable in the fragment shader instead of doing it in the vertex shader, maybe Mesa should catch this case too):

     1.6    fragment+='void main(void){';
     1.7 -  fragment+='const float factor=0.1;';
     1.8 +  if saVolume in attr then fragment+='vec3 fpos=pos.xyz*0.1;';
    1.10    // möglichst früh discarden
    1.11    if saBuild in attr then fragment+='if(texture2D(tex3, vec2(gl_TexCoord[0])).r<extra) discard;';
    1.12 @@ -144,9 +144,9 @@
    1.13     if (saWater in attr) then begin
    1.14       fragment+='vec3 _nrm = texture2D(tex2,gl_TexCoord[1].xy+vec2(sin(0.00001*time), cos(0.00001*time)), -4.0).xyz*2.0-1.0;';
    1.15     end else if saVolume in attr then begin
    1.16 -    fragment+='vec3 nf1=texture2D( tex2, pos.xy*factor).xyz;';
    1.17 -    fragment+='vec3 nf2=texture2D( tex2, pos.xz*factor).xyz;';
    1.18 -    fragment+='vec3 nf3=texture2D( tex2, pos.zy*factor).xyz;';
    1.19 +    fragment+='vec3 nf1=texture2D( tex2, fpos.xy).xyz;';
    1.20 +    fragment+='vec3 nf2=texture2D( tex2, fpos.xz).xyz;';
    1.21 +    fragment+='vec3 nf3=texture2D( tex2, fpos.zy).xyz;';
    1.22      fragment+='vec3 _nrm = normalize(vec3(norm2.z*nf1+norm2.y*nf2+norm2.x*nf3)*2.0-1.0);';
    1.23     end else fragment+='vec3 _nrm = texture2D(tex2,vec2(gl_TexCoord[0])).xyz*2.0-1.0;';
    1.24    end else if saDiffuse in attr then fragment+='vec3 normal = normalize(norm);';
    1.25 @@ -184,9 +184,9 @@
    1.26      '+vec2(0.002*sin(time*0.007+watercoord.y*30.0), -0.002+0.001*sin(time*0.002+watercoord.x*100.0)), 1.0/wh.y, 1.0-1.0/wh.y));';
    1.28    end else if (saVolume in attr) and (saDiffuse in attr) and (saTexture in attr) then begin
    1.29 -   fragment+='vec3 f1=texture2D( tex, pos.xy*factor).xyz;';
    1.30 -   fragment+='vec3 f2=texture2D( tex, pos.xz*factor).xyz;';
    1.31 -   fragment+='vec3 f3=texture2D( tex, pos.zy*factor).xyz;';
    1.32 +   fragment+='vec3 f1=texture2D( tex, fpos.xy).xyz;';
    1.33 +   fragment+='vec3 f2=texture2D( tex, fpos.xz).xyz;';
    1.34 +   fragment+='vec3 f3=texture2D( tex, fpos.zy).xyz;';
    1.35     fragment+='vec4 col = vec4(norm2.z*f1+norm2.y*f2+norm2.x*f3, gl_TexCoord[0].x);';

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.