Skip to content

Commit

Permalink
Reuse var (#367)
Browse files Browse the repository at this point in the history
Co-authored-by: Laurent Le Brun <laurentlb@gmail.com>
  • Loading branch information
eldritchconundrum and laurentlb authored May 2, 2024
1 parent c643d34 commit 27c0336
Show file tree
Hide file tree
Showing 25 changed files with 776 additions and 750 deletions.
8 changes: 4 additions & 4 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,10 @@ module private RewriterImpl =
| None -> Some []
| _ -> None
match declElts, (declElts |> List.rev) with
//| [declElt2], _ -> // float d1=f(); ...; float d2=g(); -> ...; d1=g();
// match findAssignmentReplacementFor declElt2 with
// | Some stmts -> Some stmts // Replace the Decl with the assignment. Largest win
// | None -> None
| [declElt2], _ -> // float d1=f(); ...; float d2=g(); -> ...; d1=g();
match findAssignmentReplacementFor declElt2 with
| Some stmts -> Some stmts // Replace the Decl with the assignment. Largest win
| None -> None
//| (declElt2 :: others), _ -> // float d1=f(); ...; float d2=g(),d3=h(); -> ...; d1=g(); float d3=h();
// match findAssignmentReplacementFor declElt2 with
// | Some stmts -> Some (stmts @ [Decl (ty2, others)]) // Keep the Decl, add an assignment before it.
Expand Down
34 changes: 17 additions & 17 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag... 8897 => 1528.145
mouton/mouton.vert... 17026 => 2457.862
audio-flight-v2.frag 4538 => 891.404
buoy.frag 4094 => 622.550
clod.frag... 8864 => 1522.094
mouton/mouton.vert... 16985 => 2441.964
audio-flight-v2.frag 4510 => 886.949
buoy.frag 4070 => 619.904
controllable-machinery.frag 7708 => 1220.329
ed-209.frag 7766 => 1341.089
ed-209.frag 7725 => 1336.319
elevated.hlsl 3405 => 603.218
endeavour.frag 2605 => 534.116
endeavour.frag 2591 => 531.307
from-the-seas-to-the-stars.frag 14252 => 2317.681
frozen-wasteland.frag 4578 => 806.452
kinder_painter.frag 2865 => 445.014
leizex.frag 2298 => 510.191
lunaquatic.frag 5247 => 1049.030
mandelbulb.frag 2370 => 538.322
frozen-wasteland.frag 4566 => 806.475
kinder_painter.frag 2847 => 442.771
leizex.frag 2276 => 509.507
lunaquatic.frag 5236 => 1044.110
mandelbulb.frag 2349 => 537.740
ohanami.frag 3256 => 722.517
orchard.frag 5537 => 1022.773
oscars_chair.frag 4651 => 986.364
robin.frag 6295 => 1052.336
slisesix.frag 4573 => 931.855
robin.frag 6268 => 1043.078
slisesix.frag 4562 => 925.463
terrarium.frag 3611 => 744.367
the_real_party_is_in_your_pocket.frag 12111 => 1794.687
valley_ball.glsl 4386 => 888.496
yx_long_way_from_home.frag 2947 => 606.406
Total: 135016 => 23615.203
the_real_party_is_in_your_pocket.frag 12101 => 1787.736
valley_ball.glsl 4335 => 891.345
yx_long_way_from_home.frag 2926 => 599.317
Total: 134631 => 23543.326
45 changes: 24 additions & 21 deletions tests/real/audio-flight-v2.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,26 @@ vec2 map(vec3 p,float sg)
float obj=fBox(o,vec3(.15*offWobble))-.015;
if(obj<res.x)
res=vec2(obj,11),g_hp=o;
float pid=floor((q.z+3.625)/7.25),trackmod=mod(pid,18.),deg=trackmod<12.?
trackmod<6.?
float pid=floor((q.z+3.625)/7.25);
pid=mod(pid,18.);
pid=pid<12.?
pid<6.?
4.:
6.:
10.;
pModPolar(q.xy,deg);
pModPolar(s.xy,deg*.5);
pModPolar(q.xy,pid);
pModPolar(s.xy,pid*.5);
vec3 r=s,fs=s-vec3(2.85,0,0);
r=vec3(abs(r.x),abs(r.y),r.z);
fs.z*=2.;
vec2 center=floor(fs.xz)+.5,neighbour=center+edge(fs.xz-center);
float chs=floor(center.y),height=texture(iChannel0,vec2(mod(chs,16.)*.0465,.1)).x;
height=smoothstep(.001,1.,height);
ga=height;
float ids=pMod(s.z,7.25);
vec3 qid=pMod(q,7.25);
float lq=mod(ids,2.);
iqd=qid.x;
pid=pMod(s.z,7.25);
o=pMod(q,7.25);
pid=mod(pid,2.);
iqd=o.x;
float zprs=mod(chs,tm<8.?
tm<4.?
tm<4.?
Expand All @@ -142,21 +144,21 @@ vec2 map(vec3 p,float sg)
floor(height*1.45)),d4a=length(r.xy-vec2(2.5,1.75))-.1,d4=length(r.xy-vec2(2.5,1.75))-.04+.027+.027*sin(r.z-time*4.5);
if(d4<res.x)
res=vec2(d4,12),g_hp=p;
vec2 d1=fragtail(q);
d1.x=max(d1.x,-d4a);
tun=fragtail(q);
tun.x=max(tun.x,-d4a);
s.z=abs(s.z);
float blt=sdCap(s-vec3(2.45,-.58,2.725));
if(lq<2.)
d1.x=min(blt,d1.x);
if(d1.x<res.x)
res=d1.xy,g_hp=p;
d4a=sdCap(s-vec3(2.45,-.58,2.725));
if(pid<2.)
tun.x=min(d4a,tun.x);
if(tun.x<res.x)
res=tun.xy,g_hp=p;
float me=fBox(fs-vec3(0,0,center.y),vec3(.05,.15+height,.25)),next=fBox(fs-vec3(0,0,neighbour.y),vec3(.05,.001+height,.25)),dlt=min(me,next);
if(dlt<res.x)
res=vec2(dlt,4),g_hp=p;
if(sg==1.)
beams+=1e-4/(3e-6+d4*d4);
if(sg==1.&&lq<1.)
flight+=.00025/(1e-7+blt*blt);
if(sg==1.&&pid<1.)
flight+=.00025/(1e-7+d4a*d4a);
if(sg==1.&&zprs<.1)
glow+=.00015/(2e-6+dlt*dlt);
if(sg==1.&&tm<13.25)
Expand Down Expand Up @@ -246,14 +248,15 @@ void mainImage(out vec4 O,vec2 F)
C+=abs(beams*.65)*hsv2rgb(vec3(s_hp.z*.025,.8,.6));
C+=abs(flight*.75)*vec3(.5,1,.2);
}
float px=1./R.x,d1=fBox2(uv+vec2(-.485,.2675),vec2(.005))-.002;
d1=smoothstep(px,-px,d1);
crop=1./R.x;
float d1=fBox2(uv+vec2(-.485,.2675),vec2(.005))-.002;
d1=smoothstep(crop,-crop,d1);
C=mix(C,vec3(.212,.671,.576),d1);
d1=fBox2(uv+vec2(-.465,.2675),vec2(.005))-.002;
d1=smoothstep(px,-px,d1);
d1=smoothstep(crop,-crop,d1);
C=mix(C,vec3(.757,.686,.341),d1);
d1=fBox2(uv+vec2(-.445,.2675),vec2(.005))-.002;
d1=smoothstep(px,-px,d1);
d1=smoothstep(crop,-crop,d1);
C=mix(C,vec3(.882,.459,.867),d1);
C=pow(C,vec3(.4545));
O=vec4(C,1);
Expand Down
34 changes: 18 additions & 16 deletions tests/real/buoy.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ vec4 Noise(vec2 x)
{
vec2 f=fract(x.xy);
f=f*f*(3.-2.*f);
vec2 uv=floor(x.xy).xy+f.xy;
return textureLod(iChannel0,(uv+.5)/256.,0.);
f=floor(x.xy).xy+f.xy;
return textureLod(iChannel0,(f+.5)/256.,0.);
}
vec4 Noise(ivec2 x)
{
Expand Down Expand Up @@ -143,11 +143,12 @@ vec3 ShadeBoat(vec3 pos,vec3 ray)
albedo=mix(mix(vec3(1),vec3(.04),smoothstep(-aa*4.,aa*4.,cos(atan(pos.x,pos.z)*6.))),albedo,smoothstep(.2-aa*1.5,.2,abs(pos.y)));
albedo=mix(vec3(.04),albedo,smoothstep(.05-aa,.05,abs(abs(pos.y)-.6)));
albedo=mix(vec3(1,.8,.08),albedo,smoothstep(.05-aa,.05,abs(abs(pos.y)-.65)));
vec3 col=albedo*(smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1)),specular=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1),rr=reflect(ray,norm);
albedo*=smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1);
vec3 specular=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1),rr=reflect(ray,norm);
specular+=mix(vec3(0,.04,.04),Sky(rr),smoothstep(-.1,.1,rr.y));
float fresnel=pow(1.-abs(dot(norm,ray)),5.);
fresnel=mix(.001,1.,fresnel);
return mix(col,specular,fresnel);
aa=pow(1.-abs(dot(norm,ray)),5.);
aa=mix(.001,1.,aa);
return mix(albedo,specular,aa);
}
float OceanDistanceField(vec3 pos)
{
Expand Down Expand Up @@ -184,18 +185,19 @@ vec3 ShadeOcean(vec3 pos,vec3 ray,vec2 fragCoord)
{
vec3 norm=OceanNormal(pos);
float ndotr=dot(ray,norm);
vec3 reflectedRay=ray-2.*norm*ndotr,refractedRay=ray+(-cos(1.33*acos(-ndotr))-ndotr)*norm;
refractedRay=normalize(refractedRay);
vec3 reflectedRay=ray-2.*norm*ndotr;
norm=ray+(-cos(1.33*acos(-ndotr))-ndotr)*norm;
norm=normalize(norm);
vec3 reflection=Sky(reflectedRay);
float t=TraceBoat(pos-0.*reflectedRay,reflectedRay);
if(t>0.)
reflection=ShadeBoat(pos+t*reflectedRay,reflectedRay);
t=TraceBoat(pos-0.*refractedRay,refractedRay);
vec3 col=vec3(0,.04,.04);
t=TraceBoat(pos-0.*norm,norm);
reflectedRay=vec3(0,.04,.04);
if(t>0.)
col=mix(col,ShadeBoat(pos+t*refractedRay,refractedRay),exp(-t));
col=mix(col,reflection,pow(1.-abs(ndotr),5.));
return mix(col,vec3(1),WaveCrests(pos,fragCoord));
reflectedRay=mix(reflectedRay,ShadeBoat(pos+t*norm,norm),exp(-t));
reflectedRay=mix(reflectedRay,reflection,pow(1.-abs(ndotr),5.));
return mix(reflectedRay,vec3(1),WaveCrests(pos,fragCoord));
}
void mainImage(out vec4 fragColor,vec2 fragCoord)
{
Expand All @@ -204,11 +206,11 @@ void mainImage(out vec4 fragColor,vec2 fragCoord)
vec3 pos,ray;
CamPolar(pos,ray,camRot,fragCoord);
float to=TraceOcean(pos,ray),tb=TraceBoat(pos,ray);
vec3 result=to>0.&&(to<tb||tb==0.)?
pos=to>0.&&(to<tb||tb==0.)?
ShadeOcean(pos+ray*to,ray,fragCoord):
tb>0.?
ShadeBoat(pos+ray*tb,ray):
Sky(ray);
result*=1.1*smoothstep(.35,1.,localRay.z);
fragColor=vec4(pow(result,vec3(1./GAMMA)),1);
pos*=1.1*smoothstep(.35,1.,localRay.z);
fragColor=vec4(pow(pos,vec3(1./GAMMA)),1);
}
36 changes: 18 additions & 18 deletions tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ MarchData headLower(vec3 p)
p=op;
p.y=abs(p.y+.16)-.06;
p.z-=-1.1;
float cheeks=max(sdCappedCylinder(p.xzy,1.,.03),-sdCappedCylinder(p.xzy,.55,1.));
cheeks=max(cheeks,p.z+.2);
r.d=max(r.d,-cheeks);
roof=max(sdCappedCylinder(p.xzy,1.,.03),-sdCappedCylinder(p.xzy,.55,1.));
roof=max(roof,p.z+.2);
r.d=max(r.d,-roof);
setBodyMaterial(r);
return r;
}
Expand All @@ -146,10 +146,10 @@ MarchData gunPod(vec3 p)
d=min(d,sdBox(p+vec3(0,0,.54),vec3(.1,.06,.04)));
if(d<r.d)
d=max(d,-sdCappedCylinder(pp+vec3(0,0,.1),.03,.2)),r.d=d,r.mat=vec3(.02);
float fs=fireShock();
if(fs>.5)
bump=fireShock();
if(bump>.5)
{
d=sdCappedCylinder(pp,.01+pp.z*.05,fract(fs*3322.423)*.5+.9);
d=sdCappedCylinder(pp,.01+pp.z*.05,fract(bump*3322.423)*.5+.9);
if(d<r.d)
r.d=d,r.mat=vec3(1),glow+=.1/(.01+d*d*4e2);
}
Expand Down Expand Up @@ -205,8 +205,8 @@ MarchData waist(vec3 p)
r.d=min(r.d,sdBox(pp,vec3(.24,.2,.14+.2*pp.y)));
p=pp;
pp.xz=abs(pp.xz)-vec2(.12,.25);
float pistons=min(sdCappedCylinder(pp.xzy,.1,.325),sdCappedCylinder(pp.xzy,.05,.5));
r.d=min(r.d,max(pistons,pp.y));
bump=min(sdCappedCylinder(pp.xzy,.1,.325),sdCappedCylinder(pp.xzy,.05,.5));
r.d=min(r.d,max(bump,pp.y));
p.y+=.68;
r.d=min(r.d,sdBox(p,vec3(sign(abs(p.y)-.04)*.005+.26,.2,.34)));
if(d<r.d)
Expand Down Expand Up @@ -234,14 +234,14 @@ MarchData legs(vec3 p)
pp.xz=abs(pp.xz)-vec2(.12,.25);
p.y+=.68;
p.xy=abs(p.xy)-.12;
float silver=sdBox(p,vec3(.07,.05,1.2));
legAngle=sdBox(p,vec3(.07,.05,1.2));
cp-=vec3(0,-.7,0);
r.d=sdBox(cp-vec3(0,0,1.15),vec3(.17,.17,.07))-.04;
cp.z+=1.;
r.d=min(r.d,sdChamferedCube(cp.xzy,vec2(.28-sign(abs(cp.z)-.3)*.01,.5).xyx));
r.d=min(r.d,foot(cp));
if(silver<r.d)
r.d=silver,r.mat=vec3(.8);
if(legAngle<r.d)
r.d=legAngle,r.mat=vec3(.8);
return r;
}
MarchData ed209(vec3 p)
Expand Down Expand Up @@ -284,12 +284,12 @@ MarchData room(vec3 p)
p.x-=frameInner.x;
p.xz*=rot(doorOpen*2.1);
p.x+=doorWidth;
float door=sdBox(p,vec3(doorWidth,frameInner.yz));
doorHole=sdBox(p,vec3(doorWidth,frameInner.yz));
p=abs(p)-vec3(doorWidth*.5,1.1,.14);
door=max(door,-max(sdBox(p,vec3(.45,.9,.1)),-sdBox(p,vec3(.35,.8,1))));
float d=min(doorFrame,door);
if(d<r.d)
r.d=d,r.mat=vec3(.02,.02,.024),r.specPower=10.;
doorHole=max(doorHole,-max(sdBox(p,vec3(.45,.9,.1)),-sdBox(p,vec3(.35,.8,1))));
doorFrame=min(doorFrame,doorHole);
if(doorFrame<r.d)
r.d=doorFrame,r.mat=vec3(.02,.02,.024),r.specPower=10.;
return r;
}
MarchData map(vec3 p)
Expand Down Expand Up @@ -331,8 +331,8 @@ vec3 applyLighting(vec3 p,vec3 rd,float d,MarchData data)
vec3 sunDir=normalize(sunPos-p),n=calcNormal(p,d);
float primary=max(0.,dot(sunDir,n));
primary*=mix(.2,1.,calcShadow(p,sunPos));
vec3 lig=((primary+max(0.,dot(-sunDir,n))*.2)*(map(p+.33*n).d/.33)+pow(max(0.,dot(rd,reflect(sunDir,n))),data.specPower)*2.)*vec3(2,1.6,1.7);
return mix(data.mat*lig,vec3(.01),smoothstep(.7,1.,1.+dot(rd,n)))*exp(-length(p)*.05);
sunDir=((primary+max(0.,dot(-sunDir,n))*.2)*(map(p+.33*n).d/.33)+pow(max(0.,dot(rd,reflect(sunDir,n))),data.specPower)*2.)*vec3(2,1.6,1.7);
return mix(data.mat*sunDir,vec3(.01),smoothstep(.7,1.,1.+dot(rd,n)))*exp(-length(p)*.05);
}
vec3 getSceneColor(vec3 ro,vec3 rd)
{
Expand Down
18 changes: 9 additions & 9 deletions tests/real/endeavour.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ void add(vec2 sda,vec2 sdb,out vec2 dst)
}
void dhexagonpattern(vec2 p,out float d,out vec2 ind)
{
vec2 q=vec2(p.x*1.2,p.y+p.x*.6),pi=floor(q),pf=fract(q);
vec2 q=vec2(p.x*1.2,p.y+p.x*.6),pi=floor(q);
q=fract(q);
float v=mod(pi.x+pi.y,3.),ca=step(1.,v),cb=step(2.,v);
vec2 ma=step(pf.xy,pf.yx);
d=dot(ma,1.-pf.yx+ca*(pf.x+pf.y-1.)+cb*(pf.yx-2.*pf.xy));
vec2 ma=step(q.xy,q.yx);
d=dot(ma,1.-q.yx+ca*(q.x+q.y-1.)+cb*(q.yx-2.*q.xy));
ind=pi+ca-cb*ma;
ind=vec2(ind.x/1.2,ind.y);
ind=vec2(ind.x,ind.y-ind.x*.6);
Expand All @@ -66,18 +67,17 @@ void scene(vec3 x,out vec2 sdf)
lfnoise(.5*x.y*c.xx,dis);
vec2 sdb,ya=abs(x.xz-.4*dis*c.xy)+.045*c.yx-.065*c.xy;
zextrude(x.y,-length(ya)+R,sdb.x);
float da;
stroke(sdb.x,.003,da);
stroke(sdb.x,.003,R);
sdb.y=2.;
vec2 ind;
dhexagonpattern(vec2(56,12)*vec2(x.y,atan(ya.y,ya.x)),dis,ind);
stroke(dis,.2,dis);
stroke(sdb.x,.003*step(dis,0.),sdb.x);
sdf.x=max(sdf.x,-da);
sdf.x=max(sdf.x,-R);
add(sdf,sdb,sdf);
float guard=-length(max(abs(mod(x,.1)-.05)-vec3(.05*c.xx,.6),0.));
guard=abs(guard)+.01;
sdf.x=min(sdf.x,guard);
R=-length(max(abs(mod(x,.1)-.05)-vec3(.05*c.xx,.6),0.));
R=abs(R)+.01;
sdf.x=min(sdf.x,R);
}
void normal(vec3 x,out vec3 n)
{
Expand Down
32 changes: 17 additions & 15 deletions tests/real/frozen-wasteland.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ vec3 Clouds(vec3 sky,vec3 rd)
rd.y=v;
rd.xz=rd.xz*v-time*8.;
rd.xz*=4e-4;
float f=Noise3d(rd.xzz*3.)*Noise3d(rd.zxx*1.3)*2.5;
f=f*pow(ele,.5)*2.;
f=clamp(f-.15,.01,1.);
return mix(sky,vec3(1),f);
v=Noise3d(rd.xzz*3.)*Noise3d(rd.zxx*1.3)*2.5;
v=v*pow(ele,.5)*2.;
v=clamp(v-.15,.01,1.);
return mix(sky,vec3(1),v);
}
vec3 Sky(vec3 rd,vec3 ligt)
{
Expand All @@ -249,29 +249,31 @@ void mainImage(out vec4 fragColor,vec2 fragCoord)
vec3 ro=vec3(smoothstep(0.,1.,tri(time*1.5)*.3)*1.5,smoothstep(0.,1.,tri(time*3.)*3.)*.08,-time*3.5-130.);
ro.y-=camHeight(ro.zx)-.4;
mo.x+=smoothstep(.7,1.,sin(time*.35))*.5-1.5-smoothstep(-.7,-1.,sin(time*.35))*.5;
vec3 eyedir=normalize(vec3(cos(mo.x),mo.y*2.-.05+sin(time*.5)*.1,sin(mo.x))),rightdir=normalize(vec3(cos(mo.x+1.5708),0,sin(mo.x+1.5708))),rd=normalize(p.x*rightdir+p.y*normalize(cross(rightdir,eyedir))+eyedir),ligt=normalize(vec3(1.5,.9,-.5));
float fg,rz=march(ro,rd,fg,fragCoord);
vec3 sky=Sky(rd,ligt),col=sky;
vec3 eyedir=normalize(vec3(cos(mo.x),mo.y*2.-.05+sin(time*.5)*.1,sin(mo.x))),rightdir=normalize(vec3(cos(mo.x+1.5708),0,sin(mo.x+1.5708)));
rightdir=normalize(p.x*rightdir+p.y*normalize(cross(rightdir,eyedir))+eyedir);
eyedir=normalize(vec3(1.5,.9,-.5));
float fg,rz=march(ro,rightdir,fg,fragCoord);
vec3 sky=Sky(rightdir,eyedir),col=sky;
if(rz<FAR)
{
vec3 pos=ro+rz*rd,nor=normal(pos,rz);
vec3 pos=ro+rz*rightdir,nor=normal(pos,rz);
float d=distance(pos,ro);
nor=bump(pos,nor,d);
float shd=shadow(pos,ligt),dif=clamp(dot(nor,ligt),0.,1.);
vec3 ref=reflect(rd,nor);
float fre=pow(clamp(1.+dot(rd,nor),0.,1.),3.);
float shd=shadow(pos,eyedir),dif=clamp(dot(nor,eyedir),0.,1.);
vec3 ref=reflect(rightdir,nor);
float fre=pow(clamp(1.+dot(rightdir,nor),0.,1.),3.);
col=vec3(.75);
col=col*dif*shd+pow(clamp(dot(ref,ligt),0.,1.),5.)*2.*fre*shd*SUN_COLOUR+abs(nor.y)*vec3(.12,.13,.13);
col=col*dif*shd+pow(clamp(dot(ref,eyedir),0.,1.),5.)*2.*fre*shd*SUN_COLOUR+abs(nor.y)*vec3(.12,.13,.13);
d=Occ(pos+nor*3.);
col*=vec3(d,d,min(d*1.2,1.));
col=mix(col,sky,smoothstep(FAR-25.,FAR,rz));
}
else
col=Clouds(col,rd);
col=Clouds(col,rightdir);
col=mix(col,vec3(.6,.65,.7),fg);
col=mix(col,vec3(.5),-.3);
col=sqrt(col);
float f=smoothstep(0.,3.,iTime)*.5;
col*=f+f*pow(70.*q.x*q.y*(1.-q.x)*(1.-q.y),.2);
fg=smoothstep(0.,3.,iTime)*.5;
col*=fg+fg*pow(70.*q.x*q.y*(1.-q.x)*(1.-q.y),.2);
fragColor=vec4(col,1);
}
Loading

0 comments on commit 27c0336

Please sign in to comment.