Sign in to follow this  
arpion

[.net] LockRectangle of Surface

Recommended Posts

arpion    122
I want to lock the backbuffer to read some data,so I use this: public Array LockRectangle( Type typeLock, LockFlags flags, int[] ranks ); but it runs very slow when I lock the hole region,just looks like the managed directx copys the data I locked from vedio memory to system memory. when I use another one: public GraphicsStream LockRectangle( LockFlags flags ); it runs OK,but it very slow too when I read data from it. I have tried it in C++,it's not as slow as above. And I think there is same problem when lock a texture. any good ideas?

Share this post


Link to post
Share on other sites
Premandrake    175
Unfortunately the implementation of LockRectangle is really not suitable for realtime texture updating. It creates a new array every time and that thrashes level 0 of the garbage collector. I haven't found a good workaround for this using Managed DirectX yet (w/o resorting to unsafe code).

The only good way I found to get around it was to create my own texture buffer, then an unmanaged fuction that calls LockRectangle() and copies in the passed in texture buffer. This works much faster as the GC doesn't have to deal with huge arrays being allocated every frame. It's not as clean, but it works.

Share this post


Link to post
Share on other sites
Premandrake    175
Okay, I decided to do some code digging and see what was really happening. Turns out if you use the LockRectangle() that returns a GraphicsStream it doesn't allocate a new array every time you call it. When I use that method (simply call GraphicsStream.Write(data) to update your texture) I get a massive speed up. The LockRectangle() that returns an array should be avoided at all costs for performance critical updates.

Secondly, you have to be sure that your texture is allocated in the correct area. If you are performing updates on it frequently, it should be in system memory. Pool.Managed is system memory backed, so it will lock the system memory copy, then propogate that to the graphics card when it needs it.

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