create a direct3d 11 device in csharp

To create a Direct3D 11 device in C#, you can use the following code:

main.cs
using SharpDX.Direct3D11;

Device device;
DeviceCreationFlags flags = DeviceCreationFlags.None;
#if DEBUG
    flags |= DeviceCreationFlags.Debug;
#endif

var featureLevels = new[]
{
    FeatureLevel.Level_11_1,
    FeatureLevel.Level_11_0,
};

Device.CreateDevice(null, DriverType.Hardware, flags, featureLevels, out device);
326 chars
16 lines

This code creates a Device object, which represents the Direct3D 11 device. The DeviceCreationFlags parameter specifies any special flags to use when creating the device. In this case, we're adding the Debug flag if we're running in a debug build, which enables additional error and warning messages.

The featureLevels parameter specifies the Direct3D feature levels to try when creating the device. The levels are attempted in order, with the highest level first. Level_11_1 is the highest feature level supported by Direct3D 11, and Level_11_0 is the next highest.

The CreateDevice method creates the Device object, and returns it in the out parameter. If the method is successful, device will contain a valid Direct3D 11 device that you can use to render graphics.

gistlibby LogSnag