text: import { Observable } from "../../Misc/observable";
  import { RenderTargetCreationOptions } from "../../Materials/Textures/renderTargetCreationOptions";
  import { _DevTools } from '../../Misc/devTools';
  /**
   * Defines the source of the internal texture
   */
  export var InternalTextureSource;
  (function (InternalTextureSource) {
      /**
       * The source of the texture data is unknown
       */
      InternalTextureSource[InternalTextureSource["Unknown"] = 0] = "Unknown";
      /**
      * Texture data comes from an URL
      */
      InternalTextureSource[InternalTextureSource["Url"] = 1] = "Url";
      /**
       * Texture data is only used for temporary storage
       */
      InternalTextureSource[InternalTextureSource["Temp"] = 2] = "Temp";
      /**
       * Texture data comes from raw data (ArrayBuffer)
       */
      InternalTextureSource[InternalTextureSource["Raw"] = 3] = "Raw";
      /**
       * Texture content is dynamic (video or dynamic texture)
       */
      InternalTextureSource[InternalTextureSource["Dynamic"] = 4] = "Dynamic";
      /**
       * Texture content is generated by rendering to it
       */
      InternalTextureSource[InternalTextureSource["RenderTarget"] = 5] = "RenderTarget";
      /**
       * Texture content is part of a multi render target process
       */
      InternalTextureSource[InternalTextureSource["MultiRenderTarget"] = 6] = "MultiRenderTarget";
      /**
       * Texture data comes from a cube data file
       */
      InternalTextureSource[InternalTextureSource["Cube"] = 7] = "Cube";
      /**
       * Texture data comes from a raw cube data
       */
      InternalTextureSource[InternalTextureSource["CubeRaw"] = 8] = "CubeRaw";
      /**
       * Texture data come from a prefiltered cube data file
       */
      InternalTextureSource[InternalTextureSource["CubePrefiltered"] = 9] = "CubePrefiltered";
      /**
       * Texture content is raw 3D data
       */
      InternalTextureSource[InternalTextureSource["Raw3D"] = 10] = "Raw3D";
      /**
       * Texture content is raw 2D array data
       */
      InternalTextureSource[InternalTextureSource["Raw2DArray"] = 11] = "Raw2DArray";
      /**
       * Texture content is a depth texture
       */
      InternalTextureSource[InternalTextureSource["Depth"] = 12] = "Depth";
      /**
       * Texture data comes from a raw cube data encoded with RGBD
       */
      InternalTextureSource[InternalTextureSource["CubeRawRGBD"] = 13] = "CubeRawRGBD";
  })(InternalTextureSource || (InternalTextureSource = {}));
  /**
   * Class used to store data associated with WebGL texture data for the engine
   * This class should not be used directly
   */
  var InternalTexture = /** @class */ (function () {
      /**
       * Creates a new InternalTexture
       * @param engine defines the engine to use
       * @param source defines the type of data that will be used
       * @param delayAllocation if the texture allocation should be delayed (default: false)
       */
      function InternalTexture(engine, source, delayAllocation) {
          if (delayAllocation === void 0) { delayAllocation = false; }
          /**
           * Defines if the texture is ready
           */
          this.isReady = false;
          /**
           * Defines if the texture is a cube texture
           */
          this.isCube = false;
          /**
           * Defines if the texture contains 3D data
           */
          this.is3D = false;
          /**
           * Defines if the texture contains 2D array data
           */
          this.is2DArray = false;
          /**
           * Defines if the texture contains multiview data
           */
          this.isMultiview = false;
          /**
           * Gets the URL used to load this texture
           */
          this.url = "";
          /**
           * Gets the sampling mode of the texture
           */
          this.samplingMode = -1;
          /**
           * Gets a boolean indicating if the texture needs mipmaps generation
           */
          this.generateMipMaps = false;
          /**
           * Gets the number of samples used by the texture (WebGL2+ only)
           */
          this.samples = 0;
          /**
           * Gets the type of the texture (int, float...)
           */
          this.type = -1;
          /**
           * Gets the format of the texture (RGB, RGBA...)
           */
          this.format = -1;
          /**
           * Observable called when the texture is loaded
           */
          this.onLoadedObservable = new Observable();
          /**
           * Gets the width of the texture
           */
          this.width = 0;
          /**
           * Gets the height of the texture
           */
          this.height = 0;
          /**
           * Gets the depth of the texture
           */
          this.depth = 0;
          /**
           * Gets the initial width of the texture (It could be rescaled if the current system does not support non power of two textures)
           */
          this.baseWidth = 0;
          /**
           * Gets the initial height of the texture (It could be rescaled if the current system does not support non power of two textures)
           */
          this.baseHeight = 0;
          /**
           * Gets the initial depth of the texture (It could be rescaled if the current system does not support non power of two textures)
           */
          this.baseDepth = 0;
          /**
           * Gets a boolean indicating if the texture is inverted on Y axis
           */
          this.invertY = false;
          // Private
          /** @hidden */
          this._invertVScale = false;
          /** @hidden */
          this._associatedChannel = -1;
          /** @hidden */
          this._source = InternalTextureSource.Unknown;
          /** @hidden */
          this._buffer = null;
          /** @hidden */
          this._bufferView = null;
          /** @hidden */
          this._bufferViewArray = null;
          /** @hidden */
          this._bufferViewArrayArray = null;
          /** @hidden */
          this._size = 0;
          /** @hidden */
          this._extension = "";
          /** @hidden */
          this._files = null;
          /** @hidden */
          this._workingCanvas = null;
          /** @hidden */
          this._workingContext = null;
          /** @hidden */
          this._framebuffer = null;
          /** @hidden */
          this._depthStencilBuffer = null;
          /** @hidden */
          this._MSAAFramebuffer = null;
          /** @hidden */
          this._MSAARenderBuffer = null;
          /** @hidden */
          this._attachments = null;
          /** @hidden */
          this._textureArray = null;
          /** @hidden */
          this._cachedCoordinatesMode = null;
          /** @hidden */
          this._cachedWrapU = null;
          /** @hidden */
          this._cachedWrapV = null;
          /** @hidden */
          this._cachedWrapR = null;
          /** @hidden */
          this._cachedAnisotropicFilteringLevel = null;
          /** @hidden */
          this._isDisabled = false;
          /** @hidden */
          this._compression = null;
          /** @hidden */
          this._generateStencilBuffer = false;
          /** @hidden */
          this._generateDepthBuffer = false;
          /** @hidden */
          this._comparisonFunction = 0;
          /** @hidden */
          this._sphericalPolynomial = null;
          /** @hidden */
          this._lodGenerationScale = 0;
          /** @hidden */
          this._lodGenerationOffset = 0;
          // Multiview
          /** @hidden */
          this._colorTextureArray = null;
          /** @hidden */
          this._depthStencilTextureArray = null;
          // The following three fields helps sharing generated fixed LODs for texture filtering
          // In environment not supporting the textureLOD extension like EDGE. They are for internal use only.
          // They are at the level of the gl texture to benefit from the cache.
          /** @hidden */
          this._lodTextureHigh = null;
          /** @hidden */
          this._lodTextureMid = null;
          /** @hidden */
          this._lodTextureLow = null;
          /** @hidden */
          this._isRGBD = false;
          /** @hidden */
          this._linearSpecularLOD = false;
          /** @hidden */
          this._irradianceTexture = null;
          /** @hidden */
          this._webGLTexture = null;
          /** @hidden */
          this._references = 1;
          /** @hidden */
          this._gammaSpace = null;
          this._engine = engine;
          this._source = source;
          if (!delayAllocation) {
              this._webGLTexture = engine._createTexture();
          }
      }
      /**
       * Gets the Engine the texture belongs to.
       * @returns The babylon engine
       */
      InternalTexture.prototype.getEngine = function () {
          return this._engine;
      };
      Object.defineProperty(InternalTexture.prototype, "source", {
          /**
           * Gets the data source type of the texture
           */
          get: function () {
              return this._source;
          },
          enumerable: false,
          configurable: true
      });
      /**
       * Increments the number of references (ie. the number of Texture that point to it)
       */
      InternalTexture.prototype.incrementReferences = function () {
          this._references++;
      };
      /**
       * Change the size of the texture (not the size of the content)
       * @param width defines the new width
       * @param height defines the new height
       * @param depth defines the new depth (1 by default)
       */
      InternalTexture.prototype.updateSize = function (width, height, depth) {
          if (depth === void 0) { depth = 1; }
          this.width = width;
          this.height = height;
          this.depth = depth;
          this.baseWidth = width;
          this.baseHeight = height;
          this.baseDepth = depth;
          this._size = width * height * depth;
      };
      /** @hidden */
      InternalTexture.prototype._rebuild = function () {
          var _this = this;
          var _a;
          var proxy;
          this.isReady = false;
          this._cachedCoordinatesMode = null;
          this._cachedWrapU = null;
          this._cachedWrapV = null;
          this._cachedAnisotropicFilteringLevel = null;
          switch (this.source) {
              case InternalTextureSource.Temp:
                  return;
              case InternalTextureSource.Url:
                  proxy = this._engine.createTexture((_a = this._originalUrl) !== null && _a !== void 0 ? _a : this.url, !this.generateMipMaps, this.invertY, null, this.samplingMode, function () {
                      proxy._swapAndDie(_this);
                      _this.isReady = true;
                  }, null, this._buffer, undefined, this.format);
                  return;
              case InternalTextureSource.Raw:
                  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;
              case InternalTextureSource.Raw3D:
                  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;
              case InternalTextureSource.Raw2DArray:
                  proxy = this._engine.createRawTexture2DArray(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;
              case InternalTextureSource.Dynamic:
                  proxy = this._engine.createDynamicTexture(this.baseWidth, this.baseHeight, this.generateMipMaps, this.samplingMode);
                  proxy._swapAndDie(this);
                  this._engine.updateDynamicTexture(this, this._engine.getRenderingCanvas(), this.invertY, undefined, undefined, true);
                  // The engine will make sure to update content so no need to flag it as isReady = true
                  return;
              case InternalTextureSource.RenderTarget:
                  var options = new RenderTargetCreationOptions();
                  options.generateDepthBuffer = this._generateDepthBuffer;
                  options.generateMipMaps = this.generateMipMaps;
                  options.generateStencilBuffer = this._generateStencilBuffer;
                  options.samplingMode = this.samplingMode;
                  options.type = this.type;
                  if (this.isCube) {
                      proxy = this._engine.createRenderTargetCubeTexture(this.width, options);
                  }
                  else {
                      var size_1 = {
                          width: this.width,
                          height: this.height,
                          layers: this.is2DArray ? this.depth : undefined
                      };
                      proxy = this._engine.createRenderTargetTexture(size_1, options);
                  }
                  proxy._swapAndDie(this);
                  this.isReady = true;
                  return;
              case InternalTextureSource.Depth:
                  var depthTextureOptions = {
                      bilinearFiltering: this.samplingMode !== 2,
                      comparisonFunction: this._comparisonFunction,
                      generateStencil: this._generateStencilBuffer,
                      isCube: this.isCube
                  };
                  var size = {
                      width: this.width,
                      height: this.height,
                      layers: this.is2DArray ? this.depth : undefined
                  };
                  proxy = this._engine.createDepthStencilTexture(size, depthTextureOptions);
                  proxy._swapAndDie(this);
                  this.isReady = true;
                  return;
              case InternalTextureSource.Cube:
                  proxy = this._engine.createCubeTexture(this.url, null, this._files, !this.generateMipMaps, function () {
                      proxy._swapAndDie(_this);
                      _this.isReady = true;
                  }, null, this.format, this._extension);
                  return;
              case InternalTextureSource.CubeRaw:
                  proxy = this._engine.createRawCubeTexture(this._bufferViewArray, this.width, this.format, this.type, this.generateMipMaps, this.invertY, this.samplingMode, this._compression);
                  proxy._swapAndDie(this);
                  this.isReady = true;
                  return;
              case InternalTextureSource.CubeRawRGBD:
                  proxy = this._engine.createRawCubeTexture(null, this.width, this.format, this.type, this.generateMipMaps, this.invertY, this.samplingMode, this._compression);
                  InternalTexture._UpdateRGBDAsync(proxy, this._bufferViewArrayArray, this._sphericalPolynomial, this._lodGenerationScale, this._lodGenerationOffset).then(function () {
                      proxy._swapAndDie(_this);
                      _this.isReady = true;
                  });
                  return;
              case InternalTextureSource.CubePrefiltered:
                  proxy = this._engine.createPrefilteredCubeTexture(this.url, null, this._lodGenerationScale, this._lodGenerationOffset, function (proxy) {
                      if (proxy) {
                          proxy._swapAndDie(_this);
                      }
                      _this.isReady = true;
                  }, null, this.format, this._extension);
                  proxy._sphericalPolynomial = this._sphericalPolynomial;
                  return;
          }
      };
      /** @hidden */
      InternalTexture.prototype._swapAndDie = function (target) {
          target._webGLTexture = this._webGLTexture;
          target._isRGBD = this._isRGBD;
          if (this._framebuffer) {
              target._framebuffer = this._framebuffer;
          }
          if (this._depthStencilBuffer) {
              target._depthStencilBuffer = this._depthStencilBuffer;
          }
          target._depthStencilTexture = this._depthStencilTexture;
          if (this._lodTextureHigh) {
              if (target._lodTextureHigh) {
                  target._lodTextureHigh.dispose();
              }
              target._lodTextureHigh = this._lodTextureHigh;
          }
          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;
          }
          if (this._irradianceTexture) {
              if (target._irradianceTexture) {
                  target._irradianceTexture.dispose();
              }
              target._irradianceTexture = this._irradianceTexture;
          }
          var cache = this._engine.getLoadedTexturesCache();
          var index = cache.indexOf(this);
          if (index !== -1) {
              cache.splice(index, 1);
          }
          var index = cache.indexOf(target);
          if (index === -1) {
              cache.push(target);
          }
      };
      /**
       * Dispose the current allocated resources
       */
      InternalTexture.prototype.dispose = function () {
          if (!this._webGLTexture) {
              return;
          }
          this._references--;
          if (this._references === 0) {
              this._engine._releaseTexture(this);
              this._webGLTexture = null;
          }
      };
      /** @hidden */
      InternalTexture._UpdateRGBDAsync = function (internalTexture, data, sphericalPolynomial, lodScale, lodOffset) {
          throw _DevTools.WarnImport("environmentTextureTools");
      };
      return InternalTexture;
  }());
  export { InternalTexture };
  //# sourceMappingURL=internalTexture.js.map