Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augmented operators: x=x+... becomes x+= #206

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ be an issue.

Disable this transformation with the flag `--no-move-declarations`.

### Augmented operators

We use the augmented operators (e.g. `+=`) where possible.

Input:
```glsl
spe=spe*spe;
x=x-.5;
a=a+(a<<3);
a=a^a>>15;
```

Output:
```glsl
spe*=spe;
x-=.5;
a+=a<<3;
a^=a>>15;
```

This transformation always reduces the size of the output. However, this seems
to have negligible impact on the size after compression.

### Rename vector fields

To access fields of a vector, it is possible to use `.rgba` fields, or `.xyzw`,
Expand Down
8 changes: 8 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ let (|Number|_|) = function
| Float (f, _) -> Some f
| _ -> None

let augmentableOperators = set ["+"; "-"; "*"; "/"; "%"; "<<"; ">>"; "&"; "^"; "|"]


let private simplifyOperator env = function
| FunCall(Op "-", [Int (i1, su)]) -> Int (-i1, su)
| FunCall(Op "-", [FunCall(Op "-", [e])]) -> e
Expand Down Expand Up @@ -173,6 +176,11 @@ let private simplifyOperator env = function
// x-(y-z) -> x-y+z
| FunCall(Op "-", [x; FunCall(Op "-", [y; z])]) ->
FunCall(Op "+", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env

// Match: x = x + ...
| FunCall(Op "=", [Var x; FunCall(Op op, [Var y; e])])
when x.Name = y.Name && augmentableOperators.Contains op ->
FunCall(Op (op + "="), [Var x; e])
| e -> e


Expand Down
1 change: 1 addition & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
--no-renaming --no-inlining --format indented -o tests/unit/pi.frag.expected tests/unit/pi.frag
--no-renaming --no-inlining --format indented -o tests/unit/vectors.frag.expected tests/unit/vectors.frag
--no-renaming --format indented -o tests/unit/deadcode.frag.expected tests/unit/deadcode.frag
--no-renaming --no-inlining --format indented -o tests/unit/augmented.frag.expected tests/unit/augmented.frag

# Inlining unit tests

Expand Down
12 changes: 6 additions & 6 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from-the-seas-to-the-stars.frag 14347 => 2353.607
the_real_party_is_in_your_pocket.frag 12214 => 1829.362
the_real_party_is_in_your_pocket.frag 12213 => 1829.362
ed-209.frag 7899 => 1359.091
valley_ball.glsl 4419 => 896.357
lunaquatic.frag 5232 => 1056.700
slisesix.frag 4546 => 957.481
slisesix.frag 4543 => 958.065
yx_long_way_from_home.frag 2993 => 614.607
oscars_chair.frag 4649 => 982.393
kinder_painter.frag 2901 => 456.603
kinder_painter.frag 2894 => 456.030
ohanami.frag 3293 => 746.366
terrarium.frag 3646 => 748.815
terrarium.frag 3633 => 749.292
leizex.frag 2308 => 515.785
elevated.hlsl 3406 => 611.302
buoy.frag 4237 => 630.724
orchard.frag 5618 => 1058.054
robin.frag 6298 => 1067.719
Total: 88006 => 15884.964
robin.frag 6294 => 1066.421
Total: 87978 => 15884.156
14 changes: 7 additions & 7 deletions tests/real/kinder_painter.expected
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
"{"
"vec3 nor;"
"if(col.w>2.5)"
"nor.xz=inter.xz-obj.xz,nor.y=0.,nor=nor/obj.w,uv=vec2(nor.x,inter.y);"
"nor.xz=inter.xz-obj.xz,nor.y=0.,nor/=obj.w,uv=vec2(nor.x,inter.y);"
"else"
" if(col.w>1.5)"
"nor=obj.xyz,uv=inter.xz*.2;"
"else"
" nor=inter-obj.xyz,nor=nor/obj.w,uv=nor.xy;"
" nor=inter-obj.xyz,nor/=obj.w,uv=nor.xy;"
"return nor;"
"}"
"vec4 cmov(vec4 a,vec4 b,bool cond)"
Expand Down Expand Up @@ -131,8 +131,8 @@
"ref.xyz=reflect(rd,nor);"
"spe=dot(ref.xyz,luz.xyz);"
"spe=max(spe,0.);"
"spe=spe*spe;"
"spe=spe*spe;"
"spe*=spe;"
"spe*=spe;"
"if(intersectShadow(inter,luz.xyz,luz.w))"
"dif=0.;"
"col*=texture2D(tex0,uv);"
Expand All @@ -141,8 +141,8 @@
"dif=dot(nor,-rd);"
"ref.w=dif;"
"dif=1.-dif*dif;"
"dif=dif*dif;"
"col=col+.35*vec4(dif);"
"dif*=dif;"
"col+=.35*vec4(dif);"
"return col;"
"}"
"void main()"
Expand Down Expand Up @@ -174,7 +174,7 @@
"luz.xyz=luz.xyz/luz.w;"
"col=basicShade(inter,obj,col,rd,luz,ref);"
"tmin=intersect(inter,ref.xyz,obj,col2);"
"inter=inter+ref.xyz*tmin;"
"inter+=ref.xyz*tmin;"
"luz.xyz=vec3(0,1.5,-1)-inter;"
"luz.w=length(luz.xyz);"
"luz.xyz=luz.xyz/luz.w;"
Expand Down
10 changes: 5 additions & 5 deletions tests/real/mandelbulb.expected
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const char *mandelbulb_frag =
"for(int t=1;t<7;t++)"
"{"
"\n#if 0\n"
"float z=sqrt(dot(i,i)),c=acos(i.y/z),o=atan(i.x,i.z);"
"float z=sqrt(dot(i,i)),o=acos(i.y/z),c=atan(i.x,i.z);"
"z=pow(z,8.);"
"c=c*8.;"
"o=o*8.;"
"i=v+z*vec3(sin(c)*sin(o),cos(c),sin(c)*cos(o));"
"o*=8.;"
"c*=8.;"
"i=v+z*vec3(sin(o)*sin(c),cos(o),sin(o)*cos(c));"
"\n#else\n"
"float s=i.x,d=s*s,n=d*d,m=i.y,l=m*m,p=i.z,w=p*p,r=w*w,g=d+w,a=inversesqrt(g*g*g*g*g*g*g),u=n+l*l+r-6.*l*w-6.*d*l+2.*w*d,q=d-l+w;"
"i.x=v.x+64.*s*m*p*(d-w)*q*(n-6.*d*w+r)*u*a;"
Expand Down Expand Up @@ -100,7 +100,7 @@ const char *mandelbulb_frag =
"{"
"vec3 m=c+u*s,a;"
"float q=clamp(.2+.8*dot(f,n),0.,1.),b,F,C;"
"q=q*q;"
"q*=q;"
"b=clamp(.3+.7*dot(o,n),0.,1.);"
"F=clamp(1.25*g.w-.4,0.,1.);"
"F=F*F*.5+.5*F;"
Expand Down
6 changes: 3 additions & 3 deletions tests/real/slisesix.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ float techo(float x,float y)
y=1.-y;
if(x<.1||x>.9)
return y;
x=x-.5;
x-=.5;
return-(sqrt(x*x+y*y)-.4);
}
float distToBox(vec3 p,vec3 abc)
Expand Down Expand Up @@ -190,7 +190,7 @@ void main()
lig=vec3(.5-pos.x,.8-pos.y,1.5-pos.z);
llig=dot(lig,lig);
im=inversesqrt(llig);
lig=lig*im;
lig*=im;
dif=dot(nor,lig);
if(matID==4)
dif=.5+.5*dif;
Expand Down Expand Up @@ -264,7 +264,7 @@ void main()
}
dif*=clamp((so-.4)*1.5,0.,1.);
rgb=vec3(spe)+rgb*(ao*vec3(.25,.3,.35)+dif*vec3(1.95,1.65,1.05));
rgb=rgb*exp2(-.4*t);
rgb*=exp2(-.4*t);
}
rgb=(sqrt(rgb)*.7+.3*rgb)*vec3(.83,1,.83)*1.2;
rgb*=.25+.75*clamp(.6*abs(pixel.x-1.)*abs(pixel.x+1.),0.,1.);
Expand Down
18 changes: 9 additions & 9 deletions tests/real/terrarium.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ float time=float(m)/44100.;
int IH(int a)
{
a=a^61^a>>16;
a=a+(a<<3);
a=a^a>>4;
a=a*668265261;
a=a^a>>15;
a+=a<<3;
a^=a>>4;
a*=668265261;
a^=a>>15;
return a;
}
float H(int a)
{
a=a^61^a>>16;
a=a+(a<<3);
a=a^a>>4;
a=a*668265261;
a=a^a>>15;
a+=a<<3;
a^=a>>4;
a*=668265261;
a^=a>>15;
return float(a)/2147483647;
}
vec2 rand2(int a)
Expand Down Expand Up @@ -131,7 +131,7 @@ void main()
u=sqrt(1-u*u);
x=vec3(cos(phi)*u,sin(phi)*u,w.y*2-1)*pow(w.z,1./3)*3;
q=mix(q,q+x*.5,.1*pow(H(seed),16.));
q=q+x*.2*(1.-min(time/30.,1.)+max(0.,time-130)/20.);
q+=x*.2*(1.-min(time/30.,1.)+max(0.,time-130)/20.);
u=time/8.+199.*step(.5,H(int(floor(time/60.*64.))))*step(60.,time)*step(time,120);
q.xy=mat2(cos(u),sin(u),-sin(u),cos(u))*q.xy;
u=.4+time/9.;
Expand Down
2 changes: 1 addition & 1 deletion tests/real/the_real_party_is_in_your_pocket.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const char *the_real_party_is_in_your_pocket_frag =
"break;"
"k=.02;"
"if(m()>k)"
"l=c-z*2e-4*-sign(v.z),v=v+(vec3(m(),m(),m())-.5)*.1;"
"l=c-z*2e-4*-sign(v.z),v+=(vec3(m(),m(),m())-.5)*.1;"
"else"
" l=c+z*2e-4,v=reflect(v,z)+(vec3(m(),m(),m())-.5)*.3;"
"}"
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/augmented.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
int foo(int x, int y) {
int a=x;
a=a+x;
a=a*y;
a=a>>x;
a=a^y;
a=a|y;
return a;
}

void main() {
foo(0, 0);
}
14 changes: 14 additions & 0 deletions tests/unit/augmented.frag.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int foo(int x,int y)
{
int a=x;
a+=x;
a*=y;
a>>=x;
a^=y;
a|=y;
return a;
}
void main()
{
foo(0,0);
}