Realtime graph for WPF

by Miha Markič 10. January 2012 11:31

You know the Task Manager’s CPU Usage History graph that shows CPU utilization over time? Try that type of the graph in WPF and you’ll realize that it isn’t as easy as it should be due to the WPF’s performance for real time graphs. Which sucks due to the way WPF works. Even with 3rd party you’ll have hard time to find a graph fast enough to cope with even more than few hundred samples.

Hence I’ve decide to build my own real time graph. It is based on Direct2D because Direct2D is much faster when it comes to presenting the graphical result. However, merging Direct2D into WPF is, again, not an easy task as it should be (hint to MS – there should be native WPF support for hosting Direct2D output). Anyway, I found the article Using Direct2D with WPF on CodePlex. It comes with sources which I use. Those sources require Windows API CodePack(WACP), a bug ridden set of managed API’s to various, otherwise unsupported, features (MS went with this approach (if it works, it works, if it doesn’t fix it by yourself) instead of providing an official support). The WACP binaries I provide are compiled from 1.1. sources with some bug fixed regarding DirectCompute.

Here is a snapshot from a graph showing a sinusoide as a product of many samples (the attached example contains required code and binaries).

realtimechart

I utilize a combination of Direct2D and samples optimization which yields smooth result even with million and more samples.

 

Required code

Create a class that implements IGraphItem interface. This class will hold a single sample through Time and Value property. Time typically represents a time elapsed since the start and it is expressed in millisecond while Value is a double and holds whatever value.

public class RealtimeGraphItem : IGraphItem
{    
    public int Time { get; set; }
    public double Value { get; set; }
}

Configure RealtimeGraphControl like

xmlns:rg="clr-namespace:Righthand.RealtimeGraph;assembly=Righthand.RealtimeGraph"
...
<rg:RealtimeGraphControl x:Name="Graph" AxisColor="Blue" AxisWidth="1"      VerticalLinesInterval="5" VerticalLabelsStep="2"  SpanX="100"
      MaxY="250" MinY="50"  HorizontalLinesInterval="10"/>

where SpanX is time span expressed in seconds telling the graph how many seconds are shown.

Finally create a BindingList<RealtimeGraphItem> source and bind it to Graph.SeriesSource property (BindingList is required as it has the event ListChanged that is used to trigger rendering update of the graph). And that’s it. When you add new items to the source the graph will automatically reflects the new state.

 

Requirements

.net 4.0

Dowload

Attached are binaries and the sources for the Example project.

Let me know what you think.

RealtimeGraph_1_0.zip (1.31 mb)

Tags:

.net 4.0 | DirectX | Graphics

Multitouch development on Windows 7

by Miha Markič 20. February 2010 14:10

I’ll be developing a multitouch application in WPF 4.0 running on Windows 7 and using Windows 7 native multitouch (supported in WPF 4.0) running in portrait mode. Dual finger touch should be enough. I have all the tools handy except for one: a multitouch input device aka multitouch monitor. So I am looking around for it and have found these options so far:

Bamboo Fun tablet

Last year I’ve bought a Bamboo Fun pen & touch tablet from Wacom. It supports both stylus and two finger touch input. I’ve assumed it will work natively with Windows 7. But it doesn’t. The core problem is that Bamboo has a relative touch positioning while Windows 7 requires absolute positioning – in other words Bamboo doesn’t know where your fingers are until you touch its surface. Furthermore Wacom decided to provide support through a generic driver to other OS beside Windows 7 and thus no Windows 7 native support is provided at this point in time. Disappointing.

Multitouch monitor

The most logical choice would be a multitouch enabled monitor. Heck, Windows 7 has been around for a while now (including beta and RC period) and there should be plenty of such monitors. At least that’s why I thought. Wrong again. There are some choices though. All of these supports Windows 7 native multitouch.

Acer T230H

A 23” 1920x1080 widescreen monitor from Acer. It supports dual touch through a some sort of simple mechanism using cameras. Has problems when you cross fingers or something like that. Not a huge problem in my case. But it has no pivot feature. I guess I’ll just put in horizontal portrait position on my desk or somewhere near. Despite these shortcomings this is my first choice at this point. The price tag is around 300€.

Dell SX2210T

A 21.5” 1920x1080 widescreen monitor from Dell. Smaller, slightly more expensive, dual-touch and no pivot as well.

Compaq L2105tm

The most hidden of the three. Similar to Dell’s one: 21.5” 1920x1080, no pivot. Couldn’t find it in EU so I am not sure about the price but in US is probably cheaper compared to Dell’s.

3M Multitouch developer kit

This one is interesting. A 19” 1440x900 monitor with support for up to 10 finger multi touch input. Looks like a perfect choice if it wasn’t for its price which is listed as $1.499 in US. I assume this is translated to >1.499€ for the EU. Ouch. Yet, this is the only multi-touch display that supports more than two fingers input.

Tablet PC

