Skip to content

Commit

Permalink
New formatting output: indented text
Browse files Browse the repository at this point in the history
This is a more human-friendly output, for debugging and testing
  • Loading branch information
laurentlb committed Jun 6, 2021
1 parent 41ccd71 commit ce26381
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 88 deletions.
9 changes: 9 additions & 0 deletions src/formatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ let private printNoHeader out (shaders: Ast.Shader[]) =
let str = [for shader in shaders -> Printer.print shader.code] |> String.concat "\n"
fprintf out "%s" str

let private printIndented out (shaders: Ast.Shader[]) =
[for shader in shaders do
if shaders.Length > 1 then
yield "// " + shader.filename
yield Printer.print shader.code]
|> String.concat "\n\n"
|> fprintf out "%s"

let private printJSHeader out (shaders: Ast.Shader[]) exportedNames =
fprintfn out "/* File generated with Shader Minifier %s" Options.version
fprintfn out " * http://www.ctrl-alt-test.fr"
Expand Down Expand Up @@ -75,6 +83,7 @@ let private printNasmHeader out (shaders: Ast.Shader[]) exportedNames =
fprintfn out ""

let print out shaders exportedNames = function
| Options.IndentedText -> printIndented out shaders
| Options.Text -> printNoHeader out shaders
| Options.CHeader -> printHeader out shaders false exportedNames
| Options.CList -> printHeader out shaders true exportedNames
Expand Down
1 change: 1 addition & 0 deletions src/options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let debugMode = false

type OutputFormat =
| [<CustomCommandLine("text")>] Text
| [<CustomCommandLine("indented")>] IndentedText
| [<CustomCommandLine("c-variables")>] CHeader
| [<CustomCommandLine("c-array")>] CList
| [<CustomCommandLine("js")>] JS
Expand Down
2 changes: 2 additions & 0 deletions src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ module private PrinterImpl =
else
let spaces = new string(' ', indent * 2 + 1)
match outputFormat with
| Options.IndentedText -> Environment.NewLine + new string(' ', indent * 2)
| Options.Text | Options.JS -> ""
| Options.CHeader | Options.CList -> out "\"%s%s\"" Environment.NewLine spaces
| Options.Nasm -> out "'%s\tdb%s'" Environment.NewLine spaces

let escape (s: string) =
match outputFormat with
| Options.IndentedText -> s
| Options.Text -> s
| Options.JS -> s
| Options.CHeader | Options.CList | Options.JS -> s.Replace("\"", "\\\"").Replace("\n", "\\n")
Expand Down
4 changes: 2 additions & 2 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
--hlsl --no-renaming --format c-variables -o tests/unit/geometry.hlsl.expected tests/unit/geometry.hlsl
--no-renaming --format c-array -o tests/unit/operators.expected tests/unit/operators.frag
--no-renaming --format c-array -o tests/unit/minus-zero.expected tests/unit/minus-zero.frag
--no-renaming -o tests/unit/inline.expected tests/unit/inline.frag
--no-renaming --format indented -o tests/unit/inline.expected tests/unit/inline.frag

# Partial renaming tests

Expand All @@ -24,7 +24,7 @@

# Multifile tests

--format c-variables -o tests/unit/inout.expected tests/unit/inout.frag tests/unit/inout2.frag
--format indented -o tests/unit/inout.expected tests/unit/inout.frag tests/unit/inout2.frag

# Tests with full renaming

Expand Down
33 changes: 12 additions & 21 deletions tests/unit/inline.expected
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef INLINE_EXPECTED_
# define INLINE_EXPECTED_

const char *inline_frag =
"void main()"
"{"
"float x=.5,a=x*(.6*x);"
"}"
"int arithmetic()"
"{"
"return 40;"
"}"
"int vars(int arg,int arg2)"
"{"
"return arg*(arg+arg2);"
"}";

#endif // INLINE_EXPECTED_
void main()
{
float x=.5,a=x*(.6*x);
}
int arithmetic()
{
return 40;
}
int vars(int arg,int arg2)
{
return arg*(arg+arg2);
}
117 changes: 52 additions & 65 deletions tests/unit/inout.expected
Original file line number Diff line number Diff line change
@@ -1,68 +1,55 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef INOUT_EXPECTED_
# define INOUT_EXPECTED_
# 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"
// tests/unit/inout.frag

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

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

#endif // INOUT_EXPECTED_
// tests/unit/inout2.frag

#version 330

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

0 comments on commit ce26381

Please sign in to comment.