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

Shadow variables inside loops and if statements #387

Merged
merged 6 commits into from
May 12, 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
2 changes: 1 addition & 1 deletion Shader Minifier Library.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<Compile Include="src\ast.fs" />
<Compile Include="src\printer.fs" />
<Compile Include="src\formatter.fs" />
<Compile Include="src\renamer.fs" />
<Compile Include="src\analyzer.fs" />
<Compile Include="src\renamer.fs" />
laurentlb marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="src\rewriter.fs" />
<Compile Include="src\preprocessor.fs" />
<Compile Include="src\parse.fs" />
Expand Down
2 changes: 1 addition & 1 deletion shader-minifier-linux.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<Compile Include="src\ast.fs" />
<Compile Include="src\printer.fs" />
<Compile Include="src\formatter.fs" />
<Compile Include="src\renamer.fs" />
<Compile Include="src\analyzer.fs" />
<Compile Include="src\renamer.fs" />
<Compile Include="src\rewriter.fs" />
<Compile Include="src\preprocessor.fs" />
<Compile Include="src\parse.fs" />
Expand Down
54 changes: 26 additions & 28 deletions src/renamer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,16 @@ module private RenamerImpl =
// so that we can reuse them. In other words, this function allows us
// to shadow global variables in a function.
let shadowVariables (env: Env) block =
let d = HashSet()
let collect mEnv = function
| Var id as e ->
if not (mEnv.vars.ContainsKey(id.Name)) then d.Add id.Name |> ignore<bool>
e
| FunCall(Var id, li) as e ->
match env.funRenames.TryFind id.Name with
| Some m -> if not (m.ContainsKey li.Length) then d.Add id.Name |> ignore<bool>
| None -> d.Add id.Name |> ignore<bool>
e
| e -> e
mapStmt BlockLevel.Unknown (mapEnvExpr collect) block |> ignore<MapEnv * Stmt>
let set = HashSet(Seq.choose env.varRenames.TryFind d)
let varRenames, reusable = env.varRenames |> Map.partition (fun _ id -> id.Length > 2 || set.Contains id)
let reusable = reusable |> Seq.filter (fun x -> not (List.contains x.Value options.noRenamingList))
let allAvailable = [for i in reusable -> i.Value] @ env.availableNames |> Seq.distinct |> Seq.toList
// Find all the variables known in varRenames that are used in the block.
// They should be preserved in the renaming environment.
let stillUsedSet =
[for ident in Analyzer.varUsesInStmt block -> ident.Name]
|> Seq.choose env.varRenames.TryFind |> set

let varRenames, reusable = env.varRenames |> Map.partition (fun _ id -> stillUsedSet.Contains id)
let reusable = [for i in reusable -> i.Value]
|> List.filter (fun x -> not (List.contains x options.noRenamingList))
let allAvailable = reusable @ env.availableNames |> List.distinct
env.Update(varRenames, env.funRenames, allAvailable)