As an alternative to a proper monitor I might consider a tabled PC such as Dell Latitude XT2 or Acer AS5738PG-6306. First one is expensive and not exactly a development-grade fast machine, yet is a good quality product. The later is much cheaper and perhaps faster but has one fatal flaw in my case: the screen doesn’t rotate to “tablet” position and as such it won’t work in portrait mode. Furthermore touching the screen looks kind of problematic since you can easily flip it (imagine touching a standard laptop). Both laptops feature an integrated graphic card which isn’t good either.

A tablet PC is not the best option for my project anyway so I didn’t investigate much in this direction. The same goes for All-in-one multitouch PCs.

DIY alternative

Heh, I might even built a table like MS Surface by myself. Impossible? Not at all nor it is expensive. Actually it is very cheap. On the negative side it is quite time consuming, even more if you aren’t used to build such things.

Software simulation with multiple mice(MultitouchVista project)

There is way to simulate multitouch with multiple mice on normal monitor as well. Unfortunately it is not a feature of the OS but rather through a project hosted at CodePlex. Note that project has a Vista name in it, yet it runs under Windows 7 only – both x64 and x86. The idea is to trick OS to believe that mouse pointers are in reality fingers. This is done through a custom driver and a couple of services. I’ve tried to make it work with a single mouse to simulate panning and it worked on IE but not in my WPF 4.0 application for some reason. It was really a quick test and I should investigate why it isn’t working for my application further. But if I make it work this will be my way of doing multi touch until I get a proper multi touch monitor, possibly Acer T230H.

Conclusion

Obviously multitouch development requires either a very expensive full featured or a cheaper, but simplified and feature lacking hardware. If you don’t want to spend any money at all then you have to check the MultitouchVista project. If you are looking for a cheap dual touch monitor then Acer T230H sounds like a good choice.

Note that the hardware characteristics in this post aren’t based on real experience but rather on the data collected from internet.

If you have a different, or a better solution, or some real experience, please let me know.

Tags: , , ,

Graphics | Hardware | Windows 7

A managed path to DirectCompute

by Miha Markič 4. December 2009 21:13

About DirectCompute

After NVidia Cuda, OpenCL we got DirectX’s version (of GPGPU parallelism for numerical calculations) named DirectCompute. All three technologies are very similar and have one goal: to provide some sort of API and shader language for numerical calculations that GPGPU is capable of. And if you use it wisely the performance gains over normal CPU are huge, really huge. Not just that you offload CPU but the calculations due to GPGPU massive multithreading (and multicore) are much faster compared to even the most advanced x64 CPU.

It works like this: you prepare enough data that can be worked on in parallel, send it to GPGPU, wait till GPGPU finished processing the data and fetch the results back to the CPU where your application can use them. You can achieve up to a TFLOP with a graphics card sold today.

I guess this information is enough to see what is all about. For more information on the topic check out  Cuda and OpenCL. I’d suggest to check out the DirectCompute web site but the reality is that I couldn’t find any official (or non-official) page that deals with it (it is the youngest of the three but still that could be more online information, don’t you think). However you might check the DirectCompute PDC 2009 presentation here and a simple tutorial.

Of the three I am most interested in DirectCompute because:

  1. Cuda is tied to NVidia GPGPU while OpenCL and DirectCompute aren’t
  2. I prefer using DirectX over OpenGL for a simple reason – I did work for a client using DirectX so I am more familiar with it

I have a NVidia silent GeForce 9600 series graphic card (cards from GeForce 8 and up are supported) and the good news is that NVidia has been supporting CUDA for a while and recently introduced support for DirectCompute as well. OpenCL is also supported. I don’t know the current state of support for ATI cards.

There is a potential drawback if you go with DirectCompute though. It is a part of DirectX 11 and will run on Windows 7, Windows 2008 and Vista SP2 (somebody correct me). The problem is if you have an older Windows OS like Windows XP or you want support older Windows. In that case you might consider alternatives.

Goal

The not that good aspect of a young technology such as DirectCompute is lack of samples, documentation and tutorials are almost nonexistent. What’s even worse for a .net developer as I am is apparent lack of managed support. Which turns to be there to some extent through Windows API Code Pack (WACP) managed code wrappers. Note, WACP isn’t very .netish such as Managed DirectX was but it is good enough.

So I tried the managed approach for BasicCompute11 C++ sample (that comes with DirectX SDK) through WACP and here are the steps to make it run. It should give you a good starting point if you are DirectComputing through managed code and even if you are using native code. Hopefully somebody will benefit from my experience.

BasicCompute11 sample is really a simple one. It declares a type holding an int and a float, creates two arrays of those with an incremental value ( {0, 0.0}, {1, 1.1}, {2, 2.2}, ….{8191, 8191.0}) and adds them together to a third array.

I’ll be working on Windows 7 x64 with NVidia GeForce 9600 graphics card but will create a Win32 executable.

Steps

1. Install the latest NVidia graphics drivers. Current version 195.62 works well enough.

2. Install Windows 7 SDK. It is required because WACP uses its include files and libraries.

3. Make sure that Windows 7 SDK version is currently selected. You’ll have to run [Program Files]\Microsoft SDKs\Windows\v7.0\Setup\SDKSetup.exe. Note that I had to run it from command prompt because it didn’t work from UI for some reason.

