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

distance(x,y) => length(x-y) #392

Merged
merged 1 commit 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
5 changes: 5 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ module private RewriterImpl =
| _ -> failwithf "Cannot inline function %s because type-based disambiguation of user-defined function overloading is not supported" v.Name

| FunCall(Op _, _) as op -> simplifyOperator env op

| FunCall(Var dist, [arg1; arg2]) when dist.Name = "distance" ->
let len = Ident("length", dist.Loc.line, dist.Loc.col)
FunCall(Var len, [FunCall (Op "-", [arg1; arg2])])

| FunCall(Var constr, args) when constr.Name = "vec2" || constr.Name = "vec3" || constr.Name = "vec4" ->
simplifyVec constr args

Expand Down
10 changes: 5 additions & 5 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
clod.frag... 8760 => 1486.187
mouton/mouton.vert... 16740 => 2404.493
mouton/mouton.vert... 16738 => 2401.196
audio-flight-v2.frag 4486 => 878.312
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.992
from-the-seas-to-the-stars.frag 14240 => 2285.909
frozen-wasteland.frag 4517 => 813.441
from-the-seas-to-the-stars.frag 14236 => 2285.862
frozen-wasteland.frag 4515 => 804.894
kinder_painter.frag 2836 => 442.689
leizex.frag 2252 => 506.309
lunaquatic.frag 5222 => 1043.256
Expand All @@ -20,5 +20,5 @@ 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 => 600.714
Total: 133509 => 23334.181
yx_long_way_from_home.frag 2936 => 598.845
Total: 133499 => 23320.420
20 changes: 10 additions & 10 deletions tests/real/chocolux.expected
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
// Generated with (https://github.com/laurentlb/Shader_Minifier/)
#ifndef CHOCOLUX_EXPECTED_
# define CHOCOLUX_EXPECTED_
# define VAR_resolution "d"
# define VAR_resolution "k"
# define VAR_time "v"

const char *chocolux_frag =
"uniform vec2 d;"
"uniform vec2 k;"
"uniform float v;"
"float s(vec3 d)"
"{"
"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);"
"return length(d-vec3(cos(v)+sin(v*.2),.3,2.+cos(v*.5)*.5))*length(d-vec3(-cos(v*.7),.3,2.+sin(v*.5)))*length(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 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.;"
"vec2 d=-1.+2.*gl_FragCoord.xy/k.xy;"
"vec3 c=vec3(d.x,d.y*1.25-.3,0),y=vec3(d.x+cos(v)*.3,d.y,1)/64.;"
"vec4 f=vec4(0);"
"float x=0.;"
"for(int v=0;v<75;v++)"
"{"
"if(s(i+y*x)<.4)"
"if(s(c+y*x)<.4)"
"{"
"x-=5.;"
"for(int f=0;f<5;f++)"
"{"
"if(s(i+y*x)<.4)"
"if(s(c+y*x)<.4)"
"break;"
"x+=1.;"
"}"
"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.x=s(c+y*x)-s(vec3(c+y*x+d.xyy));"
"v.y=s(c+y*x)-s(vec3(c+y*x+d.yxy));"
"v.z=s(c+y*x)-s(vec3(c+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;"
Expand Down
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 @@ -636,7 +636,7 @@ void main()
col=vec3(.3,1,.3)/7.*clamp(p.y+1.,0,1);
col=mix(col,vec3(1),length(op.xz)-.12)/4;
p.y-=.8;
p.xz+=normalize(p.xz-fp.xz)*(1.-smoothstep(0.,4.,distance(p.xz,fp.xz)))*pow(max(0.,p.y+.5),2.)*.3;
p.xz+=normalize(p.xz-fp.xz)*(1.-smoothstep(0.,4.,length(p.xz-fp.xz)))*pow(max(0.,p.y+.5),2.)*.3;
w.x=R();
col+=pow(w.x,32.)*vec3(1,1,.5)/3.;
if(p.y<-1)
Expand Down Expand Up @@ -819,7 +819,7 @@ void main()
vec3 bg=vec3(.02)*vec3(.7,.9,1)*.6;
if(time>=200)
bg*=max(0.,1.-(time-200)/7.);
c.xyz=mix(bg,c.xyz,c.w)*3*(1.-smoothstep(.2,1.,distance(vec2(coord),vec2(640,360))/880.));
c.xyz=mix(bg,c.xyz,c.w)*3*(1.-smoothstep(.2,1.,length(vec2(coord)-vec2(640,360))/880.));
c*=min(1.,time/20.)*smoothstep(0.,3.,abs(time-60-18.5))*smoothstep(0.,1.,abs(time-61))*smoothstep(0.,1.,abs(time-148))*smoothstep(0.,1.,abs(time-165));
if(time<43)
c*=1.-smoothstep(42,43,time);
Expand Down
2 changes: 1 addition & 1 deletion tests/real/frozen-wasteland.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void mainImage(out vec4 fragColor,vec2 fragCoord)
if(rz<FAR)
{
vec3 pos=ro+rz*rightdir,nor=normal(pos,rz);
float d=distance(pos,ro);
float d=length(pos-ro);
nor=bump(pos,nor,d);
float shd=shadow(pos,eyedir),dif=clamp(dot(nor,eyedir),0.,1.);
vec3 ref=reflect(rightdir,nor);
Expand Down
Loading