shadow$provide.module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$renderTarget$RenderTargetSystem = function(global, require, module, exports) {
  var Matrix = require("module$node_modules$pixi_DOT_js$lib$maths$matrix$Matrix"), Rectangle = require("module$node_modules$pixi_DOT_js$lib$maths$shapes$Rectangle"), _const = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gl$const"), calculateProjection = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$gpu$renderTarget$calculateProjection"), SystemRunner = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$system$SystemRunner"), CanvasSource = 
  require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$sources$CanvasSource"), TextureSource = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$sources$TextureSource"), Texture = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$Texture"), getCanvasTexture = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$texture$utils$getCanvasTexture"), isRenderingToScreen = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$renderTarget$isRenderingToScreen"), 
  RenderTarget = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$renderTarget$RenderTarget");
  "use strict";
  class RenderTargetSystem {
    constructor(renderer) {
      this.rootViewPort = new Rectangle.Rectangle();
      this.viewport = new Rectangle.Rectangle();
      this.layer = this.mipLevel = 0;
      this.onRenderTargetChange = new SystemRunner.SystemRunner("onRenderTargetChange");
      this.projectionMatrix = new Matrix.Matrix();
      this.defaultClearColor = [0, 0, 0, 0];
      this._renderSurfaceToRenderTargetHash = new Map();
      this._gpuRenderTargetHash = Object.create(null);
      this._renderTargetStack = [];
      this._renderer = renderer;
      renderer.gc.addCollection(this, "_gpuRenderTargetHash", "hash");
    }
    finishRenderPass() {
      this.adaptor.finishRenderPass(this.renderTarget);
    }
    renderStart({target, clear, clearColor, frame, mipLevel, layer}) {
      this._renderTargetStack.length = 0;
      this.push(target, clear, clearColor, frame, mipLevel ?? 0, layer ?? 0);
      this.rootViewPort.copyFrom(this.viewport);
      this.rootRenderTarget = this.renderTarget;
      this.renderingToScreen = isRenderingToScreen.isRenderingToScreen(this.rootRenderTarget);
      this.adaptor.prerender?.(this.rootRenderTarget);
    }
    postrender() {
      this.adaptor.postrender?.(this.rootRenderTarget);
    }
    bind(renderSurface, clear = !0, clearColor, frame, mipLevel = 0, layer = 0) {
      const renderTarget = this.getRenderTarget(renderSurface), didChange = this.renderTarget !== renderTarget;
      this.renderTarget = renderTarget;
      this.renderSurface = renderSurface;
      var gpuRenderTarget = this.getGpuRenderTarget(renderTarget);
      if (renderTarget.pixelWidth !== gpuRenderTarget.width || renderTarget.pixelHeight !== gpuRenderTarget.height) {
        this.adaptor.resizeGpuRenderTarget(renderTarget), gpuRenderTarget.width = renderTarget.pixelWidth, gpuRenderTarget.height = renderTarget.pixelHeight;
      }
      gpuRenderTarget = renderTarget.colorTexture;
      const viewport = this.viewport;
      var arrayLayerCount = gpuRenderTarget.arrayLayerCount || 1;
      (layer | 0) !== layer && (layer |= 0);
      if (0 > layer || layer >= arrayLayerCount) {
        throw Error(`[RenderTargetSystem] layer ${layer} is out of bounds (arrayLayerCount=${arrayLayerCount}).`);
      }
      this.mipLevel = mipLevel | 0;
      this.layer = layer | 0;
      arrayLayerCount = Math.max(gpuRenderTarget.pixelWidth >> mipLevel, 1);
      const pixelHeight = Math.max(gpuRenderTarget.pixelHeight >> mipLevel, 1);
      !frame && renderSurface instanceof Texture.Texture && (frame = renderSurface.frame);
      if (frame) {
        const resolution = gpuRenderTarget._resolution, scale = 1 << Math.max(mipLevel | 0, 0);
        renderSurface = Math.floor((frame.x * resolution + 0.5 | 0) / scale);
        let y = Math.floor((frame.y * resolution + 0.5 | 0) / scale), w = Math.ceil((frame.width * resolution + 0.5 | 0) / scale);
        frame = Math.ceil((frame.height * resolution + 0.5 | 0) / scale);
        renderSurface = Math.min(Math.max(renderSurface, 0), arrayLayerCount - 1);
        y = Math.min(Math.max(y, 0), pixelHeight - 1);
        w = Math.min(Math.max(w, 1), arrayLayerCount - renderSurface);
        frame = Math.min(Math.max(frame, 1), pixelHeight - y);
        viewport.x = renderSurface;
        viewport.y = y;
        viewport.width = w;
        viewport.height = frame;
      } else {
        viewport.x = 0, viewport.y = 0, viewport.width = arrayLayerCount, viewport.height = pixelHeight;
      }
      calculateProjection.calculateProjection(this.projectionMatrix, 0, 0, viewport.width / gpuRenderTarget.resolution, viewport.height / gpuRenderTarget.resolution, !renderTarget.isRoot);
      this.adaptor.startRenderPass(renderTarget, clear, clearColor, viewport, mipLevel, layer);
      didChange && this.onRenderTargetChange.emit(renderTarget);
      return renderTarget;
    }
    clear(target, clear = _const.CLEAR.ALL, clearColor, mipLevel = this.mipLevel, layer = this.layer) {
      clear && (target && (target = this.getRenderTarget(target)), this.adaptor.clear(target || this.renderTarget, clear, clearColor, this.viewport, mipLevel, layer));
    }
    contextChange() {
      this._gpuRenderTargetHash = Object.create(null);
    }
    push(renderSurface, clear = _const.CLEAR.ALL, clearColor, frame, mipLevel = 0, layer = 0) {
      renderSurface = this.bind(renderSurface, clear, clearColor, frame, mipLevel, layer);
      this._renderTargetStack.push({renderTarget:renderSurface, frame, mipLevel, layer});
      return renderSurface;
    }
    pop() {
      this._renderTargetStack.pop();
      const currentRenderTargetData = this._renderTargetStack[this._renderTargetStack.length - 1];
      this.bind(currentRenderTargetData.renderTarget, !1, null, currentRenderTargetData.frame, currentRenderTargetData.mipLevel, currentRenderTargetData.layer);
    }
    getRenderTarget(renderSurface) {
      renderSurface.isTexture && (renderSurface = renderSurface.source);
      return this._renderSurfaceToRenderTargetHash.get(renderSurface) ?? this._initRenderTarget(renderSurface);
    }
    copyToTexture(sourceRenderSurfaceTexture, destinationTexture, originSrc, size, originDest) {
      0 > originSrc.x && (size.width += originSrc.x, originDest.x -= originSrc.x, originSrc.x = 0);
      0 > originSrc.y && (size.height += originSrc.y, originDest.y -= originSrc.y, originSrc.y = 0);
      const {pixelWidth, pixelHeight} = sourceRenderSurfaceTexture;
      size.width = Math.min(size.width, pixelWidth - originSrc.x);
      size.height = Math.min(size.height, pixelHeight - originSrc.y);
      return this.adaptor.copyToTexture(sourceRenderSurfaceTexture, destinationTexture, originSrc, size, originDest);
    }
    ensureDepthStencil() {
      this.renderTarget.stencil || (this.renderTarget.stencil = !0, this.adaptor.startRenderPass(this.renderTarget, !1, null, this.viewport, 0, this.layer));
    }
    destroy() {
      this._renderer = null;
      this._renderSurfaceToRenderTargetHash.forEach((renderTarget, key) => {
        renderTarget !== key && renderTarget.destroy();
      });
      this._renderSurfaceToRenderTargetHash.clear();
      this._gpuRenderTargetHash = Object.create(null);
    }
    _initRenderTarget(renderSurface) {
      let renderTarget = null;
      CanvasSource.CanvasSource.test(renderSurface) && (renderSurface = getCanvasTexture.getCanvasTexture(renderSurface).source);
      renderSurface instanceof RenderTarget.RenderTarget ? renderTarget = renderSurface : renderSurface instanceof TextureSource.TextureSource && (renderTarget = new RenderTarget.RenderTarget({colorTextures:[renderSurface]}), renderSurface.source instanceof CanvasSource.CanvasSource && (renderTarget.isRoot = !0), renderSurface.once("destroy", () => {
        renderTarget.destroy();
        this._renderSurfaceToRenderTargetHash.delete(renderSurface);
        const gpuRenderTarget = this._gpuRenderTargetHash[renderTarget.uid];
        gpuRenderTarget && (this._gpuRenderTargetHash[renderTarget.uid] = null, this.adaptor.destroyGpuRenderTarget(gpuRenderTarget));
      }));
      this._renderSurfaceToRenderTargetHash.set(renderSurface, renderTarget);
      return renderTarget;
    }
    getGpuRenderTarget(renderTarget) {
      return this._gpuRenderTargetHash[renderTarget.uid] || (this._gpuRenderTargetHash[renderTarget.uid] = this.adaptor.initGpuRenderTarget(renderTarget));
    }
    resetState() {
      this.renderSurface = this.renderTarget = null;
    }
  }
  exports.RenderTargetSystem = RenderTargetSystem;
};

//# sourceMappingURL=module$node_modules$pixi_DOT_js$lib$rendering$renderers$shared$renderTarget$RenderTargetSystem.js.map
