David Catuhe 7 vuotta sitten
vanhempi
commit
23246d50ac

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 1529 - 1526
dist/preview release/babylon.d.ts


+ 43 - 8
src/Engine/babylon.engine.ts

@@ -749,7 +749,7 @@
         // Cache
         private _internalTexturesCache = new Array<InternalTexture>();
         protected _activeTextureChannel: number;
-        protected _activeTexturesCache: { [key: string]: Nullable<WebGLTexture> } = {};
+        protected _boundTexturesCache: { [key: string]: Nullable<InternalTexture> } = {};
         protected _currentEffect: Nullable<Effect>;
         protected _currentProgram: Nullable<WebGLProgram>;
         private _compiledEffects: { [key: string]: Effect } = {}
@@ -1302,8 +1302,8 @@
         }
 
         public resetTextureCache() {
-            for (var key in this._activeTexturesCache) {
-                this._activeTexturesCache[key] = null;
+            for (var key in this._boundTexturesCache) {
+                this._boundTexturesCache[key] = null;
             }
         }
 
@@ -4511,13 +4511,19 @@
             }
         }
 
+        private _boundUniforms: { [key: number]: WebGLUniformLocation } = {};
+
         public bindSamplers(effect: Effect): void {
             this.setProgram(effect.getProgram());
 
             var samplers = effect.getSamplers();
             for (var index = 0; index < samplers.length; index++) {
                 var uniform = effect.getUniform(samplers[index]);
-                this._gl.uniform1i(uniform, index);
+
+                if (uniform) {
+                    this._gl.uniform1i(uniform, index);
+                    this._boundUniforms[index] = uniform;
+                }
             }
             this._currentEffect = null;
         }
@@ -4530,9 +4536,22 @@
         }
 
         public _bindTextureDirectly(target: number, texture: Nullable<InternalTexture>): void {
-            if (this._activeTexturesCache[this._activeTextureChannel] !== texture) {
+            let currentTextureBound = this._boundTexturesCache[this._activeTextureChannel];
+            if (currentTextureBound !== texture) {
+                if (currentTextureBound) {
+                    currentTextureBound._designatedSlot = -1;
+                }
+
                 this._gl.bindTexture(target, texture ? texture._webGLTexture : null);
-                this._activeTexturesCache[this._activeTextureChannel] = texture;
+                this._boundTexturesCache[this._activeTextureChannel] = texture;
+                if (texture) {
+                    texture._designatedSlot = this._activeTextureChannel;
+                    if (texture._designatedSlot !== texture._initialSlot && texture._initialSlot > -1) {
+                        let uniform = this._boundUniforms[texture._initialSlot];
+                        this._gl.uniform1i(uniform, this._activeTextureChannel);
+                        this._boundUniforms[this._activeTextureChannel] = uniform;
+                    }
+                }
             }
         }
 
@@ -4541,6 +4560,13 @@
                 return;
             }
 
+            if (texture) {
+                texture._initialSlot = channel;
+                if (texture._designatedSlot > -1 && texture._designatedSlot !== channel) {
+                    channel = texture._designatedSlot;
+                }
+            }
+
             this.activateTextureChannel(this._gl.TEXTURE0 + channel);
             this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
         }
