Skip to content

Commit

Permalink
Fixed texture allocation with subsequent render calls with the same p…
Browse files Browse the repository at this point in the history
…rogram
  • Loading branch information
Benjamin-Dobell committed Feb 8, 2017
1 parent 87607d8 commit 36d2992
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function WebGLRenderer( parameters ) {

//

_textureUnitMap = {},
_usedTextureUnits = 0,

//
Expand Down Expand Up @@ -1722,8 +1723,6 @@ function WebGLRenderer( parameters ) {

function setProgram( camera, fog, material, object ) {

_usedTextureUnits = 0;

var materialProperties = properties.get( material );

if ( _clippingEnabled ) {
Expand Down Expand Up @@ -1789,6 +1788,9 @@ function WebGLRenderer( parameters ) {
_gl.useProgram( program.program );
_currentProgram = program.id;

_usedTextureUnits = 0;
_textureUnitMap = {};

refreshProgram = true;
refreshMaterial = true;
refreshLights = true;
Expand Down Expand Up @@ -2545,17 +2547,21 @@ function WebGLRenderer( parameters ) {

// Textures

function allocTextureUnit() {
function allocTextureUnit( identifier ) {

var textureUnit = _usedTextureUnits;
var textureUnit = _textureUnitMap[ identifier ];

if ( textureUnit >= capabilities.maxTextures ) {
if ( textureUnit === undefined ) {

console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );
textureUnit = _textureUnitMap[ identifier ] = ++ _usedTextureUnits;

}
if ( textureUnit >= capabilities.maxTextures ) {

console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + capabilities.maxTextures );

_usedTextureUnits += 1;
}

}

return textureUnit;

Expand Down
16 changes: 10 additions & 6 deletions src/renderers/webgl/WebGLUniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import { CubeTexture } from '../../textures/CubeTexture';
import { Texture } from '../../textures/Texture';
import { _Math } from '../../math/Math';

var emptyTexture = new Texture();
var emptyCubeTexture = new CubeTexture();
Expand Down Expand Up @@ -109,7 +110,7 @@ function flatten( array, nBlocks, blockSize ) {

// Texture unit allocation

function allocTexUnits( renderer, n ) {
function allocTexUnits( renderer, uuid, n ) {

var r = arrayCacheI32[ n ];

Expand All @@ -121,7 +122,7 @@ function allocTexUnits( renderer, n ) {
}

for ( var i = 0; i !== n; ++ i )
r[ i ] = renderer.allocTextureUnit();
r[ i ] = renderer.allocTextureUnit( uuid + n );

return r;

Expand Down Expand Up @@ -188,15 +189,15 @@ function setValue4fm( gl, v ) {

function setValueT1( gl, v, renderer ) {

var unit = renderer.allocTextureUnit();
var unit = renderer.allocTextureUnit( this.uuid );
gl.uniform1i( this.addr, unit );
renderer.setTexture2D( v || emptyTexture, unit );

}

function setValueT6( gl, v, renderer ) {

var unit = renderer.allocTextureUnit();
var unit = renderer.allocTextureUnit( this.uuid );
gl.uniform1i( this.addr, unit );
renderer.setTextureCube( v || emptyCubeTexture, unit );

Expand Down Expand Up @@ -285,7 +286,7 @@ function setValueM4a( gl, v ) {
function setValueT1a( gl, v, renderer ) {

var n = v.length,
units = allocTexUnits( renderer, n );
units = allocTexUnits( renderer, this.uuid, n );

gl.uniform1iv( this.addr, units );

Expand All @@ -300,7 +301,7 @@ function setValueT1a( gl, v, renderer ) {
function setValueT6a( gl, v, renderer ) {

var n = v.length,
units = allocTexUnits( renderer, n );
units = allocTexUnits( renderer, this.uuid, n );

gl.uniform1iv( this.addr, units );

Expand Down Expand Up @@ -343,6 +344,7 @@ function getPureArraySetter( type ) {

function SingleUniform( id, activeInfo, addr ) {

this.uuid = _Math.generateUUID();
this.id = id;
this.addr = addr;
this.setValue = getSingularSetter( activeInfo.type );
Expand All @@ -353,6 +355,7 @@ function SingleUniform( id, activeInfo, addr ) {

function PureArrayUniform( id, activeInfo, addr ) {

this.uuid = _Math.generateUUID();
this.id = id;
this.addr = addr;
this.size = activeInfo.size;
Expand All @@ -364,6 +367,7 @@ function PureArrayUniform( id, activeInfo, addr ) {

function StructuredUniform( id ) {

this.uuid = _Math.generateUUID();
this.id = id;

UniformContainer.call( this ); // mix-in
Expand Down

0 comments on commit 36d2992

Please sign in to comment.