DirectX or OpenGL [closed]
Asked Answered
K

15

10

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.

Kinnard answered 2/2, 2009 at 20:31 Comment(0)
N
9

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.

Nansen answered 17/4, 2009 at 2:30 Comment(1)
Hi, thanks for the reply. It's great that you wrote slimdx. Thanks for that.Kinnard
D
25

I'd recommend OpenGL for the following reasons :-

  1. Cross Platform - OpenGLES being of particular relevance these days for Mobile platforms
  2. The OpenGL shading language implementation is inherently superior to DirectX shaders.
  3. OpenGL is somewhat easier to learn as it's possible to set up basic renders with very few lines of code
  4. 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.
  5. 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.

Drobman answered 2/2, 2009 at 21:2 Comment(7)
1. OpenGL ES isn't important here, as there's no way to run .NET code on any popular OGLES platform. 2. With the technique model used in HLSL, I see nothing superior about GLSL -- mind backing that up? 3. OGL is much harder to get started with in the .NET space. Look at XNA. [...]Kingkingbird
4. There's nothing about Direct3D that's games-oriented any more than OGL. Yes, DX has more to it, but there's no reason you have to use any of it. 5. Yes, MS could deprecate it all, but this applies to anything proprietary. Are you really suggesting MS is going to kill DX?Kingkingbird
1. Not yet - and being able to port your knowledge is advantageous generally 2. In OpenGL the GLSL compiler is part of the video driver, whereas in DirectX the HLSL compiler is part of DirectX. Theoretically performance is superior with GLSLDrobman
3. Maybe not directly in Net, but the availability of any number of toy GLUT based systems to test rendering techniques and ideas in is and advantage. 4. True less of an issue than it was, but historically DirectX was a totally games orientated system which has since been deployed elsewhere...Drobman
...and OpenGL the reverse (Doom being it's breakthrough title into game rendering). Not a big issue this any longer I give you. 5. I simply point to the long trail of discarded MS technologies. Wouldn't be the first time :-)Drobman
would you care to elaborate why GLSL is inherently superior to Cg (which, effectively, is the same as HLSL)?Amphidiploid
Because theoretically the later compilation of GLSL by the video-drive means it can make optimal use of the card's hardware, whereas HLSL being compiler earlier must use a standard machine model.Drobman
H
12

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.

Hepsibah answered 2/2, 2009 at 20:40 Comment(4)
Is this still valid? DX always seem to win out on my machine...Teakettle
Overall yes, however for particular subsets of each API for a particular graphics card & driver combo there may be significant differences. Since the original question was so open, I felt no need to obfuscate the overall picture with such details.Hepsibah
Stability might be a difference. The OpenGL drivers seems to be less stable on windows machines.Fiance
That's just not true and it never was. If you write a perfectly optimized render scene in OpenGL and another one optimized for DirectX using at least Ogl3 features you will see that OpenGL is roughly 20% more fast than Dx. I know because I did this myself. It's just that game developers are being paid now to make their applications run better on Dx or on Ati/Nvidia sometimes even crippling the other one. I know this for a fact because I work in this industry.Mufinella
N
9

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.

Nansen answered 17/4, 2009 at 2:30 Comment(1)
Hi, thanks for the reply. It's great that you wrote slimdx. Thanks for that.Kinnard
M
7

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.

Montpelier answered 2/2, 2009 at 20:42 Comment(2)
Thanks, what precautions do you mean?Kinnard
That is, using platform-independent features and code. Direct3D is an example of non-portable application of C#/.NET.Montpelier
S
4

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

Shockheaded answered 5/6, 2009 at 14:5 Comment(0)
L
2

If you don't like XNA, you can use SlimDX. Unlike XNA, SlimDX supports DirectX 10.

Liquefacient answered 2/2, 2009 at 21:15 Comment(0)
H
2

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.

Hawkins answered 2/2, 2009 at 21:19 Comment(1)
GLUT is a dinosaur and has a non-free license, GLFW is much better.Gonium
P
2

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.

Pinnate answered 2/2, 2009 at 21:22 Comment(0)
K
1

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.

Kingkingbird answered 2/2, 2009 at 20:36 Comment(0)
G
1

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.

Generatrix answered 2/2, 2009 at 20:58 Comment(0)
J
1

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

Josephinajosephine answered 2/2, 2009 at 21:24 Comment(0)
H
1

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.

Hardden answered 17/4, 2009 at 2:37 Comment(0)
R
1

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.

Reciprocity answered 17/4, 2009 at 2:44 Comment(0)
S
1

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.

Siam answered 22/9, 2011 at 15:32 Comment(0)
S
0

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.

Shaina answered 2/2, 2009 at 20:35 Comment(2)
Managed DirectX is deprecated in favor of XNA, and XNA is a totally and completely different API.Kingkingbird
The current alternative to MDX is SlimDX not XNA, but there are also managed wrappers for OpenGL, i.e. SharpGLJosephinajosephine

© 2022 - 2024 — McMap. All rights reserved.