You can directly access the surface memory by using the Direct3DSurface8.LockRect method. When you call this method, the RECT parameter is a RECT type that describes the rectangle on the surface to access directly. To request that the entire surface be locked, set RECT to Nothing. Also, you can specify a RECT that covers only a portion of the surface. Providing that no two rectangles overlap, two threads or processes can simultaneously lock multiple rectangles in a surface. Note that a multisample back buffer cannot be locked.
The LockRect method fills a D3DLOCKED_RECT type with all the information to properly access the surface memory. The structure includes information about the and has the locked bits. When you finish accessing the surface memory, call the Direct3DSurface8.UnlockRect method to unlock it.
While you have a surface locked, you can directly manipulate the contents. The following list describes some tips for avoiding common problems with directly rendering surface memory.
- Never assume a constant display pitch. Always examine the pitch information returned by the LockRect method. This pitch can vary for a number of reasons, including the location of the surface memory, the display card type, or even the version of the Microsoft® Direct3D® driver. For more information, see Width vs. Pitch.
- Make certain you copy to unlocked surfaces. Direct3D copy methods will fail if called on a locked surface.
- Limit your application's activity while a surface is locked.
- Always copy data aligned to display memory. Microsoft® Windows® 95 and Windows 98 use a page fault handler, Vflatd.386, to implement a virtual flat-frame buffer for display cards with bank-switched memory. The handler allows these display devices to present a linear frame buffer to Direct3D. Copying data unaligned to display memory can cause the system to suspend operations if the copy spans memory banks.
- A surface may not be locked if it belongs to a resource assigned to the D3DPOOL_DEFAULT memory pool unless it is a dynamic texture or a private (FOURCC) driver format. Back buffer surfaces, which may be accessed using the Direct3DDevice8.GetBackBuffer and Direct3DSwapChain8.GetBackBuffer methods, may be locked only if the swap chain was created with the Flags member of the D3DPRESENT_PARAMETERS type set to include D3DPRESENTFLAG_LOCKABLE_BACKBUFFER.