import { normalizeShaderModule } from '@luma.gl/shadertools';
import ScreenPass from '../passes/screen-pass';
export default class PostProcessEffect {
  constructor(module, props = {}) {
    this.id = void 0;
    this.props = void 0;
    this.module = void 0;
    this.passes = void 0;
    this.id = `${module.name}-pass`;
    this.props = props;
    normalizeShaderModule(module);
    this.module = module;
  }

  setProps(props) {
    this.props = props;
  }

  preRender() {}

  postRender(device, params) {
    const passes = this.passes || createPasses(device, this.module, this.id);
    this.passes = passes;
    const {
      target
    } = params;
    let inputBuffer = params.inputBuffer;
    let outputBuffer = params.swapBuffer;

    for (let index = 0; index < this.passes.length; index++) {
      if (target && index === this.passes.length - 1) {
        outputBuffer = target;
      }

      this.passes[index].render({
        inputBuffer,
        outputBuffer,
        moduleSettings: this.props
      });
      const switchBuffer = outputBuffer;
      outputBuffer = inputBuffer;
      inputBuffer = switchBuffer;
    }

    return inputBuffer;
  }

  cleanup() {
    if (this.passes) {
      for (const pass of this.passes) {
        pass.delete();
      }

      this.passes = undefined;
    }
  }

}

function createPasses(device, module, id) {
  if (!module.passes) {
    const fs = getFragmentShaderForRenderPass(module);
    const pass = new ScreenPass(device, {
      id,
      module,
      fs
    });
    return [pass];
  }

  return module.passes.map((pass, index) => {
    const fs = getFragmentShaderForRenderPass(module, pass);
    const idn = `${id}-${index}`;
    return new ScreenPass(device, {
      id: idn,
      module,
      fs
    });
  });
}

const FILTER_FS_TEMPLATE = func => `\
#version 300 es
uniform sampler2D texture;
uniform vec2 texSize;

in vec2 position;
in vec2 coordinate;
in vec2 uv;

out vec4 fragColor;

void main() {
  vec2 texCoord = coordinate;

  fragColor = texture(texture, texCoord);
  fragColor = ${func}(fragColor, texSize, texCoord);
}
`;

const SAMPLER_FS_TEMPLATE = func => `\
#version 300 es
uniform sampler2D texture;
uniform vec2 texSize;

in vec2 position;
in vec2 coordinate;
in vec2 uv;

out vec4 fragColor;

void main() {
  vec2 texCoord = coordinate;

  fragColor = ${func}(texture, texSize, texCoord);
}
`;

function getFragmentShaderForRenderPass(module, pass = module) {
  if (pass.filter) {
    const func = typeof pass.filter === 'string' ? pass.filter : `${module.name}_filterColor`;
    return FILTER_FS_TEMPLATE(func);
  }

  if (pass.sampler) {
    const func = typeof pass.sampler === 'string' ? pass.sampler : `${module.name}_sampleColor`;
    return SAMPLER_FS_TEMPLATE(func);
  }

  return '';
}
//# sourceMappingURL=post-process-effect.js.map