summaryrefslogtreecommitdiffstats
path: root/Tools/CompressShaders/CompressShaders.cs
blob: 814f96647ef423d2d4b836f1aaa1b117a72b377d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Runtime.CompilerServices;
namespace CompressShaders;

record struct sShaderBinary
{
	public string name;
	public byte[] data;

	public sShaderBinary( string path )
	{
		name = Path.GetFileNameWithoutExtension( path );
		data = File.ReadAllBytes( path );
	}

	public bool wave64 => name.EndsWith( "64" );
	public string uniqueName => wave64 ? name.Substring( 0, name.Length - 2 ) : name;
}

sealed class FoundShaders
{
	public readonly sShaderBinary[] binaries;
	public readonly string[] names;
	public readonly int[] wave32, wave64;

	public FoundShaders( IEnumerable<sShaderBinary> found )
	{
		binaries = found
			.OrderBy( b => b.name )
			.ToArray();

		names = binaries
			.Select( b => b.uniqueName )
			.Distinct()
			.ToArray();

		wave32 = new int[ names.Length ];
		wave64 = new int[ names.Length ];
		for( int i = 0; i < names.Length; i++ )
		{
			int i32 = findIndex( names[ i ], false );
			int i64 = findIndex( names[ i ], true );
			if( i32 >= 0 && i64 >= 0 )
			{
				wave32[ i ] = i32;
				wave64[ i ] = i64;
				continue;
			}
			if( i32 >= 0 )
			{
				wave32[ i ] = wave64[ i ] = i32;
				continue;
			}
			throw new ApplicationException( $"Wave64 shader {names[ i ]} doesn't have the corresponding Wave32 one" );
		}
	}

	int findIndex( string name, bool wave64 )
	{
		for( int i = 0; i < binaries.Length; i++ )
		{
			sShaderBinary sb = binaries[ i ];
			if( sb.uniqueName != name )
				continue;
			if( sb.wave64 == wave64 )
				return i;
		}
		return -1;
	}
}

class Program
{
	static string getSolutionRoot( [CallerFilePath] string? path = null )
	{
		string? dir = Path.GetDirectoryName( path );
		dir = Path.GetDirectoryName( dir );
		dir = Path.GetDirectoryName( dir );
		return dir ?? throw new ApplicationException();
	}

#if DEBUG
	const string config = "Debug";
#else
	const string config = "Release";
#endif

	static string shadersBinDir( string root )
	{
		return Path.Combine( root, "ComputeShaders", "x64", config );
	}

	static IEnumerable<sShaderBinary> readShaders( string root )
	{
		string dir = shadersBinDir( root );
		foreach( string path in Directory.EnumerateFiles( dir, "*.cso" ) )
			yield return new sShaderBinary( path );
	}

	static void writeHeader( string root, IEnumerable<string> names )
	{
		string path = Path.Combine( root, "Whisper", "D3D", "shaderNames.h" );
		using var stream = File.CreateText( path );
		stream.WriteLine( @"// This header is generated by a tool
#pragma once
#include <stdint.h>

namespace DirectCompute
{
	enum struct eComputeShader: uint16_t
	{" );

		int id = 0;
		foreach( string name in names )
		{
			stream.WriteLine( "\t\t{0} = {1},", name, id );
			id++;
		}
		stream.Write( @"	};

	const char* computeShaderName( eComputeShader cs );
}" );
	}

	static void writeCpp( string root, IEnumerable<string> names )
	{
		string path = Path.Combine( root, "Whisper", "D3D", "shaderNames.cpp" );
		ShaderNames.write( path, names );
	}

	static void writePayloadIDs( StreamWriter stream, string varName, int[] ids )
	{
		stream.Write( @"
static const std::array<uint8_t, {0}> {1} = {{", ids.Length, varName );

		for( int i = 0; i < ids.Length; i++ )
		{
			if( 0 == i % 16 )
				stream.Write( "\r\n\t" );
			else
				stream.Write( ' ' );
			stream.Write( "{0},", ids[ i ] );
		}
		stream.Write( @"
};" );
	}

	static void writePayload( string root, FoundShaders shaders, out int cbSource, out int cbCompressed )
	{
		MemoryStream ms = new MemoryStream();
		List<int> offsets = new List<int>();
		foreach( var bin in shaders.binaries )
		{
			offsets.Add( (int)ms.Length );
			ms.Write( bin.data );
		}
		offsets.Add( (int)ms.Length );

		byte[] dxbc = ms.ToArray();
		byte[] compressed = Cabinet.compressBuffer( dxbc );
		cbSource = dxbc.Length;
		cbCompressed = compressed.Length;

		string path = Path.Combine( root, "Whisper", "D3D", $"shaderData-{config}.inl" );
		using var stream = File.CreateText( path );
		stream.Write( @"// This source file is generated by a tool

// This array contains concatenated and compressed DXBC binaries for all compiled compute shaders
static const std::array<uint8_t, {0}> s_compressedShaders =
{{", compressed.Length );

		for( int i = 0; i < compressed.Length; i++ )
		{
			if( 0 == i % 16 )
				stream.Write( "\r\n\t" );
			else
				stream.Write( ' ' );
			stream.Write( "0x{0:X02},", compressed[ i ] );
		}

		stream.Write( @"
}};

// This array contains start offsets of shader binaries in the decompressed DXBC blob.
// It includes one more entry for the end of the complete decompressed blob.
static const std::array<uint32_t, {0}> s_shaderOffsets = {{", offsets.Count );

		for( int i = 0; i < offsets.Count; i++ )
		{
			if( 0 == i % 16 )
				stream.Write( "\r\n\t" );
			else
				stream.Write( ' ' );
			stream.Write( "{0},", offsets[ i ] );
		}
		stream.Write( @"
};" );

		stream.Write( @"
// Index = eComputeShader enum value, value = index of the shader binary to use on nVidia and Intel GPUs" );
		writePayloadIDs( stream, "s_shaderBlobs32", shaders.wave32 );
		stream.Write( @"
// Index = eComputeShader enum value, value = index of the shader binary to use on AMD GPUs" );
		writePayloadIDs( stream, "s_shaderBlobs64", shaders.wave64 );

		ulong fp64Flags = 0;
		for( int i = 0; i < shaders.binaries.Length; i++ )
		{
			bool fp64 = DetectFp64.usesFp64( shaders.binaries[ i ].data );
			if( fp64 )
				fp64Flags |= (ulong)1 << i;
		}

		stream.Write( @"
// Bitmap of the shader binaries which use FP64 arithmetic instructions
constexpr uint64_t fp64ShadersBitmap = 0x{0:X}ull;", fp64Flags );
	}

	static void mainImpl()
	{
		string root = getSolutionRoot();
		LanguageCodes.produce( root );

		FoundShaders shaders = new FoundShaders( readShaders( root ) );

		writeHeader( root, shaders.names );
		writeCpp( root, shaders.names );
		writePayload( root, shaders, out int cbIn, out int cbOut );
		Console.WriteLine( "Compressed {0} compute shaders, {1:F1} kb -> {2:F1} kb", shaders.binaries.Length, cbIn / 1024.0, cbOut / 1024.0 );
	}

	static int Main( string[] args )
	{
		try
		{
			mainImpl();
			return 0;
		}
		catch( Exception ex )
		{
			Console.WriteLine( ex.Message );
			return ex.HResult;
		}
	}
}