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

Commit

Permalink
Improve cleanup.
Browse files Browse the repository at this point in the history
When removing an actor, cleanup after it so it can be moved to another
renderer.
  • Loading branch information
manthey committed Dec 18, 2018
1 parent 66ebfc2 commit d132b40
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
!/vgl.js
!/vgl.min.js
!/vgl.min.js.map
!src/**/*
.eslintcache
6 changes: 6 additions & 0 deletions src/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ vgl.mapper = function (arg) {
}
};

this._cleanup = function (renderState) {
m_this.deleteVertexBufferObjects(renderState);
cleanUpDrawObjects(renderState);
m_this.modified();
};

////////////////////////////////////////////////////////////////////////////
/**
* Create new VBO for all its geometryData sources and primitives
Expand Down
2 changes: 2 additions & 0 deletions src/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ vgl.material = function () {
m_textureAttributes[key]._cleanup(renderState);
}
}
m_shaderProgram._cleanup(renderState);
m_this.modified();
};

////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/renderWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ vgl.renderWindow = function (canvas) {
m_renderers[i]._cleanup(renderState);
}
vgl.clearCachedShaders(renderState ? renderState.m_context : null);
m_this.modified();
};

////////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 10 additions & 3 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ vgl.renderer = function (arg) {

renSt.m_context.finish();
m_this.m_contextChanged = false;
m_this.m_lastRenderState = renSt;
};

////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -561,10 +562,15 @@ vgl.renderer = function (arg) {
if (m_this.m_sceneRoot.children().indexOf(actor) !== -1) {
/* When we remove an actor, free the VBOs of the mapper and mark the
* mapper as modified; it will reallocate VBOs as necessary. */
if (actor.mapper()) {
actor.mapper().deleteVertexBufferObjects();
actor.mapper().modified();
if (m_this.m_lastRenderState) {
if (actor.mapper()) {
actor.mapper()._cleanup(m_this.m_lastRenderState);
}
if (actor.material()) {
actor.material()._cleanup(m_this.m_lastRenderState);
}
}
actor.modified();
m_this.m_sceneRoot.removeChild(actor);
m_this.modified();
return true;
Expand Down Expand Up @@ -785,6 +791,7 @@ vgl.renderer = function (arg) {
}

m_this.m_sceneRoot.removeChildren();
m_this.modified();
};

return m_this;
Expand Down
9 changes: 7 additions & 2 deletions src/shaderProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ vgl.shaderProgram = function () {
this._cleanup = function (renderState) {
m_this.deleteVertexAndFragment(renderState);
m_this.deleteProgram(renderState);
m_this.modified();
};

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -284,7 +285,9 @@ vgl.shaderProgram = function () {
*/
/////////////////////////////////////////////////////////////////////////////
this.deleteProgram = function (renderState) {
renderState.m_context.deleteProgram(m_programHandle);
if (m_programHandle) {
renderState.m_context.deleteProgram(m_programHandle);
}
m_programHandle = 0;
};

Expand All @@ -296,7 +299,9 @@ vgl.shaderProgram = function () {
this.deleteVertexAndFragment = function (renderState) {
var i;
for (i = 0; i < m_shaders.length; i += 1) {
renderState.m_context.detachShader(m_shaders[i].shaderHandle(renderState));
if (m_shaders[i].shaderHandle(renderState)) {
renderState.m_context.detachShader(m_programHandle, m_shaders[i].shaderHandle(renderState));
}
renderState.m_context.deleteShader(m_shaders[i].shaderHandle(renderState));
m_shaders[i].removeContext(renderState);
}
Expand Down
2 changes: 2 additions & 0 deletions testing/cases/phantomjs/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ describe('vgl.material', function () {
mat.addAttribute(blend);
mat.addAttribute(tex);
mat.setAttribute(prog);
mat.bind(renderState);
mat.undoBind(renderState);
glCounts = $.extend({}, vgl.mockCounts());
mat._cleanup(renderState);
expect(vgl.mockCounts().deleteProgram).toBe((glCounts.deleteProgram || 0) + 1);
Expand Down
1 change: 1 addition & 0 deletions testing/cases/phantomjs/shaderProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('vgl.shaderProgram', function () {
expect(vgl.mockCounts().useProgram).toBe((glCounts.useProgram || 0) + 1);
});
it('deleteProgram', function () {
sp._setup(renderState);
glCounts = $.extend({}, vgl.mockCounts());
sp.deleteProgram(renderState);
expect(vgl.mockCounts().deleteProgram).toBe((glCounts.deleteProgram || 0) + 1);
Expand Down

0 comments on commit d132b40

Please sign in to comment.