# How does ID3D12Resource::Map work?

How does Map() work? I would guess that it allocates memory to fit the resource size (or a range of it), but when are the bytes sent to the GPU?

There are two general ways the graphics driver can implement Map().

One way is to set up a virtual memory mapping that makes the resource's actual memory (could be system memory or GPU memory) visible as part of the application's address space. If it does happen to be GPU memory, any writes you do would typically be buffered up and sent across the PCIe bus to the GPU, using write combining. (Even on a unified-memory system, mapped resource memory probably has some different caching behavior from "regular" memory, since CPU and GPU caches aren't coherent with each other.)

The other way the driver can implement Map() is to allocate memory and create a local copy of the resource, let your app edit it, then copy the modified memory back to the resource when Unmap() is called. This might need to be done if the actual memory layout of the resource on the GPU is "weird" somehow, like having a nonstandard tiling scheme or some such.

The copying approach is clearly more expensive due to the extra copies required, so I'd expect the driver to use the memory mapping approach whenever possible.

First you may want to take a look at the doc.

Map basically allows you to retrieve a CPU pointer of a GPU resource. It performs a few operations on the background so the data is up to date. Depending on the type of resource you are mapping, you can perform different tasks like reading data or copying it. As far as I know, Map will only allocate an address not the memory of the resource.

Hope it helps!

• Documentation don't say anything about when data is sent to GPU (or I don't see it). If Map doesn't allocate memory why memcpy works on returned pointer? – Derag Jan 9 '18 at 15:25
• You must call Map on a resource that has been created (for example after calling CreateCommittedResource()) – Nacho Jan 9 '18 at 18:08
• – Nacho Jan 9 '18 at 18:09
• Ok, I get what you mean now. If I get it right: Map basically return virtual pointer to GPU memory and when you access it OS/driver send data directly to resource? – Derag Jan 9 '18 at 18:20