'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var Extensions = require('../../../extensions/Extensions.js');
var Shader = require('../shared/shader/Shader.js');
var State = require('../shared/state/State.js');
var TextureSource = require('../shared/texture/sources/TextureSource.js');
var Texture = require('../shared/texture/Texture.js');
var GlProgram = require('./shader/GlProgram.js');

const bigTriangleProgram = new GlProgram.GlProgram({
  vertex: `
        out vec2 vUv;

        void main() {
            vUv = vec2((gl_VertexID << 1) & 2, (gl_VertexID & 2));

            gl_Position = vec4(vUv * 2.0f + -1.0f, 0.0f, 1.0f);

            // flip dem UVs
            vUv.y = 1.0f - vUv.y;
        }`,
  fragment: `
        in vec2 vUv;
        out vec4 fragColor;

        uniform sampler2D uTexture;

        void main() {
            fragColor = texture(uTexture, vUv);
        }`,
  name: "big-triangle"
});
const bigTriangleShader = new Shader.Shader({
  glProgram: bigTriangleProgram,
  resources: {
    uTexture: Texture.Texture.WHITE.source
  }
});
const _GlBackBufferSystem = class {
  constructor(renderer) {
    this.useBackBuffer = false;
    this._useBackBufferThisRender = false;
    this._renderer = renderer;
  }
  init(options = {}) {
    const { useBackBuffer, antialias } = { ..._GlBackBufferSystem.defaultOptions, ...options };
    this.useBackBuffer = useBackBuffer;
    this._antialias = antialias;
  }
  renderStart({ target, clear, clearColor }) {
    this._useBackBufferThisRender = this.useBackBuffer && !!target;
    if (this.useBackBuffer) {
      const renderTarget = this._renderer.renderTarget.getRenderTarget(target);
      this._targetTexture = renderTarget.colorTexture;
      target = this._getBackBufferTexture(renderTarget.colorTexture);
    }
    clearColor ?? (clearColor = this._renderer.background.colorRgba);
    this._renderer.renderTarget.start(target, clear, clearColor);
  }
  renderEnd() {
    this._presentBackBuffer();
  }
  _presentBackBuffer() {
    const renderer = this._renderer;
    renderer.renderTarget.finishRenderPass();
    if (!this._useBackBufferThisRender)
      return;
    const gl = renderer.gl;
    renderer.renderTarget.bind(this._targetTexture, false);
    bigTriangleShader.resources.uTexture = this._backBufferTexture.source;
    renderer.shader.bind(bigTriangleShader, false);
    renderer.state.set(State.State.for2d());
    gl.drawArrays(gl.TRIANGLES, 0, 3);
  }
  _getBackBufferTexture(targetTexture) {
    const source = targetTexture.source;
    this._backBufferTexture = this._backBufferTexture || new Texture.Texture({
      source: new TextureSource.TextureSource({
        width: source.width,
        height: source.height,
        resolution: source._resolution,
        antialias: this._antialias
      })
    });
    this._backBufferTexture.source.resize(
      source.width,
      source.height,
      source._resolution
    );
    return this._backBufferTexture;
  }
  destroy() {
    if (this._backBufferTexture) {
      this._backBufferTexture.destroy();
      this._backBufferTexture = null;
    }
  }
};
let GlBackBufferSystem = _GlBackBufferSystem;
/** @ignore */
GlBackBufferSystem.extension = {
  type: [
    Extensions.ExtensionType.WebGLSystem
  ],
  name: "backBuffer"
};
GlBackBufferSystem.defaultOptions = {
  useBackBuffer: false
};

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