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

Detect more augmentable operators #369

Merged
merged 1 commit into from
May 4, 2024
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
7 changes: 7 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ module private RewriterImpl =
| 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])])
when x.Name = y.Name ->
FunCall(Op (op + "="), [Var x; e])

// Unsafe when x contains NaN or Inf values.
//| FunCall(Op "=", [Var x; FunCall(Var fctName, [Int (0, _)])])
// when List.contains fctName.Name ["vec2"; "vec3"; "vec4"; "ivec2"; "ivec3"; "ivec4"] ->
Expand Down
20 changes: 10 additions & 10 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag... 8844 => 1513.863
mouton/mouton.vert... 16973 => 2441.227
clod.frag... 8842 => 1513.669
mouton/mouton.vert... 16972 => 2439.859
audio-flight-v2.frag 4526 => 883.304
buoy.frag 4070 => 619.904
controllable-machinery.frag 7708 => 1220.329
ed-209.frag 7734 => 1333.865
elevated.hlsl 3405 => 603.218
endeavour.frag 2589 => 529.811
from-the-seas-to-the-stars.frag 14252 => 2316.095
from-the-seas-to-the-stars.frag 14250 => 2312.836
frozen-wasteland.frag 4566 => 806.475
kinder_painter.frag 2847 => 442.771
leizex.frag 2276 => 509.507
lunaquatic.frag 5236 => 1044.110
leizex.frag 2275 => 509.430
lunaquatic.frag 5235 => 1044.000
mandelbulb.frag 2347 => 537.664
ohanami.frag 3256 => 722.517
orchard.frag 5537 => 1022.773
ohanami.frag 3255 => 723.026
orchard.frag 5536 => 1022.550
oscars_chair.frag 4651 => 986.364
robin.frag 6268 => 1043.078
slisesix.frag 4550 => 914.573
slisesix.frag 4549 => 913.642
terrarium.frag 3611 => 744.367
the_real_party_is_in_your_pocket.frag 12101 => 1787.736
valley_ball.glsl 4335 => 891.345
valley_ball.glsl 4334 => 890.606
yx_long_way_from_home.frag 2926 => 599.317
Total: 134608 => 23514.210
Total: 134597 => 23507.820
4 changes: 2 additions & 2 deletions tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool jellyfish()
{
col=vec3(.2,.4,1)/3+pow(w.w,8);
float to=(of.x+of.y*8)*.3;
to=time/2+to;
to+=time/2;
w.x=R();
w.y=R();
w.z=R();
Expand Down Expand Up @@ -758,7 +758,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;
p*=transpose(mat3(s,-t,r));
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/leizex.expected
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _leizex_frag:
db 'uniform sampler2D f,m,o,y;'
db 'float t(int v)'
db '{'
db 'v=v<<13^v;'
db 'v^=v<<13;'
db 'v=v*(v*v*15731+789221)+1376312589&2147483647;'
db 'return float(v);'
db '}'
Expand Down
2 changes: 1 addition & 1 deletion tests/real/lunaquatic.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ vec3 rotateX(vec3 v,float x)
float rnd(vec2 x)
{
int n=int(x.x*40.+x.y*6400.);
n=n<<13^n;
n^=n<<13;
return 1.-float(n*(n*n*15731+789221)+1376312589&2147483647)/1073741824.;
}
float norm(float x)
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 @@ -761,7 +761,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;"
"l*=E(t*5.);"
"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 @@ -57,7 +57,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;
rd*=mat3(u,normalize(cross(w,-u)),w);
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;
seedDir*=viewMat(uv.y+sin(time*.5)*.4,uv.x+time*.5);
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/real/slisesix.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform float time;
uniform sampler2D tex0;
int icoolfFunc3d2(int n)
{
n=n<<13^n;
n^=n<<13;
return n*(n*n*15731+789221)+1376312589&2147483647;
}
float coolfFunc3d2(int n)
Expand Down
2 changes: 1 addition & 1 deletion tests/real/valley_ball.glsl.expected
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ float sphereRadius,gf_DetailLevel,pi,eps,bigeps;
float rnd(vec2 x)
{
int n=int(x.x*40.+x.y*6400.);
n=n<<13^n;
n^=n<<13;
return 1.-float(n*(n*n*15731+789221)+1376312589&2147483647)/1073741824.;
}
float smoothrnd(vec2 x)
Expand Down
4 changes: 2 additions & 2 deletions 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;
x*=.6*x;
result=x;
}
int arithmetic()
Expand All @@ -18,7 +18,7 @@ int vars(int arg,int arg2)
int arithmetic2()
{
int a=2,b=3;
b=a+b;
b+=a;
return 4*a*b;
}
int unusedVars()
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/reuse-var.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ float simple_var_decl_reuse(float x)
int b2=5+int(a);
float c=a;
sep+=vec3(0);
a=9.+a;
a+=9.;
int d=3+b+b2;
sep+=vec3(0);
b2=d+b2-b;
Expand All @@ -28,15 +28,15 @@ float multidecl_var_decl_reuse(float x)
float a=1.+x;
int b=3+int(x),b2=5+int(a);
float c=a;
a=9.+a;
a+=9.;
int d=3+b+b2;
b=d+b2-b;
float e=c*a;
c=4.-c-a;
b2=3*d*b;
d=4/b-d;
a=e-float(b2)+c;
e=4.-a+c+e;
e+=4.-a+c;
b=3*b2-d;
d=7*d-b2;
return float(b*d)*a*e*x+e/float(d-b);
Expand Down