|
@@ -8042,6 +8042,7 @@ var BABYLON;
|
|
// Cache
|
|
// Cache
|
|
this._internalTexturesCache = new Array();
|
|
this._internalTexturesCache = new Array();
|
|
this._boundTexturesCache = {};
|
|
this._boundTexturesCache = {};
|
|
|
|
+ this._boundTexturesOrder = new Array();
|
|
this._compiledEffects = {};
|
|
this._compiledEffects = {};
|
|
this._vertexAttribArraysEnabled = [];
|
|
this._vertexAttribArraysEnabled = [];
|
|
this._uintIndicesCurrentlySet = false;
|
|
this._uintIndicesCurrentlySet = false;
|
|
@@ -8051,6 +8052,7 @@ var BABYLON;
|
|
this._currentInstanceBuffers = new Array();
|
|
this._currentInstanceBuffers = new Array();
|
|
this._vaoRecordInProgress = false;
|
|
this._vaoRecordInProgress = false;
|
|
this._mustWipeVertexAttributes = false;
|
|
this._mustWipeVertexAttributes = false;
|
|
|
|
+ this._nextFreeTextureSlot = 0;
|
|
// Hardware supported Compressed Textures
|
|
// Hardware supported Compressed Textures
|
|
this._texturesSupported = new Array();
|
|
this._texturesSupported = new Array();
|
|
this._onVRFullScreenTriggered = function () {
|
|
this._onVRFullScreenTriggered = function () {
|
|
@@ -8068,6 +8070,7 @@ var BABYLON;
|
|
_this.setSize(_this._oldSize.width, _this._oldSize.height);
|
|
_this.setSize(_this._oldSize.width, _this._oldSize.height);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
+ this._boundUniforms = {};
|
|
var canvas = null;
|
|
var canvas = null;
|
|
Engine.Instances.push(this);
|
|
Engine.Instances.push(this);
|
|
if (!canvasOrContext) {
|
|
if (!canvasOrContext) {
|
|
@@ -8900,6 +8903,10 @@ var BABYLON;
|
|
};
|
|
};
|
|
Engine.prototype.resetTextureCache = function () {
|
|
Engine.prototype.resetTextureCache = function () {
|
|
for (var key in this._boundTexturesCache) {
|
|
for (var key in this._boundTexturesCache) {
|
|
|
|
+ var boundTexture = this._boundTexturesCache[key];
|
|
|
|
+ if (boundTexture) {
|
|
|
|
+ this._removeDesignatedSlot(boundTexture);
|
|
|
|
+ }
|
|
this._boundTexturesCache[key] = null;
|
|
this._boundTexturesCache[key] = null;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
@@ -11540,27 +11547,53 @@ var BABYLON;
|
|
var samplers = effect.getSamplers();
|
|
var samplers = effect.getSamplers();
|
|
for (var index = 0; index < samplers.length; index++) {
|
|
for (var index = 0; index < samplers.length; index++) {
|
|
var uniform = effect.getUniform(samplers[index]);
|
|
var uniform = effect.getUniform(samplers[index]);
|
|
- this._gl.uniform1i(uniform, index);
|
|
|
|
|
|
+ if (uniform) {
|
|
|
|
+ this._boundUniforms[index] = uniform;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
this._currentEffect = null;
|
|
this._currentEffect = null;
|
|
};
|
|
};
|
|
- Engine.prototype.activateTextureChannel = function (textureChannel) {
|
|
|
|
- if (this._activeTextureChannel !== textureChannel) {
|
|
|
|
- this._gl.activeTexture(textureChannel);
|
|
|
|
- this._activeTextureChannel = textureChannel;
|
|
|
|
|
|
+ Engine.prototype._activateTextureChannel = function (channel) {
|
|
|
|
+ if (this._activeChannel !== channel) {
|
|
|
|
+ this._gl.activeTexture(this._gl.TEXTURE0 + channel);
|
|
|
|
+ this._activeChannel = channel;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ Engine.prototype._removeDesignatedSlot = function (internalTexture) {
|
|
|
|
+ internalTexture._designatedSlot = -1;
|
|
|
|
+ var index = this._boundTexturesOrder.indexOf(internalTexture);
|
|
|
|
+ if (index > -1) {
|
|
|
|
+ this._boundTexturesOrder.splice(index, 1);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
Engine.prototype._bindTextureDirectly = function (target, texture) {
|
|
Engine.prototype._bindTextureDirectly = function (target, texture) {
|
|
- if (this._boundTexturesCache[this._activeTextureChannel] !== texture) {
|
|
|
|
|
|
+ var currentTextureBound = this._boundTexturesCache[this._activeChannel];
|
|
|
|
+ var isTextureForRendering = texture && texture._initialSlot > -1;
|
|
|
|
+ if (currentTextureBound !== texture) {
|
|
|
|
+ if (currentTextureBound) {
|
|
|
|
+ this._removeDesignatedSlot(currentTextureBound);
|
|
|
|
+ }
|
|
this._gl.bindTexture(target, texture ? texture._webGLTexture : null);
|
|
this._gl.bindTexture(target, texture ? texture._webGLTexture : null);
|
|
- this._boundTexturesCache[this._activeTextureChannel] = texture;
|
|
|
|
|
|
+ if (this._activeChannel >= 0) {
|
|
|
|
+ this._boundTexturesCache[this._activeChannel] = texture;
|
|
|
|
+ }
|
|
|
|
+ if (isTextureForRendering) {
|
|
|
|
+ this._boundTexturesOrder.push(texture);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (isTextureForRendering) {
|
|
|
|
+ texture._designatedSlot = this._activeChannel;
|
|
|
|
+ this._bindSamplerUniformToChannel(texture._initialSlot, this._activeChannel);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
Engine.prototype._bindTexture = function (channel, texture) {
|
|
Engine.prototype._bindTexture = function (channel, texture) {
|
|
if (channel < 0) {
|
|
if (channel < 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- this.activateTextureChannel(this._gl.TEXTURE0 + channel);
|
|
|
|
|
|
+ if (texture) {
|
|
|
|
+ channel = this._getCorrectTextureChannel(channel, texture);
|
|
|
|
+ }
|
|
|
|
+ this._activateTextureChannel(channel);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
|
|
};
|
|
};
|
|
Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
|
|
Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
|
|
@@ -11568,7 +11601,7 @@ var BABYLON;
|
|
};
|
|
};
|
|
Engine.prototype.unbindAllTextures = function () {
|
|
Engine.prototype.unbindAllTextures = function () {
|
|
for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
|
|
for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
|
|
- this.activateTextureChannel(this._gl.TEXTURE0 + channel);
|
|
|
|
|
|
+ this._activateTextureChannel(channel);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
|
|
if (this.webGLVersion > 1) {
|
|
if (this.webGLVersion > 1) {
|
|
@@ -11580,15 +11613,45 @@ var BABYLON;
|
|
if (channel < 0) {
|
|
if (channel < 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- if (this._setTexture(channel, texture)) {
|
|
|
|
- this._gl.uniform1i(uniform, channel);
|
|
|
|
|
|
+ if (uniform) {
|
|
|
|
+ this._boundUniforms[channel] = uniform;
|
|
}
|
|
}
|
|
|
|
+ this._setTexture(channel, texture);
|
|
|
|
+ };
|
|
|
|
+ Engine.prototype._getCorrectTextureChannel = function (channel, internalTexture) {
|
|
|
|
+ internalTexture._initialSlot = channel;
|
|
|
|
+ if (channel !== internalTexture._designatedSlot) {
|
|
|
|
+ if (internalTexture._designatedSlot > -1) {
|
|
|
|
+ channel = internalTexture._designatedSlot;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ if (this._boundTexturesCache[channel]) {
|
|
|
|
+ if (this._nextFreeTextureSlot > -1) {
|
|
|
|
+ channel = this._nextFreeTextureSlot;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ var oldestTexture = this._boundTexturesOrder.splice(0, 1)[0];
|
|
|
|
+ channel = oldestTexture._designatedSlot;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ internalTexture._designatedSlot = channel;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this._nextFreeTextureSlot = Math.max(channel + 1, this._nextFreeTextureSlot);
|
|
|
|
+ if (this._nextFreeTextureSlot >= this._caps.maxTexturesImageUnits) {
|
|
|
|
+ this._nextFreeTextureSlot = -1; // No more free slots, we will recycle
|
|
|
|
+ }
|
|
|
|
+ return channel;
|
|
|
|
+ };
|
|
|
|
+ Engine.prototype._bindSamplerUniformToChannel = function (sourceSlot, destination) {
|
|
|
|
+ var uniform = this._boundUniforms[sourceSlot];
|
|
|
|
+ this._gl.uniform1i(uniform, destination);
|
|
};
|
|
};
|
|
Engine.prototype._setTexture = function (channel, texture) {
|
|
Engine.prototype._setTexture = function (channel, texture) {
|
|
// Not ready?
|
|
// Not ready?
|
|
if (!texture) {
|
|
if (!texture) {
|
|
if (this._boundTexturesCache[channel] != null) {
|
|
if (this._boundTexturesCache[channel] != null) {
|
|
- this.activateTextureChannel(this._gl.TEXTURE0 + channel);
|
|
|
|
|
|
+ this._activateTextureChannel(channel);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
|
|
if (this.webGLVersion > 1) {
|
|
if (this.webGLVersion > 1) {
|
|
@@ -11600,7 +11663,7 @@ var BABYLON;
|
|
// Video
|
|
// Video
|
|
var alreadyActivated = false;
|
|
var alreadyActivated = false;
|
|
if (texture.video) {
|
|
if (texture.video) {
|
|
- this.activateTextureChannel(this._gl.TEXTURE0 + channel);
|
|
|
|
|
|
+ this._activateTextureChannel(channel);
|
|
alreadyActivated = true;
|
|
alreadyActivated = true;
|
|
texture.update();
|
|
texture.update();
|
|
}
|
|
}
|
|
@@ -11621,12 +11684,14 @@ var BABYLON;
|
|
else {
|
|
else {
|
|
internalTexture = this.emptyTexture;
|
|
internalTexture = this.emptyTexture;
|
|
}
|
|
}
|
|
- if (!alreadyActivated) {
|
|
|
|
- this.activateTextureChannel(this._gl.TEXTURE0 + channel);
|
|
|
|
- }
|
|
|
|
- if (this._boundTexturesCache[this._activeTextureChannel] === internalTexture) {
|
|
|
|
|
|
+ channel = this._getCorrectTextureChannel(channel, internalTexture);
|
|
|
|
+ if (this._boundTexturesCache[channel] === internalTexture) {
|
|
|
|
+ this._bindSamplerUniformToChannel(internalTexture._initialSlot, channel);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+ if (!alreadyActivated) {
|
|
|
|
+ this._activateTextureChannel(channel);
|
|
|
|
+ }
|
|
if (internalTexture && internalTexture.is3D) {
|
|
if (internalTexture && internalTexture.is3D) {
|
|
this._bindTextureDirectly(this._gl.TEXTURE_3D, internalTexture);
|
|
this._bindTextureDirectly(this._gl.TEXTURE_3D, internalTexture);
|
|
if (internalTexture && internalTexture._cachedWrapU !== texture.wrapU) {
|
|
if (internalTexture && internalTexture._cachedWrapU !== texture.wrapU) {
|
|
@@ -22074,6 +22139,8 @@ var BABYLON;
|
|
function InternalTexture(engine, dataSource) {
|
|
function InternalTexture(engine, dataSource) {
|
|
this.onLoadedObservable = new BABYLON.Observable();
|
|
this.onLoadedObservable = new BABYLON.Observable();
|
|
// Private
|
|
// Private
|
|
|
|
+ this._initialSlot = -1;
|
|
|
|
+ this._designatedSlot = -1;
|
|
this._dataSource = InternalTexture.DATASOURCE_UNKNOWN;
|
|
this._dataSource = InternalTexture.DATASOURCE_UNKNOWN;
|
|
this._references = 1;
|
|
this._references = 1;
|
|
this._engine = engine;
|
|
this._engine = engine;
|
|
@@ -22118,7 +22185,7 @@ var BABYLON;
|
|
proxy._swapAndDie(this);
|
|
proxy._swapAndDie(this);
|
|
return;
|
|
return;
|
|
case InternalTexture.DATASOURCE_RAW:
|
|
case InternalTexture.DATASOURCE_RAW:
|
|
- proxy = this._engine.createRawTexture(this._bufferView, this.baseWidth, this.baseHeight, this.format, this.generateMipMaps, this.invertY, this.samplingMode, this._compression, this.type);
|
|
|
|
|
|
+ proxy = this._engine.createRawTexture(this._bufferView, this.baseWidth, this.baseHeight, this.format, this.generateMipMaps, this.invertY, this.samplingMode, this._compression);
|
|
proxy._swapAndDie(this);
|
|
proxy._swapAndDie(this);
|
|
this.isReady = true;
|
|
this.isReady = true;
|
|
return;
|
|
return;
|
|
@@ -77903,8 +77970,8 @@ var BABYLON;
|
|
NullEngine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, offset, count) {
|
|
NullEngine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, offset, count) {
|
|
};
|
|
};
|
|
NullEngine.prototype._bindTextureDirectly = function (target, texture) {
|
|
NullEngine.prototype._bindTextureDirectly = function (target, texture) {
|
|
- if (this._boundTexturesCache[this._activeTextureChannel] !== texture) {
|
|
|
|
- this._boundTexturesCache[this._activeTextureChannel] = texture;
|
|
|
|
|
|
+ if (this._boundTexturesCache[this._activeChannel] !== texture) {
|
|
|
|
+ this._boundTexturesCache[this._activeChannel] = texture;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
NullEngine.prototype._bindTexture = function (channel, texture) {
|
|
NullEngine.prototype._bindTexture = function (channel, texture) {
|