8000 Arithmetic: better matching of int operations; disable the unsafe x*0 optimization by laurentlb · Pull Request #187 · laurentlb/shader-minifier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Arithmetic: better matching of int operations; disable the unsafe x*0 optimization #187

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

Merged
merged 1 commit into from
Dec 31, 2022
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
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ int dangling_else(int x)
Operations with constant arguments are evaluated, e.g. `5*2` will be replaced
with `10`. This is useful especially when a value has been inlined.

Known bug: `x*0` is always replaced with `0`. This is invalid when x is a vector
([#147](https://github.com/laurentlb/Shader_Minifier/issues/147)).

### Commutative operators

If you write `x*(y*z)`, Shader Minifier will not remove the parentheses because
Expand Down
72 changes: 35 additions & 37 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ let (|True|False|NotABool|) = function
| Var var when var.Name = "false" -> False
| _ -> NotABool

let (|Number|_|) = function
| Int (i, _) -> Some (decimal i)
| Float (f, _) -> Some f
| _ -> None

let rec private simplifyExpr (didInline: bool ref) env = function
| FunCall(Var v, passedArgs) as e when v.ToBeInlined ->
match env.fns.TryFind v.Name with
Expand Down Expand Up @@ -116,35 +121,13 @@ let rec private simplifyExpr (didInline: bool ref) env = function
| FunCall(Op "-", [x; Int (i, s)]) when i < 0 ->
FunCall(Op "+", [x; Int (-i, s)]) |> env.fExpr env

// Swap operands to get rid of parentheses
// x*(y*z) -> y*z*x
| FunCall(Op "*", [NoParen x; FunCall(Op "*", [y; z])]) ->
FunCall(Op "*", [FunCall(Op "*", [y; z]); x]) |> env.fExpr env
// x+(y+z) -> y+z+x
// x+(y-z) -> y-z+a
| FunCall(Op "+", [NoParen x; FunCall(Op ("+"|"-") as op, [y; z])]) ->
FunCall(Op "+", [FunCall(op, [y; z]); x]) |> env.fExpr env
// x-(y+z) -> x-y-z
| FunCall(Op "-", [x; FunCall(Op "+", [y; z])]) ->
FunCall(Op "-", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env
// x-(y-z) -> x-y+z
| FunCall(Op "-", [x; FunCall(Op "-", [y; z])]) ->
FunCall(Op "+", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env

// Boolean simplifications (let's ignore the suffix)
| FunCall(Op "<", [Int (i1, _); Int (i2, _)]) -> bool(i1 < i2)
| FunCall(Op ">", [Int (i1, _); Int (i2, _)]) -> bool(i1 > i2)
| FunCall(Op "<=", [Int (i1, _); Int (i2, _)]) -> bool(i1 <= i2)
| FunCall(Op ">=", [Int (i1, _); Int (i2, _)]) -> bool(i1 <= i2)
| FunCall(Op "==", [Int (i1, _); Int (i2, _)]) -> bool(i1 = i2)
| FunCall(Op "!=", [Int (i1, _); Int (i2, _)]) -> bool(i1 <> i2)

| FunCall(Op "<", [Float (i1,_); Float (i2,_)]) -> bool(i1 < i2)
| FunCall(Op ">", [Float (i1,_); Float (i2,_)]) -> bool(i1 > i2)
| FunCall(Op "<=", [Float (i1,_); Float (i2,_)]) -> bool(i1 <= i2)
| FunCall(Op ">=", [Float (i1,_); Float (i2,_)]) -> bool(i1 <= i2)
| FunCall(Op "==", [Float (i1,_); Float (i2,_)]) -> bool(i1 = i2)
| FunCall(Op "!=", [Float (i1,_); Float (i2,_)]) -> bool(i1 <> i2)
| FunCall(Op "<", [Number n1; Number n2]) -> bool(n1 < n2)
| FunCall(Op ">", [Number n1; Number n2]) -> bool(n1 > n2)
| FunCall(Op "<=", [Number n1; Number n2]) -> bool(n1 <= n2)
| FunCall(Op ">=", [Number n1; Number n2]) -> bool(n1 <= n2)
| FunCall(Op "==", [Number n1; Number n2]) -> bool(n1 = n2)
| FunCall(Op "!=", [Number n1; Number n2]) -> bool(n1 <> n2)

// Conditionals
| FunCall(Op "?:", [True; x; _]) -> x
Expand All @@ -157,15 +140,15 @@ let rec private simplifyExpr (didInline: bool ref) env = function
| FunCall(Op "||", [x; False]) -> x

// Stupid simplifications (they can be useful to simplify rewritten code)
| FunCall(Op "/", [e; Float (1.M,_)]) -> e
| FunCall(Op "*", [e; Float (1.M,_)]) -> e
| FunCall(Op "*", [Float (1.M,_); e]) -> e
| FunCall(Op "*", [_; Float (0.M,_) as e]) -> e
| FunCall(Op "*", [Float (0.M,_) as e; _]) -> e
| FunCall(Op "+", [e; Float (0.M,_)]) -> e
| FunCall(Op "+", [Float (0.M,_); e]) -> e
| FunCall(Op "-", [e; Float (0.M,_)]) -> e
| FunCall(Op "-", [Float (0.M,_); e]) -> FunCall(Op "-", [e])
| FunCall(Op "/", [e; Number 1M]) -> e
| FunCall(Op "*", [e; Number 1M]) -> e
| FunCall(Op "*", [Number 1M; e]) -> e
// | FunCall(Op "*", [_; Number 0M as zero]) -> zero // unsafe
// | FunCall(Op "*", [Number 0M as zero; _]) -> zero // unsafe
| FunCall(Op "+", [e; Number 0M]) -> e
| FunCall(Op "+", [Number 0M; e]) -> e
| FunCall(Op "-", [e; Number 0M]) -> e
| FunCall(Op "-", [Number 0M; e]) -> FunCall(Op "-", [e])

// No simplification when numbers have different suffixes
| FunCall(_, [Int (_, su1); Int (_, su2)]) as e when su1 <> su2 -> e
Expand All @@ -187,6 +170,21 @@ let rec private simplifyExpr (didInline: bool ref) env = function
if (Printer.exprToS e).Length <= (Printer.exprToS div).Length then e
else div

// Swap operands to get rid of parentheses
// x*(y*z) -> y*z*x
| FunCall(Op "*", [NoParen x; FunCall(Op "*", [y; z])]) ->
FunCall(Op "*", [FunCall(Op "*", [y; z]); x]) |> env.fExpr env
// x+(y+z) -> y+z+x
// x+(y-z) -> y-z+a
| FunCall(Op "+", [NoParen x; FunCall(Op ("+"|"-") as op, [y; z])]) ->
FunCall(Op "+", [FunCall(op, [y; z]); x]) |> env.fExpr env
// x-(y+z) -> x-y-z
| FunCall(Op "-", [x; FunCall(Op "+", [y; z])]) ->
FunCall(Op "-", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env
// x-(y-z) -> x-y+z
| FunCall(Op "-", [x; FunCall(Op "-", [y; z])]) ->
FunCall(Op "+", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env

// iq's smoothstep trick: http://www.pouet.net/topic.php?which=6751&page=1#c295695
| FunCall(Var var, [Float (0.M,_); Float (1.M,_); _]) as e when var.Name = "smoothstep" -> e
| FunCall(Var var, [a; b; x]) when var.Name = "smoothstep" && options.smoothstepTrick ->
Expand Down
18 changes: 9 additions & 9 deletions tests/real/from-the-seas-to-the-stars.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ float koi(vec2 t)
p.x-=.9;
op.y-=.05;
op.y=abs(op.y);
d=max(d,op.x-pow(op.y/2,.7)*1-1.1);
d=max(d,op.x-pow(op.y/2,.7)-1.1);
oop.x-=.15;
d=min(d,max(-oop.y,max(oop.x,oop.x*oop.x*4+oop.y-.7)));
oop.x+=oop.y*.3+.25;
Expand Down Expand Up @@ -184,7 +184,7 @@ float smin(float a,float b,float k)
}
void main()
{
time=float(millisecs)/1e3+R()*.015+0;
time=float(millisecs)/1e3+R()*.015;
if(mode==0)
{
col=vec3(1.,.25,.1);
Expand Down Expand Up @@ -398,7 +398,7 @@ void main()
p.z+=cos(p.x);
p.y+=(p.x+10)/2-3;
if(w.x<.1)
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz*1)-.9)*c;
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz)-.9)*c;
}
}
else
Expand Down Expand Up @@ -427,7 +427,7 @@ void main()
p.z+=cos(p.x);
p.y+=(p.x+10)/2-3;
if(w.x<.1)
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz*1)-.9)*c;
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz)-.9)*c;
w.x=R();
col+=pow(w.x,32.)*vec3(.2);
w.x=R();
Expand Down Expand Up @@ -480,7 +480,7 @@ void main()
p.x=-p.x-10+t;
p.z+=cos(p.x);
if(w.x<.1)
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz*1)-.9)*c;
p.y+=(fbm(p.xz*2)-.9)*c*.2,p.x+=(fbm(p.yz)-.9)*c;
w.x=R();
col+=pow(w.x,32.)*vec3(.2);
w.x=R();
Expand Down Expand Up @@ -805,10 +805,10 @@ void main()
w.y=R();
w.z=R();
uint colbits=uint(col.z*31+w.x)|uint(col.y*63+w.y)<<5|uint(col.x*31+w.z)<<11,depthbits=uint(clamp(1./(1.+p.z),0,1)*65535.),outbits=depthbits<<16|colbits;
imageAtomicMax(d0,ivec2(x+1,y+0),int(coord.x+0&7),outbits);
imageAtomicMax(d0,ivec2(x-1,y+0),int(coord.x+2&7),outbits);
imageAtomicMax(d0,ivec2(x+0,y-1),int(coord.x+4&7),outbits);
imageAtomicMax(d0,ivec2(x+0,y+1),int(coord.x+6&7),outbits);
imageAtomicMax(d0,ivec2(x+1,y),int(coord.x&7),outbits);
imageAtomicMax(d0,ivec2(x-1,y),int(coord.x+2&7),outbits);
imageAtomicMax(d0,ivec2(x,y-1),int(coord.x+4&7),outbits);
imageAtomicMax(d0,ivec2(x,y+1),int(coord.x+6&7),outbits);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/real/leizex.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; File generated with Shader Minifier 1.3
; File generated with
; http://www.ctrl-alt-test.fr

