This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Is using SharpDX or SlimDX easier than using C++ DirectX?

By: xoofx on Jan 29th, 2012  |  syntax: None  |  size: 4.71 KB  |  views: 862  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. This is a response to the question "Is using SharpDX or SlimDX easier than using DirectX directly from C++?" posted on gamestackexchange (
  2. _____________
  4. In short, It is much easier if you don't know anything about C++ but still requires almost the same API knowledge in order to fully understand and manage DirectX correctly under C#. Even if tutorials would help a little to bootstrap some users using C# with SlimDX/SharpDX, they would not be enough to cover all the subtleties of the whole DirectX API.
  6. As said before, in the end, they are just thin wrappers for programmers that want to leverage on this kind granularity while still being able to play with their favorite C# language. When there is a question about DirectX API, I refer to MSDN C++ DirectX documentation or to a C++ DirectX book, and the translation is almost direct into those wrappers. For example, most of the SharpDX Direct2D samples were directly ported from their C++ counterpart from the C++ DirectX SDK without any particular difficulties.
  8. But more specifically, what does bring C# and a managed DirectX wrapper against a plain C++/DirectX approach?
  10.  - **All methods in managed wrapper are checking the HRESULT for you by throwing an exception when an unexpected error occurs**. If you want to achieve the same level of quality in your C++ code, you will have to wrap every function/method call that return a HRESULT by a small function (often defined as a macro). Lets take a simple example with ID3D11Device::CreateTexture2D
  12. in C++:
  15.     D3D11_TEXTURE2D_DESC desc = {
  16.                 1024, // Width
  17.                 1024, // Height
  18.                 1,        // MipLevels;
  19.                 1,        // ArraySize;
  20.                 DXGI_FORMAT_R8G8B8A8_UNORM,      // Format;
  21.                 {1, 0}, // SampleDesc;
  22.                 D3D11_USAGE_DEFAULT, // Usage;
  23.                 D3D11_BIND_RENDER_TARGET, // UINT BindFlags;
  24.                 0,              // UINT CPUAccessFlags;
  25.                 0,              // UINT MiscFlags;
  26.         };
  28.     ID3D11Texture2D* texture2D;
  30.     CHECKDX(d3d11device->CreateTexture2D(&desc, NULL, &texture2D));
  32. in C#: 
  34.         var texture2D = new Texture2D(device, new Texture2DDescription() {
  35.                 Width = 1024,
  36.                 Height = 1024,
  37.                 MipLevels = 1,
  38.                 ArraySize = 1,
  39.                 Format = Format.R8G8B8A8_UNorm,
  40.                 SampleDescription = new SampleDescription(1, 0),
  41.                 Usage = ResourceUsage.Default, 
  42.                 BindFlags = BindFlags.None,
  43.                 OptionFlags = ResourceOptionFlags.None,
  44.                 CpuAccessFlags = CpuAccessFlags.None,
  45.         };
  47. - Lots of enums in C++ are declared as macros or used as plain integer. If you take the previous example, BindFlags, CPUAccessFlags and MiscFlags fields are declared as UINT in the D3D11_TEXTURE2D_DESC, meaning that it is not possible from the struct to know which values are allowed. **In C# SlimDX/SharpDX, all enums are fully declared and it saves you the time to discover what the values could be**. There are plenty of places like this in C++, where sometimes you don't have any enums and you have to go through the headers or browse msdn documentation.
  48. - **Also, lots of small additional methods are available from managed DirectX wrappers to simplify your life**. Some of the managed wrapper (Like DirectSound/DirectInput) are even much more elaborate and are adding a substantial amount of layer to make things easiers. For example, some methods in plain C++ DirectX requires that you call them twice, a first time to get the length of the data to receive, in order to know how much to allocate, a second time to pass your allocated buffer: this kind of laborious workflow are handled by managed wrappers.
  49. - I won't go into all the benefits of using C#/.Net against a C++ (you should know it, but if you don't know C++, you don't know the mess it CAN be), but in  short, you have: a great framework (.NET) with almost all you need for collections handling, network, multithreading, await/async...etc, fast compilation round-trip reducing test-and-run development phases, a lightweight template solution called generics in C# (have you ever faced a template error at linking time in C++? It is sometimes just deadly awful), correct and accurate auto-completion (that is sometimes not working at all for C++, because of the complexity of parsing the language itself), running in x86 or x64 without having to recompile your application... etc.
  51. **So managed DirectX wrappers are thin wrappers that makes your life easier, but they require you to at least dig into the C++ DirectX documentation/books in order get the best from them and understand what's happening**. It means, that if you are not familiar with C++, you just have to understand the C++ API which is a lot easier than having to deal with all the C++ language/compilation infrastructure black hole.
clone this paste RAW Paste Data