Fastest Possible Blending Function, OpenGL ES 2.0

Member
Posts: 31
Joined: 2010.02
Post: #1
Hi,

I want to render a 3D model textured with a image which contains alpha transparency values.

Right now I'm achieving this by rendering with this enabled:

Code:
glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_COLOR, GL_DST_COLOR );

But, this blend mode uses additive blending, which I do not need. And, it will make objects semi-transperent. I only need pixels to be completely visible or completely invisible, with no middle ground.

Is there a faster blending function or OpenGL ES 2.0 feature to achieve this?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Use glAlphaFunc() instead? I assume that hasn't disappeared in ES 2.0 though I don't know for sure. It sounds like alpha testing instead of blending doesn't make things faster on the iPhone's GPU though.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #3
Use alpha-blending or alpha-testing.

alpha-blending with alpha being either 0 or 1 is the same as alpha testing.

Macmenace Wrote:Hi,

I want to render a 3D model textured with a image which contains alpha transparency values.

Right now I'm achieving this by rendering with this enabled:

Code:
glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_COLOR, GL_DST_COLOR );

But, this blend mode uses additive blending, which I do not need. And, it will make objects semi-transperent. I only need pixels to be completely visible or completely invisible, with no middle ground.

Is there a faster blending function or OpenGL ES 2.0 feature to achieve this?
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
Blending isn't such a hit on the iPhone as on the desktop, due to the different hardware architecture. So alpha testing isn't measurably faster, if at all.
Quote this message in a reply
Member
Posts: 31
Joined: 2010.02
Post: #5
Thank you for all the replies so far.

GL_ALPHA_TEST is no longer available in OpenGL ES 2.0 (thus glAlphaFunc is gone too). Both were in ES 1.0 but I heard alpha testing was very slow.

So, it looks like I'm stuck using GL_BLEND. Is there blending function which can mimic alpha testing? (And, is not too costly.)
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #6
Macmenace Wrote:Thank you for all the replies so far.

GL_ALPHA_TEST is no longer available in OpenGL ES 2.0 (thus glAlphaFunc is gone too). Both were in ES 1.0 but I heard alpha testing was very slow.

So, it looks like I'm stuck using GL_BLEND. Is there blending function which can mimic alpha testing? (And, is not too costly.)

Yeah, you need to use shaders and the discard keyword but alpha-blending is still the preferred option ( using "discard" causes all kinds of pipeline related slowdowns)
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
If your texture has the right alpha values, eg only zero or one, use (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) for non-premultiplied, or (ONE, ONE_MINUS_SRC_ALPHA) for premultiplied textures, though in if your alpha is only ever one or zero, it shouldn't matter.
Quote this message in a reply
Member
Posts: 31
Joined: 2010.02
Post: #8
Ok, thank you for all the valuable information. I'll play around with a few different techniques and see which is the fastest.

Also, I'll have to look into premultiplied textures. I've never heard of them before.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #9
Here is the core of the fragment shader that I use with alpha test:

Code:
#ifdef GL_ES
precision highp float;
#endif

uniform sampler2D u_tex0;
uniform bool u_alphatestenable;
uniform float u_alphatestvalue;

varying vec2 v_texCoord;
varying vec4 v_color;



void main()
{
  vec4 basecolor = texture2D( u_tex0, v_texCoord ) * v_color;
  
  if(u_alphatestenable){
    if(basecolor.a == 0.0){
      discard;
    }
  }
  
  gl_FragColor = basecolor;
    
}

as mentioned before, you use discard to discard the fragment if it's alpha is equal to zero (in this case, you can change the test to anything you like).
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #10
That is a particularly inefficient shader. You'll pay the performance cost of using discard, even when you set ualphatestenable to false. You would be much better off using two different shaders. Also, you're performing the color multiply in high precision, when lowp would likely be faster.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2010.03
Post: #11
Frogblast Wrote:That is a particularly inefficient shader. You'll pay the performance cost of using discard, even when you set ualphatestenable to false. You would be much better off using two different shaders. Also, you're performing the color multiply in high precision, when lowp would likely be faster.

That is just a small section of a complex shader that needs to do those things.

While it may seem inefficient it's not if it actually needs to do those tests. As you say speed ups can be gained by writing multiple shaders and I do do this but there is also a cost when calling glSetProgram.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #12
dazza Wrote:That is just a small section of a complex shader that needs to do those things.

While it may seem inefficient it's not if it actually needs to do those tests. As you say speed ups can be gained by writing multiple shaders and I do do this but there is also a cost when calling glSetProgram.

Dazza, listen to frogblast ... from my experience he knows what he is talking about ( he works for Apple)

Me thinks you are looking at this shader and expecting it to behave like a C program ( if (..) { } ) but it wont ...
Quote this message in a reply
Member
Posts: 31
Joined: 2010.02
Post: #13
Thank you for all the input. For some reason my e-mail was bouncing and I did not receive these messages. I will try multiple methods and profile them to see which is the best.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  What is the fastest way to blit a framebuffer to the iPhone screen? Rasterman 20 27,520 Mar 2, 2011 08:26 AM
Last Post: arekkusu
  Fastest sprite method markhula 9 6,808 Sep 24, 2010 08:04 AM
Last Post: markhula
  OpenGL ES alpha blending gizzerd91 7 8,704 Jul 29, 2009 08:11 AM
Last Post: gizzerd91