问题
If you were writing the next 3d graphics intensive application in C# (like a 3d modelling and animation software), which one would be a better choice?
If we consider C# as platform independent, then OpenGL seems tempting, but what about the performance, etc?
Since the used language is C#, the performance is pretty crucial to consider.
Edit: You can also consider SlimDX and TAO, OpenTK, csGL, etc too.
回答1:
Performance in managed code, with respect to the graphics subsystem, is not bad. SlimDX pays a slightly penalty over completely native code on each call into DirectX, but it is by no means severe. The actual penalty depends on the call -- a call into DrawPrimitive will be vastly more expensive overall than a call to SetRenderState, so percentage-wise you end up losing a lot more on the SetRenderState calls. SlimDX incorporates a tuned math library that generally performs very well, although you have to be a bit careful with it. Profiling, even with a junker tool like NProf, highlights this stuff very quickly so it's not difficult to fix.
Overall, if we consider generic, completely optimal C++ and C# code doing rendering via D3D, the C# version is probably within 10-15% of the C++ version. That's hard to achieve though; consider how much time you're saving by working in C# which you can apply to higher level graphics optimizations that you probably simply wouldn't have time for if you had to build the entire thing in C++. And even if you managed to get that extra 10% in C++, it'd promptly shrink to 5% within a few months, when a new round of hardware tears through your application code faster than ever. I know what I'd pick -- which is why I wrote SlimDX to begin with.
OpenTK is subject to similar performance characteristics, with the caveat that their math library is rather slow in places. This is an implementation bug that I've discussed with them, and will hopefully be fixed before too long.
回答2:
I'd recommend OpenGL for the following reasons :-
- Cross Platform - OpenGLES being of particular relevance these days for Mobile platforms
- The OpenGL shading language implementation is inherently superior to DirectX shaders.
- OpenGL is somewhat easier to learn as it's possible to set up basic renders with very few lines of code
- Philosophically OpenGL was designed as a general purpose rendering engine, whereas DirectX was always games orientated, so OpenGL would seem a better fit for your question.
- OpenGL is stable technology that has been around for some time and will continue to be so. DirectX is more dependent on the whim of Microsoft and could be deprecated in an instant if MS felt like it.
That said, the requirements of your system and your personal preferences could tip it either way as both approaches are solid implementations. On the downside OpenGL is very much a state machine and can be tricky to fit into OO, although it's certainly not impossible.
EDIT: Added to clarify my comment on the OpenGL shader model being inherently superior to DirectX. This is because DirectX shaders are compiled with the program at development time against a generic GPU model, whereas OpenGL shaders are held as source code and compiled by the OpenGL driver at run time. Consequently theoretically it's possible for the driver writer to take advantage of specific (or updated when running an old program) features of the GPU and create compiled code that can run faster than DirectX shaders. It's a small point, but potentially quite an important one.
回答3:
Performance difference between Direct3D and OpenGL is near nil. The feature sets of the two do not map one to one, but they are close. The main pro for OpenGL is cross platform support.
回答4:
OpenGL lagged in the past in relation to performance features, but things got fixed eventually. To give an example, consider bindable uniforms, where Direct3D had a faster mechanism before OpenGL acquired a similar one. Apart from supporting different feature-sets at times, there's no difference.
So unless you intend on working with the very newest GPU features, I'd advise you go with OpenGL. It's safe to say there aren't many areas where OpenGL lags in performance-related features.
By the way, C# and .NET are platform-independent only if you take certain precautions.
回答5:
Here is my honest suggestion for you: Make use of both.
Advice
I would any day weigh which one has more tools available to finishing a program, and then for performances reason, I would make use of the other in order to ensure maximum performance on different systems.
Thoughts on the debate
Let us review: The performance of the program depends on many things, mainly your effort (the application's code) and the drivers on the given computer. Graphics APIs are a mean for your application to communicate to the GPU, and thus makes you incredibly dependent on how well a given driver is implemented for installed GPU.
My choice
Direct3D is sometimes faster on some graphics card than OpenGL, and that is because of graphics vendors and their drivers.
DirectX offers plenty of tools to speed up development. I realize it has a very steep learning curve to begin with, but allow me to remind, that you happen to be a programmer. I even dare to say a very good one.
Conclusion
Therefore you must be able to fight your way in and slowly developing your own framework that utilizes both APIs and thus make you capable of testing and implementing whatever program you have in mind.
Sincerely,
Mossa Nova Merhi
回答6:
If you don't like XNA, you can use SlimDX. Unlike XNA, SlimDX supports DirectX 10.
回答7:
With OpenGL you can use "DirectX 10 features" like geometry shaders on Windows XP and Linux. Using GLUT it is very simple to get a demo application up and running within minutes.
回答8:
I've used both OpenGL and DirectX. I think the performance is pretty similar. I prefer the programming model of OpenGL -- especially its handling of transformations, and direct support of picking operations. I dislike the way MS continues to rewrap the same functionality every time it upgrades the OS, and I think OpenGL protects you from that.
However, both are quirky and you need to spend a good deal of time making sure that it interacts nicely with the hosting application framework, whether Windows or something else.
回答9:
I don't feel that OpenGL fits nearly as well into a pure OO environment as something like XNA does. That said, if you really care about cross-platform compatibility, it shouldn't matter what you backend to.
Design the business logic of your application to be independent from the rendering backend. You should be able to plug in an OpenGL rendering object then swap it out for an XNA renderer no problem. Not only does this increase your potential customer base (by enabling support for both), but makes your application's design far nicer.
Also a small note, DX shouldn't be used from .NET, as Managed DirectX has been deprecated; use XNA.
回答10:
It depends what platform you intend to target.
If you only need to target windows use XNA
For cross-platform work, maybe someone else out there has done some work using mono with openGL - I'm assuming you are intending to make graphics software rather than a game, so having somekind of framework like winForms would be very helpful for all your UI controls.
回答11:
You will probably want to look into the IrrLicht engine, it has both a C++ and .Net API, and it entirely Graphics API agnostic (meaning you can use the same code to execute OpenGL or DirectX and the programmer wont even have to know which you are using)
You might also want to look into SlimDX, a very fast, lightweight, open source alternative to XNA
回答12:
DirectX is going to have better video driver support on Windows since it is what MSFT uses to certify cards. We've found OpenGL support is lacking, crashy or plain wrong for cheaper cards on Windows.
回答13:
You may want to look at this: http://groups.google.com/group/microsoft.public.win32.programmer.directx.managed/browse_thread/thread/1fc097147796e15b
The Managed API isn't being supported now, so OpenGL may be your best bet, unless you want to go with XNA.
Depending on what other platforms you may want to support, such as mobile devices or XBox360 then that may help you decide which api to use.
回答14:
In terms of performance both are practically the same, your code and design will be the mayor factor affecting speed. Additionally, both APIs are well mantained (in comparison to 5 years ago when dx was much ahead).
In this cases, one option is to choose based on your philosophy and the philosophy behind each API.
Some might need a comercial focus; sell software, invert money, do business. Coming from a business enviroment, where money can speed things up, dx might be well suited for this cases where you can get interesting deals with microsoft at capacitations and developing tools, among other things.
On the other hand, OpenGL is an Open Standard, this means that people can discuss/evaluate and contribute to improve the standard API that will be for the benefit of society in the first place guaranteed. This is more compatible with a scientific approach where the goal might be publish a research, release free software, make a comunnity and get feedback from developers around the world, multiplatform.
They are different views of software evolution, i think that both have their strengths, it is good that we still can choose between two APIs that come from very different backgrounds, but do very similar things.
回答15:
There is a managed API for DirectX, which gives you access to the DirectX API as "native" C# objects. That's a huge leg-up over OpenGL.
来源:https://stackoverflow.com/questions/504754/directx-or-opengl