PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Alpha blending, is this an improvement?

Hello, let's me first introduce myself ;)
I'm currently writing a cross-platform framework and now i'm at a good point: when four months ago i started the project, i searched for some good information about cross-platform programming and so, because this was my first cross-platform project and i thought some background information was necessary, mostly because i never had a PocketPC (really!)
While thinking how to organize things, i took my latest rasterization functions and started thinking on how to write them in a generic and mostly reusable way, since my previous one-platform framework really lacked genericity in rasterization functions.
I came up with some working templates, but i faced a really huge problem: eMbedded Visual C++ wasn't supporting "friends templates" at all: i was really shocked and, due to this, i decided to search around for some solutions.
One "googling" day, PocketFrog come to my attention: despite its PocketPc-oriented approach, the most obviously inspirating thing for me were the templated rasterizer functions.
I immediately realized "friends templates" wasn't needed at all (at least for the rasterization part, but now i know there are no patch from Microsoft for this): so really a great finding and keep up the good work on it!
Now, i'm currently writing a particle engine and i found out that the blending shader produce greenish results with low blending values, here is a screenshot of what i'm talking about. The particle is a black&white rgba png:

Image
Image

The particles at the left are blended with the actual PocketFrog alpha-blending shader: the middle one is a slighty minor change in the shader code but yet showing scattered greenish: the right one is a rewrite of the shader by using what i call a "lossy green mask" in order to lose the least significative bit while retaining all the 6-bits of green.
Whereas there is a great improvement on the colors and the greenish goes away, losing 1-bit on the green channel also means losing the nice shading (the second screenshot better shows the shading going away).
Here is the code for the modified shader:

Code:

// declares lossy green masking and compensation values
const Pixel LOSSY_GREEN_MASK = 0x1F << 6;
const Pixel GREEN_COMPENSATION = 0x01 << 5;


Code:

// the shader itself
struct AlphaBlend : public PixelShader
{
int blendQuantity;
Pixel operator()( Pixel src, Pixel dest ) const
{
Pixel RB1 = dest & (RED_MASK | BLUE_MASK);
Pixel G1 = dest & ( LOSSY_GREEN_MASK );
Pixel RB2 = src & (RED_MASK | BLUE_MASK);
Pixel G2 = src & ( LOSSY_GREEN_MASK );

int32_t blendingQ = blendQuantity >> 3;
Pixel RB = RB1 + (((RB2-RB1) * (blendingQ)) >> 5);
Pixel G = G1 + (((G2-G1)*(blendingQ))>>5);
RB &= (RED_MASK | BLUE_MASK);
G &= ( LOSSY_GREEN_MASK );
//if( G > 0 )
//G |= GREEN_COMPENSATION; // compensate for the green masking

return (RB | G);
}

};



I'm looking forward for your considerations: i think having color-correct stuff is good, but in 565 this came at a cost.
What do you think? Can this be seen as an improvement? Arduous question ;)

<edit>
Well, i just realized there is no need to compensate, and also doing it could lead to some artifacts (visible on a solid dark/black background)
</edit>
Well RGB565 is sucky in that green has 1 more bit then the two other channels. That means that special care is needed for green when writting graphics code.

When using alpha-blending, you can't interpolate green with the same precision as red and blue. Because of this, it is possible to get some artifacts. You could apply some sort of dithering, but the fact is you will almost never see the difference and it comes at a performance cost.

I am surprise to see that much green in your shots though... Are you sure you are blending properly?
Well, the testing particle system (i've built it specifically for testing alpha blending, additive and such) basically works this way: i've 50 particles with obvious properties such as direction, velocity, position, ttl and maxttl.
the intensity of the single particle is then calculated:

Code:

float fIntensity = ttl / maxTtl;
uint8_t intensity = fIntensity * K;


Where K is the maximum intensity i manually adjust in order to better check shades.
That is: while ( K <= 80 ) the green alpha blending is really increasing, the smaller the K value, the greenish it appears and from ( K <= 40 ) on, the greenish is really much accentuated.

(alpha = 40)
Image

(alpha = 80)
Image

Also, while ( K > 80 ) the greenish doesn't completely goes away, so i suspect the green may be overflowing in some way or another: i suspect this because of the multiplications involved in the alpha blending, as well as the multiplication on my additive blending formula.
Then the same apply with additive blending too, although i coded it quickly, here are the two versions i wrote (the per-pixel alpha functions, one "standard" and one with the lossy approach): for better performances, i wrote a specialized raster function, but for the sake of the explanation i'm doing copy-and-paste of the alpha shader (that's what i use for developing and testing before "templatization").

