public static void AddFreeShadowTexture(SlimDX.Direct3D9.Texture texture) { lock (mLockObj) { mFreeShadowTextures.Add(texture); } }
public static void GenerateMipMapA32B32G32R32F(SlimDX.Direct3D9.Texture texture) { var prevLevel = ReadTexture <Software.Texel.R32G32B32A32F>(texture, 0); for (int i = 1; i < texture.LevelCount; i++) { var sd = texture.GetLevelDescription(i); var rect = texture.LockRectangle(i, SlimDX.Direct3D9.LockFlags.None); int width = sd.Width; // rect.Pitch / Tsize; int height = sd.Height; // (int)(rect.Data.Length / rect.Pitch); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { rect.Data.Write(prevLevel[y, x] = (prevLevel[y * 2, x * 2] + prevLevel[y * 2 + 1, x * 2] + prevLevel[y * 2, x * 2 + 1] + prevLevel[y * 2 + 1, x * 2 + 1]) / 4); } if (rect.Pitch != width * 4 * 4) { rect.Data.Seek(rect.Pitch - width * 4 * 4, System.IO.SeekOrigin.Current); } } texture.UnlockRectangle(i); } }
/// <summary> /// A8B8G8R8: Uses the R + G + B values of a texture to form a heightmap /// R32F: Reads the value straight off /// </summary> public static R32F[,] ToHeightmap(SlimDX.Direct3D9.Texture texture, float heightMultiplier) { SlimDX.Direct3D9.SurfaceDescription d = texture.GetLevelDescription(0); R32F[,] heightmap = new R32F[d.Height, d.Width]; DataRectangle r = texture.LockRectangle(0, SlimDX.Direct3D9.LockFlags.ReadOnly); for (int y = 0; y < d.Height; y++) { for (int x = 0; x < d.Width; x++) { if (d.Format == SlimDX.Direct3D9.Format.A8R8G8B8) { var col = r.Data.Read <Software.Texel.A8R8G8B8>(); heightmap[y, x] = new R32F((col.R + col.G + col.B + col.A) * heightMultiplier / 256f); } else if (d.Format == SlimDX.Direct3D9.Format.X8R8G8B8) { var col = r.Data.Read <Software.Texel.A8R8G8B8>(); heightmap[y, x] = new R32F((col.R + col.G + col.B + col.A) * heightMultiplier / 256f); } else if (d.Format == SlimDX.Direct3D9.Format.R32F) { heightmap[y, x] = new R32F(r.Data.Read <float>()); } else { throw new ArgumentException("Unsupported texture format: " + d.Format); } } } texture.UnlockRectangle(0); return(heightmap); }
public override SlimDX.Direct3D9.Texture ToTexture9(SlimDX.Direct3D9.Device device) { SlimDX.Direct3D9.Texture t = new SlimDX.Direct3D9.Texture(device, Size.Width, Size.Height, 1, SlimDX.Direct3D9.Usage.Dynamic, Data[0, 0].Format9, SlimDX.Direct3D9.Pool.Default); SlimDX.DataRectangle r = t.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); TextureUtil.WriteTexture(r, Data, Data[0, 0].Size); t.UnlockRectangle(0); return(t); }
public static T[,] ReadTexture <T>(SlimDX.Direct3D9.Texture texture, int level) where T : struct { var d = texture.LockRectangle(level, SlimDX.Direct3D9.LockFlags.ReadOnly); var sd = texture.GetLevelDescription(level); var data = ReadTexture <T>(d, sd.Width, sd.Height); texture.UnlockRectangle(level); return(data); }
public static void WriteTexture <T>(SlimDX.Direct3D9.Texture texture, T[,] data, int TSize) where T : struct { var dr = texture.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); WriteTexture <T>(dr, data, TSize); texture.UnlockRectangle(0); }
public static SlimDX.Direct3D9.Texture SingleColor <T>(SlimDX.Direct3D9.Device device, T color) where T : struct { SlimDX.Direct3D9.Texture t = new SlimDX.Direct3D9.Texture(device, 1, 1, 1, SlimDX.Direct3D9.Usage.Dynamic, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Default); DataRectangle r = t.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); r.Data.Write(color); t.UnlockRectangle(0); return(t); }
private TextureHandle fromMemoryGDI(System.Drawing.Bitmap bmp) { var locked = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var texRet = new SlimDX.Direct3D9.Texture(mDevice, bmp.Width, bmp.Height, 1, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Managed); var surf = texRet.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); surf.Data.WriteRange(locked.Scan0, locked.Stride * locked.Height); texRet.UnlockRectangle(0); bmp.UnlockBits(locked); return new TextureHandle(texRet); }
private TextureHandle fromMemoryGDI(System.Drawing.Bitmap bmp) { var locked = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var texRet = new SlimDX.Direct3D9.Texture(mDevice, bmp.Width, bmp.Height, 1, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Managed); var surf = texRet.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); surf.Data.WriteRange(locked.Scan0, locked.Stride * locked.Height); texRet.UnlockRectangle(0); bmp.UnlockBits(locked); return(new TextureHandle(texRet)); }
public SkyManager() { try { foreach (var map in DBC.DBCStores.Map.Records) { mSkyMapper.Add(map.ID, new MapSky(map.ID)); } } catch (Exception) { } mSkyTexture = new SlimDX.Direct3D9.Texture(Game.GameManager.GraphicsThread.GraphicsManager.Device, 180, 1, 1, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Managed); Game.GameManager.GraphicsThread.OnFrame += new Game.VideoThread.FrameRenderDelegate(_RenderSky); }
public static SlimDX.Direct3D9.Texture FromHeightmap(SlimDX.Direct3D9.Device device, float[,] heightmap) { SlimDX.Direct3D9.Texture t = new SlimDX.Direct3D9.Texture(device, heightmap.GetLength(1), heightmap.GetLength(0), 1, SlimDX.Direct3D9.Usage.Dynamic, SlimDX.Direct3D9.Format.R32F, SlimDX.Direct3D9.Pool.Default); DataRectangle r = t.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); for (int y = 0; y < heightmap.GetLength(0); y++) { r.Data.Seek(y * r.Pitch, System.IO.SeekOrigin.Begin); for (int x = 0; x < heightmap.GetLength(1); x++) { r.Data.Write(heightmap[y, x]); } } t.UnlockRectangle(0); return(t); }
public static void Copy(SlimDX.Direct3D9.Surface source, SlimDX.Direct3D9.Texture destination) { DataRectangle read = source.LockRectangle(SlimDX.Direct3D9.LockFlags.ReadOnly); DataRectangle write = destination.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); byte[] buffer = new byte[read.Pitch]; while (read.Data.Position < read.Data.Length) { read.Data.Read(buffer, 0, buffer.Length); write.Data.Write(buffer, 0, buffer.Length); if (write.Pitch != read.Pitch) { write.Data.Seek(Math.Max(0, write.Pitch - read.Pitch), System.IO.SeekOrigin.Current); } } destination.UnlockRectangle(0); source.UnlockRectangle(); }
public Texture(SlimDX.Direct3D9.Texture texture, int level) { Data = TextureUtil.ReadTexture <T>(texture, level); }
private void btnUnfurl_Click(object sender, EventArgs e) { try { if (tvMaps.SelectedNode == null) { return; } var mapData = tvMaps.SelectedNode.Tag as MapData; if ((mapData.Width == 0) || (mapData.Height == 0)) { return; } var tileId = 0; var folderId = mapData.Folder << 16; Bitmap bmp; Bitmap dstBmp = new Bitmap(mapData.Width, mapData.Height); System.Drawing.Imaging.BitmapData srcData; System.Drawing.Imaging.BitmapData dstData; Rectangle dstRect = new Rectangle(0, 0, 256, 256); Rectangle srcRect = new Rectangle(0, 0, 256, 256); byte[] srcBytes = new byte[256 * 256 * 4]; for (var x = 0; x < mapData.Rows; x++) { dstRect.X = x * 256; for (var y = 0; y < mapData.Columns; y++) { dstRect.Y = y * 256; var thisFileId = folderId + (y * mapData.Rows + x); var filePath = DatDigger.Utilities.DataFileIdToRelativePath(thisFileId); filePath = Path.Combine(Properties.Settings.Default.GameDirectory, filePath); var file = DatDigger.Sections.SectionLoader.OpenFile(filePath); if (file == null) { dstBmp.Dispose(); return; } var gtex = file.FindChild<DatDigger.Sections.Texture.GtexData>(); using (var tex = new SlimDX.Direct3D9.Texture(context.Device, gtex.Header.Width, gtex.Header.Height, 1, SlimDX.Direct3D9.Usage.None, gtex.Format, SlimDX.Direct3D9.Pool.Scratch)) { var texData = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None); texData.Data.Write(gtex.TextureData[0], 0, gtex.TextureData[0].Length); tex.UnlockRectangle(0); using (var bmpStream = SlimDX.Direct3D9.Texture.ToStream(tex, SlimDX.Direct3D9.ImageFileFormat.Bmp)) { bmp = new Bitmap(bmpStream); } } srcData = bmp.LockBits(srcRect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); dstData = dstBmp.LockBits(dstRect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(srcData.Scan0, srcBytes, 0, srcBytes.Length); for (var i = 0; i < 256; i++) { int dstOffset = dstData.Stride * i; System.Runtime.InteropServices.Marshal.Copy(srcBytes, 256 * 4 * i, dstData.Scan0.Increment(dstOffset), 256 * 4); } bmp.UnlockBits(srcData); dstBmp.UnlockBits(dstData); bmp.Dispose(); tileId++; } } if (currentImage != null) { currentImage.Dispose(); } currentImage = dstBmp; pictureMap.Image = dstBmp; pictureMap.Width = mapData.Width; pictureMap.Height = mapData.Height; } catch (Exception ex) { MessageBox.Show("Failed to Unfurl map: " + ex.Message); } }
private TextureHandle LoadBlpTexture(SlimDX.Direct3D9.Device Render, System.IO.BinaryReader reader) { reader.BaseStream.Position += 4; byte compression = reader.ReadByte(); byte alphaDepth = reader.ReadByte(); byte alphaEncoding = reader.ReadByte(); byte hasMipMap = reader.ReadByte(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); int[] Offsets = new int[16]; int[] Sizes = new int[16]; byte[] ofsTmp = reader.ReadBytes(16 * 4); byte[] sizTmp = reader.ReadBytes(16 * 4); int levelCount = 0; int blockSize = 0; for (int i = 0; i < 16; ++i) { Offsets[i] = BitConverter.ToInt32(ofsTmp, 4 * i); Sizes[i] = BitConverter.ToInt32(sizTmp, 4 * i); if (Offsets[i] != 0 && Sizes[i] != 0) { ++levelCount; } } SlimDX.Direct3D9.Format texFmt = SlimDX.Direct3D9.Format.Unknown; if (compression == 2) { switch (alphaEncoding) { case 0: texFmt = SlimDX.Direct3D9.Format.Dxt1; blockSize = 2; break; case 1: texFmt = SlimDX.Direct3D9.Format.Dxt3; blockSize = 4; break; case 7: texFmt = SlimDX.Direct3D9.Format.Dxt5; blockSize = 4; break; } } if (compression == 3) { texFmt = SlimDX.Direct3D9.Format.A8R8G8B8; blockSize = 4; } if (texFmt == SlimDX.Direct3D9.Format.Unknown) { throw new FormatException("This format is not yet supported, sorry!"); } var texture = new SlimDX.Direct3D9.Texture(Render, width, height, levelCount, SlimDX.Direct3D9.Usage.None, texFmt, SlimDX.Direct3D9.Pool.Managed); int curLevel = 0; for (int i = 0; i < 16; ++i) { if (Sizes[i] != 0 && Offsets[i] != 0) { reader.BaseStream.Position = Offsets[i]; byte[] layerData = reader.ReadBytes(Sizes[i]); SlimDX.Direct3D9.Surface surf = texture.GetSurfaceLevel(curLevel); SlimDX.Direct3D9.SurfaceDescription desc = texture.GetLevelDescription(curLevel); System.Drawing.Rectangle rec = System.Drawing.Rectangle.FromLTRB(0, 0, desc.Width, desc.Height); SlimDX.Direct3D9.Surface.FromMemory(surf, layerData, SlimDX.Direct3D9.Filter.Triangle, 0, texFmt, blockSize * rec.Width, rec); ++curLevel; } } return(new TextureHandle(texture)); }
internal void Reload(Sprite Sprite, ResourceLoadType LoadType) { if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = true; } if (LoadType == ResourceLoadType.Delayed) { delayLoader.Enqueue(Sprite); return; } for (int i = 0; i < Sprite.internalFrames.Length; i++) { if (Sprite.internalFrames[i].HasSystemCopy) { SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(Sprite.internalFrames[i].size.x), MakePowerOfTwo(Sprite.internalFrames[i].size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; Sprite.internalFrames[i].texture = tex; Sprite.internalFrames[i].RestoreFromSystemCopy(); Sprite.internalFrames[i].loading = false; Sprite.internalFrames[i].loaded = true; } } ResourceID id = Sprite.ID; if (id.File == "" || !spriteProcessors.ContainsKey(id.Format)) { return; } ResourceID realid = (string)id; // check replacements if (replacement != null) { ResourceID newid = GetReplacementID(id); if (newid != null) { if (CheckReplacementID(newid)) { id = newid; } } } engine.IncreaseLoadingCount(); ISpriteProcessor loader = GetSpriteProcessor(id, true); if (loader is ISpriteAnimationProcessor) { ISpriteAnimationProcessor loaderAni = (ISpriteAnimationProcessor)loader; loader.Process(id); Log.Debug("reload \"" + id + "\""); for (int i = 0; i < loaderAni.FrameCount; i++) { if (!loaderAni.SetFrame(i)) { SpriteFrame[] frames = new SpriteFrame[i]; for (int j = 0; j < i; j++) { frames[j] = Sprite.Frames[j]; } Sprite.internalFrames = frames; Sprite.ani.frameCount = i; break; } SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loaderAni.FrameSize.x), MakePowerOfTwo(loaderAni.FrameSize.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frames[i].Texture = tex; Sprite.Frames[i].Width = loaderAni.FrameSize.x; Sprite.Frames[i].Height = loaderAni.FrameSize.y; Sprite.Frames[i].TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } } else { loader.Process(id); Log.Debug("reload \"" + id + "\""); SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(loader.Size.x), MakePowerOfTwo(loader.Size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); loader.Render(data.Data, data.Pitch); tex.UnlockRectangle(0); Sprite.Frame.Texture = tex; Sprite.Frame.Width = loader.Size.x; Sprite.Frame.Height = loader.Size.y; Sprite.Frame.TimeStamp = System.Diagnostics.Stopwatch.GetTimestamp(); } if (Sprite.internalFrames != null) { Sprite.internalFrames[0].loading = false; Sprite.internalFrames[0].loaded = true; } engine.DecreaseLoadingCount(); }
public Font GetFont(String File, PixelColor ForeColor, PixelColor BackColor) { ResourceInfoFont info; info.Name = File; if (BackColor != PixelColor.Black) { info.Fore = ForeColor; info.Back = BackColor; } else { info.Fore = PixelColor.White; info.Back = PixelColor.Black; } Font font = new Font(engine); font.Info = new FontInfo(); font.Info.Font = File.ToLower(); font.Info.ForeColor = ForeColor; font.Info.BackColor = BackColor; font.Info.UseBackColor = !(BackColor.a == 255 && BackColor.r == 0 && BackColor.g == 0 && BackColor.b == 0); if (fonts.ContainsKey(info)) { font.charInfo = fonts[info].charInfo; font.sprite = fonts[info].sprite; font.offset = fonts[info].offset; font.height = fonts[info].height; return(font); } FilePath path = File; if (!fontProcessors.ContainsKey(path.Extension)) { return(null); } engine.IncreaseLoadingCount(); IFontProcessor processor = (IFontProcessor)Activator.CreateInstance(fontProcessors[path.Extension].GetType()); processor.Process((string)path); Log.Debug("load \"" + path.FullPath + "\""); SlimDX.Direct3D9.Texture tex = engine.Device.CreateTexture(MakePowerOfTwo(processor.Size.x), MakePowerOfTwo(processor.Size.y)); SlimDX.Direct3D9.SurfaceDescription desc = tex.GetLevelDescription(0); MemoryUsage += desc.Width * desc.Height * 4; SlimDX.DataRectangle data = tex.LockRectangle(0, SlimDX.Direct3D9.LockFlags.Discard); System.IO.MemoryStream systemCopy = new System.IO.MemoryStream(); processor.Render(systemCopy, data.Pitch, ForeColor, BackColor); processor.Render(data.Data, data.Pitch, ForeColor, BackColor); tex.UnlockRectangle(0); //// debug output //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(processor.Size.x, processor.Size.y, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, processor.Size.x, processor.Size.y), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //byte[] bytes = new byte[processor.Size.y * bmpdata.Stride]; //systemCopy.Seek(0, System.IO.SeekOrigin.Begin); //for (int y = 0; y < processor.Size.y; y++) //{ // systemCopy.Read(bytes, y * bmpdata.Stride, processor.Size.x * 4); //} //System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpdata.Scan0, bytes.Length); //bmp.UnlockBits(bmpdata); //bmp.Save("c:\\font.png"); //bmp.Dispose(); SpriteFrame frame = new SpriteFrame(this, tex, processor.Size, systemCopy.ToArray()); font.sprite = new Sprite(this, "", frame); font.sprite.Resolution = processor.Factor; font.charInfo = processor.CharInfo; font.offset = processor.Offset; font.height = processor.Size.y; engine.DecreaseLoadingCount(); fonts.Add(info, font); return(font); }
private TextureHandle LoadBlpTexture(SlimDX.Direct3D9.Device Render, System.IO.BinaryReader reader) { reader.BaseStream.Position += 4; byte compression = reader.ReadByte(); byte alphaDepth = reader.ReadByte(); byte alphaEncoding = reader.ReadByte(); byte hasMipMap = reader.ReadByte(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); int[] Offsets = new int[16]; int[] Sizes = new int[16]; byte[] ofsTmp = reader.ReadBytes(16 * 4); byte[] sizTmp = reader.ReadBytes(16 * 4); int levelCount = 0; int blockSize = 0; for (int i = 0; i < 16; ++i) { Offsets[i] = BitConverter.ToInt32(ofsTmp, 4 * i); Sizes[i] = BitConverter.ToInt32(sizTmp, 4 * i); if (Offsets[i] != 0 && Sizes[i] != 0) ++levelCount; } SlimDX.Direct3D9.Format texFmt = SlimDX.Direct3D9.Format.Unknown; if (compression == 2) { switch (alphaEncoding) { case 0: texFmt = SlimDX.Direct3D9.Format.Dxt1; blockSize = 2; break; case 1: texFmt = SlimDX.Direct3D9.Format.Dxt3; blockSize = 4; break; case 7: texFmt = SlimDX.Direct3D9.Format.Dxt5; blockSize = 4; break; } } if (compression == 3) { texFmt = SlimDX.Direct3D9.Format.A8R8G8B8; blockSize = 4; } if (texFmt == SlimDX.Direct3D9.Format.Unknown) throw new FormatException("This format is not yet supported, sorry!"); var texture = new SlimDX.Direct3D9.Texture(Render, width, height, levelCount, SlimDX.Direct3D9.Usage.None, texFmt, SlimDX.Direct3D9.Pool.Managed); int curLevel = 0; for (int i = 0; i < 16; ++i) { if (Sizes[i] != 0 && Offsets[i] != 0) { reader.BaseStream.Position = Offsets[i]; byte[] layerData = reader.ReadBytes(Sizes[i]); SlimDX.Direct3D9.Surface surf = texture.GetSurfaceLevel(curLevel); SlimDX.Direct3D9.SurfaceDescription desc = texture.GetLevelDescription(curLevel); System.Drawing.Rectangle rec = System.Drawing.Rectangle.FromLTRB(0, 0, desc.Width, desc.Height); SlimDX.Direct3D9.Surface.FromMemory(surf, layerData, SlimDX.Direct3D9.Filter.Triangle, 0, texFmt, blockSize * rec.Width, rec); ++curLevel; } } return new TextureHandle(texture); }