使用 C++ 有什么优势吗?使用 Direct3D 时而不是 C#?
使用 Direct3D 时使用 C++ 代替 C# 有什么优势吗?我发现的 C++ 和 DirectX 教程都使用 Direct3D(据我所知是托管的)。同样,我找到的所有 C# 教程都是针对 Direct3D 的。
Direct3D 是否受管理? 在这两种语言中使用 D3D 有什么区别吗?
Is there any advantage to using C++ instead of C# when using Direct3D? The tutorials I've found for C++ and DirectX all use Direct3D (which to my knowledge is managed). Similarly, all of the C# tutorials I've found are for Direct3D.
Is Direct3D managed?
Is there any difference between using D3D in either of the two languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DirectX 完全是原生的。您可能认为它已得到管理的任何印象都是完全错误的。有一些托管包装器可以让您从托管代码中使用 DirectX。此外,DirectX 被编程为可以通过 C++ 或 C 或类似语言进行访问。如果您查看 SlimDX 项目,您会发现他们遇到了许多问题,特别是在资源收集方面,因为 C# 并不真正支持自动收集非内存资源,并且
using
并不能满足要求。此外,游戏编程可能非常消耗 CPU 资源,而且通常,使用托管语言所损失的额外性能是站不住脚的,而且几乎所有现有的支持库都是针对 C 或 C++ 的。如果您想制作一个小游戏或类似的东西,没有什么可以阻止您使用托管代码。然而,据我所知,还没有商业游戏真正走这条路。
DirectX is entirely native. Any impression you may have that it's managed is completely and utterly wrong. There are managed wrappers that will allow you to use DirectX from managed code. In addition, DirectX is programmed to be accessed from C++ or C, or similar languages. If you look at the SlimDX project, they encountered numerous issues, especially due to resource collection, because C# doesn't genuinely support non-memory resources being automatically collected and
using
doesn't cut the mustard. In addition, game programming can be very CPU-intensive, and often, the additional performance lost by using a managed language is untenable, and virtually all existing supporting libraries are for C or C++.If you want to make a small game, or something like that, there's nothing at all stopping you from using managed code. However, I know of no commercial games that actually take this route.
Direct3D 的重点是将渲染从 CPU 转移到 GPU 上。如果存在显着的性能差异,那就是在 CPU 上运行的代码。因此,对于与 Direct3D 交互的代码部分,我认为本机代码和托管代码之间不应该存在任何显着的性能差异。
Direct3D 本身不是托管代码。
The point of Direct3D is to move rendering off the CPU and onto the GPU. If there were to be a significant performance difference it would be for that code that runs on the CPU. Therefore I don't see that there should be any significant performance difference between native and managed code for the part of your code that interfaces with Direct3D.
Direct3D itself is not managed code.
这取决于你具体在做什么。正如 David Heffernan 提到的,Direct3D 的目标之一是将尽可能多的处理转移到 GPU。随着顶点着色器、像素着色器等的出现,我们比以往任何时候都更接近现实。
当然,如果有无限的时间和资源,您通常可以用 C++ 创建比 C# 更高效的算法。这将影响 CPU 级别的性能。如今,与图形无关的处理仍然主要在 CPU 上完成。诸如 CUDA、OpenCL 甚至未来版本的 DirectX 之类的东西也将开启将任何并行友好算法转移到 GPU 的可能性。但这些技术(以及支持它的显卡)的采用率尚未完全成为主流。
那么您应该考虑使用 C++ 来实现哪些类型的 CPU 密集型算法呢?
这些只是我首先想到的。至少前两种在当今的游戏中很常见。在游戏中,人工智能通常以一种折衷的方式完成,以尽可能快地运行,仅仅是因为它可能需要大量处理器。然后粒子引擎无处不在。
It depends on what you're doing exactly. As David Heffernan mentioned, one of the objectives of Direct3D is to move as much processing as possible to the GPU. With the advent of vertex shaders, pixel shaders, and much more, we're closer to that reality than ever.
Of course given infinite time and resources, you can usually create more efficient algorithms in C++ than C#. This will affect performance at the CPU level. Today, processing that is not graphics related is still mostly done on the CPU. There are things like CUDA, OpenCL, and even future versions of DirectX which will open up possibilities of moving any parallel-friendly algorithm to the GPU as well. But the adoption rate of those technologies (and the video cards that support it) isn't exactly mainstream just yet.
So what types of CPU-intensive algorithms should you consider C++ for?
Those are just the first things I can think of. At least the first two are very common in games today. AI is often done in a compromised fashion in games to run as quickly as possible, simply because it can be so processor intensive. And then particle engines are everywhere.