import { ExtensionType } from '../../../extensions/Extensions.mjs';
import { color32BitToUniform } from '../../graphics/gpu/colorToUniform.mjs';
import { compileHighShaderGpuProgram } 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 GpuMeshAdapter {
  init() {
    const gpuProgram = compileHighShaderGpuProgram({
      name: "mesh",
      bits: [
        localUniformBitGl,
        textureBitGl
      ]
    });
    this._shader = new Shader({
      gpuProgram,
      resources: {
        uTexture: Texture.EMPTY._source,
        uSampler: 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();
    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: [
    ExtensionType.WebGPUPipesAdaptor
  ],
  name: "mesh"
};

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