"use strict";

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

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

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

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

var _debug = _interopRequireDefault(require("../debug"));

var _drawLayersPass = _interopRequireDefault(require("../passes/draw-layers-pass"));

var _pickLayersPass = _interopRequireDefault(require("../passes/pick-layers-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; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var TRACE_RENDER_LAYERS = 'deckRenderer.renderLayers';

var DeckRenderer = function () {
  function DeckRenderer(device) {
    (0, _classCallCheck2.default)(this, DeckRenderer);
    this.device = void 0;
    this.gl = void 0;
    this.layerFilter = void 0;
    this.drawPickingColors = void 0;
    this.drawLayersPass = void 0;
    this.pickLayersPass = void 0;
    this.renderCount = void 0;
    this._needsRedraw = void 0;
    this.renderBuffers = void 0;
    this.lastPostProcessEffect = void 0;
    this.device = device;
    this.gl = device.gl;
    this.layerFilter = null;
    this.drawPickingColors = false;
    this.drawLayersPass = new _drawLayersPass.default(device);
    this.pickLayersPass = new _pickLayersPass.default(device);
    this.renderCount = 0;
    this._needsRedraw = 'Initial render';
    this.renderBuffers = [];
    this.lastPostProcessEffect = null;
  }

  var _proto = DeckRenderer.prototype;

  _proto.setProps = function setProps(props) {
    if (this.layerFilter !== props.layerFilter) {
      this.layerFilter = props.layerFilter;
      this._needsRedraw = 'layerFilter changed';
    }

    if (this.drawPickingColors !== props.drawPickingColors) {
      this.drawPickingColors = props.drawPickingColors;
      this._needsRedraw = 'drawPickingColors changed';
    }
  };

  _proto.renderLayers = function renderLayers(opts) {
    if (!opts.viewports.length) {
      return;
    }

    var layerPass = this.drawPickingColors ? this.pickLayersPass : this.drawLayersPass;

    var renderOpts = _objectSpread({
      layerFilter: this.layerFilter,
      isPicking: this.drawPickingColors
    }, opts);

    if (renderOpts.effects) {
      this._preRender(renderOpts.effects, renderOpts);
    }

    var outputBuffer = this.lastPostProcessEffect ? this.renderBuffers[0] : renderOpts.target;
    var renderStats = layerPass.render(_objectSpread(_objectSpread({}, renderOpts), {}, {
      target: outputBuffer
    }));

    if (renderOpts.effects) {
      this._postRender(renderOpts.effects, renderOpts);
    }

    this.renderCount++;
    (0, _debug.default)(TRACE_RENDER_LAYERS, this, renderStats, opts);
  };

  _proto.needsRedraw = function needsRedraw() {
    var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
      clearRedrawFlags: false
    };
    var redraw = this._needsRedraw;

    if (opts.clearRedrawFlags) {
      this._needsRedraw = false;
    }

    return redraw;
  };

  _proto.finalize = function finalize() {
    var renderBuffers = this.renderBuffers;

    var _iterator = _createForOfIteratorHelper(renderBuffers),
        _step;

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

    renderBuffers.length = 0;
  };

  _proto._preRender = function _preRender(effects, opts) {
    this.lastPostProcessEffect = null;
    opts.preRenderStats = opts.preRenderStats || {};

    var _iterator2 = _createForOfIteratorHelper(effects),
        _step2;

    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var effect = _step2.value;
        opts.preRenderStats[effect.id] = effect.preRender(this.device, opts);

        if (effect.postRender) {
          this.lastPostProcessEffect = effect.id;
        }
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }

    if (this.lastPostProcessEffect) {
      this._resizeRenderBuffers();
    }
  };

  _proto._resizeRenderBuffers = function _resizeRenderBuffers() {
    var renderBuffers = this.renderBuffers;

    if (renderBuffers.length === 0) {
      renderBuffers.push(this.device.createFramebuffer({
        colorAttachments: ['rgba8unorm']
      }), this.device.createFramebuffer({
        colorAttachments: ['rgba8unorm']
      }));
    }

    var _iterator3 = _createForOfIteratorHelper(renderBuffers),
        _step3;

    try {
      for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
        var buffer = _step3.value;
        buffer.resize();
      }
    } catch (err) {
      _iterator3.e(err);
    } finally {
      _iterator3.f();
    }
  };

  _proto._postRender = function _postRender(effects, opts) {
    var renderBuffers = this.renderBuffers;

    var params = _objectSpread(_objectSpread({}, opts), {}, {
      inputBuffer: renderBuffers[0],
      swapBuffer: renderBuffers[1]
    });

    var _iterator4 = _createForOfIteratorHelper(effects),
        _step4;

    try {
      for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
        var effect = _step4.value;

        if (effect.postRender) {
          if (effect.id === this.lastPostProcessEffect) {
            params.target = opts.target;
            effect.postRender(this.device, params);
            break;
          }

          var buffer = effect.postRender(this.device, params);
          params.inputBuffer = buffer;
          params.swapBuffer = buffer === renderBuffers[0] ? renderBuffers[1] : renderBuffers[0];
        }
      }
    } catch (err) {
      _iterator4.e(err);
    } finally {
      _iterator4.f();
    }
  };

  return DeckRenderer;
}();

exports.default = DeckRenderer;
//# sourceMappingURL=deck-renderer.js.map