-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #314
- Loading branch information
Showing
12 changed files
with
140 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Shader Minifier Library.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Example of use of the ShaderMinifier library from C#. | ||
*/ | ||
|
||
using ShaderMinifier; | ||
|
||
var shader = """ | ||
out vec4 fragColor; | ||
void main() | ||
{ | ||
fragColor = vec4(1., 1., 1., 1.); | ||
} | ||
"""; | ||
|
||
var file = Tuple.Create("filename.frag", shader); | ||
var options = Minifier.ParseOptions(new[] { "--format", "text" }); | ||
var minifier = new Minifier(options, new[] { file }); | ||
minifier.Format(System.Console.Out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,51 @@ | ||
module ShaderMinifier | ||
namespace ShaderMinifier | ||
|
||
open System.IO | ||
|
||
let getSize (shaders: Ast.Shader[]) = | ||
shaders |> Array.sumBy (fun s -> Printer.print s.code |> String.length) | ||
|
||
let minify (options: Options.Options) (files: (string*string)[]) = | ||
// like printfn when verbose option is set | ||
let vprintf fmt = fprintf (if options.verbose then stdout else TextWriter.Null) fmt | ||
|
||
let printSize (shaders: Ast.Shader[]) = | ||
if options.verbose then | ||
let length = getSize shaders | ||
vprintf "Shader size is: %d\n" length | ||
|
||
let names = String.concat "," [for n, c in files -> $"'{n}' ({c.Length}b)"] | ||
options.trace $"----- minifying {names}" | ||
vprintf "Input file size is: %d\n" (files |> Array.sumBy (fun (_, s) -> s.Length)) | ||
|
||
let parseAndRewrite (filename, content) = | ||
let shader = Parse.runParser options filename content | ||
let code = | ||
if shader.reorderFunctions then | ||
Rewriter.reorderFunctions options shader.code | ||
else shader.code | ||
{ shader with code = Rewriter.simplify options code } | ||
|
||
let shaders = Array.Parallel.map parseAndRewrite files | ||
vprintf "Rewrite tricks applied. "; printSize shaders | ||
|
||
if options.noRenaming then | ||
shaders, [] | ||
else | ||
let exportedNames = Renamer.rename options shaders | ||
vprintf "Identifiers renamed. "; printSize shaders | ||
shaders, exportedNames | ||
|
||
let format = Formatter.print | ||
let formatWithLocations = Formatter.printWithLocations | ||
let print = Printer.print | ||
type Minifier(options, files) = | ||
let getSize (shaders: Ast.Shader[]) = | ||
shaders |> Seq.sumBy (fun s -> Printer.print s.code |> String.length) | ||
|
||
let minify (options: Options.Options) (files: (string*string)[]) = | ||
// like printfn when verbose option is set | ||
let vprintf fmt = fprintf (if options.verbose then stdout else TextWriter.Null) fmt | ||
|
||
let printSize (shaders: Ast.Shader[]) = | ||
if options.verbose then | ||
let length = getSize shaders | ||
vprintf "Shader size is: %d\n" length | ||
|
||
let names = String.concat "," [for n, c in files -> $"'{n}' ({c.Length}b)"] | ||
options.trace $"----- minifying {names}" | ||
vprintf "Input file size is: %d\n" (files |> Array.sumBy (fun (_, s) -> s.Length)) | ||
|
||
let parseAndRewrite (filename, content) = | ||
let shader = Parse.runParser options filename content | ||
let code = | ||
if shader.reorderFunctions then | ||
Rewriter.reorderFunctions options shader.code | ||
else shader.code | ||
{ shader with code = Rewriter.simplify options code } | ||
|
||
let shaders = Array.Parallel.map parseAndRewrite files | ||
vprintf "Rewrite tricks applied. "; printSize shaders | ||
|
||
if options.noRenaming then | ||
shaders, [||] | ||
else | ||
let exportedNames = Renamer.rename options (Seq.toArray shaders) |> List.toArray | ||
vprintf "Identifiers renamed. "; printSize shaders | ||
shaders, exportedNames | ||
|
||
let shaders, exportedNames = minify options files | ||
|
||
static member ParseOptions(flags) = Options.init flags | ||
static member ParseOptionsWithFiles(flags) = Options.initFiles flags | ||
|
||
member _.GetSize = getSize shaders | ||
member _.GetShaders = shaders | ||
|
||
member _.Format(writer) = Formatter.print options writer shaders exportedNames | ||
member _.Format(writer, options) = | ||
Formatter.print options writer shaders exportedNames | ||
member _.FormatWithLocations(writer) = Formatter.printWithLocations options writer shaders exportedNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.