/// <summary> /// Saves shader code to disk. /// </summary> /// <param name="basePath">Base path of the shader cache</param> /// <param name="info">Array of shader program information for all stages</param> /// <param name="pack">Pack with spans of guest shader code</param> /// <param name="code">Host binary shader code</param> /// <param name="hash">Hash calculated from the guest shader code</param> /// <param name="index">Index to disambiguate the shader in case of hash collisions</param> public static void Save(string basePath, ShaderProgramInfo[] info, ShaderPack pack, byte[] code, int hash, int index) { ShaderCacheFileFormat scff = new ShaderCacheFileFormat(hash, pack, code, info); BinaryFormatter formatter = new BinaryFormatter(); string fileName = GetShaderPath(basePath, hash, index); using FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); formatter.Serialize(fs, scff); }
/// <summary> /// Loads all cached shaders from disk. /// </summary> /// <param name="basePath">Base path of the shader cache</param> /// <returns>Shader cache file data array</returns> public static ShaderCacheFileFormat[] Load(string basePath) { string[] files = Directory.GetFiles(basePath, $"*.{Extension}", SearchOption.TopDirectoryOnly); List <ShaderCacheFileFormat> cached = new List <ShaderCacheFileFormat>(); foreach (string fileName in files) { using FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryFormatter formatter = new BinaryFormatter(); ShaderCacheFileFormat scff = (ShaderCacheFileFormat)formatter.Deserialize(fs); cached.Add(scff); } return(cached.ToArray()); }