1

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#.

Curyous
  • 8,716
  • 15
  • 58
  • 83
  • Why C or ObjC? It seems to me that if both OO features and performance are important, and you want a C-family language, I'd go with C++. – zneak Oct 04 '10 at 05:44
  • You should profile your containers with realistic sizes. I doubt Objective C is much slower than straight C, but I could be wrong, and you won't know until you measure it. – xscott Oct 04 '10 at 05:49
  • @xscott: It can be. The overhead of sending a message as opposed to calling a function is significant if you spend a lot of time sending messages. But for any particular application, as you say, you won't know until you profile it. – JeremyP Oct 04 '10 at 08:23
  • The hardware is gonna play a huge part in this endeavour. You are much more likely to have success ont the iPad and iPhone4(iPod 4G) as the graphics processing is much better. I think Jerry Coffin is probably right OpenCL will provide you with closer to the speed you are after, however I think 30 fps for realtime raytracing will be a difficult goal on iOS devices thus far. – Kyle Oct 04 '10 at 12:56
  • I didn't realise OpenCL was available on iOS, thanks. – Curyous Oct 04 '10 at 18:00
  • @Alexander Rafferty: Real time ray tracing has been done on commodity machine in the 90s already using many hacks, in the soon 2000s clean real time ray tracers have been written, without hacks (I've done it myself several times). For official proof, look up Ingo Wald's thesis. What _exactly_ was your point? – Sebastian Mach Dec 19 '11 at 16:20
  • My point was that, typically, ray tracing is a process for generating high quality images or movies with effects like shadows and reflections. It was never intended to be anything close to a real time process. And if real-time ray tracing is achieved on a mobile device, I can't see why its quality would justify the means. – Alexander Rafferty Jan 18 '12 at 14:40

4 Answers4

1

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • @Ohmu: no -- OpenCL was what I had in mind. Here we're talking about doing the computation, not just display. While you certainly *can* do computation in shaders, if your goal is purely comptutation, OpenCL is generally a cleaner route toward that goal. – Jerry Coffin Jun 25 '11 at 14:17
  • I don't think iOS supports OpenCL yet... http://stackoverflow.com/questions/3258257/are-either-the-ipad-or-iphone-capable-of-opencl – P i Jun 25 '11 at 15:25
0

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 ;)

justin
  • 104,054
  • 14
  • 179
  • 226
0

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).

Quonux
  • 2,975
  • 1
  • 24
  • 32
0

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.

P i
  • 29,020
  • 36
  • 159
  • 267