3

When using glTexImage2D to attach textures to a Framebuffer Object (FBO), i recognized that the memory usage goes up in RAM. It looks like textures are allocated by OpenGL not only on the GPU but also in main memory. Why is that happening?
Does it have any use?
Can memory allocation in main memory be prevented?

I am using the following GL-calls to attach a texture to an FBO:

auto internalformat = GL_RGBA32F;
auto format = GL_RGBA;
auto type = GL_FLOAT;
auto w = 1920;
auto h = 1080;

GLuint handle;
glGenTextures(1, &handle);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, w, h, 0, format, type, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0);

The same happens when using glTexStorage2D like follows (instead of the glTexImage2D call):

glTexStorage2D(GL_TEXTURE_2D, 1, internalformat, w, h);

Why is this happening? I can't see why memory must be created in RAM and not only on the GPU. Are there better/other approaches?

j00hi
  • 5,420
  • 3
  • 45
  • 82
  • Have you checked your video ram? You can use a GPU Z or something similar for this propose. You vide accelerator may share CPU memory, on some integrated video-cards like Intel or integrated AMD for laptops. – Victor Gubin May 29 '18 at 11:44
  • see [OpenGL obtaining GPU memory info nVidia,AMD(ATI)](https://stackoverflow.com/a/50385807/2521214) may be the info during runtime will shine some light on the matter. Btw the memory could be some kind of buffer between OpenGL and OS but I am just guessing ... does it decrease on new texture with less resolution? – Spektre May 30 '18 at 08:07
  • @VictorGubin Didn't check GPU memory yet, but will do. However, I doubt that shared CPU memory is used, because I've tested on a GeForce GTX 1060. – j00hi May 30 '18 at 10:29
  • @Spektre Yes, the memory consumption is proportional to the texture format and size. I have tested with a framebuffer with 8 texture attachments, switching the format from GL_RGB8 to GL_RGBA32F makes a huge difference in the main memory! Why are all textures also allocated in main memory? I don't get it. – j00hi May 30 '18 at 10:34
  • @j00hi you misunderstood I was thinking more like: use `glTexImage2D` with high resolution check the memory and then use it again with lower resolution (for different texture/FBO) and check memory again. If the consumption lowers then it is most likely some temporary buffer to speedup the api... (leaving last CPU->GPU transfer in CPU side memory) – Spektre May 30 '18 at 10:50
  • I have the same "problem". I'm on NVIDIA GeForce GTX 1050 and OpenGL allocates memory on the CPU when I call glTexStorage2D and glTexSubImage2D. This makes my game allocate over 300 MB when it should allocate only ~180 MB. – Simon-Teodor Mărăcine Dec 23 '21 at 12:45

0 Answers0