Skip to content

Commit

Permalink
WebGLObjects and WebGLGeometries attributes code moved to WebGLAttrib…
Browse files Browse the repository at this point in the history
…utes.
  • Loading branch information
mrdoob committed Feb 22, 2017
1 parent be72fcd commit 380de1a
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 231 deletions.
37 changes: 20 additions & 17 deletions src/renderers/WebGLRenderer.js
Expand Up @@ -13,6 +13,7 @@ import { PlaneBufferGeometry } from '../geometries/PlaneGeometry';
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial';
import { PerspectiveCamera } from '../cameras/PerspectiveCamera';
import { OrthographicCamera } from '../cameras/OrthographicCamera';
import { WebGLAttributes } from './webgl/WebGLAttributes';
import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer';
import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer';
import { WebGLLights } from './webgl/WebGLLights';
Expand Down Expand Up @@ -220,7 +221,7 @@ function WebGLRenderer( parameters ) {

try {

var attributes = {
var contextAttributes = {
alpha: _alpha,
depth: _depth,
stencil: _stencil,
Expand All @@ -229,7 +230,7 @@ function WebGLRenderer( parameters ) {
preserveDrawingBuffer: _preserveDrawingBuffer
};

_gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
_gl = _context || _canvas.getContext( 'webgl', contextAttributes ) || _canvas.getContext( 'experimental-webgl', contextAttributes );

if ( _gl === null ) {

Expand Down Expand Up @@ -284,9 +285,11 @@ function WebGLRenderer( parameters ) {
var capabilities = new WebGLCapabilities( _gl, extensions, parameters );

var state = new WebGLState( _gl, extensions, paramThreeToGL );

var properties = new WebGLProperties();
var textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, paramThreeToGL, this.info );
var objects = new WebGLObjects( _gl, properties, this.info );
var attributes = new WebGLAttributes( _gl, properties );
var objects = new WebGLObjects( _gl, attributes, properties, this.info );
var programCache = new WebGLPrograms( this, capabilities );
var lightCache = new WebGLLights();

Expand Down Expand Up @@ -558,7 +561,7 @@ function WebGLRenderer( parameters ) {

releaseMaterialProgramReference( material );

properties.delete( material );
properties.remove( material );

}

Expand Down Expand Up @@ -600,15 +603,15 @@ function WebGLRenderer( parameters ) {
if ( object.hasUvs && ! buffers.uv ) buffers.uv = _gl.createBuffer();
if ( object.hasColors && ! buffers.color ) buffers.color = _gl.createBuffer();

var attributes = program.getAttributes();
var programAttributes = program.getAttributes();

if ( object.hasPositions ) {

_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position );
_gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );

state.enableAttribute( attributes.position );
_gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
state.enableAttribute( programAttributes.position );
_gl.vertexAttribPointer( programAttributes.position, 3, _gl.FLOAT, false, 0, 0 );

}

Expand Down Expand Up @@ -647,9 +650,9 @@ function WebGLRenderer( parameters ) {

_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );

state.enableAttribute( attributes.normal );
state.enableAttribute( programAttributes.normal );

_gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
_gl.vertexAttribPointer( programAttributes.normal, 3, _gl.FLOAT, false, 0, 0 );

}

Expand All @@ -658,7 +661,7 @@ function WebGLRenderer( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );

state.enableAttribute( attributes.uv );
state.enableAttribute( programAttributes.uv );

_gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );

Expand All @@ -669,9 +672,9 @@ function WebGLRenderer( parameters ) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );

state.enableAttribute( attributes.color );
state.enableAttribute( programAttributes.color );

_gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
_gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 );

}

Expand Down Expand Up @@ -790,7 +793,7 @@ function WebGLRenderer( parameters ) {

if ( index !== null ) {

_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, objects.getAttributeBuffer( index ) );
_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attributes.get( index ).__webglBuffer );

}