@@ -4573,7 +4599,7 @@
         private _setTexture(channel: number, texture: Nullable<BaseTexture>): boolean {
             // Not ready?
             if (!texture) {
-                if (this._activeTexturesCache[channel] != null) {
+                if (this._boundTexturesCache[channel] != null) {
                     this.activateTextureChannel(this._gl.TEXTURE0 + channel);
                     this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
                     this._bindTextureDirectly(this._gl.TEXTURE_CUBE_MAP, null);
@@ -4609,11 +4635,20 @@
                 internalTexture = this.emptyTexture;
             }
 
+            internalTexture._initialSlot = channel;
+            if (channel !== internalTexture._designatedSlot) {
+                if (internalTexture._designatedSlot > -1) {
+                    channel = internalTexture._designatedSlot;
+                } else {
+                    internalTexture._designatedSlot = channel;
+                }
+            }
+
             if (!alreadyActivated) {
                 this.activateTextureChannel(this._gl.TEXTURE0 + channel);
             }
 
-            if (this._activeTexturesCache[this._activeTextureChannel] === internalTexture) {
+            if (this._boundTexturesCache[this._activeTextureChannel] === internalTexture) {
                 return false;
             }
 

+ 2 - 2
src/Engine/babylon.nullEngine.ts

@@ -368,8 +368,8 @@
         }
 
         public _bindTextureDirectly(target: number, texture: InternalTexture): void {
-            if (this._activeTexturesCache[this._activeTextureChannel] !== texture) {
-                this._activeTexturesCache[this._activeTextureChannel] = texture;
+            if (this._boundTexturesCache[this._activeTextureChannel] !== texture) {
+                this._boundTexturesCache[this._activeTextureChannel] = texture;
             }
         }
 

+ 21 - 19
src/Materials/Textures/babylon.internalTexture.ts

@@ -32,6 +32,8 @@ module BABYLON {
         public invertY: boolean;
 
         // Private
+        public _initialSlot = -1;
+        public _designatedSlot = -1;
         public _dataSource = InternalTexture.DATASOURCE_UNKNOWN;
         public _buffer: Nullable<ArrayBuffer | HTMLImageElement>;
         public _bufferView: Nullable<ArrayBufferView>;
@@ -75,7 +77,7 @@ module BABYLON {
         constructor(engine: Engine, dataSource: number) {
             this._engine = engine;
             this._dataSource = dataSource;
-            
+
             this._webGLTexture = engine._createTexture();
         }
 
@@ -110,32 +112,32 @@ module BABYLON {
                 case InternalTexture.DATASOURCE_URL:
                     proxy = this._engine.createTexture(this.url, !this.generateMipMaps, this.invertY, null, this.samplingMode, () => {
                         this.isReady = true;
-                    }, null, this._buffer, undefined, this.format); 
+                    }, null, this._buffer, undefined, this.format);
                     proxy._swapAndDie(this);
                     return;
 
                 case InternalTexture.DATASOURCE_RAW:
-                    proxy = this._engine.createRawTexture(this._bufferView, this.baseWidth, this.baseHeight, this.format, this.generateMipMaps, 
-                                                            this.invertY, this.samplingMode, this._compression); 
+                    proxy = this._engine.createRawTexture(this._bufferView, this.baseWidth, this.baseHeight, this.format, this.generateMipMaps,
+                        this.invertY, this.samplingMode, this._compression);
                     proxy._swapAndDie(this);
 
                     this.isReady = true;
-                return;
+                    return;
 
                 case InternalTexture.DATASOURCE_RAW3D:
-                    proxy = this._engine.createRawTexture3D(this._bufferView, this.baseWidth, this.baseHeight, this.baseDepth, this.format, this.generateMipMaps, 
-                        this.invertY, this.samplingMode, this._compression); 
+                    proxy = this._engine.createRawTexture3D(this._bufferView, this.baseWidth, this.baseHeight, this.baseDepth, this.format, this.generateMipMaps,
+                        this.invertY, this.samplingMode, this._compression);
                     proxy._swapAndDie(this);
 
                     this.isReady = true;
-                return;
-                
+                    return;
+
                 case InternalTexture.DATASOURCE_DYNAMIC:
-                    proxy = this._engine.createDynamicTexture(this.baseWidth, this.baseHeight, this.generateMipMaps, this.samplingMode); 
+                    proxy = this._engine.createDynamicTexture(this.baseWidth, this.baseHeight, this.generateMipMaps, this.samplingMode);
                     proxy._swapAndDie(this);
 
                     // The engine will make sure to update content so no need to flag it as isReady = true
-                return;
+                    return;
 
                 case InternalTexture.DATASOURCE_RENDERTARGET:
                     let options = new RenderTargetCreationOptions();
@@ -146,19 +148,19 @@ module BABYLON {
                     options.type = this.type;
 
                     if (this.isCube) {
-                        proxy = this._engine.createRenderTargetCubeTexture(this.width, options); 
+                        proxy = this._engine.createRenderTargetCubeTexture(this.width, options);
                     } else {
                         let size = {
                             width: this.width,
                             height: this.height
                         }
 
-                        proxy = this._engine.createRenderTargetTexture(size, options); 
+                        proxy = this._engine.createRenderTargetTexture(size, options);
                     }
                     proxy._swapAndDie(this);
 
                     this.isReady = true;
-                return;                                      
+                    return;
 
                 case InternalTexture.DATASOURCE_CUBE:
                     proxy = this._engine.createCubeTexture(this.url, null, this._files, !this.generateMipMaps, () => {
@@ -172,14 +174,14 @@ module BABYLON {
                     proxy._swapAndDie(this);
 
                     this.isReady = true;
-                    return;                    
+                    return;
 
                 case InternalTexture.DATASOURCE_CUBEPREFILTERED:
                     proxy = this._engine.createPrefilteredCubeTexture(this.url, null, this._lodGenerationScale, this._lodGenerationOffset, (proxy) => {
                         if (proxy) {
                             proxy._swapAndDie(this);
                         }
-                        
+
                         this.isReady = true;
                     }, null, this.format, this._extension);
                     return;
@@ -207,14 +209,14 @@ module BABYLON {
             if (this._lodTextureMid) {
                 if (target._lodTextureMid) {
                     target._lodTextureMid.dispose();
-                }                
+                }
                 target._lodTextureMid = this._lodTextureMid;
             }
 
             if (this._lodTextureLow) {
                 if (target._lodTextureLow) {
                     target._lodTextureLow.dispose();
-                }                     
+                }
                 target._lodTextureLow = this._lodTextureLow;
             }
 
@@ -224,7 +226,7 @@ module BABYLON {
                 cache.splice(index, 1);
             }
         }
-        
+
         public dispose(): void {
             if (!this._webGLTexture) {
                 return;