public static MemoryStream LinkLibrary(ShaderLibrary lib) { MemoryStream memory = new MemoryStream(); memory.Write(BitConverter.GetBytes((int)lib.Type)); memory.Write(BitConverter.GetBytes(lib.Modules.Count)); foreach (var module in lib.Modules) { memory.Write(BitConverter.GetBytes(module.Name.Length)); memory.Write(Encoding.ASCII.GetBytes(module.Name)); memory.Write(BitConverter.GetBytes(module.ComponentNum)); foreach (var kv in module.Shaders) { memory.Write(BitConverter.GetBytes((int)kv.Value.type)); memory.Write(BitConverter.GetBytes((int)kv.Value.partBinary.Length)); memory.Write(kv.Value.partBinary); } } return(memory); }
public static LinkerTask Load(string inputFile) { XDocument xd = XDocument.Load(inputFile); ShaderLibrary library = new ShaderLibrary(); string outputName; var xdRoot = xd.Root; if (xdRoot.Name != "buildscript" && xdRoot.Attribute("type").Value != "ShaderLibrary") { throw new Exception(); } var meta = xdRoot.Element("meta"); string typeName = meta.Element("binary").Attribute("type").Value; library.Type = typeName.ToString() switch { "SPIRV" => LibraryType.SPIRV, "DXIL" => LibraryType.DXIL, _ => throw new ArgumentException() }; outputName = meta.Element("output").Attribute("path").Value; var xdModuleList = xdRoot.Elements("module"); foreach (var v in xdModuleList) { ShaderLibraryModule libModule = new ShaderLibraryModule(); libModule.Name = v.Attribute("name").Value; var xdShaders = v.Elements("shader"); foreach (var s in xdShaders) { ShaderLibraryModulePart shaderPart = new ShaderLibraryModulePart(); string filePath = s.Attribute("path").Value; shaderPart.partBinary = File.ReadAllBytes(filePath); shaderPart.type = s.Attribute("type").Value.ToString() switch { "Vertex" => ShaderType.Vertex, "Pixel" => ShaderType.Pixel, "Geometry" => ShaderType.Geometry, "Hull" => ShaderType.Hull, "Domain" => ShaderType.Domain, "Compute" => ShaderType.Compute, _ => throw new Exception() }; libModule.Shaders.Add(shaderPart.type, shaderPart); } library.Modules.Add(libModule); } return(new LinkerTask() { outputFileName = outputName, library = library }); } } }