import '../core/settings.mjs';
import '../color.mjs';
import '../constants.mjs';
import '../extensions.mjs';
import '../math/const.mjs';
import { Rectangle } from '../math/shapes/Rectangle.mjs';
import '../math/groupD8.mjs';
import '../math/Transform.mjs';
import '../runner.mjs';
import { settings } from '../settings/settings.mjs';
import '../ticker/settings.mjs';
import '../ticker/const.mjs';
import '../ticker/Ticker.mjs';
import '../ticker/TickerPlugin.mjs';
import '../utils/settings.mjs';
import '../utils/is_mobile/index.mjs';
import '../utils/color/premultiply.mjs';
import '../utils/media/BoundingBox.mjs';
import '../core/autoDetectRenderer.mjs';
import '../core/background/BackgroundSystem.mjs';
import '../core/batch/BatchRenderer.mjs';
import '../core/shader/Program.mjs';
import '../core/batch/BatchSystem.mjs';
import '../core/context/ContextSystem.mjs';
import '../core/filters/Filter.mjs';
import '../core/filters/FilterSystem.mjs';
import '../core/textures/BaseTexture.mjs';
import '../core/framebuffer/FramebufferSystem.mjs';
import '../core/framebuffer/MultisampleSystem.mjs';
import '../core/geometry/BufferSystem.mjs';
import '../core/geometry/GeometrySystem.mjs';
import '../core/mask/MaskSystem.mjs';
import '../core/mask/ScissorSystem.mjs';
import '../core/mask/StencilSystem.mjs';
import '../core/plugin/PluginSystem.mjs';
import '../core/projection/ProjectionSystem.mjs';
import '../core/render/ObjectRendererSystem.mjs';
import '../core/Renderer.mjs';
import '../core/renderTexture/GenerateTextureSystem.mjs';
import { RenderTexture } from '../core/renderTexture/RenderTexture.mjs';
import '../core/renderTexture/RenderTexturePool.mjs';
import '../core/renderTexture/RenderTextureSystem.mjs';
import '../core/shader/ShaderSystem.mjs';
import '../core/startup/StartupSystem.mjs';
import '../core/state/StateSystem.mjs';
import '../core/textures/TextureGCSystem.mjs';
import '../core/textures/TextureSystem.mjs';
import '../core/transformFeedback/TransformFeedbackSystem.mjs';
import '../core/view/ViewSystem.mjs';
import '../core/textures/resources/index.mjs';

