private static DxTexture ReadTextureCubeFromStream(GtexData gtex, Stream input) { Texture2DDescription descriptor = GetTextureCubeDescription(gtex); using (SafeUnmanagedArray array = new SafeUnmanagedArray(gtex.MipMapData.Sum(d => d.Length))) { DataRectangle[] rects = new DataRectangle[gtex.MipMapData.Length]; using (UnmanagedMemoryStream io = array.OpenStream(FileAccess.Write)) { byte[] buff = new byte[32 * 1024]; for (int index = 0; index < gtex.MipMapData.Length; index++) { GtexMipMapLocation mimMap = gtex.MipMapData[index]; Int32 pitch = GetPitch(descriptor, index); rects[index] = CreateDataRectangle(array, io, pitch); input.SetPosition(mimMap.Offset); input.CopyToStream(io, mimMap.Length, buff); } } Texture2D texture = new Texture2D(_device.Device, descriptor, rects); // Workaround _textureCreatingWorkaround(_device.Device.ImmediateContext, texture, ImageFileFormat.Dds); return(new DxTexture(texture, descriptor)); } }
public bool TryCreateNextStream(string key, out Stream result, out Exception ex) { GtexMipMapLocation data = _data.MipMapData[_index++]; result = new StreamSegment(_resourceStream, data.Offset, data.Length, FileAccess.Read); ex = null; return(true); }
public void Inject(WpdEntry entry, Stream input, Lazy <Stream> headers, Lazy <Stream> content, Byte[] buff) { int sourceSize = (int)input.Length; headers.Value.Position = entry.Offset; SectionHeader sectionHeader = headers.Value.ReadContent <SectionHeader>(); VtexHeader textureHeader = headers.Value.ReadContent <VtexHeader>(); byte[] unknownData = new byte[textureHeader.GtexOffset - VtexHeader.Size]; headers.Value.Read(unknownData, 0, unknownData.Length); GtexData data = headers.Value.ReadContent <GtexData>(); if (data.MipMapData.Length != 1) { throw new NotImplementedException(); } DdsHeader ddsHeader = DdsHeaderDecoder.FromFileStream(input); DdsHeaderEncoder.ToGtexHeader(ddsHeader, data.Header); GtexMipMapLocation mipMapLocation = data.MipMapData[0]; int dataSize = sourceSize - 128; if (dataSize <= mipMapLocation.Length) { content.Value.Seek(mipMapLocation.Offset, SeekOrigin.Begin); } else { content.Value.Seek(0, SeekOrigin.End); mipMapLocation.Offset = (int)content.Value.Position; } input.CopyToStream(content.Value, dataSize, buff); mipMapLocation.Length = dataSize; using (MemoryStream ms = new MemoryStream(180)) { sectionHeader.WriteToStream(ms); textureHeader.WriteToStream(ms); ms.Write(unknownData, 0, unknownData.Length); data.WriteToStream(ms); ms.SetPosition(0); DefaultWpdEntryInjector defaultInjector = new DefaultWpdEntryInjector(); defaultInjector.Inject(entry, ms, headers, content, buff); } }