mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-05-18 02:38:27 +00:00
Implemented more flexible blending modes for accelerated renderers
This fixes bug 2594 - Propose new blend mode, SDL_BLENDMODE_BLEND_DSTA blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD); This fixes bug 2828 - Subtractive Blending blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT); This goes partway to fixing bug 3684 - Add support for a pre-multiplied alpha blending mode blendMode = 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 commit is contained in:
parent
f8e1874d01
commit
c59d9923b3
16 changed files with 718 additions and 778 deletions
|
@ -44,6 +44,31 @@
|
|||
return retval; \
|
||||
}
|
||||
|
||||
/* Predefined blend modes */
|
||||
#define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
|
||||
srcAlphaFactor, dstAlphaFactor, alphaOperation) \
|
||||
(SDL_BlendMode)(((Uint32)colorOperation << 0) | \
|
||||
((Uint32)srcColorFactor << 4) | \
|
||||
((Uint32)dstColorFactor << 8) | \
|
||||
((Uint32)alphaOperation << 16) | \
|
||||
((Uint32)srcAlphaFactor << 20) | \
|
||||
((Uint32)dstAlphaFactor << 24))
|
||||
|
||||
#define SDL_BLENDMODE_NONE_FULL \
|
||||
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
|
||||
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
|
||||
|
||||
#define SDL_BLENDMODE_BLEND_FULL \
|
||||
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
|
||||
SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
|
||||
|
||||
#define SDL_BLENDMODE_ADD_FULL \
|
||||
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
|
||||
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
|
||||
|
||||
#define SDL_BLENDMODE_MOD_FULL \
|
||||
SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
|
||||
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
|
||||
|
||||
#if !SDL_RENDER_DISABLED
|
||||
static const SDL_RenderDriver *render_drivers[] = {
|
||||
|
@ -379,6 +404,23 @@ SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
|
|||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
IsSupportedBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
|
||||
{
|
||||
switch (blendMode)
|
||||
{
|
||||
/* These are required to be supported by all renderers */
|
||||
case SDL_BLENDMODE_NONE:
|
||||
case SDL_BLENDMODE_BLEND:
|
||||
case SDL_BLENDMODE_ADD:
|
||||
case SDL_BLENDMODE_MOD:
|
||||
return SDL_TRUE;
|
||||
|
||||
default:
|
||||
return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
|
||||
{
|
||||
|
@ -706,6 +748,9 @@ SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
|
|||
CHECK_TEXTURE_MAGIC(texture, -1);
|
||||
|
||||
renderer = texture->renderer;
|
||||
if (!IsSupportedBlendMode(renderer, blendMode)) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
texture->blendMode = blendMode;
|
||||
if (texture->native) {
|
||||
return SDL_SetTextureBlendMode(texture->native, blendMode);
|
||||
|
@ -1452,6 +1497,9 @@ SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
|
|||
{
|
||||
CHECK_RENDERER_MAGIC(renderer, -1);
|
||||
|
||||
if (!IsSupportedBlendMode(renderer, blendMode)) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
renderer->blendMode = blendMode;
|
||||
return 0;
|
||||
}
|
||||
|
@ -2042,4 +2090,91 @@ int SDL_GL_UnbindTexture(SDL_Texture *texture)
|
|||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
static SDL_BlendMode
|
||||
SDL_GetShortBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
switch (blendMode) {
|
||||
case SDL_BLENDMODE_NONE_FULL:
|
||||
return SDL_BLENDMODE_NONE;
|
||||
case SDL_BLENDMODE_BLEND_FULL:
|
||||
return SDL_BLENDMODE_BLEND;
|
||||
case SDL_BLENDMODE_ADD_FULL:
|
||||
return SDL_BLENDMODE_ADD;
|
||||
case SDL_BLENDMODE_MOD_FULL:
|
||||
return SDL_BLENDMODE_MOD;
|
||||
default:
|
||||
return blendMode;
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_BlendMode
|
||||
SDL_GetLongBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
switch (blendMode) {
|
||||
case SDL_BLENDMODE_NONE:
|
||||
return SDL_BLENDMODE_NONE_FULL;
|
||||
case SDL_BLENDMODE_BLEND:
|
||||
return SDL_BLENDMODE_BLEND_FULL;
|
||||
case SDL_BLENDMODE_ADD:
|
||||
return SDL_BLENDMODE_ADD_FULL;
|
||||
case SDL_BLENDMODE_MOD:
|
||||
return SDL_BLENDMODE_MOD_FULL;
|
||||
default:
|
||||
return blendMode;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_BlendMode
|
||||
SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
|
||||
SDL_BlendOperation colorOperation,
|
||||
SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
|
||||
SDL_BlendOperation alphaOperation)
|
||||
{
|
||||
SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
|
||||
srcAlphaFactor, dstAlphaFactor, alphaOperation);
|
||||
return SDL_GetShortBlendMode(blendMode);
|
||||
}
|
||||
|
||||
SDL_BlendFactor
|
||||
SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
|
||||
}
|
||||
|
||||
SDL_BlendFactor
|
||||
SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
|
||||
}
|
||||
|
||||
SDL_BlendOperation
|
||||
SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
|
||||
}
|
||||
|
||||
SDL_BlendFactor
|
||||
SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
|
||||
}
|
||||
|
||||
SDL_BlendFactor
|
||||
SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
|
||||
}
|
||||
|
||||
SDL_BlendOperation
|
||||
SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
|
||||
{
|
||||
blendMode = SDL_GetLongBlendMode(blendMode);
|
||||
return (SDL_BlendFactor)(((Uint32)blendMode >> 16) & 0xF);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue