如何释放glBufferData内存 [英] How to deallocate glBufferData memory

查看:2313
本文介绍了如何释放glBufferData内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个顶点缓冲区对象类来管理我的应用程序中的许多顶点。用户调用构造函数创建一个glBuffer,并调用glBufferData来分配一个指定的空间。

I created a Vertex Buffer Object class to manage lots of vertices in my application. The user calls the constructor to create a glBuffer and calls glBufferData to allocate a specified amount of space.

有一个叫做resize的类函数,允许用户改变容量的VBO通过再次调用glBufferData。我的问题是,如何取消以前的分配?或者是自动完成吗?

There is a class function called resize that allows the user to change the capacity of the VBO by calling again the glBufferData. My question is, how do I deallocate the previous allocation? Or Is it done automatically?

glDeleteBuffer根据opengl docs,只删除缓冲区本身,而没有提到使用glBufferData分配的实际内存。
我可以在没有内存泄漏的相同绑定缓冲区上调用glBufferData吗?

glDeleteBuffer, according to the opengl docs, only deletes the buffer itself with no mention of the actual memory allocated with glBufferData. Can I keep calling glBufferData on the same bound buffer with no memory leak?

推荐答案

不能通过为同一个缓冲区对象重复调用 glBufferData()来创建内存泄漏。新分配取代了旧分配。

You can't create a memory leak by repeatedly calling glBufferData() for the same buffer object. The new allocation replaces the old one.

有一个微妙的方面,大多数时候你不需要担心,但仍然可以有用的理解:是对同一缓冲区对象暂时进行多个活动分配的可能性。这是由于OpenGL的异步性质。为了说明,图片调用顺序如下:

There's one subtle aspect that most of the time you don't need to worry about, but may still be useful to understand: There is a possibility of having multiple active allocations for the same buffer object temporarily. This happens due to the asynchronous nature of OpenGL. For illustration, picture a call sequence like this:


  1. glBufferData(dataA)

  2. glDraw()

  3. glBufferData(dataB)

  4. glDraw()

  1. glBufferData(dataA)
  2. glDraw()
  3. glBufferData(dataB)
  4. glDraw()


$ b b

当您按照此顺序对项目3进行API调用时,GPU可能尚未完成来自调用2的绘制调用。它可能实际上仍然在驱动程序中的某处排队,并且不会切换到的GPU。因为调用2依赖于 dataA ,所以在GPU完成绘图调用2之前不能删除该数据。在这种情况下, dataA

When you make the API call for item 3 in this sequence, the GPU may not yet have finished with the draw call from call 2. It may in fact still be queued up somewhere in the driver, and not handed over to the GPU yet. Since call 2 depends on dataA, that data can not be deleted until the GPU finished executing the draw call 2. In this case, the allocations for dataA and dataB temporarily exist at the same time.

正好 c> dataA 实际上被删除取决于实现。它只是不能早于GPU完成绘制调用2的时间。之后,它可以立即,基于一些垃圾收集计时器,当内存运行低或许多其他选项。

When exactly dataA is actually deleted depends on the implementation. It just can't be earlier than the time where the GPU finishes with draw call 2. After that it could be immediately, based on some garbage collection timer, when memory runs low, or many other options.

glDeleteBuffer()也会删除缓冲区内存。非常类似于上面的点,它可能不会立即发生。再次,它只能在GPU完成执行所有使用缓冲区内存的挂起操作后删除。

glDeleteBuffer() will also delete the buffer memory. Very similarly to the point above, it may not happen immediately. Again, it can only be deleted after the GPU finished executing all pending operations that use the buffer memory.

如果您不打算使用缓冲区对象, glDeleteBuffer()是最好的选择。

If you don't plan to use the buffer object anymore, calling glDeleteBuffer() is the best option.

这篇关于如何释放glBufferData内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