瀏覽代碼

webglcontext lost: step 2

David Catuhe 8 年之前
父節點
當前提交
c0d78a58d1

文件差異過大導致無法顯示
+ 2523 - 2506
dist/preview release/babylon.d.ts


文件差異過大導致無法顯示
+ 2523 - 2506
dist/preview release/babylon.module.d.ts


+ 4 - 12
src/Materials/Textures/babylon.baseTexture.ts

@@ -182,18 +182,6 @@
             return false;
         }
 
-        public _removeFromCache(url: string, noMipmap: boolean): void {
-            var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
-            for (var index = 0; index < texturesCache.length; index++) {
-                var texturesCacheEntry = texturesCache[index];
-
-                if (texturesCacheEntry.url === url && texturesCacheEntry.generateMipMaps === !noMipmap) {
-                    texturesCache.splice(index, 1);
-                    return;
-                }
-            }
-        }
-
         public _getFromCache(url: string, noMipmap: boolean, sampling?: number): InternalTexture {
             var texturesCache = this._scene.getEngine().getLoadedTexturesCache();
             for (var index = 0; index < texturesCache.length; index++) {
@@ -210,6 +198,10 @@
             return null;
         }
 
+        public _rebuild(): void {
+            
+        }
+
         public delayLoad(): void {
         }
 

+ 4 - 0
src/Materials/Textures/babylon.dynamicTexture.ts

@@ -119,5 +119,9 @@ module BABYLON {
 
             return newTexture;
         }
+
+        public _rebuild(): void {
+            this.update();
+        }
     }
 } 

+ 29 - 17
src/Materials/Textures/babylon.internalTexture.ts

