private static ShaderResourceView LoadFromDecoder(D3DDevice device, ImagingFactory factory, BitmapDecoder bitmapDecoder)
        {
            if (bitmapDecoder.FrameCount == 0)
                throw new ArgumentException("Image file successfully loaded, but it has no image frames.");

            BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
            BitmapSource bitmapSource = bitmapFrameDecode.ToBitmapSource();

            // create texture description
            Texture2DDescription textureDescription = new Texture2DDescription()
            {
                Width = bitmapSource.Size.Width,
                Height = bitmapSource.Size.Height,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.R8G8B8A8_UNORM,
                SampleDescription = new SampleDescription()
                {
                    Count = 1,
                    Quality = 0,
                },
                Usage = Usage.Dynamic,
                BindFlags = BindFlag.ShaderResource,
                CpuAccessFlags = CpuAccessFlag.Write,
                MiscFlags = 0
            };

            // create texture
            Texture2D texture = device.CreateTexture2D(textureDescription);

            // Create a format converter
            WICFormatConverter converter = factory.CreateFormatConverter();
            converter.Initialize(
                bitmapSource,
                PixelFormats.Pf32bppRGBA,
                BitmapDitherType.None,
                BitmapPaletteType.Custom);

            // get bitmap data
            byte[] buffer = converter.CopyPixels();

            // Copy bitmap data to texture
            MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, MapFlag.Unspecified);
            Marshal.Copy(buffer, 0, texmap.Data, buffer.Length);
            texture.Unmap(0);

            // create shader resource view description
            ShaderResourceViewDescription srvDescription = new ShaderResourceViewDescription()
            {
                Format = textureDescription.Format,
                ViewDimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new Texture2DShaderResourceView()
                {
                    MipLevels = textureDescription.MipLevels,
                    MostDetailedMip = 0
                }
            };

            // create shader resource view from texture
            return device.CreateShaderResourceView(texture, srvDescription);
        }
Exemple #2
0
        private static ShaderResourceView LoadFromDecoder(D3DDevice device, ImagingFactory factory, BitmapDecoder bitmapDecoder)
        {
            if (bitmapDecoder.FrameCount == 0)
            {
                throw new ArgumentException("Image file successfully loaded, but it has no image frames.");
            }

            BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
            BitmapSource      bitmapSource      = bitmapFrameDecode.ToBitmapSource();

            // create texture description
            Texture2DDescription textureDescription = new Texture2DDescription()
            {
                Width             = bitmapSource.Size.Width,
                Height            = bitmapSource.Size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8UNorm,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0,
                },
                Usage                        = Usage.Dynamic,
                BindingOptions               = BindingOptions.ShaderResource,
                CpuAccessOptions             = CpuAccessOptions.Write,
                MiscellaneousResourceOptions = MiscellaneousResourceOptions.None
            };

            // create texture
            Texture2D texture = device.CreateTexture2D(textureDescription);

            // Create a format converter
            FormatConverter converter = factory.CreateFormatConverter();

            converter.Initialize(
                bitmapSource,
                PixelFormats.Prgba32Bpp,
                BitmapDitherType.None,
                BitmapPaletteType.Custom);

            // get bitmap data
            byte[] buffer = converter.CopyPixels();

            // Copy bitmap data to texture
            MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, Microsoft.WindowsAPICodePack.DirectX.Direct3D10.MapOptions.None);

            Marshal.Copy(buffer, 0, texmap.Data, buffer.Length);
            texture.Unmap(0);

            // create shader resource view description
            ShaderResourceViewDescription srvDescription = new ShaderResourceViewDescription()
            {
                Format        = textureDescription.Format,
                ViewDimension = ShaderResourceViewDimension.Texture2D,
                Texture2D     = new Texture2DShaderResourceView()
                {
                    MipLevels       = textureDescription.MipLevels,
                    MostDetailedMip = 0
                }
            };

            // create shader resource view from texture
            return(device.CreateShaderResourceView(texture, srvDescription));
        }