Rise
The Vieneo Province
Loading...
Searching...
No Matches
PackShaderResources11.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using System.Runtime.InteropServices;
6using Microsoft.Build.Framework;
7using Microsoft.Build.Utilities;
8
9// Build-time only. Each resource expands independently to its original DXBC.
10// API format: https://learn.microsoft.com/windows/win32/cmpapi/using-the-compression-api
11public sealed class PackShaderResources11 : Task
12{
13 [Required] public ITaskItem[] Files { get; set; }
14 public string ResourceFile { get; set; }
15 public string[] ResourceLines { get; set; }
16 [DllImport("cabinet.dll", SetLastError=true)]
17 static extern bool CreateCompressor(uint algorithm, IntPtr allocation, out IntPtr compressor);
18 [DllImport("cabinet.dll", SetLastError=true)]
19 static extern bool Compress(IntPtr compressor, byte[] input, UIntPtr inputBytes, byte[] output, UIntPtr outputBytes, out UIntPtr written);
20 [DllImport("cabinet.dll")]
21 static extern bool CloseCompressor(IntPtr compressor);
22
23 public override bool Execute()
24 {
25 long inputTotal=0, outputTotal=0;
26 foreach(var file in Files)
27 {
28 IntPtr compressor=IntPtr.Zero;
29 try
30 {
31 var raw=File.ReadAllBytes(file.ItemSpec);
32 if(raw.Length<4 || raw.Length>16*1024*1024 || raw[0]!='D' || raw[1]!='X' || raw[2]!='B' || raw[3]!='C')
33 throw new InvalidDataException("Expected bounded DXBC bytecode: "+file.ItemSpec);
34 if(!CreateCompressor(4,IntPtr.Zero,out compressor))throw new System.ComponentModel.Win32Exception();
35 UIntPtr required;
36 if(Compress(compressor,raw,(UIntPtr)raw.Length,null,UIntPtr.Zero,out required) || Marshal.GetLastWin32Error()!=122)
37 throw new System.ComponentModel.Win32Exception();
38 var compressed=new byte[checked((int)required.ToUInt64())];
39 UIntPtr written;
40 if(!Compress(compressor,raw,(UIntPtr)raw.Length,compressed,(UIntPtr)compressed.Length,out written))
41 throw new System.ComponentModel.Win32Exception();
42 byte[] packed=raw;
43 int length=checked((int)written.ToUInt64());
44 if(length+8<raw.Length)
45 {
46 packed=new byte[length+8];packed[0]=(byte)'R';packed[1]=(byte)'Z';packed[2]=(byte)'1';packed[3]=(byte)'1';
47 Buffer.BlockCopy(BitConverter.GetBytes(raw.Length),0,packed,4,4);
48 Buffer.BlockCopy(compressed,0,packed,8,length);
49 }
50 var output=Path.ChangeExtension(file.ItemSpec,".csz");
51 if(!File.Exists(output) || !File.ReadAllBytes(output).SequenceEqual(packed))File.WriteAllBytes(output,packed);
52 inputTotal+=raw.Length;outputTotal+=packed.Length;
53 }
54 catch(Exception error){Log.LogErrorFromException(error,true);return false;}
55 finally{if(compressor!=IntPtr.Zero)CloseCompressor(compressor);}
56 }
57 // WriteLinesToFile with a UTF-8 BOM rewrites identical output on some
58 // MSBuild versions. Compare the actual bytes to avoid a resource compile
59 // and relink after every warm-cache build.
60 if(!String.IsNullOrEmpty(ResourceFile))
61 {
62 var encoding=new UTF8Encoding(true);
63 var resource=encoding.GetPreamble().Concat(encoding.GetBytes(String.Join("\r\n",ResourceLines)+"\r\n")).ToArray();
64 if(!File.Exists(ResourceFile) || !File.ReadAllBytes(ResourceFile).SequenceEqual(resource))File.WriteAllBytes(ResourceFile,resource);
65 }
66 Log.LogMessage(MessageImportance.High,"Packed {0} shader resources: {1:N0} -> {2:N0} bytes (lossless)",Files.Length,inputTotal,outputTotal);
67 return true;
68 }
69}
static bool CloseCompressor(IntPtr compressor)
static bool CreateCompressor(uint algorithm, IntPtr allocation, out IntPtr compressor)
static bool Compress(IntPtr compressor, byte[] input, UIntPtr inputBytes, byte[] output, UIntPtr outputBytes, out UIntPtr written)