iPhone glShaderBinary - iphone

Does anyone have an example of how to compile a shader, save the shader binary, and use glShaderBinary to later load the shader with iPhone/iOS (OpenGL ES 2.0)?

It's not possible to do this (at least with iOS 4 and below). iOS doesn't support any precompiled binary shaders. If you query OpenGL for the number of binary shader formats it supports, you get back zero.
You're forced to compile the shaders every time you start the app.
(Answered my own question.)

The best place to start is the XCode OpenGL template project, which for some time has included ES 2.0 shaders by default. It also has the distinct advantage of being a very, very simple example.

Related

Transitioning from OpenGL ES 1.1 to OpenGL ES 2.0

It's been a while since iPhone 3GS came out, and now there might be enough market share of OpenGL ES 2.0 supporting devices to warrant developing in it.
But the situation is a lot of developers probably already have huge code bases in OpenGL ES 1.1
How does one transitionf rom ES 1.1 to ES 2.0? I suppose the matrices need to be taken care of, as well as stuff like GL_FOG, GL_CULL maybe?
Is it possible to write "substitutes" for these functions, e.g your own glTranslatef, glPushmatrix etc? Will this mean performance hit?
What other considerations are there for transitioning to ES 2.0? What advantages and disadvantages (besides the obvious device support issue) comes with using either of these?
Looking at the amount of es 2.0 tags compared to standard es tags in stackoverflow, it looks like it's not the time for 2.0 yet though.
Don't just go by activity in the tags on Stack Overflow when trying to determine whether or not to use OpenGL ES 2.0. For one thing, not every 2.0 or shader-related question is tagged as such. Also, a lot of information was present about OpenGL ES 1.1 at or soon after the launch of the iPhone SDK, so people are much more familiar with that API. There clearly is a lot of interest in OpenGL ES 2.0, as evidenced by the fact that my one class session on the subject is by far the most popular of all of my course videos.
For the most part, the way you handle your geometry will be the same between 1.1 and 2.0, as well as things like your framebuffers, but everything else shifts from being determined by built-in functions to your own shaders. You will have to write some code to replicate simple functions like using the model view matrix or texturing, but those tend to only require a few lines in a shader. For example, using the model view matrix to adjust your vertices is as simple as placing a line like this in your vertex shader:
vec4 transformedPosition = modelViewProjMatrix * position;
Personally, I replaced the glRotate(), etc. functions a long while ago using the Core Animation helper functions to manipulate what effectively is a model view matrix. This made it trivial to move that code across to OpenGL ES 2.0.
Jeff LaMarche also has an extremely useful helper class for wrapping most of your shader program setup code in his article here.
For a great guide on making the transition to OpenGL ES 1.1, see the "Migration from OpenGL ES 1.0 to OpenGL ES 2.0" article which is a chapter in the book GPU Pro and can be found within the documentation that accompanies the free PowerVR SDK.
I've explained what OpenGL ES 2.0 can be good for in my previous answers here and here, but perhaps it would be useful to demonstrate a before-and-after in regards to what the new API can give you.
OpenGL ES 1.1:
OpenGL ES 2.0:
Hopefully, you can see the payoff of replacing some of the built-in functions with shaders.
If you have existing projects, I wouldn't recommend moving to 2.0, considering the effort required is very likely more than it'd be worth. That said, for any new projects, there's no reason to even bother to consider 1.1 anymore in my opinion. Most of the devices that have been sold are 3GS' or 4s, both of which are more than capable of handling 2.0.
OpenGL ES 2.0 has a few common things with OpenGL ES 1.1 (in rendering i mean). For each rendering you'll need a shader. For example, FOG also is created with shader. But you will have more power.
+1 to #jer his answer

PowerVR SGX535 Shader Performance (OpenGL ES 2.0)

I'm currently working on a couple of shaders for an iPad game and it seems as if Apple's GLSL compiler isn't doing any optimizations (or very few). I can move a single line in a shader and drop my FPS from 30 to 24 but I really have no idea why this is happening.
Does anyone have any references for the following:
what PowerVR instructions are generated from GLSL instructions?
what are the timings of the PowerVR instructions?
what sort of parallel processing units are in the PowerVR535 and how can they be exploited?
Thanks,
Tristan
Imagination Technologies recently added Mac support for their PVRUniSCo compiler and PVRUniSCoEditor interactive shader editor. These can be downloaded for free as part of the PowerVR SDK. The compiler has support for both the PowerVR SGX 53x series as well as the 540 series in the iPad 2. Unfortunately, the editor runs as a clunky X11 application, but at least it works now.
The editor gives you line-by-line estimates of the number of GPU cycles required throughout your vertex or fragment shader, as well as more accurate best and worst case estimates of total cycles required.
I've been using it to profile my iOS shaders, and it has proven to be extremely useful in finding hotspots:
http://www.imgtec.net/factsheets/SDK/POWERVR%20SGX.OpenGL%20ES%202.0%20Application%20Development%20Recommendations.1.1f.External.pdf
This documet should help you to optimize your shaders for maximum performance. Apple should provide similar information as well.

