SharpDX not similar to D11/c++?

Started by
1 comment, last by Spazzarama 10 years ago

Hello

I just read a Dx11 Book to understand the API, i would translate it to C#/SharpDX.

But the Book describes the way to create a VertexBuffer like:

first the Description:


desc.ByteWidth = size; 
desc.MiscFlags = 0; 
desc.StructureByteStride = 0; 

So in SharpDX i don't have MiscFlags and ByteWidth ....

next, create the Buffer:


HRESULT hr = g_pDevice->CreateBuffer( &desc, pData, &pBuffer ); 

in SharpDX MiniCube Demo the way to create a Buffer:


var vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[]{......});

Here is a difference with C++D3D and Sharpdx..

Or to create DeviceContext/SwapChain.

C++:


D3DllCreateDeviceAndSwapChain

and SharpDX:


Device.CreateWithSwapChain

I thought SharpDX and C++ D3D are similar, why are the difference?

So now my question is: When i reading this book how i find die right SharpDX Commands to recreate in SharpDX?

Sorry for my english i hope you understand what i mean and can give me a answer..

alex

Advertisement
  • Google 'SharpDX' combined with the native name. First hit for 'sharpdx D3D11_BUFFER_DESC' leads me to the doc page. The C# and native identifiers are listed in table form.
  • Design follows C# idioms and what the language can resp. can't do compared to C++. E.g. free functions end up as public static functions tied to the class making the most sense. The samples you gave are constructors in nature, and since C# constructor cannot be named arbitrarily they end up as ... public static. You will learn where to look as you're learning the DX API.
  • The native memory/pointer related stuff ends up as C# arrays, usually. Though you could go raw pointers in C#, too, I don't recommend doing so. Get familiar with the DataStream class and the magic of generics. SharpDX has many (generic) convenience functions anyway.
  • Intellisense (if you're using VS) is also nice to find stuff quickly.
By the way: Which Book ? Luna's ?

SharpDX does reorganise the API a little, but it is done consistently and it is done so that it makes more sense to how you would do things in C#. You do need to be careful that objects are being disposed of correctly, e.g. if you frequently set the rasterizer state (which is set using a property) without disposing of the previous you will introduce memory leaks.

I've just had a book published on using SharpDX with Direct3D - Direct3D Rendering Cookbook, the first chapter outlines some of the mappings between the native and managed API. Amazon provides a preview of some of this chapter that you may find useful.

The second chapter "Rendering with Direct3D" provides examples of how to create vertex buffers, here is an excerpt of the code used for a simple triangle renderer:


            // Create a triangle
            triangleVertices = ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, new[] {
            /*  Vertex Position                       Vertex Color */
                new Vector4(0.0f, 0.0f, 0.5f, 1.0f),  new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Base-right
                new Vector4(-0.5f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Base-left
                new Vector4(-0.25f, 1f, 0.25f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // Apex
            }));
            triangleBinding = new VertexBufferBinding(triangleVertices, Utilities.SizeOf<Vector4>() * 2, 0);

Cheers,

J

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#)

@spazzarama

 

This topic is closed to new replies.

Advertisement