Fastest Possible Blending Function, OpenGL ES 2.0
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:
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?
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?
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.
Use alpha-blending or alpha-testing.
alpha-blending with alpha being either 0 or 1 is the same as 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?
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.
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.)
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.)
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)
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.
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.
Also, I'll have to look into premultiplied textures. I've never heard of them before.
Here is the core of the fragment shader that I use with alpha test:
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).
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).
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.
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.
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 ...
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| What is the fastest way to blit a framebuffer to the iPhone screen? | Rasterman | 20 | 22,294 |
Mar 2, 2011 08:26 AM Last Post: arekkusu |
|
| Fastest sprite method | markhula | 9 | 5,428 |
Sep 24, 2010 08:04 AM Last Post: markhula |
|
| OpenGL ES alpha blending | gizzerd91 | 7 | 6,547 |
Jul 29, 2009 08:11 AM Last Post: gizzerd91 |
|

