| 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 |
| 4 | for ($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 | } |
| 14 | Add-Type -TypeDefinition @' |
| 15 | using System; using System.Runtime.InteropServices; |
| 16 | public 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) |
| 34 | if ($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) |