class LayerTextureCache {
  constructor(layer) {
    this.layer = layer;
    this.renderTexture = null;
    this.doubleBuffer = null;
    this.currentBufferIndex = 0;
    this._tempRenderTarget = null;
    this._tempRenderTargetSource = new Rectangle();
    this._tempRenderTargetDestination = new Rectangle();
    this._tempTransform = null;
  }
  init(renderer) {
    if (!this.layer.useRenderTexture) {
      return;
    }
    const size = this.calcSize(renderer);
    this.renderTexture = RenderTexture.create(size);
    if (this.layer.useDepth) {
      this.renderTexture.framebuffer.enableDepth();
    }
    if (this.layer.useDoubleBuffer) {
      this.doubleBuffer = [
        RenderTexture.create(size),
        RenderTexture.create(size)
      ];
      if (this.layer.useDepth) {
        this.doubleBuffer[0].framebuffer.enableDepth();
        this.doubleBuffer[1].framebuffer.enableDepth();
      }
    }
  }
  calcSize(renderer) {
    const size = this.layer.screenSize || renderer?.screen;
    const width = size?.width || 100;
    const height = size?.height || 100;
    const resolution = renderer ? renderer.resolution : settings.RESOLUTION;
    return { width, height, resolution };
  }
  /** See {@link Layer#getRenderTexture}. */
  getRenderTexture() {
    if (!this.renderTexture) {
      this.init();
    }
    return this.renderTexture;
  }
  /** Prepares the layer's render-texture and set it as the render-target. */
  pushTexture(renderer) {
    const screen = renderer.screen;
    const layer = this.layer;
    if (layer.useRenderTexture && !this.renderTexture) {
      this.init(renderer);
    }
    const rt = this.renderTexture;
    const db = this.doubleBuffer;
    if (rt && !layer.screenSize) {
      const sz = this.calcSize(renderer);
      if (rt.width !== sz.width || rt.height !== sz.height || rt.baseTexture.resolution !== sz.resolution) {
        rt.baseTexture.resolution = renderer.resolution;
        rt.resize(screen.width, screen.height);
        if (db) {
          db[0].baseTexture.resolution = renderer.resolution;
          db[0].resize(screen.width, screen.height);
          db[1].baseTexture.resolution = renderer.resolution;
          db[1].resize(screen.width, screen.height);
        }
      }
    }
    if (db) {
      db[0].framebuffer.multisample = rt.framebuffer.multisample;
      db[1].framebuffer.multisample = rt.framebuffer.multisample;
    }
    this._tempRenderTarget = renderer.renderTexture.current;
    this._tempRenderTargetSource.copyFrom(renderer.renderTexture.sourceFrame);
    this._tempRenderTargetDestination.copyFrom(renderer.renderTexture.destinationFrame);
    if (layer.useTransform) {
      this._tempTransform = renderer.projection.transform;
      renderer.projection.transform = layer.transform;
    }
    renderer.batch.flush();
    if (layer.useDoubleBuffer) {
      let buffer = db[this.currentBufferIndex];
      if (!buffer.baseTexture._glTextures[renderer.CONTEXT_UID]) {
        renderer.renderTexture.bind(buffer, void 0, void 0);
        renderer.texture.bind(buffer);
        if (layer.bgColor) {
          renderer.renderTexture.clear(layer.bgColor);
        }
      }
      renderer.texture.unbind(rt.baseTexture);
      rt.baseTexture._glTextures = buffer.baseTexture._glTextures;
      rt.baseTexture.framebuffer = buffer.baseTexture.framebuffer;
      buffer = db[1 - this.currentBufferIndex];
      renderer.renderTexture.bind(buffer, void 0, void 0);
    } else {
      renderer.renderTexture.bind(rt, void 0, void 0);
    }
    const filterStack = renderer.filter.defaultFilterStack;
    if (filterStack.length > 1) {
      filterStack[filterStack.length - 1].renderTexture = renderer.renderTexture.current;
    }
  }
  /** Flushes the renderer and restores the old render-target. */
  popTexture(renderer) {
    renderer.batch.flush();
    renderer.framebuffer.blit();
    const filterStack = renderer.filter.defaultFilterStack;
    const { layer } = this;
    if (filterStack.length > 1) {
      filterStack[filterStack.length - 1].renderTexture = this._tempRenderTarget;
    }
    renderer.renderTexture.bind(this._tempRenderTarget, this._tempRenderTargetSource, this._tempRenderTargetDestination);
    this._tempRenderTarget = null;
    const rt = this.renderTexture;
    const db = this.doubleBuffer;
    if (layer.useDoubleBuffer) {
      renderer.texture.unbind(rt.baseTexture);
      this.currentBufferIndex = 1 - this.currentBufferIndex;
      const buffer = db[this.currentBufferIndex];
      rt.baseTexture._glTextures = buffer.baseTexture._glTextures;
      rt.baseTexture.framebuffer = buffer.baseTexture.framebuffer;
    }
    if (layer.useTransform) {
      renderer.projection.transform = this._tempTransform;
      this._tempTransform = null;
    }
  }
  /** Destroy the texture-cache. Set {@link Layer.textureCache} to {@code null} after destroying it! */
  destroy() {
    if (this.renderTexture) {
      this.renderTexture.destroy();
      if (this.doubleBuffer) {
        this.doubleBuffer[0].destroy(true);
        this.doubleBuffer[1].destroy(true);
      }
    }
  }
}

export { LayerTextureCache };
//# sourceMappingURL=LayerTextureCache.mjs.map