iPhone cost vs. benefit - OpenGL ES 1.x vs 2.0

I'm not sure if this question has been asked already, my stackoverflow-fu has failed me.
So I'm building an OpenGL-ES-based iPhone game and pretty much all of the examples I've found out in the wild are on OpenGL ES 1.x. Which is fine because at least I'm (re)learning a lot about OpenGL in general.
Now that newer devices support OpenGL-ES 2.0, I'm wondering if anyone has ported their OpenGL-ES 1.x app to 2.0 and if so were there any performance or efficiency gains? For instance, I can setup my lighting (in 1.x) with glLightf(blahblah) and I'm done with lighting...but apparently that function doesn't exist in 2.0 so I'm forced to write it myself? So, how can somebody with no experience "programming the pipeline" accomplish this? Is there a default lighting implementation in 2.0?
I'm probably speaking out of ignorance as I haven't really found any solid iPhone-specific OpenGL-ES 2.0 information.
Any help in this space will be greatly appreciated.
From what I've read, and from my limited time working with it, going to OpenGL ES 2.0 from 1.1 isn't so much a matter of performance as it is about capabilities. If you watch the Mastering OpenGL ES for iPhone videos (part of the iPhone Getting Started Videos available through the iPhone Developer Program site), Apple even states that if you can do what you need to under OpenGL ES 1.1, you don't need to step up to 2.0.
OpenGL ES 2.0's fully programmable pipeline can make simple actions much harder than doing the same thing in 1.1, because you need to write code for parts of the pipeline that were handled for you before. However, 2.0 makes practical many stunning effects that you just couldn't do in 1.1. For example, I recommend watching the WWDC 2010 session video 417 - OpenGL ES Shading and Advanced Rendering and the Graphics and Media State of the Union to see what's possible using OpenGL ES 2.0.
To date, few applications have used OpenGL ES 2.0, given the limited subset of iPhone devices that had compatible GPUs and the lack of documentation and examples. I think we'll see this start to change as the pre-iPhone 3G S devices are phased out. In particular, the iPad has had OpenGL ES 2.0 from launch, so if you are designing an application for it you can rely on these capabilities to be there. More code examples and documentation are sure to appear in the near future.

OpenGL for Android and iPhone

In discussion with some colleagues we were wondering whether OpenGL work developed for Android or iPhone are effectively interchangeable given that both support the spec.
Or is the reality of sharing OpenGL between the two platforms more a case of quirks, tweaks and not as easy as one might have hoped.
An OpenGL implementation normally consists of two parts:
1. Platform specific part. This has function usually related to creating and displaying surfaces.
2. The OpenGL API. This part is the same on all platforms for the specific implementation of OpenGL, in the case of Android, OpenGLES 1.0.
What this means is that the bulk of your OpenGL code should be easy to port.
In C, you might have glLoadIdentity();
In Java on Android, something like gl.glLoadIdentity();
So for the bulk of your code you can cut and paste, and then search and replace prefixes like 'gl.'
Now for the fun part: you really need to be careful what version you are coding against. OpenGL for the desktop has APIs which don't exist in OpenGLES. There are also some OpenGL data types specific to each platform. In addition, you have 1.0 (e.g. Android) 1.1 (e.g. iPhone) 2.0 (e.g. iPhone GS) to deal with. The differences in API often have to do with additional hardware capability, so it's not like you can write some easy wrapper code to emulate 2.0 features in 1.0/1.1.
OpenGL ES on Android is done according to Khronos Java GLES spec JSR239 , and wraps GL calls in something like glinst.glBindBuffer(FloatBuffer.wrap(data) ... )
OpenGL ES on iPhone is done using stock GL.h files and the same call will just look like glBindBuffer(data...)
The code will not be interchangeable and will cause many quirks, even before you get into the whole mess of differences between 1.0 1.1 and 2.0 APis.
Both platforms use OpenGL ES, but Wikipedia claims that Android uses 1.0 while the iPhone uses 1.1 (original and 3g) an 2.0 for the 3gs link. It's likely that at least some programs will use api functions not included in 1.0, so there won't be full compatibility between the 2 (well 3).

OpenGL ES 1.x Shaders

I am trying to find examples of how to implement a simple shader for OpenGL ES 1.x (specifically for the iPhone). I have never worked with shaders before, but I do understand what they are used for. I think that once I am able to load a simple shader in the simulator I will be able to take it from there and do what I need to do.
Thanks for the help,
~Eric
The iPhone currently uses OpenGL ES 1.1 which doesn't support a programmable pipeline. OpenGL ES 2.0 does seem to have them though and also be not compatible with 1.1 .
From this post:
The pixel shader of the iP* platform is programmed via texture combiners.
My understanding is that the hardware shaders on the iPhone are already being used by the OS for its features in CoreAnimation and OpenGL ES. This means they are not available to you.
Are you looking for a software shader? Try http://unity3d.com/unity/features/shaders