Open SDLBugzilla opened 3 years ago
I was actually thinking about this recently.
I did encounter a problem in which I use an intermediate target (framebuffer) to which I draw multiple things with alpha blending (SDL_BLENDMODE_BLEND
). Now, that framebuffer contains premultiplied alpha within its RGB components. In order to draw it into the main scene we need to use an equation which does not multiply the source RGB with its alpha, but only does inversed alpha multiplication for the target (so basically as you said, SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
).
I can see multiple possible use cases for blending mode with premultiplied alpha.
However, a quick look shows that multiplying source by its alpha is done only for SDL_COPY_BLEND
and SDL_COPY_ADD
:
if (flags & (SDL_COPY_BLEND|SDL_COPY_ADD)) {
/* This goes away if we ever use premultiplied alpha */
if (srcA < 255) {
srcR = (srcR * srcA) / 255;
srcG = (srcG * srcA) / 255;
srcB = (srcB * srcA) / 255;
}
}
if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
r = DRAW_MUL(r, a);
g = DRAW_MUL(g, a);
b = DRAW_MUL(b, a);
}
And so on. Otherwise the code flow for premultiplied alpha would be identical to SDL_COPY_BLEND
, so perhaps we can simplify the code by adding flag to SDL that we are using premultiplied alpha source (such as framebuffer or texture) and that there is no need to multiply source RGB by alpha value again.
I ran into this issue as well using intermediate render target textures. Compositing several transparent textures together with SDL_BLENDMODE_BLEND
gave unexpected results until I read the SDL_BlendMode documentation and realized the operation wasn't doing what I thought it would based on the name. I was expecting something similar to the "Normal" layer blend mode in Photoshop.
I have a setup where I'm using multiple render target textures to implement cameras and render passes. The order of operations is roughly:
After copying many of these textures together using SDL_BLENDMODE_BLEND
, the result was a very washed out color:
Using the suggested blend mode above for all of the intermediate render target textures solved the issue for me:
SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
)
This bug report was migrated from our old Bugzilla tracker.
These attachments are available in the static archive:
Adds a SDL_BLENDMODE_PMA blending mode for pre-multiplied alpha blending (premultiplied_alpha.patch, text/plain, 2017-06-26 22:00:46 +0000, 140025 bytes)Reported in version: HG 2.1 Reported for operating system, platform: All, All
Comments on the original bug report:
On 2017-06-26 22:00:46 +0000, Ben Kurtovic wrote:
On 2017-06-28 08:40:59 +0000, Ben Kurtovic wrote:
On 2017-08-14 12:57:34 +0000, Sam Lantinga wrote: