PocketMatrix forums

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

Choice of alpha blending method

Avatar,

I'll just give my thoughts on all three approaches:
- Scanning each pixel and scaling the transparency according to the bytes in the alpha map: This may seem slow, but consider this:
1. If the alpha map value is 0 or 255, you can skip expensive calculus. This will be the case most of the time, as you noticed yourself.
2. You can pre-scale the sprite data using the alpha map, so you only have to scale the background colors and add the prescaled sprite pixels.
I am not sure what you mean by 'this has to be done for each frame of animation of each sprite'? I assume you want to do this at run time, so yeah, you do it for each sprite, but just for one frame at a time.
- Second idea, edge checking: Read my tutorial 7, and split your sprite in horizontal spans. :) Assume that only the first pixel and the last pixel of a span can have an alpha value (this will be a pretty good approximation of the first approach). Again, prescale the sprite with the alpha data.
Using this approach you can skip the 'if' that you needed in the first approach to test for '0' or '255'. Conditional code is usually very expensive (although I doubt that the StrongARM does branch prediction), so this could speed up your code a lot (I would assume this trick would double the speed). It does take some preprocessing, more complex drawing, and some memory to store all this extra data. On the other hand, you can probably dump the alpha map once you constructed the spans, so memory should not be an issue.
- Table lookup: The problem here is that you want it the other way round. You are drawing the pixels of the sprite one by one, so to lookup whether or not a pixel has a different alpha than '255' (or '0'), you have to look through the entire lookup table. Not a good idea, I think. :)

Hope this helps.
- Jacco.
avatar:go for compiled sprites (or what's the (&@#$ name of it:) this is the fastests way to draw transaprent sprites most of the time. As Phantom suggested, you could make for ex. a 50-50 blend on the first and last pixel of a "span" or use additional byte per pixel to store a full 8bit alpha-blending channel (if you want something more then just "anti-aliased" edges)
About compiled sprites, as far as I know Phantom used them in tut7 (not really sure thru) but the main idea is simple, a sprite is created from blocks of 100% transparent pixels and semi/non-transparent pixels. All you have to do is to count how many pixels in a row are transparent and how many pixels in a row are non/semi-transparent. And now instead checking each pixel if you should draw it or not, you just read the number of pixels in a block and process following-n pixels from this info.
example
let us say we have got a 256color image with color 0 - transparent color - and other colors
like 0 , 0 , 1 , 2 ,123, 0 , 0 , 0 - 8 pixels width.
so we compress it into
2 , 3 , 1 , 2 , 123 , 3 (6 bytes from 8)
now you can wonder how do we read it? simple
first number (2) means, that we start with 2 transaprent pixels. So in your sprite-drawing procedure you simple move the destination point 2 pixels and do nothing more (no compare instructions or anything!:) now we read the next byte which is 3 - from this we know, that following 3 pixels are non/semi-transparent. and pixels values are 1 , 2 , 123 (byte 3,4 and 5) so we draw them to screen. Then we read byte 6 - the last one, which is 3 - so again we just move our destination point by 3 pixels. And that's all:)
(hope you can follow me till now..)
So if you want to add "anti-aliased" edges, instead drawing 3 pixels with values 1,2, 123 respectively, you draw first pixel (value 1) with 50/50 transparency, 2 pixel (value 2) normally and then last-third pixel (value 123) with 50/50 transparency. Smooth edges:)
I must admit, that compiled sprites get a bit tricky when you implement clipping, but nothing you can't handle in a day or two of programming:)
And the speed boost? Well depends heavily on the number of transparent pixels. comparing to normal method (checking if pixel/alpha=0) it is often 2-3 times faster. And with a heavily-transparent pictures you get a several times boost. With a very little transparent pixels you get about the same speed.
PS.this method doesn't require extra memory, actually you could see from my example that it requires less memory. The only situation where it requires more memory is when you have got very tiny blocks (1-2pixels) which is a rare situation.
PPS.Looked at phantom's post... looks like my approach and his are pretty much the same.
Mirek,

What you're referring to is called Run Length Encoding (RLE).  Compiled sprites are sequences of assembly language instructions that store immediate data (sprite) into a target buffer.  There are no conditional tests or memory reads (unless blending with the target) in the sequence of instructions. It's a bear to handle clipping but they are pretty fast.  Again, as with most sprite drawing approaches, its performance is highly data dependent.

We've been talking about sprites and alpha blending for a while now, and I don't know about you guys but I've come to the conclusion that I'd use the simple pixel-by-pixel copy/blend routines if possible.  They are extremely simple to implement and for the majority of games that I've seen this would work just fine and you'd have a frame rate >30 hz.  I'd resort to some of the more elaborate approaches once my frame rate dropped to the point it became noticable and detracted from the gameplay.  What do you think?


