4
$\begingroup$

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?

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Derag Jan 9 '18 at 15:25
  • $\begingroup$ You must call Map on a resource that has been created (for example after calling CreateCommittedResource()) $\endgroup$ – Nacho Jan 9 '18 at 18:08
  • $\begingroup$ Check this: github.com/Microsoft/DirectX-Graphics-Samples/blob/master/… $\endgroup$ – Nacho Jan 9 '18 at 18:09
  • $\begingroup$ 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? $\endgroup$ – Derag Jan 9 '18 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.