Metal viewport on iOS

Hi,

So I’ve thrown out all sanity and started implementing a Metal renderer for my iOS port. It’s actually going fairly well, I’ve implemented most of the basic renderer structures and have untextured vertices rendering. Performance also seems pretty good.

However I’m having a problem which is a bit beyond my limited grasp of matrix math to solve. OpenGL defines the viewport with (0,0) at bottom-left whereas Metal annoyingly defines it as top-left. Does anyone know the best way to deal with this in the frustum/projection matrix generation code? The code is in the metal-renderer branch on Github. (https://github.com/tokyovigilante/CesiumKit/tree/metal-renderer).

Thanks!

Ryan

After some more debugging, it turns out the problem isn’t quite the viewport but the fact that OpenGL on iOS uses an NDC clip space ranging from [-1 to 1] whereas Metal uses [0 to 1]. This means my globe is being clipped to close. I can fix it by hardcoding the perspective frustum near plane to 0.1, but I’m sure there must be a more robust way to change it when generating the projection matrix.

Ryan

IMG_0071.jpg

One must love the joys of porting code from one framework to the other. It’s always the subtle things like that clip space that catch us by surprise. As for a non-hardcoded solution to the actual problem I think I’ll have to defer to Matt, Patrick or Dan. I’ll ask them tomorrow.

Thanks Mike! I’ve found a fairly coherent discussion of DirectX vs OpenGL projection matrices at http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/perspective-projections-in-lh-and-rh-systems-r3598. DirectX uses a [0 to 1] Z-clip so I should be able to sort it out.

On the plus side hacked textures into the globe yesterday and performance was excellent compared to OpenGL. Even better to see Metal support in OS X in 10.11 today, so hopefully have the renderer up and running on both platforms soon.

Ryan

Awesome I look forward to seeing some demos when it’s ready

Cheers,

-Mike

Hi Ryan,

Let us know if you have any feedback on the Cesium renderer design and how it maps to Metal. As I said before, with the exception of the shading language, I expect it to map pretty well.

Your experience and results of this port would make a great guest blog post on the Cesium website. Let us know if you are interested.

Also, I know you already saw our “Rendering a Frame” article, but we also wrote these if you haven’t seen them: Renderer Architecture and The Graphics Stack.

Patrick

Hi Patrick,

I definitely do have some, overall the clear structure of the renderer has simplified things, there are some differences with Metal defining clear and pre-compiled pipeline states, which do have some overlap with RenderState/PassState. The shaders were actually the easiest part once I got the Unity cross-compiler built as an iOS module.

I’d like to get the Metal render at least up to parity with the existing OpenGL renderer and generalise some pragmatic assumptions I’ve put into the code at the moment. My day job is keeping me fairly busy at the moment but hopefully will have more progress to share towards the end of June/July, and would be happy to contribute something to the blog once it’s a bit more presentable.

I saw the rest of the technical posts thanks, they’re great for a high-level overview and a good cure for code-induced myopia!

Thanks,

Ryan

Ryan,

Good call on the Unity cross-compiler!

Can’t wait to read your tech blog post. Let us know when you are ready to start.

Also, any chance you will be at SIGGRAPH in LA in August? This topic would be of interest to the audience at our annual BOF and I would be happy to reserve you a speaking slot.

Patrick

Thanks, its a very good tool actually, and has great uniform/texture sampler reflection which maps very well to the existing renderer.

Hopefully once I have textures sorted then I’ll distil the changes into something coherent for the blog. I’ll keep you posted.

I’m based in Australia so won’t be able to make it to SIGGRAPH unfortunately, thanks for the invite though! I’ll be passing through LA at the end of July after a holiday on the East Coast but the dates don’t quite match up.

Ryan

All sounds good. We’ll let you know if we are ever in Australia. There’s a good chance some of us will be in Sydney at some point since Kevin, who wrote the Cesium terrain engine, is there.

Patrick

Ryan,

Any progress here? Would love to see this tech blog post.

Patrick

Hi Patrick,

Yes indeed, I’ve written the draft. I’m just tidying the build system a little, then will generate some GL vs Metal benchmarks and come up with some images and it should be done. I’ll email you the draft to have a look if you like.

Regards,

Ryan

Sounds great, thanks Ryan. I’ll get back to you with feedback on the draft soon.

Patrick