Expand Down Expand Up @@ -937,7 +940,7 @@ function WebGLRenderer( parameters ) {
var normalized = geometryAttribute.normalized;
var size = geometryAttribute.itemSize;

var attributeProperties = objects.getAttributeProperties( geometryAttribute );
var attributeProperties = attributes.get( geometryAttribute );

var buffer = attributeProperties.__webglBuffer;
var type = attributeProperties.type;
Expand Down Expand Up @@ -1579,15 +1582,15 @@ function WebGLRenderer( parameters ) {

}

var attributes = program.getAttributes();
var programAttributes = program.getAttributes();

if ( material.morphTargets ) {

material.numSupportedMorphTargets = 0;

for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {

if ( attributes[ 'morphTarget' + i ] >= 0 ) {
if ( programAttributes[ 'morphTarget' + i ] >= 0 ) {

material.numSupportedMorphTargets ++;

Expand All @@ -1603,7 +1606,7 @@ function WebGLRenderer( parameters ) {

for ( var i = 0; i < _this.maxMorphNormals; i ++ ) {

if ( attributes[ 'morphNormal' + i ] >= 0 ) {
if ( programAttributes[ 'morphNormal' + i ] >= 0 ) {

material.numSupportedMorphNormals ++;

Expand Down
148 changes: 148 additions & 0 deletions src/renderers/webgl/WebGLAttributes.js
@@ -0,0 +1,148 @@
/**
* @author mrdoob / http://mrdoob.com/
*/

function WebGLAttributes( gl, properties ) {

function createBuffer( attributeProperties, data, bufferType ) {

attributeProperties.__webglBuffer = gl.createBuffer();
gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );

var usage = data.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;

gl.bufferData( bufferType, data.array, usage );

var type = gl.FLOAT;
var array = data.array;

if ( array instanceof Float32Array ) {

type = gl.FLOAT;

} else if ( array instanceof Float64Array ) {

console.warn( "Unsupported data buffer format: Float64Array" );

} else if ( array instanceof Uint16Array ) {

type = gl.UNSIGNED_SHORT;

} else if ( array instanceof Int16Array ) {

type = gl.SHORT;

} else if ( array instanceof Uint32Array ) {

type = gl.UNSIGNED_INT;

} else if ( array instanceof Int32Array ) {

type = gl.INT;

} else if ( array instanceof Int8Array ) {

type = gl.BYTE;

} else if ( array instanceof Uint8Array ) {

type = gl.UNSIGNED_BYTE;

}

attributeProperties.bytesPerElement = array.BYTES_PER_ELEMENT;
attributeProperties.type = type;
attributeProperties.version = data.version;

data.onUploadCallback();

}

function updateBuffer( attributeProperties, data, bufferType ) {

gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );

if ( data.dynamic === false ) {

gl.bufferData( bufferType, data.array, gl.STATIC_DRAW );

} else if ( data.updateRange.count === - 1 ) {

// Not using update ranges

gl.bufferSubData( bufferType, 0, data.array );

} else if ( data.updateRange.count === 0 ) {

console.error( 'THREE.WebGLObjects.updateBuffer: dynamic THREE.BufferAttribute marked as needsUpdate but updateRange.count is 0, ensure you are using set methods or updating manually.' );

} else {

gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );

data.updateRange.count = 0; // reset range

}

attributeProperties.version = data.version;

}

//

function get( attribute ) {

if ( attribute.isInterleavedBufferAttribute ) {

return properties.get( attribute.data );

}

return properties.get( attribute );

}

function remove( attribute ) {

var attributeProperties = get( attribute );

if ( attributeProperties.__webglBuffer !== undefined ) {

gl.deleteBuffer( attributeProperties.__webglBuffer );
properties.remove( attributeProperties );

}

}

function update( attribute, bufferType ) {

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

var attributeProperties = properties.get( data );

if ( attributeProperties.__webglBuffer === undefined ) {

createBuffer( attributeProperties, data, bufferType );

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

updateBuffer( attributeProperties, data, bufferType );

}

}

return {

get: get,
remove: remove,
update: update

};

}


export { WebGLAttributes };

0 comments on commit 380de1a

Please sign in to comment.