将SlimDX.Direct3D11 Texture2D转换为.Net位图

时间:2016-05-08 12:29:30

标签: bitmap texture2d slimdx directcompute

将.Net位图转换为SlimDx Texture2D的速度非常快,如下所示: http://www.rolandk.de/index.php?option=com_content&view=article&id=65:bitmap-from-texture-d3d11&catid=16:blog&Itemid=10

private Texture2D TextureFromBitmap(FastBitmapSingle fastBitmap)
{
    Texture2D result = null;
    DataStream dataStream = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, true, false);
    DataRectangle dataRectangle = new DataRectangle(fastBitmap.BitmapData.Stride, dataStream);
    try
    {
        Texture2DDescription dt = new Texture2DDescription
        {
            BindFlags = BindFlags.ShaderResource,
            CpuAccessFlags = CpuAccessFlags.None,
            Format = Format.B8G8R8A8_UNorm,
            OptionFlags = ResourceOptionFlags.None,
            MipLevels = 1,
            Usage = ResourceUsage.Immutable,
            Width = fastBitmap.Size.X,
            Height = fastBitmap.Size.Y,
            ArraySize = 1,
            SampleDescription = new SampleDescription(1, 0),
        };
        result = new Texture2D(device, dt, dataRectangle);
    }
    finally
    {
        dataStream.Dispose();
    }
    return result;
}

为了将纹理转换回正确格式的.Net位图,我使用它,但速度非常慢:

private bool BitmapFromTexture(FastBitmapSingle fastBitmap, Texture2D texture)
{
    using (MemoryStream ms = new MemoryStream())
    {
        Texture2D.ToStream(device.ImmediateContext, texture, ImageFileFormat.Bmp, ms);
        ms.Position = 0;
        using (Bitmap temp1 = (Bitmap)Bitmap.FromStream(ms))
        {
            Rectangle bounds = new Rectangle(0, 0, temp1.Width, temp1.Height);
            BitmapData BitmapDataIn = temp1.LockBits(bounds, ImageLockMode.ReadWrite, temp1.PixelFormat);
            using (DataStream dataStreamIn = new DataStream(BitmapDataIn.Scan0, BitmapDataIn.Stride * BitmapDataIn.Height, true, false))
            using (DataStream dataStreamOut = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, false, true))
            {
                dataStreamIn.CopyTo(dataStreamOut);
            }
            temp1.UnlockBits(BitmapDataIn);
            BitmapDataIn = null;
        }
    }
    return true;
}

有没有更快的方法???我尝试了很多,比如:

但DataRectangle的数据正好是我在DataStream中需要的数据的8倍

private bool BitmapFromTexture(FastBitmapSingle fastBitmap, Texture2D texture)
{
    using (Texture2D buff = Helper.CreateTexture2D(device, texture.Description.Width, texture.Description.Height, Format.B8G8R8A8_UNorm, BindFlags.None, ResourceUsage.Staging, CpuAccessFlags.Read | CpuAccessFlags.Write))
    {
        device.ImmediateContext.CopyResource(texture, buff);

        using (Surface surface = buff.AsSurface())
        using (DataStream dataStream = new DataStream(fastBitmap.BitmapData.Scan0, fastBitmap.BitmapData.Stride * fastBitmap.BitmapData.Height, false, true))
        {
            DataRectangle rect = surface.Map(SlimDX.DXGI.MapFlags.Read);

            rect.Data.CopyTo(dataStream);

            surface.Unmap();
        }
    }

    return true;
}

有人可以帮忙吗? 复制我的数据大约占整个计算时间的50%。 如果这可以解决,我的应用程序会更快......

2 个答案:

答案 0 :(得分:0)

我正在使用的转换器是:

    public static BitmapSource Texture2DToBitmapSource(Texture2D texture2D)
    {
        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            Texture2D.ToStream(App.device.ImmediateContext, texture2D, ImageFileFormat.Png, memoryStream);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            return BitmapFrame.Create(memoryStream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
        }
    }

但是我遇到的问题是dpi是72而不是像我在程序中使用的BitmapSource那样96。希望它有所帮助。

我也在使用SlimDX。

WPF位图转换非常令人头疼。通过进入不安全的代码和锁定位来获得最大速度可能需要从Pbgra和bgra到rgba等的转换。我通常最终回到旧的绘图位图以在许多程序中转换为BitmapSource。这个片段是我正在使用的,直到我可以从我的计算着色器中获取正确的PixelFormat。

        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            Texture2D.ToStream(App.device.ImmediateContext, texture, ImageFileFormat.Png, memoryStream);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);

            //Create an image from a stream.
            System.Drawing.Image bitmap = System.Drawing.Bitmap.FromStream(memoryStream); memoryStream.Close();
            var hBitmap = ((System.Drawing.Bitmap)bitmap).GetHbitmap();
            BitmapSource source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions()
                );
            if (!(source.Width == pixelWidth)) { }
            if (!(source.Height == pixelHeight)) { }
            return source;
        }

使用System.Drawing.Image或System.drawing.Bitmap中的HBITMAP可以更好地控制不同PixelFormats的锁定位和转换。旧的CreateBitmap函数和DIB也使用HBITMAP进行转换。

速度的另一个瓶颈我会想象在Texture2D.ToStream()例程中。这些ImageFileFormat.Png,ImageFileFormat.Bmp,ImageFileFormat.Jpg等.SlimDX没有在Texture2D中添加一些复制功能,而DX11在某处有。

答案 1 :(得分:0)

我找到了一个解决方案,感谢:http://www.rolandk.de/wp/2013/06/inhalt-der-rendertarget-textur-in-ein-bitmap-kopieren/

但故事有点复杂,因为纹理间距与Bitmap Stride不匹配,所以在这里我的解决方案,比我问题中的那个快10倍:

private bool BitmapFromTexture(FastBitmapSingle fastBitmap, Texture2D texture, int row, int col)
{
    using (Texture2D stage = Helper.CreateStagingTexture(device, fastBitmap.BitmapWidths[col], fastBitmap.BitmapHeights[row]))
    {
        device.ImmediateContext.CopyResource(texture, stage);
        DataStream dsIn;
        DataBox dataBox = device.ImmediateContext.MapSubresource(stage, 0, 0, MapMode.Read, D3D.MapFlags.None, out dsIn);
        int dx = dataBox.RowPitch - fastBitmap.BitmapData[row][col].Stride;
        try
        {
            using (DataStream dsOut = new DataStream(fastBitmap.BitmapData[row][col].Scan0, fastBitmap.BitmapData[row][col].Stride * fastBitmap.BitmapData[row][col].Height, false, true))
            {
                for (int r = 0; r < fastBitmap.BitmapData[row][col].Height; r++)
                {
                    dsOut.WriteRange<byte>(dsIn.ReadRange<byte>(fastBitmap.BitmapData[row][col].Stride));
                    dsIn.Position += dx;
                }
            }
        }
        finally
        {
            device.ImmediateContext.UnmapSubresource(stage, 0);
        }
        dsIn.Dispose();
    }
    return true;
}