'use strict';

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

var Extensions = require('../../../extensions/Extensions.js');
var colorToUniform = require('../../graphics/gpu/colorToUniform.js');
var compileHighShaderToProgram = require('../../high-shader/compileHighShaderToProgram.js');
var localUniformBit = require('../../high-shader/shader-bits/localUniformBit.js');
var textureBit = require('../../high-shader/shader-bits/textureBit.js');
var Shader = require('../../renderers/shared/shader/Shader.js');
var Texture = require('../../renderers/shared/texture/Texture.js');

class GlMeshAdaptor {
  init() {
    const glProgram = compileHighShaderToProgram.compileHighShaderGlProgram({
      name: "mesh",
      bits: [
        localUniformBit.localUniformBitGl,
        textureBit.textureBitGl
      ]
    });
    this._shader = new Shader.Shader({
      glProgram,
      resources: {
        uTexture: Texture.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();
    colorToUniform.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: [
    Extensions.ExtensionType.WebGLPipesAdaptor
  ],
  name: "mesh"
};

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