(addFactor = 40)
Image

(addFactor = 80)
Image


This code generate the greenish particles on the screenshot
Code:

struct OriginalAdditive : public AlphaFunc
{
uint32_t addFactor;
Pixel operator()( AlphaPixel alpha, Pixel src, Pixel dest ) const
{
int32_t opacity = ((int32_t)alpha);
if( opacity > 0 )
{
uint32_t factor_s2 = ((uint32_t)addFactor) >> 2;
uint32_t factor_s3 = ((uint32_t)addFactor) >> 3;
uint32_t opacity_s2 = opacity >> 2;
uint32_t opacity_s3 = opacity >> 3;

// calculate source
uint32_t r = ((GETR_565( src ) * opacity_s3) >> 5)
uint32_t g = ((GETG_565( src ) * opacity_s2) >> 6)
uint32_t b = ((GETB_565( src ) * opacity_s3) >> 5)

// multiply by factor
r = ( r * factor_s3 ) >> 5;
g = ( g * factor_s2 ) >> 6;
b = ( b * factor_s3 ) >> 5;

// add dest
r += GETR_565(dest);
g += GETG_565(dest);
b += GETB_565(dest);

// clamp result
if( r > 0x1f ) r = 0x1f;
if( g > 0x3f ) g = 0x3f;
if( b > 0x1f ) b = 0x1f;

// final result
return( (r << RED_SHIFT) | (g << GREEN_SHIFT) | (b << BLUE_SHIFT) );
}
return dest;
}
};



This code doesn't generate greenish on the particles (right) but the result is pretty much unshaded

Code:

struct BlendAdd_LossyGreen : public AlphaFunc
{
int32_t addFactor;
Pixel operator()( AlphaPixel alpha, Pixel src, Pixel dest ) const
{
int32_t opacity = ((int32_t)alpha) >> 3;
if( opacity > 0 )
{
int32_t factor = ((int32_t)addFactor) >> 3;

// calculate source
int32_t r = (int32_t(GETR_565( src )*opacity) >> 5);
int32_t g = (int32_t(GETG_565_LOSSY( src )*opacity) >> 5);
int32_t b = (int32_t(GETB_565( src )*opacity) >> 5);

// multiply by factor
r = ( r * factor ) >> 5;
g = ( (g & ~0x01) * factor ) >> 5;
b = ( b * factor ) >> 5;

// add dest
r += int32_t(GETR_565(dest));
g += int32_t(GETG_565_LOSSY(dest));
b += int32_t(GETB_565(dest));

// clamp result
if( r > 0x1f ) r = 0x1f;
if( g > 0x3f ) g = 0x3f;
if( b > 0x1f ) b = 0x1f;

// compensate green for the lossy masking
if( g == 0x3e ) g = 0x3f;

// final result
return( (r << RED_SHIFT) | (g << GREEN_SHIFT) | (b << BLUE_SHIFT) );
}

return dest;

}
};


Oh well, i'm really going to be crazy! I tried to be much clear as i can, i know this stuff is hard to explain and to be understandable is hard too: i said i SUSPECT the green is overflowing by the mutliplication, by i'm not pretty sure because the same thing should apply to the red and blue channels, although it seems to me it isn't happening at all.
And again, thank you for taking time to read all this stuff!
It looks like the green problem is because your calculations use 1 more bit precision for green. The R & B get rounded down more than the green, so it looks too green.

Just use the same shift coefficients for all color components... better yet, since you're using 32 bit integers - you can't overflow, right? What's the maximum numbers coming in?

So just do 1 shift when
you're done instead of
2.
Code:

