mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
namespace Brutal.TextureApi.Gli;
|
|
|
|
public static partial class Writer
|
|
{
|
|
public static void SaveToFile(ITexture texture, string filePath)
|
|
{
|
|
if (texture is GliTexture gliTexture)
|
|
{
|
|
SaveToFileImpl(gliTexture, filePath);
|
|
return;
|
|
}
|
|
|
|
SaveToFile(
|
|
new TextureDescriptor
|
|
{
|
|
Data = texture.Data,
|
|
Extent = texture.Extent,
|
|
Format = texture.Format,
|
|
LayerCount = texture.LayerCount,
|
|
FaceCount = texture.FaceCount,
|
|
MipmapCount = texture.LevelCount,
|
|
IsCubemap = texture.IsCubemap,
|
|
},
|
|
filePath
|
|
);
|
|
}
|
|
|
|
public static void SaveToFile(TextureDescriptor descriptor, string filePath)
|
|
{
|
|
var gliTarget = GliConversionUtils.GetTarget(descriptor.Extent, descriptor.LayerCount, descriptor.IsCubemap);
|
|
|
|
using var gliNative = new GliApi.Gli.Texture(
|
|
gliTarget,
|
|
descriptor.Format.ToGliFormat(),
|
|
new(descriptor.Extent.X, descriptor.Extent.Y, descriptor.Extent.Z),
|
|
descriptor.LayerCount,
|
|
descriptor.FaceCount,
|
|
descriptor.MipmapCount,
|
|
GliApi.Gli.Swizzles.Identity
|
|
);
|
|
|
|
unsafe
|
|
{
|
|
descriptor.Data.CopyTo(new Span<byte>(gliNative.Data, descriptor.Data.Length));
|
|
}
|
|
|
|
SaveToFileImpl(gliNative, filePath);
|
|
}
|
|
|
|
private static void SaveToFileImpl(GliApi.Gli.Texture texture, string filePath)
|
|
{
|
|
var result = texture.Save(filePath);
|
|
|
|
if (!result)
|
|
{
|
|
throw new GliTextureSaveException($"Failed to save texture to file at path: \"{filePath}\".");
|
|
}
|
|
}
|
|
}
|