import { ExtensionType } from '../../../extensions/Extensions.mjs';
import { color32BitToUniform } from '../../graphics/gpu/colorToUniform.mjs';
import { compileHighShaderGlProgram } from '../../high-shader/compileHighShaderToProgram.mjs';
import { localUniformBitGl } from '../../high-shader/shader-bits/localUniformBit.mjs';
import { textureBitGl } from '../../high-shader/shader-bits/textureBit.mjs';
import { Shader } from '../../renderers/shared/shader/Shader.mjs';
import { Texture } from '../../renderers/shared/texture/Texture.mjs';

class GlMeshAdaptor {
  init() {
    const glProgram = compileHighShaderGlProgram({
      name: "mesh",
      bits: [
        localUniformBitGl,
        textureBitGl
      ]
    });
    this._shader = new Shader({
      glProgram,
      resources: {
        uTexture: Texture.EMPTY.source
      }
    });
    this._shader.addResource("globalUniforms", 0, 0);
    this._shader.addResource("localUniforms", 1, 0);
  }
  execute(meshPipe, renderable) {
    const renderer = meshPipe.renderer;
    const view = renderable.view;
    const state = view.state;
    state.blendMode = renderable.layerBlendMode;
    const localUniforms = meshPipe.localUniforms;
    localUniforms.uniforms.uTransformMatrix = renderable.layerTransform;
    localUniforms.update();
    color32BitToUniform(
      renderable.layerColor,
      localUniforms.uniforms.uColor,
      0
    );
    let shader = view._shader;
    if (!shader) {
      shader = this._shader;
      const source = view.texture.source;
      shader.resources.uTexture = source;
      shader.resources.uSampler = source.style;
    }
    shader.groups[0] = renderer.globalUniforms.bindGroup;
    shader.groups[1] = meshPipe.localUniformsBindGroup;
    renderer.encoder.draw({
      geometry: view._geometry,
      shader,
      state
    });
  }
  destroy() {
    this._shader.destroy(true);
    this._shader = null;
  }
}
GlMeshAdaptor.extension = {
  type: [
    ExtensionType.WebGLPipesAdaptor
  ],
  name: "mesh"
};

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