<p>There are two general ways the graphics driver can implement <code>Map()</code>.</p>

<p>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 <a href="https://en.wikipedia.org/wiki/Write_combining" rel="nofollow noreferrer">write combining</a>. (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 <a href="https://en.wikipedia.org/wiki/Cache_coherence" rel="nofollow noreferrer">coherent</a> with each other.)</p>

<p>The other way the driver can implement <code>Map()</code> 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 <code>Unmap()</code> 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.</p>

<p>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.</p>

CC BY-SA 3.0
2018-01-09T23:05:37.227Z
2018-01-09T23:05:37.227Z
Nathan Reed
<p>First you may want to take a look at <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dn788712%28v=vs.85%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">the doc</a>. </p>

<p>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.</p>

<p>Hope it helps!</p>

CC BY-SA 3.0
2018-01-09T15:25:54.890Z
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
CC BY-SA 3.0
2018-01-09T18:08:41.923Z
You must call Map on a resource that has been created (for example after calling CreateCommittedResource())
Nacho
CC BY-SA 3.0
2018-01-09T18:09:03.730Z
Check this: https://github.com/Microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12HelloWorld/src/HelloTriangle/D3D12HelloTriangle.cpp#L214
Nacho
CC BY-SA 3.0
2018-01-09T18:20:40.570Z
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
CC BY-SA 3.0
2018-01-09T12:28:29.443Z
2018-01-09T12:28:29.443Z
Nacho
<p>How does <code>Map()</code> 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?</p>

<directx12><synchronization>
How does ID3D12Resource::Map work?
Derag
# How does ID3D12Resource::Map work?

(Asked by: username_0 on 2018-01-05)

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?

**Tags:** directx12, synchronization


---

## 2 answers

### Answer by username_1 on 2018-01-09. Score: 4 | Accepted answer

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](https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/Cache_coherence) 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.


---

### Answer by username_2 on 2018-01-09. Score: 1

First you may want to take a look at [the doc](https://msdn.microsoft.com/en-us/library/windows/desktop/dn788712%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396).

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!

> **Comments:**
> **username_0**: 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?  
> **username_2**: You must call Map on a resource that has been created (for example after calling CreateCommittedResource())  
> **username_2**: Check this: https://github.com/Microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12HelloWorld/src/HelloTriangle/D3D12HelloTriangle.cpp#L214  
> **username_0**: 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?  

---

