| 1 | # Compiles draw.hlsl and translucent.hlsl to the shader model 4.0 bytecode d3d11.rs embeds, |
| 2 | # with the d3dcompiler_47.dll Windows 8.1 and later ship: `powershell -File compile.ps1`. |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | Add-Type -TypeDefinition @' |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Runtime.InteropServices; |
| 8 | using System.Text; |
| 9 | |
| 10 | [ComImport, Guid("8BA5FB08-5195-40e2-AC58-0D989C3A0102"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 11 | public interface ID3DBlob { |
| 12 | [PreserveSig] IntPtr GetBufferPointer(); |
| 13 | [PreserveSig] UIntPtr GetBufferSize(); |
| 14 | } |
| 15 | |
| 16 | public static class Fxc { |
| 17 | [DllImport("d3dcompiler_47.dll", CharSet = CharSet.Ansi)] |
| 18 | static extern int D3DCompile(byte[] source, UIntPtr size, string name, IntPtr defines, |
| 19 | IntPtr include, string entry, string target, uint flags, uint effectFlags, |
| 20 | out ID3DBlob code, out ID3DBlob errors); |
| 21 | |
| 22 | static byte[] Bytes(ID3DBlob blob) { |
| 23 | var bytes = new byte[(int)blob.GetBufferSize()]; |
| 24 | Marshal.Copy(blob.GetBufferPointer(), bytes, 0, bytes.Length); |
| 25 | return bytes; |
| 26 | } |
| 27 | |
| 28 | public static byte[] Compile(string path, string entry, string target) { |
| 29 | var source = File.ReadAllBytes(path); |
| 30 | ID3DBlob code, errors; |
| 31 | // D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_OPTIMIZATION_LEVEL3 |
| 32 | int result = D3DCompile(source, (UIntPtr)source.Length, Path.GetFileName(path), |
| 33 | IntPtr.Zero, IntPtr.Zero, entry, target, (1u << 11) | (1u << 15), 0, |
| 34 | out code, out errors); |
| 35 | if (result < 0) { |
| 36 | throw new Exception(errors == null ? "D3DCompile: 0x" + result.ToString("x8") |
| 37 | : Encoding.ASCII.GetString(Bytes(errors))); |
| 38 | } |
| 39 | return Bytes(code); |
| 40 | } |
| 41 | } |
| 42 | '@ |
| 43 | foreach ($shader in @( |
| 44 | @('..\..\draw.hlsl', 'draw'), |
| 45 | @('..\translucent.hlsl', 'translucent') |
| 46 | )) { |
| 47 | $source = Join-Path $PSScriptRoot $shader[0] |
| 48 | foreach ($stage in @(@('vertex', 'vs_4_0'), @('fragment', 'ps_4_0'))) { |
| 49 | $out = Join-Path $PSScriptRoot ($shader[1] + '.' + $stage[0] + '.dxbc') |
| 50 | [IO.File]::WriteAllBytes($out, [Fxc]::Compile($source, $stage[0], $stage[1])) |
| 51 | } |
| 52 | } |