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

Support compound assignments in reassignment detection #384

Merged
merged 3 commits into from
May 11, 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
1 change: 1 addition & 0 deletions src/builtin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ let nonAssignOps = set [
"&"; "^"; "|"
"&&"; "^^"; "||"
]
let augmentableOperators = set ["+"; "-"; "*"; "/"; "%"; "<<"; ">>"; "&"; "^"; "|"]

let castFunctions = builtinTypes - set ["void"]
let trigonometryFunctions = set([
Expand Down
20 changes: 14 additions & 6 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ let rec private sideEffects = function

let rec private isPure e = sideEffects e = []

let private desugarCompoundAssignOp = function
| Expr (FunCall (Op op, [Var name; init])) as s
when Builtin.assignOps.Contains op ->
let baseOp = op.TrimEnd('=')
if not (Builtin.augmentableOperators.Contains baseOp)
then s
else let init2 = FunCall (Op baseOp, [Var name; init])
Expr (FunCall (Op "=", [Var name; init2]))
| s -> s

module private RewriterImpl =

// Remove useless spaces in macros
Expand Down Expand Up @@ -114,9 +124,7 @@ module private RewriterImpl =
| Float (f, _) -> Some f
| _ -> None

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



let simplifyOperator env = function
| FunCall(Op "-", [Int (i1, su)]) -> Int (-i1, su)
| FunCall(Op "-", [FunCall(Op "-", [e])]) -> e
Expand Down Expand Up @@ -221,7 +229,7 @@ module private RewriterImpl =
| FunCall(Op "=", [Var x; Var y]) when x.Name = y.Name -> Var y
// x=x+... -> x+=...
| FunCall(Op "=", [Var x; FunCall(Op op, [Var y; e])])
when x.Name = y.Name && augmentableOperators.Contains op ->
when x.Name = y.Name && Builtin.augmentableOperators.Contains op ->
FunCall(Op (op + "="), [Var x; e])

// x=...+x -> x+=...
Expand Down Expand Up @@ -533,9 +541,9 @@ module private RewriterImpl =
mapExpr (mapEnvExpr visitAndReplace) expr

// Merge two consecutive items into one, everywhere possible in a list.
let rec squeeze (f : 'a * 'a -> 'a list option) = function
let rec squeeze (f : Stmt * Stmt -> Stmt list option) = function
| h1 :: h2 :: t ->
match f (h1, h2) with
match f (desugarCompoundAssignOp h1, desugarCompoundAssignOp h2) with
| Some xs -> squeeze f (xs @ t)
| None -> h1 :: (squeeze f (h2 :: t))
| h :: t -> h :: t
Expand Down
40 changes: 20 additions & 20 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag... 8767 => 1496.872
mouton/mouton.vert... 16840 => 2429.241
audio-flight-v2.frag 4497 => 881.523
buoy.frag 4021 => 603.502
clod.frag... 8760 => 1495.202
mouton/mouton.vert... 16770 => 2417.200
audio-flight-v2.frag 4486 => 878.312
buoy.frag 4013 => 605.720
controllable-machinery.frag 7683 => 1225.947
ed-209.frag 7673 => 1338.691
elevated.hlsl 3400 => 602.269
ed-209.frag 7672 => 1338.529
elevated.hlsl 3401 => 602.542
endeavour.frag 2568 => 529.742
from-the-seas-to-the-stars.frag 14243 => 2308.126
frozen-wasteland.frag 4542 => 804.865
kinder_painter.frag 2841 => 442.120
leizex.frag 2253 => 508.647
lunaquatic.frag 5226 => 1042.965
mandelbulb.frag 2330 => 534.927
ohanami.frag 3248 => 721.439
orchard.frag 5479 => 1019.336
oscars_chair.frag 4651 => 986.364
robin.frag 6214 => 1040.149
slisesix.frag 4503 => 895.918
terrarium.frag 3585 => 744.519
the_real_party_is_in_your_pocket.frag 11999 => 1781.926
from-the-seas-to-the-stars.frag 14240 => 2306.531
frozen-wasteland.frag 4517 => 803.422
kinder_painter.frag 2836 => 442.689
leizex.frag 2252 => 507.685
lunaquatic.frag 5222 => 1043.813
mandelbulb.frag 2325 => 534.901
ohanami.frag 3246 => 721.169
orchard.frag 5393 => 1002.991
oscars_chair.frag 4648 => 986.069
robin.frag 6204 => 1039.733
slisesix.frag 4497 => 895.900
terrarium.frag 3575 => 750.087
the_real_party_is_in_your_pocket.frag 11986 => 1779.232
valley_ball.glsl 4307 => 881.820
yx_long_way_from_home.frag 2938 => 599.331
Total: 133808 => 23420.237
Total: 133539 => 23388.566
9 changes: 3 additions & 6 deletions tests/real/audio-flight-v2.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ void mainImage(out vec4 O,vec2 F)
s_hp=g_hp;
if(d<MAXDIST)
{
vec3 p=ro+rd*d,n=normal(p,d),lpos=vec3(0,0,.25)-p;
lpos+=lp;
vec3 p=ro+rd*d,n=normal(p,d),lpos=vec3(0,0,.25)-p+lp;
lpos.xy+=path(lpos.z);
vec3 l=normalize(lpos);
float diff=clamp(dot(n,l),.01,1.),spec=pow(max(dot(reflect(l,n),rd),.01),24.);
Expand All @@ -246,10 +245,8 @@ void mainImage(out vec4 O,vec2 F)
FC=vec3(.8);
}
else
C+=abs(glow*.7)*hsv2rgb(vec3(s_hp.z*.01,.8,.6)),C+=abs(objglow*.65)*vec3(1);
C=mix(C,FC,1.-exp(-7.5e-5*t.x*t.x*t.x));
C+=abs(beams*.65)*hsv2rgb(vec3(s_hp.z*.025,.8,.6));
C+=abs(flight*.75)*vec3(.5,1,.2);
C=C+abs(glow*.7)*hsv2rgb(vec3(s_hp.z*.01,.8,.6))+abs(objglow*.65)*vec3(1);
C=mix(C,FC,1.-exp(-7.5e-5*t.x*t.x*t.x))+abs(beams*.65)*hsv2rgb(vec3(s_hp.z*.025,.8,.6))+abs(flight*.75)*vec3(.5,1,.2);
}
crop=1./R.x;
float d1=fBox2(uv+vec2(-.485,.2675),vec2(.005))-.002;
Expand Down
10 changes: 4 additions & 6 deletions tests/real/buoy.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ float WavesSmooth(vec3 pos)
}
float WaveCrests(vec3 ipos,vec2 fragCoord)
{
ipos*=.2*vec3(1);
ipos=.2*vec3(1)*ipos;
float f=0.;
ipos+=iTime*vec3(0,.1,.1);
vec3 pos2=ipos;
Expand Down Expand Up @@ -122,9 +122,8 @@ vec3 WorldToBoat(vec3 dir)
}
float TraceBoat(vec3 pos,vec3 ray)
{
vec3 c=boatPosition;
c-=pos;
float t=dot(c,ray),p=length(c-t*ray);
pos=boatPosition-pos;
float t=dot(pos,ray),p=length(pos-t*ray);
return p>1.?
0.:
t-sqrt(1.-p*p);
Expand All @@ -136,8 +135,7 @@ vec3 ShadeBoat(vec3 pos,vec3 ray)
pos=WorldToBoat(pos);
vec3 lightDir=normalize(vec3(-2,3,1));
float aa=4./iResolution.x;
pos=mix(vec3(1,.8,.08),mix(vec3(.04),mix(mix(vec3(1),vec3(.04),smoothstep(-aa*4.,aa*4.,cos(atan(pos.x,pos.z)*6.))),mix(vec3(.04),vec3(1,.01,0),smoothstep(.25-aa,.25,abs(pos.y))),smoothstep(.2-aa*1.5,.2,abs(pos.y))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.6))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.65)));
pos*=smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1);
pos=mix(vec3(1,.8,.08),mix(vec3(.04),mix(mix(vec3(1),vec3(.04),smoothstep(-aa*4.,aa*4.,cos(atan(pos.x,pos.z)*6.))),mix(vec3(.04),vec3(1,.01,0),smoothstep(.25-aa,.25,abs(pos.y))),smoothstep(.2-aa*1.5,.2,abs(pos.y))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.6))),smoothstep(.05-aa,.05,abs(abs(pos.y)-.65)))*(smoothstep(-.1,1.,dot(norm,lightDir))*vec3(1,.9,.8)+vec3(.06,.1,.1));
lightDir=pow(max(0.,dot(norm,normalize(lightDir-ray))),1e2)*1e2/32.*vec3(1);
vec3 rr=reflect(ray,norm);
lightDir+=mix(vec3(0,.04,.04),Sky(rr),smoothstep(-.1,.1,rr.y));
Expand Down
40 changes: 18 additions & 22 deletions tests/real/chocolux.expected
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
// Generated with (https://github.com/laurentlb/Shader_Minifier/)
#ifndef CHOCOLUX_EXPECTED_
# define CHOCOLUX_EXPECTED_
# define VAR_resolution "k"
# define VAR_resolution "d"
# define VAR_time "v"

