"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.glArrayFromType = glArrayFromType;
exports.getBufferAttributeLayout = getBufferAttributeLayout;
exports.getStride = getStride;
exports.bufferLayoutEqual = bufferLayoutEqual;
exports.getGLTypeFromTypedArray = getGLTypeFromTypedArray;

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

var _constants = require("@luma.gl/constants");

var _glTypeToBufferFormat;

function glArrayFromType(glType) {
  switch (glType) {
    case 5126:
      return Float32Array;

    case 5130:
      return Float64Array;

    case 5123:
    case 33635:
    case 32819:
    case 32820:
      return Uint16Array;

    case 5125:
      return Uint32Array;

    case 5121:
      return Uint8ClampedArray;

    case 5120:
      return Int8Array;

    case 5122:
      return Int16Array;

    case 5124:
      return Int32Array;

    default:
      throw new Error('Unknown GL type');
  }
}

var glTypeToBufferFormat = (_glTypeToBufferFormat = {}, (0, _defineProperty2.default)(_glTypeToBufferFormat, 5126, 'float32'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5130, 'float32'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5123, 'uint16'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 33635, 'uint16'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 32819, 'uint16'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 32820, 'uint16'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5125, 'uint32'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5121, 'uint8'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5120, 'sint8'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5122, 'sint16'), (0, _defineProperty2.default)(_glTypeToBufferFormat, 5124, 'sint32'), _glTypeToBufferFormat);

function getBufferAttributeLayout(name, accessor) {
  var _accessor$type;

  var type = glTypeToBufferFormat[(_accessor$type = accessor.type) !== null && _accessor$type !== void 0 ? _accessor$type : 5126];

  if (accessor.normalized) {
    type = type.replace('int', 'norm');
  }

  return {
    attribute: name,
    format: accessor.size > 1 ? "".concat(type, "x").concat(accessor.size) : type,
    byteOffset: accessor.offset || 0
  };
}

function getStride(accessor) {
  return accessor.stride || accessor.size * accessor.bytesPerElement;
}

function bufferLayoutEqual(accessor1, accessor2) {
  var _accessor1$type, _accessor2$type;

  return ((_accessor1$type = accessor1.type) !== null && _accessor1$type !== void 0 ? _accessor1$type : 5126) === ((_accessor2$type = accessor2.type) !== null && _accessor2$type !== void 0 ? _accessor2$type : 5126) && accessor1.size === accessor2.size && getStride(accessor1) === getStride(accessor2) && (accessor1.offset || 0) === (accessor2.offset || 0);
}

var ERR_TYPE_DEDUCTION = 'Failed to deduce GL constant from typed array';

function getGLTypeFromTypedArray(arrayOrType) {
  var type = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;

  switch (type) {
    case Float32Array:
      return 5126;

    case Uint16Array:
      return 5123;

    case Uint32Array:
      return 5125;

    case Uint8Array:
      return 5121;

    case Uint8ClampedArray:
      return 5121;

    case Int8Array:
      return 5120;

    case Int16Array:
      return 5122;

    case Int32Array:
      return 5124;

    default:
      throw new Error(ERR_TYPE_DEDUCTION);
  }
}
//# sourceMappingURL=gl-utils.js.map