_leizex_frag:
Expand All @@ -16,7 +16,7 @@ _leizex_frag:
db 'vec3 f=fract(x);'
db 'f=f*f*(3.-2.*f);'
db 'int n=ip.x+ip.y*57+113*ip.z+sem;'
db 'float res=mix(mix(mix(coolfFunc3d2(n+0),coolfFunc3d2(n+1),f.x),mix(coolfFunc3d2(n+57),coolfFunc3d2(n+58),f.x),f.y),mix(mix(coolfFunc3d2(n+113),coolfFunc3d2(n+114),f.x),mix(coolfFunc3d2(n+170),coolfFunc3d2(n+171),f.x),f.y),f.z);'
db 'float res=mix(mix(mix(coolfFunc3d2(n),coolfFunc3d2(n+1),f.x),mix(coolfFunc3d2(n+57),coolfFunc3d2(n+58),f.x),f.y),mix(mix(coolfFunc3d2(n+113),coolfFunc3d2(n+114),f.x),mix(coolfFunc3d2(n+170),coolfFunc3d2(n+171),f.x),f.y),f.z);'
db 'return 1.-res*(1./1073741824.);'
db '}'
db 'vec2 celular(vec3 x)'
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 @@ -106,7 +106,7 @@ void main()
break;
t+=d;
}
gl_FragColor.xyz*=1*sh*(.2+.8*l)*vec3(1,1,.9)*.7+l2*vec3(.85,.85,1)*.4;
gl_FragColor.xyz*=sh*(.2+.8*l)*vec3(1,1,.9)*.7+l2*vec3(.85,.85,1)*.4;
}
else
{
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 @@ -17,7 +17,7 @@ float noise3f(vec3 p)
vec3 u=fract(p);
u=u*u*(3.-2.*u);
int n=ip.x+ip.y*57+ip.z*113;
float res=mix(mix(mix(coolfFunc3d2(n+0),coolfFunc3d2(n+1),u.x),mix(coolfFunc3d2(n+57),coolfFunc3d2(n+58),u.x),u.y),mix(mix(coolfFunc3d2(n+113),coolfFunc3d2(n+114),u.x),mix(coolfFunc3d2(n+170),coolfFunc3d2(n+171),u.x),u.y),u.z);
float res=mix(mix(mix(coolfFunc3d2(n),coolfFunc3d2(n+1),u.x),mix(coolfFunc3d2(n+57),coolfFunc3d2(n+58),u.x),u.y),mix(mix(coolfFunc3d2(n+113),coolfFunc3d2(n+114),u.x),mix(coolfFunc3d2(n+170),coolfFunc3d2(n+171),u.x),u.y),u.z);
return 1.-res*(1./1073741824.);
}
float fbm(vec3 p)
Expand Down
0