using System.Runtime.InteropServices; using EvilEngine.Formats.Textures; using Godot; using Godot.Collections; using Texture = EvilEngine.Formats.Textures.Texture; namespace EvilEngine.Shared.World; public sealed record DecodedTexture(IReadOnlyList Images, bool Layered, bool Cube); public static class WorldTextures { private const int RgbeBlock = 1 << 16; private static readonly Lazy RgbeHalves = new(() => { var table = new ushort[256 * 256]; for (var exponent = 0; exponent < 256; exponent++) { var scale = Math.ScaleB(1.0, (sbyte)exponent) / 255; for (var value = 0; value < 256; value++) table[(exponent << 8) | value] = BitConverter.HalfToUInt16Bits((Half)(value * scale)); } return table; }); public static Image Checker() { var image = Image.CreateEmpty(16, 16, false, Image.Format.Rgba8); for (var y = 0; y < 16; y++) for (var x = 0; x < 16; x++) image.SetPixel(x, y, (x / 8 + y / 8) % 2 == 0 ? new Color(1, 0, 1) : Colors.Black); return image; } public static DecodedTexture? Decode(Texture texture, out string? problem) { problem = null; if (texture.Depth > 1 || (texture.Layers > 1 && texture.Cube)) { problem = $"{texture.Layers} layers of depth {texture.Depth} aren't drawn yet"; return null; } var images = new List(); for (var layer = 0; layer < texture.Layers; layer++) for (var face = 0; face < texture.Faces; face++) if (Face(texture, layer, face, out problem) is { } image) images.Add(image); else return null; return new DecodedTexture(images, texture.Layers > 1, texture.Cube); } public static Godot.Texture Upload(DecodedTexture decoded) { if (decoded.Layered) { var array = new Texture2DArray(); array.CreateFromImages(new Array(decoded.Images)); return array; } if (!decoded.Cube) return ImageTexture.CreateFromImage(decoded.Images[0]); var cube = new Cubemap(); cube.CreateFromImages(new Array(decoded.Images)); return cube; } private static Image? Face(Texture texture, int layer, int face, out string? problem) { problem = null; var (format, convert) = Format(texture); if (format is null) { problem = $"{texture.Format} with swizzle {texture.Swizzle} isn't drawn yet"; return null; } var full = (int)Math.Floor(Math.Log2(Math.Max(texture.Width, texture.Height))) + 1; var levels = texture.Levels.Count == full ? full : 1; var parts = new ReadOnlyMemory[levels]; for (var level = 0; level < levels; level++) { var image = texture.Image(level, layer, face); parts[level] = convert is null ? image : convert(image.ToArray()); } var data = new byte[parts.Sum(part => part.Length)]; var at = 0; foreach (var part in parts) { part.Span.CopyTo(data.AsSpan(at)); at += part.Length; } return Image.CreateFromData(texture.Width, texture.Height, levels > 1, format.Value, data); } private static (Image.Format? Format, Func? Convert) Format(Texture texture) { if (texture.Hdr == TextureHdr.SourceLightmap && texture.Format == TextureFormat.R8G8B8A8Unorm) return (Image.Format.Rgbah, Rgbe); var swizzle = texture.Swizzle; if (swizzle == "rrr1" && texture.Format is TextureFormat.R8Unorm or TextureFormat.R8Srgb) return (Image.Format.L8, null); if (swizzle != "rgba") return (null, null); return texture.Format switch { TextureFormat.R8Unorm or TextureFormat.R8Srgb => (Image.Format.R8, null), TextureFormat.R8G8Unorm or TextureFormat.R8G8Srgb => (Image.Format.Rg8, null), TextureFormat.R8G8B8Unorm or TextureFormat.R8G8B8Srgb => (Image.Format.Rgb8, null), TextureFormat.R8G8B8A8Unorm or TextureFormat.R8G8B8A8Srgb => (Image.Format.Rgba8, null), TextureFormat.R16Sfloat => (Image.Format.Rh, null), TextureFormat.R16G16Sfloat => (Image.Format.Rgh, null), TextureFormat.R16G16B16A16Sfloat => (Image.Format.Rgbah, null), TextureFormat.R32Sfloat => (Image.Format.Rf, null), TextureFormat.R32G32Sfloat => (Image.Format.Rgf, null), TextureFormat.R32G32B32Sfloat => (Image.Format.Rgbf, null), TextureFormat.R32G32B32A32Sfloat => (Image.Format.Rgbaf, null), TextureFormat.Bc1RgbUnorm or TextureFormat.Bc1RgbSrgb or TextureFormat.Bc1RgbaUnorm or TextureFormat.Bc1RgbaSrgb => (Image.Format.Dxt1, null), TextureFormat.Bc2Unorm or TextureFormat.Bc2Srgb => (Image.Format.Dxt3, null), TextureFormat.Bc3Unorm or TextureFormat.Bc3Srgb => (Image.Format.Dxt5, null), TextureFormat.Bc4Unorm => (Image.Format.RgtcR, null), TextureFormat.Bc5Unorm => (Image.Format.RgtcRg, null), TextureFormat.Bc6hUfloat => (Image.Format.BptcRgbfu, null), TextureFormat.Bc6hSfloat => (Image.Format.BptcRgbf, null), TextureFormat.Bc7Unorm or TextureFormat.Bc7Srgb => (Image.Format.BptcRgba, null), _ => (null, null) }; } private static byte[] Rgbe(byte[] rgbe) { var halves = new byte[rgbe.Length * 2]; var table = RgbeHalves.Value; var one = BitConverter.HalfToUInt16Bits((Half)1); var texels = rgbe.Length / 4; Parallel.For(0, (texels + RgbeBlock - 1) / RgbeBlock, block => { var output = MemoryMarshal.Cast(halves.AsSpan()); var end = Math.Min(texels, (block + 1) * RgbeBlock); for (var i = block * RgbeBlock * 4; i < end * 4; i += 4) { var exponent = rgbe[i + 3] << 8; output[i] = table[exponent | rgbe[i]]; output[i + 1] = table[exponent | rgbe[i + 1]]; output[i + 2] = table[exponent | rgbe[i + 2]]; output[i + 3] = one; } }); return halves; } }