Fixed bug 4903 - Lack of color multiply with alpha (SDL_BLENDMODE_MOD + SDL_BLENDMODE_BLEND) blending mode for all renderers

Konrad

This kind of blending is rather quite useful and in my opinion should be available for all renderers. I do need it myself, but since I didn't want to use a custom blending mode which is supported only by certain renderers (e.g. not in software which is quite important for me) I did write implementation of SDL_BLENDMODE_MUL for all renderers altogether.

SDL_BLENDMODE_MUL implements following equation:

dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA))
dstA = (srcA * dstA) + (dstA * (1-srcA))

Background:

https://i.imgur.com/UsYhydP.png

Blended texture:

https://i.imgur.com/0juXQcV.png

Result for SDL_BLENDMODE_MOD:

https://i.imgur.com/wgNSgUl.png

Result for SDL_BLENDMODE_MUL:

https://i.imgur.com/Veokzim.png

I think I did cover all possibilities within included patch, but I didn't write any tests for SDL_BLENDMODE_MUL, so it would be lovely if someone could do it.
This commit is contained in:
Sam Lantinga 2020-01-16 08:52:59 -08:00
parent 0cb537758c
commit 7328bbad9e
19 changed files with 819 additions and 159 deletions

View file

@ -495,7 +495,7 @@ SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
status = 0;
flags = surface->map->info.flags;
surface->map->info.flags &=
~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL);
switch (blendMode) {
case SDL_BLENDMODE_NONE:
break;
@ -508,6 +508,9 @@ SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
case SDL_BLENDMODE_MOD:
surface->map->info.flags |= SDL_COPY_MOD;
break;
case SDL_BLENDMODE_MUL:
surface->map->info.flags |= SDL_COPY_MUL;
break;
default:
status = SDL_Unsupported();
break;
@ -532,7 +535,7 @@ SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
}
switch (surface->map->
info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
case SDL_COPY_BLEND:
*blendMode = SDL_BLENDMODE_BLEND;
break;
@ -542,6 +545,9 @@ SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
case SDL_COPY_MOD:
*blendMode = SDL_BLENDMODE_MOD;
break;
case SDL_COPY_MUL:
*blendMode = SDL_BLENDMODE_MUL;
break;
default:
*blendMode = SDL_BLENDMODE_NONE;
break;
@ -874,7 +880,7 @@ SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
{
static const Uint32 complex_copy_flags = (
SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD |
SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL |
SDL_COPY_COLORKEY
);