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 void OnWritingCompleted(ArchiveEntry entry, MemoryStream ms, bool?compression) { ms.Position = 0; int compressedSize = 0; int uncompressedSize = (int)ms.Length; byte[] copyBuff = new byte[Math.Min(uncompressedSize, 32 * 1024)]; try { bool compress = uncompressedSize > 256 && (compression ?? entry.IsCompressed); if (compress) { using (SafeUnmanagedArray buff = new SafeUnmanagedArray(uncompressedSize + 256)) using (UnmanagedMemoryStream buffStream = buff.OpenStream(FileAccess.ReadWrite)) { compressedSize = ZLibHelper.Compress(ms, buffStream, uncompressedSize); if (uncompressedSize - compressedSize > 256) { using (Stream output = OpenOrAppendBinary(entry, compressedSize)) { buffStream.Position = 0; buffStream.CopyToStream(output, compressedSize, copyBuff); } return; } } } ms.Position = 0; compressedSize = uncompressedSize; using (Stream output = OpenOrAppendBinary(entry, uncompressedSize)) ms.CopyToStream(output, uncompressedSize, copyBuff); } finally { entry.Size = compressedSize; entry.UncompressedSize = uncompressedSize; } }
private static DataBox CreateDataBox(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 rowPitch, Int32 depthPitch) { return(new DataBox(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), rowPitch, depthPitch)); }
private static DataRectangle CreateDataRectangle(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 pitch) { return(new DataRectangle(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), pitch)); }