Skip to content

Instantly share code, notes, and snippets.

@BoyBaykiller
Created August 22, 2024 17:42
Show Gist options
  • Select an option

  • Save BoyBaykiller/6334c26912e6d2cf0da5c7a76d341a41 to your computer and use it in GitHub Desktop.

Select an option

Save BoyBaykiller/6334c26912e6d2cf0da5c7a76d341a41 to your computer and use it in GitHub Desktop.
OpenGL MAP_READ_BIT or MAP_COHERENT_BIT cause buffer to reside in HOST memory even with ReBar/SAM

I've noticed that buffers allocated with GL_MAP_WRITE_BIT or GL_MAP_READ_BIT end up in HOST memory. Example:

uint32_t size = 1 << 22; // 4MiB
uint32_t buf;
glCreateBuffers(1, &buf);
glNamedBufferStorage(buf, size, nullptr, GL_MAP_PERSISTENT_BIT | GL_MAP_WRITE_BIT);
//glMapNamedBufferRange(buf, 0, size, GL_MAP_PERSISTENT_BIT | GL_MAP_WRITE_BIT); // not needed to reproduce

It happens with any other additional combination of flags too. What's interersting is that the memory report of a RX 5700 XT shows a 256MiB DEVICE_LOCAL_BIT | HOST_VISIBLE_BIT | HOST_COHERENT_BIT heap. So at least in theory I think it should be possible to place the buffer in DEVICE memory. This is with ReBAR/SAM disabled.

In an attempt to "fix" that behaviour I enabled ReBar/SAM. With it the above buffer actually goes into DEVICE memory! Unfortunately when either the GL_MAP_READ_BIT or GL_MAP_COHERENT_BIT flag are used its back to HOST memory. The memory report for my RX 5700 XT with ReBar/SAM enabled shows Memory Heap 1 exposing all 8GiB of VRAM with DEVICE_LOCAL_BIT | HOST_VISIBLE_BIT | HOST_COHERENT_BIT.

So my questions are:

  • With ReBAR/SAM, why does the GL_MAP_READ_BIT or GL_MAP_COHERENT_BIT flag in particular cause mapped buffers to go into HOST memory?
  • Without ReBAR/SAM, why do mapped buffers go into HOST memory?

For performance reasons it would be nice if DEVICE memory was prioritised since I did not specify GL_CLIENT_STORAGE_BIT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment