using System.Collections.Frozen; using System.Text; using Brutal.KtxApi; using Brutal.KtxApi.Abstractions; namespace Brutal.TextureApi.Ktx; public static partial class Loader { /// /// Loads a ktx texture from a file. /// /// Path to file /// Options for the load /// Texture that was loaded /// If the texture could not be loaded. /// e.g The SuperCompressionFamily was not compatible with the texture's data, /// The file could not be found etc. /// public static KtxTexture LoadFromFile(string filePath, LoadSettings? loadOptions = null) { var loadSettings = loadOptions ?? LoadSettings.Default; var loadResult = KtxApi.KtxTexture.CreateFromNamedFile( Encoding.UTF8.GetBytes(filePath), LoadFlags(loadSettings), out var textureHandle ); if (loadResult != KtxErrorCode.Success) { textureHandle.Dispose(); throw new KtxTextureException( $"Failed to load texture from filePath \"{filePath}\". error:{loadResult}", loadResult ); } if (!textureHandle.NeedsTranscoding()) return new KtxTexture(textureHandle); var result = Transcode(textureHandle, loadSettings.SuperCompressionTranscodeFormat); if (result == KtxErrorCode.Success) return new KtxTexture(textureHandle); textureHandle.Dispose(); throw new KtxTextureException($"Failed to transcode supercompressed texture. KtxErrorCode:{result}"); } public static KtxTexture LoadFromMemory(ReadOnlySpan data, LoadSettings? loadData = null) { var loadSettings = loadData ?? new LoadSettings(); var createResult = KtxTexture2.CreateFromMemory(data, LoadFlags(loadSettings), out var texture2Handle); if (createResult != KtxErrorCode.Success) throw new KtxTextureException("Failed to create texture from memory", createResult); return new KtxTexture(texture2Handle); } public class LoadSettings : TextureLoadSettings { public static LoadSettings Default { get; } = new(); /// /// If the texture is supercompressed, this indicates the format to transcode it to on load. /// /// Defaults to KtxTranscodeFmt.Bc7Rgba public KtxTranscodeFmt SuperCompressionTranscodeFormat = KtxTranscodeFmt.Bc7Rgba; /// /// If true, does not immediately load the texture data. /// Data is loaded when accessed. /// This flag is ignored if the texture is super compressed and needs transcoding because transcoding /// will happen when loading the file. /// public bool LazyLoad = false; } private static KtxTextureCreateFlagBits LoadFlags(LoadSettings loadSettings) { return loadSettings.LazyLoad ? KtxTextureCreateFlagBits.NoFlags : KtxTextureCreateFlagBits.LoadImageDataBit; } private static KtxErrorCode Transcode(KtxTextureHandle textureHandle, KtxTranscodeFmt transcodeFormat) { var ktxTexture2Handle = textureHandle.AsKtxTexture2Handle(); var result = ktxTexture2Handle.TranscodeBasis(transcodeFormat, KtxTranscodeFlagBits.HighQuality); return result; } public static FrozenSet SupportedExtensions { get; } = FrozenSet.Create(StringComparer.OrdinalIgnoreCase, [".ktx", ".ktx2"]); }