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

New renaming strategy for when minifying multiple files #100

Merged
merged 1 commit into from
Jun 5, 2021
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
30 changes: 21 additions & 9 deletions src/renamer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,21 @@ module private RenamerImpl =
let newName = sprintf "%04d" !numberOfUsedIdents
env.Rename(id, newName), newName

// FIXME: handle 2-letter names
// A renaming strategy that considers how a variable is used. It optimizes the frequency of
// adjacent characters, which can make the output more compression-friendly. This is best
// suited when minifying a single file.
let optimizeContext contextTable env id =
let cid = char (1000 + int id)
let newName = chooseIdent contextTable cid env.availableNames
env.Rename(id, newName), newName

// A renaming strategy that always picks the first available name. This optimizes the
// frequency of a few variables. It also ensures that two identical functions will use the
// same names for local variables, which can be very important in some multifile scenarios.
let optimizeNameFrequency (env: Env) id =
let newName = env.availableNames.Head
env.Rename(id, newName), newName

let dontRename (env: Env) name =
env.Rename(name, name)

Expand Down Expand Up @@ -336,17 +345,20 @@ module private RenamerImpl =
let rename shaders =
let exportedNames = assignUniqueIds shaders

// Get data about the context
let text = [for shader in shaders -> Printer.printText shader.code] |> String.concat "\0"
let names = computeListOfNames text
let contextTable = computeContextTable text

// TODO: combine from all shaders
let forbiddenNames = (Seq.head shaders).forbiddenNames

let names = names |> List.filter (fun x -> not <| List.exists ((=) x) forbiddenNames)

let mutable env = Env.Create(names, true, optimizeContext contextTable, shadowVariables)
// Compute the list of variable names to use
let text = [for shader in shaders -> Printer.printText shader.code] |> String.concat "\0"
let names = computeListOfNames text
|> List.filter (fun x -> not <| List.exists ((=) x) forbiddenNames)

let mutable env =
if Array.length shaders > 1 then
Env.Create(names, true, optimizeNameFrequency, shadowVariables)
else
let contextTable = computeContextTable text
Env.Create(names, true, optimizeContext contextTable, shadowVariables)
env <- dontRenameList env options.noRenamingList
env.exportedNames := exportedNames
renameAsts shaders env
Expand Down
82 changes: 41 additions & 41 deletions tests/unit/inout.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,66 @@
*/
#ifndef INOUT_EXPECTED_
# define INOUT_EXPECTED_
# define VAR_AMBIENTLIGHT "r"
# define VAR_DIFFUSECOLOR "n"
# define VAR_EMISSIVECOLOR "t"
# define VAR_FRAGMENTCOLOR "f"
# define VAR_MEDIUMDENSITY "s"
# define VAR_NORMAL "m"
# define VAR_SPECULARCOLOR "c"
# define VAR_TEXTURE0 "v"
# define VAR_VIEWVEC "d"
# define VAR_AMBIENTLIGHT "a"
# define VAR_DIFFUSECOLOR "m"
# define VAR_EMISSIVECOLOR "l"
# define VAR_FRAGMENTCOLOR "o"
# define VAR_MEDIUMDENSITY "t"
# define VAR_NORMAL "c"
# define VAR_SPECULARCOLOR "i"
# define VAR_TEXTURE0 "e"
# define VAR_VIEWVEC "v"

const char *inout_frag =
"#version 330\n"
"uniform samplerCube v;"
"in vec3 m,d;"
"out vec4 f;"
"uniform samplerCube e;"
"in vec3 c,v;"
"out vec4 o;"
"void main()"
"{"
"vec3 r=normalize(d),n=normalize(m),c=vec3(.1,.2,.3),t=vec3(.5,.5,.5);"
"float s=1.5;"
"vec3 z=texture(v,reflect(-r,n)).xyz,e=texture(v,refract(-r,n,1./s)).xyz,p=mix(c*e,z,.1);"
"f=vec4(p,1.);"
"vec3 r=normalize(v),t=normalize(c),a=vec3(.1,.2,.3),m=vec3(.5,.5,.5);"
"float l=1.5;"
"vec3 i=texture(e,reflect(-r,t)).xyz,n=texture(e,refract(-r,t,1./l)).xyz,f=mix(a*n,i,.1);"
"o=vec4(f,1.);"
"}"
"vec3 e(vec3 v,vec3 m,vec3 f)"
"vec3 r(vec3 e,vec3 c,vec3 v)"
"{"
"float s=1.-clamp(dot(m,f),0.,1.);"
"return s*s*s*s*s*(1.-v)+v;"
"float o=1.-clamp(dot(c,v),0.,1.);"
"return o*o*o*o*o*(1.-e)+e;"
"}"
"vec3 e(vec3 v,vec3 m,vec3 r,vec3 n,vec3 d,float s)"
"vec3 r(vec3 e,vec3 c,vec3 v,vec3 o,vec3 r,float t)"
"{"
"vec3 f=normalize(v+m);"
"float c=1.+2048.*(1.-s)*(1.-s);"
"vec3 z=n,t=vec3(pow(clamp(dot(f,r),0.,1.),c)*(c+4.)/8.),e=x(d,v,f);"
"return mix(z,t,e);"
"vec3 a=normalize(e+c);"
"float m=1.+2048.*(1.-t)*(1.-t);"
"vec3 l=o,i=vec3(pow(clamp(dot(a,v),0.,1.),m)*(m+4.)/8.),f=n(r,e,a);"
"return mix(l,i,f);"
"}";

const char *inout2_frag =
"#version 330\n"
"uniform samplerCube v;"
"uniform float s;"
"uniform vec3 r,n,t,c;"
"in vec3 m,d;"
"out vec4 f;"
"vec3 x(vec3 v,vec3 m,vec3 f)"
"uniform samplerCube e;"
"uniform float t;"
"uniform vec3 a,m,l,i;"
"in vec3 c,v;"
"out vec4 o;"
"vec3 n(vec3 e,vec3 c,vec3 v)"
"{"
"float s=1.-clamp(dot(m,f),0.,1.);"
"return s*s*s*s*s*(1.-v)+v;"
"float o=1.-clamp(dot(c,v),0.,1.);"
"return o*o*o*o*o*(1.-e)+e;"
"}"
"void main()"
"{"
"vec3 s=normalize(d),v=normalize(m),e=n,z=c;"
"float p=.5;"
"vec3 x=t+mix(e*r,r,p);"
"f=vec4(x,1.);"
"vec3 e=normalize(v),r=normalize(c),t=m,n=i;"
"float f=.5;"
"vec3 u=l+mix(t*a,a,f);"
"o=vec4(u,1.);"
"}"
"vec3 x(vec3 v,vec3 m,vec3 f,vec3 n,vec3 d,float s)"
"vec3 n(vec3 e,vec3 c,vec3 v,vec3 o,vec3 r,float t)"
"{"
"vec3 r=normalize(v+m);"
"float c=1.+2048.*(1.-s)*(1.-s);"
"vec3 z=n,e=vec3(pow(clamp(dot(r,f),0.,1.),c)*(c+4.)/8.),t=x(d,v,r);"
"return mix(z,e,t);"
"vec3 a=normalize(e+c);"
"float m=1.+2048.*(1.-t)*(1.-t);"
"vec3 l=o,i=vec3(pow(clamp(dot(a,v),0.,1.),m)*(m+4.)/8.),f=n(r,e,a);"
"return mix(l,i,f);"
"}";

#endif // INOUT_EXPECTED_