Пример #1
0
        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);
            }
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
 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);
 }
Пример #4
0
        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));
        }
Пример #5
0
        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();
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }