Skip to content

Commit

Permalink
Add Crinkler library, use it to generate report about compressed size…
Browse files Browse the repository at this point in the history
… of shaders
  • Loading branch information
laurentlb committed Jan 2, 2023
1 parent dbd5be5 commit 8bcb532
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 4 deletions.
32 changes: 30 additions & 2 deletions Checker/Checker.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>5</WarningLevel>
<DocumentationFile>bin\Debug\Checker.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
<StartWorkingDirectory>../../..</StartWorkingDirectory>
<StartArguments>--update-golden</StartArguments>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Release\Checker.XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
<StartWorkingDirectory>..\..\..</StartWorkingDirectory>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
Expand All @@ -58,7 +84,11 @@
</Choose>
<Import Project="$(FSharpTargetsPath)" />
<ItemGroup>
<Compile Include="compression_test.fs" />
<Compile Include="main.fs" />
<None Include="../lib/Compressor.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Reference Include="Argu">
Expand All @@ -71,8 +101,6 @@
<HintPath>..\lib\OpenTK.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shader Minifier Library.fsproj">
<Name>Shader Minifier Library</Name>
<Project>{059c6af3-1877-4698-be34-bf7eb55d060a}</Project>
Expand Down
66 changes: 66 additions & 0 deletions Checker/compression_test.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module CompressionTests

open System.Runtime.InteropServices
open System.IO
open System.Text

#nowarn "51" // use of native pointers

module Crinkler =
[<DllImport(@"Compressor.dll", CallingConvention = CallingConvention.Cdecl)>]
extern void InitCompressor()

[<DllImport(@"Compressor.dll", CallingConvention = CallingConvention.Cdecl)>]
extern single ApproximateModels4k(char* data, int datasize)


let testFiles = [
"from-the-seas-to-the-stars.frag"
"the_real_party_is_in_your_pocket.frag"
"ed-209.frag"
"valley_ball.glsl"
"lunaquatic.frag"
"slisesix.frag"
"yx_long_way_from_home.frag"
"oscars_chair.frag"
"kinder_painter.frag"
"ohanami.frag"
"terrarium.frag"
"leizex.frag"
"elevated.hlsl"
]

let writer = new StringWriter()

let log fmt =
let logger str =
printf "%s" str
writer.Write(str)

Printf.ksprintf logger fmt

let testFile (file: string) =
let args = if file.EndsWith("hlsl") then [|"--hlsl"|] else [||]
Options.init(args)
let minified =
use out = new StringWriter()
let shaders, exportedNames = ShaderMinifier.minifyFiles [|"tests/real/" + file|]
Formatter.print out shaders exportedNames Options.Text
out.ToString().ToCharArray()

let pointer = &&minified.[0]
log "%-40s " file
log "%5d " minified.Length
let compressedSize = Crinkler.ApproximateModels4k(pointer, minified.Length)
log "=> %8.3f\n" compressedSize
minified.Length, float compressedSize

let run () =
Crinkler.InitCompressor()

writer.GetStringBuilder().Clear() |> ignore<StringBuilder>
let sizes = List.map testFile testFiles
let minifiedSum = List.sumBy fst sizes
let compressedSum = List.sumBy snd sizes
log "Total: %5d => %9.3f\n" minifiedSum compressedSum
File.WriteAllText("tests/compression_results.log", writer.ToString())
42 changes: 42 additions & 0 deletions Shader Minifier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,71 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|x64.ActiveCfg = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|x64.Build.0 = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|x86.ActiveCfg = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Debug|x86.Build.0 = Debug|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|Any CPU.Build.0 = Release|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|x64.ActiveCfg = Release|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|x64.Build.0 = Release|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|x86.ActiveCfg = Release|Any CPU
{059C6AF3-1877-4698-BE34-BF7EB55D060A}.Release|x86.Build.0 = Release|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Debug|x64.ActiveCfg = Debug|x64
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Debug|x64.Build.0 = Debug|x64
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Debug|x86.ActiveCfg = Debug|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Release|Any CPU.Build.0 = Release|Any CPU
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Release|x64.ActiveCfg = Release|x64
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Release|x64.Build.0 = Release|x64
{CF735CB3-7698-41DA-9E91-69D0057087CB}.Release|x86.ActiveCfg = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|x64.ActiveCfg = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|x64.Build.0 = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|x86.ActiveCfg = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Debug|x86.Build.0 = Debug|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|Any CPU.Build.0 = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|x64.ActiveCfg = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|x64.Build.0 = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|x86.ActiveCfg = Release|Any CPU
{7A0B4487-C3F6-4753-9C8D-FEE09049F52D}.Release|x86.Build.0 = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|x64.ActiveCfg = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|x64.Build.0 = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|x86.ActiveCfg = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Debug|x86.Build.0 = Debug|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|Any CPU.Build.0 = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|x64.ActiveCfg = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|x64.Build.0 = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|x86.ActiveCfg = Release|Any CPU
{2DF47B2C-A68C-430C-B23F-BF51F68DF74D}.Release|x86.Build.0 = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|x64.ActiveCfg = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|x64.Build.0 = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|x86.ActiveCfg = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Debug|x86.Build.0 = Debug|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|Any CPU.Build.0 = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|x64.ActiveCfg = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|x64.Build.0 = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|x86.ActiveCfg = Release|Any CPU
{C1A4652F-0F92-4530-B1C8-2AAC9C383650}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Binary file added lib/Compressor.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion src/options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let private argParser = lazy (
let private initPrivate argv needFiles =
let args = argParser.Value.Parse(argv)

let opt = args.GetResult(NoRenamingList, defaultValue = "main")
let opt = args.GetResult(NoRenamingList, defaultValue = "main,mainImage")
let noRenamingList = [for i in opt.Split([|','|]) -> i.Trim()]
let filenames = args.GetResult(Filenames, defaultValue=[]) |> List.toArray

Expand Down
2 changes: 1 addition & 1 deletion tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
--format c-variables -o tests/real/heart.frag.expected tests/real/heart.frag

# Other - real files, to get a bit more coverage
--no-renaming --format indented --no-renaming-list mainImage -o tests/real/ed-209.frag.expected tests/real/ed-209.frag
--no-renaming --format indented -o tests/real/ed-209.frag.expected tests/real/ed-209.frag
--no-renaming --format indented -o tests/real/ohanami.frag.expected tests/real/ohanami.frag
--no-renaming --format indented -o tests/real/terrarium.frag.expected tests/real/terrarium.frag
--no-renaming --format indented -o tests/real/monjori.frag.expected tests/real/monjori.frag
Expand Down
14 changes: 14 additions & 0 deletions tests/compression_results.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from-the-seas-to-the-stars.frag 32480 => 2797.928
the_real_party_is_in_your_pocket.frag 17782 => 2279.606
ed-209.frag 11197 => 1479.056
valley_ball.glsl 5837 => 942.129
lunaquatic.frag 7237 => 1175.040
slisesix.frag 7115 => 1116.704
yx_long_way_from_home.frag 4494 => 712.244
oscars_chair.frag 6588 => 1166.886
kinder_painter.frag 4085 => 503.991
ohanami.frag 5019 => 846.307
terrarium.frag 5313 => 841.249
leizex.frag 3121 => 541.299
elevated.hlsl 4366 => 671.319
Total: 114634 => 15073.758

0 comments on commit 8bcb532

Please sign in to comment.