Basic Direct2D drawing with SharpDX

If you are an experienced DirectX programmer chances are you used the DirectDraw API for 2D drawing. If not, here’s a quick summary: it was used for quickly copying parts of one image to another using binary operations (“bit blitting”). This was a quick and good enough method for paletted or low BPP images, but if you wanted to do more advanced operations like rotation or alpha blending you had to implement your own software solution. With the advent of 3D graphics cards, hardware 2D support was phased out in favor of 3D acceleration, and this set the demise of DirectDraw.

Many years later, Direct2D was born to provide true 2D graphics to the DirectX API, instead of drawing 3D shapes using an orthographic projection. It features GPU accelerated raster and vector graphics support, and an immediate mode with a syntax similar to XNA’s SpriteBatch.

In this sample we will add Direct2D drawing to the basic Direct3D app created in the previous tutorial, so get the source code from the repository in case you don’t have it. First we must add the DeviceCreationFlags.BgraSupport parameter to the creation of our Direct3D11.Device, since it’s the color format that Direct2D works with. The next step is to create the default Direct2D device from the existing DXGI.Device2 we initialized previously and use it to generate a Direct2D1.DeviceContext instance:

Next up we must create a Bitmap1 object that will hold a reference to the backbuffer so Direct2D has a surface to draw on; to do this we have to specify its properties using a BitmapProperties1 declaration:

This way we are going to create a bitmap in 32-bit BGRA format (that’s why we previously changed the DeviceCreationFlags!) with premultiplied alpha; BitmapOptions.Target allows the bitmap to be used as a device context target and BitmapOptions.CannotDraw forgives from using it when creating a brush or other drawing resource.

Next up we get the surface of the swap chain that hold the backbuffer and create the Bitmap1 using it as its source:

Bitmap1 creation

Surface backBuffer = swapChain.GetBackBuffer<Surface>(0);

d2dTarget = newBitmap1(d2dContext, backBuffer, properties);

And it’s ready to use it as our drawing target. Simply assign it to the Target property of the Direct2D1.DeviceContext you created previously and start drawing whatever your want to it.

To draw anything, we have to specify a brush that holds the properties of the stroke used to fill the figure. In this tutorial we use the three basic brushes:

SolidColorBrush: just a solid, plain color.

LinearGradientBrush: a linear gradient that interpolates between the GradientStop specified in a GradientStopCollection. We use a 2-stop gradient.

RadialGradientBrush: another gradient brush, but this time in a radial distribution. You can specify its RadiusX and RadiusY properties independently to get a circle or an ellipse.

Below is the code for drawing, where we set the drawing target and tell the context to start drawing. After clearing it with a well-known color, we fill different geometric shapes with the brushes previously created. EndDraw tells Direct2D to flush the buffer and raster as needed.

Problem: I’ve downloaded the latest version of SharpDX and SharpDX.Direct2D1 doesn’t contain a class “Device”.
Question: In many places the is an argument “Intptr nativePointer” — what should be put in there?
I try to make a hardware-accelerated control for Windows Forms

Hello Jace, I think SharpDX only supports Direct2D 1.1 – this means that you need to use the assemblies for DirectX 11.1 and upwards, so check that you are using the correct ones (and you are using Windows 8 or Windows 7 with SP1). The best way to do it is by installing it from the NuGet packages, since the install script does everything automatically.

About the IntPtr argument in the constructors, you shouldn’t be using those overrides unless you really know what you are doing; they are used internally by SharpDX to create managed objects from the native DirectX pointers.

As for the Windows Forms controls, there is no need to do it from scratch! SharpDX has both a RenderControl and a RenderForm inside the SharpDX.Windows namespace. Good luck!