1
\$\begingroup\$

I am interested in understanding a bit more behind how OpenGL does its memory management and what are some good practices before I start heavily coding and back myself into a corner.

The real question I have is regarding memory. I have a class called Mesh that consists of a pointer to some vertex data, a pointer to the face data, as well as the number of vertices and faces. I am familiar with one possible process that OpenGL uses to get these drawn on the screen: generate a buffer for vertices and a buffer for faces, bind them as GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER, call glBufferData to move the data to the buffers, and lastly call glDrawElements to draw them. From here I can understand how I can load several Mesh objects and draw them on screen, and I have no issue there.

My question then lies in where the memory is stored and what I should be doing in order to conserve memory. My assumption is that after the call to glBufferData, the data is copied to the GPU (or to some other register to later be copied to the GPU). Is it safe for me to delete the original Mesh object that was used with the buffer calls? In my head, the mesh data is retained inside the GPU so the data is not lost, and so long as I do not need to reload this data using OpenGL later then I won't miss this data at any point. Also in my head, this frees up RAM space for other uses, so my Mesh, in some sense, now totally lives in GPU memory and nowhere else, and I can do my drawing solely by making OpenGL calls to the correct buffers (so I obviously need to save the generated buffer IDs, but that part goes without saying).

If someone could tell me if this is correct/incorrect logic, as well as good/bad game dev practice?

\$\endgroup\$
2
\$\begingroup\$

We all started once. Let me suggest to get the OpenGL Pogramming Guide 9th Edition, aka the "Red Book". It's worth so much more than any internet tutorial, especially in the beginning.

Yes, after calling glBufferData() the data has been passed to the video driver and been copied to VRAM (to be exact, where exactly it resides depends on a few settings) and can be deleted from your application's memory if it is not needed elsewhere.

And yes you must store the buffer names ("ids"). They are the handles to the OpenGL objects and you need them everywhere, including deletion when they are no longer needed.

\$\endgroup\$
0

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.