예제 #1
0
        public static Texture LoadTgaTexture(
            GraphicsDevice graphicsDevice,
            ResourceFactory resourceFactory,
            Stream stream,
            Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm)
        {
            using var bitmap = new TgaImage(stream, true).GetBitmap();

            var width  = (uint)bitmap.Width;
            var height = (uint)bitmap.Height;

            var sampledTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Sampled));
            var stagingTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Staging));

            var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var byteCount  = bitmapData.Stride * bitmap.Height;
            var pixelData  = new byte[byteCount];
            var pointer    = bitmapData.Scan0;

            Marshal.Copy(pointer, pixelData, 0, byteCount);

            graphicsDevice.UpdateTexture(stagingTexture, pixelData, 0, 0, 0, width, height, 1, 0, 0);

            CreateCommandList(graphicsDevice, resourceFactory, stagingTexture, sampledTexture);

            return(sampledTexture);
        }
예제 #2
0
        public static Texture LoadBlpTexture(
            GraphicsDevice graphicsDevice,
            ResourceFactory resourceFactory,
            Stream stream,
            Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm)
        {
            using var blpFile = new BlpFile(stream);

            var width  = (uint)blpFile.Width;
            var height = (uint)blpFile.Height;

            var mipMapCount = (uint)blpFile.MipMapCount;

            var sampledTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, mipMapCount, 1, pixelFormat, TextureUsage.Sampled));
            var stagingTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, mipMapCount, 1, pixelFormat, TextureUsage.Staging));

            var mipMapWidths  = new uint[mipMapCount];
            var mipMapHeights = new uint[mipMapCount];

            for (var mipMapLevel = 0; mipMapLevel < mipMapCount; mipMapLevel++)
            {
                var pixelData    = blpFile.GetPixels(mipMapLevel, out var w, out var h);
                var mipMapWidth  = (uint)w;
                var mipMapHeight = (uint)h;

                mipMapWidths[mipMapLevel]  = mipMapWidth;
                mipMapHeights[mipMapLevel] = mipMapHeight;
                graphicsDevice.UpdateTexture(stagingTexture, pixelData, 0, 0, 0, mipMapWidth, mipMapHeight, 1, (uint)mipMapLevel, 0);
            }

            CreateCommandList(graphicsDevice, resourceFactory, stagingTexture, sampledTexture);

            return(sampledTexture);
        }
        /// <inheritdoc/>
        /// <remarks>
        /// Created textures are 2D, non-multisampled, non-mipmapped textures with RGBA8 UNorm format and
        /// texture usage <see cref="TextureUsage.Sampled"/>.
        /// </remarks>
        public int CreateTexture(int width, int height, VELDRIDPIXFMT pFormat)
        {
            CheckDisposed();

            var textureDescr = TextureDescription.Texture2D((uint)width, (uint)height, 1, 1, pFormat, TextureUsage.Sampled);
            var tex          = _gd.ResourceFactory.CreateTexture(ref textureDescr);

            return(_AddTexture(tex));
        }
예제 #4
0
        public static Texture LoadTexture(
            GraphicsDevice graphicsDevice,
            ResourceFactory resourceFactory,
            Stream stream,
            Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm)
        {
            FileFormatVersion blpVersion;
            var oldPosition = stream.Position;

            using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
            {
                blpVersion = (FileFormatVersion)reader.ReadUInt32();
            }

            stream.Position = oldPosition;
            return(Enum.IsDefined(typeof(FileFormatVersion), blpVersion)
                ? LoadBlpTexture(graphicsDevice, resourceFactory, stream, pixelFormat)
                : LoadTgaTexture(graphicsDevice, resourceFactory, stream, pixelFormat));
        }
예제 #5
0
        public static Texture GetDummyTexture(
            GraphicsDevice graphicsDevice,
            ResourceFactory resourceFactory,
            Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm)
        {
            var width  = 1U;
            var height = 1U;

            var sampledTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Sampled));
            var stagingTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, 1, 1, pixelFormat, TextureUsage.Staging));

            var pixelData = new byte[] { 3, 3, 255, 255 };

            graphicsDevice.UpdateTexture(stagingTexture, pixelData, 0, 0, 0, width, height, 1, 0, 0);

            CreateCommandList(graphicsDevice, resourceFactory, stagingTexture, sampledTexture);

            return(sampledTexture);
        }