C# DirectX 或 OpenGL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/504754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DirectX or OpenGL
提问by Joan Venge
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?
如果您正在用 C# 编写下一个 3d 图形密集型应用程序(如 3d 建模和动画软件),哪个是更好的选择?
If we consider C# as platform independent, then OpenGL seems tempting, but what about the performance, etc?
如果我们认为 C# 是独立于平台的,那么 OpenGL 似乎很诱人,但是性能等呢?
Since the used language is C#, the performance is pretty crucial to consider.
由于使用的语言是 C#,因此性能是非常重要的考虑因素。
Edit: You can also consider SlimDX and TAO, OpenTK, csGL, etc too.
编辑:您也可以考虑 SlimDX 和 TAO、OpenTK、csGL 等。
采纳答案by Promit
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.
就图形子系统而言,托管代码的性能还不错。在每次调用 DirectX 时,SlimDX 都会对完全本机代码造成轻微的惩罚,但这绝不是严重的。实际损失取决于调用——调用 DrawPrimitive 总体上比调用 SetRenderState 代价高得多,所以按百分比计算,你最终会在 SetRenderState 调用上损失更多。SlimDX 结合了一个经过调整的数学库,它通常表现得非常好,尽管您必须小心使用它。分析,即使使用像 NProf 这样的垃圾工具,也能很快突出显示这些东西,因此不难修复。
Overall, if we consider generic, completely optimalC++ 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.
总的来说,如果我们考虑通过 D3D 进行渲染的通用的、完全优化的C++ 和 C# 代码,C# 版本可能是 C++ 版本的 10-15%。但这很难实现;考虑一下通过在 C# 中工作可以节省多少时间,您可以将这些时间应用于更高级别的图形优化,如果您必须用 C++ 构建整个东西,您可能根本没有时间。即使你设法在 C++ 中获得额外的 10%,它也会在几个月内迅速缩小到 5%,当新一轮的硬件比以往任何时候都更快地撕裂你的应用程序代码时。我知道我会选择什么——这就是我开始编写 SlimDX 的原因。
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.
OpenTK 具有类似的性能特征,但需要注意的是,它们的数学库在某些地方相当缓慢。这是我与他们讨论过的一个实现错误,希望能在不久之后得到修复。
回答by Joel Coehoorn
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.
DirectX 有一个托管 API,它使您可以将 DirectX API 作为“本机”C# 对象访问。这是 OpenGL 的一大优势。
回答by Cody Brocious
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.
我觉得 OpenGL 不像 XNA 那样适合纯 OO 环境。也就是说,如果您真的关心跨平台兼容性,那么您的后端应该无关紧要。
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.
将应用程序的业务逻辑设计为独立于渲染后端。您应该能够插入 OpenGL 渲染对象,然后将其换成 XNA 渲染器没问题。这不仅增加了您的潜在客户群(通过支持两者),而且使您的应用程序的设计更好。
Also a small note, DX shouldn't be used from .NET, as Managed DirectX has been deprecated; use XNA.
还有一点要注意的是,不应从 .NET 使用 DX,因为 Managed DirectX 已被弃用;使用 XNA。
回答by Ronny Vindenes
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.
Direct3D 和 OpenGL 之间的性能差异几乎为零。两者的特征集并没有一一对应,而是接近。OpenGL 的主要优点是跨平台支持。
回答by Eduard - Gabriel Munteanu
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.
OpenGL 过去在性能特性方面落后,但问题最终得到了解决。举个例子,考虑可绑定的制服,在 OpenGL 获得类似的机制之前,Direct3D 有一个更快的机制。除了有时支持不同的功能集之外,没有什么区别。
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.
因此,除非您打算使用最新的 GPU 功能,否则我建议您使用 OpenGL。可以肯定地说,OpenGL 在与性能相关的功能方面落后的领域并不多。
By the way, C# and .NET are platform-independent only if you take certain precautions.
顺便说一下,C# 和 .NET 只有在您采取某些预防措施时才独立于平台。
回答by JSmyth
It depends what platform you intend to target.
这取决于您打算针对哪个平台。
If you only need to target windows use XNA
如果您只需要定位 Windows,请使用 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.
对于跨平台工作,也许其他人已经使用 mono 和 openGL 做了一些工作 - 我假设你打算制作图形软件而不是游戏,所以拥有像 winForms 这样的框架对你所有的人都非常有帮助用户界面控件。
回答by Cruachan
I'd recommend OpenGL for the following reasons :-
我推荐 OpenGL 的原因如下:-
- 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.
- 跨平台 - 现在 OpenGLES 与移动平台特别相关
- OpenGL 着色语言实现在本质上优于 DirectX 着色器。
- OpenGL 更容易学习,因为它可以用很少的代码行设置基本渲染
- 从哲学上讲,OpenGL 被设计为通用渲染引擎,而 DirectX 始终面向游戏,因此 OpenGL 似乎更适合您的问题。
- OpenGL 是一种稳定的技术,已经存在了一段时间,并将继续如此。DirectX 更依赖于微软的心血来潮,如果微软愿意,它可能会立即被弃用。
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.
也就是说,您的系统要求和您的个人偏好可能会以任何一种方式倾斜,因为这两种方法都是可靠的实现。不利的一面是,OpenGL 在很大程度上是一种状态机,很难适应 OO,尽管这当然不是不可能的。
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.
编辑:添加以澄清我对 OpenGL 着色器模型本质上优于 DirectX 的评论。这是因为 DirectX 着色器在开发时针对通用 GPU 模型与程序一起编译,而 OpenGL 着色器作为源代码保存并在运行时由 OpenGL 驱动程序编译。因此,从理论上讲,驱动程序编写者可以利用 GPU 的特定(或在运行旧程序时更新)功能,并创建运行速度比 DirectX 着色器更快的编译代码。这是一个小点,但可能是一个非常重要的点。
回答by Michael Kelley
回答by heeen
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.
使用 OpenGL,您可以使用“DirectX 10 功能”,例如 Windows XP 和 Linux 上的几何着色器。使用 GLUT,在几分钟内启动并运行演示应用程序非常简单。
回答by Jeff Kotula
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.
我使用过 OpenGL 和 DirectX。我认为性能非常相似。我更喜欢 OpenGL 的编程模型——尤其是它对转换的处理,以及对拣选操作的直接支持。我不喜欢 MS 每次升级操作系统时继续重新包装相同功能的方式,我认为 OpenGL 可以保护您免受这种影响。
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.
但是,两者都很古怪,您需要花费大量时间确保它与托管应用程序框架(无论是 Windows 还是其他框架)良好地交互。
回答by Neil N
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)
您可能想要查看 IrrLicht 引擎,它同时具有 C++ 和 .Net API,并且完全与图形 API 无关(意味着您可以使用相同的代码来执行 OpenGL 或 DirectX,并且程序员甚至不必知道您使用的是哪个正在使用)
You might also want to look into SlimDX, a very fast, lightweight, open source alternative to XNA
您可能还想了解 SlimDX,它是 XNA 的一种非常快速、轻量级的开源替代品