let rec renStmt env =
Expand All @@ -292,30 +286,34 @@ module private RenamerImpl =
renList env renStmt b |> ignore<Env>
env
| If(cond, th, el) ->
renStmt env th |> ignore<Env>
Option.iter (renStmt env >> ignore<Env>) el
renStmt (env.onEnterFunction env th) th |> ignore<Env>
Option.iter (fun el -> renStmt (env.onEnterFunction env el) el |> ignore<Env>) el
renExpr env cond
env
| ForD(init, cond, inc, body) ->
let newEnv = renDecl false env init
| ForD(init, cond, inc, body) as stmt ->
let newEnv = env.onEnterFunction env stmt
let newEnv = renDecl false newEnv init
renStmt newEnv body |> ignore<Env>
Option.iter (renExpr newEnv) cond
Option.iter (renExpr newEnv) inc
if options.hlsl then newEnv
else env
| ForE(init, cond, inc, body) ->
| ForE(init, cond, inc, body) as stmt ->
let newEnv = env.onEnterFunction env stmt
renOpt init
renOpt cond
renOpt inc
renStmt env body |> ignore<Env>
renStmt newEnv body |> ignore<Env>
env
| While(cond, body) ->
renExpr env cond
renStmt env body |> ignore<Env>
| While(cond, body) as stmt ->
let newEnv = env.onEnterFunction env stmt
renExpr newEnv cond
renStmt newEnv body |> ignore<Env>
env
| DoWhile(cond, body) ->
renExpr env cond
renStmt env body |> ignore<Env>
| DoWhile(cond, body) as stmt ->
let newEnv = env.onEnterFunction env stmt
renExpr newEnv cond
renStmt newEnv body |> ignore<Env>
env
| Jump(_, e) -> renOpt e; env
| Verbatim _ -> env
Expand Down
28 changes: 14 additions & 14 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
clod.frag... 8760 => 1495.202
mouton/mouton.vert... 16770 => 2417.200
clod.frag... 8760 => 1486.187
mouton/mouton.vert... 16740 => 2404.493
audio-flight-v2.frag 4486 => 878.312
buoy.frag 4013 => 605.720
buoy.frag 4013 => 606.290
controllable-machinery.frag 7683 => 1225.947
ed-209.frag 7672 => 1338.529
elevated.hlsl 3401 => 602.542
endeavour.frag 2568 => 529.742
from-the-seas-to-the-stars.frag 14240 => 2306.531
frozen-wasteland.frag 4517 => 803.422
endeavour.frag 2568 => 529.992
from-the-seas-to-the-stars.frag 14240 => 2285.909
frozen-wasteland.frag 4517 => 813.441
kinder_painter.frag 2836 => 442.689
leizex.frag 2252 => 507.685
lunaquatic.frag 5222 => 1043.813
leizex.frag 2252 => 506.309
lunaquatic.frag 5222 => 1043.256
mandelbulb.frag 2325 => 534.901
ohanami.frag 3246 => 721.169
ohanami.frag 3246 => 711.753
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
slisesix.frag 4497 => 890.639
terrarium.frag 3575 => 747.116
the_real_party_is_in_your_pocket.frag 11986 => 1774.550
valley_ball.glsl 4307 => 881.820
yx_long_way_from_home.frag 2938 => 599.331
Total: 133539 => 23388.566
yx_long_way_from_home.frag 2938 => 600.714
Total: 133509 => 23334.181
16 changes: 8 additions & 8 deletions tests/real/chocolux.expected
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ const char *chocolux_frag =
"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 b=0;b<75;b++)"
"for(int v=0;v<75;v++)"
"{"
"if(s(i+y*x)<.4)"
"{"
"x-=5.;"
"for(int m=0;m<5;m++)"
"for(int f=0;f<5;f++)"
"{"
"if(s(i+y*x)<.4)"
"break;"
"x+=1.;"
"}"
"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;"
"vec3 d=vec3(.01,0,0),v=vec3(0);"
"v.x=s(i+y*x)-s(vec3(i+y*x+d.xyy));"
"v.y=s(i+y*x)-s(vec3(i+y*x+d.yxy));"
"v.z=s(i+y*x)-s(vec3(i+y*x+d.yyx));"
"v=normalize(v);"
"f+=max(dot(vec3(0,0,-.5),v),0.)+max(dot(vec3(0,-.5,.5),v),0.)*.5;"
"break;"
"}"
"x+=5.;"
Expand Down
2 changes: 1 addition & 1 deletion tests/real/clod.expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
uniform vec2 v;uniform float s;uniform sampler2D y,c,n,C;float m(vec3 v){float s=(sin(v.x)+v.y*.25)*.35;v=vec3(cos(s)*v.x-sin(s)*v.y,sin(s)*v.x+cos(s)*v.y,v.z);return dot(cos(v)*cos(v),vec3(1))-1.2;}vec3 m(vec3 v,vec3 s){float y=0.,c,f;for(int r=0;r<75;r++){if(m(v+s*y)<0.){c=y-.125;f=y;for(int u=0;u<10;u++){y=(c+f)*.5;if(m(v+s*y)<0.)f=y;else c=y;}vec3 d=vec3(.1,0,0),i=v+s*y,n=-normalize(vec3(m(i+d),m(i+d.yxy),m(i+d.yyx))+vec3(sin(i*75.))*.01);return vec3(mix((max(-dot(n,vec3(.577)),0.)+.125*max(-dot(n,vec3(-.707,-.707,0)),0.))*(mod(length(i.xy)*20.,2.)<1.?vec3(.71,.85,.25):vec3(.79,.93,.4)),vec3(.93,.94,.85),vec3(pow(y/9.,5.))));}y+=.125;}return vec3(.93,.94,.85);}void main(){vec2 i=-1.+2.*gl_FragCoord.xy/v.xy;gl_FragColor=vec4(m(vec3(sin(s*1.5)*.5,cos(s)*.5,s),normalize(vec3(i.xy,1))),1);}
uniform vec2 v;uniform float s;uniform sampler2D y,c,n,C;float m(vec3 v){float s=(sin(v.x)+v.y*.25)*.35;v=vec3(cos(s)*v.x-sin(s)*v.y,sin(s)*v.x+cos(s)*v.y,v.z);return dot(cos(v)*cos(v),vec3(1))-1.2;}vec3 m(vec3 v,vec3 s){float y=0.,c,f;for(int r=0;r<75;r++){if(m(v+s*y)<0.){c=y-.125;f=y;for(int r=0;r<10;r++){y=(c+f)*.5;if(m(v+s*y)<0.)f=y;else c=y;}vec3 r=vec3(.1,0,0),i=v+s*y,n=-normalize(vec3(m(i+r),m(i+r.yxy),m(i+r.yyx))+vec3(sin(i*75.))*.01);return vec3(mix((max(-dot(n,vec3(.577)),0.)+.125*max(-dot(n,vec3(-.707,-.707,0)),0.))*(mod(length(i.xy)*20.,2.)<1.?vec3(.71,.85,.25):vec3(.79,.93,.4)),vec3(.93,.94,.85),vec3(pow(y/9.,5.))));}y+=.125;}return vec3(.93,.94,.85);}void main(){vec2 i=-1.+2.*gl_FragCoord.xy/v.xy;gl_FragColor=vec4(m(vec3(sin(s*1.5)*.5,cos(s)*.5,s),normalize(vec3(i.xy,1))),1);}
68 changes: 34 additions & 34 deletions tests/real/elevated.hlsl.expected
Original file line number Diff line number Diff line change
Expand Up @@ -78,54 +78,54 @@ const char *elevated_hlsl =
"float3 r=float3(.55,.65,.75)+.1*D(s+t[3].w*.2,10)+.5*pow(1-x.y,8)+pow(saturate(mul(x,t[3])),16)*float3(.4,.3,.1)+float4(1+.4*w,2,3+.5*w,0)*(1-cos(12.5664*s.y))*saturate(1-abs(s.y)/10-abs(s.x+t[5+w].x*.0012-8)/20)*exp(-t[5+w].x*2e-4);"
"if(y.w>.5)"
"{"
"float c=length(y.xyz-t[4].xyz),p=t[1].w-y.y;"
"if(p<0)"
"float f=length(y.xyz-t[4].xyz),w=t[1].w-y.y;"
"if(w<0)"
"{"
"float3 e=D(y.xz,.001*c,12-log2(c));"
"float n=D(3*y.xz,3),d=D(666*y.xz);"
"r=(.1+.75*t[2].x)*(.8+.2*d);"
"r=lerp(r,lerp(float3(.8,.85,.9),float3(.45,.45,.2)*(.8+.2*d),t[2].x),smoothstep(.5-.8*e.y,1-1.1*e.y,n*.15));"
"r=lerp(r,lerp(float3(.37,.23,.08),float3(.42,.4,.2),t[2].x)*(.5+.5*d),smoothstep(0,1,50*(e.y-1)+(n+t[2].x)/.4));"
"r*=l(y,e,D(y.xz,.001*c,5));"
"float3 x=D(y.xz,.001*f,12-log2(f));"
"float w=D(3*y.xz,3),s=D(666*y.xz);"
"r=(.1+.75*t[2].x)*(.8+.2*s);"
"r=lerp(r,lerp(float3(.8,.85,.9),float3(.45,.45,.2)*(.8+.2*s),t[2].x),smoothstep(.5-.8*x.y,1-1.1*x.y,w*.15));"
"r=lerp(r,lerp(float3(.37,.23,.08),float3(.42,.4,.2),t[2].x)*(.5+.5*s),smoothstep(0,1,50*(x.y-1)+(w+t[2].x)/.4));"
"r*=l(y,x,D(y.xz,.001*f,5));"
"}"
"else"
"{"
"c=(t[1].w-t[4].y)/x.y;"
"y=t[4]+x.xyzz*c;"
"float3 d=normalize(D(float2(512,32)*y.xz+saturate(p*60)*float2(t[3].w,0),.001*c,4)*float3(1,6,1));"
"r=.12*(float3(.4,1,1)-float3(.2,.6,.4)*saturate(p*16));"
"f=(t[1].w-t[4].y)/x.y;"
"y=t[4]+x.xyzz*f;"
"float3 z=normalize(D(float2(512,32)*y.xz+saturate(w*60)*float2(t[3].w,0),.001*f,4)*float3(1,6,1));"
"r=.12*(float3(.4,1,1)-float3(.2,.6,.4)*saturate(w*16));"
"r*=.3+.7*t[2].x;"
"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+=pow(1-mul(-x,z),4)*(pow(mul(t[3],reflect(-x,z)),32)*float3(.32,.31,.3)+.1);"
"r=lerp(r,l(y,z,z),smoothstep(1,0,t[2].x+w*60-D(666*y.xz+saturate(w*60)*float2(t[3].w,0)*2,5))*.5);"
"}"
"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));"
"r=r*(.7+.3*smoothstep(0,1,256*abs(w)))*exp(-.042*f);"
"r+=(1-exp(-.1*f))*(float3(.52,.59,.65)+pow(saturate(mul(x,t[3])),8)*float3(.6,.4,.1));"
"}"
"return float4(r,0);"
"}"
"float4 s(float2 x:texcoord):color"
"{"
"float2 s=x+.5/1280;"
"float4 r=tex2D(m,s);"
"float3 w=tex2D(y,s);"
"if(r.w>.5)"
"float4 w=tex2D(m,s);"
"float3 r=tex2D(y,s);"
"if(w.w>.5)"
"{"
"r=mul(z,float4(r.xyz,1));"
"r.y*=-1;"
"w=0;"
"for(float p=0;p<16;p++)"
"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=mul(z,float4(w.xyz,1));"
"w.y*=-1;"
"r=0;"
"for(float f=0;f<16;f++)"
"r.x+=tex2D(y,s+f*(.5+.5*w.xy/w.w-s)/16+float2(2,0)/1280).x,r.y+=tex2D(y,s+f*(.5+.5*w.xy/w.w-s)/16+float2(0,0)/1280).y,r.z+=tex2D(y,s+f*(.5+.5*w.xy/w.w-s)/16+float2(-2,0)/1280).z;"
"r/=16;"
"}"
"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;"
"w-=.005*c;"
"w.x+=.01*tex2D(f,s+=.1);"
"w.y+=.01*tex2D(f,s+=.1);"
"w.z+=.01*tex2D(f,s+=.1);"
"return float4(w,0);"
"r=(pow(r,.45)*t[2].z+t[2].y)*(.4+9.6*s.x*s.y*(1-s.x)*(1-s.y));"
"r.xz*=.98;"
"float D=tex2D(f,t[3].w*.1);"
"s+=D;"
"r-=.005*D;"
"r.x+=.01*tex2D(f,s+=.1);"
"r.y+=.01*tex2D(f,s+=.1);"
"r.z+=.01*tex2D(f,s+=.1);"
"return float4(r,0);"
"}";

