WPF 中的 SharpDX 渲染 [英] SharpDX render in WPF

查看:20
本文介绍了WPF 中的 SharpDX 渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尽快画线.出于这个原因,我使用 InteropBitmap 实现了一个方法.这很好用.下一步是与 ShardDX 进行比较.基本上我想做的是:在 BackgroundWorker 中运行以下代码.这会通知 WPF WIC 的更新.我发现此代码(创建 ShapeDX 和绘制线所需的所有内容)比使用 InteropBitmap 执行相同操作需要大约 10 毫秒的时间.

I want to draw lines as fast as possible. For that reason I implemented a method using InteropBitmap. This works quite good. Next step was to compare with ShardDX. Basically what I want to do is: Running the following code in a BackgroundWorker. This does inform the WPF about an update of WIC. I found out that this code (creating all needed for ShapeDX and draw line) takes about 10ms longer than doing the same using InteropBitmap.

我现在的问题很简单,如何加快速度?我可以以某种方式更改代码,只需要调用 BeginDraw、创建线条和 EndDraw,而不总是执行所有这些图像编码/解码操作吗?或者有更好的方法吗?

My question now is simply, how to speed this up? Can I change the code somehow that I only have to call BeginDraw, create lines and EndDraw, not always doing all of this Image Encoding/Decoding stuff? Or is there a better approach?

var wicFactory = new ImagingFactory();
var d2dFactory = new SharpDX.Direct2D1.Factory();

const int width = 800;
const int height = 200;

var wicBitmap = new Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);

var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);

var d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties);

var solidColorBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dRenderTarget, SharpDX.Color.White);

d2dRenderTarget.BeginDraw();
//draw whatever you want
d2dRenderTarget.EndDraw();

// Memorystream.
MemoryStream ms = new MemoryStream();
var stream = new WICStream(wicFactory, ms);
// JPEG encoder
var encoder = new SharpDX.WIC.JpegBitmapEncoder(wicFactory);
encoder.Initialize(stream);

// Frame encoder
var bitmapFrameEncode = new BitmapFrameEncode(encoder);
bitmapFrameEncode.Initialize();
bitmapFrameEncode.SetSize(width, height);
var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare;
bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid);
bitmapFrameEncode.WriteSource(wicBitmap);

bitmapFrameEncode.Commit();
encoder.Commit();

ms.Seek(0, SeekOrigin.Begin);

// JPEG decoder
var decoder = new System.Windows.Media.Imaging.JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
// Write to wpf image
_WIC = decoder.Frames[0];
// Tell WPF to update
RaisePropertyChanged("WIC");

bitmapFrameEncode.Dispose();
encoder.Dispose();
stream.Dispose();

与:

System.Windows.Media.Imaging.BitmapFrame _WIC;
    public System.Windows.Media.Imaging.BitmapSource WIC
    {
        get
        {
            return (System.Windows.Media.Imaging.BitmapSource)_WIC.GetAsFrozen();
        }
    }

还有:

<StackPanel>
   <Image Name="huhu1" Source="{Binding WIC}" />  
</StackPanel>

推荐答案

SharpDX Toolkit 通过 Direct3D11-to-Direct3D9 共享纹理支持 WPF.它在 SharpDXElement<中实现/a> 类.

SharpDX Toolkit has support for WPF via a Direct3D11-to-Direct3D9 shared texture. It is implemented in the SharpDXElement class.

您可能无法按原样重用它,因为 Direct2D(您用来绘制的)可以与 Direct3D11.1 或 Direct3D10 互操作,而 SharpDX 使用 Direct3D11 来支持 WPF,因此您需要稍微调整解决方案位.

You may not be able to reuse this as is because Direct2D (which you are using to draw) can interop either with Direct3D11.1 or Direct3D10 and SharpDX uses Direct3D11 for WPF support, so you will need to tweak the solution a little bit.

基本上你需要做到以下几点:

Basically you need to do the following:

  • Initialize Direct3D (10 or 11.1).
  • Initialize Direct2D.
  • Create the D3D render target with Direct2D support and the Shared flag (here is how it is done in SharpDX).
  • Initialize Direct3D9.
  • Create the shared texture.
  • Bind the texture to an D3DImage.

不要忘记调用 D3DImage.AddDirtyRect 渲染目标的内容更新时.

Do not forget to call D3DImage.AddDirtyRect when the contents of the render target are updated.

根据您提供的代码,不清楚您是否只执行一次所有初始化,因此尝试只调用一次任何初始化代码并重用渲染目标 - 只需在每一帧的开头清除它.这是获得体面性能所必需的.

From the code you provided it is not clear if you are doing all initializations only once or not, so try to call any initialization code only once and reuse the render target - just clear it at the beginning of every frame. This is mandatory to get a decent performance.

更新:SharpDX.Toolkit 已被弃用,不再维护.它被移动到一个单独的存储库.

Update: SharpDX.Toolkit has been deprecated and it is not maintained anymore. It is moved to a separate repository.

这篇关于WPF 中的 SharpDX 渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