Przeglądaj źródła

Merge pull request #6824 from tschw/Renderer_misc

Clarified program deallocation + minor optimization.
Ricardo Cabello 10 lat temu
rodzic
commit
d6890f17fa
2 zmienionych plików z 25 dodań i 33 usunięć
  1. 15 25
      src/renderers/WebGLRenderer.js
  2. 10 8
      src/renderers/webgl/WebGLProgram.js

+ 15 - 25
src/renderers/WebGLRenderer.js

@@ -715,44 +715,34 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 		material.program = undefined;
 
-		// only deallocate GL program if this was the last use of shared program
-		// assumed there is only single copy of any program in the _programs list
-		// (that's how it's constructed)
+		for ( var i = 0, n = _programs.length; i !== n; ++ i ) {
 
-		var i, il, programInfo;
-		var deleteProgram = false;
-
-		for ( i = 0, il = _programs.length; i < il; i ++ ) {
-
-			programInfo = _programs[ i ];
+			var programInfo = _programs[ i ];
 
 			if ( programInfo.program === program ) {
 
-				programInfo.usedTimes --;
-
-				if ( programInfo.usedTimes === 0 ) {
-
-					deleteProgram = true;
-
-				}
+				var newReferenceCount = -- programInfo.usedTimes;
 
-				break;
+				if ( newReferenceCount === 0 ) {
 
-			}
+					// the last meterial that has been using the program let
+					// go of it, so remove it from the (unordered) _programs
+					// set and deallocate the GL resource
 
-		}
+					var newLength = n - 1;
 
-		if ( deleteProgram === true ) {
+					_programs[ i ] = _programs[ newLength ];
+					_programs.pop();
 
-			// avoid using array.splice, instead replace with last and pop
+					_gl.deleteProgram( program );
 
-			_programs[ i ] = _programs[ il - 1 ];
-			_programs.pop();
+					_infoMemory.programs = newLength;
 
+				}
 
-			_gl.deleteProgram( program );
+				break;
 
-			_infoMemory.programs --;
+			}
 
 		}
 

+ 10 - 8
src/renderers/webgl/WebGLProgram.js

@@ -436,28 +436,30 @@ THREE.WebGLProgram = ( function () {
 
 		// set up caching for uniform locations
 
-		var getUniforms = function() { return this._cachedUniforms; };
+		var _cachedUniforms;
+
+		var getUniforms = function() { return _cachedUniforms; };
 
 		this.getUniforms = function() {
 
 			// fetch, cache, and next time just use a dumb accessor
-			var uniforms = fetchUniformLocations( gl, program );
-			this._cachedUniforms = uniforms;
+			_cachedUniforms = fetchUniformLocations( gl, program );
 			this.getUniforms = getUniforms;
-			return uniforms;
+			return _cachedUniforms;
 
 		};
 
 		// set up caching for attribute locations
 
-		var getAttributes = function() { return this._cachedAttributes; };
+		var _cachedAttributes;
+
+		var getAttributes = function() { return _cachedAttributes; };
 
 		this.getAttributes = function() {
 
-			var attributes = fetchAttributeLocations( gl, program );
-			this._cachedAttributes = attributes;
+			_cachedAttributes = fetchAttributeLocations( gl, program );
 			this.getAttributes = getAttributes;
-			return attributes;
+			return _cachedAttributes;
 
 		};