import '../core/settings.mjs';
import '../color.mjs';
import '../constants.mjs';
import '../extensions.mjs';
import '../math/const.mjs';
import '../math/groupD8.mjs';
import '../math/Transform.mjs';
import '../runner.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 { Buffer } from '../core/geometry/Buffer.mjs';
import { Geometry } from '../core/geometry/Geometry.mjs';
import '../core/batch/BatchRenderer.mjs';
import { Program } from '../core/shader/Program.mjs';
import { Shader } from '../core/shader/Shader.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 '../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';

const fragment = `#version 300 es
precision highp float;

out vec4 outColor;

void main() {
    outColor = vec4(0.0, 0.0, 0.0, 1.0);
}
`;
function genVertex(elemSize, elemCount) {
  let code = `#version 300 es
precision highp float;
precision highp int;
`;
  for (let i = 1; i <= elemCount; i++) {
    code += `in vec${elemSize} a_silly${i};
`;
  }
  for (let i = 1; i <= elemCount; i++) {
    code += `out vec${elemSize} v_silly${i};
`;
  }
  code += `void main() {`;
  for (let i = 1; i <= elemCount; i++) {
    code += `v_silly${i} = a_silly${i};
`;
  }
  code += `
    gl_PointSize = 0.0;
    gl_Position = vec4(-2.0, -2.0, 0.0, 1.0);
}
`;
  return code;
}
function genAttribs(geom, buf, elemSize, elemCount) {
  for (let i = 1; i <= elemCount; i++) {
    geom.addAttribute(`a_silly${i}`, buf, elemSize);
  }
}
function genVaryings(elemCount) {
  const res = [];
  for (let i = 1; i <= elemCount; i++) {
    res.push(`v_silly${i}`);
  }
  return res;
}
class TFBufferCopier {
  constructor(strideFloats = 16) {
    this.elemCount = 0;
    this.elemSize = 0;
    this.program = null;
    this.shader = null;
    this.tf = {};
    this.strideFloats = strideFloats;
    this.strideBytes = 4 * strideFloats;
    this.initProgram();
    this.initGeom();
  }
  initProgram() {
    const { strideFloats } = this;
    let elemSize = 4;
    while (elemSize > 1 && strideFloats % elemSize > 0) {
      elemSize--;
    }
    this.elemSize = elemSize;
    this.elemCount = strideFloats / this.elemSize;
    const vertex = genVertex(this.elemSize, this.elemCount);
    const tfNames = genVaryings(this.elemCount);
    this.program = new Program({ vertex, fragment, name: "silly tf" }, {
      transformFeedbackVaryings: {
        names: tfNames,
        bufferMode: "interleaved"
      }
    });
    this.shader = new Shader(this.program);
  }
  initGeom() {
    this.tempBuffer = new Buffer(new Float32Array());
    this.geom = new Geometry();
    genAttribs(this.geom, this.tempBuffer, this.elemSize, this.elemCount);
  }
  doCopy(renderer, src, target, strideBytes, copies, copyCount = copies.length) {
    if (strideBytes % this.strideBytes !== 0) {
      return;
    }
    const gl = renderer.gl;
    const CONTEXT_UID = renderer.CONTEXT_UID;
    let tf = this.tf[CONTEXT_UID];
    if (!tf) {
      tf = this.tf[CONTEXT_UID] = gl.createTransformFeedback();
    }
    if (!target._glBuffers[CONTEXT_UID]) {
      renderer.buffer.bind(target);
      renderer.buffer.update(target);
    }
    this.geom.swapBuffer(0, src);
    renderer.shader.bind(this.shader);
    renderer.geometry.bind(this.geom);
    gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, tf);
    for (let i = 0; i < copyCount; i++) {
      const op = copies[i];
      gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, 0, target._glBuffers[CONTEXT_UID].buffer, op.dst * strideBytes, op.count * strideBytes);
      gl.beginTransformFeedback(gl.POINTS);
      gl.drawArrays(gl.POINTS, op.src * strideBytes / this.strideBytes, op.count * strideBytes / this.strideBytes);
      gl.endTransformFeedback();
    }
    gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
    renderer.geometry.unbind();
    this.geom.swapBuffer(0, this.tempBuffer);
  }
}

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