Pixel perfect collision detection in DirectX

The basic collision detection system everyone starts with is bounding box collision detection, but this method can be a bit inaccurate; however we will take advantage of it in this article.
The basic idea is to check first, if bounding box collision detection has occurred, and then check pixel by pixel if the collision trully took place:
check bounding box collisions
if they exist, take a look pixel by pixel

Bounding box collisions

Lets see how we can implement bounding box collisions:
Microsoft provides us with an useful struct called RECT. It represents an axis aligned rectangle, and we will use it to test collisions.
What we should do, is save the bounding rectangle of our SPRITE class, and when we want to look for collisions between to sprites, the only thing we have to do is intersect the 2 RECT scructs the sprites hold.
The code would look something like this:
RECT dest;
IntersectRect(&dest, &sprite1.rect, &sprite2.rect);

Pixel perfect check

Once we know that a collision has happened, it's time to check pixel by pixel!
We will be working directly with the LPDIRECT3DTEXTURE9 variables in order to read their pixels.
First, we will lock them, so that we can read them from the GPU, we have to lock every texture we want to read pixels from:
D3DLOCKED_RECT rectS1;
HRESULT hResult = myTexture->LockRect(0, &rectS1, NULL, NULL);
Then, we will grab the pixel data by reading de pBits component of our locked rectangle (Again, we need to do this for every texture we are working with):
D3DCOLOR* pixelsS1 = (D3DCOLOR*)rectS1.pBits;
Now is when the fun part comes in. We have to check the intersected rectangle pixels to see if the pixels collide.
Sadly, we just can read our texture's data (not the screen data) so we will have to tranlate screen coordinates to each texture's coordinates, and then read the texture's pixels.
for (int rx = dest.left; rx < dest.right; rx++)
{
for (int ry = dest.top; ry < dest.bottom; ry ++)
{
// Translate screen coordinates into texture coordinates
int s1x = rx - sprite1.x;
int s1y = ry - sprite1.y;
int s2x = rx - sprite2.x;
int s2y = ry - sprite2.y;
// a and b will hold the alpha values of the pixel we are staring at
BYTE a = (pixelsS1[s1y * TEXTURE1_WIDTH + s1x] & 0xFF000000) >> 24;
BYTE b = (pixelsS2[s2y * TEXTURE2_WIDTH + s2x] & 0xFF000000) >> 24;
// if both pixels are opaque a collision has taken place
if (a == 255 && b == 255)
{
//Remember to unlock your textures
texture1->UnlockRect(0);
texture2->UnlockRect(0);
return 1;
}
}
}

Conclusion

I hope to have helped you figuring out how you can implement pixel based collisions in DirectX.
But... what to do now? This is just a "simple" example. Based on this example you can develop code that takes into account sprite scaling and rotation. It is often said that locking textures is not very efficient, because it reduces GPU acceleration. A simple approach to solve this would be to store texture pixels in a boolean array and read that array instead of reading the texture's ones.
Another thing to do now is implementing bouncing based on pixels, there are lots of possibilities!