Locking Resources


Locking a resource means granting CPU access to its storage. The locking procedure is defined by the constants of the LockFlags enumeration. However, these locking flags are only hints, and the run time does not check that applications are following the functionality specified by these flags.

An application that specifies LockFlags.ReadOnly but then writes to the resource should expect undefined results. In general, working against locking flags, including the locking usage flags, is not guaranteed to work in later releases and may incur a significant performance penalty.

A lock operation is followed by an unlock operation. For example, after locking a texture, the application subsequently relinquishes direct access to locked textures by unlocking them. In addition to granting processor access, any other operations involving that resource are serialized for the duration of a lock. Only a single lock for a resource is allowed, even for non-overlapping regions, and no accelerator operations on a surface can be ongoing while a lock operation is outstanding on that surface.

Each resource class has methods for locking the contained buffers. Each texture resource can also lock a sub-portion of that resource. Two-dimensional resources (surfaces) allow the locking of sub-rectangles, and volume resources allow the locking of sub-volumes or boxes. Each lock method returns a System.Array Leave Site that contains values representing the distances between rows or planes of data, depending on the resource type. The returned array always begins with the top-left byte in the locked sub-regions.

When working with index and vertex buffers, you can make multiple lock calls; however, you must ensure that the number of lock calls matches the number of unlock calls.

The Microsoft DirectX Transform set of compressed texture formats, which store pixels in encoded 4x4 blocks, can only be locked on 4x4 boundaries.


Send comments about this topic to Microsoft. © Microsoft Corporation. All rights reserved.

Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center