//loads the data from a stream in to a texture object. private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture) { stream.Position = 0; if (offsetMipMaps == 0) { texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width); int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height); int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height)); int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps); Format format = texture.GetLevelDescription(0).Format; Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default); for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++) { int levelWidth = MipMapSize(j, width); int levelHeight = MipMapSize(j, height); SharpDX.DataStream ds; texture.LockRectangle(i, LockFlags.ReadOnly, out ds); texture.UnlockRectangle(i); SharpDX.DataStream ds2; offsetedTexture.LockRectangle(j, LockFlags.None, out ds2); ds2.Position = 0; ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format)); offsetedTexture.UnlockRectangle(j); } texture.Dispose(); texture = offsetedTexture; } }
private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture) { var fileStream = stream as FileStream; byte[] data = fileStream != null ? null : SharpDX.Utilities.ReadStream(stream); int maxTextureDimension = MyRender.GraphicsDevice.Capabilities.MaxTextureWidth; if (IntPtr.Size == 4 || MyRender.GraphicsDevice.AvailableTextureMemory < 1024 * 1024 * 600) { maxTextureDimension = 2048; } ImageInformation imageInfo; if (fileStream != null) { //imageInfo = GetImageInfoFromFileW(fileStream.Name); imageInfo = (ImageInformation)GetImageInfoFromFileWMethodInfo.Invoke(null, new object[] { fileStream.Name }); } else { imageInfo = ImageInformation.FromMemory(data); } var textureWidth = imageInfo.Width; var textureHeight = imageInfo.Height; textureWidth = Math.Max(1, textureWidth >> offsetMipMaps); textureHeight = Math.Max(1, textureHeight >> offsetMipMaps); int loadMipmapOffset = offsetMipMaps; while (textureWidth > maxTextureDimension || textureHeight > maxTextureDimension) { loadMipmapOffset++; textureWidth = Math.Max(1, textureWidth >> 1); textureHeight = Math.Max(1, textureHeight >> 1); } if (offsetMipMaps == 0 && loadMipmapOffset == 0) { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.Linear, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.Linear, 0); } } else if (loadMipmapOffset > 0) { var skipFilter = (Filter)D3DXSkipDDSMipLevels((uint)loadMipmapOffset); if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } } else { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width); int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height); int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height)); int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps); Format format = texture.GetLevelDescription(0).Format; Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default); for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++) { int levelWidth = MipMapSize(j, width); int levelHeight = MipMapSize(j, height); SharpDX.DataStream ds; texture.LockRectangle(i, LockFlags.ReadOnly, out ds); texture.UnlockRectangle(i); SharpDX.DataStream ds2; offsetedTexture.LockRectangle(j, LockFlags.None, out ds2); ds2.Position = 0; ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format)); offsetedTexture.UnlockRectangle(j); } texture.Dispose(); texture = offsetedTexture; } }