'use strict';

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

class LayerTextureCache {
  constructor(layer) {
    this.layer = layer;
    this.renderTexture = null;
    this.doubleBuffer = null;
    this.currentBufferIndex = 0;
    this._tempRenderTarget = null;
    this._tempRenderTargetSource = new Rectangle.Rectangle();
    this._tempRenderTargetDestination = new Rectangle.Rectangle();
    this._tempTransform = null;
  }
  init(renderer) {
    if (!this.layer.useRenderTexture) {
      return;
    }
    const size = this.calcSize(renderer);
    this.renderTexture = RenderTexture.RenderTexture.create(size);
    if (this.layer.useDepth) {
      this.renderTexture.framebuffer.enableDepth();
    }
    if (this.layer.useDoubleBuffer) {
      this.doubleBuffer = [
        RenderTexture.RenderTexture.create(size),
        RenderTexture.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.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);
      }
    }
  }
}

exports.LayerTextureCache = LayerTextureCache;
//# sourceMappingURL=LayerTextureCache.js.map
