using System; using System.IO; using System.Collections; using GlitchyEngine.Math; using GlitchyEngine.Renderer; namespace GlitchyEngine.Content; class TextureLoader : IProcessedAssetLoader { public Result Load(Stream dataStream) { TextureDimension dimension = Try!(dataStream.Read()); Format pixelFormat = Try!(dataStream.Read()); uint32 width = Try!(dataStream.Read()); uint32 height = Try!(dataStream.Read()); uint32 depth = Try!(dataStream.Read()); uint32 arraySize = Try!(dataStream.Read()); uint32 mipMapCount = Try!(dataStream.Read()); bool isCubemap = Try!(dataStream.Read()); SamplerStateDescription sampler = Try!(ReadSampler(dataStream)); List surfaceDatas = scope .(mipMapCount * arraySize); defer { ClearAndDeleteItems!(surfaceDatas); } TextureSliceData[] slices = scope TextureSliceData[mipMapCount * arraySize]; int index = 0; for (int arraySlice < arraySize) { for (int mipSlice < mipMapCount) { uint32 linePitch = Try!(dataStream.Read()); uint32 slicePitch = Try!(dataStream.Read()); uint64 byteCount = Try!(dataStream.Read()); uint8[] surfaceData = new uint8[byteCount]; Try!(dataStream.TryRead(surfaceData)); slices[index] = .(surfaceData.Ptr, linePitch, slicePitch); index++; surfaceDatas.Add(surfaceData); } } Texture result = null; switch (dimension) { case .Texture2D: if (isCubemap) { Runtime.NotImplemented(); } else { Texture2DDesc desc = .(width, height, pixelFormat, arraySize, mipMapCount, .Immutable, .None); Texture2D texture = new Texture2D(desc); texture.SetData(slices); result = texture; } default: Runtime.NotImplemented(); } using (SamplerState samplerState = SamplerStateManager.GetSampler(sampler)) { result.SamplerState = samplerState; } return result; } private Result ReadSampler(Stream dataStream) { SamplerStateDescription sampler; sampler.MinFilter = Try!(dataStream.Read()); sampler.MagFilter = Try!(dataStream.Read()); sampler.MipFilter = Try!(dataStream.Read()); sampler.FilterMode = Try!(dataStream.Read()); sampler.ComparisonFunction = Try!(dataStream.Read()); sampler.AddressModeU = Try!(dataStream.Read()); sampler.AddressModeV = Try!(dataStream.Read()); sampler.AddressModeW = Try!(dataStream.Read()); sampler.MipLODBias = Try!(dataStream.Read()); sampler.MipMinLOD = Try!(dataStream.Read()); sampler.MipMaxLOD = Try!(dataStream.Read()); sampler.MaxAnisotropy = Try!(dataStream.Read()); sampler.BorderColor = Try!(dataStream.Read()); return sampler; } }