Requesting changes in legacy default alpha blending


I am using this framework to render sprites that contains drawTiles() to a texture and use it for special effects. But when I try to render the sprite to a RGBA texture, I see transparent edges that exposes the background.

The default alpha blend function is this: GL.blendFunc (GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);.
This method works find when you are performing it on RGB targets, but for some of the effects including God Rays, I need the alpha channel of the foreground layer. We multiply image2 RGBA by image2’s alpha value, and multiple image1 RGBA by 1-image2’s alpha value. This makes sense but it will create a problem here if you have image2 with alpha 0.5 and image1 with alpha 1.0. As you can see, 0.50.5 + 1.00.5 is less than 1.0, which exposes the background color when it’s not suppose to.

To correctly blend alpha, I think you will need


Presumably this will let the program use the first 2 parameters on RGB and the last 2 for alpha, preventing the first alpha from multiplying itself.

If possible, please improve the alpha blending of the drawTiles() API. Thanks very much.

I think the key here (and a difference between legacy and the newer code) is that it was not premultiplied before, but is premultiplied now. This should have an impact on how it interacts with other aspects of rendering, but is positive by and large for supporting hardware blend modes (which wasn’t really possible before)

Hmm, I am using legacy, so I think the alpha is not premultiplied.

The newer code supports hardware ADD, MULTIPLY and (if I remember properly) SCREEN support. It sounds like this is similar to some of the effects you want to accomplish

There’s a compile flag for the legacy code to try and enable premultiplied alpha, but it never worked in all cases (though I know some people who use it successfully)

lime rebuild windows -Dlegacy -DLEGACY_PREMULTIPLIED_ALPHA (might need to use -clean)

The newer code has some issue when I am trying to port the framework there, including added tiles not showing up, my render passes not showing up etc. I am trying to achieve simply alpha blending RGBA without incorrectly exposing the background.
I am not sure if premultiplying works, because it might still multiply the alpha by itself. I will try it though.

rebuilding lime didnt work…

@singmajesty I have modified the OpenGLContext.cpp to correctly blend alpha when premAlpha is not used. But I am somehow unable to test it. When I run lime rebuild windows -Dlegacy the ndll is not updated at all.

Sorry for stupid question, but do you using lime from github? Rebuilding doesn’t work for lime from haxelib.

I used lime upgrade openfl and it seems that it downloaded from github. I can see and everything. Is this the correct version?

I have lime from haxelib, and there is and even .cpp sources in legacy.
But for me rebuild never works for lime from haxelib. But when I do git clone ... from lime repo, than everything works well. Probably haxelib git ... should also work.

That make sense. What are the steps to install a github version of lime and openfl?

haxelib git lime

You can find documentation on that here:

I suggest you use a development version of openfl too, since the lime development version isn’t always compatible with the haxelib openfl version.

1 Like

Thanks a lot guys, I will try it when I get home.

Hell yea it works. How do I upload the changed file so that it will be used in future updates? I think I’m not allowed to contribute (yet)

1 Like

On github click on fork to create a copy of the repository for yourself,
push to this repository, it’s recommended to use a separate branch.
Then click on the green button, “compare, review, create a pull request” to create a pull request.

I hope it’s not too unhelpful, if not don’t hesitate to ask again :wink:

Thanks for the help. I have submitted a pull request and I think all I do is just wait now.

1 Like