OpenGL ES sprite alpha

Nibbie
Posts: 2
Joined: 2009.03
Post: #16
There is a real reason for using premultiplied alpha as this is the correct way of doing blending, please check out the blog from tom forsyth
http://home.comcast.net/~tom_forsyth/blog.wiki.html
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #17
jlanquetot Wrote:There is a real reason for using premultiplied alpha as this is the correct way of doing blending...

Maybe in DirectX it is the correct way of doing blending, but not in OpenGL (I know nothing about DirectX, BTW). You can blend either way in OpenGL and both are "correct". Wink
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.03
Post: #18
It is not related to OpenGL or DirectX but the math behind.

Look for premultiplied alpha on Tom Forsyth's blog and you'll get all the explanation about why this the correct way.

This also explained in this paper wrote 25 years ago, when there was no opengl or DirectX.
http://keithp.com/~keithp/porterduff/
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #19
I still don't see how using premultiplied alpha is the "correct" way to do things. If the result of the blending multiply winds up being exactly the same, and you lose no performance because either way the multiply is being done in hardware regardless, how can it possibly be "correct" to do it one way or the other? I already tried to read some of Forsyth's stuff on the premultiplied thing but I can't make heads or tails out of it right now. Perhaps you have an easier explanation?
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #20
AnotherJake Wrote:You could consider filing a bug with Apple to help get them to offer non-premultiplication from Core Graphics.
Out of curiosity, is the alpha pre-mult problem only an issue in the iPhone implementation, or would I also see this issue in an application written for OS X?

My understanding is that a tool in the toolchain that builds the iPhone application bundle is what is responsible for applying the alpha pre-multiplication.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #21
It's not really a "problem", it's just the way they do it. The result works out the same in OpenGL either way in general use. The only time you'll miss your alpha is if you're using shaders, and you have something special going on there. The only other time you'll miss it is if you're trying to work out a particular blend mode, but that doesn't mean it can't be used, just that the blend func needs to be different to account for the premultiplied alpha. It's kind of a pain in the butt because most GL example code out there assumes non-pre-multiplied, so it can throw you for a loop at times. I suppose that could be consider a "problem", depending on your point of view. Wink

Yes, if you use Core Graphics to load your imagery on OS X, it is premultiplied there as well. Just use libpng if you don't want premultiplied.

The toolchain thing you're talking about is more than likely in reference to the png mangling, not the alpha premultiplication. The alpha premultiplication is done by Core Graphics when loading the image. [edit] my bad, apparently the png mangling process involves premultiplication as well... lame! [/edit]

The whole point behind premultiplication is that it's a faster way to draw graphics in software (which is what Quartz originally did exclusively, although I don't know how much of it is hardware now). In hardware, it totally doesn't matter because the alpha multiplication happens regardless. In software it's faster because you can skip a multiply when drawing. Apple really should allow the option of loading without pre-multiplied alpha. File a bug!
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #22
AnotherJake Wrote:I still don't see how using premultiplied alpha is the "correct" way to do things.

Agreed. I have plenty of textures/atlases where certain bits are pre-multiplied and others aren't (and it's totally on purpose and "correct" for that texture). If there's ever a problem with an alpha channel, then it's an art problem, not something that needs to be solved with code.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #23
See also this description of how premultiplied alpha avoids the "fringing" problem that people here seem to run into on a weekly basis, writing My First Sprite Engine.

The key points here are:
1) for 2D compositing, premultiplication is perfectly reasonable, and Quartz is built entirely around it. All images drawn by CoreGraphics will be forced to be premultiplied.
2) for 3D graphics, there are many cases where premultiplication does not make sense. For example, GL's fog, or point/line/polygon smoothing. Those features directly manipulate the RGB or A components of a pixel, and it is up to you to perform alpha multiplication, i.e. in the blend stage. Another common example is putting a normal map in RGB and a gloss map in A in a single texture. It's up to you to apply the channels correctly in the fragment stage, and clearly you don't want the RGB to have been multiplied by A by the image loader.

