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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

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

Post 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 =)
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

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

Post 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!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

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

Post 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.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

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

Post 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
Post Reply