Skip to content

Commit

Permalink
Discard attribute typed arrays for buffered geometries that are not m…
Browse files Browse the repository at this point in the history
…odified after initial rendering (#9512)

* add setDiscardBuffer method to BufferGeometry

* added discard support to BufferAttribute

* add mechanism for discard of BufferAttribute TypedArrays

* use more elegant method for creating dummy typed array.

* fix typo

* Update BufferGeometry.js

fix brain fade

* rework to use callbacks (phase 1)

* rework part 2

* remove build file

* support setting onUploadCallback from Geometry

* remove repeated calculation from renderer

* remove now redundant getter

* remove geoemtry interface

* document discard mechanism.

* merge fixes

* restore return.this

* drop unneeded call()

* rename discard() method to disposeArray()
  • Loading branch information
aardgoose authored and mrdoob committed Nov 5, 2016
1 parent d0b259b commit 629195b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/api/core/BufferAttribute.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ <h3>[property:Integer version]</h3>
A version number, incremented every time the needsUpdate property is set to true.
</div>

<h3>[property:Function onUploadCallback]</h3>
<div>
A callback function that is executed after the Renderer has transfered the attribute array data to the GPU.
The callback is executed with a single parameter "name", the name of the buffer attribute, and "this" set to the BufferAttribute object.
</div>

<h2>Methods</h2>

Expand Down Expand Up @@ -108,6 +113,12 @@ <h3>[method:BufferAttribute clone]() </h3>
Copies this attribute.
</div>

<h3>[method:null disposeArray]() </h3>
<div>
Discards the typed array containing the attribute data. This can be executed after the data has been transfered to the GPU to allow the memory to be reclaimed.
For use where the attribute will not be changed after the discard method is called.
</div>

<h2>Source</h2>

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
Expand Down
9 changes: 9 additions & 0 deletions src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function BufferAttribute( array, itemSize, normalized ) {
this.updateRange = { offset: 0, count: - 1 };

this.version = 0;
this.onUploadCallback = null;

}

Expand Down Expand Up @@ -323,6 +324,14 @@ BufferAttribute.prototype = {

return new this.constructor().copy( this );

},

disposeArray: function () {

var oldArray = this.array;

this.array = new oldArray.constructor( 1 ); // create dummy minimal length TypedArray

}

};
Expand Down
14 changes: 10 additions & 4 deletions src/renderers/webgl/WebGLObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function WebGLObjects( gl, properties, info ) {

for ( var name in attributes ) {

updateAttribute( attributes[ name ], gl.ARRAY_BUFFER );
updateAttribute( attributes[ name ], gl.ARRAY_BUFFER, name );

}

Expand All @@ -58,15 +58,15 @@ function WebGLObjects( gl, properties, info ) {

}

function updateAttribute( attribute, bufferType ) {
function updateAttribute( attribute, bufferType, name ) {

var data = ( attribute.isInterleavedBufferAttribute ) ? attribute.data : attribute;

var attributeProperties = properties.get( data );

if ( attributeProperties.__webglBuffer === undefined ) {

createBuffer( attributeProperties, data, bufferType );
createBuffer( attributeProperties, data, bufferType, name );

} else if ( attributeProperties.version !== data.version ) {

Expand All @@ -76,7 +76,7 @@ function WebGLObjects( gl, properties, info ) {

}

function createBuffer( attributeProperties, data, bufferType ) {
function createBuffer( attributeProperties, data, bufferType, name ) {

attributeProperties.__webglBuffer = gl.createBuffer();
gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
Expand Down Expand Up @@ -126,6 +126,12 @@ function WebGLObjects( gl, properties, info ) {
attributeProperties.type = type;
attributeProperties.version = data.version;

if ( data.onUploadCallback ) {

data.onUploadCallback( name );

}

}

function updateBuffer( attributeProperties, data, bufferType ) {
Expand Down

0 comments on commit 629195b

Please sign in to comment.