Additive Alpha Blending Routine in Assembly
This is example source code of an alpha blending routine in assembly. According to it's author, the code can easily be inserted into VC++, in fact, that's what you're supposed to do.
If you can think of any way to speed this up without using MMX (I'm sure there are ways with MMX, but not all of us have it) I'd be glad to hear from you. My email is zer@neo.rr.com. I'm hoping this goes into a code section, and if you don't understand any Assembler, PLEASE don't ask me about it. You can find nice tutorials on Assembler from many places, and it isn't as complex as it looks. Just low level.
Well, have a nice day, and I really hope this helps some of you still using slow C++ methods of blending. If you're putting this into C++, as I'm sure you will be since it uses variables which means it probably won't work with a straight Assembler, you'll want to enclose it in brackets like so:
_asm
{
code goes here
}xor eax, eax // Initialize the variables. This is VC++ 6
xor ebx, ebx // inline Assembler. Mostly this is designed
mov ecx, xheight // for a DirectDraw locked surface.
xor edx, edx
mov esi, ddsdSource.lpSurface // This variable should be the pointer to the source surface.
mov edi, ddsdDest.lpSurface // This variable should be the pointer to the dest surface.
heightLoop16bit565:
push ecx
mov ecx, xwidth
widthLoop16bit565:
mov ax, [esi] // Reads the 16-bit pixel (this is designed for 565 RGB).
test ax, ax // Tests it with an AND to itself. Sets flags, basically.
jz exLoop16bit565 // Skips if pure black (transparent in my game). Use cmp ax, value
// if black isn't transparent for you. Take out this line and the previous
// if you don't need a transparent additive effect.
mov dx, [edi]
and edx, 63454 // Take out the last bits of red, green, and blue.
and eax, 63454 // This ensures the last bits aren't used by the colors.
add eax, edx // This is necessary later on. Total source and dest up into a 32-bit register.
mov ebx, eax // EBX is going to be used as our temporary register here.
and ebx, 65536 // AND it by 65536, which strips out all but the last bit if it overflowed past the max.
jz noRedLoop16bitAAB565 // Skips the next line if an overflow did not occur.
or eax, 63488 // This OR turns on all the red bits.
noRedLoop16bitAAB565:
mov ebx, eax // We do the same thing to green.
and ebx, 2048 // Oh and don't worry about bit 17 being turned on in red,
jz noGreenLoop16bitAAB565 // that will be solved later.
or eax, 2016
noGreenLoop16bitAAB565:
mov ebx, eax // Finally we process blue.
and ebx, 32 // Almost done here!
jz noBlueLoop16bitAAB565
or eax, 31
noBlueLoop16bitAAB565:
mov [edi], ax // Move the latter 16-bits of the pixel into the destination.
exLoop16bit565:
add esi, 2 // Add 2 bytes to the source pointer, to go to the next pixel.
add edi, 2 // Ditto with the destination pointer.
dec ecx // Decrease ECX. As you may have noticed, height was PUSHed in ECX,
// and we did that to make room for width. x86 needs more registers.
jnz widthLoop16bit565 // If we aren't out of pixels on that row, go back for another pixel.
pop ecx // Pulls the height from the stack. Height is now in ECX for a sec.
add esi, ipitchsrc // ipitchsrc should be the pitch DirectDraw gave you in Lock()
add edi, ipitchdest // minus twice the source width. ipitchdest should be the same thing for
dec ecx // the destination surface. We're almost done, now we are decreasing
jnz heightLoop16bit565 // the height and looping back for the next row.Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Jim Bellinger
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion