Sign in to follow this  
akira32

In DirectX9, how to swap fastly two textures

Recommended Posts

LPDIRECT3DTEXTURE9 texture1; LPDIRECT3DTEXTURE9 texture2; CreateTexture.... I have a special case and need to swap two textures. How do I reach the goal to swap them? If the two textures have the same size and format. way1: LPDIRECT3DTEXTURE9 texture3; texture3=texture1; texture1=texture2; texture2=texture3; way2: Lock the two textures and memcpy the pBits of the D3DLOCKED_RECT. But I have a problem how do I know the pBits's size? Or another way that is fast.

Share this post


Link to post
Share on other sites
The fastest way is to swap the pointers. Don't pass around pointers to textures, pass around pointers to pointers to textures.

Alternatively, the best way is probably with LockRect(). You should lock both textures, and copy scanlines at a time. You can't copy the texture in one go, since if there's extra bookkeeping data, you'll trample all over it.

You can use GetSurfaceDesc() to get the width, height and bpp of the texture (Well, the format, and you can work out bpp from there), then copy scanlines at a time from one texture to the other.


Although as I said, the fastest way by far is not to copy the contents of the textures, but to copy the texture pointers.

Share this post


Link to post
Share on other sites
If you do need to copy the actual bits of the texture, you might also consider offloading the task to the gpu, by doing a render to texture. This might be the more complicated solution, but it's probably the fastest (other than just swapping the pointers as was suggested previously). It also provides a flexible mechanism for doing quick/simple image processing at runtime (scale your texture, perform a shader on it, etc.). Just a thought.

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