digby:because of various reasons I use a pixel-by-pixel method... mainly because i haven't got any PPC to test anything (ie optimizations) so I try to stick to simple stuff and make sure it works everywhere. In the longer run I want to "port" my windows/ddraw/asm RLE sprite routines to PPC (actually I will write them from null cause on PC they use 80x86asm+mmx instructions...)
The performace advantage is really worth it and writing those instructions is a weekend of programming max.
PS.Currently I'm developing a Map Editor which supports multiple layers, so if someone wants to make a colorfull graphics it will be possible, althrought it will be a big performace hit, that's where RLE encoded sprites are really handy. You get a cheap and powerfull toy.
RLE also often saves some memory, which is good on the low-memory PPCs
I also use 4444 ARGB images... poor quality but decent speed. I plan to allow using 8888 ARGB images later as well on faster machines.
ok I have made a post about quite everything here:>
Thanks for the suggestions. I guess I will drop the idea of table lookup and go along with pixel-by-pixel checking w/ alpha. I might consider implementing the RLE encoding: that seems like a real time saver.

Here's a quick question. Say I'm doing alpha calculation. I have a sprite and an alpha map. I want to know which colour to output to the screen, so I multiply the RGB * 255 (or whatever the alpha value is at that point) and then divide by 256. That sounds slow, doesn't it? Any idea on how I can optimise that? I think bit shifiting by 8 would produce a different result...

Another (sort-of) related thing. Has anyone experimented with using simple compression techniques, such as Huffman encoding (or was it Hoffman?) on images? How effective (compression ration-wise) is it? Is it worth implementing (obviously there would be speed penalties on the program load, when the image data gets decompressed)?
avatar:about compression... well I don't know how good it will work, I plan to use the free zlib library, it gives a pretty decent compression rate without quality loss and I hope that will be enought for me.

about alpha blending, you need to have the mul instruction but you can replace the slow divide by 256 with a >>8. A real time saver. You need to remember one thing - you need to process the R,G and B value separately, else you will get weird results.
btw few tips from how I did it(they seem to be pretty correct regarding results)
1.for alpha-blending use the formula:
alpha*(source-destination)+destination - only one multiply per pixel.
2.If low image quality is acceptable, you can use 4444ARGB images to save space.
3.remember, that you need to compute R,G,B values separately, but you can add the destination (at the end of alpha-blending formula) value as one number - no need to separate it, so the final formula looks like:
alpha*(source.R-destination.R)+alpha*(source.g-destination.g)+alpha*(source.b+destination.b)+destination
but remember, that after every alpha*(source-destination) you need to shift those values to proper bits and use an AND function to erase other bits (so they won't be added later to other color components and create wrong results)

ok, I hope it helps you a bit:) you can also read an article about alpha-blending on gamedev.net (go to featured and then the article at the bottom - "alpha-blending with mmx" that's the full name I guess, but it starts with some basics info about alpha-blending which could help you a bit)
Is it at all possible to replace that multiplication? Perhaps if I pre-process my sprites BEFORE displaying anything (through premultiplied alpha) then I just deal with addition. I wonder if that's a good idea (the tradeoff here is, again, increased load-time at program startup).
Mirek,

Sometime I'd like for you to explain to me how you can add the destination color (which is a WORD) without having to first split it into the 3 color components, and end up with the correct value to write to the frame buffer.  

Phantom and I discussed a method weeks ago where you could do this with a DWORD by shifting the color components around and leaving gaps of 0 bits between them, but that doesn't look like what you're doing in your example.

Man, we have really beat alpha blending to death here.  

Avatar, go back and read some of the threads about the shortcuts I posted using premultiplied alpha and storing your alpha mask as an inverse of actual alpha.  Might also help to read that thread where Jacco and I were discussing being able to compute alpha without having to unpack a pixel.

Digby:well I have pretty much explained it above and it seems to work correctly on my images... to be honest, as far as I see things, it can create an error because if you go over 31 it will write 32 - so 0 for the first color and move 1 bit to the next color... but remember, this is the least meaning bit and some PPC's (IPAQ) even use 4 bits per color instead 565... so it's a very small error but makes calculations way easier:)
PS.hmm, about inverting alpha and precalculating stuff... nice idea, as long as you don't change the image it creates some pretty good results I guess... Well for the time being my soft runs with 30-40fps on IPAQ and mostly because of very slow sysmem-vidmem copy procedure, so I will rather update it and then eventually search for a faster alpha-blending stuff:) (I would start with rle-encoded sprites first thru - they save a lot of time...)
btw one thing, from your and my idea we can make a nice stuff which is as simple as this:
you precompute image with alpha and invert the alpha channel, and then use a simlpe formula like this:
source + (invertedalpha*destination.r + invertedalpha*destination.g + invertedalpha*destination.b)
and that could create some pretty nice results I guess:) (It would be much faster and more exact then using a 4444RGBA image...) - you end up with the same number of muls, but you don't do a lot of shift's/and's
ohh, and one more thing that comes into my mind... with pre-calculated image and inverted alpha you can save some space! instead a full 32bpp image you can get away with 24bits! (you store the pre-calculated image as a 16bpp image and 8bits for inverted alpha)
So what do others think about it? For me it seems like a cheap, pretty exact(you might screw the least meaning color bits sometimes,rest should be ok) and pretty low-memory method to get alpha-blending.
ARRRGGGG!  I've just spent the last 30 minutes replying to this thread and once I posted my message and code it was riddled with those dumb*ss smiley icons.  