const char *chocolux_frag =
"uniform vec2 k;"
"uniform vec2 d;"
"uniform float v;"
"float s(vec3 y)"
"float s(vec3 d)"
"{"
"float f=distance(y,vec3(cos(v)+sin(v*.2),.3,2.+cos(v*.5)*.5));"
"f*=distance(y,vec3(-cos(v*.7),.3,2.+sin(v*.5)));"
"f*=distance(y,vec3(-sin(v*.2)*.5,sin(v),2));"
"f*=cos(y.y)*cos(y.x)-.1-cos(y.z*7.+v*7.)*cos(y.x*3.)*cos(y.y*4.)*.1;"
"return f;"
"return distance(d,vec3(cos(v)+sin(v*.2),.3,2.+cos(v*.5)*.5))*distance(d,vec3(-cos(v*.7),.3,2.+sin(v*.5)))*distance(d,vec3(-sin(v*.2)*.5,sin(v),2))*(cos(d.y)*cos(d.x)-.1-cos(d.z*7.+v*7.)*cos(d.x*3.)*cos(d.y*4.)*.1);"
"}"
"void main()"
"{"
"vec2 y=-1.+2.*gl_FragCoord.xy/k.xy;"
"vec3 f=vec3(y.x,y.y*1.25-.3,0),c=vec3(y.x+cos(v)*.3,y.y,1)/64.;"
"vec4 d=vec4(0);"
"vec2 c=-1.+2.*gl_FragCoord.xy/d.xy;"
"vec3 i=vec3(c.x,c.y*1.25-.3,0),y=vec3(c.x+cos(v)*.3,c.y,1)/64.;"
"vec4 f=vec4(0);"
"float x=0.;"
"for(int r=0;r<75;r++)"
"for(int b=0;b<75;b++)"
"{"
"if(s(f+c*x)<.4)"
"if(s(i+y*x)<.4)"
"{"
"x-=5.;"
"for(int b=0;b<5;b++)"
"for(int m=0;m<5;m++)"
"{"
"if(s(f+c*x)<.4)"
"if(s(i+y*x)<.4)"
"break;"
"x+=1.;"
"}"
"vec3 b=vec3(.01,0,0),i=vec3(0);"
"i.x=s(f+c*x)-s(vec3(f+c*x+b.xyy));"
"i.y=s(f+c*x)-s(vec3(f+c*x+b.yxy));"
"i.z=s(f+c*x)-s(vec3(f+c*x+b.yyx));"
"i=normalize(i);"
"d+=max(dot(vec3(0,0,-.5),i),0.)+max(dot(vec3(0,-.5,.5),i),0.)*.5;"
"vec3 m=vec3(.01,0,0),r=vec3(0);"
"r.x=s(i+y*x)-s(vec3(i+y*x+m.xyy));"
"r.y=s(i+y*x)-s(vec3(i+y*x+m.yxy));"
"r.z=s(i+y*x)-s(vec3(i+y*x+m.yyx));"
"r=normalize(r);"
"f+=max(dot(vec3(0,0,-.5),r),0.)+max(dot(vec3(0,-.5,.5),r),0.)*.5;"
"break;"
"}"
"x+=5.;"
"}"
"gl_FragColor=d+x*.025*vec4(.1,.2,.5,1);"
"gl_FragColor=f+x*.025*vec4(.1,.2,.5,1);"
"}";

