'use strict';

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

var Extensions = require('../../../extensions/Extensions.js');
var Matrix = require('../../../maths/Matrix.js');
var _const = require('../../batcher/shared/const.js');
var compileHighShaderToProgram = require('../../high-shader/compileHighShaderToProgram.js');
var colorBit = require('../../high-shader/shader-bits/colorBit.js');
var generateTextureBatchBit = require('../../high-shader/shader-bits/generateTextureBatchBit.js');
var localUniformBit = require('../../high-shader/shader-bits/localUniformBit.js');
var batchSamplersUniformGroup = require('../../renderers/gl/shader/batchSamplersUniformGroup.js');
var Shader = require('../../renderers/shared/shader/Shader.js');
var UniformGroup = require('../../renderers/shared/shader/UniformGroup.js');
var colorToUniform = require('../gpu/colorToUniform.js');

class GlGraphicsAdaptor {
  init() {
    const uniforms = new UniformGroup.UniformGroup({
      uColor: { value: new Float32Array([1, 1, 1, 1]), type: "vec4<f32>" },
      uTransformMatrix: { value: new Matrix.Matrix(), type: "mat3x3<f32>" }
    });
    const glProgram = compileHighShaderToProgram.compileHighShaderGlProgram({
      name: "graphics",
      bits: [
        colorBit.colorBitGl,
        generateTextureBatchBit.generateTextureBatchBitGl(_const.MAX_TEXTURES),
        localUniformBit.localUniformBitGl
      ]
    });
    this._shader = new Shader.Shader({
      glProgram,
      resources: {
        localUniforms: uniforms,
        batchSamplers: batchSamplersUniformGroup.batchSamplersUniformGroup
      }
    });
  }
  execute(graphicsPipe, renderable) {
    const context = renderable.view.context;
    const shader = context.customShader || this._shader;
    const renderer = graphicsPipe.renderer;
    const contextSystem = renderer.graphicsContext;
    if (!contextSystem.updateGpuContext(context).batches.length) {
      return;
    }
    const {
      geometry,
      instructions
    } = contextSystem.getContextRenderData(context);
    const state = graphicsPipe.state;
    state.blendMode = renderable.layerBlendMode;
    renderer.state.set(graphicsPipe.state);
    const localUniforms = shader.resources.localUniforms.uniforms;
    localUniforms.uTransformMatrix = renderable.layerTransform;
    colorToUniform.color32BitToUniform(
      renderable.layerColor,
      localUniforms.uColor,
      0
    );
    renderer.shader.bind(shader);
    renderer.shader.bindUniformBlock(renderer.globalUniforms.uniformGroup, "globalUniforms");
    renderer.geometry.bind(geometry, shader.glProgram);
    const batches = instructions.instructions;
    for (let i = 0; i < instructions.instructionSize; i++) {
      const batch = batches[i];
      if (batch.size) {
        for (let j = 0; j < batch.textures.textures.length; j++) {
          renderer.texture.bind(batch.textures.textures[j], j);
        }
        renderer.geometry.draw("triangle-list", batch.size, batch.start);
      }
    }
  }
  destroy() {
    this._shader.destroy(true);
    this._shader = null;
  }
}
/** @ignore */
GlGraphicsAdaptor.extension = {
  type: [
    Extensions.ExtensionType.WebGLPipesAdaptor
  ],
  name: "graphics"
};

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