/// <summary> /// Load the texture. This may be stored in a special format; otherwise it is DDS. /// </summary> /// <returns></returns> protected override Asset Load() { if (Archive.Platform == DSPlatform.PS3) { using (var stream = Open()) { Texture2D texture = new Texture2D(); Format format = Ps3Format; Vector2i dimensions = Ps3Dimensions; byte[] data = new byte[format.AlignedByteSize(dimensions)]; for (int level = 0; ; level++) { TextureLevel2D textureLevel = texture.Surface.Levels[level]; int size = format.AlignedByteSize(dimensions); stream.Read(data, 0, size); textureLevel.DataCompressed(format, dimensions, data); if (dimensions == Vector2i.One) { break; } dimensions.X = Math.Max(1, (dimensions.X + 1) / 2); dimensions.Y = Math.Max(1, (dimensions.Y + 1) / 2); } return(new TextureAsset(Manager, Name, texture)); } } else { return(base.Load()); } }
/// <summary>Load the DDS file.</summary> /// <param name="loader"></param> /// <returns></returns> public override Asset Load(AssetLoader loader) { using (BinaryReader reader = loader.Reader) { reader.RequireMagic(Magic); int headerSize = reader.ReadInt32(); if (headerSize != HeaderSize) { throw new InvalidDataException(); } Flags flags = (Flags)reader.ReadInt32(); if ((flags & RequiredFlags) != RequiredFlags) { throw new InvalidDataException(); } int height = reader.ReadInt32(); int width = reader.ReadInt32(); int pitchOrLinearSize = reader.ReadInt32(); int depth = reader.ReadInt32(); int mipMapCount = reader.ReadInt32(); reader.BaseStream.Seek(4 * 11, SeekOrigin.Current); // Reserved var pixelFormat = new PixelFormat(reader); Caps caps = (Caps)reader.ReadInt32(); // Surface complexity if ((caps & Caps.Texture) == 0) { throw new InvalidDataException(); } Caps2 caps2 = (Caps2)reader.ReadInt32(); // Surface type int caps3 = reader.ReadInt32(); int caps4 = reader.ReadInt32(); int reserved2 = reader.ReadInt32(); Format format = null; if (pixelFormat.HasFourCC) { if (pixelFormat.FourCC == "DX10") { throw new NotSupportedException(); } format = FormatFourCCs[pixelFormat.FourCC]; } else if (pixelFormat.HasRGB) { int b0 = 255, b1 = 255 << 8, b2 = 255 << 16, b3 = 255 << 24; if (pixelFormat.HasAlphaPixels && pixelFormat.RGBBitCount == 32) { if (pixelFormat.MatchMasks(b2, b1, b0, b3)) { format = Glare.Graphics.Formats.Vector4nbBGRA; } else if (pixelFormat.MatchMasks(b0, b1, b2, b3)) { format = Glare.Graphics.Formats.Vector4nb; } } } if (format == null) { throw new NotSupportedException(); } if (caps2 != Caps2.None) { throw new NotSupportedException("Cube maps or volume textures not supported."); } int linearSize; if ((flags & Flags.Pitch) != 0) { linearSize = pitchOrLinearSize * height; } else if ((flags & Flags.LinearSize) != 0) { linearSize = pitchOrLinearSize; } else { linearSize = format.AlignedByteSize(new Vector2i(width, height)); } byte[] data = new byte[linearSize]; if ((flags & Flags.MipMapCount) == 0) { mipMapCount = 1; } Texture2D texture = new Texture2D(); //format, new Vector2i(width, height)); for (int level = 0; level < mipMapCount; level++) { if (reader.Read(data, 0, linearSize) != linearSize) { throw new InvalidDataException(); } texture.Surface.Levels[level].DataCompressed(format, new Vector2i(width, height), data); width = Math.Max(1, (width + 1) / 2); height = Math.Max(1, (height + 1) / 2); linearSize = format.AlignedByteSize(width, height); } texture.Name = loader.Name; return(new TextureAsset(loader, texture)); } }