Skip to content

Commit

Permalink
Remove unsafe vec optimization (#234)
Browse files Browse the repository at this point in the history
The optimization tried to remove a useless call to vec, e.g. replace
vec2(v.xy) with v.xy This is not safe when the argument is not a vec2,
e.g. it's an ivec2.

Fixes #233
  • Loading branch information
laurentlb authored Feb 12, 2023
1 parent 8e5d8f2 commit e6d5aa7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
open System.IO
open Argu

let version = "1.3.3" // Shader Minifier version
let version = "1.3.4" // Shader Minifier version
let debugMode = false

type OutputFormat =
Expand Down
7 changes: 1 addition & 6 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,7 @@ let private simplifyVec constr args =

let args = combineSwizzles args |> List.map useInts
let args = mergeAllEquals args args
match args with
| [Dot (_, field) as arg] when field.Length > 1 && isFieldSwizzle field ->
// vec3(v.xxy) => v.xxy
// However, vec3(v.x) should be preserved.
arg
| _ -> FunCall (Var constr, args)
FunCall (Var constr, args)

let private simplifyExpr (didInline: bool ref) env = function
| FunCall(Var v, passedArgs) as e when v.ToBeInlined ->
Expand Down
8 changes: 4 additions & 4 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
clod.frag... 8960 => 1545.563
audio-flight-v2.frag 4650 => 902.661
buoy.frag 4239 => 632.427
buoy.frag 4245 => 633.229
controllable-machinery.frag 7773 => 1227.926
ed-209.frag 7926 => 1363.748
ed-209.frag 7938 => 1360.765
elevated.hlsl 3416 => 603.918
endeavour.frag 2661 => 541.942
from-the-seas-to-the-stars.frag 14370 => 2345.702
Expand All @@ -12,12 +12,12 @@ leizex.frag 2327 => 515.566
lunaquatic.frag 5231 => 1055.816
mandelbulb.frag 2419 => 546.857
ohanami.frag 3292 => 736.461
orchard.frag 5643 => 1034.091
orchard.frag 5649 => 1035.594
oscars_chair.frag 4653 => 984.586
robin.frag 6343 => 1057.981
slisesix.frag 4587 => 937.138
terrarium.frag 3634 => 747.342
the_real_party_is_in_your_pocket.frag 12248 => 1815.043
valley_ball.glsl 4386 => 888.496
yx_long_way_from_home.frag 3030 => 615.535
Total: 119302 => 21362.097
Total: 119326 => 21361.418
2 changes: 1 addition & 1 deletion tests/real/ed-209.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ float sdOctogon(vec2 p,float r)
{
const vec3 k=vec3(-.9238795325,.3826834323,.4142135623);
p=abs(p);
p-=2.*min(dot(k.xy,p),0.)*k.xy;
p-=2.*min(dot(vec2(k.xy),p),0.)*vec2(k.xy);
p-=2.*min(dot(vec2(-k.x,k.y),p),0.)*vec2(-k.x,k.y);
p-=vec2(clamp(p.x,-k.z*r,k.z*r),r);
return length(p)*sign(p.y);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/vectors.frag.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
float swizzles()
{
vec2 v1=vec2(1);
vec3 v2=v1.xyx,v3=vec3(v1.x,v2.xx);
vec4 v4=v1.xxyx,v5=vec4(1,v2.zy,2),v6=vec4(v1.xy,v2.xy);
vec3 v2=vec3(v1.xyx),v3=vec3(v1.x,v2.xx);
vec4 v4=vec4(v1.xxyx),v5=vec4(1,v2.zy,2),v6=vec4(v1.xy,v2.xy);
return v1.x+v2.x+v3.x+v4.x+v5.x+v6.x;
}
vec4 constructor()
Expand Down

0 comments on commit e6d5aa7

Please sign in to comment.