iOS 上的实时光线追踪器使用 C 或 ObjC?
我开始为 iOS 构建一个实时光线追踪器。我对光线追踪这件事很陌生,到目前为止我所做的只是在 ObjC 中编写一个基本的。在我看来,基于 C 的光线追踪器会比用 ObjC 编写的光线追踪器更快,但 ObjC 的光线追踪器会简单得多,因为对象层次结构非常方便。不过,速度非常重要,因为我希望它是实时的,比如 30 fps。
您对 C 语言的加速是否值得增加额外的复杂性有何看法?我可以预见 C 代码会花费更长的时间,并因大量错误而让我头疼(尽管我对 C 并不陌生),但最初追求更快的速度是很诱人的。
有没有用 C 语言编写的光线追踪器的例子?我对此类内容的 google 搜索被大量 C++ 和 C# 的结果污染了。
I'm starting to build a real-time raytracer for iOS. I'm new to this raytracing thing, all I've done so far is write a rudimentary one in ObjC. It seems to me that a C-based raytracer is going to be faster than one written in ObjC, but the ObjC one will be far simpler, as object hierarchies come in very handy. Speed is very important, though, as I want it to be real-time, say 30 fps.
What's your opinion on whether the speed up of C be worth the extra complexity? I can forsee the C code taking much longer and causing me headaches with lots of bugs (although I'm not new to C), but going for more speed is seductive initially.
Are there any examples out there of raytracers written in C? My google search for such things is contaminated with lots of results for C++ and C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要快速光线追踪,您几乎可以忘记使用 C 或 Objective C。您几乎肯定想使用 OpenCL。它仍然不足以让你(甚至非常接近)30 fps,但它可能至少是 CPU 上运行的任何东西的两倍(并且快 5-10 倍也不会是什么真正的惊喜) )。
If you want fast ray tracing, you can pretty much forget about using either C or Objective C. You almost certainly want to use OpenCL. It's still not going to be enough to get you (even very close to) 30 fps, but it'll probably be at least twice as fast as anything running on the CPU (and 5-10 times faster wouldn't be any real surprise).
正如 zneak 所说,C++ 是速度和多态性的最佳组合。
但是,您可以通过减少 objc 调用来完成一些接近的任务(阅读:将多态接口减少到所需的最小集,然后将需要快速的部分放入纯 c 或 c++ 中)。
objc 消息调度非常快,并且您通常可以从接口中删除许多虚拟/动态方法(假设每个 objc 实例方法都是虚拟的)。 objc 方法中的 c 代码仍然是 c 代码...从那里确定瓶颈在哪里——在更改工作代码之前进行分析也没有什么坏处;)
as zneak stated, c++ is the best combination for speed and polymorphism.
however, you can accomplish something close by reducing the objc calls (read: reduce the polymorphic interface to the minimum set required, then just putting the parts that need to be fast in plain c or c++).
objc message dispatch is quite fast, and you can typically remove much of the virtual/dynamic methods from your interfaces (assume every objc instance method is virtual). c code in objc methods is still c code... from there, determine where your bottlenecks are -- it doesn't hurt to profile before changing working code, either ;)
编写“实时光线追踪器”不需要使用手工优化的程序集(或使用“便宜的”英特尔编译器;),但这对于这个平台来说是不可能的),因为你需要速度,所以不可能。
此外,您需要大量的处理能力,但我想即使是 OpenCL 路径也不够强大(在我看来,即使对于真正的桌面计算机也是如此,其原因是缺乏真正的大 图形处理器上的缓存)。
Writing a "realtime Raytracer" is without the use of Hand-Optimized Assembly (or the use of the "cheap" Intel compiler ;) , but this is not possible for this platform), impossible because you need the speed.
Further more you need a lot of Processing power but i guess even the OpenCL path is not powerful enought (this is in my opinion the case even for real Desktop machines, the reason for this is the lack of an real big cache on the Graphics Processor).
看看 http://ofps.oreilly.com/titles/9780596804824/ 这是尽可能接近。
这不是光线追踪,我已经编写了光线追踪器,这是一项巨大的工作量。 GL 使用不同的图形技术,因此它无法渲染钻石捕获光线的能力。该链接包含示例代码,您可以下载并运行它。您会意识到,即使是一些中等复杂的示例在实际设备上也确实很出色……我们正在谈论<< 1 帧/秒。
Have a look through http://ofps.oreilly.com/titles/9780596804824/ that is as close as you'll get.
It isn't ray tracing, I have written a ray tracer and it is a huge amount of work. GL uses a different technique for graphics, hence it will be unable for example to render the capacity of a diamond to capture light. that link contains sample code, you can download and run it. You will realise that even some of the moderately complex examples really chug on an actual device... we are talking < 1 fps.