Making a 2D game for iPhone/iPad and need better performance?

I’m using Unity and SpriteManager 2 to make a 2D game for the iPhone/iPad. Using the shader included with SM2 and/or the mobile vertex colored shaders included with Unity left me extremely GPU/fillrate bound on the iPad in particular. I was seeing anywhere from 50-90ms per frame for “cpu-waits-gpu” from the internal profiler running on the iPad. I wrote my own shaders and dropped this to an average of 10ms per frame. I’ve still got a few optimizations to go on my game but I figured I’d share the shaders I wrote in case anyone else out there might find them useful.

The best use for these shaders is if you are making a sprite-based game where you layer the sprites and use alpha to create a more traditional 2D looking game. These shaders do not support scene lighting at all but they do support vertex coloring. Basically if you just want your textures to look pixel perfect and unaltered when rendered in game then these might help.

Also, be sure to edit your AppController.mm to disable OpenGL ES 2.0 or your framerate is going to suffer tremendously. Line 70 of your AppController.mm should read:

#define USE_OPENGLES20_IF_AVAILABLE 0

Here is the shader that supports alpha transparency and vertex coloring:

Shader “Unlit Transparent Vertex Colored” {
Properties {
_MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
}

Category {
Tags {“Queue”=“Transparent” “IgnoreProjector”=“True” “RenderType”=“Transparent”}
ZWrite Off
Alphatest Greater 0
Blend SrcAlpha OneMinusSrcAlpha
SubShader {
Pass {
BindChannels {
Bind “Vertex”, vertex
Bind “texcoord”, texcoord
Bind “Color”, color
}

Fog { Mode Off }
Lighting Off
SetTexture [_MainTex] {
Combine texture * primary, texture * primary
}
}
}
}
}

Here is a shader that just renders your texture without any lighting or alpha transparency:
Shader “Unlit” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
}

SubShader {
Pass {
SetTexture [_MainTex] {
Combine texture
}
}
}
}

Wow, you just helped me getting my game to run a solid 30 fps instead of 20 fps on the iPad by changing one lousy number! (disabling OpenGL ES 2.0) Thanks! Can’t believe I didn’t know about that one before!

OpenGL ES 2.0 line didn’t do much for me, but HOLY CRAP your shader doubled my framerate! Mega-uber-thanks! Unity should add this as an official “2D” shader… :slight_smile:

Wow!
thanks, great share!

I always assumed that 2.0 was turned off when we selected Arm6 (OpenGL es1.1) rather than the universal option.

Are you saying even if we make that selection we should still change that line?

with ARMV6 you indeed have no OES 2 support in at all.

But problem is it is getting harder and harder to get ARMV6 only to apple as apple wants 3GS and 4th gen opted apps and that means ARMV7. don’t expect to send them non-armv7 opted stuff for long anymore if it still works at all I should say.

I dont think I apple really looks under the hood that much when it comes to
Approval.

They look massively under the hood.
Something like this though will normally show up on your end already as the uploader already checks basic requirements and refuses to upload stuff that fails to comply to basic requirements. (for example if you do an ARMV7 only and target anything but iOS4 and/or forget to set the requirement in the plist while its an iphone app, the uploader will refuse to pick it up at all)

The problem with performance on ARMv7 should go away with Unity 3.2. Aras has put a lot of work into making sure that the right precision modifiers are used when compiling GLSL from Cg, and that the built-in shaders use appropriate precision everywhere.

where can i add this code… after i add this it got errors…

Shader “Unlit Transparent Vertex Colored” {
Properties {
_MainTex (“Base (RGB) Trans (A)”, 2D) = “white” {}
}

Category {
Tags {“Queue”=“Transparent” “IgnoreProjector”=“True” “RenderType”=“Transparent”}
ZWrite Off
Alphatest Greater 0
Blend SrcAlpha OneMinusSrcAlpha
SubShader {
Pass {
BindChannels {
Bind “Vertex”, vertex
Bind “texcoord”, texcoord
Bind “Color”, color
}

Fog { Mode Off }
Lighting Off
SetTexture [_MainTex] {
Combine texture * primary, texture * primary
}
}
}
}
}

Here is a shader that just renders your texture without any lighting or alpha transparency:
Shader “Unlit” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
}

SubShader {
Pass {
SetTexture [_MainTex] {
Combine texture
}
}
}
}

the errors is

invalid suffix “D” on integer constant
invalid suffix “D” on integer constant
expected constructor, destructor, or type conversion before string constant

I am currently using the Unlit shaders, where they recently added or why do you not use them?

Are your shaders faster than the Unlit ones Unity provide?

The original post was from 2010. Not really valid anymore as Unity 3.3 comes with new super optimised shaders in the Standard Assets (Mobile) bundle.

Ahh did not know that, are they better than using the Unlit ones?

It’s hard to find all this information scattered around the internet, that’s why I created this thread, I wrote about texture shaders, what should the correct info be you think?

Here:
http://forum.unity3d.com/threads/85974-2D-game-for-iPhone-checklist

why not test on device.

Just tested these shaders, it’s still not really clear to me which to use.

The above one works just like I hoped it would, it gives me an unlit shader that has support for vertex colors with alpha.

In the standard mobile assets we get one that also handles this, but it has a bunch of extra options like shininess and so on. I wonder which one is the faster, I can’t tell from testing currently.

Also there doesn’t seem to be any good shader in the mobile category for simple unlit transparent textures. There is one called background which doesn’t support transparency and one called Transparent → Vertex Color but that one adds the vertex colors on top of the texture.

This is a bit confusing, what should I use? Right now I am using the Unlit Transparent and Unlit ones (not from the mobile category) and the nice shader for vertex colors posten on the top of this thread.

Is there an official recommended shader for use on the iPhone with transparent textures + one for vertex colors with alpha (unlit)?