4. Install DirectX SDK August 2009 SDK.

  • If it doesn't exist yet then declare an environment variable DXSDK_DIR that points to [Program Files (x86)]\Microsoft DirectX SDK (August 2009)\ path.
    image

5. Install Windows API Code Pack. Or better, copy the source to a folder on your computer. Open those sources and do the following:

  • Add $(DXSDK_DIR)Include path to Include folder
    image
  • And $(DXSDK_DIR)Lib\x86 to libraries
    image

After these settings the project should compile. However, there are additional steps, because one of the methods in there is flawed. Let’s correct it.

  • Open file Header Files\D3D11\Core\D3D11DeviceContext.h and find method declaration 
    Map(D3DResource^ resource, UInt32 subresource, D3D11::Map mapType, MapFlag mapFlags, MappedSubresource mappedResource);
    (there are no overloads to Map method). Convert it to
    MappedSubresource^ Map(D3DResource^ resource, UInt32 subresource, D3D11::Map mapType, MapFlag mapFlags);
  • Open file Source Files\D3D11\Core\D3D11DeviceContext.cpp and find the (same) method definition
    DeviceContext::Map(D3DResource^ resource, UInt32 subresource, D3D11::Map mapType, D3D11::MapFlag mapFlags, MappedSubresource mappedResource)
    Replace the entire method with this code:
    MappedSubresource^ DeviceContext::Map(D3DResource^ resource, UInt32 subresource, 
    D3D11::Map mapType, D3D11::MapFlag mapFlags) { D3D11_MAPPED_SUBRESOURCE mappedRes; CommonUtils::VerifyResult(GetInterface<ID3D11DeviceContext>()->Map( resource->GetInterface<ID3D11Resource>(), subresource, static_cast<D3D11_MAP>(mapType), static_cast<UINT>(mapFlags), &mappedRes)); return gcnew MappedSubresource(mappedRes); }

DirectX wrappers we need are now functional. Compile the project and close it.

6. Open DirectX Sample Browser (you’ll find it in Start\All Programs\Microsoft DirectX SDK (August 2009) and install BasicCompute11 sample somewhere on the disk.

image

7. Open the sample in Visual Studio 2008 and run it. The generated console output should look like this:

image Pay attention to the output because if DirectX can’t find an adequate hardware/driver support for DirectCompute it will run the code with a reference shader – using CPU not GPGPU (“No hardware Compute Shader capable device found, trying to create ref device.”) or even won’t run at all.

If the sample did run successfully it means that your environment supports DirectCompute. Good for you.

8. Now try running my test project (attached to this article) that is pure managed C# code. It more or less replicates the original BasicCompute11 sample which is a C++ project. The main differences with the original are two:

  • I removed the majority of device capability checking code (you’ll have to have compute shader functional or else…)
  • I compile compute shader code (HLSL –> FX) at design time rather at run time. This is required because there are no runtime compilation managed wrappers in WACP because those are part of D3DX which isn’t part of the OS but rather redistributed separately. So you have to rely on FXC compiler (part of DirectX SDK). A note here: I’ve lost quite a lot of time (again!) figuring out why FXC doesn’t find a suitable entry point to my compute shader code when compiling (and errors out). I finally remembered that I’ve stumbled upon this problem quite a long time ago: FXC compiles only ASCII(!!!!!) files. Guys, is this 2009 or 1979? Ben 10 would say. “Oh man.”
    Anyway, the FX code is included with project so you won’t have to compile it again. This time the output should look like this:
    image

I didn’t document my rewritten BasicCompute11 project. You can search for comments in the original project.

Conclusion

Compute shaders are incredibly powerful when it comes to numerical calculations. And being able to work with them through managed code is just great. Not a lot of developers will need their power though, but if/when they’ll need them the support is there.

As for final word: I miss documentations, tutorials and samples. Hopefully eventually they’ll come but for now there is a good book on CUDA (remember, the technologies are similar) – the book comes in a PDF format along NVidia CUDA SDK.

Have fun DirectComputing!

DirectComputeManaged.zip (2.05 mb)

Tags: , , , ,

.net 3.5 | DirectX | Graphics | Parallel programming

Hollywood graphics FX

by Miha Markič 21. January 2009 11:15

Did you ever watch a Hollywood movie/episode where police/detective/bad guy/alien manages to get a blurry low res photo of a vehicle? And then they enhance the picture to get a readable vehicle’s license plate? Like, for example the picture below:IMG_0081_o

The enhancement in the movie is done by a single click and (through a nice video animation) they get the result below (intentionally hidden two letters):

 

IMG_0081

Notice how much readable and sharper is the second plate?

Is this kind of enhancement really possible? No. You just can’t recreate the information that isn’t available in the source. It can go the other way round but not the way as shown by Hollywood movie makers. In the reality they’d produce a interpolated version of original picture, like the zoomed region in first picture. Can you read that? Nope.

So, has been Hollywood lying to us all the time and movies aren’t real? Perhaps not. I think that reading a blurry low res the license plate is still possible but not as easily as shown in the movies. Most probably there is often enough information there to recreate the letters even when human can’t read them. With proper software you can really read those letters I guess – it still depends on the quality of the photo.

Tags:

Graphics

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS