'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 GpuMeshAdapter {
  init() {
    const gpuProgram = compileHighShaderToProgram.compileHighShaderGpuProgram({
      name: "mesh",
      bits: [
        localUniformBit.localUniformBitGl,
        textureBit.textureBitGl
      ]
    });
    this._shader = new Shader.Shader({
      gpuProgram,
      resources: {
        uTexture: Texture.Texture.EMPTY._source,
        uSampler: Texture.Texture.EMPTY._source.style
      }
    });
  }
  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;
      shader.groups[2] = renderer.texture.getTextureBindGroup(view.texture);
    }
    shader.groups[0] = renderer.globalUniforms.bindGroup;
    shader.groups[1] = renderer.renderPipes.uniformBatch.getUniformBindGroup(localUniforms, true);
    renderer.encoder.draw({
      geometry: view._geometry,
      shader,
      state
    });
  }
  destroy() {
    this._shader.destroy(true);
    this._shader = null;
  }
}
/** @ignore */
GpuMeshAdapter.extension = {
  type: [
    Extensions.ExtensionType.WebGPUPipesAdaptor
  ],
  name: "mesh"
};

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