@@ -1,5 +1,17 @@
 module BABYLON {
     export class InternalTexture {
+
+        public static DATASOURCE_UNKNOWN = 0;
+        public static DATASOURCE_URL = 1;
+        public static DATASOURCE_TEMP = 2;
+        public static DATASOURCE_RAW = 3;
+        public static DATASOURCE_DYNAMIC = 4;
+        public static DATASOURCE_RENDERTARGET = 5;
+        public static DATASOURCE_MULTIRENDERTARGET = 6;
+        public static DATASOURCE_CUBE = 7;
+        public static DATASOURCE_CUBELOD = 8;
+        public static DATASOURCE_CUBERAW = 9;
+
         public isReady: boolean;
         public isCube: boolean;
         public url: string;
@@ -13,8 +25,10 @@ module BABYLON {
         public height: number;
         public baseWidth: number;
         public baseHeight: number;
+        public invertY: boolean;
 
         // Private
+        private _dataSource = InternalTexture.DATASOURCE_UNKNOWN;
         public _size: number;
         public _workingCanvas: HTMLCanvasElement;
         public _workingContext: CanvasRenderingContext2D;
@@ -35,32 +49,37 @@ module BABYLON {
         public _lodTextureHigh: BABYLON.BaseTexture;
         public _lodTextureMid: BABYLON.BaseTexture;
         public _lodTextureLow: BABYLON.BaseTexture;
-        
+
         public _webGLTexture: WebGLTexture;
         public _references: number = 1;
         private _engine: Engine;
 
-        constructor(engine: Engine, texture?: WebGLTexture) {
-            this._engine = engine;
+        public get dataSource(): number {
+            return this._dataSource;
+        }
 
-            if (texture) {
-                this._webGLTexture = texture;               
-            } else {
-                this._webGLTexture = engine._createTexture();
-            }
+        constructor(engine: Engine, dataSource: number) {
+            this._engine = engine;
+            this._dataSource = dataSource;
+            
+            this._webGLTexture = engine._createTexture();
         }
 
-        public incrementReferences() {
+        public incrementReferences(): void {
             this._references++;
         }
 
-        public updateSize(width: number, height: number) {
+        public updateSize(width: number, height: number): void {
             this.width = width;
             this.height = height;
             this._size = width * height;
             this.baseWidth = width;
             this.baseHeight = height;
         }
+
+        public _rebuild(): void {
+           // this._engine.createTexture(this.url, !this.generateMipMaps, this.invertY, scene, this.samplingMode, null, null, this._buffer, null, this._format);
+        }
         
         public dispose(): void {
             if (!this._webGLTexture) {
@@ -69,13 +88,6 @@ module BABYLON {
 
             this._references--;
             if (this._references === 0) {
-                var texturesCache = this._engine.getLoadedTexturesCache();
-                var index = texturesCache.indexOf(this);
-
-                if (index > -1) {
-                    texturesCache.splice(index, 1);
-                }
-
                 this._engine._releaseTexture(this);
                 this._webGLTexture = null;
             }

+ 6 - 0
src/Materials/Textures/babylon.renderTargetTexture.ts

@@ -577,5 +577,11 @@
 
             super.dispose();
         }
+
+        public _rebuild(): void {
+            if (this.refreshRate === RenderTargetTexture.REFRESHRATE_RENDER_ONCE) {
+                this.refreshRate = RenderTargetTexture.REFRESHRATE_RENDER_ONCE;
+            }
+        }
     }
 }

+ 35 - 31
src/babylon.engine.ts

@@ -615,7 +615,7 @@
         private _alphaMode = Engine.ALPHA_DISABLE;
 
         // Cache
-        private _loadedTexturesCache = new Array<InternalTexture>();
+        private _internalTexturesCache = new Array<InternalTexture>();
         private _maxTextureChannels = 16;
         private _activeTexture: number;
         private _activeTexturesCache = new Array<WebGLTexture>(this._maxTextureChannels);
@@ -800,12 +800,12 @@
                 // Rebuild effects
                 this._rebuildEffects();
 
-                // Rebuild buffers
-                this._rebuildBuffers();
-
                 // Rebuild textures
-                this._rebuildTextures();
+                this._rebuildInternalTextures();
 
+                // Rebuild buffers
+                this._rebuildBuffers();
+                
                 // Cache
                 this.wipeCaches(true);
 
@@ -897,7 +897,10 @@
             this.enableOfflineSupport = (BABYLON.Database !== undefined);
         }
 
-        private _rebuildTextures(): void {
+        private _rebuildInternalTextures(): void {
+            for (var internalTexture of this._internalTexturesCache) {
+                internalTexture._rebuild();
+            }
         }
 
         private _rebuildEffects(): void {
@@ -915,6 +918,7 @@
             for (var scene of this.scenes) {
                 scene.resetCachedMaterial();
                 scene._rebuildGeometries();
+                scene._rebuildTextures();
             }
 
             // Uniforms
@@ -1127,7 +1131,7 @@
         }
 
         public getLoadedTexturesCache(): InternalTexture[] {
-            return this._loadedTexturesCache;
+            return this._internalTexturesCache;
         }
 
         public getCaps(): EngineCapabilities {
@@ -2607,9 +2611,8 @@
          *
          * @returns {WebGLTexture} for assignment back into BABYLON.Texture
          */
-        public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: ArrayBuffer | HTMLImageElement = null, fallBack?: WebGLTexture, format?: number): InternalTexture {
-            let webGLtexture = fallBack ? fallBack : this._gl.createTexture();
-            let texture = new InternalTexture(this, webGLtexture);
+        public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: ArrayBuffer | HTMLImageElement = null, fallBack?: InternalTexture, format?: number): InternalTexture {
+            let texture = fallBack ? fallBack : new InternalTexture(this, InternalTexture.DATASOURCE_URL);
 
             var url = String(urlArg); // assign a new string, so that the original is still available in case of fallback
             var fromData = url.substr(0, 5) === "data:";
@@ -2632,11 +2635,12 @@
             texture.url = url;
             texture.generateMipMaps = !noMipmap;
             texture.samplingMode = samplingMode;
+            texture.invertY = invertY;
 
             if (onLoad) {
                 texture.onLoadedObservable.add(onLoad);
             }
-            if (!fallBack) this._loadedTexturesCache.push(texture);
+            if (!fallBack) this._internalTexturesCache.push(texture);
 
             var onerror = () => {
                 scene._removePendingData(texture);
@@ -2707,7 +2711,7 @@
                         }
 
                         // Using shaders to rescale because canvas.drawImage is lossy
-                        let source = new InternalTexture(this);
+                        let source = new InternalTexture(this, InternalTexture.DATASOURCE_TEMP);
                         this._bindTextureDirectly(gl.TEXTURE_2D, source);
                         gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, internalFormat, gl.UNSIGNED_BYTE, img);
 
@@ -2820,7 +2824,7 @@
         }
 
         public createRawTexture(data: ArrayBufferView, width: number, height: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: string = null): InternalTexture {
-            var texture = new InternalTexture(this);
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RAW);
             texture.baseWidth = width;
             texture.baseHeight = height;
             texture.width = width;
@@ -2843,13 +2847,13 @@
 
             texture.samplingMode = samplingMode;
 
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             return texture;
         }
 
         public createDynamicTexture(width: number, height: number, generateMipMaps: boolean, samplingMode: number): InternalTexture {
-            var texture = new InternalTexture(this)
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_DYNAMIC)
             texture.baseWidth = width;
             texture.baseHeight = height;
 
@@ -2867,7 +2871,7 @@
 
             this.updateTextureSamplingMode(samplingMode, texture);
 
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             return texture;
         }
@@ -2992,7 +2996,7 @@
             }
             var gl = this._gl;
 
-            var texture = new InternalTexture(this);
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
             this._bindTextureDirectly(gl.TEXTURE_2D, texture);
 
             var width = size.width || size;
@@ -3043,7 +3047,7 @@
 
             this.resetTextureCache();
 
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             return texture;
         }
@@ -3107,7 +3111,7 @@
                     Tools.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
                 }
 
-                var texture = new InternalTexture(this);
+                var texture = new InternalTexture(this, InternalTexture.DATASOURCE_MULTIRENDERTARGET);
                 var attachment = gl["COLOR_ATTACHMENT" + i];
                 textures.push(texture);
                 attachments.push(attachment);
@@ -3145,12 +3149,12 @@
                 texture._generateDepthBuffer = generateDepthBuffer;
                 texture._generateStencilBuffer = generateStencilBuffer;
 
-                this._loadedTexturesCache.push(texture);
+                this._internalTexturesCache.push(texture);
             }
 
             if (generateDepthTexture) {
                 // Depth texture
-                var depthTexture = new InternalTexture(this);
+                var depthTexture = new InternalTexture(this, InternalTexture.DATASOURCE_MULTIRENDERTARGET);
 
                 gl.activeTexture(gl.TEXTURE0);
                 gl.bindTexture(gl.TEXTURE_2D, depthTexture._webGLTexture);
@@ -3191,7 +3195,7 @@
                 depthTexture._generateStencilBuffer = generateStencilBuffer;
 
                 textures.push(depthTexture)
-                this._loadedTexturesCache.push(depthTexture);
+                this._internalTexturesCache.push(depthTexture);
             }
 
             gl.drawBuffers(attachments);
