Sign in to follow this  
Headkaze

Fast 32-bit memset

Recommended Posts

Headkaze    607
Working in DirectX I often need to grab frames in 32 bit ARGB format. Quite often I'm finding a need to go from RGB32 to ARGB32 so for now I just iterate through each 32-bit pixel setting the alpha to 0xff. If I don't do this the image will appear transparent. Eg.
for(int i=0; i<m_width * m_height; i++)
	((UINT32*)m_frameBuffer)[i] |= 0xff000000;
I'm just curious is there a memset() type function that works in 32-bits so I could simply call
memset32(m_frameBuffer, 0xff000000, m_width * m_height);
Or something faster than iterating through each pixel?

Share this post


Link to post
Share on other sites
Headkaze    607
Sorry I should have explained it a bit better. I am grabbing frames from an ActiveX window then creating texture frames from a dib of the window. But these frames are in RGB32 format but without the alpha channel set. So I need to set the alpha channel before I convert the bitmap to a texture otherwise it will appear transparent.

I also realised memset wouldn't help me anyway because I need to OR the original data not overwrite it.

Oh well I guess I will either have to use an RGB32 texture or just have a loop to set the alpha channel.

Share this post


Link to post
Share on other sites
Hodgman    51221
Quote:
Original post by Headkaze
I also realised memset wouldn't help me anyway because I need to OR the original data not overwrite it.

Oh well I guess I will either have to use an RGB32 texture or just have a loop to set the alpha channel.


Assuming the the upper 8 bits are already set to zero, then OR'ing will have the same effect as ADD'ing.
e.g.
unsigned int number = 0x00123456;
unsigned int mask = 0xFF000000;
assert( number + mask == number | mask )

Therefore, you could use a SIMD ADD instruction that will add 0xFF000000 to every 32-bit number in your array.
I don't know much about using SIMD instructions, such as MMX/SSE/etc, but there's plenty of other forum members who do.

Share this post


Link to post
Share on other sites
Endurion    5407
If you don't need alpha test (parts of the image transparent) just modifying the renderstates (ie. turn off alphatest and -blend) you don't have to modify the texture data at all (best speed).

If the states are turned off the card doesn't care if any alpha values are zero. Or go straight for X8R8G8B8.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this