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:
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” {}
}
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…
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.
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.
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” {}
}
invalid suffix “D” on integer constant
invalid suffix “D” on integer constant
expected constructor, destructor, or type conversion before string constant
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?
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)?