#endif // ELEVATED_HLSL_EXPECTED_
16 changes: 8 additions & 8 deletions tests/real/leizex.expected
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ _leizex_frag:
db 'ivec3 f=ivec3(floor(v));'
db 'vec3 x=fract(v);'
db 'vec2 n=vec2(1);'
db 'for(int y=-1;y<=1;y++)'
db 'for(int i=-1;i<=1;i++)'
db 'for(int r=-1;r<=1;r++)'
db 'for(int v=-1;v<=1;v++)'
db 'for(int y=-1;y<=1;y++)'
db 'for(int i=-1;i<=1;i++)'
db '{'
db 'int c=f.x+r+57*(f.y+i)+113*(f.z+y);'
db 'vec3 m=vec3(float(r),float(i),float(y))-x+vec3(t(c),t(c+1217),t(c+2513))/2147483647.;'
db 'int c=f.x+i+57*(f.y+y)+113*(f.z+v);'
db 'vec3 m=vec3(float(i),float(y),float(v))-x+vec3(t(c),t(c+1217),t(c+2513))/2147483647.;'
db 'float s=dot(m,m);'
db 'if(s<n.x)'
db 'n.y=n.x,n.x=s;'
Expand Down Expand Up @@ -95,10 +95,10 @@ _leizex_frag:
db 'for(i=.1;i<5.;)'
db '{'
db 'o=y+i*m;'
db 'float d=c(o,t);'
db 'if(d<.001)'
db 'float v=c(o,t);'
db 'if(v<.001)'
db 'break;'
db 'i+=d*.12;'
db 'i+=v*.12;'
db '}'
db 'r=x(o);'
db 'y=vec3(1.+.5*c(96.*o));'
Expand Down
16 changes: 8 additions & 8 deletions tests/real/mandelbulb.expected
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ const char *mandelbulb_frag =
"vec4 d;"
"if(f(c,e,m,s,d))"
"{"
"vec3 x=c+m*e;"
"float n=clamp(.2+.8*dot(y,s),0.,1.);"
"n*=n;"
"vec3 i=c+m*e;"
"float v=clamp(.2+.8*dot(y,s),0.,1.);"
"v*=v;"
"float t=clamp(.3+.7*dot(o,s),0.,1.),w=clamp(1.25*d.w-.4,0.,1.);"
"w=w*w*.5+.5*w;"
"float r;"
"vec3 p;"
"vec4 g;"
"if(n>.001)"
"if(f(x,y,r,p,g))"
"n=.1;"
"z=mix(mix(mix(vec3(1),vec3(.8,.6,.2),sqrt(d.x)*1.25),vec3(.8,.3,.3),sqrt(d.y)*1.25),vec3(.7,.4,.3),sqrt(d.z)*1.25)*((.5+.5*s.y)*vec3(.14,.15,.16)*.8+n*vec3(1,.85,.4)+.5*t*vec3(.08,.1,.14))*vec3(pow(w,.8),pow(w,1.),pow(w,1.1));"
"vec4 x;"
"if(v>.001)"
"if(f(i,y,r,p,x))"
"v=.1;"
"z=mix(mix(mix(vec3(1),vec3(.8,.6,.2),sqrt(d.x)*1.25),vec3(.8,.3,.3),sqrt(d.y)*1.25),vec3(.7,.4,.3),sqrt(d.z)*1.25)*((.5+.5*s.y)*vec3(.14,.15,.16)*.8+v*vec3(1,.85,.4)+.5*t*vec3(.08,.1,.14))*vec3(pow(w,.8),pow(w,1.),pow(w,1.1));"
"z=1.5*(z*.15+.85*sqrt(z));"
"}"
"else"
Expand Down
Loading