mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-28 14:08:25 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
6 changed files with 784842 additions and 0 deletions
18
Brutal.Ktx/Abstractions/GpuUploadUtils.cs
Normal file
18
Brutal.Ktx/Abstractions/GpuUploadUtils.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
namespace Brutal.KtxApi.Abstractions;
|
||||
|
||||
public static class GpuUploadUtils
|
||||
{
|
||||
public static bool IsRowSize4BytePadded(KtxTextureHandle textureHandle, int level)
|
||||
{
|
||||
if (textureHandle.ClassId == ClassId.Texture2C)
|
||||
return false;
|
||||
|
||||
// Catches compressed textures (All multiple of 4 in element size), and uncompressed with 4 byte elements
|
||||
if (textureHandle.GetElementSize() % 4 == 0)
|
||||
return false;
|
||||
|
||||
var levelPixelWidth = textureHandle.BaseWidth >> level;
|
||||
|
||||
return textureHandle.GetRowPitch((uint)level) != textureHandle.GetElementSize() * levelPixelWidth;
|
||||
}
|
||||
}
|
||||
25
Brutal.Ktx/Abstractions/KtxAstcParamsPartial.cs
Normal file
25
Brutal.Ktx/Abstractions/KtxAstcParamsPartial.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
namespace Brutal.KtxApi;
|
||||
|
||||
public partial struct KtxAstcParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialises the StructSize field to the correct value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This constructor only runs when you call <c>new KtxAstcParams()</c> explicitly - it is
|
||||
/// bypassed by <c>default(KtxAstcParams)</c>, array elements (<c>new KtxAstcParams[n]</c>),
|
||||
/// and struct fields in other types, all of which leave StructSize at 0. Prefer
|
||||
/// <see cref="Create"/> when you're not immediately assigning every field via an object
|
||||
/// initializer.
|
||||
/// </remarks>
|
||||
public unsafe KtxAstcParams()
|
||||
{
|
||||
StructSize = (uint)sizeof(KtxAstcParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a KtxAstcParams with StructSize correctly initialised. Preferred over
|
||||
/// <c>default(KtxAstcParams)</c> or array/field initialization, which leave StructSize at 0.
|
||||
/// </summary>
|
||||
public static KtxAstcParams Create() => new();
|
||||
}
|
||||
25
Brutal.Ktx/Abstractions/KtxBasisParamsPartial.cs
Normal file
25
Brutal.Ktx/Abstractions/KtxBasisParamsPartial.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
namespace Brutal.KtxApi;
|
||||
|
||||
public partial struct KtxBasisParams
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialises the StructSize field to the correct value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This constructor only runs when you call <c>new KtxBasisParams()</c> explicitly - it is
|
||||
/// bypassed by <c>default(KtxBasisParams)</c>, array elements (<c>new KtxBasisParams[n]</c>),
|
||||
/// and struct fields in other types, all of which leave StructSize at 0. Prefer
|
||||
/// <see cref="Create"/> when you're not immediately assigning every field via an object
|
||||
/// initializer.
|
||||
/// </remarks>
|
||||
public unsafe KtxBasisParams()
|
||||
{
|
||||
StructSize = (uint)sizeof(KtxBasisParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a KtxBasisParams with StructSize correctly initialised. Preferred over
|
||||
/// <c>default(KtxBasisParams)</c> or array/field initialization, which leave StructSize at 0.
|
||||
/// </summary>
|
||||
public static KtxBasisParams Create() => new();
|
||||
}
|
||||
40
Brutal.Ktx/Abstractions/KtxNativeBuffer.cs
Normal file
40
Brutal.Ktx/Abstractions/KtxNativeBuffer.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace Brutal.KtxApi;
|
||||
|
||||
/// <summary>
|
||||
/// Owns a native buffer returned by WriteToMemory/WriteKtx2ToMemory. The buffer is allocated with
|
||||
/// malloc() inside the native KTX library, so it must be freed with the matching native free() -
|
||||
/// Dispose() does this for you. Do not free the underlying pointer with any other allocator (e.g.
|
||||
/// Brutal.Memory.Mem.Free), as that frees memory with a different allocator than the one that
|
||||
/// allocated it, corrupting the heap.
|
||||
/// </summary>
|
||||
public unsafe struct KtxNativeBuffer : IDisposable
|
||||
{
|
||||
private byte* _pointer;
|
||||
|
||||
/// <summary>The size of the buffer in bytes.</summary>
|
||||
public ulong Length { get; }
|
||||
|
||||
/// <summary>Raw pointer to the native buffer.</summary>
|
||||
public byte* Pointer => _pointer;
|
||||
|
||||
/// <summary>
|
||||
/// A read-only view of the buffer. Throws if Length exceeds int.MaxValue, since Span<T>
|
||||
/// cannot address more than 2GiB - use Pointer and Length directly for buffers that large.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<byte> Span => new(_pointer, checked((int)Length));
|
||||
|
||||
internal KtxNativeBuffer(byte* pointer, ulong length)
|
||||
{
|
||||
_pointer = pointer;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_pointer is null)
|
||||
return;
|
||||
|
||||
KtxNativeMemory.Free(_pointer);
|
||||
_pointer = null;
|
||||
}
|
||||
}
|
||||
13
Brutal.Ktx/Abstractions/KtxNativeMemory.cs
Normal file
13
Brutal.Ktx/Abstractions/KtxNativeMemory.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Brutal.KtxApi;
|
||||
|
||||
/// <summary>
|
||||
/// Binds to the native free() used by the KTX library itself, for releasing buffers it allocated
|
||||
/// with malloc() (e.g. via WriteToMemory/WriteKtx2ToMemory). See KtxNativeBuffer.
|
||||
/// </summary>
|
||||
internal static unsafe partial class KtxNativeMemory
|
||||
{
|
||||
[LibraryImport("Ktx", EntryPoint = "ktx_FreeWriteToMemoryBuffer1")]
|
||||
public static partial void Free(void* ptr);
|
||||
}
|
||||
137
Brutal.Ktx/Abstractions/KtxTextureExtensions.cs
Normal file
137
Brutal.Ktx/Abstractions/KtxTextureExtensions.cs
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
using System.Text;
|
||||
using Brutal.Numerics;
|
||||
|
||||
namespace Brutal.KtxApi.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Provides useful extensions to the KTX texture types.
|
||||
/// </summary>
|
||||
public static class KtxTextureExtensions
|
||||
{
|
||||
#region File IO
|
||||
|
||||
public static KtxErrorCode WriteToNamedFile(this KtxTexture2Handle textureHandle, string filePath) =>
|
||||
textureHandle.WriteToNamedFile(Encoding.UTF8.GetBytes(filePath + "\0"));
|
||||
|
||||
public static KtxErrorCode WriteToNamedFile(this KtxTexture1Handle textureHandle, string filePath) =>
|
||||
textureHandle.WriteToNamedFile(Encoding.UTF8.GetBytes(filePath + "\0"));
|
||||
|
||||
public static KtxErrorCode WriteToNamedFile(this KtxTextureHandle textureHandle, string filePath) =>
|
||||
textureHandle.WriteToNamedFile(Encoding.UTF8.GetBytes(filePath + "\0"));
|
||||
|
||||
#endregion
|
||||
|
||||
#region Data Access
|
||||
/// <summary>
|
||||
/// Returns a span covering the image data for a given level, layer and face.
|
||||
/// </summary>
|
||||
/// <param name="textureHandle">Handle to KTX texture of any version</param>
|
||||
/// <param name="level">The mip level to retrieve data for</param>
|
||||
/// <param name="layer">The layer</param>
|
||||
/// <param name="faceSlice">The face if this is cubemap texture </param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns>KtxErrorCode for the result the function</returns>
|
||||
public static unsafe KtxErrorCode GetImageData(
|
||||
this KtxTextureHandle textureHandle,
|
||||
uint level,
|
||||
uint layer,
|
||||
uint faceSlice,
|
||||
out Span<byte> data
|
||||
)
|
||||
{
|
||||
var result = textureHandle.GetImageOffset(level, layer, faceSlice, out var imageOffset);
|
||||
if (result != KtxErrorCode.Success)
|
||||
{
|
||||
data = new Span<byte>();
|
||||
return result;
|
||||
}
|
||||
|
||||
data = new Span<byte>(textureHandle.GetData() + imageOffset, (int)textureHandle.GetImageSize(level));
|
||||
return KtxErrorCode.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a span of the image data for a given level, layer and face for a v2 texture.
|
||||
/// </summary>
|
||||
/// <param name="textureHandle">Handle to version 2 KTX texture</param>
|
||||
/// <param name="level">The mip level to retrieve data for</param>
|
||||
/// <param name="layer">The layer</param>
|
||||
/// <param name="faceSlice">The face if this is cubemap texture </param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns>KtxErrorCode for the result the function</returns>
|
||||
public static KtxErrorCode GetImageData(
|
||||
this KtxTexture2Handle textureHandle,
|
||||
uint level,
|
||||
uint layer,
|
||||
uint faceSlice,
|
||||
out Span<byte> data
|
||||
)
|
||||
{
|
||||
return ((KtxTextureHandle)textureHandle).GetImageData(level, layer, faceSlice, out data);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Returns a span of the image data for a given level, layer and face for a v1 texture.
|
||||
/// </summary>
|
||||
/// <param name="textureHandle">Handle to version 1 KTX texture</param>
|
||||
/// <param name="level">The mip level to retrieve data for</param>
|
||||
/// <param name="layer">The layer</param>
|
||||
/// <param name="faceSlice">The face if this is cubemap texture </param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns>KtxErrorCode for the result the function</returns>
|
||||
public static KtxErrorCode GetImageData(
|
||||
this KtxTexture1Handle textureHandle,
|
||||
uint level,
|
||||
uint layer,
|
||||
uint faceSlice,
|
||||
out Span<byte> data
|
||||
)
|
||||
{
|
||||
return ((KtxTextureHandle)textureHandle).GetImageData(level, layer, faceSlice, out data);
|
||||
}
|
||||
|
||||
public static unsafe int3 GetBlockExtent(this KtxTextureHandle textureHandle)
|
||||
{
|
||||
return new(
|
||||
(int)textureHandle.Protected->FormatSize.BlockWidth,
|
||||
(int)textureHandle.Protected->FormatSize.BlockHeight,
|
||||
(int)textureHandle.Protected->FormatSize.BlockDepth
|
||||
);
|
||||
}
|
||||
|
||||
#region Texture Version Conversion
|
||||
|
||||
/// <summary>
|
||||
/// Converts a generic KtxTextureHandle to a KtxTexture2Handle if the underlying texture is a version 2
|
||||
/// texture.
|
||||
/// </summary>
|
||||
/// <param name="handle">Handle to the version agnostic KTX texture</param>
|
||||
/// <returns>The KtxTexture2Handle of the texture</returns>
|
||||
/// <exception cref="InvalidCastException">Thrown if the texture is not a version 2 texture</exception>
|
||||
public static KtxTexture2Handle AsKtxTexture2Handle(this KtxTextureHandle handle)
|
||||
{
|
||||
if (handle.ClassId != ClassId.Texture2C)
|
||||
throw new InvalidCastException($"Texture is not a v2 texture");
|
||||
|
||||
return new KtxTexture2Handle(handle.Impl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a generic KtxTextureHandle to a KtxTexture1Handle if the underlying texture is a version 1
|
||||
/// texture.
|
||||
/// </summary>
|
||||
/// <param name="handle">Handle to the version agnostic KTX texture</param>
|
||||
/// <returns>The KtxTexture1Handle of the texture</returns>
|
||||
/// <exception cref="InvalidCastException">Thrown if the texture is not a version 1 texture</exception>
|
||||
public static KtxTexture1Handle AsKtxTexture1Handle(this KtxTextureHandle handle)
|
||||
{
|
||||
if (handle.ClassId != ClassId.Texture1C)
|
||||
throw new InvalidCastException($"Texture is not a v1 texture");
|
||||
|
||||
return new KtxTexture1Handle(handle.Impl);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Loading…
Reference in a new issue