Graphics Reference
In-Depth Information
enum D3D11_MAP {
D3D11_MAP_READ,
D3D11_MAP_WRITE,
D3D11_MAP_READ_WRITE,
D3D11_MAP_WRITE_DISCARD J
D3D11_MAP_WRITE_N0_0VERWRITE
}
Listing 2.50. The members of the D3D11_MAP enumeration.
The first three values in the enumeration are fairly self-explanatory. If the resource
is to be read, it must have been created with the D3D11_CPU_ACCESS_READ flag, and if
the resource is to be written to, it must have been created with the D3D11_CPU_ACCESS_
WRITE flag. When reading a resource, its contents will be available in the D3D11_MAPPED_
SUBRESOURCE structure pointed to by the pMappedResource parameter. When writing to
a resource, the same D3D11_MAPPED_SUBRES0URCE structure is used by the application to
know where to write the desired resource data to. When both reading and writing, the
contents of the resource are made available and can then be overwritten by the application,
thus "mapping" the contents of the resource to system memory. The call to unmap the re-
source will make any written changes take effect in the resource.
The D3D11_MAP_WRITE_DISCARD and D3D11_MAP_WRITE_NO_OVERWRITE flags are
used together to allow for dynamic resource usage. The D3D11_MAP_WRITE_DISCARD map-
ping type invalidates the contents of the resource when it is mapped, even if only a sub-
resource is currently being mapped. The D3D11_MAP_WRITE_NO_OVERWRITE mapping type
allows writing to a portion of a buffer that has not previously been updated since the last
call to D3D11_MAP_WRITE_DISCARD. In this way, the contents of the resource that have been
loaded into a dynamic resource may start to be used while other portions of the resource
are still being filled up.
After the resource has been read or written from the mapped subresource structure,
the resource must eventually be unmapped to allow the GPU to continue using it. This is
done simply with the ID3DllDeviceContext: :Unmap() method, which identifies the re-
source and subresource index to be unmapped.
An important consideration for the mapping of resources is that a complete subre-
source must be mapped at a time. This means that it is not possible to read/write a portion
of a subresource, although the entire subresource can be mapped while only updating a
smaller portion of it. Depending on the size of the subresource, this can involve a large
amount of memory being transferred to and from system memory. In some cases this is
unavoidable, but in other cases it would be beneficial to have a method for updating only a
portion of the subresource. We will see such a method in the "Update Subresource" section.
A common example of using the map and unmap methods is to update the contents of
a constant buffer before using it in a pipeline execution. In this case, the resource would be
mapped with the D3D11_MAP_WRITE flag, since we are not interested in reading the contents
Search WWH ::




Custom Search