Skip to content

Commit

Permalink
Use context-based renaming when minifying multiple files (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentlb authored Jan 16, 2023
1 parent 24011db commit ddfd9f7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Checker/compression_test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let compressionTest args files =
out.ToString().ToCharArray()

let pointer = &&minified.[0]
log "%-40s " (match files with [f] -> f | f::_ -> f + "...")
log "%-40s " (match files with [f] -> f | f::_ -> f + "..." | [] -> "?")
log "%5d " minified.Length
let compressedSize = Crinkler.ApproximateModels4k(pointer, minified.Length)
log "=> %8.3f\n" compressedSize
Expand Down
16 changes: 15 additions & 1 deletion src/renamer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ module private RenamerImpl =
d.[id.OldName] <- newName
env.Rename(id, newName)

// Renaming safe across multiple files (e.g. uniform/in/out variables are
// renamed in a consistent way) that tries to optimize based on the context
// and variable reuse.
let multiFileRenaming contextTable (exportRenames: IDictionary<string, string>) (env: Env) (id: Ident) =
match exportRenames.TryGetValue(id.Name) with
| true, newName -> env.Rename(id, newName)
| false, _ ->
let cid = char (1000 + int id.Name)
let newName = chooseIdent contextTable cid env.availableNames
env.Rename(id, newName)

let dontRename (env: Env) (id: Ident) =
env.Rename(id, id.Name)

Expand Down Expand Up @@ -376,7 +387,10 @@ module private RenamerImpl =

let mutable env =
if Array.length shaders > 1 then
Env.Create(names, true, bijectiveRenaming names, shadowVariables)
// Env.Create(names, true, bijectiveRenaming names, shadowVariables)
let exportsRenames = Seq.zip [for export in exportedNames -> export.name] names |> dict
let contextTable = computeContextTable text
Env.Create(names, true, multiFileRenaming contextTable exportsRenames, shadowVariables)
else
let contextTable = computeContextTable text
Env.Create(names, true, optimizeContext contextTable, shadowVariables)
Expand Down
4 changes: 2 additions & 2 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
clod.frag... 9414 => 1697.174
clod.frag... 8960 => 1549.730
audio-flight-v2.frag 4650 => 904.201
buoy.frag 4239 => 629.709
controllable-machinery.frag 7773 => 1230.829
Expand All @@ -20,4 +20,4 @@ terrarium.frag 3634 => 749.630
the_real_party_is_in_your_pocket.frag 12248 => 1815.725
valley_ball.glsl 4386 => 888.815
yx_long_way_from_home.frag 3030 => 615.535
Total: 119757 => 21535.071
Total: 119303 => 21387.626
82 changes: 41 additions & 41 deletions tests/unit/inout.expected
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
/* File generated with
* http://www.ctrl-alt-test.fr
*/
# define VAR_ambientLight "m"
# define VAR_diffuseColor "i"
# define VAR_emissiveColor "a"
# define VAR_fragmentColor "o"
# define VAR_mediumDensity "t"
# define VAR_normal "c"
# define VAR_specularColor "n"
# define VAR_texture0 "e"
# define VAR_viewVec "v"
# define VAR_ambientLight "f"
# define VAR_diffuseColor "n"
# define VAR_emissiveColor "z"
# define VAR_fragmentColor "r"
# define VAR_mediumDensity "s"
# define VAR_normal "m"
# define VAR_specularColor "e"
# define VAR_texture0 "v"
# define VAR_viewVec "d"

// tests/unit/inout.frag
"#version 330\n"
"uniform samplerCube e;"
"in vec3 c,v;"
"out vec4 o;"
"uniform samplerCube v;"
"in vec3 m,d;"
"out vec4 r;"
"void main()"
"{"
"vec3 l=normalize(v),u=normalize(c),f=vec3(.1,.2,.3),x=texture(e,reflect(-l,u)).xyz,p=texture(e,refract(-l,u,1./1.5)).xyz,z=mix(f*p,x,.1);"
"o=vec4(z,1);"
"vec3 f=normalize(d),z=normalize(m),c=vec3(.1,.2,.3),n=texture(v,reflect(-f,z)).xyz,e=texture(v,refract(-f,z,1./1.5)).xyz,p=mix(c*e,n,.1);"
"r=vec4(p,1);"
"}"
"vec3 r(vec3 d,vec3 l,vec3 s)"
"vec3 t(vec3 v,vec3 m,vec3 f)"
"{"
"float C=1.-clamp(dot(l,s),0.,1.);"
"return C*C*C*C*C*(1.-d)+d;"
"float r=1.-clamp(dot(m,f),0.,1.);"
"return r*r*r*r*r*(1.-v)+v;"
"}"
"vec3 r(vec3 l,vec3 y,vec3 u,vec3 f,vec3 d,float w)"
"vec3 t(vec3 v,vec3 m,vec3 f,vec3 n,vec3 d,float r)"
"{"
"vec3 s=normalize(l+y),b,Z,Y;"
"float X=1.+2048.*(1.-w)*(1.-w);"
"b=f;"
"Z=vec3(pow(clamp(dot(s,u),0.,1.),X)*(X+4.)/8.);"
"Y=r(d,l,s);"
"return mix(b,Z,Y);"
"vec3 z=normalize(v+m),p,e,c;"
"float s=1.+2048.*(1.-r)*(1.-r);"
"p=n;"
"e=vec3(pow(clamp(dot(z,f),0.,1.),s)*(s+4.)/8.);"
"c=t(d,v,z);"
"return mix(p,e,c);"
"}",

// tests/unit/inout2.frag
"#version 330\n"
"uniform samplerCube e;"
"uniform float t;"
"uniform vec3 m,i,a,n;"
"in vec3 c,v;"
"out vec4 o;"
"vec3 r(vec3 d,vec3 l,vec3 s)"
"uniform samplerCube v;"
"uniform float s;"
"uniform vec3 f,n,z,e;"
"in vec3 m,d;"
"out vec4 r;"
"vec3 t(vec3 v,vec3 m,vec3 f)"
"{"
"float C=1.-clamp(dot(l,s),0.,1.);"
"return C*C*C*C*C*(1.-d)+d;"
"float r=1.-clamp(dot(m,f),0.,1.);"
"return r*r*r*r*r*(1.-v)+v;"
"}"
"void main()"
"{"
"vec3 f=i,z=a+mix(f*m,m,.5);"
"o=vec4(z,1);"
"vec3 v=n,p=z+mix(v*f,f,.5);"
"r=vec4(p,1);"
"}"
"vec3 r(vec3 l,vec3 y,vec3 u,vec3 f,vec3 d,float w)"
"vec3 t(vec3 v,vec3 m,vec3 f,vec3 n,vec3 d,float r)"
"{"
"vec3 s=normalize(l+y),b,Z,Y;"
"float X=1.+2048.*(1.-w)*(1.-w);"
"b=f;"
"Z=vec3(pow(clamp(dot(s,u),0.,1.),X)*(X+4.)/8.);"
"Y=r(d,l,s);"
"return mix(b,Z,Y);"
"vec3 z=normalize(v+m),p,e,c;"
"float s=1.+2048.*(1.-r)*(1.-r);"
"p=n;"
"e=vec3(pow(clamp(dot(z,f),0.,1.),s)*(s+4.)/8.);"
"c=t(d,v,z);"
"return mix(p,e,c);"
"}",

0 comments on commit ddfd9f7

Please sign in to comment.