namespace MrGameEng.Graphics;
///
/// Builds the 64-bit sort key the batcher orders sprites by:
/// layer (8 bits) → depth (32 bits) → texture (24 bits).
/// Texture bits only group equal textures for batching; collisions are harmless.
///
public static class SpriteSortKey
{
/// Composes a sort key from layer, depth and texture grouping key.
public static ulong Make(byte layer, float depth, int textureKey) =>
((ulong)layer << 56)
| ((ulong)DepthToSortableBits(depth) << 24)
| ((uint)textureKey & 0xFF_FFFF);
///
/// Maps a float to bits whose unsigned order matches the float order
/// (negative depths sort before positive ones).
///
public static uint DepthToSortableBits(float depth)
{
var bits = BitConverter.SingleToUInt32Bits(depth);
return (bits & 0x8000_0000) != 0 ? ~bits : bits | 0x8000_0000;
}
}