#endif // CHOCOLUX_EXPECTED_
3 changes: 1 addition & 2 deletions tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ vec3 calcNormal(vec3 p,float t)
vec3 vignette(vec3 col,vec2 fragCoord)
{
fragCoord=fragCoord.xy/iResolution.xy;
col*=.5+.5*pow(16.*fragCoord.x*fragCoord.y*(1.-fragCoord.x)*(1.-fragCoord.y),.4);
return col;
return col*(.5+.5*pow(16.*fragCoord.x*fragCoord.y*(1.-fragCoord.x)*(1.-fragCoord.y),.4));
}
vec3 applyLighting(vec3 p,vec3 rd,float d,MarchData data)
{
Expand Down
6 changes: 2 additions & 4 deletions tests/real/elevated.hlsl.expected
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ const char *elevated_hlsl =
"r+=pow(1-mul(-x,d),4)*(pow(mul(t[3],reflect(-x,d)),32)*float3(.32,.31,.3)+.1);"
"r=lerp(r,l(y,d,d),smoothstep(1,0,t[2].x+p*60-D(666*y.xz+saturate(p*60)*float2(t[3].w,0)*2,5))*.5);"
"}"
"r*=.7+.3*smoothstep(0,1,256*abs(p));"
"r*=exp(-.042*c);"
"r=r*(.7+.3*smoothstep(0,1,256*abs(p)))*exp(-.042*c);"
"r+=(1-exp(-.1*c))*(float3(.52,.59,.65)+pow(saturate(mul(x,t[3])),8)*float3(.6,.4,.1));"
"}"
"return float4(r,0);"
Expand All @@ -118,8 +117,7 @@ const char *elevated_hlsl =
"w.x+=tex2D(y,s+p*(.5+.5*r.xy/r.w-s)/16+float2(2,0)/1280).x,w.y+=tex2D(y,s+p*(.5+.5*r.xy/r.w-s)/16+float2(0,0)/1280).y,w.z+=tex2D(y,s+p*(.5+.5*r.xy/r.w-s)/16+float2(-2,0)/1280).z;"
"w/=16;"
"}"
"w=pow(w,.45)*t[2].z+t[2].y;"
"w*=.4+9.6*s.x*s.y*(1-s.x)*(1-s.y);"
"w=(pow(w,.45)*t[2].z+t[2].y)*(.4+9.6*s.x*s.y*(1-s.x)*(1-s.y));"
"w.xz*=.98;"
"float c=tex2D(f,t[3].w*.1);"
"s+=c;"
Expand Down
3 changes: 1 addition & 2 deletions tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ void ammonite()
bool jellyfish()
{
col=vec3(.2,.4,1)/3+pow(w.w,8);
float to=(of.x+of.y*8)*.3;
to+=time/2;
float to=(of.x+of.y*8)*.3+time/2;
w.x=R();
w.y=R();
w.z=R();
Expand Down
16 changes: 5 additions & 11 deletions tests/real/frozen-wasteland.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@

float height(vec2 p)
{
float h=sin(p.x*.1+p.y*.2)+sin(p.y*.1-p.x*.2)*.5;
h+=sin(p.x*.04+p.y*.01+3.)*4.;
h-=sin(h*10.)*.1;
return h;
float h=sin(p.x*.1+p.y*.2)+sin(p.y*.1-p.x*.2)*.5+sin(p.x*.04+p.y*.01+3.)*4.;
return h-sin(h*10.)*.1;
}
float camHeight(vec2 p)
{
float h=sin(p.x*.1+p.y*.2)+sin(p.y*.1-p.x*.2)*.5;
h+=sin(p.x*.04+p.y*.01+3.)*4.;
return h;
return sin(p.x*.1+p.y*.2)+sin(p.y*.1-p.x*.2)*.5+sin(p.x*.04+p.y*.01+3.)*4.;
}
float smin(float a,float b)
{
Expand Down Expand Up @@ -154,8 +150,7 @@ float map(vec3 p)
float d=smin(smin(p.y+.5,vine(p+vec3(.8,0,0),30.,3.3)),vine(p.zyx+vec3(0,0,17),33.,1.4));
d+=p.y*1.2*Noise3d(p*.05);
p.xz*=.3;
d+=Noise3d(p*.3);
return d;
return d+Noise3d(p*.3);
}
float fogmap(vec3 p,float d)
{
Expand Down Expand Up @@ -261,8 +256,7 @@ void mainImage(out vec4 fragColor,vec2 fragCoord)
float fre=pow(clamp(1.+dot(rightdir,nor),0.,1.),3.);
col=vec3(.75)*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));
col=mix(col*vec3(d,d,min(d*1.2,1.)),sky,smoothstep(FAR-25.,FAR,rz));
}
else
col=Clouds(col,rightdir);
Expand Down
66 changes: 32 additions & 34 deletions tests/real/kinder_painter.expected
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@
"b=(-dot(v.xyz,z)-v.w)/(sign(b)*max(abs(b),.001));"
"return b-.001;"
"}"
"vec3 n(vec4 v,vec4 z,vec3 m,out vec2 r)"
"vec3 n(vec4 v,vec4 m,vec3 r,out vec2 b)"
"{"
"vec3 b;"
"r=z.w>2.5?"
"(b.xz=m.xz-v.xz,b.y=0.,b/=v.w,vec2(b.x,m.y)):"
"z.w>1.5?"
"(b=v.xyz,m.xz*.2):"
"(b=m-v.xyz,b/=v.w,b.xy);"
"return b;"
"vec3 z;"
"b=m.w>2.5?"
"(z.xz=r.xz-v.xz,z.y=0.,z/=v.w,vec2(z.x,r.y)):"
"m.w>1.5?"
"(z=v.xyz,r.xz*.2):"
"(z=(r-v.xyz)/v.w,z.xy);"
"return z;"
"}"
"vec4 m(vec4 z,vec4 v,bool r)"
"{"
Expand Down Expand Up @@ -125,41 +125,39 @@
"}"
"bool x(vec3 v,vec3 z,float i)"
"{"
"bvec4 b;"
"b.x=d(r[0],v,z,i);"
"b.y=d(r[1],v,z,i);"
"b.z=t(r[2],v,z,i);"
"b.w=t(r[3],v,z,i);"
"return any(b);"
"bvec4 w;"
"w.x=d(r[0],v,z,i);"
"w.y=d(r[1],v,z,i);"
"w.z=t(r[2],v,z,i);"
"w.w=t(r[3],v,z,i);"
"return any(w);"
"}"
"vec4 d(vec3 v,vec4 z,vec4 b,vec3 r,vec4 i,out vec4 f)"
"vec4 d(vec3 v,vec4 z,vec4 r,vec3 w,vec4 i,out vec4 b)"
"{"
"vec3 w;"
"float m,o;"
"vec2 d;"
"w=n(z,b,v,d);"
"m=dot(w,i.xyz);"
"f.xyz=reflect(r,w);"
"o=max(dot(f.xyz,i.xyz),0.);"
"vec3 f;"
"float d,o;"
"vec2 m;"
"f=n(z,r,v,m);"
"d=dot(f,i.xyz);"
"b.xyz=reflect(w,f);"
"o=max(dot(b.xyz,i.xyz),0.);"
"o*=o;"
"o*=o;"
"if(x(v,i.xyz,i.w))"
"m=0.;"
"b*=texture2D(y,d);"
"m=max(m,0.);"
"b=b*(vec4(.3,.34,.38,1)+.5*vec4(1,.95,.8,1)*m)+.5*o;"
"m=dot(w,-r);"
"f.w=m;"
"m=1.-m*m;"
"m*=m;"
"b+=.35*vec4(m);"
"return b;"
"d=0.;"
"r*=texture2D(y,m);"
"d=max(d,0.);"
"r=r*(vec4(.3,.34,.38,1)+.5*vec4(1,.95,.8,1)*d)+.5*o;"
"d=dot(f,-w);"
"b.w=d;"
"d=1.-d*d;"
"d*=d;"
"return r+.35*vec4(d);"
"}"
"void main()"
"{"
"vec4 f,i,w,o;"
"vec2 y=-1.+2.*gl_FragCoord.xy/v.xy;"
"y*=vec2(v.x/v.y,1);"
"vec2 y=(-1.+2.*gl_FragCoord.xy/v.xy)*vec2(v.x/v.y,1);"
"r[0]=vec4(1.2*sin(2.073423*z),0,1.8*sin(2.450409*z+1.),1);"
"r[1]=vec4(1.5*sin(1.947761*z+4.),sin(1.822099*z+1.9),1.8*sin(1.822099*z),1);"
"r[2]=vec4(-1.2,0,0,.4);"
Expand Down
Loading