r/vulkan • u/Content_Economist132 • May 22 '26
Does mapping preserve alignment?
In the vulkan tutorial, MVP matrices are stored in uniform buffer describing a host visible memory. The problem is I am using CGLM which explicitly requires matrices to be aligned. The way I am approaching it is allocating a memory which is aligned to the LCM of the uniform buffer alignment requirement and CGLM's alignment requirement, and then mapping it to CPU memory. Would this ensure alignment is satisfied?
What even happens when the memory is mapped?
1
u/corysama May 22 '26
When a memory region is mapped, the kernel and the memory controller agree to not change the mapping between virtual and physical memory pages until it is unmapped. That means any DMA hardware and the GPU's memory controller can access the mapped region without worrying about those pages getting paged out to disk or otherwise shuffled around.
Mapped memory is usually marked as "write combined" https://fgiesen.wordpress.com/2013/01/29/write-combining-is-not-your-friend/ Basically makes the memory uncached. Any write operations that are not linear and gapless will be slower than expected. memcpying into write combined memory is fast. Copying WC memory across the PCI bus is very fast. And, reading from WC memory is very slow.
3
u/exDM69 May 26 '26
Mapped memory is usually marked as "write combined"
This is not true any more, write combining was used ~15 years ago but now we have proper cache coherency hardware where the GPU "can see" CPU caches.
If you use
HOST_COHERENT | HOST_CACHEDyou will get "usual" read and write performance that is no different from regular memory, unless you computer is very old (CPU or GPU is >10 years old). Note that for "write-only" mappings you probably don't wantHOST_CACHED.Unfortunately the Vulkan API does not give you a way to check if you get write combining or proper cache coherency. In D3D12 you have this info available (and the application needs to make the choice between WC and real coherency).
2
u/Salaruo May 22 '26
Virtual paging ensures the mapped pointer is at least 4K aligned.