Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit 038c01a

Browse files
committed
Clean up VBOs when we remove actors.
Eventually, we will run out of Vertex Buffer Objects. Now, it is possible for the GPU to reuse them. Some sources cite that it is poor practice to allocated and deallocate VBOs (though much better than never deallocating them), and it would be better to maintain a reference to them and reuse them instead. Also, cache the depthBits parameter. Getting WebGL parameters takes some time, and if we know they haven't changed, we can cache the values to get a minor speed improvement.
1 parent 130b056 commit 038c01a

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

src/mapper.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,26 @@ vgl.mapper = function (arg) {
3232
m_bufferVertexAttributeMap = {},
3333
m_dynamicDraw = arg.dynamicDraw === undefined ? false : arg.dynamicDraw,
3434
m_glCompileTimestamp = vgl.timestamp(),
35-
m_context = null;
35+
m_context = null,
36+
m_this = this;
3637

3738
////////////////////////////////////////////////////////////////////////////
3839
/**
3940
* Delete cached VBO if any
40-
*
41-
* @private
4241
*/
4342
////////////////////////////////////////////////////////////////////////////
44-
function deleteVertexBufferObjects(renderState) {
43+
this.deleteVertexBufferObjects = function (renderState) {
4544
var i;
46-
for (i = 0; i < m_buffers.length; i += 1) {
47-
renderState.m_context.deleteBuffer(m_buffers[i]);
45+
var context = m_context;
46+
if (renderState) {
47+
context = renderState.m_context;
4848
}
49-
}
49+
if (context) {
50+
for (i = 0; i < m_buffers.length; i += 1) {
51+
context.deleteBuffer(m_buffers[i]);
52+
}
53+
}
54+
};
5055

5156
////////////////////////////////////////////////////////////////////////////
5257
/**
@@ -121,7 +126,7 @@ vgl.mapper = function (arg) {
121126
////////////////////////////////////////////////////////////////////////////
122127
function setupDrawObjects(renderState) {
123128
// Delete buffer objects from past if any.
124-
deleteVertexBufferObjects(renderState);
129+
m_this.deleteVertexBufferObjects(renderState);
125130

126131
// Clear any cache related to buffers
127132
cleanUpDrawObjects(renderState);

src/renderer.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ vgl.renderer = function (arg) {
203203
renSt = new vgl.renderState();
204204
renSt.m_renderer = m_this;
205205
renSt.m_context = m_this.renderWindow().context();
206-
m_this.m_depthBits = renSt.m_context.getParameter(vgl.GL.DEPTH_BITS);
206+
if (!m_this.m_depthBits || m_this.m_contextChanged) {
207+
m_this.m_depthBits = renSt.m_context.getParameter(vgl.GL.DEPTH_BITS);
208+
}
207209
renSt.m_contextChanged = m_this.m_contextChanged;
208210

209211
if (m_this.m_renderPasses) {
@@ -558,6 +560,12 @@ vgl.renderer = function (arg) {
558560
////////////////////////////////////////////////////////////////////////////
559561
this.removeActor = function (actor) {
560562
if (m_this.m_sceneRoot.children().indexOf(actor) !== -1) {
563+
/* When we remove an actor, free the VBOs of the mapper and mark the
564+
* mapper as modified; it will reallocate VBOs as necessary. */
565+
if (actor.mapper()) {
566+
actor.mapper().deleteVertexBufferObjects();
567+
actor.mapper().modified();
568+
}
561569
m_this.m_sceneRoot.removeChild(actor);
562570
m_this.modified();
563571
return true;

0 commit comments

Comments
 (0)