views:

663

answers:

4

I've written a simple GDI-based data plotter using C++/CLI but it's not particularly fast (some basic profiling indicates it's the rendering to screen that's the problem).

Is there any way to enable hardware acceleration for a UserControl or is there a .net interface for direct3D? ...or are there some other options I could consider.

We're using managed code so the solution really needs to be CLI compatible if at all possible.

[Edit] In case it helps, I'm rending strips (128 data points) of rectangles which are each 2x2 pixels using Graphics::FillRectangle - maybe there's a better way of doing that?

+2  A: 

From my experience, you won't get good enough performance out of using GDI+. Even for simple drawing, you will quickly realize that there's a lot of overhead.

Alternatives would (as you mentioned) be Direct3D, or you could consider regular GDI with system calls. That obviously makes the code platform dependent but it can be quite fast. I've had good results using that.

It all depends on how much complexity you're willing to deal with. GDI can be relatively easy once you figure out the basics, Direct3D is a bit more complex. Though Direct3D is more future proof.

Jordy Boom
Does D3D have a .net wrapper? I've been trying to find one but the fact that I've not googled up anything useful yet suggests the answer is probably a no.
Jon Cage
DirectX/Direct3D already has a managed interface that is provided if you get the SDK. If you're looking for a simplified interface I'm not sure if a decent one exists. I don't think it would be unlikely that there is an abstract canvas out there that can use Direct3D behind the scenes.
Jordy Boom
Ah, I hadn't realised it came with a manged interface. I'll give that a shot - thanks very much!
Jon Cage
+3  A: 

Managed DirectX has been deprecated for some time. You really don't want to use that. Instead, you should use SlimDX which is an open source interop layer for the DirectX SDK APIs written in C++/CLI. It is better than Managed DirectX and is supported by an expert community of developers. (I'm going to be working on improving the DirectWrite support with them soon.)

legalize
+1/Accepted: This looks excellent - thanks!
Jon Cage
do u have link for managed directx that says it has been deprecated
Ahmed Said
I don't have a link handy, but it has been deprecated for quite some time. The March 2009 SDK says this under "Older Components:"<blockquote>These technologies were removed from the Nov 2007 release: DirectAnimation, Managed DX 1.1, Direct3D Retained Mode, and DirectPlay Voice.</blockquote>So its been deprecated for at least 2+ years.
legalize
+1  A: 

It is true that GDI+ isn't very good performance-wise, however I have myself written a GDI+ plotter in a work-related project that's able to spit out graphs with thousand of points at ~30 frames pr second at 1680x1050 resolution (scrolling graph).

It took a lot of tuning to achieve this:

  • Convert everything to one single path before drawing.
  • If using back-buffer, use one with pixel format Format32bppPArgb, this can speed up blitting 2-4x.
  • If drawing a path with a lot of vertical lines (high frequency signal), draw them as horizontal lines on a back buffer instead, and then draw the image rotated on the screen. Be aware that drawing an image rotated also have certain cost.

I can't see how your scenario requires a whole lot of optimization though, 128 points of data is nothing. Putting these points into one GraphicsPath could make a difference though, since that would mean less marshalling overhead.

What resolution and frame rate are we talking about here by the way?

BeeWarloc
Actually it's more like 6.5k points (128*360/7). Resolution is pretty low (~500*500px). Each point is drawn as a rectangle - have a look at my other post (http://stackoverflow.com/questions/1055920/how-to-write-to-a-bitmap-image-buffer-for-faster-gdi-displays). We can cheat a bit because I only write 128*2 points each time I get new data.
Jon Cage
+1  A: 

Microsoft now also has Direct2D, which is hardware accelerated 2D drawing:

Direct2D is a hardware-accelerated, immediate-mode, 2-D graphics API that provides high performance and high-quality rendering for 2-D geometry, bitmaps, and text. The Direct2D API is designed to interoperate well with GDI, GDI+, and Direct3D.

It requires Windows 7/Server 2008 R2, but support has been back-added to Vista/Server 2008 through the Platform Update:

Ian Boyd