public static D3D11.ID3D11Texture2D LoadTexture2DFromMappedTexture(EngineDevice device, MemoryMappedTexture <int> mappedTexture, bool generateMiplevels)
            {
                // Create the texture
                var dataRectangle = new D3D11.SubresourceData(
                    mappedTexture.Pointer,
                    mappedTexture.Width * 4);

                D3D11.ID3D11Texture2D result;
                if (generateMiplevels)
                {
                    result = device.DeviceD3D11_1.CreateTexture2D(new D3D11.Texture2DDescription
                    {
                        Width             = mappedTexture.Width,
                        Height            = mappedTexture.Height,
                        ArraySize         = 1,
                        BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget,
                        Usage             = D3D11.ResourceUsage.Default,
                        CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                        Format            = DEFAULT_TEXTURE_FORMAT,
                        MipLevels         = 0,
                        OptionFlags       = D3D11.ResourceOptionFlags.None | D3D11.ResourceOptionFlags.GenerateMips,
                        SampleDescription = new SampleDescription(1, 0)
                    },
                                                                  new D3D11.SubresourceData[]
                    {
                        dataRectangle, dataRectangle, dataRectangle, dataRectangle,
                        dataRectangle, dataRectangle, dataRectangle, dataRectangle,
                        dataRectangle, dataRectangle, dataRectangle, dataRectangle
                    });

                    // Auto generate miplevels
                    using (var shaderResourceView = device.DeviceD3D11_1.CreateShaderResourceView(result))
                    {
                        device.DeviceImmediateContextD3D11.GenerateMips(shaderResourceView);
                    }
                }
                else
                {
                    result = device.DeviceD3D11_1.CreateTexture2D(new D3D11.Texture2DDescription
                    {
                        Width             = mappedTexture.Width,
                        Height            = mappedTexture.Height,
                        ArraySize         = 1,
                        BindFlags         = D3D11.BindFlags.ShaderResource,
                        Usage             = D3D11.ResourceUsage.Default,
                        CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                        Format            = DEFAULT_TEXTURE_FORMAT,
                        MipLevels         = 1,
                        OptionFlags       = D3D11.ResourceOptionFlags.None,
                        SampleDescription = new SampleDescription(1, 0)
                    },
                                                                  new D3D11.SubresourceData[] { dataRectangle });
                }

                return(result);
            }
        /// <summary>
        /// Converts a System.Drawing.Bitmap to a DirectX 11 texture object.
        /// </summary>
        /// <param name="device">Device on which the resource should be created.</param>
        /// <param name="bitmap">The source bitmap.</param>
        /// <param name="mipLevels">Total count of levels for mipmapping.</param>
        internal static D3D11.ID3D11Texture2D LoadTextureFromBitmap(EngineDevice device, GDI.Bitmap bitmap, int mipLevels)
        {
            device.EnsureNotNull(nameof(device));
            bitmap.EnsureNotNull(nameof(bitmap));
            mipLevels.EnsurePositiveOrZero(nameof(mipLevels));

            D3D11.ID3D11Texture2D?result = null;

            // Lock bitmap so it can be accessed for texture loading
            var bitmapData = bitmap.LockBits(
                new GDI.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            try
            {
                // Open a reading stream for bitmap memory
                var dataRectangle = new D3D11.SubresourceData(bitmapData.Scan0, bitmap.Width * 4);

                // Load the texture
                result = device.Internals.DeviceD3D11_1.CreateTexture2D(
                    new D3D11.Texture2DDescription
                {
                    BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget,
                    CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                    Format            = GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT,
                    OptionFlags       = D3D11.ResourceOptionFlags.None | D3D11.ResourceOptionFlags.GenerateMips,
                    MipLevels         = 0,
                    Usage             = D3D11.ResourceUsage.Default,
                    Width             = bitmap.Width,
                    Height            = bitmap.Height,
                    ArraySize         = 1,
                    SampleDescription = new SampleDescription(1, 0)
                },
                    new[]
                {
                    dataRectangle, dataRectangle, dataRectangle, dataRectangle,
                    dataRectangle, dataRectangle, dataRectangle, dataRectangle,
                    dataRectangle, dataRectangle, dataRectangle, dataRectangle
                });

                // Workaround for now... auto generate mip-levels
                using var shaderResourceView = device.Internals.DeviceD3D11_1.CreateShaderResourceView(result);
                device.Internals.DeviceImmediateContextD3D11.GenerateMips(shaderResourceView);
            }
            finally
            {
                // Free bitmap-access resources
                bitmap.UnlockBits(bitmapData);
            }

            return(result);
        }