@@ -3297,7 +3301,7 @@
         public createRenderTargetCubeTexture(size: number, options?: any): InternalTexture {
             var gl = this._gl;
 
-            var texture = new InternalTexture(this);
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
 
             var generateMipMaps = true;
             var generateDepthBuffer = true;
@@ -3356,7 +3360,7 @@
 
             this.resetTextureCache();
 
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             return texture;
         }
@@ -3391,7 +3395,7 @@
                     let lodIndex = minLODIndex + (maxLODIndex - minLODIndex) * roughness;
                     let mipmapIndex = Math.round(Math.min(Math.max(lodIndex, 0), maxLODIndex));
 
-                    var glTextureFromLod = new InternalTexture(this);
+                    var glTextureFromLod = new InternalTexture(this, InternalTexture.DATASOURCE_CUBELOD);
                     glTextureFromLod.isCube = true;
                     this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, glTextureFromLod);
 
@@ -3437,7 +3441,7 @@
         public createCubeTexture(rootUrl: string, scene: Scene, files: string[], noMipmap?: boolean, onLoad: (data?: any) => void = null, onError: () => void = null, format?: number, forcedExtension = null): InternalTexture {
             var gl = this._gl;
 
-            var texture = new InternalTexture(this);
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_CUBE);
             texture.isCube = true;
             texture.url = rootUrl;
             texture.generateMipMaps = !noMipmap;
@@ -3560,7 +3564,7 @@
                 }, files, onError);
             }
 
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             return texture;
         }
@@ -3610,7 +3614,7 @@
 
         public createRawCubeTexture(data: ArrayBufferView[], size: number, format: number, type: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: string = null): InternalTexture {
             var gl = this._gl;
-            var texture = new InternalTexture(this);
+            var texture = new InternalTexture(this, InternalTexture.DATASOURCE_CUBERAW);
             texture.isCube = true;
             texture.generateMipMaps = generateMipMaps;
             texture.format = format;
@@ -3683,7 +3687,7 @@
             var texture = this.createRawCubeTexture(null, size, format, type, !noMipmap, invertY, samplingMode);
             scene._addPendingData(texture);
             texture.url = url;
-            this._loadedTexturesCache.push(texture);
+            this._internalTexturesCache.push(texture);
 
             var onerror = () => {
                 scene._removePendingData(texture);
@@ -3863,9 +3867,9 @@
             // Unbind channels
             this.unbindAllTextures();
 
-            var index = this._loadedTexturesCache.indexOf(texture);
+            var index = this._internalTexturesCache.indexOf(texture);
             if (index !== -1) {
-                this._loadedTexturesCache.splice(index, 1);
+                this._internalTexturesCache.splice(index, 1);
             }
 
             // Integrated fixed lod samplers.

+ 6 - 0
src/babylon.scene.ts

@@ -3839,6 +3839,12 @@
             }
         }
 
+        public _rebuildTextures(): void {
+            for (var texture of this.textures) {
+                texture._rebuild();
+            }
+        }
+
         public createDefaultCameraOrLight(createArcRotateCamera = false, replace = false, attachCameraControls = false) {
             // Dispose existing camera or light in replace mode.
             if (replace) {