BlueGit

ict rusty-teams

Files

rusty-teams / scripts / fxc-shim.ps1

fxc-shim.ps1

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
fxc-shim.ps1 2.6 KiB Download Raw
1# fxc.exe stand-in: compiles HLSL via d3dcompiler_47.dll (ships with Windows). Args as fxc: /T /E /Fh /Vn /O3 <file>
2$FxcArgs = $args
3$target = $null; $entry = $null; $header = $null; $varName = $null; $source = $null
4for ($index = 0; $index -lt $FxcArgs.Count; $index++) {
5 switch ($FxcArgs[$index]) {
6 '/T' { $target = $FxcArgs[++$index] }
7 '/E' { $entry = $FxcArgs[++$index] }
8 '/Fh' { $header = $FxcArgs[++$index] }
9 '/Vn' { $varName = $FxcArgs[++$index] }
10 '/O3' { }
11 default { $source = $FxcArgs[$index] }
12 }
13}
14Add-Type -TypeDefinition @'
15using System; using System.Runtime.InteropServices;
16public static class D3DC {
17 [DllImport("d3dcompiler_47.dll", CharSet=CharSet.Unicode)]
18 public static extern int D3DCompileFromFile(string file, IntPtr defines, IntPtr include, [MarshalAs(UnmanagedType.LPStr)] string entry, [MarshalAs(UnmanagedType.LPStr)] string target, uint flags1, uint flags2, out IntPtr code, out IntPtr errors);
19 [DllImport("d3dcompiler_47.dll")] public static extern int D3DDisassemble(IntPtr a, IntPtr b, uint c, IntPtr d, out IntPtr e);
20 public static byte[] Blob(IntPtr blob) {
21 IntPtr vtable = Marshal.ReadIntPtr(blob);
22 var getPointer = (GetPtr)Marshal.GetDelegateForFunctionPointer(Marshal.ReadIntPtr(vtable, 3 * IntPtr.Size), typeof(GetPtr));
23 var getSize = (GetSize)Marshal.GetDelegateForFunctionPointer(Marshal.ReadIntPtr(vtable, 4 * IntPtr.Size), typeof(GetSize));
24 IntPtr data = getPointer(blob); int size = (int)getSize(blob);
25 byte[] bytes = new byte[size]; Marshal.Copy(data, bytes, 0, size); return bytes;
26 }
27 [UnmanagedFunctionPointer(CallingConvention.StdCall)] delegate IntPtr GetPtr(IntPtr self);
28 [UnmanagedFunctionPointer(CallingConvention.StdCall)] delegate UIntPtr GetSize(IntPtr self);
29}
30'@
31$code = [IntPtr]::Zero; $errors = [IntPtr]::Zero
32# 1 = D3D_COMPILE_STANDARD_FILE_INCLUDE; flags: optimization level 3 = 1 shl 15
33$result = [D3DC]::D3DCompileFromFile($source, [IntPtr]::Zero, [IntPtr]1, $entry, $target, 32768, 0, [ref]$code, [ref]$errors)
34if ($result -lt 0) {
35 if ($errors -ne [IntPtr]::Zero) { [Console]::Error.WriteLine([Text.Encoding]::ASCII.GetString([D3DC]::Blob($errors))) }
36 [Console]::Error.WriteLine("D3DCompileFromFile failed: 0x{0:X8}" -f $result)
37 exit 1
38}
39$bytes = [D3DC]::Blob($code)
40$lines = for ($offset = 0; $offset -lt $bytes.Length; $offset += 8) {
41 ' ' + ((($bytes[$offset..([Math]::Min($offset + 7, $bytes.Length - 1))]) | ForEach-Object { '0x{0:x2}' -f $_ }) -join ', ')
42}
43$text = "const BYTE $varName[] =`n{`n" + ($lines -join ",`n") + "`n};`n"
44[IO.File]::WriteAllText($header, $text)