Page 1 of 1

Alpha Blending (was What does your OS look like?)

Posted: Fri Sep 14, 2012 2:16 am
by Jezze
I guess the 32-32 RGB is just a memcpy right?

For the alpha I was thinking something in the line of:

Code: Select all

destPixel = destPixel + ((sourceAlpha * (sourcePixel - destPixel)) >> 8);
where each sourcePixel, sourceAlpha and alphaPixel would be a value from 0-255 as in 24 and 32bit color systems.

Oh, and nice work btw =)

Re: What does your OS look like? (Screen Shots..)

Posted: Fri Sep 14, 2012 2:36 am
by klange
Jezze wrote:I guess the 32-32 RGB is just a memcpy right?

For the alpha I was thinking something in the line of:

Code: Select all

destPixel = destPixel + ((sourceAlpha * (sourcePixel - destPixel)) >> 8);
where each sourcePixel, sourceAlpha and alphaPixel would be a value from 0-255 as in 24 and 32bit color systems.

Oh, and nice work btw =)
RGBA-RGBA blits look like this for each pixel:

Code: Select all

        uint8_t a = _ALP(top);
        uint8_t b = ((int)_ALP(bottom) * (255 - a)) / 255;
        uint8_t alp = a + b;
        uint8_t red = alp ? (int)(_RED(bottom) * (b) + _RED(top) * a) / (alp): 0;
        uint8_t gre = alp ? (int)(_GRE(bottom) * (b) + _GRE(top) * a) / (alp): 0;
        uint8_t blu = alp ? (int)(_BLU(bottom) * (b) + _BLU(top) * a) / (alp): 0;
Where those last four bytes are the destination values.

I'm going to look at doing this with SIMD instructions to try to get more performance out of it, but that'll require some interesting alignment hacks.


Thanks!

Re: What does your OS look like? (Screen Shots..)

Posted: Fri Sep 14, 2012 3:59 am
by bluemoon

Code: Select all

        uint8_t a = _ALP(top);
        uint8_t b = ((int)_ALP(bottom) * (255 - a)) / 255;
        uint8_t alp = a + b;
        uint8_t red = alp ? (int)(_RED(bottom) * (b) + _RED(top) * a) / (alp): 0;
        uint8_t gre = alp ? (int)(_GRE(bottom) * (b) + _GRE(top) * a) / (alp): 0;
        uint8_t blu = alp ? (int)(_BLU(bottom) * (b) + _BLU(top) * a) / (alp): 0;
It seems do not handle overflow/saturate well, it that blend function designed for purpose?

A normal alpha blend function may look like this:

Code: Select all

reg0 = b1 b2 b1 b2 : b1 b2 b1 b2 ; blend factor

Load Pixel
reg1 = A1 R1 G1 B1 : A2 R2 G2 B2

PSHUFB
reg2 = A1 A2 R1 R2 : G1 G2 B1 B2

PMADDUBSW
DEST[15-0  ] = Saturate(b1B1 + b2B2)
DEST[31-16] = Saturate(b1G1 + b2G2)
DEST[47-32] = Saturate(b1R1 + b2R2)
DEST[63-48] = Saturate(b1A1 + b2A2)

Store Pixel
Here you calculate b1, b2, which is the blending factor using the function of your choice - note that this will may depends on alpha of each pixel, or otherwise remain constant for the whole operation.
You may expand this to do 4 pixel per iteration as well.

Re: What does your OS look like? (Screen Shots..)

Posted: Fri Sep 14, 2012 10:23 am
by klange
bluemoon wrote:It seems do not handle overflow/saturate well, it that blend function designed for purpose?

A normal alpha blend function may look like this:

Code: Select all

...
Here you calculate b1, b2, which is the blending factor using the function of your choice - note that this will may depends on alpha of each pixel, or otherwise remain constant for the whole operation.
You may expand this to do 4 pixel per iteration as well.
My implementation is a fully accurate alpha blend. The divisions are mathemetically sound, such that none of the operations will overflow and saturation is implicit with the alpha calculations.
Image