问题
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?
回答1:
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.)
回答2:
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.
回答3:
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?
回答4:
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:
- Windows 7
- Windows Server 2008 R2
- Windows Vista SP2 with Platform Update for Windows Vista
- Windows Server 2008 SP2 with Platform Update for Windows Server 2008
来源:https://stackoverflow.com/questions/1074813/looking-for-a-faster-than-gdi-solution-for-rendering-dynamic-data-plots