Sign in to follow this  

Buffer: Set aplha bytes to 0xFF

Recommended Posts

Hallo What is the best way to set all alpha bytes in a buffer to 0xFF. For example: I have a texture in A8R8G8B8 Format. And I have a second buffer, which delivers me the image I want to put on the texture. This buffer is also 32Bit RGB so I copy it line per line to the texture. But unfortunately the alpha bytes are not correct. I know that there is a solution around this problem, for this scenario. If I would use a X8R8G8B8 texture it wouldn't matter. But my question is more theoretically. What if have MUST modify all alpha bytes in a buffer. Is there some way like masking it? As far as I can see now I have the following solutions: 1. Copy the data DWORD by DWORD while masking the alpha byte to 0xFF 2. Copy the data BYTE by BYTE (*argh*) 3. Copy the complete buffer line by line and then iterate throug all apha bytes and set them to 0xFF (which is slower than solution 1) 4. Using PixelShaders to do the work afterwards (which I haven't testet yet and let's say... it's not an option for now). What else can I do to handle the problem efficent? Is there may be a trick or function which I don't know of? Thanks Snowprog

Share this post

Link to post
Share on other sites
Do you specifically need to use the alpha channel? Could you not survive with an X8R8G8B8 instead?

Alternatively, leave the alpha channel as junk and disable any blending operations.

Pixel shaders would definitely be a simple solution - you can sample in the RGB and then return a float4(tex.rgb, 1.0f) to fill in the alpha channel.

If you want to fix the actual data in storage then I would recommend you do it as a CPU operation outside the resource lock. Try to keep the code inside the lock as simple as possible and ideally as a single memcpy_s() operation.

On a recent compiler (e.g. VC8 and above) you should have a look at the OpenMP extensions for parallelising a for loop over each row. There is no inter-row dependency so it should work quite efficiently and writing a function to operate on a given row (reinterpret_cast<> to a DWORD/BYTE array and alter as appropriate).

If you really need it to be performance critical you might want to look into the SSE instructions and the SIMD route - although I've not dabbled with that myself.


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