Skip to content

Commit

Permalink
Augmentable operators optimization: * is not always commutative (#372)
Browse files Browse the repository at this point in the history
This is a fix to an issue found in #369.
Thanks to @therontarigo!
  • Loading branch information
laurentlb authored May 5, 2024
1 parent 5b8f945 commit cccfcd7
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ module private RewriterImpl =

// a=a; -> a
| FunCall(Op "=", [Var x; Var y]) when x.Name = y.Name -> Var y
// Match: x = x + ...
// x=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])

// Match: x = ... + x
// works only if the operator is commutative
| FunCall(Op "=", [Var x; FunCall(Op ("+"|"*"|"&"|"^"|"|" as op), [e; Var y])])
// x=...+x -> x+=...
// Works only if the operator is commutative. * is not commutative with vectors and matrices.
| FunCall(Op "=", [Var x; FunCall(Op ("+"|"&"|"^"|"|" as op), [e; Var y])])
when x.Name = y.Name ->
FunCall(Op (op + "="), [Var x; e])

Expand Down Expand Up @@ -370,12 +370,12 @@ module private RewriterImpl =

List.collect replacements stmts

// Squeeze declarations: "float a=2.; float b;" => "float a=2.,b;"
// Squeeze declarations: "float a=2.; float b;" -> "float a=2.,b;"
let rec squeezeConsecutiveDeclarations = function
| []-> []
| Decl(ty1, li1) :: Decl(ty2, li2) :: l when ty1 = ty2 ->
squeezeConsecutiveDeclarations (Decl(ty1, li1 @ li2) :: l)
// int a=2; for (int b=3; ...) -> int a=2, b=3; for (; ...)
// int a=2; for (int b=3; ...) -> int a=2, b=3; for (; ...)
| Decl(ty1, li1) :: ForD((ty2, li2), cond, inc, body) :: l when ty1 = ty2 ->
squeezeConsecutiveDeclarations (Decl(ty1, li1 @ li2) :: ForE(None, cond, inc, body) :: l)
| e::l -> e :: squeezeConsecutiveDeclarations l
Expand Down
12 changes: 6 additions & 6 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag... 8836 => 1515.830
mouton/mouton.vert... 16951 => 2447.462
audio-flight-v2.frag 4509 => 881.311
mouton/mouton.vert... 16952 => 2447.464
audio-flight-v2.frag 4510 => 881.310
buoy.frag 4051 => 607.428
controllable-machinery.frag 7703 => 1216.907
ed-209.frag 7697 => 1340.743
elevated.hlsl 3400 => 602.269
endeavour.frag 2584 => 532.679
from-the-seas-to-the-stars.frag 14250 => 2308.842
from-the-seas-to-the-stars.frag 14251 => 2308.886
frozen-wasteland.frag 4566 => 806.475
kinder_painter.frag 2847 => 442.771
leizex.frag 2270 => 510.375
lunaquatic.frag 5234 => 1043.382
mandelbulb.frag 2342 => 537.655
ohanami.frag 3255 => 723.732
orchard.frag 5505 => 1027.273
ohanami.frag 3256 => 723.324
orchard.frag 5506 => 1027.274
oscars_chair.frag 4651 => 986.364
robin.frag 6254 => 1047.419
slisesix.frag 4549 => 913.642
terrarium.frag 3593 => 741.557
the_real_party_is_in_your_pocket.frag 12087 => 1780.714
valley_ball.glsl 4307 => 881.820
yx_long_way_from_home.frag 2942 => 598.650
Total: 134383 => 23495.298
Total: 134388 => 23494.936
2 changes: 1 addition & 1 deletion tests/real/audio-flight-v2.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ vec2 marcher(vec3 ro,vec3 rd)
}
vec3 normal(vec3 p,float t)
{
t*=MINDIST;
t=MINDIST*t;
vec2 h=vec2(1,-1)*.5773;
return normalize(h.xyy*map(p+h.xyy*t,0.).x+h.yyx*map(p+h.yyx*t,0.).x+h.yxy*map(p+h.yxy*t,0.).x+h.xxx*map(p+h.xxx*t,0.).x);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ void main()
p.y+=.5;
vec3 r=normalize(camtarget-campos),s=normalize(cross(vec3(0,1,0),r)),t=cross(s,r);
p-=campos;
p*=transpose(mat3(s,-t,r));
p=transpose(mat3(s,-t,r))*p;
p.xy*=rotmat(sin(time/2)*.05*cameraroll+cameraroll2);
p.y+=(fbm(vec2(time/16))-.5)*.1*cameraroll;
p.x+=(fbm(vec2(time/14+10))-.5)*.1*cameraroll;
Expand Down
2 changes: 1 addition & 1 deletion tests/real/mouton/mouton.expected
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ const char *mouton_frag =
"b=mix(b,mix(mix(vec3(1,.5,0),vec3(.8,.5,1),(cos(Y*5.+t*5.)*.5+.5)*a.y),vec3(1),smoothstep(-Y,Y,q)),a.x);"
"}"
"l=vec2(abs(f.x*5.-.35)-1.8,f.y*5.-1.4);"
"l*=E(t*5.);"
"l=E(t*5.)*l;"
"G=(1.4+.2*sin(t*20.))*smoothstep(.5,1.,a.x);"
"O=J(l,G);"
"P=mix(vec3(1,.6,0),vec3(1,.2,0),smoothstep(-.1,.6,J(l,G*.5)))*1.3;"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/ohanami.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void main()
if(ti==10)
ro.y+=2;
vec3 w=normalize(vec3(0,1.3,0)-ro),u=normalize(cross(w,vec3(0,1,0)));
rd*=mat3(u,normalize(cross(w,-u)),w);
rd=mat3(u,normalize(cross(w,-u)),w)*rd;
gl_FragColor.xyz=vec3(.8,.8,1)/6;
float t=0.,d=0.;
for(int i=0;i<100;++i)
Expand Down
2 changes: 1 addition & 1 deletion tests/real/orchard.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void mainImage(out vec4 fragColour,vec2 fragCoord)
#endif

vec3 seedDir=normalize(vec3(0,0,1));
seedDir*=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5);
seedDir=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5)*seedDir;
vec3 rd=seedDir,col=vec3(0),sky=mix(vec3(FOG_COLOUR),vec3(0,.4,.6),abs(rd.y));
float alpha=marchScene(camera,rd,fragCoord);
vec4 mat;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/inline.no.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ float result;
void main()
{
float x=.5;
x*=.6*x;
x=.6*x*x;
result=x;
}
int arithmetic()
Expand Down

0 comments on commit cccfcd7

Please sign in to comment.