How do you create a post without that AOL sh*t?  I selected 'No Smilies' before posting, posted my message, then found that a number of the numerical values and parentheses had changed into a smiley face wearing sunglasses and smoking a cigarette (quite the image for today's youth).  I then modified the message and bracketed my code with the code tags in hope that would eliminate Sir Cool Smiley and his bast*rd siblings and it still happened!  This is horrible!  I would think that anyone who is old enough to have hair growing in places normally covered by pants would not be using these stupid icons dispersed throughout their message - especially in a technical forum!

Jacco, is there anyway to totally disable those idiotic smiley icons in this forum, where we frequently post code examples?  

As long as I'm bitching, why is the font so small when you use the code tags?  It should be the same size as the normal text, just make it use a constant pitch spacing.

Maybe I should just post my replies containing code as a txt file on a webserver and just post the link here?

I use [ fixed ] tags for my code. It doesn't hide the smileys though. Just make sure you put spaces before and after parenthesis. Chris will be putting new versions of PM online before too long, which will start fixing some of these problems. Eventually a whole new BB will be used which will solve the problems. Also, since it is written in php, I may be able to help customize them. I would love to see syntactic coloring for source code.

Dan East
...I selected 'No Smilies' before posting, posted my message, then found that a number of the numerical values and parentheses had changed into a smiley face wearing sunglasses and smoking a cigarette (quite the image for today's youth).  I then modified the message and bracketed my code with the code tags in hope that would eliminate Sir Cool Smiley and his bast*rd siblings and it still happened!


LMAO! These smileys piss me off as well, especially when you are trying to get a forumla or a piece of code to display correctly. It's also often puzzling, especially if you don't know what combination of character produces the "Sir Cool Smiley," to see it in  a middle of a word.

Anyway, back to the subject. Mirek: I am already using 8 bit alphas (there's no need for more colours) but 24bit images (because I need the individual R, G, & B pixels, and as far as I understand it it would be 8 bytes (per colour) x 3 colours = 24 bit image. Maybe I'm missing something. In PSP or PhotoShop I only see options to save in either 8 bit or 24 bit depths...
and everyone got totally of subject:/
smiles suxx thru

...you can get away with 24bits! (you store the pre-calculated image as a 16bpp image and 8bits for inverted alpha)


Well I know how to mix an alpha and an image in something like Photoshop, but I've got no clue on how to seperate and retreive the individual images froma single file. Hmm...Unless I parse the raw RGB bits and Alpha bits myself and then create a combined image data format that would go something like RGBARGBARGBA (where A is alpha). Actually, that's a pretty good idea. Lol ;) That was a good self-discussion.
Avatar · 06 Oct 2001, 19:06 UTC — Choice of alpha blending method
I'm just wondering which approach to take to alpha blending implementation in a game. First, I could have a seperate grayscale file (say 256 shades of gray) for each sprite that I will use as an alpha-map, scanning through each pixel, checking its value and altering the actual sprite transparency accordingly. This sounds simple, but slow, especially if this has to be done for each frame of animation of each sprite. Second, I could make some sort of an edge-checking algorithm that will analyse the actual sprite, find its borders and make them semi-transparent or what not (anti-alias effect). This uses less space (no alpha files) but the amount of processing seems to be about the same. Finally, I could implement a table lookup, where I have a pre-created table for each sprite that says this this and this coordinates have this this and this transparency. In this implementation, there's no need for scanning of entire image, so it should be faster than the 2 previous methods.
Now, since I haven't put any of these to practice yet, I don't know if my theories regarding speed are correct. Can anyone confirm/deny my speculations? Or perhaps suggest another implementation?

Thanks
College Degree · 09 Aug 2006, 04:03 UTC — College Degree Online
Get A Bachelors Degree Online
if you wanna get smarter I guess?