Quellcode durchsuchen

refactor to avoid duplicating upload to texture logic in engine

Trevor Baron vor 6 Jahren
Ursprung
Commit
7506a2a984
3 geänderte Dateien mit 31 neuen und 41 gelöschten Zeilen
  1. 10 34
      src/Engines/engine.ts
  2. 17 3
      src/Materials/Textures/Loaders/basisTextureLoader.ts
  3. 4 4
      src/Misc/basis.ts

+ 10 - 34
src/Engines/engine.ts

@@ -4313,11 +4313,6 @@ export class Engine {
                         this._bindTextureDirectly(gl.TEXTURE_2D, source, true);
                         gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, internalFormat, gl.UNSIGNED_BYTE, img);
 
-                        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
-                        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
-                        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
-                        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
-
                         this._rescaleTexture(source, texture, scene, internalFormat, () => {
                             this._releaseTexture(source);
                             this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
@@ -4348,7 +4343,12 @@ export class Engine {
         return texture;
     }
 
-    private _rescaleTexture(source: InternalTexture, destination: InternalTexture, scene: Nullable<Scene>, internalFormat: number, onComplete: () => void): void {
+    public _rescaleTexture(source: InternalTexture, destination: InternalTexture, scene: Nullable<Scene>, internalFormat: number, onComplete: () => void): void {
+        this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
+        this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
+        this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
+        this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
+
         let rtt = this.createRenderTargetTexture({
             width: destination.width,
             height: destination.height,
@@ -4976,36 +4976,12 @@ export class Engine {
     }
 
     /** @hidden */
-    public _uploadDataToTextureAndResize(texture: InternalTexture, imageData: ArrayBufferView, width: number, height: number, babylonInternalFormat?: number) {
-        // Get gl type and formats
-        var textureType = this._getWebGLTextureType(texture.type);
-        var format = this._getInternalFormat(texture.format);
-        var internalFormat = babylonInternalFormat === undefined ? this._getRGBABufferInternalSizedFormat(texture.type, format) : this._getInternalFormat(babylonInternalFormat);
-
-        // Create non power of two texture
-        var gl = this._gl;
-        let source = new InternalTexture(this, InternalTexture.DATASOURCE_TEMP);
-        this._bindTextureDirectly(gl.TEXTURE_2D, source, true);
-        gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, width, height, 0, format, textureType, imageData);
-
-        // rescale to a power of two
-        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
-        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
-        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
-        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
-        this._rescaleTexture(source, texture, this.scenes[0], internalFormat, () => {
-            this._releaseTexture(source);
-            this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
-        });
-    }
-
-    /** @hidden */
-    public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
+    public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0, babylonInternalFormat?: number, useTextureWidthAndHeight = false): void {
         var gl = this._gl;
 
         var textureType = this._getWebGLTextureType(texture.type);
         var format = this._getInternalFormat(texture.format);
-        var internalFormat = this._getRGBABufferInternalSizedFormat(texture.type, format);
+        var internalFormat = babylonInternalFormat === undefined ? this._getRGBABufferInternalSizedFormat(texture.type, format) : this._getInternalFormat(babylonInternalFormat);
 
         this._unpackFlipY(texture.invertY);
 
@@ -5016,8 +4992,8 @@ export class Engine {
 
         const lodMaxWidth = Math.round(Scalar.Log2(texture.width));
         const lodMaxHeight = Math.round(Scalar.Log2(texture.height));
-        const width = Math.pow(2, Math.max(lodMaxWidth - lod, 0));
-        const height = Math.pow(2, Math.max(lodMaxHeight - lod, 0));
+        const width = useTextureWidthAndHeight ? texture.width : Math.pow(2, Math.max(lodMaxWidth - lod, 0));
+        const height = useTextureWidthAndHeight ? texture.height : Math.pow(2, Math.max(lodMaxHeight - lod, 0));
 
         gl.texImage2D(target, lod, internalFormat, width, height, 0, format, textureType, imageData);
     }

+ 17 - 3
src/Materials/Textures/Loaders/basisTextureLoader.ts

@@ -73,6 +73,7 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
             var loadedFile = BasisTools.LoadBasisFile(data);
             var fileInfo = BasisTools.GetFileInfo(loadedFile);
             var format = BasisTools.GetSupportedTranscodeFormat(texture.getEngine(), fileInfo);
+            texture._invertVScale = true;
 
             // TODO this should be done in web worker
             var transcodeResult = BasisTools.TranscodeFile(format, fileInfo, loadedFile);
@@ -82,10 +83,23 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
                 if (transcodeResult.fallbackToRgb565) {
                     texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
                     texture.format = Engine.TEXTUREFORMAT_RGB;
-                    texture.getEngine()._uploadDataToTextureAndResize(texture, transcodeResult.pixels, fileInfo.alignedWidth, fileInfo.alignedHeight, Engine.TEXTUREFORMAT_RGB);
+
+                    // Create non power of two texture
+                    let source = new InternalTexture(texture.getEngine(), InternalTexture.DATASOURCE_TEMP);
+
+                    source.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
+                    source.format = Engine.TEXTUREFORMAT_RGB;
+                    source.width = fileInfo.alignedWidth;
+                    source.height = fileInfo.alignedHeight;
+                    texture.getEngine()._bindTextureDirectly(source.getEngine()._gl.TEXTURE_2D, source, true);
+                    texture.getEngine()._uploadDataToTextureDirectly(source, transcodeResult.pixels, 0, 0, Engine.TEXTUREFORMAT_RGB, true);
+
+                    // Resize to power of two
+                    source.getEngine()._rescaleTexture(source, texture, texture.getEngine().scenes[0], source.getEngine()._getInternalFormat(Engine.TEXTUREFORMAT_RGB), () => {
+                        source.getEngine()._releaseTexture(source);
+                        source.getEngine()._bindTextureDirectly(source.getEngine()._gl.TEXTURE_2D, texture, true);
+                    });
                 }else {
-                    // compress texture needs to be flipped
-                    texture._invertVScale = true;
                     texture.getEngine()._uploadCompressedDataToTextureDirectly(texture, BasisTools.GetInternalFormatFromBasisFormat(format!), fileInfo.width, fileInfo.height, transcodeResult.pixels, 0, 0);
                 }
             });

+ 4 - 4
src/Misc/basis.ts

@@ -34,7 +34,7 @@ class BasisFileInfo {
  */
 export class BasisTools {
     private static _IgnoreSupportedFormats = false;
-    private static LoadScriptPromise: any = null;
+    private static _LoadScriptPromise: any = null;
     // TODO should load from cdn location as fallback once it exists
     private static _FallbackURL = "../dist/preview%20release/basisTranscoder/basis_transcoder.js";
     private static _BASIS_FORMAT = {
@@ -63,8 +63,8 @@ export class BasisTools {
         }
 
         // Otherwise load script from fallback url
-        if (!this.LoadScriptPromise) {
-            this.LoadScriptPromise = Tools.LoadScriptAsync(BasisTools._FallbackURL, "basis_transcoder").then((success) => {
+        if (!this._LoadScriptPromise) {
+            this._LoadScriptPromise = Tools.LoadScriptAsync(BasisTools._FallbackURL, "basis_transcoder").then((success) => {
                 return new Promise((res, rej) => {
                     if ((window as any).Module) {
                         (window as any).Module.onRuntimeInitialized = () => {
@@ -78,7 +78,7 @@ export class BasisTools {
                 });
             });
         }
-        return this.LoadScriptPromise;
+        return this._LoadScriptPromise;
     }
 
     /**