Attack of the Triangles

iOS 8 is finally out and it comes with a lot of new features. And there’s one in particular that caught my attention since it was announced back in the WWDC earlier this year: Metal. Designed specifically for the A7 chip (and newer ones), Metal is Apple’s new graphics API that claims to reduce the overhead between the GPU and the software running on it.

I managed to get some free time during the past couple of weeks (meaning that I’m sleeping less than usual) and I used it to figure out a way to integrate Metal with Crimild. As you might have guest, I succeeded.

The first (and probably most difficult) step was the obvious one: clear the screen to something other than black and render a triangle on it. Since Crimild always made use OpenGL (and it’s relatives) for rendering purposes, some of its subsystems were designed based on how that particular API works. Therefore, I had to be creative in order to translate some of the major concepts of Metal into something that Crimild understands. Fortunately (or maybe not), Metal is not that different from OpenGL in terms of the requirements of a rendering pipeline. We still need buffers, samplers, shaders and transformations in order to display our scenes.

After several failures attempts, I finally managed to visualize an old friend of mine in the screen of my phone:

Crimild + Metal
Crimild + Metal

Indeed, that was the most difficult part. Once the colored triangle was visible and I understood how the new shading language worked, it wasn’t hard to apply transformations and implement a simple simulation loop:

Isn’t he beautiful? Wait. What’s the point of a Metal demo that displays only one triangle? After all, Metal is supposed to give us a lot more draw calls than OpenGL ES, right? And then, our little triangle got some friends and initiated the invasion of… Oh, you’re not reading anymore and went straight for the second video. OK, that’s fine. I’ll wait.

The demo displays nearly 200 triangles in the least efficient way just to demonstrate that Metal can handle it. Truth is, I started to notice some frame drops here and there, but I can’t tell for sure if it’s GPU or CPU related (I guess it’s the latest one). And it doesn’t feel quite right yet, since I’m using a lot more C++ than Apple wants (why did they made the API Obj-C based? Why?). I’ll be back to this demo once I have more knowledge about how (and why) everything works.

Finally, I cannot submit this little beast to the App Store, but you can grab the code for the demo at GitHub. Remember this project only works for iOS 8 and A7-based devices.