Skip to content

Commit

Permalink
Merge pull request #7230 from benaadams/instancing-reset
Browse files Browse the repository at this point in the history
Instancing reset
  • Loading branch information
mrdoob committed Sep 24, 2015
2 parents 7072a5d + 5626611 commit e9bd4a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,6 @@ THREE.WebGLRenderer = function ( parameters ) {

if ( geometryAttribute !== undefined ) {

state.enableAttribute( programAttribute );

var size = geometryAttribute.itemSize;
var buffer = objects.getAttributeBuffer( geometryAttribute );

Expand All @@ -921,52 +919,46 @@ THREE.WebGLRenderer = function ( parameters ) {
var stride = data.stride;
var offset = geometryAttribute.offset;

_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );

if ( data instanceof THREE.InstancedInterleavedBuffer ) {

if ( extension === null ) {

console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
return;

}

extension.vertexAttribDivisorANGLE( programAttribute, data.meshPerAttribute );
state.enableAttributeAndDivisor( programAttribute, data.meshPerAttribute, extension );

if ( geometry.maxInstancedCount === undefined ) {

geometry.maxInstancedCount = data.meshPerAttribute * data.count;

}

}

} else {
} else {

_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32
state.enableAttribute( programAttribute );

if ( geometryAttribute instanceof THREE.InstancedBufferAttribute ) {
}

if ( extension === null ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );

console.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
return;
} else {

}
if ( geometryAttribute instanceof THREE.InstancedBufferAttribute ) {

extension.vertexAttribDivisorANGLE( programAttribute, geometryAttribute.meshPerAttribute );
state.enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute, extension );

if ( geometry.maxInstancedCount === undefined ) {

geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * geometryAttribute.count;

}

} else {

state.enableAttribute( programAttribute );

}

_gl.bindBuffer( _gl.ARRAY_BUFFER, buffer );
_gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32

}

} else if ( materialDefaultAttributeValues !== undefined ) {
Expand Down
30 changes: 30 additions & 0 deletions src/renderers/webgl/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ THREE.WebGLState = function ( gl, extensions, paramThreeToGL ) {

var newAttributes = new Uint8Array( 16 );
var enabledAttributes = new Uint8Array( 16 );
var attributeDivisors = new Uint8Array( 16 );

var capabilities = {};

Expand Down Expand Up @@ -78,6 +79,35 @@ THREE.WebGLState = function ( gl, extensions, paramThreeToGL ) {

}

if ( attributeDivisors[ attribute ] !== 0 ) {

var extension = extensions.get( 'ANGLE_instanced_arrays' );

extension.vertexAttribDivisorANGLE( attribute, 0 );
attributeDivisors[ attribute ] = 0;

}

};

this.enableAttributeAndDivisor = function ( attribute, meshPerAttribute, extension ) {

newAttributes[ attribute ] = 1;

if ( enabledAttributes[ attribute ] === 0 ) {

gl.enableVertexAttribArray( attribute );
enabledAttributes[ attribute ] = 1;

}

if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {

extension.vertexAttribDivisorANGLE( attribute, meshPerAttribute );
attributeDivisors[ attribute ] = meshPerAttribute;

}

};

this.disableUnusedAttributes = function () {
Expand Down

0 comments on commit e9bd4a1

Please sign in to comment.