![]() |
|
alpha-blending .. the fastest way? |
fsFreak
Member #1,017
February 2001
|
I'm doing semi-transparent windows in my game, and i just wonder if i can optimize this code.. cause it kinda slows down the FPS when drawing many windows at the same time ... code: any idea how to speed things up? |
23yrold3yrold
Member #1,134
March 2001
![]() |
I'm not sure, but should you be releasing the bitmap before those calls to rect()? Try moving that release_bitmap() call to after the rect()'s. Of course, going pixel by pixel as you seem to be is going to be dog slow in any case .... -- |
fsFreak
Member #1,017
February 2001
|
yeah of course .. releasing after the rect should do something .. how silly of me .. but anyhow, is there a better (faster |
Gabhonga
Member #1,247
February 2001
![]() |
yeah. in 16bit mode you could read and write 2 pixels at once, process both, and then store them both within a single 32bit store. and forget about all the creepy allegro functions like getr(): you'll need one routine for every colordepth...extract the color components manually (or by copy&paste other extraction code, for example allegro's). -------------------------------------------------------- |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
OK, but hey, plz ppl tell me about mmx and sse!! I don't really know how they work ( or I have some idea but I'm sure it's not as powerful thingy as u can offer OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
Bob
Free Market Evangelist
September 2000
![]() |
<shameless self promotion> -- |
orz
Member #565
August 2000
|
your inner loop has this code: actually, on second thought... maybe that's kinda complicated... I think I premultiplied |
Korval
Member #1,538
September 2001
![]() |
Why are you doing alpha-blending in software anyway? Why not let the hardware handle it? |
fsFreak
Member #1,017
February 2001
|
Thanks a lot for all intelligent answers ... |
Bob
Free Market Evangelist
September 2000
![]() |
Koval: to use hardware translucency, he'd have to switch to 3D (either OpenGL or Direct3D), both of which are a pain to use if you want to do regular 2D stuff. Simply have a look at AllegroGL's Allegro driver. We try to implement as many Allegro functions as we can, but soem of them are just too anoying (floodfill anyone?) -- |
Korval
Member #1,538
September 2001
![]() |
I assumed that hardware blitting would support alpha transparency if avaliable. And, I also assumed that if it didn't, then Allegro would use an assembly optimized blitting loop. |
Gabhonga
Member #1,247
February 2001
![]() |
for coding for mmx or sse you need to learn assembler, and that's maybe a bit too long topic for this messageboard. but anyways, here's some (at least faster than yours) 16bit c(++) blending loop utilizing the packing operations I spoke of above: 1#define dword unsigned long
2void unpack(dword dizone)
3{ dizone = ((dizone&0x7E0)<<16) | (diyone&0xF81F); }
4void repack(ulong datone)
5{ datone = (datone&0xFFFF) | ((datone>>16)&0x7E0); }
6void cleanpack(ulong wichone)
7{ wichone &= 0x7E0F81F; }
8//blends a mem bitmap onto another
9//both must have same size
10//alpha range 0..255
11void blend16noclip(BITMAP* from, BITMAP* to, dword x, dword y, dword alpha)
12{ void* pf = ((void*)from->line[0]);
13 void* pt = ((void*)to->line[0]);
14 dword ecx = from->h*from->w; alpha >>= 3; //more than 5 bits precision would overflow
15 dword recal = alpha ^ 0x1F; //recal = 255-alpha
16 if(pt&3) //at least eleminate misalligned writes, for misallignment check between pf and pt this routine would be even bigger
17 { dword c1 = *((short*)pf);
18 unpack(c1);
19 c1*=alpha;c1>>=5;
20 c2 = *((short*)pt);
21 unpack(c2);
22 c2*=recal;c2>>=5;
23 cleanpack(c1);cleanpack(c2);
24 c1+=c2; repack(c1);
25 *((short*)pt)=c1;
26 pt+=2;pf+=2; }
27 for(ecx;ecx>1;ecx-=2) //...lol...
28 { dword c1=*((dword*)pf); //fetch two pixels. the first is in the lower word, the second in the upper
29 dword c2=c1>>16;
30 unpack(c1);
31 c1*=alpha;c1>>=5;
32 cleanpack(c1);
33 unpack(c2);
34 c2*=alpha;c2>>=5;
35 cleanpack(c2);
36 repack(c1);
37 repack(c2);
38 c1|=c2<<16;
39 c2=*((dword*)pt); //fetch dest pixels and scale inversed
40 dword c3=c2>>16;
41 unpack(c2);
42 c2*=recal;c2>>=5;
43 cleanpack(c2);
44 unpack(c3);
45 c2*=recal;c3>>=5;
46 cleanpack(c3);
47 repack(c2);
48 repack(c3);
49 c2|=c3<<16;
50 c1+=c2; //add source and dest
51 *((dword*)pt) = c1; //store 2 pixels back
52 pt+=4;pf+=4; //advance both pointers by 4 bytes
53 }
54 if(ecx) //still a pixel left
55 { dword c1 = *((short*)pf);
56 unpack(c1);
57 c1*=alpha;c1>>=5;
58 c2 = *((short*)pt);
59 unpack(c2);
60 c2*=recal;c2>>=5;
61 cleanpack(c1);cleanpack(c2);
62 c1+=c2; repack(c1);
63 *((short*)pt)=c1; }
64}
pheew, I really hope there's no error inside, because I've just hacked it in, but I'm quite sure it works, I've already tested lots of similliar code. -------------------------------------------------------- |
kdevil
Member #1,075
March 2001
![]() |
This thread is a nightmare to read. ----- |
Bob
Free Market Evangelist
September 2000
![]() |
orz (or matthew?), would you mind inserting line breaks in your code? -- |
orz
Member #565
August 2000
|
1. If there's a way for me to edit my post, I don't see it. |
Bob
Free Market Evangelist
September 2000
![]() |
There's a little icon on the top left that looks like a crayon with a paper. -- |
orz
Member #565
August 2000
|
I see a message icon at the top left of each post, but it's not a link. |
Trumgottist
Member #95
April 2000
![]() |
Yep. The pen and paper icon right next to the profile and private message icons is the one. It's for editing, not replying - try it. (Or simply move the mouse over it to get a pop up description of it: "Edit/Delete Post" At least in IE that's true.) -- Play my game: Frasse and the Peas of Kejick |
orz
Member #565
August 2000
|
yes, I feel stupid now. I coulda sworn I tried that button several and used it to reply... |
|