OpenGL: how to untie and remove buffers correctly

I studied opengl and wrote a simple program that uses a buffer object (colors, positions, normals, element indices) to draw a shape as shown below. Even when uninstalling and expanding, the GPU monitor program shows an increase in memory usage after each button press. Memory is freed only when the application is closed. If I press the button repeatedly, the GPU tuner fills up to 2 GB and starts to multiply to accommodate RAM. How can I free resources / buffers correctly?

enter image description here

The program stream is similar:

When I click a button, the below program is executed: glControl1.MakeCurrent(); GL.GenBuffers(4,buf) GL.BindBuffer(BufferTarget.ArrayBuffer,buf[0]) GL.BufferData(BufferTarget.ArrayBuffer,......) same for buf[1] buf[2] and buf[3](this one ElementArrayBuffer) Console.WriteLine(GL.GetError()); // no error GL.Finish(); [iteration loop] glControl1.MakeCurrent(); GL.BindBuffer(BufferTarget.ArrayBuffer,buf[0]) GL.ColorBuffer(....) GL.BindBuffer(BufferTarget.ArrayBuffer,buf[1]) GL.VertexBuffer(....) GL.BindBuffer(BufferTarget.ArrayBuffer,buf[2]) GL.NormalBuffer(....) GL.BindBuffer(BufferTarget.ElementArrayBuffer,buf[3]) GL.EnableClientStates(.....) GL.DrawElements(.....) //draws the thing GL.DisableClientStates(.....) GL.BindBuffer(BufferTarget.ElementArrayBuffer,0) //is this unbinding? GL.BindBuffer(BufferTarget.ArrayBuffer,0) GL.BindBuffer(BufferTarget.ArrayBuffer,0) GL.BindBuffer(BufferTarget.ArrayBuffer,0) GL.Finish(); [loop end] //deleting buffers glControl1.MakeCurrent() GL.DeleteBuffers(4,buf) Console.WriteLine(GL.GetError()); // no error comes GL.Finish(); 
+4
source share
1 answer

As far as I can tell, you create and populate a new buffer when you click, not delete them. In the end, you only delete the 4 buffers that you created LAST.

When you call glGenBuffers , it just generates a new buffer and returns a descriptor, it does not determine if this descriptor is used or something like that. Thus, when you give the same pens as before, it generates new descriptors, overwrites the ones you are currently tracking, effectively deleting any link to the old buffers. But he does not free these buffers. This way you constantly fill up more memory.

Now, if you just want to write new data to existing buffers, you simply call glBufferSubData . If you want to change the size of the buffer, you call glBufferData , which deletes the previous data. But you do not need to create new buffers, since you already have buffer descriptors.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1496806/


All Articles