struct OriginalAdditive : public AlphaFunc
{
uint32_t addFactor;
Pixel operator()( AlphaPixel alpha, Pixel src, Pixel dest ) const
{
int32_t opacity = ((int32_t)alpha);
if( opacity > 0 )
{

// calculate source
uint32_t r = ((GETR_565( src ) * opacity) >> 8)
uint32_t g = ((GETG_565( src ) * opacity) >> 8)
uint32_t b = ((GETB_565( src ) * opacity) >> 8)

// multiply by factor
r = ( r * addFactor ) >> 8;
g = ( g * addFactor ) >> 8;
b = ( b * addFactor ) >> 8;

// add dest
r += GETR_565(dest);
g += GETG_565(dest);
b += GETB_565(dest);

// clamp result
if( r > 0x1f ) r = 0x1f;
if( g > 0x3f ) g = 0x3f;
if( b > 0x1f ) b = 0x1f;

// final result
return( (r << RED_SHIFT) | (g << GREEN_SHIFT) | (b << BLUE_SHIFT) );
}
return dest;
}
};


I'm still a bit confused between the addfactor and opacity... Looks like it's doing the same thing twice to me.
I came up with a solution some days ago and i performed various test in order to ensure it's working correctly and, until now, the greenish is quite disappeared, additive blending also works correctly and looks really really good, so i think this is the way to go.

@fast_rx: You are absolutely correct to use the same precision for all color components, but this still leads to greenish results where it shouldn't.
The solution i found is to round the values before shifting them, as the shift itself perform rounding towards negative infinity, so i'm just adding the fixed-point equivalent of "0.5" and things are really better.

The alpha blending equation is a simple linear interpolation, and in floating point math it looks this way, as you already know:
Code:
Pixel p1, p2;
float alpha;

result = p1 * alpha + p2 * ( 1.0 - alpha );

When at first i translated it to fixed point, i was also scaling the alpha value to the number of bits of the color channel, as you can see from my previous post: that is, i was using alpha values [0-31] for the red and blue channels and [0-63] for the green one.
This was the first error i did, because the alpha value has to be the same for all the channels (i just thought about on how we have to represent a white color in 565, 0x1f/0x3f/0x1f and here the green obviously dominates). So here it's the first pass for a correct version:
Code:
blendQuantity = ( [0-255] >> 3 );
Pixel RB = RB1 + ( ( ( RB2 - RB1 ) * blendQuantity ) >> 5 );
Pixel G = G1 + ( ( ( G2 - G1 ) * blendQuantity ) >> 5 );

This seems correct, because now i'm scaling by the same factor, BUT this formula still leads to greenish results.
I thought my deductions about the need to NOT scale the alpha quantity for the green channel was correct, so i started suspecting about the fixed point version of the formula: specifically, i was suspecting the lack of a bias value in order to round the computed values before the last ">> 5" shift.
Rounding values in floating point math, i'm used to add 0.5f while casting to an int, but how to perform this simple task in fixed point math?
Well, i found out (reading some VirtualDub stuff) that the fixed point version of the "0.5" value is "1 << ( shift - 1 )" so i changed the formulas to perform biasing on pre-shifted values and moved the masking thing directly here (prevents overflowing in the near channels):
Code:
blendQuantity = ( [0-255] >> 3 );
Pixel RB = RB1 + ( (( ( RB2 - RB1 ) * blendQuantity) + 0x8010 ) >> 5 ) & 0xF81F;
Pixel G = G1 + ( (( ( G2 - G1 ) * blendQuantity) + 0x200 ) >> 5 ) & 0x7E0;

Doing it this way, the greenish goes away quite completely, but more importantly, the nice shades are back on screen ;)


About the opacity and addFactor:

This version of additive blending has been coded in a hurry and i'm pretty sure there are a lot of better ways to do this, anyway, the framework's user should be able to decide how much of the computed source pixel has to be added to the destination by using the addFactor. The opacity value come straight from the Surface object, that contains alpha information.
The addFactor shouldn't interact with the opacity: i intend to pre-multiply alpha information at Surface construction time (if requested), so i'll implement both methods and choose them at runtime.
Have you seen this post:

http://www.pocketmatrix.com/forums/viewtopic.php?t=16270&postdays;=0&postorder;=asc&start;=0

Tala showed here how it could be done with a single multiply.
j.edwards wrote:Have you seen this post:

http://www.pocketmatrix.com/forums/viewtopic.php?t=16270&postdays;=0&postorder;=asc&start;=0

Tala showed here how it could be done with a single multiply.


Yes, thank you: i'm using this formula with rounding:

Code:

d = ( ( ( (d-s)*(m_alpha) + 0x2008010 ) >> 5 ) + s ) & mask;