import { ExtensionType } from '../../../extensions/Extensions.mjs';
import { Matrix } from '../../../maths/Matrix.mjs';
import { compileHighShaderGlProgram } from '../../high-shader/compileHighShaderToProgram.mjs';
import { colorBitGl } from '../../high-shader/shader-bits/colorBit.mjs';
import { generateTextureBatchBitGl } from '../../high-shader/shader-bits/generateTextureBatchBit.mjs';
import { batchSamplersUniformGroup } from '../../renderers/gl/shader/batchSamplersUniformGroup.mjs';
import { Shader } from '../../renderers/shared/shader/Shader.mjs';
import { UniformGroup } from '../../renderers/shared/shader/UniformGroup.mjs';
import { State } from '../../renderers/shared/state/State.mjs';
import { MAX_TEXTURES } from '../shared/const.mjs';

class GlBatchAdaptor {
  constructor() {
    this._didUpload = false;
    this._tempState = State.for2d();
  }
  init() {
    const uniforms = new UniformGroup({
      tint: { value: new Float32Array([1, 1, 1, 1]), type: "f32" },
      translationMatrix: { value: new Matrix(), type: "mat3x3<f32>" }
    });
    const glProgram = compileHighShaderGlProgram({
      name: "batch",
      bits: [
        colorBitGl,
        generateTextureBatchBitGl(MAX_TEXTURES)
      ]
    });
    this._shader = new Shader({
      glProgram,
      resources: {
        uniforms,
        batchSamplers: batchSamplersUniformGroup
      }
    });
  }
  start(batchPipe, geometry) {
    const renderer = batchPipe.renderer;
    renderer.shader.bind(this._shader, this._didUpload);
    renderer.shader.bindUniformBlock(renderer.globalUniforms.uniformGroup, "globalUniforms", 0);
    renderer.geometry.bind(geometry, this._shader.glProgram);
  }
  execute(batchPipe, batch) {
    const renderer = batchPipe.renderer;
    this._didUpload = true;
    this._tempState.blendMode = batch.blendMode;
    renderer.state.set(this._tempState);
    const textures = batch.textures.textures;
    for (let i = 0; i < textures.length; i++) {
      renderer.texture.bind(textures[i], i);
    }
    renderer.geometry.draw("triangle-list", batch.size, batch.start);
  }
  destroy() {
    this._shader.destroy(true);
    this._shader = null;
  }
}
/** @ignore */
GlBatchAdaptor.extension = {
  type: [
    ExtensionType.WebGLPipesAdaptor
  ],
  name: "batch"
};

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