If the image loader you're using forces premultiplication, it's broken. Use a different one.

Xcode's premultiplication "optimization" of .pngs is a travesty that the spec specifically forbids. Yes, you should file a bug about this, because Apple is corrupting the PNG spec. They should be called ".optpng"s or ".iphonepng"s or something, not ".png"s.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #24
arekkusu Wrote:=Xcode's premultiplication "optimization" of .pngs is a travesty that the spec specifically forbids. Yes, you should file a bug about this, because Apple is corrupting the PNG spec. They should be ".optpng"s or "iphonepng"s or something, not ".png"s.

Holy crap, that's actually part of the toolchain and not just Core Graphics? Blink Wow, that does suck!
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #25
arekkusu Wrote:Xcode's premultiplication "optimization" of .pngs is a travesty that the spec specifically forbids. Yes, you should file a bug about this, because Apple is corrupting the PNG spec. They should be called ".optpng"s or ".iphonepng"s or something, not ".png"s.

AnotherJake Wrote:Holy crap, that's actually part of the toolchain and not just Core Graphics? Blink Wow, that does suck!
So, does this confirm that it is indeed the Xcode iPhone bundle creation step that is performing the alpha pre-multiplication? If I take the image loading code from my iPhone app and move it to another app built against OS X (where I can just read a PNG file from disk), should I see the correct, unaltered RGBA values?
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #26
Guys, take 5 minutes to watch how Xcode builds your app, and do a Google search. You can figure this out, instead of guessing:

If you compile for the device, look at the build log. You'll see a section like:
Code:
CompressResources /Users/.../Your.app
    cd /Users/...
    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/iphoneos-optimize /Users/.../Your.app

Now, what do you think "iphoneos-optimize" does?
Well, it's a script. You can read it. Or try running it:

$ /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/iphoneos-optimize
usage: iphoneos-optimize path [-skip-PNGs]

So, there's already an option to not optimize PNGs.

Now use Google.

Or, read the Xcode build documentation, which lists an option specifically for this.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #27
I actually did read the documentation a loooong time ago (and I do read the build log from time to time, and I damn-well use Google but I never wondered about the script because I was using Core Graphics anyway), and forgot about the mangler (aka "optimization script") actually doing the premultiplication there. In fact, for quite a while I even forgot that Core Graphics did the premultiplication because I'm so used to using libpng on the Mac and I was using Texture2D blindly on iPhone in the beginning.

Quote:So, there's already an option to not optimize PNGs.
Yeah, but not with Core Graphics. Cry

@ kalimba: So yes, this confirms that the PNG mangler does premultiplication too, but it can be disabled. However, you're still stuck with Core Graphics premultiplication unless you use a different loader.
Quote this message in a reply
Member
Posts: 142
Joined: 2002.11
Post: #28
jlanquetot Wrote:There is a real reason for using premultiplied alpha as this is the correct way of doing blending, please check out the blog from tom forsyth
http://home.comcast.net/~tom_forsyth/blog.wiki.html

pre-multiplying the alpha can cause a loss of precision and makes it so setting opacity with glColor4f does not work correctly. So in this case it's the wrong way to do it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES alpha blending gizzerd91 7 8,668 Jul 29, 2009 08:11 AM
Last Post: gizzerd91
  Modfied OpenGL Sprite Rendering tonyb 3 2,963 Jul 26, 2009 07:30 AM
Last Post: tonyb
  For 2D (sprite) games, do I use OpenGL or something else? lindsay 24 12,041 May 9, 2009 12:06 PM
Last Post: Weltevrede
  OpenGL Animated Sprite Question Aboqa 6 5,955 May 6, 2009 11:17 AM
Last Post: AnotherJake
  OpenGl Es Sprite render jjslay 43 25,749 Nov 1, 2008 04:24 AM
Last Post: wonza