This post provides a DirectX 10 sample app that simply displays a square with a photo on it. (a quad with a texture map).
It uses C# and the SlimDX which is an excellent and lightweight way to use Direct X from .NET.
Although there is already a SlimDX sample to display a colored triangle, this sample demonstrates a few additional points:
- How to apply a texture (no it's not obvious at all!)
- How to use a quad instead of a triangle (again not obvious)
- Data is initialized using collections. This allows the geometry count to be defined in 0 places instead of 4 (it's implied by the collection count) which is less error prone when making data changes.
- Collections also make the code more readable because all data types such as Points can be declared as isolated groups rather than all types mixed together in one big array.
Visual Studio 2008 Sample Project:
Download SimpleTexturedQuad
I encourage anyone new to DirectX to learn version 10 because it's the foundation for both Vista and Windows 7, and significantly different than version 9 which you might be able to avoid altogether and save a lot of time.
Also a strong recommendation to use SlimDX and to not use C++ for DirectX if you have a choice. Even if you know C++ well it's just a huge drop in efficiency compared to a more modern language and environment like C#. In most cases the performance difference is very small.
"Also a strong recommendation to use SlimDX and to not use C++ for DirectX if you have a choice. Even if you know C++ well it's just a huge drop in efficiency compared to a more modern language and environment like C#. In most cases the performance difference is very small."
You are very wrong... If you can't program in c++, don't make such comments... sigh...
Posted by: Pal Mezei | March 26, 2009 at 10:30 AM
Hi Pal,
I welcome debate on this blog, but I don't see any support for your claims.
1) Your assumption that I have not used C++ is false. In fact I've been a developer on large C++ projects you would know by name, including one of the larger 3d content creation packages.
2) You say the C# performance penalty is not very small in most cases. What tests have you done to show this? What tests can you cite by others that show this? The results I've seen show an average difference of < 5%.
Of course there are edge cases on both sides:
- If your game/app is shader intensive you might see no significant difference at all.
- If you try to code CPU intensive areas in C# like heavy physics simulations you could see a big difference - but I would say in turn that general case functionality that needs maximum optimization might be best used as a library like Havok Cloth or other middleware specializations.
So I'll grant you there is an "it depends" in there, but I don't know of any data that in general invalidates my original comments.
Posted by: staff | March 26, 2009 at 12:44 PM
Hi!
What are those projects? I don't see any of them referenced in your blog... I'm working in the vfx / cg industry, so I'll probably know those programs.
My support? A haven't said anything about performance, only the sentence, that it's a huge efficiency drop. If you know c++ well, there's nothing against using it. So that means you don't know it well.
Posted by: Pal Mezei | March 28, 2009 at 03:50 AM
My resume is online elsewhere but I don't think it will help resolve this discussion :).
Can you point to any examples of C# showing a "huge efficiency drop" compared to C++?
Its easy to show the opposite: Right now I'm porting a simple ray tracing demo to C# with speed within 5% of C++.
Would seeing this convince you?
If not, what test case would convince you that in the average scenario there is not a huge efficiency drop?
Posted by: staff | March 28, 2009 at 09:33 AM
Wait, I think you don't get it. I haven't stated, that c# is a huge efficiency drop. You said, c++ is a huge efficiency drop.
" Even if you know C++ well it's just a huge drop in efficiency compared to a more modern language and environment like C#."
Simple raytracer? Everyone can write a simple raytracer, and can make it reasonably fast. I'm working on a production quality raytracer (to be more precisely a raycaster) right now as part of my daily work, and it's has nothing common with a simple raytracer.
But that's not my problem. C# is fun, and easy to use, but if you know c++ well, and you are experienced, it's the way to go, especially with real time, or non real time graphics. All I have stated, that using c++ is not a huge efficiency drop (as you stated). That's all.
(sorry for my bad grammar, but English is my third language)
Posted by: Pal Mezei | March 28, 2009 at 09:43 AM
Ok, I misunderstood.
Let's consider the point of C++ being less productive than C# for developers (given equal skills).
I will still propose we cite real data here rather than conjecture. For example this applies to C# as well as Java:
"it is generally conceded that a Java program takes less time to
develop than a C++ program of roughly the same length because Java
is strongly typed and garbage collected"
From this paper:
http://www.hpl.hp.com/personal/Robert_Schreiber/papers/2004%20Software%20Productivity%20Metrics/Final%20Version/HPCS%20Final%20Submission%20Directory/HPCSProductivityMetricsFinal.pdf
There is other research agreeing with this.
Is there any research OR data OR non-anecdotal real-life comparisons supporting your point?
Posted by: staff | March 28, 2009 at 09:51 AM
I agree with Lee, looking at directx c++ code makes me want to vomit.
Posted by: Roman Payne | October 24, 2009 at 10:27 AM
Keep in mind that running a C++ project nowadays is very risky for small to mid sized business.
C++ is good for:
- systems development (the "Trust the programmer" principle in all it's glory! Where every bit in memory management matters! respect...)
- supporting legacy projects (10+ years of serious work)
- be 100% depended by 3rd party vendors or SDKs (Game consoles need C++, period)
- security (maybe the false impression of secure and god-like perfect software)
But supporting C++ for any other non-practical way (as I mentioned before) simply putting it's "efficient" or it's "faster" it immature.
This guys created an amazing demo in just a few days (imagine what could have happened in 1 year...)
http://www.youtube.com/watch?v=TgChURF5fQE
Posted by: Bombaman | November 26, 2009 at 12:07 PM
"it is generally conceded that a Java program takes less time to
develop than a C++ program of roughly the same length because Java
is strongly typed and garbage collected"
If you are going to take a quote do not just take the part that makes your argument yet put it into context. The full quote ends with "...,so there are fewer opportunities to introduce errors." So you are using a quote that says because there is a possibility of making less mistakes it is more productive. How about writing unit tests and there is no possibility of producing errors in tested code.
No wonder the slimdx team did not want this example due to the baggage the example comes with.
Posted by: WhoAreYou | December 07, 2009 at 07:06 AM
Hi,
From my tests (in both languages) and based on my little game engine I've built in these months with SlimDX, I actually think the difference between C++ & C# (speed-wise) in gaming would be evident or USEFUL only on AAA games (based on the assumption that the two programmer have roughly the SAME knowledge of their domain).
In the other situations I prefer and SUGGEST to use C# as language. Its base library (Boost is still far from it) and language by itself (LINQ support, Lambda expressions) allows a faster development cycle. (Very important for indie or little-company games)
Please anyway note that if you're going to get C++ like performance (close the gap between the two in some particular situation where the differences are more evident) sometimes you will have to compromise and use some PInvoke like memcpy or use unsafe code. :D
Just my two cents.
Posted by: C13 | December 07, 2009 at 10:02 AM
@WhoAreYou:
1) The SlimDX guys said they did not reject this sample, they just didn't have time to setup any user sample area on the site, so you're wrong on this point.
2) Second I don't see how the part of the research I left out of the quote is misleading.
3) Using C++ unit tests does not "catch up" the productivity of C++ to C#. Unit tests can be a boost to both languages, so essentially that point is a wash.
Posted by: staff | December 08, 2009 at 09:02 AM
1) I was only replying to a statement you made
2) It is misleading as that is the point of the argument
3) Yes, which reduces the"... opportunities to introduce errors." therefore makes the comment mute.
Posted by: WhoAreYou | December 08, 2009 at 10:04 AM
Hi Dylan Muller here
why not check out directx 11
DirectX 11, the next generation arrives with Windows 7. Windows games create more immersive and detailed worlds and experiences.
DirectX 11 features include:
Tessellation – Tessellation is implemented on the GPU to calculate a smoother curved surface resulting in more graphically detailed images, including more lifelike characters in the gaming worlds that you explore.
DirectCompute – Developers can utilize the power of discrete graphics cards to accelerate both gaming and non-gaming applications. This improves graphics, while also enabling players to accelerate everyday tasks, like video editing, on their Windows 7 PC.
Posted by: Dylan Muller - Software engineering (SE) | September 15, 2010 at 10:05 PM