"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _shadertools = require("@luma.gl/shadertools");

var _screenPass = _interopRequireDefault(require("../passes/screen-pass"));

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

var PostProcessEffect = function () {
  function PostProcessEffect(module) {
    var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck2.default)(this, PostProcessEffect);
    this.id = void 0;
    this.props = void 0;
    this.module = void 0;
    this.passes = void 0;
    this.id = "".concat(module.name, "-pass");
    this.props = props;
    (0, _shadertools.normalizeShaderModule)(module);
    this.module = module;
  }

  var _proto = PostProcessEffect.prototype;

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

  _proto.preRender = function preRender() {};

  _proto.postRender = function postRender(device, params) {
    var passes = this.passes || createPasses(device, this.module, this.id);
    this.passes = passes;
    var target = params.target;
    var inputBuffer = params.inputBuffer;
    var outputBuffer = params.swapBuffer;

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

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

    return inputBuffer;
  };

  _proto.cleanup = function cleanup() {
    if (this.passes) {
      var _iterator = _createForOfIteratorHelper(this.passes),
          _step;

      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var pass = _step.value;
          pass.delete();
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }

      this.passes = undefined;
    }
  };

  return PostProcessEffect;
}();

exports.default = PostProcessEffect;

function createPasses(device, module, id) {
  if (!module.passes) {
    var fs = getFragmentShaderForRenderPass(module);
    var pass = new _screenPass.default(device, {
      id: id,
      module: module,
      fs: fs
    });
    return [pass];
  }

  return module.passes.map(function (pass, index) {
    var fs = getFragmentShaderForRenderPass(module, pass);
    var idn = "".concat(id, "-").concat(index);
    return new _screenPass.default(device, {
      id: idn,
      module: module,
      fs: fs
    });
  });
}

var FILTER_FS_TEMPLATE = function FILTER_FS_TEMPLATE(func) {
  return "#version 300 es\nuniform sampler2D texture;\nuniform vec2 texSize;\n\nin vec2 position;\nin vec2 coordinate;\nin vec2 uv;\n\nout vec4 fragColor;\n\nvoid main() {\n  vec2 texCoord = coordinate;\n\n  fragColor = texture(texture, texCoord);\n  fragColor = ".concat(func, "(fragColor, texSize, texCoord);\n}\n");
};

var SAMPLER_FS_TEMPLATE = function SAMPLER_FS_TEMPLATE(func) {
  return "#version 300 es\nuniform sampler2D texture;\nuniform vec2 texSize;\n\nin vec2 position;\nin vec2 coordinate;\nin vec2 uv;\n\nout vec4 fragColor;\n\nvoid main() {\n  vec2 texCoord = coordinate;\n\n  fragColor = ".concat(func, "(texture, texSize, texCoord);\n}\n");
};

function getFragmentShaderForRenderPass(module) {
  var pass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : module;

  if (pass.filter) {
    var func = typeof pass.filter === 'string' ? pass.filter : "".concat(module.name, "_filterColor");
    return FILTER_FS_TEMPLATE(func);
  }

  if (pass.sampler) {
    var _func = typeof pass.sampler === 'string' ? pass.sampler : "".concat(module.name, "_sampleColor");

    return SAMPLER_FS_TEMPLATE(_func);
  }

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